From 230243c0dd96bdbf410627d838c585400505ab7e Mon Sep 17 00:00:00 2001 From: Longsheng Mou Date: Thu, 16 Oct 2025 19:28:36 +0800 Subject: [PATCH 1/2] [mlir][emitc] Fix `emitc.for` verification crash This PR adds block arguments check to prevent a crash in `emitc.for` verifier. --- mlir/lib/Dialect/EmitC/IR/EmitC.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp index 4754f0bfe895e..77a56fb463243 100644 --- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp +++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp @@ -584,6 +584,10 @@ void ForOp::print(OpAsmPrinter &p) { LogicalResult ForOp::verifyRegions() { // Check that the body defines as single block argument for the induction // variable. + if (getBody()->getNumArguments() == 0) + return emitOpError("expected body to have a single block argument for the " + "induction variable"); + if (getInductionVar().getType() != getLowerBound().getType()) return emitOpError( "expected induction variable to be same type as bounds and step"); From 850b4c6fa94a7932a84215b38367904c7c884d34 Mon Sep 17 00:00:00 2001 From: Longsheng Mou Date: Thu, 16 Oct 2025 19:30:26 +0800 Subject: [PATCH 2/2] add test --- mlir/test/Dialect/EmitC/invalid_ops.mlir | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/mlir/test/Dialect/EmitC/invalid_ops.mlir b/mlir/test/Dialect/EmitC/invalid_ops.mlir index 5f594fb08c43f..f30a614a93de0 100644 --- a/mlir/test/Dialect/EmitC/invalid_ops.mlir +++ b/mlir/test/Dialect/EmitC/invalid_ops.mlir @@ -876,3 +876,28 @@ func.func @test_do(%arg0 : !emitc.ptr) { return } + +// ----- + +func.func @test_for_none_block_argument(%arg0: index) { + // expected-error@+1 {{expected body to have a single block argument for the induction variable}} + "emitc.for"(%arg0, %arg0, %arg0) ( + { + emitc.yield + } + ) : (index, index, index) -> () + return +} + +// ----- + +func.func @test_for_unmatch_type(%arg0: index) { + // expected-error@+1 {{expected induction variable to be same type as bounds}} + "emitc.for"(%arg0, %arg0, %arg0) ( + { + ^bb0(%i0 : f32): + emitc.yield + } + ) : (index, index, index) -> () + return +}