Issue 239319
Summary [mlir][DialectConversion] applyPartialConversion hangs on a self-referential unrealized_conversion_cast
Labels mlir
Assignees
Reporter lin334141-cell
    <html><head></head><body><h1><span><span>[mlir]</span><span>[DialectConversion]</span> applyPartialConversion hangs on a self-referential unrealized_conversion_cast</span></h1><h2><span>Summary</span></h2><p><code><span>mlir-opt</span></code><span> hangs when running </span><code><span>lower-affine</span></code><span> on a module containing a self-referential </span><code><span>builtin.unrealized_conversion_cast</span></code><span> inside a graph region.</span></p><p><span>The input is accepted by the MLIR parser and verifier, but the pass enters a non-terminating lookup loop in </span><code><span>ConversionPatternRewriterImpl::lookupOrDefault</span></code><span> (</span><code><span>mlir/lib/Transforms/Utils/DialectConversion.cpp</span></code><span>).</span></p><p><span>The underlying problem is a self-mapping materialization:</span></p><pre><code><span>%0 = "builtin.unrealized_conversion_cast"(%0)
     : (i32) -&gt; i32</span></code></pre><p><span>During dialect conversion, the materialization lookup can map the value back to itself. </span><code><span>lookupOrDefault()</span></code><span> repeatedly follows this non-empty mapping without checking whether progress was made or whether the mapping chain contains a cycle.</span></p><p><span>The observed result is sustained CPU activity with no output. The process does not return before the configured execution timeout.</span></p><p><span>This is a distinct code path from the previously reported canonicalizer hang in #236562 and the self-referential Cast assertion in #194824.</span></p><h2><span>Minimal Reproducer</span></h2><p><span>Save the following exact generic MLIR as </span><code><span>min.mlir</span></code><span>:</span></p><pre><code><span>"builtin.module"()({"test.graph_region"()({%0="builtin.unrealized_conversion_cast"(%0):(i32)-&gt;i32"test.return"():()-&gt;()}):()-&gt;()}):()-&gt;()</span></code></pre><p><strong><span>Build requirement:</span></strong><span> This reproducer uses </span><code><span>test.graph_region</span></code><span> from MLIR's internal Test dialect.</span></p><p><span>Use an assertions-enabled </span><code><span>mlir-opt</span></code><span> built with </span><code><span>MLIR_INCLUDE_TESTS=ON</span></code><span>. The tested local build used Release + assertions with this option enabled, which registers the Test dialect in </span><code><span>mlir-opt</span></code><span>.</span></p><p><span>A normal distribution build without the Test dialect may reject </span><code><span>test.graph_region</span></code><span> before reaching the problematic conversion code. Such a parser rejection is not a reproduction of this hang.</span></p><p><span>The </span><code><span>test.graph_region</span></code><span> operation provides a graph region in which the forward/self-reference is accepted. No affine operation is required inside the minimal reproducer.</span></p><h3><span>Command</span></h3><pre><code><span>mlir-opt min.mlir \
  --mlir-disable-threading \
  --verify-each \
  --mlir-print-op-generic \
  --pass-pipeline='builtin.module(lower-affine)' \
  -o out.mlir</span></code></pre><p><span>Run the command under an external process timeout (for example, 12 seconds) to avoid leaving the hung process running indefinitely.</span></p><h2><span>Actual Behavior</span></h2><p><span>The pass does not terminate before the configured timeout. It produces no completed output IR, and stdout/stderr remain empty.</span></p><h3><span>Original Generated Program</span></h3><p><span>On an LLVM </span><code><span>24.0.0git</span></code><span> Linux x86_64 build at commit </span><code><span>67005b724c917e1bdca50c82ecaa1b21a69244ed</span></code><span>:</span></p><ul><li><p><span>Pass: </span><code><span>lower-affine</span></code></p></li><li><p><span>Configured tool timeout: 45 seconds</span></p></li><li><p><span>Measured wall time, including monitoring/termination: </span><strong><span>47.263 seconds</span></strong></p></li><li><p><span>Measured CPU time: </span><strong><span>23.822 seconds</span></strong></p></li><li><p><span>Runner result: </span><code><span>TIMEOUT</span></code><span>, return code </span><code><span>-9</span></code><span> after termination</span></p></li><li><p><span>stdout/stderr: empty</span></p></li><li><p><span>Process: reaped, with no remaining descendants</span></p></li></ul><p><span>The program passed source parsing/verification before the pass was invoked.</span></p><h3><span>Minimized Reproduction</span></h3><p><span>On LLVM </span><code><span>main</span></code><span> at </span><code><span>61817b48e757f9a07f2a84e36326542052c12c22</span></code><span> (Release + assertions, macOS arm64, </span><code><span>MLIR_INCLUDE_TESTS=ON</span></code><span>):</span></p>
