Shaders: lower waterfall loops over per-lane descriptors in one pass

A descriptor held in VGPRs is used through a waterfall loop: each pass
reads the first active lane's dwords with v_readfirstlane_b32, compares
them with every lane's own (v_cmp_eq), runs the access with the lanes
that match and repeats for the rest. Tracing stopped at the readfirstlane
results ("a descriptor neither loaded nor built").

The tracer now follows the read lanes' dwords to where each lane gets its
own: global_load_dword* from a table or heap at a per-lane address (an
image descriptor's two x4 loads lined up), or a v_cndmask_b32 choice per
lane between such sources (e.g. two s_load descriptors). Other sources
fail naming them (LDS read, buffer load, phi, input, computed VGPRs).
The loop's compares of those reads with their own dwords are recorded.

Lowering gives the access each lane's heap index (its load address over
the descriptor size, or a per-lane select of indices) with
createHandleFromHeap's nonUniformIndex set, lowers the recorded
readfirstlanes as the lane's own dword and their compares as true, so
the loop runs once with all lanes. Wave32 and wave64 are tested.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66
This commit is contained in:
Claude committed 2026-11-24 02:07:43 +00:00
1 parent a3d3bc91e7
commit ffa663571f
4 files changed
+755 -11

No files matched your search

+154 -5
View File
@@ -37,6 +37,12 @@ pub(crate) struct Lowered {
pub root_srvs: Vec<u8>,
pub user_data: Vec<UserData>,
pub samplers: Vec<[u32; 4]>,
/// Heap handles made with a non-uniform index.
#[cfg(test)]
pub nonuniform_handles: u32,
/// Whether the program uses wave operations.
#[cfg(test)]
pub wave_ops: bool,
}
pub(crate) struct Parts<'a> {
@@ -887,6 +893,10 @@ impl<'a> Lower<'a> {
cmp_samplers,
root_cbvs,
root_srvs,
#[cfg(test)]
nonuniform_handles: self.s.nonuniform_handles,
#[cfg(test)]
wave_ops: self.s.flags & flags::WAVE_OPS != 0,
dxil: self.s.finish(),
user_data,
samplers: self.samplers,
@@ -3168,7 +3178,9 @@ impl<'a> Lower<'a> {
/// The used-resource entry a descriptor loaded from a table falls in.
fn used_kind(&self, src: &Source) -> Option<&'a UsedResource> {
if let Source::Phi { args, .. } | Source::Select { args, .. } = src {
if let Source::Phi { args, .. } | Source::Select { args, .. } | Source::Lanes { args, .. } =
src
{
return self.used_kind(args.first()?);
}
if let Source::Built { address, .. } = src {
@@ -3180,7 +3192,7 @@ impl<'a> Lower<'a> {
.iter()
.find(|u| u.root == RootType::Descriptor && u.user_data == rp.user_data);
}
let Source::Loaded { load, first } = src else {
let (Source::Loaded { load, first } | Source::Fetched { load, first }) = src else {
return None;
};
let a = self.scalar.get(load)?;
@@ -3216,7 +3228,7 @@ impl<'a> Lower<'a> {
return Ok(h);
}
let index = self.heap_index(*load, *first, sampler)?;
let h = self.s.heap_handle(self.cur, index, sampler, props);
let h = self.s.heap_handle(self.cur, index, sampler, false, props);
self.handles.insert(key, h);
Ok(h)
}
@@ -3285,7 +3297,7 @@ impl<'a> Lower<'a> {
let join = self.block[*block].ok_or("a descriptor chosen by control flow")?;
let phi = self.s.m.phi(join, self.s.t.i32);
self.s.m.set_phi(phi, incoming);
Ok(self.s.heap_handle(self.cur, phi, sampler, props))
Ok(self.s.heap_handle(self.cur, phi, sampler, false, props))
}
Source::Select { scc, args } => {
// The heap index of each choice, picked by SCC (uniform).
@@ -3303,11 +3315,92 @@ impl<'a> Lower<'a> {
let set = self.heap_index(a, fa, sampler)?;
let clear = self.heap_index(b, fb, sampler)?;
let index = self.s.m.select(self.cur, cond, set, clear);
Ok(self.s.heap_handle(self.cur, index, sampler, props))
Ok(self.s.heap_handle(self.cur, index, sampler, false, props))
}
Source::Fetched { .. } | Source::Lanes { .. } => {
// 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.
let index = self.lane_heap_index(src, sampler)?;
Ok(self.s.heap_handle(self.cur, index, sampler, true, props))
}
}
}
/// The heap index of a descriptor that may differ between lanes,
/// computed in the current block.
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),
Source::Fetched { load, first } => self.fetched_index(*load, *first, sampler),
Source::Lanes { mask, args } | Source::Select { scc: mask, args } => {
let [a, b] = &args[..] else {
return Err("a descriptor chosen per lane");
};
let cond = if matches!(src, Source::Lanes { .. }) {
let &m = self
.x
.st
.masks
.get(mask)
.ok_or("a descriptor chosen per lane by an untracked mask")?;
self.boolean(m, self.cur)?
} else {
match self.get(*mask)? {
Val::Bit(x) => x,
_ => return Err("SCC without a value"),
}
};
let set = self.lane_heap_index(a, sampler)?;
let clear = self.lane_heap_index(b, sampler)?;
Ok(self.s.m.select(self.cur, cond, set, clear))
}
_ => Err("a per-lane descriptor from neither a heap load nor a choice of them"),
}
}
/// The heap index of the descriptor at dword `first` of what the global
/// load at `load` reads, in each lane: its address (base plus offsets)
/// over the descriptor size.
fn fetched_index(&mut self, load: Site, first: u8, sampler: bool) -> Result<V, Why> {
const WHY: Why = "a per-lane descriptor load";
let a = *self
.scalar
.get(&load)
.ok_or("an untracked descriptor load")?;
match self.x.native.layout.get(a.param).map(|p| p.root) {
Some(RootType::Table | RootType::ResourceHeap | RootType::SamplerHeap) => {}
_ => return Err("a descriptor outside the heaps"),
}
let li: &Inst = &self.x.p.blocks[load.0].insts[load.1];
let Fields::Flat {
addr: Some(Operand::Vector { reg, .. }),
saddr,
offset,
..
} = li.fields
else {
return Err(WHY);
};
let c = self.cur;
// The low dword of the address: SGPR base plus VGPR offset, or the
// VGPR pair's low half (heap addresses fit in 32 bits).
let mut at = self.i32_of(li.read(Reg::V(reg)).ok_or(WHY)?)?;
if let Some(Operand::Scalar { code, .. }) = saddr
&& code != NULL
{
let base = self.i32_of(li.read(Reg::S(code)).ok_or(WHY)?)?;
at = self.s.m.bin(c, Bin::Add, base, at);
}
let k = self
.s
.m
.i32((offset as i32 as u32).wrapping_add(4 * u32::from(first)));
at = self.s.m.bin(c, Bin::Add, at, k);
let shift = self.s.m.i32(if sampler { 4 } else { 5 });
Ok(self.s.m.bin(c, Bin::LShr, at, shift))
}
/// The heap index of the descriptor at dword `first` of what the scalar
/// load at `load` reads, computed in the current block.
fn heap_index(&mut self, load: Site, first: u8, sampler: bool) -> Result<V, Why> {
@@ -3353,6 +3446,12 @@ impl<'a> Lower<'a> {
unreachable!()
};
let name = inst.name();
if self.x.res.waterfall.compares.contains(&(b, i)) {
// The first lane's dwords of a waterfall loop are each lane's
// own (see `resource::Waterfall`).
self.lane(inst, Some(true));
return Ok(());
}
if let Some(rest) = name.strip_prefix("v_cmp_").or(name.strip_prefix("v_cmpx_")) {
let r = if v.dpp.is_some() {
Err("a compare with DPP")
@@ -4010,6 +4109,16 @@ impl<'a> Lower<'a> {
let r = self.s.m.select(c, is, a, old);
int(self, r)
}
"v_readfirstlane_b32"
if inst
.writes
.first()
.is_some_and(|w| self.x.res.waterfall.reads.contains(&w.1)) =>
{
// A waterfall loop's read of a descriptor dword: the lane's own.
let a = s(self, 0)?;
int(self, a)
}
"v_readfirstlane_b32" => {
let a = s(self, 0)?;
self.s.flags |= flags::WAVE_OPS;
@@ -6749,6 +6858,46 @@ mod tests {
lower_with(&words, layout(), used()).unwrap();
}
/// Tables of 2D textures at s0 and samplers at s1.
fn texture_tables() -> (Vec<RootParam>, Vec<UsedResource>) {
let range = |kind, user_data, slot| UsedResource {
kind,
root: RootType::Table,
table_offset_dwords: 0,
lower_bound: 0,
stride: 0,
dwords: if kind == ResourceType::Sampler { 4 } else { 8 },
user_data,
slot,
};
(
vec![root(RootType::Table, 0, 0), root(RootType::Table, 1, 1)],
vec![
range(ResourceType::Srv, 0, 0),
range(ResourceType::Sampler, 1, 1),
],
)
}
#[test]
fn waterfall_loops_over_per_lane_descriptors_sample_with_each_lanes_own() {
use crate::resource::tests::{
WATERFALL_FETCHED_WAVE32, WATERFALL_FETCHED_WAVE64, WATERFALL_PICKED_WAVE32,
};
for (words, wave64) in [
(WATERFALL_FETCHED_WAVE32, false),
(WATERFALL_FETCHED_WAVE64, true),
(WATERFALL_PICKED_WAVE32, false),
] {
let (layout, used) = texture_tables();
let l = lower_wave(words, layout, used, wave64).unwrap();
// The loop's reads of the first lane became each lane's own
// dwords: no wave operation is left.
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,
+15 -3
View File
@@ -65,6 +65,8 @@ pub(crate) struct Shader {
/// Root SRVs (raw buffers) read, likewise.
pub root_srvs: Vec<(u8, V)>,
pub flags: u64,
/// Heap handles made with a non-uniform index.
pub nonuniform_handles: u32,
/// A compute shader with this thread-group size.
pub threads: Option<[u32; 3]>,
/// The wave size a compute shader requires (`WaveSize`).
@@ -118,6 +120,7 @@ impl Shader {
root_cbvs: Vec::new(),
root_srvs: Vec::new(),
flags: 0,
nonuniform_handles: 0,
threads: None,
wave_size: None,
lds_dwords: 0,
@@ -272,14 +275,23 @@ impl Shader {
)
}
/// A descriptor heap handle, annotated with `props`.
pub fn heap_handle(&mut self, block: usize, index: V, sampler: bool, props: (u32, u32)) -> V {
/// A descriptor heap handle, annotated with `props`; `nonuniform` when
/// the index may differ between lanes (`NonUniformResourceIndex`).
pub fn heap_handle(
&mut self,
block: usize,
index: V,
sampler: bool,
nonuniform: bool,
props: (u32, u32),
) -> V {
self.flags |= if sampler {
flags::SAMPLER_HEAP
} else {
flags::RESOURCE_HEAP
};
let (s, n) = (self.m.bool(sampler), self.m.bool(false));
self.nonuniform_handles += u32::from(nonuniform);
let (s, n) = (self.m.bool(sampler), self.m.bool(nonuniform));
let h = self.op(
block,
"createHandleFromHeap",