Skip to content
Open
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
13 changes: 4 additions & 9 deletions apps/victory/src/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,16 +883,11 @@ fn tally_resources(p: &NativeProgram, r: &resource::Resources, t: &mut Tally) {
unreachable!("leaves are no phis, selects or per-lane choices")
}
};
// Heaps hold dynamic resources, which no used entry lists; code
// tables are no root parameter.
if let Some(a) = a
&& a.param != resource::CODE
&& !matches!(
p.layout[a.param].root,
RootType::ResourceHeap | RootType::SamplerHeap
)
// Heaps have no used entry; code tables and raw memory are not root parameters.
if let Some(param) = a.and_then(|a| p.layout.get(a.param))
&& !matches!(param.root, RootType::ResourceHeap | RootType::SamplerHeap)
{
traced.insert(p.layout[a.param].user_data);
traced.insert(param.user_data);
}
}
// Root constants are listed as a CBV, read straight from the SGPRs.
Expand Down
47 changes: 47 additions & 0 deletions crates/victory-shader/src/emit/bitcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,53 @@ impl Module {
}
}

#[cfg(test)]
pub(super) fn call_args<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a [V]> {
self.insts.iter().filter_map(move |inst| match &inst.op {
Op::Call(f, args) if self.funcs[*f as usize].name == name => Some(args.as_slice()),
_ => None,
})
}

/// Evaluate the integer address arithmetic of a synthetic test shader with known user data.
#[cfg(test)]
pub(super) fn address_value(&self, v: V, user: &[u32]) -> Option<u32> {
if let Some(n) = self.int_value(v) {
return Some(n as u32);
}
let V::Inst(i) = v else { return None };
let eval = |v| self.address_value(v, user);
match &self.insts[i as usize].op {
Op::Bin(op, a, b) => {
let (a, b) = (eval(*a)?, eval(*b)?);
Some(match op {
Bin::Add => a.wrapping_add(b),
Bin::Shl => a.wrapping_shl(b),
Bin::LShr => a.wrapping_shr(b),
Bin::And => a & b,
_ => return None,
})
}
Op::Extract(V::Inst(i), dword) => {
let Op::Call(f, args) = &self.insts[*i as usize].op else {
return None;
};
if self.funcs[*f as usize].name != "dx.op.cbufferLoadLegacy.i32" {
return None;
}
let row = self.int_value(args[2])? as usize;
user.get(4 * row + *dword as usize).copied()
}
Op::Call(f, args)
if self.funcs[*f as usize].name == "dx.op.binary.i32"
&& self.int_value(args[0]) == Some(40) =>
{
Some(eval(args[1])?.min(eval(args[2])?))
}
_ => None,
}
}

