Skip to content

Fix whole-wave min reduction over a partial wave (WaveActive* recognition) - #1857

Open
llegate26 wants to merge 6 commits into
xcl-dev:mainfrom
llegate26:wave64-partial-min-test
Open

llegate26 wants to merge 6 commits into
xcl-dev:mainfrom
llegate26:wave64-partial-min-test

Conversation

@llegate26

@llegate26 llegate26 commented Nov 24, 2026 •

Copy link
Copy Markdown

What is in it

  • Recognise the whole-wave reduction idiom (seed v_cndmask_b32 + 3× DPP8 xor 1/2/4 + DPP16 row_mirror + v_permlanex16 + v_readlane 32/0) and emit WaveActiveMin/WaveActiveMax/WaveActiveBitOr over the launched lanes, suppressing only the within-half DPP steps.
  • Keeps the v_readlane lane 32 + op + lane 0 steps that combine the two 32-lane halves of a wave64 (cross-half).
  • Adds gpu_wave64_ps_partial_wave_min (synthetic, no game data) and negative tests for the recogniser.

Tested

  • hardware (GPU + driver, CPU), Rust version, debug / release: NVIDIA RTX 5070 Ti, Windows 11, Rust 1.100.0, debug (cargo test profile).
  • cargo test --workspace -- --test-threads=4: 1060 passed, 4 failed, 26 ignored. The 4 failures are fuzz_validate_and_gpu_* failing with CreateComputePipelineState: 0x8007000E (out of memory) — pre-existing, unrelated to this change.

Regression run

  • Not run: scripts/smoke.ps1 needs a real title (no title data in this environment).

Not tested

  • The retail smoke (scripts/smoke.ps1) on a real title.
  • Full retail GTA VI (out of scope; no game data).

AI use

  • DeepSeek Harness (an AI coding agent): diagnosed the bug, wrote the recognition + WaveActive* emission, and wrote and ran the synthetic tests.

llegate26 added 2 commits November 24, 2026 15:57
…duccion min en onda parcial)

Nuevo test que dibuja un triangulo pequeno (3 quads = 6 pixeles, onda parcial) y reduce
V_MIN_U32 sobre los pixeles impares. Reproduce el bug: las lanes no lanzadas se leen como 0
en vez de la identidad -1, y el min devuelve 0 en vez del minimo real (2).
…veActive*

La reduccion de onda (seed v_cndmask_b32 + DPP8 xor 1/2/4 + DPP16 row_mirror +
v_permlanex16 + v_readlane 32/0) se reconoce en translate() y el seed se reduce con
WaveActiveMin/Max/BitOr sobre las lanes lanzadas; los pasos DPP/readlane se suprimen
(leian las lanes no lanzadas como 0 en vez de la identidad, rompiendo min).

Antes: gpu_wave64_ps_partial_wave_min daba min=0 (want 2.0).
Despues: da 2.0. Regresion: gpu_wave64_ps_whole_wave_reductions,
gpu_wave64_ps_spill_slots y 104 tests GPU pasan.
@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Checked on Ampere: RTX 3070 8 GB (32-lane waves, so wave64 compute is split over two host waves), driver 616.56, Windows 11, debug.

gpu_wave64_ps_partial_wave_min and gpu_wave64_ps_whole_wave_reductions pass on ae53d84.

A regression in split-wave64 compute. recognize_whole_wave_reductions runs for every program, not only graphics. In a wave64 compute program on a 32-lane GPU, WaveActiveMin covers one 32-lane host wave. The v_readlane …, 32 step it skips is what joined the two halves. I ran your whole_wave_reduce idiom as a 64-thread wave64 compute program: seed 1000 - tid, every lane in the mask, so the minimum (937) is in lane 63, and every lane stores s40:

Tree Lanes 0..31 Lanes 32..63
base b9b6ea6 937 937
ae53d84 969 969

969 is the minimum of lanes 0..31 only. A wave64 PS on these GPUs has no populated upper half (upper_lanes), so the PS tests can't show this. Limiting the recognition to that case, or skipping it when the console wave is split (runner::split_wave64() / the emitter's x64 mode), would keep compute correct.

