[Mlir-commits] [mlir] [mlir][emitc] Fix emitc.for verification crash (PR #163754)
Longsheng Mou
llvmlistbot at llvm.org
Fri Oct 31 21:10:15 PDT 2025
https://github.com/CoTinker updated https://github.com/llvm/llvm-project/pull/163754
>From 5322f72f5fa6d36b0070fad9ebb36454b8a7a04d Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Thu, 16 Oct 2025 19:28:36 +0800
Subject: [PATCH 1/3] [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 0992ce14b4afb..bae1e5202975f 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 3ccbe53df43ea1c84062b37c5941c2d13a62a68b Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Thu, 16 Oct 2025 19:30:26 +0800
Subject: [PATCH 2/3] 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<i32>) {
return
}
+
+// -----
+
+func.func @test_for_none_block_argument(%arg0: index) {
+ // expected-error at +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 at +1 {{expected induction variable to be same type as bounds}}
+ "emitc.for"(%arg0, %arg0, %arg0) (
+ {
+ ^bb0(%i0 : f32):
+ emitc.yield
+ }
+ ) : (index, index, index) -> ()
+ return
+}
>From 4feaf0173f5fdf43066a560e1103df654e20da31 Mon Sep 17 00:00:00 2001
From: Longsheng Mou <longshengmou at gmail.com>
Date: Sat, 1 Nov 2025 12:09:07 +0800
Subject: [PATCH 3/3] fix review
---
mlir/lib/Dialect/EmitC/IR/EmitC.cpp | 2 +-
mlir/test/Dialect/EmitC/invalid_ops.mlir | 13 +++++++++++++
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
index bae1e5202975f..d478220221f7a 100644
--- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
+++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
@@ -584,7 +584,7 @@ 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)
+ if (getBody()->getNumArguments() != 1)
return emitOpError("expected body to have a single block argument for the "
"induction variable");
diff --git a/mlir/test/Dialect/EmitC/invalid_ops.mlir b/mlir/test/Dialect/EmitC/invalid_ops.mlir
index f30a614a93de0..f285196d466ce 100644
--- a/mlir/test/Dialect/EmitC/invalid_ops.mlir
+++ b/mlir/test/Dialect/EmitC/invalid_ops.mlir
@@ -891,6 +891,19 @@ func.func @test_for_none_block_argument(%arg0: index) {
// -----
+func.func @test_for_more_than_one_block_argument(%arg0: index) {
+ // expected-error at +1 {{expected body to have a single block argument for the induction variable}}
+ "emitc.for"(%arg0, %arg0, %arg0) (
+ {
+ ^bb0(%i0 : index, %i1 : index):
+ emitc.yield
+ }
+ ) : (index, index, index) -> ()
+ return
+}
+
+// -----
+
func.func @test_for_unmatch_type(%arg0: index) {
// expected-error at +1 {{expected induction variable to be same type as bounds}}
"emitc.for"(%arg0, %arg0, %arg0) (
More information about the Mlir-commits
mailing list