Skip to content

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

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
shdann:shdann/shader-meta-fold-inverted-logic
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
shdann:shdann/shader-meta-fold-inverted-logic

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. Finding: test report on #4289.

What and why

_step_state in tools/shader_meta.py follows the constants in the SGPRs. It did not fold the inverted scalar logic operations. Thus a literal S# that has one dword made with one of these operations was not in literal_samplers of the index.

Hand-written listing (no game data):

s_mov_b32 s12, 0x92
s_not_b32 s13, 0
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
  • main (03b09a08): literal_samplers is [].
  • This branch: literal_samplers is [[146, 4294967295, 0, 0]].

This change:

  • s_not_b32 of a known constant gives a known constant (the complement, 32 bits).
  • s_nand_b32, s_nor_b32 and s_xnor_b32 of two known constants fold together with the two-source logic operations that _fold_scalar_alu already has (s_and_b32, s_or_b32, s_xor_b32, s_andn2_b32, s_orn2_b32).
  • A source that is not a known constant gives no constant, as before. The destination then loses its old constant, as before.

The diff in tools/shader_meta.py is 13 added lines and 1 removed line.

Relation to #4289

#4289 folds s_brev_b32. The two changes are independent. This branch starts at main and does not need #4289. I merged #4289 (8479d6c0) into this branch as a test: Git merges the two files without a conflict, and tests/test_shader_meta.py gives 23 tests OK. With the two changes together, the listing from the report (s_not_b32 s13, 0, then s_brev_b32 s13, s13) gives [[146, 4294967295, 0, 0]].

Operations that I did not fold

A probe with hand-written listings shows that _step_state also does not fold s_ashr_i32, s_bfm_b32, s_bfe_u32, s_min_u32, s_max_u32 and s_abs_i32. I did not add them, to keep this change small. I do not know if a title uses them to make an S# dword.

Tested

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

New tests in tests/test_shader_meta.py:

  • test_inverted_scalar_logic_folds_into_literal_sampler: 11 listings. Each listing makes the dword s13 of the S# with s_not_b32, s_nand_b32, s_nor_b32 or s_xnor_b32 from immediates and from SGPRs that hold known constants. One listing uses the same register as source and destination. One listing folds the result a second time with s_and_b32.
  • test_inverted_scalar_logic_of_unknown_source_is_not_a_literal: 5 listings where one source is a user SGPR. The result must be no literal sampler, also when the destination held a constant before.
Command tools/shader_meta.py of main (03b09a08) This branch
python -I tests/test_shader_meta.py FAILED (failures=11): the 11 listings of the first new test 21 tests OK
python -I tests/test_shader_meta_operands.py OK 5 tests 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: no problem found.
  • 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

…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>
@Atomic258

Copy link
Copy Markdown
Contributor

Tested 14b51cc7de on Linux, Python 3.10, CPU only, synthetic fixtures, no game files: pass.

  • python -I tests/test_shader_meta.py → 21/21.
  • Negative control on main's tools/shader_meta.py: 11 failures, so the s_not/s_nand/s_nor/s_xnor folding cases discriminate.
  • git diff --check and check_no_game_data.py clean.

AI-assisted (Devin).

@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 14b51cc7 on Windows 11: python -I tests/test_shader_meta.py -> 21 tests, OK; git diff --check clean.

shader_meta now folds s_not_b32, s_nand_b32, s_nor_b32 and s_xnor_b32 of known constants, so an S# built with them reaches literal_samplers; the added cases pass. No blocking issue.

@CaravelaLabs

Copy link
Copy Markdown
Contributor

Automated test: 14b51cc7 merged onto main d0835dd9 on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5), Mesa 25.2.8, Ubuntu 24.04, runtime rebuilt from this branch in a clean sandbox. Result: pass.

Same machine and setup as my earlier manual reports. AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested 14b51cc merged with main d0835dd (merge 154d85a85, no conflicts) on Linux (WSL2) Python 3.12.16 and native Windows 11 Python 3.13.1: pass.

  • tests/test_shader_meta.py: 21/21 OK on both.
  • Negative control: tools/shader_meta.py from main, same tests: 11 failures (subtests of the two new tests) on Linux and on Windows.
  • Read the diff: s_not_b32, s_nand_b32, s_nor_b32, s_xnor_b32 fold only when every source is a known constant; a destination that held a constant is overwritten, not left stale, when the source is unknown (the test has s_mov s13, 0 then s_not s13, s3 and expects no literal sampler). Results are masked to 32 bits.

@memorypressure

Copy link
Copy Markdown

Verified on Linux: merges cleanly into current main; test_shader_meta 21/21 including the constant folds, chained folds and the unknown-source invalidation negatives (re-run); masking is 32-bit-clean and no other shader_meta consumer mentions the four mnemonics, so the widened const-prop cannot shift current analyses. Unrelated observation: tests/test_shader_index.py silently runs nothing under python -I — pre-existing on main, confirmed here, not from this PR. Checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@ItsNotRexed
ItsNotRexed merged commit 24f05e5 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.

7 participants