a-publier: a scalar counted in a divergent loop is read per lane after it (each lane reads its own iteration count) #352

Open
opened 2026-11-27 00:58:18 +00:00 by github-bot · 6 comments
Owner

@⁠adrianmjim on GitHub · 2026-11-26 21:41 UTC · original

On a-publier (cd8dfc0), a scalar counted inside a loop whose lanes leave at different iterations is read after the loop as each lane's own count, not as the console's. Found while checking whether #110's wave-size rule (which targets main) applies to this line. It doesn't apply, because the mechanism it adjusts is not here.

What the console does. A scalar register (SGPR) is one value per wave. In a loop where lane i stays for i + 1 iterations, an s_add inside the loop runs once per wave iteration while any lane is still in it. So after the loop every lane reads 64 for a wave64 group of 64 threads. main lowers this with leave_loops / find_loop_exits in emit/lower.rs. Those come from main's first commit and are not on this line. #110 then makes such a program ask for the console's wave size.

What a-publier gives. The three tests from main (two from #110, one from its follow-up c20a5d0), ported to this line's test harness with the same shader words, all fail on an RX 7900 XT (Windows 11 26300). They run on the hardware adapter:

test expected got
scalar_counted_in_a_divergent_loop_read_after_it (wave64, 64 threads) 64 on every lane lane i reads i + 1 (1, 2, …, 64)
scalar_counted_in_nested_divergent_loops 16 on every lane 1, 2, 3, 4, …, 16 (per-lane products)
scalar_counted_in_a_wave32_divergent_loop_read_after_it (wave32, 64 threads) 32 on lanes 0–31, 64 on lanes 32–63 lane i reads i + 1

Each lane gets its own value, so this is not a wrong wave size. The emitted programs also declare no WaveSize (PSV0 range (0, u32::MAX)). The scalar is lowered per lane, and its join after the loop doesn't take the wave's value. The rest of victory-shader --lib passes on this line (302 passed, 4 ignored, besides these three).

Reproduction: branch https://github.com/adrianmjim/VIctory-Belobug-AMD/tree/publier/divergent-loop-probes (471e11b, one commit on cd8dfc0, tests only, in emit/valu_tests.rs):

cargo test -p victory-shader --lib -- --test-threads=1 --nocapture scalar_counted

Not known: how many of the title's programs have this pattern, and what it does in the game. Fixing it here would mean porting main's loop-exit handling first, and then #110 and c20a5d0 on top. #204 targets main and doesn't change the per-lane count.

**@⁠adrianmjim** on GitHub · 2026-11-26 21:41 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/issues/352) On `a-publier` (`cd8dfc0`), a scalar counted inside a loop whose lanes leave at different iterations is read after the loop as each lane's own count, not as the console's. Found while checking whether #110's wave-size rule (which targets `main`) applies to this line. It doesn't apply, because the mechanism it adjusts is not here. **What the console does.** A scalar register (SGPR) is one value per wave. In a loop where lane `i` stays for `i + 1` iterations, an `s_add` inside the loop runs once per wave iteration while any lane is still in it. So after the loop every lane reads 64 for a wave64 group of 64 threads. `main` lowers this with `leave_loops` / `find_loop_exits` in `emit/lower.rs`. Those come from `main`'s first commit and are not on this line. #110 then makes such a program ask for the console's wave size. **What `a-publier` gives.** The three tests from `main` (two from #110, one from its follow-up `c20a5d0`), ported to this line's test harness with the same shader words, all fail on an RX 7900 XT (Windows 11 26300). They run on the hardware adapter: | test | expected | got | |---|---|---| | `scalar_counted_in_a_divergent_loop_read_after_it` (wave64, 64 threads) | 64 on every lane | lane `i` reads `i + 1` (1, 2, …, 64) | | `scalar_counted_in_nested_divergent_loops` | 16 on every lane | 1, 2, 3, 4, …, 16 (per-lane products) | | `scalar_counted_in_a_wave32_divergent_loop_read_after_it` (wave32, 64 threads) | 32 on lanes 0–31, 64 on lanes 32–63 | lane `i` reads `i + 1` | Each lane gets its own value, so this is not a wrong wave size. The emitted programs also declare no `WaveSize` (PSV0 range `(0, u32::MAX)`). The scalar is lowered per lane, and its join after the loop doesn't take the wave's value. The rest of `victory-shader --lib` passes on this line (302 passed, 4 ignored, besides these three). **Reproduction:** branch https://github.com/adrianmjim/VIctory-Belobug-AMD/tree/publier/divergent-loop-probes (`471e11b`, one commit on `cd8dfc0`, tests only, in `emit/valu_tests.rs`): ``` cargo test -p victory-shader --lib -- --test-threads=1 --nocapture scalar_counted ``` **Not known:** how many of the title's programs have this pattern, and what it does in the game. Fixing it here would mean porting `main`'s loop-exit handling first, and then #110 and `c20a5d0` on top. #204 targets `main` and doesn't change the per-lane count.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 00:58:18 +00:00
Author
Owner