Test (add next to gpu_wave64_ps_partial_wave_min)
#[test]
fn review_wave64_cs_whole_wave_min_spans_both_halves() {
    if !gpu_ready() {
        return;
    }
    use super::{dxc, CsInterface};
    let code = program_of(&[
        ins("V_SUB_NC_U32", "ENC_VOP2", &[("VDST", 13), ("SRC0", 255), ("VSRC1", 0)], Some(1000)), // v13 = 1000 - tid
        ins("S_MOV_B64", "ENC_SOP1", &[("SDST", 18), ("SSRC0", 126)], None),                          // s[18:19] = exec
        whole_wave_reduce("V_MIN_U32", 193, 40),
        ins("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 130), ("VSRC1", 0)], None),       // v1 = tid * 4
        ins("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 7), ("SRC0", 0), ("VSRC1", 1)], None),           // v7 = s0 + tid*4
        ins("V_MOV_B32", "ENC_VOP1", &[("VDST", 5), ("SRC0", 40)], None),                           // v5 = s40
        ins("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 7), ("DATA", 5), ("SADDR", 0x7D)], None),
    ]);
    let cs = CsInterface { threads: [64, 1, 1], user_sgprs: 2, tgid: [false, false, false], tg_size: false, tid_components: 1 };
    let o = on_gpu(Options { mode: Mode::Compute(cs), wave64: true, ..Default::default() });
    let t = translate(&code, &o).unwrap();
    assert!(t.complete(), "{:?}", t.unsupported_why);
    let dxil = dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl));
    let base = 0x100usize;
    let mut io = vec![0u32; 128];
    io[0] = base as u32;
    let out = runner::dispatch(&dxil, [1, 1, 1], &io, &vec![0u32; (base + 64 * 4) / 4]).unwrap();
    let got: Vec<u32> = (0..64).map(|tid| out[(base + tid * 4) / 4]).collect();
    eprintln!("split={} WaveActive in HLSL={} lanes 0..31: {:?}, lanes 32..63: {:?}", runner::split_wave64(), t.hlsl.contains("WaveActiveMin"), &got[..4], &got[32..36]);
    assert!(got.iter().all(|&v| v == 937), "whole-wave min: {got:?}");
}

AI-assisted (Claude Code). Synthetic tests only.

@Anon64

Anon64 commented Nov 24, 2026

Copy link
Copy Markdown

Regression run for this PR: head ae53d84, based on main b9b6ea6. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, XCL_NETWORK=0, A pressed every 4 s from 20 s, scripts/smoke.ps1 on a retail title for 60 s, back to back with the main run.

main b9b6ea6:

xcl-smoke v1
commit: b9b6ea62080cc62fc19e212033372d01c6214e5c
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1496 (PresentX 1496)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 88754 MB

this PR:

xcl-smoke v1
commit: ae53d8435f346dbe66d47a78bd647060fe74eea9
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1523 (PresentX 1523)
presents per 2 s (last 3): 51 3 24
device removals: 0
crashes: 0
peak commit: 65273 MB

No device removals or crashes; 102 % of main's frames, 74 % of its peak commit.

Peak commit on b9b6ea6 is mostly the pipeline preload's DXC compiles on this machine (#1035, fixed on later main by #1321), and it varies a lot from run to run.

Not tested: the whole suite; whether the title reaches the changed reductions in these 60 s.

Written with Claude Code (Claude Opus 5.5) from runs on this machine.

@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head ae53d84 (on its base b9b6ea6).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form --skip gpu::recomp::lower_dxil::tests::f16_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::integer_programs_on_the_gpu --skip gpu::recomp::lower_dxil::tests::f64_programs_on_the_gpu: 889 passed, 0 failed, 42 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: gpu_wave64_ps_partial_wave_min.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 890 passed, 0 failed, 42 ignored. SKIPPED: heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • On NVIDIA, --skip leaves out five tests where this PR's base has them. They fail on main 689f2db too, not because of this PR: under XCL_REQUIRE_GPU=mesh, runner::skip_test turns their skips into failures. These are heap_window_out_of_memory_falls_back_to_the_switch_form (Tests: heap_window_out_of_memory_falls_back_to_the_switch_form fails under XCL_REQUIRE_GPU=mesh (opt-in skip counted as missing coverage) #1651) and the four gpu::recomp::lower_dxil::tests::*_programs_on_the_gpu tests (follow-up on #1651). The AMD run does not set XCL_REQUIRE_GPU.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
@WebIdentity1

Copy link
Copy Markdown

Scoped fix for the split-wave64 compute regression that @upipibi found on #1857, verified on an RTX 3090 at exact head ae53d8435f346dbe66d47a78bd647060fe74eea9. I adapted their public fixture and kept the patch on this author-owned PR.

Tested — native Windows 10 19045, RTX 3090 driver 591.74, Rust 1.98.1 MSVC, XCL_ADAPTER=3090; direct DXIL and forced split unset. Public synthetic shader only.

  • The same gpu_wave64_cs_whole_wave_min_spans_both_halves test on the unmodified head: 0 passed / 1 failed, adapter printed NVIDIA GeForce RTX 3090, split=true, WaveActiveMin=true; all 64 lanes read 969 instead of 937. The intrinsic sees only the first host wave32; the suppressed readlane steps had joined both halves.
  • With the local guard, that test passed 1/0, split=true, WaveActiveMin=false, and all 64 lanes read 937. The three gpu_wave64_ps_ controls passed 3/0, including the PR's partial-wave pixel minimum of 2. The rewrite remains enabled there.
  • cargo build --locked --workspace: exit 0 on both snapshots. cargo test --locked --workspace --no-fail-fast -- --test-threads=4 --show-output: control 889 passed / 2 failed / 42 ignored, fix 890 / 1 / 42, across 20 targets with six explicit SKIPPED lines each. The common failure is the existing gpu_clamp_and_omod_per_instruction on this driver; the extra control failure is this new test. No fix-only failure.
  • Both 295-file source snapshots matched SHA-256 manifests before and after the Windows runs. The guard matches Em::x64()'s mode predicate, so the original cross-half sequence stays intact only where a console wave spans two hardware waves.

No commit or competing PR from me; please incorporate the small guard and regression if useful:

Two-file patch (40 insertions, one deletion)
diff --git a/crates/xcl/src/gpu/recomp/mod.rs b/crates/xcl/src/gpu/recomp/mod.rs
index f36cf65..a19931d 100644
--- a/crates/xcl/src/gpu/recomp/mod.rs
+++ b/crates/xcl/src/gpu/recomp/mod.rs
@@ -1559,7 +1559,15 @@ pub fn translate(code: &[u8], opt: &Options) -> Result<Translation, String> {
         }
     }
     let mut em = emit::Em::new(opt.clone());
