native: fold s_brev_b32 and s_bfe_u32 with constant operands #3526

Manually merged
github merged 2 commits from github-bot/gh-3524 into gh-pr/3524/base 2026-11-30 09:03:22 +00:00 AGit
Owner

@⁠JB2WIN on GitHub · 2026-11-29 22:46 UTC · original
JB2WIN:native/fold-brev-bfe-constants → main · +63 −3 · 3 files · 2 commits · merged 2026-11-30 08:59 UTC

Change

The native compute symbol analysis (transfer, src/gfx1013_compute_native.cpp) now folds s_brev_b32 with a constant source and s_bfe_u32 with two constant sources into constant symbols. Until now both always produced an unknown symbol.

Compilers write high-bit constants as s_brev_b32 sN, <inline constant>. For example, 0x80000000 is word 3 of a static sampler with an OPAQUE_WHITE border. A literal sampler built that way was refused as nonconstant sampler. With the fold it is a literal sampler.

The fold matches the DXIL lowering. For s_bfe_u32 it computes (S0 >> S1[4:0]) & ((1 << S1[22:16]) - 1), with width 0 giving 0 (gfx1013_direct_compute.inl, the s_bfe_u32/s_bfe_i32 case). A width of 32 or more is left unknown. s_brev_b32 is the 32-bit bit reverse.

Refs #3226 (claim: issuecomment-7263320436), #3023. This replaces #3519, which I closed.

Verification

Build cmake --preset release / cmake --build --preset release on 2ff74a6a, Windows 11, NVIDIA GeForce RTX 3080, hardware adapter.

  • native_sampler_heap_check.py, new fixture mode mixed_border: the literal static sampler of mixed is built with s_mov_b32 s44, 0x60008; s_bfe_u32 s40, 0x3200, s44, so word 0 is 0x32 (CLAMP on x, BORDER on y). Word 3 is s_brev_b32 s43, 1 (OPAQUE_WHITE). At the fixture coordinate (v = -0.375) every lane reads the white border and matches the CPU model. The fixture's CPU model now reads the address mode per axis. Before, it applied CLAMP_X to both axes; the other modes set the same mode on both axes, so their expectations are unchanged.
  • Compile controls: s_brev_b32 s15, 1 as word 3 of a literal sampler compiles. s_brev_b32 of v_readfirstlane_b32 s44, v0 and s_bfe_u32 with width 32 still refuse with nonconstant sampler.
  • ctest -R 'native_sampler|inline_sampler|native_exec_symbol|startup_compute_dispatch_native|native_direct|native_compute|native_salu|native_descriptor': 47/47 pass. native_sampler_heap_warp and startup_compute_dispatch_native_warp skip (wave32), as on main.
  • One-line mutations, each run on hardware: without the change, mixed_border is refused (fixture case mixed_border exits 0 fails). Bit index 31 - bit → 30 - bit: lane 0 reads (0, 0, 0, 1) instead of (1, 1, 1, 1). Mask width → width - 1: lane 0 reads the clamped texel (31, 2, 103, 1000).
  • Not covered: at this coordinate, the run pins the y field and the top field bit of the s_bfe_u32 result, not the x field (v is outside, so y alone decides).

Evidence and follow-up

All fixtures are source-built (LLVM 15 clang -cc1as -target-cpu gfx1013 encodings, in comments next to the words).

Local census on my 2,689 slot 67 compute requests (compile_dxil --refusals --jobs 8, local data, counts only), against the same main without the change: 828 compile both times. Two requests move from unbound numeric SGPR sN to the later direct resource-heap load requires the selected GPU slot and its view type. No request goes from compiled to refused.

On the #3023 prototype route, this turns word 3 of 299 comparison samplers into the constant 0x80000000. Those samplers then stop at the comparison path's CLAMP_LAST_TEXEL-only check (CLAMP_BORDER on x and y). That is the next PR.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed. (no change)
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

🤖 Generated with Claude Code

