Recompiler: direct pixel shaders read lane reads once more from the first live lane #3689

Open
lucsoft wants to merge 1 commit from upstream-pr/3689 into main
lucsoft commented 2026-12-02 13:07:23 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by lucsoft on 2026-12-02T13:07:23Z.


On AMD under RADV the tiled-lighting pixel shaders of the retail title (vs 73e9322e… + one TL_PS_PBR_MATERIAL_* each, ~45 passes a frame) cost about 21 ms each, about half of a 2 s frame. The console's scalar values reach them per lane, so the PC compiler treats every heap index derived from a v_readlane / v_readfirstlane as divergent. This reads those results once more from the console wave's first live lane, so the compiler sees them as wave-uniform. Where one hardware wave is one console wave (RADV with RADV_PERFTEST=pswave32), each lighting pass drops from ~21 ms to 1.5–2.6 ms.

Part of #1269. Based on main (c53e9696); it does not need #3547. The measurements below come from our #3547 test stack, where long runs are stable on this machine; a measurement on main is pending.

Review (Foniass) addressed in this push: the conflict with #3655 (main's split-form early returns first), the helper-only fallback (no wave op; each lane keeps its own value, which is also per console half), XCL_PS_UNIFORM_READS=0 = main exactly, the wave-size gate (branch on the lane count for wave32 programs), and a GPU test of the values.