-    let (reductions, skip) = recognize_whole_wave_reductions(&insts);
+    // A split wave64 compute/mesh/geometry wave spans two host wave32s. WaveActive*
+    // only reduces one half, while the original readlane steps join both halves.
+    let split_x64 = opt.wave64 && opt.split_wave64
+        && matches!(opt.mode, Mode::Kernel | Mode::Compute(_) | Mode::Mesh(_) | Mode::Geometry(_));
+    let (reductions, skip) = if split_x64 {
+        (BTreeMap::new(), BTreeSet::new())
+    } else {
+        recognize_whole_wave_reductions(&insts)
+    };
     em.reductions = reductions;
     em.skip = skip;
     em.configure_round_modes(&insts, branches == 0);
diff --git a/crates/xcl/src/gpu/recomp/tests.rs b/crates/xcl/src/gpu/recomp/tests.rs
index 27a8a9f..a59abbc 100644
--- a/crates/xcl/src/gpu/recomp/tests.rs
+++ b/crates/xcl/src/gpu/recomp/tests.rs
@@ -2285,6 +2285,37 @@ fn gpu_wave64_ps_partial_wave_min() {
     }
 }
 
+/// A reduction over a wave64 compute program must include both wave32 halves when the
+/// adapter runs the console wave in the split form. Reproducer from @upipibi on #1857.
+#[test]
+fn gpu_wave64_cs_whole_wave_min_spans_both_halves() {
+    if !gpu_ready() {
+        return;
+    }
+    use super::{dxc, CsInterface};
+    let code = program_of(&[
+        ins("V_SUB_NC_U32", "ENC_VOP2", &[("VDST", 13), ("SRC0", 255), ("VSRC1", 0)], Some(1000)),
+        ins("S_MOV_B64", "ENC_SOP1", &[("SDST", 18), ("SSRC0", 126)], None),
+        whole_wave_reduce("V_MIN_U32", 193, 40),
+        ins("V_LSHLREV_B32", "ENC_VOP2", &[("VDST", 1), ("SRC0", 130), ("VSRC1", 0)], None),
+        ins("V_ADD_NC_U32", "ENC_VOP2", &[("VDST", 7), ("SRC0", 0), ("VSRC1", 1)], None),
+        ins("V_MOV_B32", "ENC_VOP1", &[("VDST", 5), ("SRC0", 40)], None),
+        ins("GLOBAL_STORE_DWORD", "ENC_FLAT_GLBL", &[("ADDR", 7), ("DATA", 5), ("SADDR", 0x7D)], None),
+    ]);
+    let cs = CsInterface { threads: [64, 1, 1], user_sgprs: 2, tgid: [false, false, false], tg_size: false, tid_components: 1 };
+    let opt = on_gpu(Options { mode: Mode::Compute(cs), wave64: true, ..Default::default() });
+    let t = translate(&code, &opt).unwrap();
+    assert!(t.complete(), "{:?}", t.unsupported_why);
+    let dxil = dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}\n{}", t.hlsl));
+    let base = 0x100usize;
+    let mut io = vec![0u32; 128];
+    io[0] = base as u32;
+    let out = runner::dispatch(&dxil, [1, 1, 1], &io, &vec![0u32; (base + 64 * 4) / 4]).unwrap();
+    let got: Vec<u32> = (0..64).map(|tid| out[(base + tid * 4) / 4]).collect();
+    eprintln!("adapter={} split={} WaveActiveMin={} lanes 0..31: {:?}, lanes 32..63: {:?}", runner::adapter().unwrap(), opt.split_wave64, t.hlsl.contains("WaveActiveMin"), &got[..4], &got[32..36]);
+    assert!(got.iter().all(|&v| v == 937), "whole-wave min: {got:?}");
+}
+
 /// SGPR spill slots in lanes 0..47 of a VGPR (`spill_slots`) in a wave64 PS: slots ≥ 32 do
 /// not exist in a 32-lane wave, and a slot in a lane that was not launched is lost in a
 /// partial wave (a primitive's edge). Drawn over the whole target and as a 6-pixel triangle.

