Issue 242841
Summary [mlir][SCF] convert-scf-to-cf breaks SizedRegion<1> constraint when lowering scf.if inside test.isolated_region_branch
Labels mlir
Assignees
Reporter lin334141-cell
    <h2><span>Summary</span></h2><p><span>The </span><code><span>convert-scf-to-cf</span></code><span> pass produces invalid IR when lowering an </span><code><span>scf.if</span></code><span> nested directly inside a </span><code><span>test.isolated_region_branch</span></code><span> operation.</span></p><p><span>The parent operation requires each of its regions to contain exactly one block (</span><code><span>SizedRegion&lt;1&gt;</span></code><span>). However, </span><code><span>IfLowering::matchAndRewrite</span></code><span> unconditionally splits the containing block and moves the branches of </span><code><span>scf.if</span></code><span> into the parent region, violating this constraint.</span></p><p><span>The original module passes verification, but the transformed module fails with:</span></p><pre><code><span>error: 'test.isolated_region_branch' op region #0 ('branches')
failed to verify constraint: region with 1 blocks</span></code></pre><p><span>This is related to #237682, which involves </span><code><span>scf.for</span></code><span> inside </span><code><span>gpu.warp_execute_on_lane_0</span></code><span>. The present reproducer exercises a different lowering pattern (</span><code><span>IfLowering</span></code><span>) and a different region constraint (</span><code><span>SizedRegion&lt;1&gt;</span></code><span>), providing additional coverage for the same general class of failure.</span></p><h2><span>Minimal Reproducer</span></h2><p><span>The following 623-byte MLIR module reproduces the problem:</span></p><pre><code><span>"builtin.module"() ({
 "func.func"() &lt;{function_type = (i1, i32) -&gt; i32, sym_name = "nested_isolation_min"}&gt; ({
  ^bb0(%arg0: i1, %arg1: i32):
    %0 = "test.isolated_region_branch"(%arg0, %arg1) ({
    ^bb0(%arg2: i1, %arg3: i32):
      %1 = "scf.if"(%arg2) ({
        %2 = "arith.addi"(%arg3, %arg3) &lt;{overflowFlags = #arith.overflow&lt;none&gt;}&gt; : (i32, i32) -&gt; i32
        "scf.yield"(%2) : (i32) -&gt; ()
      }, {
 "scf.yield"(%arg3) : (i32) -&gt; ()
      }) : (i1) -&gt; i32
 "test.isolated_region_yield"(%1) : (i32) -&gt; ()
    }) : (i1, i32) -&gt; i32
    "func.return"(%0) : (i32) -&gt; ()
  }) : () -&gt; ()
}) : () -&gt; ()</span></code></pre><p><span>The reproducer requires an </span><code><span>mlir-opt</span></code><span> build with the Test dialect registered.</span></p><h2><span>Steps to Reproduce</span></h2><p><span>First, verify the original input:</span></p><pre><code><span>mlir-opt min.mlir -o /dev/null</span></code></pre><p><span>The command succeeds.</span></p><p><span>Then run:</span></p><pre><code><span>mlir-opt min.mlir \
  --pass-pipeline="builtin.module(convert-scf-to-cf)" \
  -o /dev/null</span></code></pre><h2><span>Actual Behavior</span></h2><p><span>The conversion exits with code 1:</span></p><pre><code><span>error: 'test.isolated_region_branch' op region #0 ('branches')
failed to verify constraint: region with 1 blocks</span></code></pre><p><span>The </span><code><span>scf.if</span></code><span> is lowered by introducing control-flow blocks inside the parent region, leaving the region with more than its permitted single block.</span></p><h2><span>Expected Behavior</span></h2><p><span>The conversion should preserve the structural invariants of the enclosing operation.</span></p><p><span>If lowering </span><code><span>scf.if</span></code><span> would violate a parent region's fixed block-count constraint, the conversion should either:</span></p><ul><li><p><span>Preserve the operation when appropriate through context-dependent legality, or</span></p></li><li><p><span>Fail the conversion cleanly without producing structurally invalid IR.</span></p></li></ul><h2><span>Root Cause Analysis</span></h2><p><span>The relevant code is in:</span></p><p><code><span>mlir/lib/Conversion/SCFToControlFlow/SCFToControlFlow.cpp</span></code></p><p><span>Inside </span><code><span>IfLowering::matchAndRewrite</span></code><span>:</span></p><pre><code><span>auto *condBlock = rewriter.getInsertionBlock();
auto opPosition = rewriter.getInsertionPoint();
auto *remainingOpsBlock =
 rewriter.splitBlock(condBlock, opPosition);</span></code></pre><p><span>The call to </span><code><span>splitBlock</span></code><span> does not account for whether the containing block belongs to a region constrained to have exactly one block.</span></p><p><span>In this reproducer, the containing region belongs to </span><code><span>test.isolated_region_branch</span></code><span>, whose region definition uses </span><code><span>SizedRegion&lt;1&gt;</span></code><span>.</span></p><p><span>Furthermore, the conversion target declares </span><code><span>scf::IfOp</span></code><span> illegal without a contextual exemption for fixed-block-count regions:</span></p><pre><code><span>target.addIllegalOp&lt;scf::ForallOp, scf::ForOp,
                    scf::IfOp, scf::IndexSwitchOp,
 scf::ParallelOp, scf::WhileOp,
 scf::ExecuteRegionOp&gt;();</span></code></pre><p><span>Consequently, the conversion attempts to lower the operation even when the resulting CFG cannot satisfy the enclosing region's constraints.</span></p><h2><span>Relationship to #237682</span></h2><p><span>Issue #237682 reports a closely related failure where </span><code><span>ForLowering</span></code><span> splits blocks inside the single-block region of </span><code><span>gpu.warp_execute_on_lane_0</span></code><span>.</span></p><p><span>The two reproducers differ in several important respects:</span></p>
Aspect | #237682 | This reproducer
-- | -- | --
SCF operation | scf.for | scf.if
Conversion pattern | ForLowering | IfLowering
Enclosing operation | gpu.warp_execute_on_lane_0 | test.isolated_region_branch
Region constraint | SingleBlockImplicitTerminator | SizedRegion<1>
Verification failure | GPU region block-count violation | Test region block-count violation

<p><span>Both failures appear to arise from the same underlying lack of contextual legality checks before block splitting.</span></p><p><span>I am reporting this variant separately to document the </span><code><span>IfLowering</span></code><span> path and the </span><code><span>SizedRegion&lt;1&gt;</span></code><span> constraint, which may require additional regression-test coverage beyond a fix targeting only </span><code><span>ForLowering</span></code><span> or the GPU operation.</span></p><p><span>If maintainers prefer tracking both under #237682, they can be consolidated.</span></p><h2><span>Environment</span></h2><ul><li><p><span>LLVM: </span><code><span>24.0.0git</span></code></p></li><li><p><span>Commit: </span><code><span>8635f9042c2a42d15844b0bc4b2e8e27b06f3f54</span></code></p></li><li><p><span>Platform: Linux x86-64</span></p></li><li><p><span>Build: Optimized, with assertions enabled</span></p></li><li><p><span>Pass: </span><code><span>builtin.module(convert-scf-to-cf)</span></code></p></li></ul><p><span>The failure was discovered during external-generator-based MLIR compiler testing and reproduced independently with the minimized input.</span></p>
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to