Shaders: per-lane descriptors chosen by control flow

A waterfall loop whose descriptor dwords are VGPR phis stopped with "a
descriptor read from the first lane of a phi" (294 programs of the
title on #307, all at if/else joins of two s_load_dwordx4 descriptors
copied into VGPRs). An EXEC if/else leaves two phis per dword: the
flow block's joins an arm's dwords with what lanes that skipped the arm
held before (inputs, unrelated loads and arithmetic in the title), the
join after the other arm picks per path. Only lanes that never read the
descriptor bring those other values, so tracing every edge fails.

Tracing (resource::track now takes the program's per-lane form):
- per-lane phis of one block are traced on each edge into it that the
  lanes accessing the descriptor can take, from structure's lane
  conditions (new Structured::reach, the exits' conditions, and
  Bools::meet, a read-only satisfiability check); the trace starts from
  what holds in the access's block. Edges no such lane takes are None
  in the new Source::LanePhi { block, args };
- every per-lane write it follows (v_mov_b32 copies, v_cndmask_b32,
  global_load_dword*) must run in all those lanes: a guarded one fails
  with "a per-lane descriptor written where some lanes may keep the old
  one". On #307 an else arm without its s_cbranch_execz traced as the
  second descriptor for every lane;
- a loop header's phi fails with "a descriptor read from the first lane
  of a phi at a loop header", words defined after the join with "a
  descriptor partly joined by control flow"; without the per-lane form
  (structurisation failed) the join is refused.

Lowering computes each traced edge's heap index at its predecessor's end
(through lane_heap_index, so loads, per-lane loads, choices and nested
joins work), joins them with a phi (0 on untaken edges) and makes the
handle non-uniform. The waterfall compares need no change.

Tests: the if/else of two loaded image descriptors, a join of a per-lane
load and a v_cndmask choice, nested arms choosing a buffer descriptor
with lanes that skip both and do not load (pruned only by the access's
condition), a loop header phi, and an unskipped else arm. On #307's head
the three join programs fail with "...of a phi" and the unskipped arm is
traced as one descriptor for all lanes. On an RTX 5070 and WARP, each
lane reads its own arm's texture or buffer; the shaders pass dxil.dll
1.8.2502.11. Reversing the phi's inputs fails validation, flipping the
heap index fails the GPU check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
r4.andClaude Opus 5.5 committed 2026-11-24 06:59:46 +02:00
1 parent 6285e19a8a
commit 6638098cee
8 files changed
+1114 -66

No files matched your search

+68 -6
View File
@@ -3183,6 +3183,9 @@ impl<'a> Lower<'a> {
{
return self.used_kind(args.first()?);
}
if let Source::LanePhi { args, .. } = src {
return args.iter().flatten().find_map(|a| self.used_kind(a));
}
if let Source::Built { address, .. } = src {
let rp = self.x.native.layout.get(address.param)?;
return self
@@ -3317,7 +3320,7 @@ impl<'a> Lower<'a> {
let index = self.s.m.select(self.cur, cond, set, clear);
Ok(self.s.heap_handle(self.cur, index, sampler, false, props))
}
Source::Fetched { .. } | Source::Lanes { .. } => {
Source::Fetched { .. } | Source::Lanes { .. } | Source::LanePhi { .. } => {
// A descriptor each lane has its own of (a waterfall loop
// reads it from the first active lane): each lane's heap
// index, non-uniform.
@@ -3328,7 +3331,8 @@ impl<'a> Lower<'a> {
}
/// The heap index of a descriptor that may differ between lanes,
/// computed in the current block.
/// computed in the current block (or, chosen by control flow, at the
/// join).
fn lane_heap_index(&mut self, src: &Source, sampler: bool) -> Result<V, Why> {
match src {
Source::Loaded { load, first } => self.heap_index(*load, *first, sampler),
@@ -3355,6 +3359,36 @@ impl<'a> Lower<'a> {
let clear = self.lane_heap_index(b, sampler)?;
Ok(self.s.m.select(self.cur, cond, set, clear))
}
Source::LanePhi { block, args } => {
// Each predecessor computes its heap index at its end (what
// it reads is there: the dwords are, on that edge); the join
// picks the one of the edge the lane came by. An edge no
// lane reading it takes gives index 0.
let join =
self.block[*block].ok_or("a per-lane descriptor chosen by control flow")?;
let mut incoming = Vec::new();
for (&q, arg) in self.x.p.blocks[*block].preds.iter().zip(args) {
if self.block[q].is_none() {
continue;
}
let tail =
self.tail[q].ok_or("a per-lane descriptor joined from a later block")?;
let at = self.edges.get(&(q, *block)).copied().unwrap_or(tail);
let index = match arg {
Some(arg) => {
let here = std::mem::replace(&mut self.cur, at);
let index = self.lane_heap_index(arg, sampler);
self.cur = here;
index?
}
None => self.s.m.i32(0),
};
incoming.push((index, at));
}
let phi = self.s.m.phi(join, self.s.t.i32);
self.s.m.set_phi(phi, incoming);
Ok(phi)
}
_ => Err("a per-lane descriptor from neither a heap load nor a choice of them"),
}
}
@@ -6710,9 +6744,9 @@ mod tests {
code,
};
let p = ir::build(&native.code, false, 256).unwrap();
let res = resource::track(&p, &native.layout, native.stage).unwrap();
let io = io::raise(&p, &native).unwrap();
let st = structure::structure(&p).unwrap();
let res = resource::track(&p, &native.layout, native.stage, Some(&st)).unwrap();
let io = io::raise(&p, &native).unwrap();
lower(&Parts {
native: &native,
p: &p,
@@ -6760,9 +6794,9 @@ mod tests {
code,
};
let p = ir::build(&native.code, wave64, 256).unwrap();
let res = resource::track(&p, &native.layout, native.stage)?;
let io = io::raise(&p, &native).unwrap();
let st = structure::structure(&p).unwrap();
let res = resource::track(&p, &native.layout, native.stage, Some(&st))?;
let io = io::raise(&p, &native).unwrap();
lower(&Parts {
native: &native,
p: &p,
@@ -6898,6 +6932,34 @@ mod tests {
}
}
/// [`texture_tables`] with a raw buffer UAV at dword 16 of the first.
pub(crate) fn texture_and_uav_tables() -> (Vec<RootParam>, Vec<UsedResource>) {
let (layout, mut used) = texture_tables();
used.push(UsedResource {
kind: ResourceType::Uav,
root: RootType::Table,
table_offset_dwords: 16,
lower_bound: 0,
stride: 0,
dwords: 4,
user_data: 0,
slot: 0,
});
(layout, used)
}
#[test]
fn waterfall_loops_over_descriptors_joined_per_lane_sample_with_each_lanes_own() {
use crate::resource::tests::{WATERFALL_JOINED_MIXED_WAVE32, WATERFALL_JOINED_WAVE32};
for words in [WATERFALL_JOINED_WAVE32, WATERFALL_JOINED_MIXED_WAVE32] {
let (layout, used) = texture_and_uav_tables();
let l = lower_wave(words, layout, used, false).unwrap();
// One image handle, from the join's per-lane heap index.
assert_eq!(l.nonuniform_handles, 1);
assert!(!l.wave_ops);
}
}
#[test]
fn buffer_formats_by_number() {
// The numbers the disassembler names 8_8_8_8_UNORM, 16_16_SNORM,