[Mlir-commits] [mlir] Fix affine for fold detached block (PR #209111)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Jul 24 00:14:20 PDT 2026


https://github.com/LouisLu060211 updated https://github.com/llvm/llvm-project/pull/209111

>From 3a9b774ca84ccb1395764dac6683a553c6e751e8 Mon Sep 17 00:00:00 2001
From: LouisLu0602 <yaolu0602 at gmail.com>
Date: Tue, 23 Jun 2026 18:02:12 +0800
Subject: [PATCH 1/2] [mlir][memref] Avoid mem2reg crash on element count
 overflow

Avoid crash in test alias analysis operand printing

Fix AffineForOP creash

remove

fix

fix issue
---
 mlir/lib/Dialect/Affine/IR/AffineOps.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
index f095500495f18..544715e6a169d 100644
--- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
+++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
@@ -2596,6 +2596,9 @@ static SmallVector<OpFoldResult> AffineForEmptyLoopFolder(AffineForOp forOp) {
       return {};
     if (iterArgIt == iterArgs.end()) {
       // `val` is defined outside of the loop.
+      if(!val.getParentRegion()){
+        return{};
+      }
       assert(forOp.isDefinedOutsideOfLoop(val) &&
              "must be defined outside of the loop");
       hasValDefinedOutsideLoop = true;

>From da4eb6dd9bdc89e684ed2f79e1733a8cb698e195 Mon Sep 17 00:00:00 2001
From: LouisLu0602 <yaolu0602 at gmail.com>
Date: Fri, 24 Jul 2026 15:10:39 +0800
Subject: [PATCH 2/2] [mlir][affine] Add regression test for fold with detached
 block

---
 mlir/test/Conversion/FuncToLLVM/func-to-llvm.mlir | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/mlir/test/Conversion/FuncToLLVM/func-to-llvm.mlir b/mlir/test/Conversion/FuncToLLVM/func-to-llvm.mlir
index 94dfceadbc449..bbf1c4de63b82 100644
--- a/mlir/test/Conversion/FuncToLLVM/func-to-llvm.mlir
+++ b/mlir/test/Conversion/FuncToLLVM/func-to-llvm.mlir
@@ -583,3 +583,17 @@ module attributes {transform.with_named_sequence} {
     transform.yield
   }
 }
+
+// Regression test: the affine.for empty loop folder used to crash when fold
+// was attempted during dialect conversion, after the old entry block was
+// detached but the loop still yielded one of its arguments. The folder must
+// bail out instead of hitting the isDefinedOutsideOfLoop assertion.
+
+// CHECK-LABEL: @affine_for_fold_detached_block
+// CHECK: affine.for
+func.func @affine_for_fold_detached_block(%arg0: index) -> index {
+  %0 = affine.for %i = 0 to 10 iter_args(%acc = %arg0) -> (index) {
+    affine.yield %arg0 : index
+  }
+  return %0 : index
+}
\ No newline at end of file



More information about the Mlir-commits mailing list