@⁠ANDREANIADS on GitHub · 2026-11-26 21:49 UTC · original

I reproduced all three fixtures on a physical GeForce RTX 3070, NVIDIA driver 621.40, Windows build 26300. I used your test commit 471e11b, based on a-publier@cd8dfc0, selected the adapter with VICTORY_D3D12_ADAPTER=RTX 3070, and ran with VICTORY_WARP=0 and one test thread. All three shaders created pipelines, executed and returned all 64 words.

Fixture Actual NVIDIA readback for lane i Values that differ from expected
Single loop, wave64 i + 1 63/64
Single loop, wave32 i + 1 62/64
Nested loops (1 + i % 4) * (1 + (i >> 4)) 60/64

All three emitted a PSV0 range of (0, 4294967295). The compiler in these fixtures lowers before selecting the device, with the default unknown host width. The results match your RX 7900 XT observations on another vendor. Release build, Rust 1.98.0 MSVC and offline dependency versions. No game assets or in-game inference. Tested with Codex.

**@⁠ANDREANIADS** on GitHub · 2026-11-26 21:49 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/issues/352#issuecomment-7204086646) I reproduced all three fixtures on a physical **GeForce RTX 3070**, NVIDIA driver 621.40, Windows build 26300. I used your test commit `471e11b`, based on `a-publier@cd8dfc0`, selected the adapter with `VICTORY_D3D12_ADAPTER=RTX 3070`, and ran with `VICTORY_WARP=0` and one test thread. All three shaders created pipelines, executed and returned all 64 words. | Fixture | Actual NVIDIA readback for lane i | Values that differ from expected | |---|---|---:| | Single loop, wave64 | `i + 1` | 63/64 | | Single loop, wave32 | `i + 1` | 62/64 | | Nested loops | `(1 + i % 4) * (1 + (i >> 4))` | 60/64 | All three emitted a PSV0 range of `(0, 4294967295)`. The compiler in these fixtures lowers before selecting the device, with the default unknown host width. The results match your RX 7900 XT observations on another vendor. Release build, Rust 1.98.0 MSVC and offline dependency versions. No game assets or in-game inference. Tested with Codex.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 21:50 UTC · original

Thanks @⁠ANDREANIADS. The same per-lane values on an NVIDIA card (32-lane waves only) as on the RX 7900 XT (which runs these as wave64) rule out the driver and the host's wave size: it is the translation on this line, the scalar lowered per lane with no wave-level join after the loop.

**@⁠adrianmjim** on GitHub · 2026-11-26 21:50 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/issues/352#issuecomment-7204103445) Thanks @⁠ANDREANIADS. The same per-lane values on an NVIDIA card (32-lane waves only) as on the RX 7900 XT (which runs these as wave64) rule out the driver and the host's wave size: it is the translation on this line, the scalar lowered per lane with no wave-level join after the loop.
Author
Owner

@⁠fladit123 on GitHub · 2026-11-26 22:04 UTC · original

I independently reproduced the two Wave64 scalar-count regressions on a-publier@cd8dfc06 using a physical NVIDIA RTX 4060 (driver 610.88).

The incorrect per-lane results were identical with the default configuration, VICTORY_WAVE_LOCAL=0, and VICTORY_WAVE_LOCAL=idiom.

I now have a local experimental correction targeting eligible single-console-wave compute groups. It makes divergent loop branches vote across both Wave32 halves while preserving per-lane EXEC guards for vector operations.

Current results:

  • Simple divergent loop: 64 in all 64 lanes.
  • Nested divergent loops: 16 in all 64 lanes.
  • Both passed production emission, DXIL validation, D3D12 dispatch and GPU readback.
  • Focused validation: 11 GPU tests and one CPU control passed.
  • Bounded library comparison: 300 baseline passes versus 308 candidate passes, zero failures, four ignored tests each, and identical capability skips. Two dispatch-heavy tests were excluded from both runs.

The separate native Wave32 scalar-count failure remains outside the current patch's scope.

I am reviewing the synchronization and control-flow implications before submitting a PR targeting a-publier.

Thanks to the contributors of #110, #299 and #352 for the prior investigation and regression fixtures.

No physical AMD validation or gameplay performance claims are being made.

AI-assisted investigation, implementation and testing using OpenAI ChatGPT and Codex.

