[Mlir-commits] [mlir] 4d777c1 - [mlir] Handle null region in LoopLikeOpInterface::isDefinedOutsideOfLoop (#204521)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Jul 23 08:17:15 PDT 2026


Author: Prem C
Date: 2026-07-23T08:17:10-07:00
New Revision: 4d777c139fc23303e8fc5cc9a78069aef94ab0ab

URL: https://github.com/llvm/llvm-project/commit/4d777c139fc23303e8fc5cc9a78069aef94ab0ab
DIFF: https://github.com/llvm/llvm-project/commit/4d777c139fc23303e8fc5cc9a78069aef94ab0ab.diff

LOG: [mlir] Handle null region in LoopLikeOpInterface::isDefinedOutsideOfLoop (#204521)

Fixes #203860 

In LoopLikeOpInterface::isDefinedOutsideOfLoop default implementation,
value.getParentRegion() can return null during signature conversion
rollbacks when blocks/ops are unlinked. Check for null region to avoid a
segmentation fault.

Also, add a regression test for convert-func-to-llvm with
index-bitwidth=32 on functions with affine.for loops.

Added: 
    

Modified: 
    mlir/include/mlir/IR/Region.h
    mlir/include/mlir/Interfaces/LoopLikeInterface.td
    mlir/test/Conversion/FuncToLLVM/func-to-llvm.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/Region.h b/mlir/include/mlir/IR/Region.h
index fa9d8f08d84bb..a61eaaa54d401 100644
--- a/mlir/include/mlir/IR/Region.h
+++ b/mlir/include/mlir/IR/Region.h
@@ -212,6 +212,9 @@ class Region {
   /// Return the parent operation this region is attached to.
   Operation *getParentOp() { return container; }
 
+  /// Return true if this region is attached to an operation.
+  bool isAttached() { return container != nullptr; }
+
   /// Find the first parent operation of the given type, or nullptr if there is
   /// no ancestor operation.
   template <typename ParentT>

diff  --git a/mlir/include/mlir/Interfaces/LoopLikeInterface.td b/mlir/include/mlir/Interfaces/LoopLikeInterface.td
index 5fb897339ffde..0bb82c1e4638e 100644
--- a/mlir/include/mlir/Interfaces/LoopLikeInterface.td
+++ b/mlir/include/mlir/Interfaces/LoopLikeInterface.td
@@ -62,7 +62,8 @@ def LoopLikeOpInterface : OpInterface<"LoopLikeOpInterface"> {
       /*args=*/(ins "::mlir::Value ":$value),
       /*methodBody=*/"",
       /*defaultImplementation=*/[{
-        return !$_op->isAncestor(value.getParentRegion()->getParentOp());
+        ::mlir::Region *region = value.getParentRegion();
+        return !region || !region->isAttached() || !$_op->isAncestor(region->getParentOp());
       }]
     >,
     InterfaceMethod<[{

diff  --git a/mlir/test/Conversion/FuncToLLVM/func-to-llvm.mlir b/mlir/test/Conversion/FuncToLLVM/func-to-llvm.mlir
index 94dfceadbc449..6a04960e3513a 100644
--- a/mlir/test/Conversion/FuncToLLVM/func-to-llvm.mlir
+++ b/mlir/test/Conversion/FuncToLLVM/func-to-llvm.mlir
@@ -583,3 +583,18 @@ module attributes {transform.with_named_sequence} {
     transform.yield
   }
 }
+
+// During signature conversion with index-bitwidth=32, pattern rollback can
+// unlink blocks/ops from their regions. If loop-like operations are folded
+// in this state, querying `isDefinedOutsideOfLoop` on block arguments
+// of unlinked blocks would previously dereference a null parent region pointer
+// and crash.
+// See: https://github.com/llvm/llvm-project/issues/203860
+// CHECK32-LABEL: llvm.func @affine_for_index_bitwidth_32
+func.func @affine_for_index_bitwidth_32(%arg0: index, %arg1: index, %arg2: index, %arg3: index) {
+  %0 = affine.for %arg4 = 0 to 101 iter_args(%arg5 = %arg1) -> (index) {
+    affine.yield %arg3 : index
+  }
+  return
+}
+


        


More information about the Mlir-commits mailing list