native: fold s_brev_b32 and s_bfe_u32 with constant operands #3524

Merged
JB2WIN merged 2 commits from upstream-pr/3524 into main 2026-11-30 08:59:15 +00:00
JB2WIN commented 2026-11-29 22:46:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by JB2WIN on 2026-11-29T22:46:00Z.


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 #3224 (claim: issuecomment-7263320436), #3023. This replaces #3517, 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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5555967124 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3524). Originally posted by [JB2WIN](https://github.com/JB2WIN) on 2026-11-29T22:46:00Z. --- ## 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 #3224 (claim: issuecomment-7263320436), #3023. This replaces #3517, 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)
Leyukaka commented 2026-11-29 23:06:14 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-29T23:06:14Z.

Review: APPROVED


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.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6105278042 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3524#pullrequestreview-6105278042). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-29T23:06:14Z. **Review:** APPROVED --- 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.
JB2WIN commented 2026-11-29 23:19:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by JB2WIN on 2026-11-29T23:19:42Z.


@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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7264375727 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3524#issuecomment-7264375727). Originally posted by [JB2WIN](https://github.com/JB2WIN) on 2026-11-29T23:19:42Z. --- @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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!3524
No description provided.