← Back to Pull requests

canonicalize-parallel: unroll whiles that cannot iterate a third time #3071

Open
unknown opened this pull request with 486 lines diff across 6 files
View on GitHub
unknown unknown commented on Sep 02, 2026 15:21 UTC
Author

Part of #2968.

A scf.while cannot iterate a third time in two cases, and then unrolls (under the forward-progress guarantee) to

body(init); if (cond) { after; body(next) }

1. The exit test is a pure function of carried state whose next-iteration values are loop-invariant: a third iteration would start from exactly the second's state and take the same exit decision, so the loop would never terminate. Only the state the exit test reads needs invariant yields; an accumulator can be carried alongside (bilininteg_curlcurl_pa.cpp's scf.while (%k, %acc) sums with %k yielded as a constant and %acc passed through).
2. The bits known of the second iteration's state decide the exit test. They are computed with llvm::KnownBits from the initial operands through the first iteration's condition operands and yields, and through the arith ops, arith.selects and scf.ifs on the way (an scf.if with an unknown condition intersects its two yields).

MFEM's strided copy loops (k = tid; do { copy(k); k += stride; } while (k < n)) reach us the first way once the range analysis folds the increment to a constant, e.g. in bilininteg_curlcurl_pa.cpp:

scf.while (%arg23 = %102) { ... %113 = arith.cmpi ult, %arg23, %80; scf.condition(%113) } do { scf.yield %c4_i32 }

The second way covers clang's done-flag loops, e.g. tmop_pa_h3d.cpp (assemble diag):

scf.while (%arg13 = %true, %arg14 = %59, %arg15 = %58) { ... scf.condition(%arg13) } do { scf.yield %false, %61, %62 }

the rotated single-trip loop around bilininteg_batchitrans.cpp's team solve, whose trip flag is the result of an scf.if on the previous flag and is yielded false, and the reduction over a leading dimension of one or two in bilininteg_vectorfemass_pa.cpp:

scf.while (%acc = %cst, %k = %c0_i32) { ... %next = arith.addi %k, %c1; %bits = arith.ori %k, %odd; %again = arith.cmpi eq, %bits, %c0; scf.condition(%again) %sum, %next } do { scf.yield }

where the second iteration's %k is 1, so %k | %odd has its low bit set whatever %odd is.

The tmop_pa_h3d.cpp loop carries an llvm.alloca (%58, a nested mfem::future::tensor struct) through the loop as an iter arg, which keeps ConvertLLVMAllocaToMemrefAlloca from converting it; that TU then fails raising on the surviving llvm.alloca. Running the unroll as a canonicalize-parallel pattern, i.e. before the final llvm-to-affine-access, lets the alloca convert normally (with #3064's gep fold for the 8-byte offset). Replaying that TU's imported module through the full pipeline with this branch, the llvm.alloca failure is gone and raising proceeds to the next (independent) blocker, a lane-private memref<9xf64> scratch (#2996's scope); the union's flattenLLVMArrayAllocas helper is not needed for it.

This supersedes #3013's raiser-local unrollInvariantYieldWhiles, with one soundness correction: #3013 accepted any pass-through of a value forwarded by scf.condition as invariant, and its own @dowhile test (%next = %k + 16, scf.condition(%cond) %next, scf.yield %j) is a genuine multi-iteration loop that it unrolled to two iterations. Here after-block arguments resolve through the forwarded condition operands, and the exit condition must be pure over loop-external values and the before-block arguments (no memory reads, since the body may write what the test reads). The negative cases in the test cover the varying pass-through against an unknown bound, a condition that loads memory, a yield computed in the after region from the carried state, an exit test that reads the accumulator, a trip flag yielded true, and a masked (andi) counter whose second value is not decided.

Test: test/lit_tests/canonicalize_parallel_while_unroll.mlir.

🤖 Generated with Claude Code

https://claude.ai/code/session_016zErYp7upmqr4NHfhod9UD

Diff: 486 lines (+364 / -122) across 6 files Reviewer: ftynse Review duration: 87091m
All Vouch review confidence checks passed
Residual risk 0.30 = Change Risk (0.54) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.54 · 486 lines diff across 6 files
0.54
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
44%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.370
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #3071"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 44.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 54.3% (MEDIUM) · Higher = larger blast radius
54.3%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 44.7% · Lower = rushed or shallow review
44.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 30% · Safe to merge
30%