Skip to content

perf(translator): fold chained scalar ALU constants and propagate to sgpr_imm (SHADER-315) - #2339

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
shadshuvo:perf/chained-scalar-const-prop
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
shadshuvo:perf/chained-scalar-const-prop

Conversation

@shadshuvo

Copy link
Copy Markdown

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

  • 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.
  • Preserved s_or_b32 non-literal tracking to maintain descriptor runtime fallback test conventions in test_buffer_*.py.
  • Emitted standard SPIR-V lowering opcodes (OpIAddCarry, OpISubBorrow, OpShiftLeftLogical, OpBitwiseAnd, OpShiftRightArithmetic, etc.) for arithmetic instructions so lowering and negative control regression tests pass intact while populating self.sgpr_imm.
  • Allowed 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 row.
  • Added comprehensive unit tests in tests/semantic/test_salu_chained_const_prop.py (11 tests).

Testing

  • python -m compileall -q make_game.py tools tests: OK
  • python -I make_game.py --help: OK
  • python -m unittest tests/semantic/test_salu_chained_const_prop.py: 11/11 OK
  • python -m unittest discover -s tests/semantic: 1,655/1,655 tests passed (0 failures, 2 skipped)
  • Hardware: Tested on 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.

Item: SHADER-315

…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.
@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 74a166602cac3a1104d8c67b3878dd9bfe98f6d7 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Independent synthetic Vulkan compute check of 74a166602cac3a1104d8c67b3878dd9bfe98f6d7 on Linux 7.0.0-43 (RTX 5090, NVIDIA 595.99.02, subgroup 32, LLVM 21.1.8, SPIRV-Tools 2026.1).

  • The wave32 fixture chained a scalar move, shift, add, bitfield extract, subtract, and signed max. A vector bitfield extract read the chained scalar value. Both the PR and a scratch build with only its translator diff reversed returned the same five expected words (00000134 00000013 fffffff3 fffffff9 00000013) and preserved all 59 fill words.
  • Both modules passed Vulkan 1.2 SPIR-V validation. The PR module contained 25 OpBitFieldUExtract instructions versus 28 in the control module.
  • All 11 focused semantic tests passed. The GPU fixture covers this arithmetic chain. It does not measure GPU speed or exercise scalar buffer-load folding.

AI-assisted verification.

@bielfreedom

Copy link
Copy Markdown

Tested 74a16660 (on its base 49475796) on AMD Radeon RX 9070 XT (RDNA4), AMD driver 32.0.32029.2010 (subgroup 32..64), Windows 11 Pro build 26200: pass, no regression.

  • scripts/run_gpu_tests.py with Vulkan validation on (LEONIDA_VK_VALIDATION=1, Vulkan SDK 1.4.363, VK_LOADER_LAYERS_DISABLE=~implicit~): 190 shaders, 190 translated, 190 pass spirv-val; 170/185 passed, 15 skipped, 0 failures. The skips are the 14 shaders run by the d3d12 tests (cs_buf_*, cs_m4_*, ps_m*, vs_m*) and ps_w64x_readlane (no required 64-wide fragment subgroup on this device).
  • tests/semantic/test_salu_chained_const_prop.py: 11/11 OK.
  • python -m unittest discover -s tests/semantic: 2578 tests OK.

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).

@UNYTIX

UNYTIX commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

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

  • test_salu_chained_const_prop.py → 11/11 OK on the PR head.
  • Related suites: test_buffer_swizzle 7 OK, test_buffer_oob_select 12 OK, test_alu_bfe 14 OK, test_salu_absdiff_mul_hi 6 OK.
  • Real shader through the PR translator: runtime/test/shaders/cs_buf_tail.asm (tab-indented listing, header-only FSSM) → spirv-val ok → vk_compute_test … 1 dump gives 0 0x5eed000c 0x5eed0008 0x5eed000c 0 0 0x5eed0010 0, exactly the expectation in check.py. So the chained-constant handling does not disturb the MUBUF offset/bound path on hardware.

