|
|
@@ -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> {
|
|
|
@@ -900,6 +906,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,
|
|
|
@@ -3250,9 +3260,14 @@ 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, .. } = src {
|
|
|
+ if let Source::Phi { args, .. } | Source::Select { args, .. } | Source::Lanes { args, .. } =
|
|
|
+ src
|
|
|
+ {
|
|
|
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
|
|
|
@@ -3262,7 +3277,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)?;
|
|
|
@@ -3298,7 +3313,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)
|
|
|
}
|
|
|
@@ -3367,11 +3382,141 @@ 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).
|
|
|
+ let [
|
|
|
+ Source::Loaded { load: a, first: fa },
|
|
|
+ Source::Loaded { load: b, first: fb },
|
|
|
+ ] = args[..]
|
|
|
+ else {
|
|
|
+ return Err("a descriptor chosen by a condition");
|
|
|
+ };
|
|
|
+ let cond = match self.get(*scc)? {
|
|
|
+ Val::Bit(x) => x,
|
|
|
+ _ => return Err("SCC without a value"),
|
|
|
+ };
|
|
|
+ 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, false, props))
|
|
|
+ }
|
|
|
+ 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.
|
|
|
+ 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 (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),
|
|
|
+ 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))
|
|
|
+ }
|
|
|
+ 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"),
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 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> {
|
|
|
@@ -3417,6 +3562,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")
|
|
|
@@ -4074,6 +4225,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;
|
|
|
@@ -6781,8 +6942,10 @@ impl<'a> Lower<'a> {
|
|
|
|
|
|
#[cfg(test)]
|
|
|
mod tests {
|
|
|
- use super::{Numeric, Parts, buffer_format, half_shift, lower};
|
|
|
- use crate::native::{ComputeIo, HwStage, NativeProgram, StageIo};
|
|
|
+ use super::{Lowered, Numeric, Parts, buffer_format, half_shift, lower};
|
|
|
+ use crate::native::{
|
|
|
+ ComputeIo, HwStage, NativeProgram, ResourceType, RootParam, RootType, StageIo, UsedResource,
|
|
|
+ };
|
|
|
use crate::{io, ir, resource, structure};
|
|
|
|
|
|
const S_ENDPGM: u32 = 0xBF81_0000;
|
|
|
@@ -6803,9 +6966,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,
|
|
|
@@ -6820,6 +6983,67 @@ mod tests {
|
|
|
.map(|_| ())
|
|
|
}
|
|
|
|
|
|
+ /// Lowers a compute program with the root parameters `layout` (in user
|
|
|
+ /// SGPRs from s0 on) and the used resources `used`.
|
|
|
+ fn lower_with(
|
|
|
+ words: &[u32],
|
|
|
+ layout: Vec<RootParam>,
|
|
|
+ used: Vec<UsedResource>,
|
|
|
+ ) -> Result<Lowered, crate::ShaderError> {
|
|
|
+ lower_wave(words, layout, used, false)
|
|
|
+ }
|
|
|
+
|
|
|
+ /// [`lower_with`] in wave64 if `wave64`.
|
|
|
+ fn lower_wave(
|
|
|
+ words: &[u32],
|
|
|
+ layout: Vec<RootParam>,
|
|
|
+ used: Vec<UsedResource>,
|
|
|
+ wave64: bool,
|
|
|
+ ) -> Result<Lowered, crate::ShaderError> {
|
|
|
+ let code: Vec<u8> = words.iter().flat_map(|w| w.to_le_bytes()).collect();
|
|
|
+ let sgprs: u32 = layout.iter().map(|p| p.sgprs).sum();
|
|
|
+ let native = NativeProgram {
|
|
|
+ stage: HwStage::Cs,
|
|
|
+ wave64,
|
|
|
+ vgprs: 256,
|
|
|
+ layout,
|
|
|
+ used,
|
|
|
+ io: StageIo::Cs(ComputeIo {
|
|
|
+ // The layout's user SGPRs (at least two), 512 bytes of LDS.
|
|
|
+ rsrc2: 1 << 15 | sgprs.max(2) << 1,
|
|
|
+ threads: [1, 1, 1],
|
|
|
+ }),
|
|
|
+ code,
|
|
|
+ };
|
|
|
+ let p = ir::build(&native.code, wave64, 256).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,
|
|
|
+ st: &st,
|
|
|
+ io: &io,
|
|
|
+ res: &res,
|
|
|
+ inputs: vec![],
|
|
|
+ outputs: vec![],
|
|
|
+ prims: vec![],
|
|
|
+ mesh_stride: 0,
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ /// Root parameter `slot` in user SGPRs from `user_data` on.
|
|
|
+ fn root(root: RootType, user_data: u8, slot: u8) -> RootParam {
|
|
|
+ RootParam {
|
|
|
+ root,
|
|
|
+ table_offset_dwords: 0,
|
|
|
+ sgprs: if root == RootType::Descriptor { 2 } else { 1 },
|
|
|
+ base_register: 0,
|
|
|
+ user_data,
|
|
|
+ slot,
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
#[test]
|
|
|
fn a_scalar_carry_chain_reaches_the_select() {
|
|
|
// The high dword of a 64-bit add or subtract, then a select on the
|
|
|
@@ -6893,12 +7117,10 @@ mod tests {
|
|
|
.flat_map(|b| &b.insts)
|
|
|
.any(|i| i.name() == "v_perm_b32"));
|
|
|
|
|
|
- let res = resource::track(
|
|
|
- &p, &native.layout, native.stage
|
|
|
- ).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();
|
|
|
- let st = structure::structure(&p).unwrap();
|
|
|
|
|
|
let emitted = lower(&Parts {
|
|
|
native: &native,
|
|
|
@@ -6935,6 +7157,112 @@ mod tests {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ #[test]
|
|
|
+ fn a_descriptor_selected_by_scc_picks_its_heap_index() {
|
|
|
+ // Two buffer descriptors loaded from the table at s0, one picked by
|
|
|
+ // a root constant (s1) with s_cselect_b64 per half, and loaded
|
|
|
+ // through by index and stored to LDS so the load is needed.
|
|
|
+ let words = [
|
|
|
+ 0xBE84_0300, // s_mov_b32 s4, s0
|
|
|
+ 0xBE85_0380, // s_mov_b32 s5, 0
|
|
|
+ 0xF408_0202,
|
|
|
+ 0xFA00_0000, // s_load_dwordx4 s[8:11], s[4:5], null
|
|
|
+ 0xF408_0302,
|
|
|
+ 0xFA00_0010, // s_load_dwordx4 s[12:15], s[4:5], 0x10
|
|
|
+ 0xBF06_8001, // s_cmp_eq_u32 s1, 0
|
|
|
+ 0x8590_0C08, // s_cselect_b64 s[16:17], s[8:9], s[12:13]
|
|
|
+ 0x8592_0E0A, // s_cselect_b64 s[18:19], s[10:11], s[14:15]
|
|
|
+ 0xE030_2000,
|
|
|
+ 0x8004_0100, // buffer_load_dword v1, v0, s[16:19], 0 idxen
|
|
|
+ 0xD834_0000,
|
|
|
+ 0x0000_0100, // ds_write_b32 v0, v1
|
|
|
+ S_ENDPGM,
|
|
|
+ ];
|
|
|
+ let layout = || vec![root(RootType::Table, 0, 0), root(RootType::Constants, 1, 1)];
|
|
|
+ // Both are structured buffers of dwords in the table's range.
|
|
|
+ let used = || {
|
|
|
+ vec![UsedResource {
|
|
|
+ kind: ResourceType::Srv,
|
|
|
+ root: RootType::Table,
|
|
|
+ table_offset_dwords: 0,
|
|
|
+ lower_bound: 0,
|
|
|
+ stride: 4,
|
|
|
+ dwords: 4,
|
|
|
+ user_data: 0,
|
|
|
+ slot: 0,
|
|
|
+ }]
|
|
|
+ };
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// [`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 scalar_bit_tests_lower() {
|
|
|
// Each tests one bit of a constant into SCC and branches on it (only
|