Shaders: a lane-branch region that only raises an LDS flag runs as two host waves (one more wave64 compute on NVIDIA) #592

Open
github-bot wants to merge 1 commit from github-bot/gh-592 into main AGit
Owner

@⁠FrancescoGelosoEterea on GitHub · 2026-11-25 02:45 UTC · original
FrancescoGelosoEterea:lds-flags → main · +339 −2 · 2 files · 1 commits · open

Why

On a 32-lane host (NVIDIA), a console wave64 compute program with LDS in a group over 32 threads runs as two host waves. lds_meetings makes them meet at group barriers on the edges of every lane-branch region that touches LDS. It refuses a region that both reads and writes LDS, because a read inside it may miss the other host wave's write. Such a program stays at WaveSize(64), which the driver refuses:

host pipeline refused error=0x80070057 shaders=[compute 2cc4c197ee29e291]

This is sr_tracing:CS_Tiling_Generate_Opaque, one of the wave64 computes still refused on NVIDIA (#7, #11). It uses LDS only as two group-wide flags. One lane zeroes them. Inside per-lane branches and a loop, lanes write 1 to one flag and read it back to leave the loop early. After the region, every lane reads both flags.

What

The new lds_flag_region in emit/lower.rs checks a region that reads and writes LDS. If the check passes, lds_meetings keeps the region instead of refusing it. The barriers on its edges stay the same as for any other region. The check requires all of the following:

  • every LDS access in the region is a ds_read_b32 or ds_write_b32 at a constant address, and every write stores the same constant;
  • at most one address is both read and written in the region (the flag), and every write a lane can reach after one of its reads goes to that flag;
  • the region has no other effect: no store, atomic, export, message or barrier;
  • no register the region writes is live, lane by lane, where a lane leaves it. A lane's own unguarded write kills a register. The read of a VGPR's old value, which only the lanes outside EXEC keep, does not count as a use.

Under these conditions a read that misses the other half's write changes nothing the region leaves behind:

  • the lane only goes on as if the flag were still down;
  • anything that lane writes is the flag's own constant, which the missed write has already put there.

So the flag ends up raised exactly when some lane raises it, as on the console, and nothing else depends on when the lane saw it. VICTORY_LDS_FLAGS=0 turns the check off.

For this program the lowering then takes the exact "local waves (idiom)" form, not an approximate one, with WaveSize(32). In the emitted DXIL all 8 group barriers sit in blocks every thread reaches (the entry and the joins after the branches), never inside a branch.

Tested

RTX 5070 Ti, driver 621.24, Windows 11. Own 1.0.2.1 copy.

  • Offline, the whole shader pack (translate --validate --pipelines --list on shaders.rpf with VICTORY_HOST_MAX_WAVE=32, base c96b4f2 against this branch): of 25,889 programs, exactly one changes: 2cc4c197… goes from refused (E_INVALIDARG) to created. Nothing is lost. Compute pipelines created go from 2,645 to 2,646; every other count (emitted, validated, graphics and mesh pipelines) is the same.
  • Tests: two new tests in wave64_tests.rs, on a hand-assembled 64-thread program.
    • A branch that raises a flag and reads it back lowers to WaveSize(32). This test fails with VICTORY_LDS_FLAGS=0.
    • A branch that writes the flag a second, different value after its read stays at 64.
    • cargo test -p victory-shader --lib: 216 passed.
    • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • In game (prologue, 7 minutes in the world, on a branch with this change plus #158, #368 and other open PRs, VICTORY_SPLIT_LDS=1):
    • host pipeline shaders=[compute 2cc4c197ee29e291] is created and bound;
    • no device removal, crash or dropped dispatch;
    • the room renders as before;
    • the computes still refused there are 02384a60… and 6b82bc7d….

The two that are left (CS_DefragHeap_ComputeRelocations, CS_CollectTriangles_Meshlets) use LDS atomics and lane reads across halves, which this check does not cover.

🤖 Generated with Claude Code

**@⁠FrancescoGelosoEterea** on GitHub · 2026-11-25 02:45 UTC · [original](https://github.com/fxceless/victory-fork/pull/592) `FrancescoGelosoEterea:lds-flags` → `main` · +339 −2 · 2 files · 1 commits · open ## Why On a 32-lane host (NVIDIA), a console wave64 compute program with LDS in a group over 32 threads runs as two host waves. `lds_meetings` makes them meet at group barriers on the edges of every lane-branch region that touches LDS. It refuses a region that both reads and writes LDS, because a read inside it may miss the other host wave's write. Such a program stays at `WaveSize(64)`, which the driver refuses: ``` host pipeline refused error=0x80070057 shaders=[compute 2cc4c197ee29e291] ``` This is `sr_tracing:CS_Tiling_Generate_Opaque`, one of the wave64 computes still refused on NVIDIA (#7, #11). It uses LDS only as two group-wide flags. One lane zeroes them. Inside per-lane branches and a loop, lanes write `1` to one flag and read it back to leave the loop early. After the region, every lane reads both flags. ## What The new `lds_flag_region` in `emit/lower.rs` checks a region that reads and writes LDS. If the check passes, `lds_meetings` keeps the region instead of refusing it. The barriers on its edges stay the same as for any other region. The check requires all of the following: - every LDS access in the region is a `ds_read_b32` or `ds_write_b32` at a constant address, and every write stores the same constant; - at most one address is both read and written in the region (the flag), and every write a lane can reach after one of its reads goes to that flag; - the region has no other effect: no store, atomic, export, message or barrier; - no register the region writes is live, lane by lane, where a lane leaves it. A lane's own unguarded write kills a register. The read of a VGPR's old value, which only the lanes outside EXEC keep, does not count as a use. Under these conditions a read that misses the other half's write changes nothing the region leaves behind: - the lane only goes on as if the flag were still down; - anything that lane writes is the flag's own constant, which the missed write has already put there. So the flag ends up raised exactly when some lane raises it, as on the console, and nothing else depends on when the lane saw it. `VICTORY_LDS_FLAGS=0` turns the check off. For this program the lowering then takes the exact "local waves (idiom)" form, not an approximate one, with `WaveSize(32)`. In the emitted DXIL all 8 group barriers sit in blocks every thread reaches (the entry and the joins after the branches), never inside a branch. ## Tested RTX 5070 Ti, driver 621.24, Windows 11. Own 1.0.2.1 copy. - **Offline, the whole shader pack** (`translate --validate --pipelines --list` on `shaders.rpf` with `VICTORY_HOST_MAX_WAVE=32`, base `c96b4f2` against this branch): of 25,889 programs, exactly one changes: `2cc4c197…` goes from refused (`E_INVALIDARG`) to created. Nothing is lost. Compute pipelines created go from 2,645 to 2,646; every other count (emitted, validated, graphics and mesh pipelines) is the same. - **Tests:** two new tests in `wave64_tests.rs`, on a hand-assembled 64-thread program. - A branch that raises a flag and reads it back lowers to `WaveSize(32)`. This test fails with `VICTORY_LDS_FLAGS=0`. - A branch that writes the flag a second, different value after its read stays at 64. - `cargo test -p victory-shader --lib`: 216 passed. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - **In game** (prologue, 7 minutes in the world, on a branch with this change plus #158, #368 and other open PRs, `VICTORY_SPLIT_LDS=1`): - `host pipeline shaders=[compute 2cc4c197ee29e291]` is created and bound; - no device removal, crash or dropped dispatch; - the room renders as before; - the computes still refused there are `02384a60…` and `6b82bc7d…`. The two that are left (`CS_DefragHeap_ComputeRelocations`, `CS_CollectTriangles_Meshlets`) use LDS atomics and lane reads across halves, which this check does not cover. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A console wave64 program with LDS in a group over 32 threads runs on a
32-lane host as two host waves that meet at group barriers on the edges of
each lane-branch region. A region that both reads and writes LDS was
refused, since a read inside it may miss the other half's write.

That changes nothing the region leaves behind when it only raises a flag:
every access is a ds_read_b32/ds_write_b32 at a constant address, every
write stores the same constant, at most one address is both read and
written, every write a lane can reach after a read goes to that flag, the
region has no other effect, and no register it writes is live (lane by
lane) where a lane leaves it. A missed write then only keeps a lane going
as if the flag were down, and what it writes is the constant already
there. VICTORY_LDS_FLAGS=0 turns this off.

sr_tracing:CS_Tiling_Generate_Opaque (2cc4c197ee29e291) now translates in
waves of 32 and its pipeline is created on an RTX 5070 Ti. Translating
the whole shader pack for a 32-lane host, it is the only program whose
outcome changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:47:02 +00:00
Author
Owner

@⁠Dathinel on GitHub · 2026-11-25 19:25 UTC · original

Tested the lowerer from 8bc48bf on d510ee4 with an RTX 3050 Laptop (4 GB, driver 32.0.16.1062). The PR's two wave-size tests passed. I also dispatched its synthetic LDS flag program with one UAV output per thread, so a missing write could not pass as a cleared flag.

  • With the default setting, all 36 dispatches returned the expected 64 dwords each (2,304 values). Four consecutive runs passed.
  • With VICTORY_LDS_FLAGS=0, the same test binary stopped at host compute pipeline creation with an invalid-parameter error.

The inputs exercise both sides of the 32-thread boundary, each scalar branch outcome, and both same-value flag variants. This measures the synthetic program only. AI-assisted test and review.

Reproduce the hardware check

Append the following test to crates/victory-shader/src/emit/wave64_tests.rs with the PR applied. Run cargo test --locked -p victory-shader --lib -- lds_flag_rtx_readback --nocapture on the hardware adapter. Repeat with the environment variable above for the control.

#[test]
fn lds_flag_rtx_readback() {
    let host = Host::new(false).expect("hardware");
    for second in [None, Some(1)] {
        let mut native = flag_program(second);
        let last = native.code.len() - 12;
        let tail: Vec<u8> = [0xE070_0000u32, 0x8002_0500, S_ENDPGM]
            .iter()
            .flat_map(|w| w.to_le_bytes())
            .collect();
        assert_eq!(native.code[last..], tail);
        native.code.truncate(last);
        for word in [
            0x3406_0082, // v_lshlrev_b32 v3, 2, v0 (one dword per thread)
            0x4A0A_0A81, // v_add_nc_u32 v5, 1, v5 (zero now means unwritten)
            0xE070_1000,
            0x8002_0503,
            S_ENDPGM,
        ] {
            native.code.extend(word.to_le_bytes());
        }
        let p = crate::spill::build(&native).unwrap();
        let shader = lowered(&native, &p, &HashSet::new()).unwrap();
        let (root, pso) = host.compute_pipeline(&shader).expect("hardware pipeline");
        for below in [0u32, 1, 16, 31, 32, 33, 48, 63, 64] {
            for skip in [0, 1] {
                let mut ud = vec![0; shader.user_data_dwords().max(1) as usize];
                for u in &shader.user_data {
                    if let (RootType::Constants, 0, d @ 0..=1) = (u.root, u.slot, u.dword) {
                        ud[u.index as usize] = [below, skip][d as usize];
                    }
                }
                let out = host
                    .dispatch(
                        &root,
                        &pso,
                        &Dispatch {
                            roots: vec![(0, Root::Constants(ud))],
                            views: &[View::RawUav(64)],
                            samplers: &[],
                            groups: [1, 1, 1],
                        },
                    )
                    .expect("readback");
                assert_eq!(
                    out[0],
                    vec![1 + u32::from(below > 0); 64],
                    "below={below} skip={skip} second={second:?}"
                );
            }
        }
    }
}
**@⁠Dathinel** on GitHub · 2026-11-25 19:25 UTC · [original](https://github.com/fxceless/victory-fork/pull/592#issuecomment-7172273429) Tested the lowerer from `8bc48bf` on `d510ee4` with an RTX 3050 Laptop (4 GB, driver 32.0.16.1062). The PR's two wave-size tests passed. I also dispatched its synthetic LDS flag program with one UAV output per thread, so a missing write could not pass as a cleared flag. - With the default setting, all 36 dispatches returned the expected 64 dwords each (2,304 values). Four consecutive runs passed. - With `VICTORY_LDS_FLAGS=0`, the same test binary stopped at host compute pipeline creation with an invalid-parameter error. The inputs exercise both sides of the 32-thread boundary, each scalar branch outcome, and both same-value flag variants. This measures the synthetic program only. AI-assisted test and review. <details><summary>Reproduce the hardware check</summary> Append the following test to `crates/victory-shader/src/emit/wave64_tests.rs` with the PR applied. Run `cargo test --locked -p victory-shader --lib -- lds_flag_rtx_readback --nocapture` on the hardware adapter. Repeat with the environment variable above for the control. ```rust #[test] fn lds_flag_rtx_readback() { let host = Host::new(false).expect("hardware"); for second in [None, Some(1)] { let mut native = flag_program(second); let last = native.code.len() - 12; let tail: Vec<u8> = [0xE070_0000u32, 0x8002_0500, S_ENDPGM] .iter() .flat_map(|w| w.to_le_bytes()) .collect(); assert_eq!(native.code[last..], tail); native.code.truncate(last); for word in [ 0x3406_0082, // v_lshlrev_b32 v3, 2, v0 (one dword per thread) 0x4A0A_0A81, // v_add_nc_u32 v5, 1, v5 (zero now means unwritten) 0xE070_1000, 0x8002_0503, S_ENDPGM, ] { native.code.extend(word.to_le_bytes()); } let p = crate::spill::build(&native).unwrap(); let shader = lowered(&native, &p, &HashSet::new()).unwrap(); let (root, pso) = host.compute_pipeline(&shader).expect("hardware pipeline"); for below in [0u32, 1, 16, 31, 32, 33, 48, 63, 64] { for skip in [0, 1] { let mut ud = vec![0; shader.user_data_dwords().max(1) as usize]; for u in &shader.user_data { if let (RootType::Constants, 0, d @ 0..=1) = (u.root, u.slot, u.dword) { ud[u.index as usize] = [below, skip][d as usize]; } } let out = host .dispatch( &root, &pso, &Dispatch { roots: vec![(0, Root::Constants(ud))], views: &[View::RawUav(64)], samplers: &[], groups: [1, 1, 1], }, ) .expect("readback"); assert_eq!( out[0], vec![1 + u32::from(below > 0); 64], "below={below} skip={skip} second={second:?}" ); } } } } ``` </details>
Author
Owner

@⁠HAL9000RELOADED on GitHub · 2026-11-25 19:29 UTC · original

RX 7900 XTX (RDNA3), Windows 10 + #402 for SM 6.6, main d510ee4: both new tests pass on the card with this PR. With the tests alone on the base, a_branch_that_only_raises_an_lds_flag_runs_in_waves_of_32 fails. No other test changed. Run table: #129 (#129).

**@⁠HAL9000RELOADED** on GitHub · 2026-11-25 19:29 UTC · [original](https://github.com/fxceless/victory-fork/pull/592#issuecomment-7172374704) RX 7900 XTX (RDNA3), Windows 10 + #402 for SM 6.6, main `d510ee4`: both new tests pass on the card with this PR. With the tests alone on the base, `a_branch_that_only_raises_an_lds_flag_runs_in_waves_of_32` fails. No other test changed. Run table: #129 (https://git.gtahub.st/github/fxceless--victory-fork/pulls/129).
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/592/head:github-bot/gh-592'
git switch 'github-bot/gh-592'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-592'
git switch 'github-bot/gh-592'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-592'
git switch 'github-bot/gh-592'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-592'
git switch 'main'
git merge --squash 'github-bot/gh-592'
git switch 'main'
git merge --ff-only 'github-bot/gh-592'
git switch 'main'
git merge 'github-bot/gh-592'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.