Not tested: current-main integration, retail smoke/gameplay, driver compile cost, peak memory, AMD native-wave64, or other reduction idioms. This does not establish merge readiness; the PR description check is still red.

AI use: Codex desktop (GPT-6) adapted @upipibi's fixture, prepared the local patch, ran the Windows tests and drafted this report from the retained logs.

llegate26 added 2 commits November 24, 2026 18:11
…s (v_readlane 32 + op)

El reconocedor ahora suprime solo la reduccion dentro de cada mitad (3x DPP8 + DPP16 +
v_permlanex16 + op) y CONSERVA v_readlane lane32 + op + v_readlane lane0, que combinan las
dos mitades wave32 en la reduccion wave64 global. Antes (skip de 8) suprimia el readlane32 y
la reduccion quedaba por-mitad (WaveActive* solo cubre la wave fisica de 32 lanes).

Regresion: gpu_wave64_ps_partial_wave_min, gpu_wave64_ps_whole_wave_reductions,
gpu_wave64_ps_spill_slots y 104 tests GPU pasan.
…/2/4, DPP16 row_mirror y lanes de readlane 32/0

Los campos DPP se leen de la codificacion de extension (emit::ext_encoding), no de d.enc.
Ahora el reconocedor solo acepta la secuencia exacta de reduccion de onda (DPP8 xor 1/2/4,
DPP16 row_mirror ctrl 0x140, v_readlane lane 32 y 0), reduciendo falsos positivos (riesgos A/B).
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main — needs a rebase before it can be tested.

llegate26 added 2 commits November 24, 2026 20:39
…ocedor

- El seed v_cndmask_b32 debe leer exactamente 1 VGPR (el valor); asi la identidad queda
  forzada a ser una constante, no un registro.
- Test recognize_rejects_wrong_reduction_variants: verifica que el reconocedor acepta la
  reduccion canonica y RECHAZA variantes con selectores DPP8 (xor3), DPP16 (row_half_mirror),
  lane de readlane (16/1) o identidad como VGPR.

Suite completa: 842 passed, 0 failed, 22 ignored.
Adopta el refactor del upstream (em.emit_insts con try_emit_x64_reduction_tail) y conserva
mi reconocimiento de reduccion (recognize_whole_wave_reductions + WaveActive* + skip), que
cubre la onda parcial dentro de la mitad (bug que main aun tiene). El skip se movio a
emit_insts.

Tests: 1060 passed; 4 fuzz_validate_and_gpu_* fallan por OOM de GPU (0x8007000E, preexistente,
no relacionado con el fix). gpu_wave64_ps_* y recognize_rejects_wrong_reduction_variants PASS.
@raylitebull

Copy link
Copy Markdown
Member

CI workflow approved for 762c36e.

Review note on 762c36e: recomp/mod.rs still calls recognize_whole_wave_reductions(&insts) unconditionally, so split-wave64 compute/mesh/geometry dispatches on 32-lane GPUs still hit the cross-half regression pointed out above (missing upper-half contribution). Guarding it when opt.wave64 && opt.split_wave64 && matches!(opt.mode, Mode::Kernel | Mode::Compute(_) | Mode::Mesh(_) | Mode::Geometry(_)), fixing the PR description check, and getting fresh AMD + NVIDIA verification on the updated head are needed before merge.

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Re-checked 762c36e on the RTX 3070 (driver 616.56, 32-lane waves, so the split form): gpu_wave64_ps_partial_wave_min and recognize_rejects_wrong_reduction_variants pass with XCL_DIRECT_DXIL=0 and 1.

AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants