startup: fold s_brev_b32 of a known constant in gfx2hlsl #2311
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2319"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by denn0x on 2026-11-25T12:09:00Z.
What
An inline sampler descriptor can be built from scalar constants with
s_movk_i32,s_bfm_b32,s_mov_b32ands_brev_b32. For example,s_brev_b32 sN, 1sets only bit 31. The first three already gave known words intools/startup/gfx2hlsl.py.s_brev_b32was emitted as a run-timereversebits, so a descriptor with one such word was refused asdescriptor operand s[24:27] holds ('const', 0).Stage.scalarnow folds the operations listed inSCALAR_CONST_FOLDwhen every operand is a known constant (an inline integer or an SGPR that holds a constant). Onlys_brev_b32is listed (AMD RDNA 2 ISA:D[31:0] = S0[0:31], no SCC write). A run-time operand keepsreversebits.Cache revision. The change alters the HLSL emitted for stages that already translated:
reversebits(c)becomes the folded word. The shader cache revision therefore moves tox2w-startup-shader-13. The new value appears in:d3d12_draw.inl;pipeline_keyandcompute_key;startup_indirect_draw_indexed.cpp;startup_compute_request_offline_check.py;docs/startup-runtime.md.I skipped 12 because open #2308 takes it. Whichever PR lands second will need a rebase of the revision lines.
Test
tests/startup_gfx2hlsl_scalar_brev_fold_check.py(new). It is registered in CMake asstartup_gfx2hlsl_scalar_brev_fold, in the Python CI list and in AGENTS.md. The fixture is a hand-assembled pixel stage, and the decoder reads it back. The expected values are computed bit by bit in the test, not with the translator's table.s_brev_b32 s1, 1gives 0x80000000, and a brev of the foldeds_bfm_b32 s0, 2, 22gives 0x00000300. Both are known constants, and the emitted lines are the folded words.startup_image_3d_checkstage, with its fourth sampler word written bys_brev_b32 s27, 0, now samples through the constant samplersmp 0 0x0 0x0 0x0 0x0.s2 = reversebits(s5);;On
main, the check fails at the first constant (AssertionError: (1, None)).Other checks on this branch:
tests/startup_gfx2hlsl_*_check.py,tests/startup_image_*_check.py,startup_translator_check.py,startup_flat_param_check.py,startup_inline_sampler_check.pyandstartup_compute_request_offline_check.pypasses;startup_stream_consumer:startup_stream_check.pyandstartup_compute_request_check.py, plus itsdisablednegative control, which prints the expectedFAIL;startup_indirect_draw_indexed warpandwarp drawpass.Environment
allbuilds (Ninja Release, MSVC Build Tools 18).Refs #1723
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T12:14:23Z.
Review: COMMENTED
Review of head
194815adf67cb2d48a855e744f3e0445d94049f2(comment only).Summary: the fold logic is correct and the new test kills mutations. The PR cannot merge as it stands: it conflicts with main on the shader cache revision.
P1 (blocker): merge conflict, revision now stale.
gh pr viewreportsCONFLICTING. #2308 is merged, so main (31f46857) already carriesx2w-startup-shader-12. The PR bumps 11 to 13.git merge-tree origin/main HEADreports conflicts insrc/startup_stubs/d3d12_draw.inl,docs/startup-runtime.mdandtests/startup_compute_request_offline_check.py. After the rebase, resolve each place to-13(and-13cs) and check these all agree:d3d12_draw.inl(both literals),gfx2hlsl.pypipeline_keyandcompute_key,startup_indirect_draw_indexed.cpp, the offline check literal and the docs line. Keep the docs sentence about the #2308 change as well as the new one.P2: none.
Nit
tools/startup/gfx2hlsl.py(newSCALAR_CONST_FOLDblock): the new comment sits between the "shift amount of the scalar shift-and-add forms" comment andSHIFT_ADD, so that comment no longer sits next to the table it describes. Move the new block above it.Correctness (checked against the diff)
s_brev_b32D = S0 bit-reversed, no SCC write (RDNA 2 ISA). The fold writes no SCC and discards the mask, consistent with that.FLOATS) is not folded. An unknown SGPR keepsreversebits.Commands run in the worktree (all passed)
python3 -B tests/startup_gfx2hlsl_scalar_brev_fold_check.pypython3 -B tests/startup_compute_request_offline_check.pypython3 -B tests/startup_inline_sampler_check.pypython3 -B tests/startup_gfx2hlsl_sopk_signext_check.pypython3 tests/docs_links_check.py(448 links)python3 tests/bidi_controls_check.pygit diff --check origin/main...HEAD(clean)Mutations (each reverted with
git checkout -- .)AssertionError: (1, ('const', 1)). Killed.if None not in operandstoif False): test fails withAssertionError: (1, None). Killed.CI / hygiene: no checks are reported on the branch (
gh pr checks). Title and body are neutral. The test is wired into CMake,windows.ymlandAGENTS.md. I did not run the Windows WARP job: the change is Python-only constant folding covered by the offline test. I ran the new test script directly, not through ctest.Ask: rebase on main, resolve the revision conflicts as above, then push.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T12:27:46Z.
Merged current main into the PR at
1827682b. The graphics and compute keys and their fixtures now use revision 13 together, and the revision 12 SAVEEXEC fix and regression are still present.Locally the constant-folding, request-key, SAVEEXEC and translator Python checks passed. The Windows Release stream, compute-request, indirect-draw and folding selection passed all 11 tests after building the separate stream-depth target and rerunning that case. Hosted CI is running on the new head.