Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::structure::{Atom, Bool, LaneExit, Map, Set, Structured};
mod atomics;
mod barriers;
mod bvh;
mod descriptor_records;
mod f64;
mod first_lane;
/// Format loads that move 16-bit halves without the d16 form, a child
Expand Down Expand Up @@ -4931,7 +4932,10 @@ impl<'a> Lower<'a> {
.find_map(|&(s, _)| use_of(s))
});
let Some((at, first)) = use_at else {
return self.view_addresses(inst, site, code, count);
return match self.unused_buffer(site) {
Some(props) => self.buffer_records(inst, site, code, count, props),
None => self.view_addresses(inst, site, code, count),
};
};
let src = Source::Loaded { load: site, first };
let used = self.used_kind(&src);
Expand Down
101 changes: 101 additions & 0 deletions crates/victory-shader/src/emit/lower/descriptor_records.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//! A buffer descriptor the program loads only to read its record count
//! (dword 2): no instruction of the program accesses the buffer, so its
//! kind comes from the used-resource entry of the table slot it is loaded
//! from.

use super::*;

impl Lower<'_> {
/// The handle properties of the buffer descriptor that `site` loads
/// whole from a table slot, when the program never accesses it: by its
/// used-resource entry's stride, structured (the record count in
/// elements) or raw (in bytes), as for a buffer read with scalar loads
/// ([`Lower::descriptor_data`]). None for anything else: a load that
/// does not start at a buffer descriptor of the entry, a constant
/// buffer, or an odd stride.
pub(super) fn unused_buffer(&self, site: Site) -> Option<(u32, u32)> {
// Only the record count: a program that also reads the address
// takes it from the view addresses ([`Lower::view_addresses`]).
let inst = &self.x.p.blocks[site.0].insts[site.1];
let read = |v: Value| {
self.x.p.blocks.iter().any(|b| {
b.phis.iter().any(|f| f.args.contains(&v))
|| b.insts.iter().any(|i| i.reads.iter().any(|r| r.1 == v))
})
};
let Fields::Smem {
data: Some(Operand::Scalar { code, .. }),
..
} = inst.fields
else {
return None;
};
let others = inst
.writes
.iter()
.filter(|w| w.0 != Reg::S(code + 2))
.any(|w| read(w.1));
if others {
return None;
}
let a = self.scalar.get(&site)?;
let used = self.used_kind(&Source::Loaded {
load: site,
first: 0,
})?;
let dword = a.offset / 4;
if !a.offset.is_multiple_of(4)
|| used.dwords != 4
|| !(dword - used.table_offset_dwords).is_multiple_of(4)
{
return None;
}
let u = match used.kind {
ResourceType::Srv => 0,
ResourceType::Uav => 0x1000,
_ => return None,
};
match used.stride {
0 | 1 => Some((11 | u, 0)),
s if s.is_multiple_of(4) => Some((12 | u, s)),
_ => None,
}
}

/// Dword 2 of the buffer descriptor `site` loads ([`Lower::unused_buffer`],
/// `props`): the record count the host view gives; the other dwords
/// stop the program when read, as in [`Lower::descriptor_data`].
pub(super) fn buffer_records(
&mut self,
inst: &Inst,
site: Site,
code: u8,
count: u32,
props: (u32, u32),
) -> Result<(), Why> {
let src = Source::Loaded {
load: site,
first: 0,
};
let h = self.handle(site, &src, false, props)?;
let c = self.cur;
let i32t = self.s.t.i32;
let dims = self.s.m.named_struct("dx.types.Dimensions", vec![i32t; 4]);
let mip = self.s.m.undef(i32t);
let d = self
.s
.op(c, "getDimensions", Attr::ReadOnly, dims, 72, &[h, mip]);
let records = self.s.m.extract(c, d, 0);
for k in 0..count {
if k == 2 {
self.write(inst, Reg::S(code + 2), records, None);
} else if let Some(v) = inst.write(Reg::S(code + k as u8)) {
self.vals.insert(
v,
Val::Missing("a descriptor's address or format read as data"),
);
}
}
Ok(())
}
}
85 changes: 82 additions & 3 deletions crates/victory-shader/src/emit/waterfall_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ use crate::{io, ir, resource, structure};

