canonicalize-parallel: unroll whiles that cannot iterate a third time #3071
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
ftynse
Part of #2968.
A
scf.whilecannot iterate a third time in two cases, and then unrolls (under the forward-progress guarantee) to1. 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'sscf.while (%k, %acc)sums with%kyielded as a constant and%accpassed through).2. The bits known of the second iteration's state decide the exit test. They are computed with
llvm::KnownBitsfrom the initial operands through the first iteration's condition operands and yields, and through thearithops,arith.selects andscf.ifs on the way (anscf.ifwith 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. inbilininteg_curlcurl_pa.cpp:The second way covers clang's done-flag loops, e.g.
tmop_pa_h3d.cpp(assemble diag):the rotated single-trip loop around
bilininteg_batchitrans.cpp's team solve, whose trip flag is the result of anscf.ifon the previous flag and is yieldedfalse, and the reduction over a leading dimension of one or two inbilininteg_vectorfemass_pa.cpp:where the second iteration's
%kis 1, so%k | %oddhas its low bit set whatever%oddis.The
tmop_pa_h3d.cpploop carries anllvm.alloca(%58, a nestedmfem::future::tensorstruct) through the loop as an iter arg, which keepsConvertLLVMAllocaToMemrefAllocafrom converting it; that TU then fails raising on the survivingllvm.alloca. Running the unroll as acanonicalize-parallelpattern, i.e. before the finalllvm-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, thellvm.allocafailure is gone and raising proceeds to the next (independent) blocker, a lane-privatememref<9xf64>scratch (#2996's scope); the union'sflattenLLVMArrayAllocashelper 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 byscf.conditionas invariant, and its own@dowhiletest (%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 yieldedtrue, 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