Input | Pass | Wall limit | Result
-- | -- | -- | --
Original generated program | lower-affine | 12 s | TIMEOUT, killed
Cast-only reduction | lower-affine | 12 s | TIMEOUT, killed
Minimal graph-region Cast above | lower-affine | 12 s | TIMEOUT, killed
Affine-only control, with Cast region removed | lower-affine | — | Exit 0, about 0.053 s

<p><span>These bounded runs reproduce the hang and show that the self-referential Cast region is necessary for the observed failure.</span></p><p><span>The timeout observations alone do not constitute a mathematical proof of nontermination; the source-level loop analysis below explains the lack of progress.</span></p><h2><span>Sampling Evidence</span></h2><p><span>While the cast-only reduction was hung, the process was sampled for approximately 3 seconds with the macOS </span><code><span>sample</span></code><span> utility.</span></p><p><span>All </span><strong><span>2504/2504 stack samples</span></strong><span> were inside </span><code><span>ConversionPatternRewriterImpl::lookupOrDefault</span></code><span> or its inlined helper frames.</span></p><p><span>The relevant call chain was:</span></p><pre><code><span>LowerAffine::runOnOperation
  -&gt; applyPartialConversion
    -&gt; OperationConverter::applyConversion
      -&gt; ConversionPatternRewriterImpl::applyRewrites
        -&gt; ReplaceOperationRewrite::commit
          -&gt; findOrBuildReplacementValue
            -&gt; lookupOrNull
              -&gt; lookupOrDefault</span></code></pre><p><span>This is a non-progressing loop during conversion rewrite commitment, not stack recursion or a slow affine computation.</span></p><p><span>The sampled run was terminated separately and did not have a normal bounded-run </span><code><span>closed.json</span></code><span> receipt. Its process sample and execution were recorded separately in the investigation.</span></p><h2><span>Expected Behavior</span></h2><p><code><span>lower-affine</span></code><span> should complete or terminate with a diagnostic/failure when it cannot safely process the input.</span></p><p><span>A self-referential materialization or a cyclic replacement mapping must not make the conversion infrastructure spin indefinitely.</span></p><p><span>The expected behavior does not require the pass to successfully lower every operation in the module; a controlled conversion failure is preferable to nontermination.</span></p><h2><span>Root Cause</span></h2><p><span>The relevant implementation is </span><code><span>ConversionPatternRewriterImpl::lookupOrDefault</span></code><span> in:</span></p><p><code><span>mlir/lib/Transforms/Utils/DialectConversion.cpp</span></code></p><p><span>The function resolves replacement values and unresolved materializations by following a chain of value mappings.</span></p><p><span>When pattern rollback is disabled, its lookup helper can inspect an unresolved </span><code><span>UnrealizedConversionCastOp</span></code><span> directly:</span></p><pre><code><span>auto castOp = dyn_cast&lt;UnrealizedConversionCastOp&gt;(op);
if (!castOp)
  return {};
if (!this-&gt;unresolvedMaterializations.contains(castOp))
  return {};
if (castOp.getOutputs() != values)
  return {};
return castOp.getInputs();</span></code></pre><p><span>For the self-referential Cast:</span></p><pre><code><span>castOp.getInputs()  = <span>[%0]</span>
castOp.getOutputs() = <span>[%0]</span></span></code></pre><p><span>The lookup returns the same non-empty vector that was supplied as input.</span></p><p><span>The composed lookup first attempts individual replacements. If these do not change the vector, it attempts a whole-vector mapping:</span></p><pre><code><span>ValueVector r = lookup(values);
if (r.empty())
  return {};