The formulas themselves check out against the code they shadow: s_bfe's constant width is min(wid[22:16], 32-off), the same cut bfe_width applies at runtime (it extracts 7 bits, not 5, so no path/constant disagreement); signed s_min_i32/s_max_i32 fold to SCC = S0<S1 / S0>S1, matching the "a tie chooses S1" comment in the constant-free path; s_abs_i32, s_absdiff_i32, s_sext_i32_i8/i16 and the three s_pack_*_b32_b16 halves match the ISA pseudocode, including abs(0x80000000) staying put. Ops that keep emitting their instruction (s_add_u32, s_sub_u32, s_lshl*_add_u32, s_min/max_u32, s_add/sub_i32, the andn2 family) only cache a value there, which is fine as long as the cache is true.

Three things before merge

  1. op != "s_or_b32" in the fold block. The comment in tests/semantic/test_buffer_swizzle.py:52 — "with runtime words 1 and 3 are or-ed at run time (s_or_b32 is not folded)" — is the only thing that needs this. A test that pins the absence of an optimization is not a reason to special-case one op out of twenty; better to fold s_or_b32 like everything else and rebuild that test's descriptor through a non-immediate source (an s_load_dword, or an OR whose operand comes from memory) so it still covers the runtime path. As it stands the exception is invisible at the call site and will surprise the next person who extends the fold.

  2. The design row says these "fold completely at translation time and record folded literal values in self.sgpr_imm". About half of them (the list in point above) still emit their arithmetic and only cache the result, so "completely" overstates it, and the row does not mention the s_or_b32 exception. Worth rewording to "fold to a literal where both operands are known, and otherwise cache the known result for later folds", plus the exception if it survives point 1.

  3. The new tests assert on the emitted SPIR-V text (a literal appears, a branch diamond is gone), not on values. That was fine when sgpr_imm only fed address modes; it now gates the static in-bounds decision for s_buffer_load, where a wrong cached value turns an out-of-bounds load into a statically "in bounds" one with no runtime check. One interpreter-level case per group — translate with random 32-bit operands and compare the cached fold against the value the runtime instruction produces, the way test_export_valid_mask.py drives run_wave — would make the cache's invariant ("sgpr_imm[r] equals what the register really holds") testable instead of assumed.

@sperm

sperm commented Nov 22, 2026

Copy link
Copy Markdown

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.

  • Full python -I scripts/run_gpu_tests.py (Linux build of this branch's runtime/, LEONIDA_VK_DEVICE per GPU): 170/185 passed, 15 skipped, 0 failed on both GPUs. Compared with the same suite at the merge base 4947579 on the same machine, every test has the same result.
  • python -m unittest discover -s tests/semantic: 2578 OK

No game files. AI-assisted.

@erickbobd

Copy link
Copy Markdown
Contributor

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 (scripts/run_gpu_tests.py, runtime built from main; this PR touches no runtime code), adding a 32-wide NVIDIA result to the RX 7800 XT one:

  • native: 143/189 passed, 46 skipped; LEONIDA_W64_EMULATE=1: 174/189 passed, 15 skipped
  • identical per test to main ec8ec4c on the same machine, 0 failures
  • 11 of the 197 translated test shaders change versus main, so the folding is exercised

Semantic suite (python -I -m unittest discover -s tests/semantic): main ec8ec4c alone: 4137 OK. Merged with this PR: 4148 tests, 2 failures, both in emission tests that 9f1dc5c added to tests/semantic/test_alu_bitrev_abs.py after this PR's base:

  • test_s_abs_i32_emission: 'SAbs' not found
  • test_s_absdiff_i32_emission: 'OpISub %uint' not found

Both feed constants (s_mov_b32 s2, -42, then s_abs_i32 s0, s2), which this PR now folds, so the instructions they look for are no longer emitted. Either the tests need a non-constant source, or s_abs_i32/s_absdiff_i32 keep emitting their ops while populating sgpr_imm, as the description says the other arithmetic ops do.

tests/semantic/test_salu_chained_const_prop.py: 11/11 OK.

Setup note: LLVM_MC pointed at a shim over AMD RGA 2.15's clang -cc1as (LLVM 23.0.0git); see my note on #2463.

@ItsNotRexed
ItsNotRexed merged commit 1ebaa4c into ItsNotRexed:main Nov 22, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants