shader: lower s_ff1_i32 of a tracked scalar #2058

Merged
SimSalabimse merged 1 commit from upstream-pr/2065 into main 2026-11-25 00:08:31 +00:00
SimSalabimse commented 2026-11-24 23:55:01 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by SimSalabimse on 2026-11-24T23:55:01Z.


Part of #2022 (item F)

Summary

tools/compute_store_aot.py lowers s_ff1_i32_b32 and s_ff1_i32_b64 when the source is a tracked data word (the same words scalar ALU already accepts). The result is the index of the lowest set bit, or 0xffffffff when the source is 0. b64 reads an even SGPR pair and scans all 64 bits. SCC stays unmodelled.

s_ff1_i32_b64 sD, exec and s_ff1_i32_b32 sD, exec_lo still fold to 0 outside a guarded region. Untracked SGPRs, literals, descriptors, the saved exec mask, vcc, exec_hi, a mismatched width, and s_ff0_i32_b32 stay refused.

LLVM 15.0.7 selects both opcodes as AMDGPUffbl_b32 (llvm/lib/Target/AMDGPU/SOPInstructions.td lines 259-264) and defines that node as count-trailing-zeros with -1 when the input is zero (llvm/lib/Target/AMDGPU/AMDGPUISelLowering.h line 433). Mesa 23.3.6 defines find_lsb as that scan, returning -1 when no bit is set (src/compiler/nir/nir_opcodes.py lines 550-558), and ACO selects a scalar 32-bit source to s_ff1_i32_b32 and a scalar 64-bit source to s_ff1_i32_b64 (src/amd/compiler/aco_instruction_selection.cpp lines 1757-1764).

Fixture

Hand-assembled gfx1013, one group of 32 threads, four stored words: b32 of 0, of 1 (bit 0), and of 0x80000000 (bit 31), and b64 whose only set bit is bit 63. The reference is an independent bit scan. Dropping the scalar lowering makes the translation control raise Unsupported first active lane source.

Tests

./out/pe-env/bin/python -B tools/run_checks_parallel.py   # 77 platform-independent Python checks
exit 0

./out/pe-env/bin/python -B tests/compute_store_check.py binary llvm-mc-15 llvm-objcopy-15 libLLVM-15.so.1
exit 0

LLVM 15.0.7. Negative control: with the scalar lowering disabled, ff1_scalar_controls exited 1 (Unsupported first active lane source: s4). Restored before commit.

Windows 11 (MSVC 19.42, RTX 4070 Ti, driver 621.35), dev build at a81a6f07:

ctest --preset dev --no-tests=error --output-on-failure -R "^compute_store_aot_ff1_scalar_(warp|hardware)$"
compute_store_aot_ff1_scalar_hardware ... Passed
compute_store_aot_ff1_scalar_warp ....... Passed
100% tests passed out of 2, exit 0

