What: after SCCP (and the constant-field reads), canonicalization
unrolls each outermost loop whose exits the constants decide and whose
counter decides a branch or switch of its body (`canon::unroll`), then
runs SCCP again, which folds what each copy decides and deletes the
paths no copy takes; at most 4 rounds, so a nest is taken one level a
round. A loop qualifies when it is reducible with one preheader edge
and one back edge from its latch; it is left only from its header or
latch, each a conditional branch with one way out; its exits are
decided iteration by iteration by integer arithmetic, comparisons,
selects, element and member accesses and constant-global reads of its
header's PHIs and constants (no float operation or comparison is
evaluated); a non-exit branch or switch of its body tests a value
computed that way from its own or its nested loops' header PHIs; and
its latch carries no `llvm.loop.unroll.disable`. Each copy is the
loop's blocks with the header PHIs replaced by that iteration's
values; every instruction is copied as it is, with its flags; a value
used after the loop is the last iteration's.
Why: an uber shader that feeds a `uint4` of function-constant opcodes,
indexed by a 4x4 loop's counters, to a 16-case switch kept every case
of every iteration: 3952 words where the same shader with the
constants written in is 2612. Unrolled and folded it is 2296.
The ceilings are fixed: 64 iterations (`TRIPS`), 8192 instructions
over one loop's copies (`UNROLLED`), 32768 instructions added per
entry over all rounds (`GROWTH`), 4 rounds, 256 instructions evaluated
to decide one iteration's exits (`SLICE`). They are not a
speed-for-accuracy trade, so `docs/FAST-PATHS.md` has no row for them:
unrolling changes no operation and runs each the same number of
times, so a loop past a ceiling stays a loop and computes the same
answers. The ceilings bound only how large the copies grow, and so the
translation's time, memory and output size.
Rule 1: nothing new is evaluated on the host in float. The existing,
documented rule that SCCP folds `fcmp` of two constants (exact IEEE
comparison) is unchanged; a copy can give it new operands where a float
PHI's first iteration value is a constant.
Golden drift: `engine/simd_divergent` `uniform_continue_k` (its
`for (i < 8)` loop branches on `i & 1`) is unrolled, moving its 38
cells (`.ll` and `.air.ll` on 19 profiles); `engine_simd_emulated` runs
it on the CPU interpreter at every width and local size and its output
is unchanged. The table is re-blessed on `development`.
Tests: `canon::tests::unrolling` (six iterations over constant lanes
fold to straight-line code in order; past `TRIPS`, a run-time bound,
`unroll.disable` and run-time opcodes stay loops; at `TRIPS` it
unrolls; undefined lanes unroll to the default case).
Verified: `cargo nextest run -p metal2vulkan-air -p metal2vulkan
--all-features -E 'package(metal2vulkan-air) |
test(/engine_arena|engine_specialized_folds|engine_simd_emulated/)'`
553 passed; golden_hashes fails only on those 38 cells and the new
fixtures' rows; clippy on the workspace with all targets and features.
(cherry picked from commit a99e888de1d35e42de962b678c2f1c00f7383b22)
(cherry picked from commit c1ce0edbd491c3c8fd5ede8e331d5eb958c6bb25)