Merge pull request #432 from Proudnoob4/fix/uninitialized-sgprs
Shaders: reads of uninitialized SGPRs read as zero, as VGPRs do
This commit is contained in:
3 files changed
+47
-1
No files matched your search
@@ -921,8 +921,16 @@ impl<'a> Lower<'a> {
|
||||
for &(reg, v) in &self.x.p.inputs {
|
||||
let val = match reg {
|
||||
Reg::S(_) if self.params.contains_key(&v) => continue, // loaded on first use
|
||||
Reg::S(_) => match io_inputs.get(&v) {
|
||||
Reg::S(n) => match io_inputs.get(&v) {
|
||||
Some(&input) => self.input(input),
|
||||
// Above the SGPRs the hardware initializes, a register
|
||||
// holds what the last wave left, so no program depends
|
||||
// on it: a read the paths only seem to allow (a branch
|
||||
// past the write, taken only with one past the read)
|
||||
// reads zero, as VGPRs do.
|
||||
None if n <= 105 && self.x.io.initialized_sgprs.is_some_and(|k| n >= k) => {
|
||||
Val::I32(self.s.m.i32(0))
|
||||
}
|
||||
None => Val::Missing("a hardware SGPR input"),
|
||||
},
|
||||
Reg::Scc => Val::Missing("SCC on entry"),
|
||||
|
||||
@@ -182,6 +182,9 @@ pub struct Io {
|
||||
pub inputs: Vec<(Value, Input)>,
|
||||
pub interps: Vec<(Site, Interp)>,
|
||||
pub exports: Vec<Export>,
|
||||
/// Compute: how many SGPRs the hardware initializes
|
||||
/// ([`ComputeIo::initialized_sgprs`](crate::native::ComputeIo::initialized_sgprs)).
|
||||
pub initialized_sgprs: Option<u8>,
|
||||
}
|
||||
|
||||
/// Raises the I/O of `p`, built from `native`'s patched code. Fails for
|
||||
@@ -287,6 +290,10 @@ pub fn raise(p: &Program, native: &NativeProgram) -> Result<Io, ShaderError> {
|
||||
|
||||
let mut io = Io {
|
||||
inputs: inputs.clone(),
|
||||
initialized_sgprs: match &native.io {
|
||||
StageIo::Cs(cs) => Some(cs.initialized_sgprs()),
|
||||
_ => None,
|
||||
},
|
||||
..Io::default()
|
||||
};
|
||||
let defs: std::collections::HashMap<Value, Site> = p
|
||||
|
||||
@@ -319,6 +319,15 @@ impl ComputeIo {
|
||||
(ids, take(10))
|
||||
}
|
||||
|
||||
/// SGPRs the hardware initializes: the user SGPRs, the enabled system
|
||||
/// ones, then the scratch wave offset when scratch is enabled (bit 0).
|
||||
/// The ones above hold whatever the previous wave left.
|
||||
pub fn initialized_sgprs(&self) -> u8 {
|
||||
let (ids, info) = self.system_sgprs();
|
||||
let end = ids.into_iter().chain([info]).flatten().map(|s| s + 1).max();
|
||||
end.unwrap_or(0).max(self.user_sgprs()) + (self.rsrc2 & 1) as u8
|
||||
}
|
||||
|
||||
/// Thread id components in v0, v1, v2 (1 to 3).
|
||||
pub fn thread_id_vgprs(&self) -> u8 {
|
||||
(self.rsrc2 >> 11 & 3) as u8 + 1
|
||||
@@ -948,6 +957,28 @@ mod tests {
|
||||
assert_eq!((cs.thread_id_vgprs(), cs.lds_bytes()), (3, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn initialized_sgprs_covers_user_system_and_scratch() {
|
||||
// 5 user SGPRs, group ids x and y (s5, s6), no scratch: s0..=s6.
|
||||
let cs = ComputeIo {
|
||||
rsrc2: 0x1098A,
|
||||
threads: [16, 16, 1],
|
||||
};
|
||||
assert_eq!(cs.initialized_sgprs(), 7);
|
||||
// All three group ids after 6 user SGPRs, plus the info SGPR: s0..=s9.
|
||||
let cs = ComputeIo {
|
||||
rsrc2: 0x78C,
|
||||
threads: [8, 8, 1],
|
||||
};
|
||||
assert_eq!(cs.initialized_sgprs(), 10);
|
||||
// The same with scratch enabled adds the wave offset: s0..=s10.
|
||||
let cs = ComputeIo {
|
||||
rsrc2: 0x78D,
|
||||
threads: [8, 8, 1],
|
||||
};
|
||||
assert_eq!(cs.initialized_sgprs(), 11);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_every_native_blob() {
|
||||
let es = xbch(&fssm(&[(HwStage::Es, &VS_CODE)]));
|
||||
|
||||
Reference in new issue
Block a user