AI-assisted (Cursor)

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6593873266 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2065). Originally posted by [SimSalabimse](https://github.com/SimSalabimse) on 2026-11-24T23:55:01Z. --- Part of #2022 (item F) ## Summary `tools/compute_store_aot.py` lowers `s_ff1_i32_b32` and `s_ff1_i32_b64` when the source is a tracked data word (the same words scalar ALU already accepts). The result is the index of the lowest set bit, or `0xffffffff` when the source is 0. `b64` reads an even SGPR pair and scans all 64 bits. `SCC` stays unmodelled. `s_ff1_i32_b64 sD, exec` and `s_ff1_i32_b32 sD, exec_lo` still fold to 0 outside a guarded region. Untracked SGPRs, literals, descriptors, the saved exec mask, `vcc`, `exec_hi`, a mismatched width, and `s_ff0_i32_b32` stay refused. LLVM 15.0.7 selects both opcodes as `AMDGPUffbl_b32` (`llvm/lib/Target/AMDGPU/SOPInstructions.td` lines 259-264) and defines that node as count-trailing-zeros with -1 when the input is zero (`llvm/lib/Target/AMDGPU/AMDGPUISelLowering.h` line 433). Mesa 23.3.6 defines `find_lsb` as that scan, returning -1 when no bit is set (`src/compiler/nir/nir_opcodes.py` lines 550-558), and ACO selects a scalar 32-bit source to `s_ff1_i32_b32` and a scalar 64-bit source to `s_ff1_i32_b64` (`src/amd/compiler/aco_instruction_selection.cpp` lines 1757-1764). ## Fixture Hand-assembled gfx1013, one group of 32 threads, four stored words: `b32` of 0, of 1 (bit 0), and of `0x80000000` (bit 31), and `b64` whose only set bit is bit 63. The reference is an independent bit scan. Dropping the scalar lowering makes the translation control raise `Unsupported first active lane source`. ## Tests ```text ./out/pe-env/bin/python -B tools/run_checks_parallel.py # 77 platform-independent Python checks exit 0 ./out/pe-env/bin/python -B tests/compute_store_check.py binary llvm-mc-15 llvm-objcopy-15 libLLVM-15.so.1 exit 0 ``` LLVM 15.0.7. Negative control: with the scalar lowering disabled, `ff1_scalar_controls` exited 1 (`Unsupported first active lane source: s4`). Restored before commit. Windows 11 (MSVC 19.42, RTX 4070 Ti, driver 621.35), dev build at `a81a6f07`: ```text ctest --preset dev --no-tests=error --output-on-failure -R "^compute_store_aot_ff1_scalar_(warp|hardware)$" compute_store_aot_ff1_scalar_hardware ... Passed compute_store_aot_ff1_scalar_warp ....... Passed 100% tests passed out of 2, exit 0 ``` AI-assisted (Cursor)
upstream-sync closed this pull request 2026-11-25 01:01:25 +00:00
tedin7 commented 2026-11-25 01:01:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T00:07:50Z.

Review: COMMENTED


Review of head a81a6f0738 (shader: lower s_ff1_i32 of a tracked scalar).

Findings: 0 P1, 0 P2. Merge-tree against origin/main is clean; PR is MERGEABLE.

  • Semantics: s_ff1_i32_b32 of a tracked data word lowers to firstbitlow(word) (0xffffffff for 0). b64 takes an even SGPR pair, low half first, else 32 plus the high half, else 0xffffffff. The exec/exec_lo fold is unchanged. Untracked, pending, descriptor, saved-exec, odd or mis-sized pair, and wrong-width sources are refused, each with a control.
  • CMakeLists.txt: only registers compute_store_aot_ff1_scalar_<adapter>.
  • Clean-room: docs cite public LLVM 15.0.7 and Mesa 23.3.6 sources; the Python reference is an independent bit scan.

Commands executed locally:

  • python3 -B tests/compute_store_check.py (control section runs at start and passed): PASS
  • python3 tests/docs_links_check.py: PASS (18 files, 389 links)
  • python3 tests/bidi_controls_check.py: PASS
  • git diff --check origin/main...HEAD: PASS
  • gh pr checks 2065: Windows build and test, Linux Python checks, gfx1013 fixtures all pass
  • python3 x2w_pipe.py win2025 2065 --wait 900: Debug (dev) PASS, Release /O2 PASS, includes compute_store_aot_ff1_scalar_warp

Mutations killed (reverted afterwards):

  1. b64 high-half offset 32u changed to 31u in tools/compute_store_aot.py: controls fail on the exact-HLSL assertion.
  2. b64 all-zero result 0xffffffffu changed to 0u: controls fail on the exact-HLSL assertion.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6049495734 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2065#pullrequestreview-6049495734). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T00:07:50Z. **Review:** COMMENTED --- Review of head a81a6f073827da4fb2c017340d8bcec433397c72 (shader: lower s_ff1_i32 of a tracked scalar). Findings: 0 P1, 0 P2. Merge-tree against origin/main is clean; PR is MERGEABLE. - Semantics: `s_ff1_i32_b32` of a tracked data word lowers to `firstbitlow(word)` (0xffffffff for 0). `b64` takes an even SGPR pair, low half first, else 32 plus the high half, else 0xffffffff. The exec/exec_lo fold is unchanged. Untracked, pending, descriptor, saved-exec, odd or mis-sized pair, and wrong-width sources are refused, each with a control. - CMakeLists.txt: only registers `compute_store_aot_ff1_scalar_<adapter>`. - Clean-room: docs cite public LLVM 15.0.7 and Mesa 23.3.6 sources; the Python reference is an independent bit scan. Commands executed locally: - `python3 -B tests/compute_store_check.py` (control section runs at start and passed): PASS - `python3 tests/docs_links_check.py`: PASS (18 files, 389 links) - `python3 tests/bidi_controls_check.py`: PASS - `git diff --check origin/main...HEAD`: PASS - `gh pr checks 2065`: Windows build and test, Linux Python checks, gfx1013 fixtures all pass - `python3 x2w_pipe.py win2025 2065 --wait 900`: Debug (dev) PASS, Release /O2 PASS, includes `compute_store_aot_ff1_scalar_warp` Mutations killed (reverted afterwards): 1. b64 high-half offset `32u` changed to `31u` in `tools/compute_store_aot.py`: controls fail on the exact-HLSL assertion. 2. b64 all-zero result `0xffffffffu` changed to `0u`: controls fail on the exact-HLSL assertion. AI-assisted; commands listed were executed locally.
ChazY11 commented 2026-11-25 01:02:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-25T00:00:54Z.


Executed your check on both adapters here, then mutated the lowering three ways — caught on each backend

This PR's test needs dxc and a built offscreen_check, both of which exist on this machine, so unlike the pure-Python PRs I could run the real draw:

out/pe-env/Scripts/python.exe -B tests/compute_store_check.py draw-ff1-scalar \
    "<Windows Kits>/10/bin/10.0.26100.0/x64/dxc.exe" \
    out/build/dev/offscreen_check.exe warp

backend=warp debug_layer=enabled
adapter=Microsoft Basic Render Driver vendor=1414 device=008c
driver=10.0.26100.9818
groups=1 compared_words=320 mismatches=0
debug_errors=0 debug_warnings=0                                   exit 0

and on the physical adapter:

backend=hardware debug_layer=enabled
adapter=NVIDIA GeForce RTX 5080 Laptop GPU vendor=10de device=2c59
driver=32.0.15.8142
max_view_descriptor_heap_size=1000000
groups=1 compared_words=320 mismatches=0
debug_errors=0 debug_warnings=0                                   exit 0

Windows 11 build 26300.9974, DXC 10.0.26100.0, debug layer enabled on both.

Mutation, run against both backends

mutation in tools/compute_store_aot.py warp hardware
the b64 high half loses 32u + caught caught
the b64 "no bit set" result becomes 0u instead of 0xffffffffu caught caught
b32 uses firstbithigh instead of firstbitlow caught caught

All three fail through the actual comparison (mismatches), not through a compile error, so the fixture is checking values on a real device rather than
text. That is the strongest form this review can take, and it is worth saying in the PR body: 320 compared words, two adapters, zero debug-layer
errors.

The one property worth extra attention

({lo} != 0u ? firstbitlow({lo}) : ({hi} != 0u ? 32u + firstbitlow({hi}) : 0xffffffffu)) — the 0xffffffffu case is what an all-zero 64-bit pair
returns, and it is the value a caller tests against to mean "no active lane". A zero there would be indistinguishable from lane 0 and would silently
select a lane that is not active. It is mutation two, and it fails on both adapters.

What is still not covered

The pairing rules (lo % 2 == 0, hi == lo + 1, hi <= 105) are refusals, and a draw fixture cannot show them: they only fire on assemblies the
fixture never emits. If you want those pinned, they belong in the refusal-form check rather than here — the same split other handlers in this file use.

Notes

  • AI-assisted review; both adapters were executed locally.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7148041179 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2065#issuecomment-7148041179). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-25T00:00:54Z. --- ## Executed your check on both adapters here, then mutated the lowering three ways — caught on each backend This PR's test needs `dxc` and a built `offscreen_check`, both of which exist on this machine, so unlike the pure-Python PRs I could run the real draw: ```powershell out/pe-env/Scripts/python.exe -B tests/compute_store_check.py draw-ff1-scalar \ "<Windows Kits>/10/bin/10.0.26100.0/x64/dxc.exe" \ out/build/dev/offscreen_check.exe warp backend=warp debug_layer=enabled adapter=Microsoft Basic Render Driver vendor=1414 device=008c driver=10.0.26100.9818 groups=1 compared_words=320 mismatches=0 debug_errors=0 debug_warnings=0 exit 0 ``` and on the physical adapter: ```text backend=hardware debug_layer=enabled adapter=NVIDIA GeForce RTX 5080 Laptop GPU vendor=10de device=2c59 driver=32.0.15.8142 max_view_descriptor_heap_size=1000000 groups=1 compared_words=320 mismatches=0 debug_errors=0 debug_warnings=0 exit 0 ``` Windows 11 build 26300.9974, DXC 10.0.26100.0, debug layer enabled on both. ## Mutation, run against **both** backends | mutation in `tools/compute_store_aot.py` | warp | hardware | |---|---|---| | the b64 high half loses `32u +` | **caught** | **caught** | | the b64 "no bit set" result becomes `0u` instead of `0xffffffffu` | **caught** | **caught** | | b32 uses `firstbithigh` instead of `firstbitlow` | **caught** | **caught** | All three fail through the actual comparison (`mismatches`), not through a compile error, so the fixture is checking values on a real device rather than text. That is the strongest form this review can take, and it is worth saying in the PR body: 320 compared words, two adapters, zero debug-layer errors. ## The one property worth extra attention `({lo} != 0u ? firstbitlow({lo}) : ({hi} != 0u ? 32u + firstbitlow({hi}) : 0xffffffffu))` — the `0xffffffffu` case is what an all-zero 64-bit pair returns, and it is the value a caller tests against to mean "no active lane". A zero there would be indistinguishable from lane 0 and would silently select a lane that is not active. It is mutation two, and it fails on both adapters. ## What is still not covered The pairing rules (`lo % 2 == 0`, `hi == lo + 1`, `hi <= 105`) are refusals, and a draw fixture cannot show them: they only fire on assemblies the fixture never emits. If you want those pinned, they belong in the refusal-form check rather than here — the same split other handlers in this file use. ## Notes - AI-assisted review; both adapters were executed locally.
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!2058
No description provided.