Translator: a repeated region of 500 or more instructions is written once, as a kept routine

What: a new pass, spirv::outline, runs on every module after rolling and hoisting. It finds a set
of regions of one function that are the same code (one sequence of the function's spine, loop
bodies included; entered only at the start and left only at the end), each of at least 500
instructions with a call of a kept routine counting one, writes the first as a new kept routine
(Function::kept, declared DontInline, the form ModuleBuilder::kept_routine makes) and replaces
every region with a call of it, then looks again, at most 64 times. Values from before a region
are parameters (one per distinct set of the regions' values; a constant that differs only where
any value may stand); a pointer parameter is a Function, Private, Workgroup or UniformConstant
variable itself or a PhysicalStorageBuffer pointer; values read after a region are the routine's
results (several in a struct). A Function variable only its region uses becomes the routine's
own where the region is in no loop or its first use stores the whole variable in a block the
region always runs; otherwise it is passed. Anything else that differs keeps the regions in line.
Candidates come from the spine's keys (16-item windows hashed, extended both ways); a set whose
exact comparison fails at an operand or result is compared again without that item. The spine
(roll::spine) takes a flag to walk loop bodies as sequences; rolling passes false and is
unchanged. SEMANTICS_VERSION 2259. API.md and ARCHITECTURE.md say the routines exist.

Why: NVIDIA's driver compiles a DontInline routine once and Mesa's drivers inline it either way.
On the RTX 3090 (620.70.06, job m3-max-64453) a generated kernel calling a 1,984-instruction
function from 50 places was created in 21.5 s inlined and 52 ms as a kept routine (51.8 s and
75 ms with 64 values live across every call); its dispatch GPU time was the same with few values
live and 20.6% lower as a routine with 64 live. A 497-instruction function: 0.1% and 19.7% lower.
500 is the smallest size measured within 3%. RADV and ANV (Mesa 26.2.6) compiled identical code
for the call, DontInline and inlined forms (jobs m3-max-64357, m3-max-64379).

How verified: spirv::outline::tests build kernels with three equal regions (a variable, a
selection's phi, a counting loop), a chain where each region reads the previous one's result, a
middle region that differs, and two regions in a loop whose accumulator is read before it is
written; each checks the routine and call counts, the module through the verifier and spirv-val,
and the words the interpreter writes against the unrewritten module's and the expected ones.
This commit is contained in:
Anees Iqbal committed 2026-11-21 12:18:23 -08:00
1 parent 0429ff1bd4
commit 7e92b1d0fa
12 files changed
+2236 -46

No files matched your search

+6 -2
View File
@@ -523,10 +523,14 @@ assert!(stats.reachable_blocks <= stats.blocks);
blocks are the program's, while a module that calls a many-block function from many sites holds
a program far larger than its own blocks; the two read alike in `blocks` alone. Lowering emits
such functions itself: the handle-table search (unrolled branch-free steps) and the root-table
search (a loop), each emitted once and called at every access.
search (a loop), each emitted once and called at every access. A region of 500 or more
instructions that a function holds more than once (a function the program calls from several
places, inlined at each) is written once too, as a routine declared `DontInline` and called
from each place: NVIDIA's driver compiles it once, and RADV and ANV (Mesa 26.2.6) compile the
same code with or without the control.
- `entry_weight` is what a driver compiles once it has inlined every call: the entry's
instructions with each call counted as its callee's body, after repeated regions are rolled
into loops. A stage past `pipeline::COMPILED_STAGE_WEIGHT` (99,109, the heaviest measured to
into loops (a region written as a routine counts at each of its calls). A stage past `pipeline::COMPILED_STAGE_WEIGHT` (99,109, the heaviest measured to
compile within host-sized memory on the slowest compiler measured) is one whose creation may
take the host's memory: the translator makes it all the same, and `metal2vulkan-runtime`
compiles such a kernel only in a helper process the consumer caps (its README, "Kernels past