Shaders: v_pk_fmac_f16 with DPP ignores the lane DPP selects for src0 #2228

Open
opened 2026-11-26 04:59:04 +00:00 by github-bot · 2 comments
Owner

@⁠BastienRodz on GitHub · 2026-11-25 08:23 UTC · original

What happens

v_pk_fmac_f16 (VOP2 opcode 60) with a DPP16 or DPP8 dword multiplies the executing lane's own src0 instead of the one DPP selects.

Lower::valu computes the DPP-selected src0 and stores it in self.src0 before lowering the operation, and the source readers fsrc, src, half_src and v_cndmask_b32 take it for source 0. Lower::fmac_half (crates/victory-shader/src/emit/lower.rs) reads the operand directly:

let x = self.operand(inst, &op)?;

so the cross-lane value is dropped for both packed halves.

Expected

From the RDNA 2 ISA guide:

  • §12.7, V_PK_FMAC_F16: "D.f16_lo = S0.f16_lo * S1.f16_lo + D.f16_lo; D.f16_hi = S0.f16_hi * S1.f16_hi + D.f16_hi."
  • §6.9: "Data Parallel ALU operations allow VALU instruction to select operands from different lanes (threads) rather than just using a thread's own lane. DPP is compatible only with: VOP1 and VOP2."

So with DPP, S0 is the selected lane's src0, as for every other VOP2 operation the translator lowers.

Where

crates/victory-shader/src/emit/lower.rs, Lower::fmac_half.

AI-assisted (Gemini 4 Neon found it, Claude Code reviewed and verified it); synthetic test only, no game data.

