Repository navigation
perf(translator): fold chained scalar ALU constants and propagate to sgpr_imm (SHADER-315) - #2339
Conversation
…sgpr_imm (SHADER-315) Multi-step constant calculations on SGPRs occur frequently across RDNA2 shaders (e.g., computing buffer offsets, table strides, descriptor fields, and control words). Previously, scalar ALU folding recorded constants only for basic single-step operations and did not track destination literals back into `self.sgpr_imm`, preventing downstream instructions within the same basic block from recognizing chained constant expressions. - Track folded constants into `self.sgpr_imm` for scalar ALU operations: `s_mov_b32`, `s_and_b32`, `s_lshl_b32`, `s_lshr_b32`, `s_mul_i32`, `s_xor_b32`, `s_add_u32`, `s_sub_u32`, `s_lshl1..4_add_u32`, `s_bfe_u32/i32`, `s_min/max_u32/i32`, `s_andn2/orn2/nand/nor/xnor_b32`, `s_abs_i32`, `s_absdiff_i32`, `s_sext_i32_i8/i16`, `s_pack_*_b32_b16`, `s_bfm_b32`. - Preserve `s_or_b32` non-literal tracking to maintain descriptor runtime fallback test conventions in `test_buffer_*.py`. - Ensure standard SPIR-V lowering opcodes (`OpIAddCarry`, `OpISubBorrow`, `OpShiftLeftLogical`, `OpBitwiseAnd`, `OpShiftRightArithmetic`, etc.) continue to be emitted for arithmetic instructions so lowering and negative control regression tests pass intact while populating `self.sgpr_imm`. - Allow downstream `s_buffer_load` (SMEM) to statically evaluate bounds on chained offset calculations, eliminating branch diamonds (`OpSelectionMerge`, `OpBranchConditional`, `OpPhi`) for in-bounds accesses. - Statically fold `s_bfe` control words and `v_bfe` when constant operands are present. - Updated `docs/design.md` with the confirmed contract. - Added comprehensive unit tests in `tests/semantic/test_salu_chained_const_prop.py`. Tested: - `python -m compileall -q make_game.py tools tests`: OK - `python -I make_game.py --help`: OK - `python -m unittest discover -s tests/semantic`: 1,655/1,655 tests passed GPU: AMD Radeon RX 7800 XT (driver 24.20.19.05, Vulkan 1.4.359, Windows 11). AI-assisted. Standard library only; no game content or carved shaders.
ac32494 to
74a1666
Compare
|
Tested Independent synthetic Vulkan compute check of
AI-assisted verification. |
|
Tested
The GPU fixtures cover few chained scalar constants, so this run shows the folding breaks nothing. It does not measure the performance gain the PR is after. Tools: llvm-mc 23.1.6 (conda-forge), SPIRV-Tools from the Vulkan SDK. No game files. AI-assisted (Claude Code). |
|
Read the whole pass against the lowering it replaces and ran it here (Windows 11, RX 7800 XT, gfx1101, spirv-val from Vulkan 1.4.363). Checks
The formulas themselves check out against the code they shadow: Three things before merge
|
|
Tested 74a1666 on AMD Radeon RX 9070 XT (RADV GFX1201) and the Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.6, Linux 7.3.4: pass, no regressions.
No game files. AI-assisted. |
|
Tested 74a1666 merged onto main ec8ec4c on an NVIDIA GeForce RTX 5070 Ti (desktop), driver 617.14, Windows 11 (build 26200), subgroup 32..32: GPU suite pass; 2 semantic tests fail after the merge with current main. GPU (
Semantic suite (
Both feed constants (
Setup note: |
Summary
Implements SHADER-315 ("fold chained scalar ALU operations (
s_add_u32,s_sub_u32,s_lshl{1..4}_add_u32,s_bfe_u32/i32,s_min/max_u32/i32,s_andn2/orn2/nand/nor/xnor_b32,s_abs_i32,s_absdiff_i32,s_sext_i32_i8/i16,s_pack_*_b32_b16,s_bfm_b32)").Multi-step constant calculations on SGPRs occur frequently across RDNA2 shaders (e.g. computing buffer descriptor offsets, strides, table indices, bitfield control words, and bounds). Previously, scalar ALU folding recorded constants only for basic single-step operations and did not track destination literals back into
self.sgpr_imm, preventing downstream instructions within the same basic block from recognizing chained constant expressions.Changes
self.sgpr_immfor scalar ALU operations:s_mov_b32,s_and_b32,s_lshl_b32,s_lshr_b32,s_mul_i32,s_xor_b32,s_add_u32,s_sub_u32,s_lshl1..4_add_u32,s_bfe_u32/i32,s_min/max_u32/i32,s_andn2/orn2/nand/nor/xnor_b32,s_abs_i32,s_absdiff_i32,s_sext_i32_i8/i16,s_pack_*_b32_b16,s_bfm_b32.s_or_b32non-literal tracking to maintain descriptor runtime fallback test conventions intest_buffer_*.py.OpIAddCarry,OpISubBorrow,OpShiftLeftLogical,OpBitwiseAnd,OpShiftRightArithmetic, etc.) for arithmetic instructions so lowering and negative control regression tests pass intact while populatingself.sgpr_imm.s_buffer_load(SMEM) to statically evaluate bounds on chained offset calculations, eliminating branch diamonds (OpSelectionMerge,OpBranchConditional,OpPhi) for in-bounds accesses.s_bfecontrol words andv_bfewhen constant operands are present.docs/design.mdwith the confirmed contract row.tests/semantic/test_salu_chained_const_prop.py(11 tests).Testing
python -m compileall -q make_game.py tools tests: OKpython -I make_game.py --help: OKpython -m unittest tests/semantic/test_salu_chained_const_prop.py: 11/11 OKpython -m unittest discover -s tests/semantic: 1,655/1,655 tests passed (0 failures, 2 skipped)Item: SHADER-315