/// Gives constant `old` the content of `new` (both stay valid).
pub fn replace_const(&mut self, old: V, new: V) {
if let (V::Const(o), V::Const(n)) = (old, new) {
Expand Down
106 changes: 101 additions & 5 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2552,7 +2552,7 @@ impl<'a> Lower<'a> {
};
let c = self.cur;
let mut off = self.s.m.i32(a.offset);
if let Some(d) = a.dynamic {
for d in [a.dynamic, a.dynamic2].into_iter().flatten() {
let d = self.i32_of(d)?;
off = self.s.m.bin(c, Bin::Add, off, d);
}
Expand Down Expand Up @@ -3180,6 +3180,10 @@ impl<'a> Lower<'a> {
self.tail[b] = Some(self.cur);
}
self.fill_phis()?;
#[cfg(test)]
if let Some(inspect) = tests::INSPECT_OFFSETS.take() {
inspect(&self.s.m);
}
let user_data = self.user_reads.values().copied().collect();
self.s.samplers = self.samplers.len() as u32;
let root_cbvs = self.s.root_cbvs.iter().map(|r| r.0).collect();
Expand Down Expand Up @@ -8164,6 +8168,9 @@ impl<'a> Lower<'a> {

/// `s_load_dword*` from the program's constant table.
fn code_smem(&mut self, inst: &Inst, a: Address) -> Result<(), Why> {
if a.dynamic2.is_some() {
return Err("an address with two computed terms");
}
let Fields::Smem {
data: Some(Operand::Scalar { code, .. }),
..
Expand Down Expand Up @@ -8564,10 +8571,14 @@ impl<'a> Lower<'a> {
fn built_offset(&mut self, src: &Source) -> Result<(u32, Option<V>), Why> {
match src {
Source::Built { address, .. } => {
let mut x = match address.dynamic {
Some(v) => Some(self.i32_of(v)?),
None => None,
};
let mut x = None;
for term in [address.dynamic, address.dynamic2].into_iter().flatten() {
let term = self.i32_of(term)?;
x = Some(match x {
Some(first) => self.s.m.bin(self.cur, Bin::Add, first, term),
None => term,
});
}
if let Some(bias) = self.root_bias(address.param) {
x = Some(match x {
Some(d) => self.s.m.bin(self.cur, Bin::Add, d, bias),
Expand Down Expand Up @@ -14378,6 +14389,11 @@ mod tests {

const S_ENDPGM: u32 = 0xBF81_0000;

thread_local! {
pub(super) static INSPECT_OFFSETS: std::cell::Cell<Option<fn(&super::super::bitcode::Module)>> =
const { std::cell::Cell::new(None) };
}

/// A 128-thread group whose divergent region both reads and writes LDS. With that branch
/// taken by both halves of a wave, the program lowers to wave 32 and the LDS sites meet
/// per wave. Without the branch marked, no meet is emitted in the divergent block.
Expand Down Expand Up @@ -14711,6 +14727,86 @@ mod tests {
}
}

#[test]
fn scalar_table_load_uses_the_base_index_and_computed_soffset() {
// User data: table byte base 160, base index 2, soffset index 3.
INSPECT_OFFSETS.set(Some(|m| {
let slots: Vec<_> = m
.call_args("dx.op.createHandleFromHeap")
.map(|args| m.address_value(args[1], &[160, 2, 3]))
.collect();
assert_eq!(slots, [Some((160 + (2 << 5) + (3 << 5) + 32) >> 5)]);
}));
lower_with(
&resource::tests::TWO_TERM_TABLE_LOAD,
vec![
root(RootType::Table, 0, 0),
RootParam {
sgprs: 2,
..root(RootType::Constants, 1, 1)
},
],
vec![UsedResource {
kind: ResourceType::Srv,
root: RootType::Table,
table_offset_dwords: 0,
lower_bound: 0,
stride: 0,
dwords: 4,
user_data: 0,
slot: 0,
}],
)
.unwrap();
assert!(INSPECT_OFFSETS.get().is_none());
}

#[test]
fn global_load_adds_the_root_view_index_and_computed_vgpr_offset() {
let words = [
0x8F06_8202, // s_lshl_b32 s6, s2, 2
0x8004_0600, // s_add_u32 s4, s0, s6
0x8205_8001, // s_addc_u32 s5, s1, 0
0x7E02_0203, // v_mov_b32 v1, s3
0x3402_0282, // v_lshlrev_b32 v1, 2, v1
0xDC30_800C,
0x0204_0001, // global_load_dword v2, v1, s[4:5] offset:12
0xD834_0000,
0x0000_0200, // ds_write_b32 v0, v2
S_ENDPGM,
];
INSPECT_OFFSETS.set(Some(|m| {
let bytes: Vec<_> = m
.call_args("dx.op.rawBufferLoad.f32")
.map(|args| m.address_value(args[2], &[0, 0, 7, 11]))
.collect();
assert_eq!(bytes, [Some((7 << 2) + (11 << 2) + 12)]);
}));
let lowered = lower_with(
&words,
vec![
root(RootType::Descriptor, 0, 0),
RootParam {
sgprs: 2,
..root(RootType::Constants, 2, 1)
},
],
vec![UsedResource {
kind: ResourceType::Srv,
root: RootType::Descriptor,
table_offset_dwords: 0,
lower_bound: 0,
stride: 0,
dwords: 4,
user_data: 0,
slot: 0,
}],
)
.unwrap();
assert_eq!(lowered.root_srvs, [0]);
assert!(INSPECT_OFFSETS.get().is_none());
}

/// Lowers a one-thread compute program that copies four root constants
/// (s0..s3) into v1..v4, runs `body`, and stores v0 to a raw buffer from a
/// descriptor table, so the vector instructions in `body` have an effect.
Expand Down
Loading
Loading