Skip to content

Commit f725f84

Browse files
authored
[mlir][emitc] Fix emitc.for verification crash (#163754)
This PR adds block arguments check to prevent a crash in `emitc.for` verifier. Fixes #159950.
1 parent 3252e11 commit f725f84

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

mlir/lib/Dialect/EmitC/IR/EmitC.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,10 @@ void ForOp::print(OpAsmPrinter &p) {
584584
LogicalResult ForOp::verifyRegions() {
585585
// Check that the body defines as single block argument for the induction
586586
// variable.
587+
if (getBody()->getNumArguments() != 1)
588+
return emitOpError("expected body to have a single block argument for the "
589+
"induction variable");
590+
587591
if (getInductionVar().getType() != getLowerBound().getType())
588592
return emitOpError(
589593
"expected induction variable to be same type as bounds and step");

mlir/test/Dialect/EmitC/invalid_ops.mlir

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,3 +876,41 @@ func.func @test_do(%arg0 : !emitc.ptr<i32>) {
876876

877877
return
878878
}
879+
880+
// -----
881+
882+
func.func @test_for_none_block_argument(%arg0: index) {
883+
// expected-error@+1 {{expected body to have a single block argument for the induction variable}}
884+
"emitc.for"(%arg0, %arg0, %arg0) (
885+
{
886+
emitc.yield
887+
}
888+
) : (index, index, index) -> ()
889+
return
890+
}
891+
892+
// -----
893+
894+
func.func @test_for_more_than_one_block_argument(%arg0: index) {
895+
// expected-error@+1 {{expected body to have a single block argument for the induction variable}}
896+
"emitc.for"(%arg0, %arg0, %arg0) (
897+
{
898+
^bb0(%i0 : index, %i1 : index):
899+
emitc.yield
900+
}
901+
) : (index, index, index) -> ()
902+
return
903+
}
904+
905+
// -----
906+
907+
func.func @test_for_unmatch_type(%arg0: index) {
908+
// expected-error@+1 {{expected induction variable to be same type as bounds}}
909+
"emitc.for"(%arg0, %arg0, %arg0) (
910+
{
911+
^bb0(%i0 : f32):
912+
emitc.yield
913+
}
914+
) : (index, index, index) -> ()
915+
return
916+
}

0 commit comments

Comments
 (0)