Skip to content

shader_meta: fold s_ashr_i32, s_bfm_b32, s_bfe_u32, s_min_u32, s_max_u32 and s_abs_i32 of known constants (follow-up of #4546) - #4604

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
shdann:shdann/shader-meta-fold-shift-bitfield
Nov 23, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
shdann:shdann/shader-meta-fold-shift-bitfield

Conversation

@shdann

@shdann shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue (claim). The claim has no item ID at this time.

Depends on: #4546

This branch is one commit (005b2306) on top of the commit of #4546 (14b51cc7). Until #4546 merges, the diff of this pull request also shows the commit of #4546. Review only the second commit here.

What and why

_step_state in tools/shader_meta.py follows the constants in the SGPRs. #4546 lists six scalar operations that it does not fold. This pull request folds them. Without the fold, a literal S# that has one dword made with one of these operations is not in literal_samplers of the index.

Hand-written listing (no game data), one example:

s_mov_b32 s12, 0x92
s_bfm_b32 s13, 4, 8
s_mov_b32 s14, 0
s_mov_b32 s15, 0
image_sample v[4:7], v[0:1], s[16:23], s[12:15] dmask:0xf dim:SQ_RSRC_IMG_2D
s_endpgm

The folds use the same rules that tools/rdna2spirv.py uses when the sources are immediates:

Operation Result for known constants x, y
s_ashr_i32 x as a signed value, shifted right by y & 31
s_bfm_b32 a mask of x & 31 ones, shifted left by y & 31
s_bfe_u32 offset y & 31, width bits 22 to 16 of y; the field stops at bit 31; width 0 gives 0
s_min_u32, s_max_u32 the unsigned minimum and maximum
s_abs_i32 the absolute value of x as a signed value; 0x80000000 stays 0x80000000

A source that is not a known constant gives no constant, as before.

The second commit changes tools/shader_meta.py by 22 added lines and 2 removed lines.

I did not add s_bfe_i32, s_min_i32 and s_max_i32. My probe did not include them, and I keep this change to the six operations of the claim.

Tested

Windows 11, CPython 3.14.7, CPU only. No GPU and no game files.

New tests in tests/test_shader_meta.py:

  • test_shift_bitfield_min_max_abs_fold_into_literal_sampler: 16 listings. They include a shift count larger than 31, a width of 0, a field that goes past bit 31, values with the top bit set for the unsigned minimum and maximum, and 0x80000000 for s_abs_i32.
  • test_shift_bitfield_min_max_abs_of_unknown_source_is_not_a_literal: 8 listings where one source is a user SGPR. The result must be no literal sampler.
Command tools/shader_meta.py of #4546 (14b51cc7) This branch
python -I tests/test_shader_meta.py FAILED (failures=16): the 16 listings of the first new test 23 tests OK
python -I tests/test_shader_meta_operands.py OK OK

Other tests that import shader_meta, on this branch:

  • python -I tests/test_fspx_shaders_stage.py: OK.
  • python -I tests/test_fssm_wave_size.py: OK.
  • python -I tests/test_shader_index.py: exit code 0.
  • python -I tests/test_spirv_stubs.py: exit code 0.
  • python -I scripts/check_no_game_data.py: exit code 0.
  • I did not run tests/test_mesh_compute.py, tests/test_pc_tail_ubo.py, tests/test_rdna2spirv_pixelinput.py and tests/test_translate.py. They need pytest, and pytest is not installed on this computer.

AI-assisted (Claude Code).

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: no translator rule.)

🤖 Generated with Claude Code

shdann and others added 2 commits November 23, 2026 05:19
…known constants

_step_state did not fold the inverted scalar logic operations. Thus a
literal S# that has one dword made with one of them (for example
0xFFFFFFFF as s_not_b32 of 0) was not in literal_samplers.

s_not_b32 of a known constant is now a known constant. s_nand_b32,
s_nor_b32 and s_xnor_b32 of two known constants fold together with the
other two-source logic operations. A source that is not a known constant
gives no constant, as before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…u32 and s_abs_i32 of known constants

_step_state did not fold these scalar operations. Thus a literal S# that
has one dword made with one of them was not in literal_samplers.

The two-source operations fold in _fold_scalar_alu with the values that
the translator computes for immediates: the shift count of s_ashr_i32 is
modulo 32, s_bfm_b32 makes a mask of (width & 31) ones at (offset & 31),
and the field of s_bfe_u32 stops at bit 31. s_abs_i32 of a known constant
is a known constant. A source that is not a known constant gives no
constant, as before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@SweatierKey

