Skip to content

shader_meta: fold s_brev_b32 so a literal S# built with it reaches literal_samplers (SHADER-769) - #4289

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
SphyserX:shader-769-brev-fold
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
SphyserX:shader-769-brev-fold

Conversation

@SphyserX

Copy link
Copy Markdown

Claimed in the coordination issue as: SHADER-769

What and why

tools/shader_meta.py (_step_state) tracked constants through s_mov_b32, s_movk_i32, the scalar ALU ops and s_mulk_i32, but not s_brev_b32. A literal S# with one dword built by s_brev_b32 therefore lost that dword's constant and was left out of literal_samplers in the index. The census in #4268 counts 28,659 such literal S# sites in 6,824 shaders, which a layer registering literal samplers from the index (#2887) would miss.

s_brev_b32 d, src now records d as the bit-reversed constant when src is an immediate or an SGPR holding a known constant (RDNA2 ISA SOP1: D[31:0] = S0[0:31]; no SCC). Any other source leaves d unknown, as before.

Tested

  • python -I tests/test_shader_meta.py: 21 tests pass. Two new tests on hand-written listings: s_brev_b32 of an immediate (1 gives 0x80000000) and of a constant SGPR (0x3 gives 0xC0000000) both reach literal_samplers; s_brev_b32 of a user SGPR leaves no literal S#. The first fails on main (KeyError: 'literal_samplers').
  • python -I tests/test_shader_meta_operands.py, python -m compileall -q make_game.py tools tests, python -I scripts/check_no_game_data.py: pass.
  • No GPU involved; not re-run over the corpus (no game files here).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: index metadata only, no translator rule.)

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 8479d6c on Windows, CPython 3.14.7, CPU only (no GPU, no game files): pass.

python -m pytest -q tests/test_shader_meta.py tests/test_shader_meta_operands.py gives 26 tests passed and 11 subtests passed. I used a virtual environment with tests/requirements.txt.

I translated synthetic pixel-shader listings with s12 = 0x92, s14 = s15 = 0, and image_sample that reads s[12:15]. I made s13 in three ways:

  • two s_brev_b32 on 3: the result is 3;
  • XOR of 3 and 1, then s_brev_b32: the result is 0x40000000;
  • s_movk_i32 3, s_mulk_i32 2, then s_brev_b32: the result is 0x60000000.
    At this head, each sampler is in literal_samplers. With tools/shader_meta.py from the parent commit (79e7cc5), all three are absent.

One related gap, not from this pull request (the parent has it too): s_not_b32 s13, 0 followed by s_brev_b32 s13, s13 gives no literal_samplers entry. The two instructions have known results, and the expected sampler is [146, 4294967295, 0, 0]. The new s_brev_b32 fold works when it gets a known constant, but the metadata pass does not fold s_not_b32. You can add it here, or I can post it as a separate finding.

AI-assisted.

@SweatierKey

Copy link
Copy Markdown
Contributor

Tested 8479d6c0 on Linux (WSL2, Python 3.13.5, CPU only, no game files): pass.

  • Code read (tools/shader_meta.py, tests/test_shader_meta.py):
  • Checks run (clean worktree on 03b09a08):
    • python3 -I tests/test_shader_meta.py -v: 21/21 OK (including test_s_brev_b32_constant_folding and test_s_brev_b32_of_unknown_source_is_not_a_literal).
    • git diff --check, test_docs_links.py (6/6), test_docs_repo_paths.py (2/2), test_text_write_line_endings.py (3/3), test_text_encoding.py (2/2): all pass.

AI-assisted review.

@Alpaca-exe

Copy link
Copy Markdown
Contributor

Tested 8479d6c0 on Windows 11, Python 3.11.9 (CPU only, no GPU, no game files): pass

  • python -I tests/test_shader_meta.py -v: 21/21 OK
  • python -I tests/test_shader_meta_operands.py -v: 5/5 OK
  • python -I scripts/check_no_game_data.py: pass

Diff reviewed: s_brev_b32 constant folding masks to 32 bits before bit-reversal and preserves unknown state when the source is not a known constant.

AI-assisted.

@rafaelpssz

Copy link
Copy Markdown
Contributor

Corpus check of 8479d6c0 on my own 1.0.2.1 copy (Windows, CPU only, counts only). I ran shader_meta.analyse_instructions over every listing, once with main's shader_meta.py and once with this PR's. Same blocks, same 25,030 analysed listings, 1,473 skipped (embedded data words or no instructions), 0 errors:

main #4289
shaders with ≥ 1 literal S# 5,030 7,566
(shader, S#) pairs 14,479 22,598
distinct literal S# values 19 27
  • No shader loses a literal S# it had on main.
  • 2,536 shaders get their first literal S#, and 3,209 that already had some get more.
  • So the fold only adds S#s.

For #2887, this means about half again as many shaders have their static samplers registered by the layer once this lands. LGTM from the data side.

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.

6 participants