**@⁠fladit123** on GitHub · 2026-11-26 22:04 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/issues/352#issuecomment-7204330014) I independently reproduced the two Wave64 scalar-count regressions on `a-publier@cd8dfc06` using a physical NVIDIA RTX 4060 (driver 610.88). The incorrect per-lane results were identical with the default configuration, `VICTORY_WAVE_LOCAL=0`, and `VICTORY_WAVE_LOCAL=idiom`. I now have a local experimental correction targeting eligible single-console-wave compute groups. It makes divergent loop branches vote across both Wave32 halves while preserving per-lane EXEC guards for vector operations. Current results: - Simple divergent loop: 64 in all 64 lanes. - Nested divergent loops: 16 in all 64 lanes. - Both passed production emission, DXIL validation, D3D12 dispatch and GPU readback. - Focused validation: 11 GPU tests and one CPU control passed. - Bounded library comparison: 300 baseline passes versus 308 candidate passes, zero failures, four ignored tests each, and identical capability skips. Two dispatch-heavy tests were excluded from both runs. The separate native Wave32 scalar-count failure remains outside the current patch's scope. I am reviewing the synchronization and control-flow implications before submitting a PR targeting `a-publier`. Thanks to the contributors of #110, #299 and #352 for the prior investigation and regression fixtures. No physical AMD validation or gameplay performance claims are being made. AI-assisted investigation, implementation and testing using OpenAI ChatGPT and Codex.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 22:04 UTC · original

Thanks @⁠fladit123, good to have it on a third card. When you open the PR against a-publier, I can run it on the RX 7900 XT. That's the AMD side you haven't covered: an RDNA3 host that runs these console programs as wave64 natively, where the wave32 test (scalar_counted_in_a_wave32_divergent_loop_read_after_it, lanes 0–31 should read 32 and lanes 32–63 64) also gives each lane its own count today. I'd run the three fixtures from 471e11b plus victory-shader --lib, before and after.

**@⁠adrianmjim** on GitHub · 2026-11-26 22:04 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/issues/352#issuecomment-7204336442) Thanks @⁠fladit123, good to have it on a third card. When you open the PR against `a-publier`, I can run it on the RX 7900 XT. That's the AMD side you haven't covered: an RDNA3 host that runs these console programs as wave64 natively, where the wave32 test (`scalar_counted_in_a_wave32_divergent_loop_read_after_it`, lanes 0–31 should read 32 and lanes 32–63 64) also gives each lane its own count today. I'd run the three fixtures from `471e11b` plus `victory-shader --lib`, before and after.
Author
Owner

@⁠fladit123 on GitHub · 2026-11-26 22:06 UTC · original

Thanks, @⁠adrianmjim! Independent RDNA3 validation would be very helpful.

The current implementation is intentionally restricted to single-console-wave compute groups on hosts with a maximum wave size of 32. It does not yet address the native Wave32 scalar regression or native Wave64 execution.

I am finishing the portability and synchronization review before opening the PR against a-publier. I will include the reproduction instructions and limitations so that the AMD results can be compared accurately.

**@⁠fladit123** on GitHub · 2026-11-26 22:06 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/issues/352#issuecomment-7204374938) Thanks, @⁠adrianmjim! Independent RDNA3 validation would be very helpful. The current implementation is intentionally restricted to single-console-wave compute groups on hosts with a maximum wave size of 32. It does not yet address the native Wave32 scalar regression or native Wave64 execution. I am finishing the portability and synchronization review before opening the PR against `a-publier`. I will include the reproduction instructions and limitations so that the AMD results can be compared accurately.
Author
Owner

@⁠fladit123 on GitHub · 2026-11-26 22:25 UTC · original

@⁠adrianmjim The draft PR is now available: #366.

The final revision is ac445ea90803500b610c801df236d98f5bff47eb.

The portability changes are complete, and the RTX 4060 passed 12 GPU tests and 3 CPU-only controls.

The targeted tests can be run with:

cargo test --locked -p victory-shader --lib pr299_ scalar_loop_control_ -- --nocapture --test-threads=1

The fixtures perform CPU emission checks for the Wave32-only path independently of hardware availability and report explicit GPU capability skips.

As discussed, the correction does not target native Wave64 execution or the separate native Wave32 scalar regression. Your RX 7900 XT comparison would be valuable for checking that the existing AMD execution paths remain unaffected.

Thank you for offering independent validation!

**@⁠fladit123** on GitHub · 2026-11-26 22:25 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/issues/352#issuecomment-7204662108) @⁠adrianmjim The draft PR is now available: #366. The final revision is `ac445ea90803500b610c801df236d98f5bff47eb`. The portability changes are complete, and the RTX 4060 passed 12 GPU tests and 3 CPU-only controls. The targeted tests can be run with: ```powershell cargo test --locked -p victory-shader --lib pr299_ scalar_loop_control_ -- --nocapture --test-threads=1 ``` The fixtures perform CPU emission checks for the Wave32-only path independently of hardware availability and report explicit GPU capability skips. As discussed, the correction does not target native Wave64 execution or the separate native Wave32 scalar regression. Your RX 7900 XT comparison would be valuable for checking that the existing AMD execution paths remain unaffected. Thank you for offering independent validation!
This discussion has been locked. Commenting is limited to contributors.
No description provided.