Only changed dwords go back into title memory; cheap safe copies; title's own arguments; more shader forms

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 20:28:42 -06:00
1 parent d9a31188be
commit 7fa012814f
16 files changed
+973 -29

No files matched your search

+129 -1
View File
@@ -1118,10 +1118,17 @@ impl<'a> Tracer<'a> {
let Operand::Scalar { code, dwords } = *op else {
return Err("a descriptor not in SGPRs");
};
let words = (0..dwords)
let mut words = (0..dwords)
.map(|k| inst.read(Reg::S(code + k)).map(|v| self.origin(v)))
.collect::<Option<Vec<_>>>()
.ok_or("a descriptor not in SGPRs")?;
// A byte access through a buffer descriptor made raw reads what the
// descriptor's view does with a raw host view (VIctory upstream 0b35b7f).
if !inst.name().contains("format")
&& let Some(raw) = self.made_raw(&words)
{
words = raw.to_vec();
}
// Only the lanes that reach the access read the descriptor.
let mut known = Vec::from_iter(self.lanes.map(|l| (l.reach[at], true)));
self.classify(&words, &mut Vec::new(), &mut known)
@@ -1462,6 +1469,46 @@ impl<'a> Tracer<'a> {
}
}
/// The words of a buffer descriptor that `words` makes raw, as the
/// FidelityFX upscaler's compiler does before byte accesses: the stride
/// (dword 1 bits 16-29) cleared, the record count times max(stride, 1),
/// out-of-bounds checking by bytes (dword 3 bits 28-29 set). The bounds
/// are then the view's size in bytes either way.
fn made_raw(&self, words: &[Term]) -> Option<[Term; 4]> {
let [w0, Term::Value(w1), Term::Value(w2), Term::Value(w3)] = *words else {
return None;
};
// `x op c` with `c` either side.
let with = |v: Value, name: &str, c: u32| match self.sop2(v, &[name])?.1 {
[x, Term::Const(k)] | [Term::Const(k), x] if k == c => Some(x),
_ => None,
};
let d1 = with(w1, "s_and_b32", 0xC000_FFFF)?;
let Term::Value(m3) = with(w3, "s_or_b32", 0x3000_0000)? else {
return None;
};
let d3 = with(m3, "s_and_b32", 0xCFFF_FFFF)?;
let [a, b] = self.sop2(w2, &["s_mul_i32"])?.1;
let max = |t: Term| match t {
Term::Value(s) => with(s, "s_max_u32", 1),
Term::Const(_) => None,
};
let (d2, field) = match (max(a), max(b)) {
(_, Some(f)) => (a, f),
(Some(f), None) => (b, f),
(None, None) => return None,
};
let Term::Value(field) = field else {
return None;
};
// s_bfe_u32: offset 16, width 14.
let (_, [x, Term::Const(0x000E_0010)]) = self.sop2(field, &["s_bfe_u32"])? else {
return None;
};
(x == d1).then_some([w0, d1, d2, d3])
}
/// For a `v_readfirstlane_b32` result: the VGPR value it reads.
fn first_lane_source(&self, v: Value) -> Option<Value> {
let inst = self.def(v)?;
@@ -2331,6 +2378,12 @@ fn copy_source(inst: &Inst, k: usize) -> Option<Moved> {
if inst.name() == "s_movk_i32" {
return Some(Moved::Const(i32::from((*imm)? as i16) as u32));
}
// A mask of S0 bits from bit S1, as the upscaler's compiler makes an
// immediate sampler's word.
if let ("s_bfm_b32", 0, [Operand::Const(w), Operand::Const(at)]) = (inst.name(), k, &src[..]) {
let (w, at) = (inline(*w)? & 31, inline(*at)? & 31);
return Some(Moved::Const(((1u32 << w) - 1) << at));
}
let src = src.first()?;
match (inst.name(), *src) {
("s_mov_b32" | "s_mov_b64", Operand::Scalar { code, .. }) if code != NULL => {
@@ -2343,6 +2396,11 @@ fn copy_source(inst: &Inst, k: usize) -> Option<Moved> {
// that has no inline form; fold more if descriptors need it.
("s_brev_b32", Operand::Const(c)) => inline(c).map(|x| Moved::Const(x.reverse_bits())),
("s_not_b32", Operand::Const(c)) => inline(c).map(|x| Moved::Const(!x)),
// shortcut: a literal below 2^31, whose high dword is 0 whether the
// hardware extends it with zeros or with its sign.
("s_mov_b64", Operand::Literal(x)) if x >> 31 == 0 => {
Some(Moved::Const(if k == 0 { x } else { 0 }))
}
// 64-bit inline integers are sign-extended.
("s_mov_b64", Operand::Const(c @ 128..=208)) => {
let low = inline(c)?;
@@ -2763,6 +2821,76 @@ pub(crate) mod tests {
assert_eq!(r.scalar, [((0, 2), at(0, 0)), ((0, 3), at(0, 0x10))]);
}
/// The FidelityFX upscaler's byte accesses make a loaded buffer
/// descriptor raw first (stride cleared, records times stride, bounds
/// in bytes): the access reads the loaded descriptor. Any other change
/// still fails.
#[test]
fn a_descriptor_made_raw_is_the_loaded_one() {
let words = |mask: u32| -> Vec<u8> {
[
0xBE88_0304, // s_mov_b32 s8, s4
0xBE89_0380, // s_mov_b32 s9, 0
0xF408_0204,
0xFA00_0160, // s_load_dwordx4 s[8:11], s[8:9], 0x160
0xBF8C_C07F, // s_waitcnt lgkmcnt(0)
0x938C_FF09,
0x000E_0010, // s_bfe_u32 s12, s9, 0x000e0010
0x848C_810C, // s_max_u32 s12, s12, 1
0x8709_FF09,
mask, // s_and_b32 s9, s9, mask
0x870B_FF0B,
0xCFFF_FFFF, // s_and_b32 s11, s11, 0xcfffffff
0x930A_0C0A, // s_mul_i32 s10, s10, s12
0x880B_FF0B,
0x3000_0000, // s_or_b32 s11, s11, 0x30000000
0xE078_1000,
0x8002_0007, // buffer_store_dwordx4 v[0:3], v7, s[8:11], 0 offen
S_ENDPGM,
]
.iter()
.flat_map(|w: &u32| w.to_le_bytes())
.collect()
};
let run = |mask| {
let p = crate::ir::build(&words(mask), false, 256).unwrap();
track(&p, &[param(Table, 4, 1)], HwStage::Cs, lanes(&p).as_ref())
};
assert!(run(0xC000_FFFF).is_ok());
assert_eq!(
run(0xC000_FFFE).unwrap_err(),
ShaderError::Unsupported {
offset: 0x3c,
what: "a descriptor changed after loading"
}
);
}
/// Immediate sampler words the upscaler's compiler makes with a bit
/// mask and a 64-bit move of a literal.
#[test]
fn sampler_words_from_bit_masks_and_wide_literals_are_constants() {
let code: Vec<u8> = [
0xB010_3092, // s_movk_i32 s16, 0x3092
0x9211_8C8C, // s_bfm_b32 s17, 12, 12
0xBE92_04FF,
0x1850_0000, // s_mov_b64 s[18:19], 0x18500000
S_ENDPGM,
]
.iter()
.flat_map(|w: &u32| w.to_le_bytes())
.collect();
let p = crate::ir::build(&code, false, 256).unwrap();
let layout = [param(Table, 4, 1)];
let t = Tracer::new(&p, &layout, HwStage::Cs, None).unwrap();
let block = &p.blocks[0];
let value = |n: usize, k: usize| t.origin(block.insts[n].writes[k].1);
assert_eq!(value(1, 0), Term::Const(0x00FF_F000));
assert_eq!(value(2, 0), Term::Const(0x1850_0000));
assert_eq!(value(2, 1), Term::Const(0));
}
#[test]
fn a_selection_that_mixes_two_descriptors_fails() {
// The high half picked from the first load whatever SCC is: when