**@⁠JB2WIN** on GitHub · 2026-11-29 22:46 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3524) `JB2WIN:native/fold-brev-bfe-constants` → `main` · +63 −3 · 3 files · 2 commits · merged 2026-11-30 08:59 UTC ## Change The native compute symbol analysis (`transfer`, `src/gfx1013_compute_native.cpp`) now folds `s_brev_b32` with a constant source and `s_bfe_u32` with two constant sources into constant symbols. Until now both always produced an unknown symbol. Compilers write high-bit constants as `s_brev_b32 sN, <inline constant>`. For example, 0x80000000 is word 3 of a static sampler with an OPAQUE_WHITE border. A literal sampler built that way was refused as `nonconstant sampler`. With the fold it is a literal sampler. The fold matches the DXIL lowering. For `s_bfe_u32` it computes `(S0 >> S1[4:0]) & ((1 << S1[22:16]) - 1)`, with width 0 giving 0 (`gfx1013_direct_compute.inl`, the `s_bfe_u32`/`s_bfe_i32` case). A width of 32 or more is left unknown. `s_brev_b32` is the 32-bit bit reverse. Refs #3226 (claim: issuecomment-7263320436), #3023. This replaces #3519, which I closed. ## Verification Build `cmake --preset release` / `cmake --build --preset release` on `2ff74a6a`, Windows 11, NVIDIA GeForce RTX 3080, hardware adapter. - `native_sampler_heap_check.py`, new fixture mode `mixed_border`: the literal static sampler of `mixed` is built with `s_mov_b32 s44, 0x60008; s_bfe_u32 s40, 0x3200, s44`, so word 0 is 0x32 (CLAMP on x, BORDER on y). Word 3 is `s_brev_b32 s43, 1` (OPAQUE_WHITE). At the fixture coordinate (v = -0.375) every lane reads the white border and matches the CPU model. The fixture's CPU model now reads the address mode per axis. Before, it applied CLAMP_X to both axes; the other modes set the same mode on both axes, so their expectations are unchanged. - Compile controls: `s_brev_b32 s15, 1` as word 3 of a literal sampler compiles. `s_brev_b32` of `v_readfirstlane_b32 s44, v0` and `s_bfe_u32` with width 32 still refuse with `nonconstant sampler`. - `ctest -R 'native_sampler|inline_sampler|native_exec_symbol|startup_compute_dispatch_native|native_direct|native_compute|native_salu|native_descriptor'`: 47/47 pass. `native_sampler_heap_warp` and `startup_compute_dispatch_native_warp` skip (wave32), as on main. - One-line mutations, each run on hardware: without the change, `mixed_border` is refused (`fixture case mixed_border exits 0` fails). Bit index `31 - bit` → `30 - bit`: lane 0 reads (0, 0, 0, 1) instead of (1, 1, 1, 1). Mask width → width - 1: lane 0 reads the clamped texel (31, 2, 103, 1000). - Not covered: at this coordinate, the run pins the y field and the top field bit of the `s_bfe_u32` result, not the x field (v is outside, so y alone decides). ## Evidence and follow-up All fixtures are source-built (LLVM 15 `clang -cc1as -target-cpu gfx1013` encodings, in comments next to the words). Local census on my 2,689 slot 67 compute requests (`compile_dxil --refusals --jobs 8`, local data, counts only), against the same `main` without the change: 828 compile both times. Two requests move from `unbound numeric SGPR sN` to the later `direct resource-heap load requires the selected GPU slot and its view type`. No request goes from compiled to refused. On the #3023 prototype route, this turns word 3 of 299 comparison samplers into the constant 0x80000000. Those samplers then stop at the comparison path's CLAMP_LAST_TEXEL-only check (CLAMP_BORDER on x and y). That is the next PR. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. (no change) - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The compute symbol analysis left both instructions unknown even when
every operand is a constant. Compilers write high-bit constants as
s_brev_b32 sN, <inline constant> (0x80000000 is word 3 of a static
sampler with an OPAQUE_WHITE border), so such a literal sampler was
refused as "nonconstant sampler". Both now produce a constant symbol.
An s_bfe_u32 width of 32 or more stays unknown.

The sampler heap fixture gets a mixed_border mode whose literal sampler
is built with both instructions (CLAMP on x, BORDER on y, white border),
and its CPU model now reads the address mode per axis.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 22:51:16 +00:00
Author
Owner

@⁠Leyukaka on GitHub · 2026-11-29 23:06 UTC · original · approved these changes

