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
28 changes: 24 additions & 4 deletions tools/shader-compiler/src/hlsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ mod ray_resolve;
mod fspx_wave;
mod singleton_wave;
mod wave_sum;
mod wave_loops;
mod raw_index;
mod reflection_compact;
pub(crate) mod radiance_probe;
Expand Down Expand Up @@ -554,6 +555,8 @@ struct Emitter<'a> {
required_wave_size: Option<u32>,
// Proven whole-wave EXECZ guards of verified masked reductions.
reduction_execz_guards: BTreeSet<usize>,
/// Structured loop latches with a verified guest trip bound.
wave_bounded_latches: BTreeSet<usize>,
portable_wave64: bool,
msel: HashMap<usize, MSel>,
/// Buffer ops (MUBUF, s_buffer_load) whose descriptor register holds one of several
Expand Down Expand Up @@ -1175,6 +1178,10 @@ fn reads_sgpr(ins: &Insn, n: u8) -> bool {
})
}

fn branch_pc(ins: &Insn) -> i64 {
ins.offset as i64 + 4 + ins.aux.imm * 4
}

fn branch_targets(insns: &[Insn]) -> BTreeSet<i64> {
insns.iter()
.filter(|b| b.enc == Enc::Sopp && Emitter::branch_cond(b.op).is_some())
Expand Down Expand Up @@ -1607,7 +1614,13 @@ fn translate_outputs_once(ex: &ShaderExtract, feeds_ps: Option<&InputInterp>, ou
em.reflected_lds_words = Some(bytes/4);
}
if let Some(width) = required_wave_size {
em.reduction_execz_guards = wave_sum::check_control_flow(&em.insns, width)?;
match wave_sum::check_control_flow(&em.insns, width) {
Ok(guards) => em.reduction_execz_guards = guards,
Err(reason) if em.resolve_wave32 && !requires_ballot && scan_width.is_none() => {
em.wave_bounded_latches = wave_loops::bounded_latches(&em).ok_or(reason)?;
}
Err(reason) => return Err(reason),
}
let [x, y, z] = compute_group(ex);
if ex.stage != Stage::Compute || !x.checked_mul(y).and_then(|n| n.checked_mul(z)).is_some_and(|n| n % width == 0) {
return Err(format!("wave operation requires complete {width}-lane compute waves"));
Expand Down Expand Up @@ -2127,6 +2140,7 @@ impl<'a> Emitter<'a> {
view_cull: None,
required_wave_size: None,
reduction_execz_guards: BTreeSet::new(),
wave_bounded_latches: BTreeSet::new(),
portable_wave64: false,
msel: HashMap::new(),
bsel: HashMap::new(),
Expand Down Expand Up @@ -4279,7 +4293,7 @@ uint tb_fmac_half(uint a, uint b, uint c) {
continue;
}
if ins.enc == Enc::Sopp && Self::branch_cond(ins.op).is_some() {
let t = ins.offset as i64 + 4 + ins.aux.imm * 4;
let t = branch_pc(ins);
let ti = if t >= 0 && t as usize >= prog_end {
self.insns.len() // at or beyond the end of the program (s_code_end padding): exit
} else {
Expand Down Expand Up @@ -4549,7 +4563,10 @@ uint tb_fmac_half(uint a, uint b, uint c) {
}
let exits: Vec<String> = labels.iter().filter(|&&t| t > i).map(|t| format!("j{t}")).collect();
let exit = if exits.is_empty() { String::new() } else { format!(" && !({})", exits.join(" || ")) };
let guard = if self.wave_cull || self.light_grid || probe_trace || self.packed.as_ref().is_some_and(|p| p.copy_backedges.contains(&i) || p.bounded_backedges.contains(&i)) {
let guard = if self.wave_cull || self.light_grid || probe_trace
|| self.wave_bounded_latches.contains(&i)
|| self.packed.as_ref().is_some_and(|p| p.copy_backedges.contains(&i) || p.bounded_backedges.contains(&i))
{
""
} else {
" && ++tb_guard < 65536u"
Expand Down Expand Up @@ -4620,6 +4637,9 @@ uint tb_fmac_half(uint a, uint b, uint c) {
if !self.reduction_execz_guards.is_empty() {
return Err("wave reduction EXECZ vote in a block-dispatched program".into());
}
if !self.wave_bounded_latches.is_empty() {
return Err("bounded wave reduction in a block-dispatched program".into());
}
self.dispatched = true;
let n = self.insns.len();
let mut off2idx: HashMap<usize, usize> = self.insns.iter().enumerate().map(|(i, x)| (x.offset, i)).collect();
Expand All @@ -4634,7 +4654,7 @@ uint tb_fmac_half(uint a, uint b, uint c) {
continue;
}
if Self::branch_cond(ins.op).is_some() {
let t = ins.offset as i64 + 4 + ins.aux.imm * 4;
let t = branch_pc(ins);
let ti = if t >= 0 && t as usize >= prog_end {
self.insns.len() // at or beyond the end of the program (s_code_end padding): exit
} else {
Expand Down
Loading
Loading