[Mlir-commits] [mlir] [mlir][transform] Fix crash in transform.get_parent_op. (PR #66492)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Sep 15 03:52:04 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir
            
<details>
<summary>Changes</summary>
The previous implementation crashed if run on a `builtin.module` using an `op_name` filter (because the initial value of `parent` in the while loop was a `nullptr`). This PR fixes the crash and adds a test.
--
Full diff: https://github.com/llvm/llvm-project/pull/66492.diff

2 Files Affected:

- (modified) mlir/lib/Dialect/Transform/IR/TransformOps.cpp (+3-2) 
- (modified) mlir/test/Dialect/Transform/test-interpreter.mlir (+12) 


<pre>
diff --git a/mlir/lib/Dialect/Transform/IR/TransformOps.cpp b/mlir/lib/Dialect/Transform/IR/TransformOps.cpp
index de3cd1b28e435bc..f1d07b85adb7576 100644
--- a/mlir/lib/Dialect/Transform/IR/TransformOps.cpp
+++ b/mlir/lib/Dialect/Transform/IR/TransformOps.cpp
@@ -1233,7 +1233,7 @@ transform::GetParentOp::apply(transform::TransformRewriter &amp;rewriter,
   DenseSet&lt;Operation *&gt; resultSet;
   for (Operation *target : state.getPayloadOps(getTarget())) {
     Operation *parent = target-&gt;getParentOp();
-    do {
+    while (parent) {
       bool checkIsolatedFromAbove =
           !getIsolatedFromAbove() ||
           parent-&gt;hasTrait&lt;OpTrait::IsIsolatedFromAbove&gt;();
@@ -1241,7 +1241,8 @@ transform::GetParentOp::apply(transform::TransformRewriter &amp;rewriter,
                          parent-&gt;getName().getStringRef() == *getOpName();
       if (checkIsolatedFromAbove &amp;&amp; checkOpName)
         break;
-    } while ((parent = parent-&gt;getParentOp()));
+      parent = parent-&gt;getParentOp();
+    }
     if (!parent) {
       DiagnosedSilenceableFailure diag =
           emitSilenceableError()
diff --git a/mlir/test/Dialect/Transform/test-interpreter.mlir b/mlir/test/Dialect/Transform/test-interpreter.mlir
index 68e3a4851539690..91a283c799941bb 100644
--- a/mlir/test/Dialect/Transform/test-interpreter.mlir
+++ b/mlir/test/Dialect/Transform/test-interpreter.mlir
@@ -1891,6 +1891,18 @@ transform.sequence failures(propagate) {
   test_print_number_of_associated_payload_ir_ops %4 : !transform.any_op
 }
 
+
+// -----
+
+// expected-note @below {{target op}}
+module {
+  transform.sequence  failures(propagate) {
+  ^bb0(%arg0: !pdl.operation):
+    // expected-error @below{{could not find a parent op that matches all requirements}}
+    %3 = get_parent_op %arg0 {op_name = &quot;builtin.module&quot;} : (!pdl.operation) -&gt; !transform.any_op
+  }
+}
+
 // -----
 
 func.func @cast(%arg0: f32) -&gt; f64 {
</pre>
</details>


https://github.com/llvm/llvm-project/pull/66492


More information about the Mlir-commits mailing list