What is in it

  • Cause. On the console a v_readfirstlane / v_readlane result is an SGPR: one value for the wave. In the direct translation it is built from quad reads and helper-lane selects that pick per lane (rla's quad reads; on amd-dev also helper_reads_own). The PC compiler cannot prove every live lane holds the same value, so everything derived from it is divergent: heap window indices, descriptor indices, branches. In an RGP capture of the retail title, each lighting PS compiled to 120–190 KB of wave64 code at 252–256 VGPRs, with ~59 waterfall loops, 40 of them inside the per-light loop.
  • Fix (dxil/lanes.rs Lw::ps_uniform_read, called from the v_readfirstlane_b32 / v_readlane_b32 lowering in lower_valu.rs, after main's split-form early returns):
    • Read the result once more with WaveReadLaneAt, from the console wave's first live lane (ballot-derived). Live lanes get the same value as before; helper lanes now get the console wave's value.
    • Where no live lane runs (a helper-only stretch), no wave operation has a defined result, so each lane keeps its own value: v_readfirstlane's source for a helper lane (helper_reads_own), so a helper-only waterfall loop ends in one pass. No wave read happens there.
  • Wave size. The read is uniform only where a hardware wave is one console wave. These console PS are wave32; RADV runs pixel shaders in wave64 by default on RDNA3, so one hardware wave then holds two console waves (xcl_half). For wave32 programs the read is therefore behind a branch on WaveGetLaneCount() == 32: on 64-lane hardware only the branch is added and main's value is kept; on 32-lane hardware (RADV_PERFTEST=pswave32, NVIDIA) the read is uniform. Split wave64 forms are left as they were.
  • helper_reads_own (a helper lane's v_readfirstlane takes its own value, so a helper-only waterfall loop converges in one pass) comes over from raylitebull's amd-dev work (6fa964de, part of #3547); it is the helper value ps_uniform_read falls back to.
  • XCL_PS_UNIFORM_READS=0 restores main's form exactly (no helper_reads_own either), with its own translated-cache identity (pso.rs).
  • Tests: direct_ps_lane_reads_end_in_a_uniform_read (both lane reads, wave32 and wave64 programs: DXIL validates, has the live-lane ballot, no waveReadLaneFirst, the lane-count gate exactly for wave32), and on the GPU gpu_direct_ps_readfirstlane_reads_the_first_live_lane: a triangle whose edge quads hold helper lanes; every live lane's v_readfirstlane of its own index must equal the lane s_ff1 of EXEC names.

Measurements (RX 7800 XT, RADV, retail title, the same commit on our #3547 test stack, XCL_GPU_TIMES over 8 frames from 790 s, 900 s runs, 0 page faults in each)

#3547 stack + pswave32 + pswave32 + this PR
frame (wall) 2019 ms 1706 ms 1074 ms
GPU time measured per frame 1651 ms 1188 ms 764 ms
one tiled-lighting pass ~21 ms ~12–24 ms 1.5–2.6 ms
the lighting submit 1279 ms – 333 ms

Tested

  • AMD Radeon RX 7800 XT, Mesa RADV 26.3.1, Ryzen 7 7800X3D, Wine; Windows GNU release build.
  • This head (570a8741): gpu::dxil::lanes:: 10 passed, 0 failed, both with RADV's default (pixel shaders wave64: the gated path) and with RADV_PERFTEST=pswave32 (the uniform read), incl. the new GPU value test and the two triangle-edge helper-lane tests.
  • Previous head (cdaf44e2, on main df0cfef6): workspace suite (Wine, 2 threads, one module per process) lib 2738 passed, 17 failed, 31 ignored, 1 died vs main 1c8b5514's 2735 / 17 / 31 / 1; integration 143 / 5 / 20 for both; no failure only on that head. The suite on this head is pending.
  • Visual check (render-target dumps at the post-load cell window, previous head vs pswave32 alone): no regression shown, none ruled out; two runs of the same build already differ a lot in the light-accumulation target (SSIM 0.56 with matching G-buffers).

Regression run

Both from the previous head cdaf44e2 (on main df0cfef6), with RADV_PERFTEST=pswave32, 180 s; 0 page faults, ring timeouts and resets for each. On this head's base, main c53e9696 ends a few seconds after start under Wine with any GPU until #3707 is merged (#3655's helper functions abort dxil-spirv), so the smoke pair is rerun then.

main:

xcl-smoke v1
commit: df0cfef638306f325b208e5adae18d35ab4bf4ed
gpu: AMD Radeon RX 7800 XT (RADV NAVI32)
title: f651e787
seconds: 180
exit: 0x0
frames shown: 2523 (PresentX 2523)
presents per 2 s (last 3): 0 1 2
device removals: 0
crashes: 0
peak commit: 0 MB

PR:

xcl-smoke v1
commit: cdaf44e2aa343963db72519642dc6ed23c18940b
gpu: AMD Radeon RX 7800 XT (RADV NAVI32)
title: f651e787
seconds: 180
exit: 0x0
frames shown: 2314 (PresentX 2314)
presents per 2 s (last 3): 3 1 2
device removals: 0
crashes: 0
peak commit: 0 MB

Not tested

  • Windows drivers, and how they choose the pixel shader wave size.
  • NVIDIA (32-lane waves; the read should be uniform there).
  • RDNA2 / RDNA4.
  • An exact image comparison (no deterministic frame replay; see Tested).

AI use

  • Claude Code, Claude Opus 5.5: the profiling (GPU times, queue trace, RGP code objects), the A/B runs, porting the commit and this description. The change itself was written earlier with Claude Code during the frame-time investigation; reviewed by me.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ivall/xcl:issue:5607028362 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3689). Originally posted by [lucsoft](https://github.com/lucsoft) on 2026-12-02T13:07:23Z. --- On AMD under RADV the tiled-lighting pixel shaders of the retail title (`vs 73e9322e…` + one `TL_PS_PBR_MATERIAL_*` each, ~45 passes a frame) cost about 21 ms each, about half of a 2 s frame. The console's scalar values reach them per lane, so the PC compiler treats every heap index derived from a `v_readlane` / `v_readfirstlane` as divergent. This reads those results once more from the console wave's first live lane, so the compiler sees them as wave-uniform. Where one hardware wave is one console wave (RADV with `RADV_PERFTEST=pswave32`), each lighting pass drops from ~21 ms to 1.5–2.6 ms. Part of #1269. Based on `main` (c53e9696); it does not need #3547. The measurements below come from our #3547 test stack, where long runs are stable on this machine; a measurement on `main` is pending. **Review (Foniass) addressed in this push:** the conflict with #3655 (main's split-form early returns first), the helper-only fallback (no wave op; each lane keeps its own value, which is also per console half), `XCL_PS_UNIFORM_READS=0` = main exactly, the wave-size gate (branch on the lane count for wave32 programs), and a GPU test of the values. **What is in it** - **Cause.** On the console a `v_readfirstlane` / `v_readlane` result is an SGPR: one value for the wave. In the direct translation it is built from quad reads and helper-lane selects that pick per lane (`rla`'s quad reads; on amd-dev also `helper_reads_own`). The PC compiler cannot prove every live lane holds the same value, so everything derived from it is divergent: heap window indices, descriptor indices, branches. In an RGP capture of the retail title, each lighting PS compiled to 120–190 KB of wave64 code at 252–256 VGPRs, with ~59 waterfall loops, 40 of them inside the per-light loop. - **Fix** (`dxil/lanes.rs` `Lw::ps_uniform_read`, called from the `v_readfirstlane_b32` / `v_readlane_b32` lowering in `lower_valu.rs`, after main's split-form early returns): - Read the result once more with `WaveReadLaneAt`, from the console wave's first live lane (ballot-derived). Live lanes get the same value as before; helper lanes now get the console wave's value. - Where no live lane runs (a helper-only stretch), no wave operation has a defined result, so each lane keeps its own value: `v_readfirstlane`'s source for a helper lane (`helper_reads_own`), so a helper-only waterfall loop ends in one pass. No wave read happens there. - **Wave size.** The read is uniform only where a hardware wave is one console wave. These console PS are wave32; RADV runs pixel shaders in wave64 by default on RDNA3, so one hardware wave then holds two console waves (`xcl_half`). For wave32 programs the read is therefore behind a branch on `WaveGetLaneCount() == 32`: on 64-lane hardware only the branch is added and main's value is kept; on 32-lane hardware (`RADV_PERFTEST=pswave32`, NVIDIA) the read is uniform. Split wave64 forms are left as they were. - `helper_reads_own` (a helper lane's `v_readfirstlane` takes its own value, so a helper-only waterfall loop converges in one pass) comes over from raylitebull's amd-dev work (`6fa964de`, part of #3547); it is the helper value `ps_uniform_read` falls back to. - `XCL_PS_UNIFORM_READS=0` restores main's form exactly (no `helper_reads_own` either), with its own translated-cache identity (`pso.rs`). - Tests: `direct_ps_lane_reads_end_in_a_uniform_read` (both lane reads, wave32 and wave64 programs: DXIL validates, has the live-lane ballot, no `waveReadLaneFirst`, the lane-count gate exactly for wave32), and on the GPU `gpu_direct_ps_readfirstlane_reads_the_first_live_lane`: a triangle whose edge quads hold helper lanes; every live lane's `v_readfirstlane` of its own index must equal the lane `s_ff1` of EXEC names. **Measurements** (RX 7800 XT, RADV, retail title, the same commit on our #3547 test stack, `XCL_GPU_TIMES` over 8 frames from 790 s, 900 s runs, 0 page faults in each) | | #3547 stack | + `pswave32` | + `pswave32` + this PR | |---|---|---|---| | frame (wall) | 2019 ms | 1706 ms | **1074 ms** | | GPU time measured per frame | 1651 ms | 1188 ms | **764 ms** | | one tiled-lighting pass | ~21 ms | ~12–24 ms | **1.5–2.6 ms** | | the lighting submit | 1279 ms | – | 333 ms | **Tested** - AMD Radeon RX 7800 XT, Mesa RADV 26.3.1, Ryzen 7 7800X3D, Wine; Windows GNU release build. - This head (570a8741): `gpu::dxil::lanes::` 10 passed, 0 failed, both with RADV's default (pixel shaders wave64: the gated path) and with `RADV_PERFTEST=pswave32` (the uniform read), incl. the new GPU value test and the two triangle-edge helper-lane tests. - Previous head (cdaf44e2, on main df0cfef6): workspace suite (Wine, 2 threads, one module per process) lib 2738 passed, 17 failed, 31 ignored, 1 died vs main 1c8b5514's 2735 / 17 / 31 / 1; integration 143 / 5 / 20 for both; no failure only on that head. The suite on this head is pending. - Visual check (render-target dumps at the post-load cell window, previous head vs `pswave32` alone): no regression shown, none ruled out; two runs of the same build already differ a lot in the light-accumulation target (SSIM 0.56 with matching G-buffers). **Regression run** Both from the previous head cdaf44e2 (on main df0cfef6), with `RADV_PERFTEST=pswave32`, 180 s; 0 page faults, ring timeouts and resets for each. On this head's base, main c53e9696 ends a few seconds after start under Wine with any GPU until #3707 is merged (#3655's helper functions abort dxil-spirv), so the smoke pair is rerun then. main: ``` xcl-smoke v1 commit: df0cfef638306f325b208e5adae18d35ab4bf4ed gpu: AMD Radeon RX 7800 XT (RADV NAVI32) title: f651e787 seconds: 180 exit: 0x0 frames shown: 2523 (PresentX 2523) presents per 2 s (last 3): 0 1 2 device removals: 0 crashes: 0 peak commit: 0 MB ``` PR: ``` xcl-smoke v1 commit: cdaf44e2aa343963db72519642dc6ed23c18940b gpu: AMD Radeon RX 7800 XT (RADV NAVI32) title: f651e787 seconds: 180 exit: 0x0 frames shown: 2314 (PresentX 2314) presents per 2 s (last 3): 3 1 2 device removals: 0 crashes: 0 peak commit: 0 MB ``` **Not tested** - Windows drivers, and how they choose the pixel shader wave size. - NVIDIA (32-lane waves; the read should be uniform there). - RDNA2 / RDNA4. - An exact image comparison (no deterministic frame replay; see Tested). **AI use** - Claude Code, Claude Opus 5.5: the profiling (GPU times, queue trace, RGP code objects), the A/B runs, porting the commit and this description. The change itself was written earlier with Claude Code during the frame-time investigation; reviewed by me. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Foniass commented 2026-12-02 13:17:48 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T13:17:48Z.


Review (head cdaf44e2, diff read, not a merge-check; draft, conflicts with main d71578f8, so not built or run):

The commit adds Lw::ps_uniform_read, which reads every direct-PS v_readlane / v_readfirstlane result once more with WaveReadLaneAt from the first lane of WaveActiveBallot(true) (per console half when gfx_half is set), falling back to WaveReadLaneFirst of a new helper_reads_own value when that ballot is empty; it's on by default for every direct PS and off with XCL_PS_UNIFORM_READS=0.

  • Conflict. Only crates/xcl/src/gpu/dxil/lower_valu.rs, in the v_readfirstlane_b32 / v_readlane_b32 arms (~1056), against #3655's 65f89f04 (uniform_reads / read_first_lane_uniform) and 685008d1 (lane_waits / keep_lane / How::OwnFirst). Those plans are only built for split compute / mesh (kernel.rs ~350, planned), so keeping main's early returns and putting this PR's two calls in the non-uniform_reads branch resolves it.
  • The helper-only fallback relies on undefined wave-op behaviour. any is false only when the ballot sees no non-helper lane, which this module assumes leaves helpers out (ballot_ps_local doc); WaveReadLaneFirst(helper) (lanes.rs ~1098) then has no participating lane, so its result is undefined in exactly the helper-only waterfall case it's meant to end. A driver value matching no helper's index would never drain that loop, a GPU hang.
  • The fallback isn't per console half. With gfx_half (wave32 console PS in 64-lane hardware: the RADV default, and probably AMD's Windows driver), u is read per half but WaveReadLaneFirst reads the whole hardware wave, so a helper-only half next to a live half gets the other console wave's value (lanes.rs ~1097-1099).
  • XCL_PS_UNIFORM_READS=0 doesn't restore main. The off path returns own = helper_reads_own(a, t) (lower_valu.rs), so helper lanes take their own value: amd-dev's 6fa964de behaviour, not main's, though the description says it "restores the previous form". helper_reads_own is a second behaviour change with no switch or test of its own, and its doc comment ("converge in one pass on AMD RDNA4") no longer holds once the fallback gives all helpers one value.
  • Not gated to where it helps, and default users pay for it. The only gates are is_pixel / !split / the env var, nothing on wave size. On 64-lane hardware k depends on the per-lane xcl_half, so it isn't uniform there and each lane read gains a ballot, two firstbitlow, WaveReadLaneAt, WaveReadLaneFirst, isHelperLane and selects for no gain. On NVIDIA (32-lane) it should fold to a uniform read, but neither NVIDIA nor Windows was run; the suite, smoke and visual check are all pending.
  • The test can't catch a wrong lane. direct_ps_lane_reads_end_in_a_uniform_read only checks that waveReadLaneFirst.i32( appears in the disassembly. Nothing checks the value live lanes get, the per-half path, or the helper-only fallback, though gpu_direct_ps_masks_hold_helper_lanes_on_triangle_edges (lanes.rs ~1287) shows a GPU PS harness exists for such a test. All measurements are from the #3547 stack, none from main.

Fine: for live lanes, "first live lane" gives the console's value, since t is built from the same first_lane(exec) / rla in every live lane; split forms are left alone; the rebase needs nothing from amd-dev beyond helper_reads_own, which the commit carries (GraphicsIo::is_helper_lane exists on main); the translated-cache identity is right for both settings.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7346651542 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3689#issuecomment-7346651542). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-02T13:17:48Z. --- Review (head cdaf44e2, diff read, not a merge-check; draft, conflicts with main d71578f8, so not built or run): The commit adds `Lw::ps_uniform_read`, which reads every direct-PS `v_readlane` / `v_readfirstlane` result once more with `WaveReadLaneAt` from the first lane of `WaveActiveBallot(true)` (per console half when `gfx_half` is set), falling back to `WaveReadLaneFirst` of a new `helper_reads_own` value when that ballot is empty; it's on by default for every direct PS and off with `XCL_PS_UNIFORM_READS=0`. - **Conflict.** Only `crates/xcl/src/gpu/dxil/lower_valu.rs`, in the `v_readfirstlane_b32` / `v_readlane_b32` arms (~1056), against #3655's `65f89f04` (`uniform_reads` / `read_first_lane_uniform`) and `685008d1` (`lane_waits` / `keep_lane` / `How::OwnFirst`). Those plans are only built for split compute / mesh (`kernel.rs` ~350, `planned`), so keeping main's early returns and putting this PR's two calls in the non-`uniform_reads` branch resolves it. - **The helper-only fallback relies on undefined wave-op behaviour.** `any` is false only when the ballot sees no non-helper lane, which this module assumes leaves helpers out (`ballot_ps_local` doc); `WaveReadLaneFirst(helper)` (`lanes.rs` ~1098) then has no participating lane, so its result is undefined in exactly the helper-only waterfall case it's meant to end. A driver value matching no helper's index would never drain that loop, a GPU hang. - **The fallback isn't per console half.** With `gfx_half` (wave32 console PS in 64-lane hardware: the RADV default, and probably AMD's Windows driver), `u` is read per half but `WaveReadLaneFirst` reads the whole hardware wave, so a helper-only half next to a live half gets the other console wave's value (`lanes.rs` ~1097-1099). - **`XCL_PS_UNIFORM_READS=0` doesn't restore main.** The off path returns `own` = `helper_reads_own(a, t)` (`lower_valu.rs`), so helper lanes take their own value: amd-dev's `6fa964de` behaviour, not main's, though the description says it "restores the previous form". `helper_reads_own` is a second behaviour change with no switch or test of its own, and its doc comment ("converge in one pass on AMD RDNA4") no longer holds once the fallback gives all helpers one value. - **Not gated to where it helps, and default users pay for it.** The only gates are `is_pixel` / `!split` / the env var, nothing on wave size. On 64-lane hardware `k` depends on the per-lane `xcl_half`, so it isn't uniform there and each lane read gains a ballot, two `firstbitlow`, `WaveReadLaneAt`, `WaveReadLaneFirst`, `isHelperLane` and selects for no gain. On NVIDIA (32-lane) it should fold to a uniform read, but neither NVIDIA nor Windows was run; the suite, smoke and visual check are all pending. - **The test can't catch a wrong lane.** `direct_ps_lane_reads_end_in_a_uniform_read` only checks that `waveReadLaneFirst.i32(` appears in the disassembly. Nothing checks the value live lanes get, the per-half path, or the helper-only fallback, though `gpu_direct_ps_masks_hold_helper_lanes_on_triangle_edges` (`lanes.rs` ~1287) shows a GPU PS harness exists for such a test. All measurements are from the #3547 stack, none from main. Fine: for live lanes, "first live lane" gives the console's value, since `t` is built from the same `first_lane(exec)` / `rla` in every live lane; split forms are left alone; the rebase needs nothing from amd-dev beyond `helper_reads_own`, which the commit carries (`GraphicsIo::is_helper_lane` exists on main); the translated-cache identity is right for both settings. AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
gut0to commented 2026-12-02 19:45:51 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-12-02T19:45:51Z.

Review: COMMENTED


AI-assisted review: GPT-6 helped inspect the DXIL path and HLSL wave contract; I verified the behavior.

<!-- greyfinch-sync:ivall/xcl:review:6157721471 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3689#pullrequestreview-6157721471). Originally posted by [gut0to](https://github.com/gut0to) on 2026-12-02T19:45:51Z. **Review:** COMMENTED --- AI-assisted review: GPT-6 helped inspect the DXIL path and HLSL wave contract; I verified the behavior.
gut0to commented 2026-12-02 19:45:51 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-12-02T19:45:51Z.

Code review location: crates/xcl/src/gpu/dxil/lanes.rs; line 1106

@@ -1051,6 +1051,75 @@ impl Lw {
         let lnz = self.nz(lo);
         self.sel(lnz, fl, hv)
     }
+
+    /// `v_readfirstlane_b32`'s result `first` of `own` in a pixel shader: a helper lane takes its
+    /// own value instead. [`Lw::ps_uniform_read`]'s fallback where no live lane runs (a
+    /// helper-only stretch): each helper then reads its own index, so a helper-only waterfall
+    /// loop ends in one pass, with no wave operation (none has a defined result there).
+    pub fn helper_reads_own(&mut self, own: Value, first: Value) -> Value {
+        let Some(g) = self.gfx.as_mut().filter(|g| g.ps.is_some()) else {
+            return first;
+        };
+        let helper = g.io.is_helper_lane(&mut self.s);
+        self.sel(helper, own, first)
+    }
+
+    /// A pixel shader's `v_readlane` / `v_readfirstlane` result `t` made wave-uniform for the PC
+    /// compiler (`ps_uniform_reads_wanted`): `t` is the same in every live lane of the console
+    /// wave, but the quad reads and helper selects that build it are per lane, so everything
+    /// derived from the scalar register – heap window indices, descriptor indices, branches –
+    /// was divergent too, and RADV wrapped each heap read of a tiled-lighting PS's light loop
+    /// in a waterfall loop (40 of them per light, each waiting for all loads in flight).
+    ///
+    /// Every lane takes `t` of the console wave's first live lane. Where no live lane runs (a
+    /// helper-only stretch) a wave read has no participating lane, so each lane keeps its own
+    /// value instead: `own` (`v_readfirstlane`'s source, through [`Lw::helper_reads_own`]) or
+    /// `t`. Only where one hardware wave is one console wave: a wave32 console PS in 64-lane
+    /// hardware (`gfx_half`) holds two console waves, a read there would stay per half and
+    /// gain nothing, so that form branches on the hardware's lane count and keeps `t` on the
+    /// 64-lane side. Off (`XCL_PS_UNIFORM_READS=0`), outside pixel shaders and in the split
+    /// form: `t` unchanged.
+    pub fn ps_uniform_read(&mut self, t: Value, own: Option<Value>) -> super::lower::LResult<Value> {
+        if !self.is_pixel() || self.split || !crate::gpu::recomp::emit::ps_uniform_reads_wanted() {
+            return Ok(t);
+        }
+        if self.wave64 || self.gfx_half.is_none() {
+            return Ok(self.first_live_read(t, own));
+        }
+        // a wave32 console PS: uniform only in 32-lane hardware (one hardware wave = one console wave)
+        let n = self.s.wave_lane_count();
+        let narrow = self.cmpc(Pred::Eq, n, 32);
+        let var = self.tmp_var();
+        self.wr(var, t);
+        self.guarded(narrow, &mut |lw| {
+            let u = lw.first_live_read(t, own);
+            lw.wr(var, u);
+            Ok(())
+        })?;
+        Ok(self.rd(var))
+    }
+
+    /// [`Lw::ps_uniform_read`] where one hardware wave is one console wave: `t` of the first live
+    /// lane in every lane; each lane's own value where none runs.
+    fn first_live_read(&mut self, t: Value, own: Option<Value>) -> Value {
+        let tr = self.s.m.i1c(true);
+        let lv = self.s.wave_ballot(tr);

Medium: WaveActiveBallot(true) can include helper lanes in pixel shaders. If a helper has a lower lane index than the first live lane, firstbitlow selects it and waveReadLaneAt broadcasts its per-lane t to the live lanes, so this does not guarantee the first live lane's value. helper_reads_own only handles an empty-ballot fallback; it does not exclude helpers when the ballot is nonempty. Could the ballot predicate exclude helper lanes while retaining the empty-ballot fallback, with a test where a lower-index helper precedes a live lane? AI-assisted review: GPT-6 helped inspect the DXIL path; I verified this against Microsoft's Shader Model 6 documentation and WaveActiveBallot documentation.

<!-- greyfinch-sync:ivall/xcl:review-comment:4775025431 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3689#discussion_r4775025431). Originally posted by [gut0to](https://github.com/gut0to) on 2026-12-02T19:45:51Z. **Code review location:** `crates/xcl/src/gpu/dxil/lanes.rs`; line 1106 ~~~~diff @@ -1051,6 +1051,75 @@ impl Lw { let lnz = self.nz(lo); self.sel(lnz, fl, hv) } + + /// `v_readfirstlane_b32`'s result `first` of `own` in a pixel shader: a helper lane takes its + /// own value instead. [`Lw::ps_uniform_read`]'s fallback where no live lane runs (a + /// helper-only stretch): each helper then reads its own index, so a helper-only waterfall + /// loop ends in one pass, with no wave operation (none has a defined result there). + pub fn helper_reads_own(&mut self, own: Value, first: Value) -> Value { + let Some(g) = self.gfx.as_mut().filter(|g| g.ps.is_some()) else { + return first; + }; + let helper = g.io.is_helper_lane(&mut self.s); + self.sel(helper, own, first) + } + + /// A pixel shader's `v_readlane` / `v_readfirstlane` result `t` made wave-uniform for the PC + /// compiler (`ps_uniform_reads_wanted`): `t` is the same in every live lane of the console + /// wave, but the quad reads and helper selects that build it are per lane, so everything + /// derived from the scalar register – heap window indices, descriptor indices, branches – + /// was divergent too, and RADV wrapped each heap read of a tiled-lighting PS's light loop + /// in a waterfall loop (40 of them per light, each waiting for all loads in flight). + /// + /// Every lane takes `t` of the console wave's first live lane. Where no live lane runs (a + /// helper-only stretch) a wave read has no participating lane, so each lane keeps its own + /// value instead: `own` (`v_readfirstlane`'s source, through [`Lw::helper_reads_own`]) or + /// `t`. Only where one hardware wave is one console wave: a wave32 console PS in 64-lane + /// hardware (`gfx_half`) holds two console waves, a read there would stay per half and + /// gain nothing, so that form branches on the hardware's lane count and keeps `t` on the + /// 64-lane side. Off (`XCL_PS_UNIFORM_READS=0`), outside pixel shaders and in the split + /// form: `t` unchanged. + pub fn ps_uniform_read(&mut self, t: Value, own: Option<Value>) -> super::lower::LResult<Value> { + if !self.is_pixel() || self.split || !crate::gpu::recomp::emit::ps_uniform_reads_wanted() { + return Ok(t); + } + if self.wave64 || self.gfx_half.is_none() { + return Ok(self.first_live_read(t, own)); + } + // a wave32 console PS: uniform only in 32-lane hardware (one hardware wave = one console wave) + let n = self.s.wave_lane_count(); + let narrow = self.cmpc(Pred::Eq, n, 32); + let var = self.tmp_var(); + self.wr(var, t); + self.guarded(narrow, &mut |lw| { + let u = lw.first_live_read(t, own); + lw.wr(var, u); + Ok(()) + })?; + Ok(self.rd(var)) + } + + /// [`Lw::ps_uniform_read`] where one hardware wave is one console wave: `t` of the first live + /// lane in every lane; each lane's own value where none runs. + fn first_live_read(&mut self, t: Value, own: Option<Value>) -> Value { + let tr = self.s.m.i1c(true); + let lv = self.s.wave_ballot(tr); ~~~~ --- **Medium**: `WaveActiveBallot(true)` can include helper lanes in pixel shaders. If a helper has a lower lane index than the first live lane, `firstbitlow` selects it and `waveReadLaneAt` broadcasts its per-lane `t` to the live lanes, so this does not guarantee the first live lane's value. `helper_reads_own` only handles an empty-ballot fallback; it does not exclude helpers when the ballot is nonempty. Could the ballot predicate exclude helper lanes while retaining the empty-ballot fallback, with a test where a lower-index helper precedes a live lane? AI-assisted review: GPT-6 helped inspect the DXIL path; I verified this against [Microsoft's Shader Model 6 documentation](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/hlsl-shader-model-6-0-features-for-direct3d-12) and [WaveActiveBallot documentation](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/waveballot).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3689:upstream-pr/3689'
git switch 'upstream-pr/3689'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3689'
git switch 'upstream-pr/3689'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3689'
git switch 'upstream-pr/3689'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3689'
git switch 'main'
git merge --squash 'upstream-pr/3689'
git switch 'main'
git merge --ff-only 'upstream-pr/3689'
git switch 'main'
git merge 'upstream-pr/3689'
git push origin 'main'
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
xcl-dev/xcl!3689
No description provided.