Copy link
Copy Markdown
Contributor

Tested 005b2306 on Linux (x86_64, Python 3.13.5): pass (code read + unit tests).

What I ran:

  • git diff --check d0835dd9...HEAD: clean.
  • python3 -m unittest -v tests/test_shader_meta.py: Ran 23 tests in 0.030s
    , OK (including all new folding and unknown-source negative cases for s_not_b32, s_nand/nor/xnor_b32, s_ashr_i32, s_bfm_b32, s_bfe_u32, s_min_u32, s_max_u32, and s_abs_i32).

Code review:

  • Cross-checked _fold_scalar_alu against tools/rdna2spirv.py for s_bfm_b32 (x & 31, y & 31), s_bfe_u32 (offset = y & 31, width = min((y >> 16) & 0x7F, 32 - offset)), s_ashr_i32 (signed shift mod 32), s_min_u32/s_max_u32, and s_abs_i32 (abs(0x80000000) == 0x80000000): all match bit-for-bit.
  • Unknown sources cleanly avoid recording a ("const", ...) entry so non-literal samplers are not falsely reported.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran this at 005b2306 on Windows 11: tests/test_shader_meta.py -> 23 tests, OK; git diff --check clean.

The six ops (s_ashr_i32, s_bfm_b32, s_bfe_u32, s_min_u32, s_max_u32, s_abs_i32) fold known constants with the same 32-bit width/signedness handling as the #4546 set, and the added cases cover each op plus the non-constant case that must stay unfolded. No blocking issue.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 005b230 merged with main d0835dd (merge c68760b72, no conflicts) on Linux (WSL2) Python 3.12.16 and native Windows 11 Python 3.13.1: pass.

  • tests/test_shader_meta.py: 23/23 OK on both.
  • Negative control: tools/shader_meta.py from main, same tests: 27 failures (subtests) on Linux and on Windows.
  • Read the diff against the ISA definitions: s_bfm_b32 (mask of S0[4:0] ones shifted by S1[4:0]), s_bfe_u32 (offset S1[4:0], width S1[22:16], clamped at bit 31), s_ashr_i32 (arithmetic shift by S1[4:0]), s_min_u32/s_max_u32, s_abs_i32 (0x80000000 stays) are all 32-bit masked.
  • Overlap: the s_not_b32/s_nand_b32/s_nor_b32/s_xnor_b32 folds in this branch are the same change as shader_meta: fold s_not_b32, s_nand_b32, s_nor_b32 and s_xnor_b32 so a literal S# built with them reaches literal_samplers #4546 (open, tools/shader_meta.py and tests/test_shader_meta.py); the two branches merge without a textual conflict here, but one of them carries the other's work.

@whattheflop whattheflop left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted independent Windows CPU check of the top commit 005b2306 on #4546 14b51cc7, uv CPython 3.12.13. analyse_instructions() returned the expected literal_samplers in 427 synthetic comparisons against a separate bit-list reference. The matrix covered shift counts 31/32/33/63, bitfields extending past bit 31, unsigned high-bit ordering, abs(INT_MIN), and destinations aliasing source 0, source 1 or both. Chained folds across both PRs propagated known words, and an unknown input invalidated a previously known destination. The two stock metadata suites passed 28/28. No game data or GPU execution.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested head 005b2306 — Windows, CPython 3.15.2, CPU only, synthetic fixtures, no game files: pass.

  • python -I tests/test_shader_meta.py -> 23/23 OK.
  • Control (main's tools/shader_meta.py + this test file): 27 subtest failures, all in the new constant-folding cases (s_ashr_i32, s_bfm_b32, s_bfe_u32, s_min_u32, s_max_u32, s_abs_i32).
  • git diff --check and scripts/check_no_game_data.py clean.

AI-assisted (Devin).

@memorypressure

Copy link
Copy Markdown

Verified against main 3faac55: merges cleanly; test_shader_meta 23/23, test_shader_meta_operands 5/5, test_vs_packed_inputs 3/3 in the venv (re-run). The new fold rules match GCN/RDNA semantics including the bfe width clamp, bfm width-0 to 0 and the INT_MIN abs edge, so conservative folding cannot change a known-constant result. Title references closed #4546 — its content is carried in your own commits, no dependency. Checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@ItsNotRexed
ItsNotRexed merged commit 91de0bd into ItsNotRexed:main Nov 23, 2026
2 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