return r;</span></code></pre><p><span>With the self-referential materialization, this returns the original vector instead of an empty result.</span></p><p><span>The outer lookup loop then behaves as follows:</span></p><pre><code><span>do {
  // Examine the current replacement values.

  ValueVector next = composedLookup(current);
  if (next.empty())
    break;

  current = std::move(next);
} while (true);</span></code></pre><p><span>Because </span><code><span>next</span></code><span> is identical to </span><code><span>current</span></code><span> and remains non-empty, every iteration re-enters the same state.</span></p><p><span>There is no explicit no-progress or cycle detection in this loop. The process can therefore remain in </span><code><span>lookupOrDefault()</span></code><span> indefinitely.</span></p><p><span>The sampling results are consistent with this exact path.</span></p><h2><span>Controls and Relation to Existing Issues</span></h2><h3><span>1. The Affine Operation Is Not the Trigger</span></h3><p><span>The original generated module contained both a self-referential Cast region and a separate valid </span><code><span>affine.apply</span></code><span> function.</span></p><p><span>Removing the Cast region and retaining the affine function makes </span><code><span>lower-affine</span></code><span> complete normally (exit 0).</span></p><p><span>Conversely, retaining only the Cast region reproduces the hang, even when no affine operation remains.</span></p><p><span>This isolates the failure to shared conversion infrastructure rather than a particular affine lowering pattern.</span></p><p><span>Other passes that reach the same conversion lookup path may be affected, but the confirmed pass in this report is </span><code><span>lower-affine</span></code><span>.</span></p><h3><span>2. Distinct from #236562</span></h3><p><span>Issue #236562 describes a </span><code><span>canonicalize</span></code><span> hang involving a self-referential </span><code><span>arith.addi</span></code><span>.</span></p><p><span>That failure involves the Greedy Pattern Rewrite Driver and repeated folding/re-enqueueing through </span><code><span>foldSingleResultHook</span></code><span>.</span></p><p><span>The present failure occurs in:</span></p><pre><code><span>ConversionPatternRewriterImpl::lookupOrDefault</span></code></pre><p><span>during dialect conversion rewrite commitment.</span></p><p><span>The drivers, internal loops, and value-processing mechanisms are different.</span></p><h3><span>3. Distinct from #194824 / #205064 / #228851</span></h3><p><span>These issues describe an assertion when canonicalization folds a self-referential </span><code><span>unrealized_conversion_cast</span></code><span> and attempts to erase the operation while it still has uses.</span></p><p><span>The same generated program also exhibits that known assertion under </span><code><span>canonicalize</span></code><span>.</span></p><p><span>However, under </span><code><span>lower-affine</span></code><span>, it does not fail in </span><code><span>RewriterBase::eraseOp</span></code><span>. It hangs in </span><code><span>lookupOrDefault</span></code><span> while traversing unresolved materialization mappings.</span></p><p><span>The two failures share a self-referential input shape but have different immediate root causes.</span></p><p><span>The pending Cast folding fix in PR #207185 addresses the canonicalizer assertion path. This report does not claim that the PR has been tested against, or resolves, the conversion lookup hang.</span></p><p><span>Related reports:</span></p><ul><li><p><a href="" href="" href="" href="" href="" Fix Direction</span></h2><p><span>Ensure that the mapping-chain traversal makes progress and terminates in the presence of cyclic mappings.</span></p><p><span>Possible approaches include:</span></p><ol start="1"><li><p><span>Stop following a mapping when the next value vector is identical to the current vector.</span></p></li><li><p><span>Detect longer cycles involving multiple value vectors, not just direct self-mappings.</span></p></li><li><p><span>Handle self-referential unresolved materializations explicitly so their inputs cannot create non-progressing lookup chains.</span></p></li></ol><p><span>Any change should preserve the intended type-conversion and materialization behavior for valid mappings while ensuring termination for pathological inputs.</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>, Linux x86_64 (original generated program, 45-second bounded execution).</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, </span><code><span>MLIR_INCLUDE_TESTS=ON</span></code><span> (original program, Cast-only reduction, and minimal reproducer).</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 relevant </span><code><span>lookupOrDefault()</span></code><span> traversal still has no explicit progress/cycle guard. This exact 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 independently reduced and investigated.</span></p><p><span>The reported failure is a pass hang on verifier-accepted graph-region IR. No incorrect runtime result or silent miscompilation 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