Shaders: reads of uninitialized SGPRs read as zero, as VGPRs do

Compute programs read SGPRs the hardware does not initialize when two
branches make the paths look like they allow a read before a write, even
though the branch that skips the write does not reach the read. That stops
translation with 'a hardware SGPR input is not supported' and drops the
program's dispatches.

ComputeIo::initialized_sgprs() counts the user SGPRs, the enabled system
ones and the scratch wave offset. A read of a general SGPR (0..=105) at or
above that count holds whatever the previous wave left, so no program can
depend on its value: read it as zero, as unset VGPRs already are. Reserved
registers (VCC, trap temporaries, M0, EXEC) keep their current handling.
This commit is contained in:
local committed 2026-11-23 22:16:27 -05:00
1 parent d2a4bae1f4
commit f4438b38f4
3 files changed
+47 -1

No files matched your search

+9 -1
View File
@@ -908,8 +908,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"),
+7
View File
@@ -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
+31
View File
@@ -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)]));