Issue 239318
Summary [mlir] [transform] transform.foreach 验证器在遇到非 transform.yield 终止符时中止
Labels mlir
Assignees
Reporter lin334141-cell
    <html><head></head><body><h1><span><span>[mlir]</span><span>[transform]</span> transform.foreach verifier aborts on a non-transform.yield terminator</span></h1><h2><span>Summary</span></h2><p><code><span>mlir-opt</span></code><span> aborts during verification when the body of a </span><code><span>transform.foreach</span></code><span> operation ends with </span><code><span>func.return</span></code><span> instead of the required </span><code><span>transform.yield</span></code><span>.</span></p><p><span>The input IR is </span><strong><span>invalid</span></strong><span> and should be rejected with a verifier diagnostic. Instead, </span><code><span>ForeachOp::verify()</span></code><span> calls </span><code><span>ForeachOp::getYieldOp()</span></code><span>, which unconditionally casts the region terminator to </span><code><span>transform::YieldOp</span></code><span>:</span></p><pre><code><span>transform::YieldOp transform::ForeachOp::getYieldOp() {
  return cast&lt;transform::YieldOp&gt;(getBody().front().getTerminator());
}</span></code></pre><p><span>This causes a </span><code><span>cast&lt;Ty&gt;() argument of incompatible type!</span></code><span> assertion before the region's </span><code><span>SingleBlockImplicitTerminator</span></code><span> trait can report the invalid terminator.</span></p><p><span>The failure is distinct from #234637, which involves a different unchecked cast in </span><code><span>ForeachOp::getEffects()</span></code><span> on a non-terminator operation.</span></p><h2><span>Minimal Reproducer</span></h2><p><span>Save as </span><code><span>min.mlir</span></code><span> (generic MLIR syntax):</span></p><pre><code><span>"builtin.module"()({
  "builtin.module"()({
    "transform.named_sequence"()&lt;{function_type=(!transform.any_op)-&gt;(),sym_name="__transform_main"}&gt;({
      ^bb0(%arg0:!transform.any_op):
        "transform.foreach"(%arg0)({
        ^bb0(%arg1:!transform.any_op):
          "func.return"():()-&gt;()
        }):(!transform.any_op)-&gt;()
        "transform.yield"():()-&gt;()
    }):()-&gt;()
  }){transform.with_named_sequence}:()-&gt;()
}):()-&gt;()</span></code></pre><p><span>The enclosing </span><code><span>transform.named_sequence</span></code><span> has the correct </span><code><span>transform.yield</span></code><span> terminator. Only the nested </span><code><span>transform.foreach</span></code><span> body has the wrong terminator.</span></p><h3><span>Command</span></h3><pre><code><span>mlir-opt min.mlir --mlir-disable-threading \
  --verify-each --mlir-print-op-generic \
  -o /dev/null</span></code></pre><p><span>No optimization or conversion pass is needed. The failure occurs during parsing/verification.</span></p><h2><span>Actual Behavior</span></h2><p><span>On an assertions-enabled build, </span><code><span>mlir-opt</span></code><span> aborts with </span><code><span>SIGABRT</span></code><span> (shell exit status 134; subprocess return code </span><code><span>-6</span></code><span>).</span></p><pre><code><span>Assertion `isa&lt;To&gt;(Val) &amp;&amp; "cast&lt;Ty&gt;() argument of incompatible type!"' failed.

llvm::cast&lt;mlir::transform::YieldOp&gt;(...)
mlir::transform::ForeachOp::getYieldOp()
mlir::transform::ForeachOp::verify()</span></code></pre><p><span>The original generated program produces the same assertion. Its symbolized stack identifies </span><code><span>ForeachOp::getYieldOp()</span></code><span> and </span><code><span>ForeachOp::verify()</span></code><span>.</span></p><p><span>The minimized reproducer also reproduces the abort on an assertions-enabled LLVM build.</span></p><h2><span>Expected Behavior</span></h2><p><span>The verifier should reject the invalid </span><code><span>transform.foreach</span></code><span> body with a normal diagnostic, for example:</span></p><pre><code><span>'transform.foreach' op expects regions to end with
'transform.yield', found 'func.return'</span></code></pre><p><span>The exact diagnostic wording is not important. Invalid IR should not cause the verifier to abort.</span></p><h2><span>Root Cause</span></h2><p><span>The underlying problem is a verification-ordering issue combined with the unchecked cast in </span><code><span>getYieldOp()</span></code><span>.</span></p><p><span>In </span><code><span>mlir/lib/Dialect/Transform/IR/TransformOps.cpp</span></code><span>, </span><code><span>ForeachOp::verify()</span></code><span> accesses:</span></p><pre><code><span>for (auto <span>[resultOpt, yieldOperandOpt]</span> :
     llvm::zip_longest(getResults(), getYieldOp().getOperands())) {
  // ...
}</span></code></pre><p><span>However, </span><code><span>getYieldOp()</span></code><span> assumes that the last operation in the body is always </span><code><span>transform.yield</span></code><span>.</span></p><p><span>The operation has the </span><code><span>SingleBlockImplicitTerminator&lt;"::mlir::transform::YieldOp"&gt;</span></code><span> trait, which can diagnose an incorrect terminator, but the relevant region-trait verification happens after the operation's custom verifier:</span></p><ol start="1"><li><p><code><span>verifyInvariants()</span></code><span> invokes the custom </span><code><span>ForeachOp::verify()</span></code><span>.</span></p></li><li><p><span>The custom verifier calls </span><code><span>getYieldOp()</span></code><span>.</span></p></li><li><p><code><span>getYieldOp()</span></code><span> attempts </span><code><span>cast&lt;transform::YieldOp&gt;</span></code><span> on </span><code><span>func.return</span></code><span>.</span></p></li><li><p><span>The cast asserts before </span><code><span>verifyRegionInvariants()</span></code><span> can emit the region-trait diagnostic.</span></p></li></ol><p><span>Relevant implementation locations at the tested revision:</span></p><ul><li><p><code><span>TransformOps.cpp</span></code><span>: </span><code><span>ForeachOp::getYieldOp()</span></code><span> and </span><code><span>ForeachOp::verify()</span></code><span>.</span></p></li><li><p><code><span>OpDefinition.h</span></code><span>: custom verifier invocation through </span><code><span>verifyInvariants()</span></code><span>.</span></p></li><li><p><code><span>Verifier.cpp</span></code><span>: region-invariant verification after the operation's nested regions have been checked.</span></p></li></ul><p><span>This is a verifier robustness defect, not a miscompilation of legal source IR.</span></p><h2><span>Relation to Existing Issue #234637 and PR #236566</span></h2><p><span>Although both issues involve </span><code><span>transform.foreach</span></code><span>, they have different trigger conditions and failure locations.</span></p>
  | #234637 | This Issue
-- | -- | --
Failing function | ForeachOp::getEffects() | ForeachOp::getYieldOp()
Invalid operation | Non-transform operation inside the body | Wrong region terminator
Example trigger | arith.constant | func.return
Terminator | Valid transform.yield | Invalid func.return
Failing cast | cast<TransformOpInterface> | cast<YieldOp>

<p><span>PR #236566 addresses the first problem by using </span><code><span>dyn_cast</span></code><span> in </span><code><span>getEffects()</span></code><span> and adding verification of body operations.</span></p><p><span>However, the new verification loop iterates over </span><code><span>without_terminator()</span></code><span>, so it excludes the terminator itself. The PR does not change </span><code><span>getYieldOp()</span></code><span>.</span></p><p><span>Consequently, the wrong-terminator case is not covered by that proposed fix and can still reach the unchecked cast in </span><code><span>ForeachOp::verify()</span></code><span>.</span></p><p><span>Related links:</span></p><ul><li><p><a href="" href="" Fix Direction</span></h2><p><span>Check the terminator type before calling </span><code><span>getYieldOp()</span></code><span> in </span><code><span>ForeachOp::verify()</span></code><span>.</span></p><p><span>For example, the verifier could test whether the terminator implements </span><code><span>transform::YieldOp</span></code><span> and emit an operation error if it does not.</span></p><p><span>Alternatively, the required region terminator invariant could be checked before the custom verifier accesses the terminator through a typed accessor.</span></p><p><span>The important requirement is that malformed input is rejected with a diagnostic instead of reaching an unchecked cast.</span></p><h2><span>Tested Versions</span></h2><ul><li><p><strong><span>Reproduced:</span></strong><span> LLVM </span><code><span>24.0.0git</span></code><span>, commit </span><code><span>67005b724c917e1bdca50c82ecaa1b21a69244ed</span></code><span>, assertions-enabled Linux x86_64 </span><code><span>mlir-opt</span></code><span> (original generated program).</span></p></li><li><p><strong><span>Reproduced:</span></strong><span> LLVM </span><code><span>main</span></code><span> at </span><code><span>61817b48e757f9a07f2a84e36326542052c12c22</span></code><span>, Release + assertions, macOS arm64 (original and minimized inputs).</span></p></li><li><p><strong><span>Source inspection only:</span></strong><span> LLVM </span><code><span>main</span></code><span> at </span><code><span>ae02bf2a525c599d6d566f4eb9312560d7aa7589</span></code><span> (2026-11-06). The unchecked </span><code><span>getYieldOp()</span></code><span> cast and its call from </span><code><span>ForeachOp::verify()</span></code><span> remain present. This revision was not built or executed for this report.</span></p></li></ul><h2><span>Context</span></h2><p><span>Found by the SEMA MLIR compiler-testing pipeline and reduced from a generated program. The original failure and the minimized reproducer exercise the same verifier assertion.</span></p><p><span>The report concerns graceful rejection of malformed IR; no silent wrong-answer behavior is claimed.</span></p></body></html>
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to