Read at 1c89ddde (diff only, nothing built here). The new instruction words decode by hand to what the comments say (SOP1 op 0x0b s_brev_b32, SOP2 op 0x27 s_bfe_u32, literals and SGPR numbers included), and both folds match the DXIL lowering on main: src/gfx1013_direct_compute.inl:4505 (Bfrev) and :4777-4784 (width 0 gives 0, otherwise shift and mask; width >= 32 is left unknown here, which is the conservative side).

Each half has a check that fails on revert: without the s_bfe_u32 fold word 0 of the literal sampler is not constant and fixture case mixed_border exits 0 fails; without the s_brev_b32 fold the brev_word3 control fails; without the width < 32 guard bfe_wide_word0 is no longer refused.

Non-blocking:

  • On a runner without 32-lane waves the script exits 77 at the first fixture case (tests/native_sampler_heap_check.py:262-264, loop at :340), before the compile-only controls at :391-393, so CI does not run any of the new checks. Running the compile-only controls before the fixture loop would let CI cover brev_word3, brev_gpu_word3 and bfe_wide_word0.
  • transfer() is also called by the mesh path (src/gfx1013_direct_mesh.inl:693, :757, :815, :983, :1028, :1036, :1102), which the compute census does not cover. The change only turns an unknown into a constant there.
**@⁠Leyukaka** on GitHub · 2026-11-29 23:06 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3524#pullrequestreview-6105278042) · **approved these changes** Read at 1c89ddde (diff only, nothing built here). The new instruction words decode by hand to what the comments say (SOP1 op 0x0b `s_brev_b32`, SOP2 op 0x27 `s_bfe_u32`, literals and SGPR numbers included), and both folds match the DXIL lowering on main: `src/gfx1013_direct_compute.inl:4505` (Bfrev) and `:4777-4784` (width 0 gives 0, otherwise shift and mask; width >= 32 is left unknown here, which is the conservative side). Each half has a check that fails on revert: without the `s_bfe_u32` fold word 0 of the literal sampler is not constant and `fixture case mixed_border exits 0` fails; without the `s_brev_b32` fold the `brev_word3` control fails; without the `width < 32` guard `bfe_wide_word0` is no longer refused. Non-blocking: - On a runner without 32-lane waves the script exits 77 at the first fixture case (`tests/native_sampler_heap_check.py:262-264`, loop at `:340`), before the compile-only controls at `:391-393`, so CI does not run any of the new checks. Running the compile-only controls before the fixture loop would let CI cover `brev_word3`, `brev_gpu_word3` and `bfe_wide_word0`. - `transfer()` is also called by the mesh path (`src/gfx1013_direct_mesh.inl:693`, `:757`, `:815`, `:983`, `:1028`, `:1036`, `:1102`), which the compute census does not cover. The change only turns an unknown into a constant there.
A runner without 32-lane waves exits 77 at the first fixture case, so the
compile-only controls for the s_brev_b32/s_bfe_u32 fold now run first.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Owner

@⁠JB2WIN on GitHub · 2026-11-29 23:19 UTC · original

@⁠Leyukaka thanks. Moved the three compile-only fold controls ahead of the fixture loop in 73881a42. On WARP the check now prints them, then exits 77 at the first fixture case.

On the mesh callers of transfer(): it only turns an unknown into a constant there as well. The symbol stays numeric (Symbol::numeric() covers both), so require_numeric and the masked checks behave the same.

AI-assisted.

**@⁠JB2WIN** on GitHub · 2026-11-29 23:19 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3524#issuecomment-7264375727) @⁠Leyukaka thanks. Moved the three compile-only fold controls ahead of the fixture loop in 73881a42. On WARP the check now prints them, then exits 77 at the first fixture case. On the mesh callers of `transfer()`: it only turns an unknown into a constant there as well. The symbol stays numeric (`Symbol::numeric()` covers both), so `require_numeric` and the masked checks behave the same. AI-assisted.
github-bot changed target branch from main to gh-pr/3524/base 2026-11-30 09:03:19 +00:00
github manually merged commit 73881a42f4 into gh-pr/3524/base 2026-11-30 09:03:22 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.