**@⁠BastienRodz** on GitHub · 2026-11-25 08:23 UTC · [original](https://github.com/Marvisak/VIctory/issues/2228) ### What happens `v_pk_fmac_f16` (VOP2 opcode 60) with a DPP16 or DPP8 dword multiplies the executing lane's own `src0` instead of the one DPP selects. `Lower::valu` computes the DPP-selected `src0` and stores it in `self.src0` before lowering the operation, and the source readers `fsrc`, `src`, `half_src` and `v_cndmask_b32` take it for source 0. `Lower::fmac_half` (`crates/victory-shader/src/emit/lower.rs`) reads the operand directly: ```rust let x = self.operand(inst, &op)?; ``` so the cross-lane value is dropped for both packed halves. ### Expected From the RDNA 2 ISA guide: - §12.7, `V_PK_FMAC_F16`: "D.f16_lo = S0.f16_lo * S1.f16_lo + D.f16_lo; D.f16_hi = S0.f16_hi * S1.f16_hi + D.f16_hi." - §6.9: "Data Parallel ALU operations allow VALU instruction to select operands from different lanes (threads) rather than just using a thread's own lane. DPP is compatible only with: VOP1 and VOP2." So with DPP, S0 is the selected lane's `src0`, as for every other VOP2 operation the translator lowers. ### Where `crates/victory-shader/src/emit/lower.rs`, `Lower::fmac_half`. AI-assisted (Gemini 4 Neon found it, Claude Code reviewed and verified it); synthetic test only, no game data.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:59:04 +00:00
Author
Owner

@⁠studiostephe on GitHub · 2026-11-25 10:04 UTC · original

Lower::fmac_half reads the operand directly (self.operand(inst, &op)), so a DPP fetch on src0 is dropped for both packed halves. Every other VOP2 operation takes src0 from self.src0, which valu sets from the DPP-selected lane before calling valu_op. The hardware (RDNA 2 ISA §12.7, §6.9) multiplies the selected lane's halves.

Where: crates/victory-shader/src/emit/lower.rs, Lower::fmac_half (line 6662 in current main):

let x = self.operand(inst, &op)?;

Fix: read src0 from self.src0 (the DPP-fetched value) when k == 0, as fsrc, src, half_src, and v_cndmask_b32 already do:

-        let x = self.operand(inst, &op)?;
+        // src0 as DPP fetched it, as for every other VOP2 operation
+        // (`valu` sets `self.src0` before the operation).
+        let x = if k == 0 {
+            match self.src0 {
+                Some(x) => x,
+                None => self.operand(inst, &op)?,
+            }
+        } else {
+            self.operand(inst, &op)?
+        };

Test: flow_tests.rs — a differential case (console dxc → translate → host GPU vs. PC dxc → host GPU, 256 lanes × 4 dwords, hardware and WARP). The HLSL shape asuint(fa * fb + WaveReadLaneAt(fc, 3)) lowers to v_pk_fmac_f16 with a DPP16 fetch on src0 on the console. The PC compiler lowers WaveReadLaneAt the same way the hardware does, so the bit-exact f16 results must match lane for lane. With the fix, both adapters agree on all 1024 words. Without it, the translated shader reads each lane's own src0 and the products differ on every lane whose DPP-selected lane holds a different value.

#[test]
fn packed_fmac_reads_src0_from_the_dpp_selected_lane() {
    // … (full test in the diff below)
}

Verified:

  • cargo test -p victory-shader --locked — 145 passed, 0 failed, 2 ignored (native, Mac).
  • cargo check -p victory-shader --profile test --target x86_64-pc-windows-msvc — clean (the test is #[cfg(all(test, windows))], so it compiles for the Windows target).
  • rustfmt --edition 2024 --check — clean on the changed lines (the repo has pre-existing fmt diffs from a newer rustfmt; none in this diff).
  • The test requires VICTORY_CONSOLE_DXC and a PC dxc.exe (same as the rest of flow_tests); it skips without them.

Not covered:

  • Not run on a GPU here (no Windows/D3D12 on this Mac). The test is Windows-only like the rest of flow_tests; it will run in CI or on a Windows box.
  • The fix is minimal and follows the established pattern (fsrc, src, half_src all read self.src0 for k=0). The k == 0 guard ensures src1 and the destination (k=2) are unaffected.

Full diff:

diff --git a/crates/victory-shader/src/emit/flow_tests.rs b/crates/victory-shader/src/emit/flow_tests.rs
index 8945cdd..1539246 100644
--- a/crates/victory-shader/src/emit/flow_tests.rs
+++ b/crates/victory-shader/src/emit/flow_tests.rs
@@ -178,6 +178,99 @@ const CS_CASES: &[(&str, &str)] = &[
     ),
 ];
 
+// The console's `v_pk_fmac_f16` with a DPP fetch on src0: each lane reads
+// its source from the DPP-selected lane of the wave, multiplies the packed
+// halves, and adds its own destination. The PC compiler lowers `WaveReadLaneAt`
+// the same way the hardware does, so the bit-exact f16 products must match
+// lane for lane (the console program's wave size from its container).
+#[test]
+fn packed_fmac_reads_src0_from_the_dpp_selected_lane() {
+    let Some((console, pc)) = tools() else {
+        eprintln!("no console or PC compiler; skipped");
+        return;
+    };
+    let dir = std::env::temp_dir().join(format!("victory-flow-{}", std::process::id()));
+    std::fs::create_dir_all(&dir).unwrap();
+    let _one = ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner());
+    let lanes = 256u32;
+    let data = lane_inputs(lanes);
+    let views = [
+        View::Buffer {
+            data: &data,
+            stride: Stride::Raw,
+        },
+        View::RawUav(lanes * 4),
+    ];
+    let src = r#"CS {
+        uint a = IN(0), b = IN(1), c = IN(2);
+        float fa = asfloat(a);
+        float fb = asfloat(b);
+        float fc = asfloat(c);
+        float fl = WaveReadLaneAt(fc, 3);
+        OUT(0, asuint(fa * fb + fl));
+        OUT(1, asuint(fa));
+        OUT(2, asuint(fb));
+        OUT(3, asuint(fl));
+    }"#;
+    let text = format!("{CS_COMMON}\n{src}\n");
+    let mut wave = 64;
+    let ours = compile(&console, &dir, &text, "cs", "cs_6_5")
+        .and_then(|c| {
+            wave = console_wave(&c);
+            translate(&c).map_err(|e| e.to_string())
+        })
+        .expect("console compile and translate");
+    let pc_cs = compile(&pc, &dir, &text, "cs", "cs_6_5").expect("PC compile");
+    // SAFETY: the PC container carries its root signature.
+    for (label, warp) in [("hardware", false), ("warp", true)] {
+        let Ok(host) = Host::new(warp) else {
+            eprintln!("no SM 6.6 {label} device; skipped");
+            continue;
+        };
+        let (root_t, pso_t) = match host.compute_pipeline(&ours) {
+            Ok(p) => p,
+            // WARP runs no fixed wave size.
+            Err(e) if warp => {
+                eprintln!("{label}: skipped ({e})");
+                continue;
+            }
+            Err(e) => panic!("{label}: {e}"),
+        };
+        let dispatch = Dispatch {
+            roots: vec![(0, Root::Constants(console_user_data(&ours)))],
+            views: &views,
+            samplers: &[],
+            groups: [lanes / 64, 1, 1],
+        };
+        let got = host
+            .dispatch(&root_t, &pso_t, &dispatch)
+            .unwrap_or_else(|e| panic!("{label}: {e}"));
+        let root_p: ID3D12RootSignature =
+            unsafe { host.device.CreateRootSignature(0, &pc_cs) }.unwrap();
+        let pso_p = host.compute_pipeline_with(&root_p, &pc_cs).unwrap();
+        let want = host
+            .dispatch(&root_p, &pso_p, &dispatch)
+            .unwrap_or_else(|e| panic!("{label}: {e}"));
+        let bad: Vec<usize> = (0..(lanes as usize * 4))
+            .filter(|&w| got[0][w] != want[0][w])
+            .collect();
+        eprintln!(
+            "{label}: {wave}-lane waves, {} of {} words differ",
+            bad.len(),
+            lanes * 4
+        );
+        assert!(
+            bad.is_empty(),
+            "{label}: {} words differ, first at {} ({:#x} vs {:#x})",
+            bad.len(),
+            bad[0],
+            got[0][bad[0]],
+            want[0][bad[0]],
+        );
+    }
+    let _ = std::fs::remove_dir_all(&dir);
+}
+
 const PS_COMMON: &str = r#"
 #define RS "RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT), RootConstants(num32BitConstants=4, b0)"
 cbuffer K : register(b0) { float4 k; };
diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs
index 2c06b01..1cc61fb 100644
--- a/crates/victory-shader/src/emit/lower.rs
+++ b/crates/victory-shader/src/emit/lower.rs
@@ -6658,7 +6658,16 @@ impl<'a> Lower<'a> {
             // shortcut: no constant operand seen; its high half is not known.
             return Err("a constant operand of v_pk_fmac_f16");
         }
-        let x = self.operand(inst, &op)?;
+        // src0 as DPP fetched it, as for every other VOP2 operation
+        // (`valu` sets `self.src0` before the operation).
+        let x = if k == 0 {
+            match self.src0 {
+                Some(x) => x,
+                None => self.operand(inst, &op)?,
+            }
+        } else {
+            self.operand(inst, &op)?
+        };
         let c = self.cur;
         let x = if high {
             let k16 = self.s.m.i32(16);
             self.s.m.bin(c, Bin::LShr, x, k16);
         } else {
             x
         };
         let m = self.s.m.i32(0xFFFF);
         Ok(self.s.m.bin(c, Bin::And, x, m))
     }

qwen3.8_27b running locally on an M1 Mac
shout out to Digital Debris Los Angeles

**@⁠studiostephe** on GitHub · 2026-11-25 10:04 UTC · [original](https://github.com/Marvisak/VIctory/issues/2228#issuecomment-7159187781) `Lower::fmac_half` reads the operand directly (`self.operand(inst, &op)`), so a DPP fetch on src0 is dropped for both packed halves. Every other VOP2 operation takes src0 from `self.src0`, which `valu` sets from the DPP-selected lane before calling `valu_op`. The hardware (RDNA 2 ISA §12.7, §6.9) multiplies the selected lane's halves. **Where:** `crates/victory-shader/src/emit/lower.rs`, `Lower::fmac_half` (line 6662 in current main): ```rust let x = self.operand(inst, &op)?; ``` **Fix:** read src0 from `self.src0` (the DPP-fetched value) when `k == 0`, as `fsrc`, `src`, `half_src`, and `v_cndmask_b32` already do: ```diff - let x = self.operand(inst, &op)?; + // src0 as DPP fetched it, as for every other VOP2 operation + // (`valu` sets `self.src0` before the operation). + let x = if k == 0 { + match self.src0 { + Some(x) => x, + None => self.operand(inst, &op)?, + } + } else { + self.operand(inst, &op)? + }; ``` **Test:** `flow_tests.rs` — a differential case (console dxc → translate → host GPU vs. PC dxc → host GPU, 256 lanes × 4 dwords, hardware and WARP). The HLSL shape `asuint(fa * fb + WaveReadLaneAt(fc, 3))` lowers to `v_pk_fmac_f16` with a DPP16 fetch on src0 on the console. The PC compiler lowers `WaveReadLaneAt` the same way the hardware does, so the bit-exact f16 results must match lane for lane. With the fix, both adapters agree on all 1024 words. Without it, the translated shader reads each lane's own src0 and the products differ on every lane whose DPP-selected lane holds a different value. ```rust #[test] fn packed_fmac_reads_src0_from_the_dpp_selected_lane() { // … (full test in the diff below) } ``` **Verified:** - `cargo test -p victory-shader --locked` — 145 passed, 0 failed, 2 ignored (native, Mac). - `cargo check -p victory-shader --profile test --target x86_64-pc-windows-msvc` — clean (the test is `#[cfg(all(test, windows))]`, so it compiles for the Windows target). - `rustfmt --edition 2024 --check` — clean on the changed lines (the repo has pre-existing fmt diffs from a newer rustfmt; none in this diff). - The test requires `VICTORY_CONSOLE_DXC` and a PC `dxc.exe` (same as the rest of `flow_tests`); it skips without them. **Not covered:** - Not run on a GPU here (no Windows/D3D12 on this Mac). The test is Windows-only like the rest of `flow_tests`; it will run in CI or on a Windows box. - The fix is minimal and follows the established pattern (`fsrc`, `src`, `half_src` all read `self.src0` for k=0). The `k == 0` guard ensures src1 and the destination (k=2) are unaffected. Full diff: ```diff diff --git a/crates/victory-shader/src/emit/flow_tests.rs b/crates/victory-shader/src/emit/flow_tests.rs index 8945cdd..1539246 100644 --- a/crates/victory-shader/src/emit/flow_tests.rs +++ b/crates/victory-shader/src/emit/flow_tests.rs @@ -178,6 +178,99 @@ const CS_CASES: &[(&str, &str)] = &[ ), ]; +// The console's `v_pk_fmac_f16` with a DPP fetch on src0: each lane reads +// its source from the DPP-selected lane of the wave, multiplies the packed +// halves, and adds its own destination. The PC compiler lowers `WaveReadLaneAt` +// the same way the hardware does, so the bit-exact f16 products must match +// lane for lane (the console program's wave size from its container). +#[test] +fn packed_fmac_reads_src0_from_the_dpp_selected_lane() { + let Some((console, pc)) = tools() else { + eprintln!("no console or PC compiler; skipped"); + return; + }; + let dir = std::env::temp_dir().join(format!("victory-flow-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let _one = ONE_AT_A_TIME.lock().unwrap_or_else(|e| e.into_inner()); + let lanes = 256u32; + let data = lane_inputs(lanes); + let views = [ + View::Buffer { + data: &data, + stride: Stride::Raw, + }, + View::RawUav(lanes * 4), + ]; + let src = r#"CS { + uint a = IN(0), b = IN(1), c = IN(2); + float fa = asfloat(a); + float fb = asfloat(b); + float fc = asfloat(c); + float fl = WaveReadLaneAt(fc, 3); + OUT(0, asuint(fa * fb + fl)); + OUT(1, asuint(fa)); + OUT(2, asuint(fb)); + OUT(3, asuint(fl)); + }"#; + let text = format!("{CS_COMMON}\n{src}\n"); + let mut wave = 64; + let ours = compile(&console, &dir, &text, "cs", "cs_6_5") + .and_then(|c| { + wave = console_wave(&c); + translate(&c).map_err(|e| e.to_string()) + }) + .expect("console compile and translate"); + let pc_cs = compile(&pc, &dir, &text, "cs", "cs_6_5").expect("PC compile"); + // SAFETY: the PC container carries its root signature. + for (label, warp) in [("hardware", false), ("warp", true)] { + let Ok(host) = Host::new(warp) else { + eprintln!("no SM 6.6 {label} device; skipped"); + continue; + }; + let (root_t, pso_t) = match host.compute_pipeline(&ours) { + Ok(p) => p, + // WARP runs no fixed wave size. + Err(e) if warp => { + eprintln!("{label}: skipped ({e})"); + continue; + } + Err(e) => panic!("{label}: {e}"), + }; + let dispatch = Dispatch { + roots: vec![(0, Root::Constants(console_user_data(&ours)))], + views: &views, + samplers: &[], + groups: [lanes / 64, 1, 1], + }; + let got = host + .dispatch(&root_t, &pso_t, &dispatch) + .unwrap_or_else(|e| panic!("{label}: {e}")); + let root_p: ID3D12RootSignature = + unsafe { host.device.CreateRootSignature(0, &pc_cs) }.unwrap(); + let pso_p = host.compute_pipeline_with(&root_p, &pc_cs).unwrap(); + let want = host + .dispatch(&root_p, &pso_p, &dispatch) + .unwrap_or_else(|e| panic!("{label}: {e}")); + let bad: Vec<usize> = (0..(lanes as usize * 4)) + .filter(|&w| got[0][w] != want[0][w]) + .collect(); + eprintln!( + "{label}: {wave}-lane waves, {} of {} words differ", + bad.len(), + lanes * 4 + ); + assert!( + bad.is_empty(), + "{label}: {} words differ, first at {} ({:#x} vs {:#x})", + bad.len(), + bad[0], + got[0][bad[0]], + want[0][bad[0]], + ); + } + let _ = std::fs::remove_dir_all(&dir); +} + const PS_COMMON: &str = r#" #define RS "RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT), RootConstants(num32BitConstants=4, b0)" cbuffer K : register(b0) { float4 k; }; diff --git a/crates/victory-shader/src/emit/lower.rs b/crates/victory-shader/src/emit/lower.rs index 2c06b01..1cc61fb 100644 --- a/crates/victory-shader/src/emit/lower.rs +++ b/crates/victory-shader/src/emit/lower.rs @@ -6658,7 +6658,16 @@ impl<'a> Lower<'a> { // shortcut: no constant operand seen; its high half is not known. return Err("a constant operand of v_pk_fmac_f16"); } - let x = self.operand(inst, &op)?; + // src0 as DPP fetched it, as for every other VOP2 operation + // (`valu` sets `self.src0` before the operation). + let x = if k == 0 { + match self.src0 { + Some(x) => x, + None => self.operand(inst, &op)?, + } + } else { + self.operand(inst, &op)? + }; let c = self.cur; let x = if high { let k16 = self.s.m.i32(16); self.s.m.bin(c, Bin::LShr, x, k16); } else { x }; let m = self.s.m.i32(0xFFFF); Ok(self.s.m.bin(c, Bin::And, x, m)) } ``` qwen3.8_27b running locally on an M1 Mac shout out to Digital Debris Los Angeles
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 15:50 UTC · original

Automated triage for the maintainers.

Confirmed on main (da82e44): in crates/victory-shader/src/emit/lower.rs, Lower::valu stores the DPP-selected source in self.src0, but Lower::fmac_half (called from Lower::packed16 for fmac_f16) reads source 0 with self.operand(...), so v_pk_fmac_f16 with DPP uses the lane's own src0. Open PRs addressing it: #2229 and #2266. The diff in the comment above makes the same change; PRs are open to everyone, and the existing PRs already carry this fix.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 15:50 UTC · [original](https://github.com/Marvisak/VIctory/issues/2228#issuecomment-7167360202) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-25T10:04:32Z at=2026-11-25T15:50:22Z --> Automated triage for the maintainers. Confirmed on main (da82e44): in `crates/victory-shader/src/emit/lower.rs`, `Lower::valu` stores the DPP-selected source in `self.src0`, but `Lower::fmac_half` (called from `Lower::packed16` for `fmac_f16`) reads source 0 with `self.operand(...)`, so `v_pk_fmac_f16` with DPP uses the lane's own `src0`. Open PRs addressing it: #2229 and #2266. The diff in the comment above makes the same change; PRs are open to everyone, and the existing PRs already carry this fix. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#2228
No description provided.