/// Lowers a wave32 compute program of 32 threads with the table of views at
/// s0 (textures, or with `buffers` structured buffers of dwords, at entries
/// 0 and 1, a raw UAV at entry 2) and the table of samplers at s1.
/// 0 and 1, a raw UAV at entry 2) and the table of samplers at s1, with one
/// access by each lane's own heap index.
fn build(words: &[u32], buffers: bool) -> HostShader {
build_with(words, buffers, 1)
}

/// `build` for a program with `nonuniform` accesses by each lane's own heap
/// index: 0 where every lane's descriptor is the same.
fn build_with(words: &[u32], buffers: bool, nonuniform: u32) -> HostShader {
let table = |user_data: u8| RootParam {
root: RootType::Table,
table_offset_dwords: 0,
Expand Down Expand Up @@ -80,8 +87,8 @@ fn build(words: &[u32], buffers: bool) -> HostShader {
view_marks: false,
})
.expect("lower");
// One access with each lane's own heap index, no wave operation.
assert_eq!(lowered.nonuniform_handles, 1);
// No wave operation.
assert_eq!(lowered.nonuniform_handles, nonuniform);
assert!(!lowered.wave_ops);
let shader = HostShader {
stage: HwStage::Cs,
Expand Down Expand Up @@ -269,3 +276,75 @@ fn lanes_sample_their_own_texture_in_a_waterfall_narrowed_by_v_cmpx() {
}
}
}

#[test]
fn lanes_sample_a_descriptor_moved_under_the_exec_their_waterfall_starts_with() {
let words = crate::resource::tests::waterfall_narrowed_wave32();
// Every lane's descriptor is the first texture's, so the waterfall is
// one uniform access.
let shader = build_with(&words, false, 0);
for (label, red) in sampled(&shader) {
// Lanes below 16 moved the first texture's descriptor and sample
// it; the others keep v12's 0.
let want: Vec<f32> = (0..32).map(|l| if l < 16 { 1.0 } else { 0.0 }).collect();
assert_eq!(red, want, "{label}");
}
}

/// A buffer descriptor (table entry 0, a structured buffer of dwords) loaded
/// only for its record count, dword 2, which every lane stores at lane * 4
/// of the buffer at dword 16: no instruction accesses the buffer.
const RECORDS_ONLY_WAVE32: &[u32] = &[
0xBE84_0300, // s_mov_b32 s4, s0
0xBE85_0380, // s_mov_b32 s5, 0
0xF408_0602,
0xFA00_0000, // s_load_dwordx4 s[24:27], s[4:5], null
0xF408_0A02,
0xFA00_0040, // s_load_dwordx4 s[40:43], s[4:5], 0x40
0x3420_0082, // v_lshlrev_b32 v16, 2, v0
0xBF8C_C07F, // s_waitcnt lgkmcnt(0)
0x7E02_021A, // v_mov_b32 v1, s26
0xE070_1000,
0x800A_0110, // buffer_store_dword v1, v16, s[40:43], 0 offen
0xBF81_0000, // s_endpgm
];

#[test]
fn a_buffer_descriptor_read_only_for_its_record_count_gives_the_views_elements() {
let code: Vec<u8> = RECORDS_ONLY_WAVE32
.iter()
.flat_map(|w| w.to_le_bytes())
.collect();
let all = crate::gfx10::decode(&code)
.expect("decodes")
.iter()
.map(|i| i.text(false))
.collect::<Vec<_>>()
.join("\n");
for want in [
"s_load_dwordx4 s[24:27], s[4:5], null",
"s_load_dwordx4 s[40:43], s[4:5], 0x000040",
"v_mov_b32 v1, s26",
"buffer_store_dword v1, v16, s[40:43], 0 offen",
] {
assert!(all.contains(want), "{want:?} not in:\n{all}");
}
let shader = build_with(RECORDS_ONLY_WAVE32, true, 0);
let first: Vec<u32> = (0..24).collect();
let second = [0u32; 4];
let views = [
View::Buffer {
data: &first,
stride: Stride::Structured(4),
},
View::Buffer {
data: &second,
stride: Stride::Structured(4),
},
View::RawUav(32),
];
for (label, got) in run(&shader, &views) {
// The console's record count of a structured buffer: its elements.
assert_eq!(got, vec![24; 32], "{label}");
}
}
Loading