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
4 changes: 4 additions & 0 deletions tools/shader-compiler/src/hlsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ mod ray_resolve;
mod fspx_wave;
mod singleton_wave;
mod wave_sum;
mod portable_wave64;
mod lockstep;
mod wave_loops;
mod raw_index;
Expand Down Expand Up @@ -312,6 +313,9 @@ struct Emitter<'a> {
/// Structured loop latches with a verified guest trip bound.
wave_bounded_latches: BTreeSet<usize>,
portable_wave64: bool,
portable_w64: Option<portable_wave64::Plan>,
hoisted_w64: Option<usize>,
in_active_guard: bool,
msel: HashMap<usize, MSel>,
/// Buffer ops (MUBUF, s_buffer_load) whose descriptor register holds one of several
/// descriptors: (register key, candidates as (descriptor id, resource)).
Expand Down
45 changes: 33 additions & 12 deletions tools/shader-compiler/src/hlsl/driver/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,20 +344,39 @@ pub(in crate::hlsl) fn translate_outputs_once(ex: &ShaderExtract, feeds_ps: Opti
return Err(format!("wave operation requires complete {width}-lane compute waves"));
}
}
em.portable_wave64 = (straight_exchange || (required_wave_size == Some(64)
&& compute_group(ex) == [64, 1, 1]))
&& em.lockstep.is_none()
&& ex.stage == Stage::Compute
&& !wave_cull && !page_waves && !allocation_wave64 && !requires_ballot
&& !em.compute_swizzle && !em.indexed_lane && !em.compute_heap
&& em.packed.is_none() && !uses_quad_add && !uses_bpermute && !probe_trace
&& scan_width.is_none() && !particle_sort && !hair_cluster_export && !hair_cluster
let portable_eligible = ex.stage == Stage::Compute
&& !wave_cull && !page_waves && !lone_lane::on() && !resolve_wave32 && !light_grid
&& !uses_quad_add && !uses_bpermute && !probe_trace
&& !particle_sort && !hair_cluster_export && !hair_cluster
&& !instance_cull && !hzb_cull && view_cull.is_none() && !post_cull
&& !hair_visibility_indirect && !hair_visibility_single && hair_compact.is_none()
&& !subsurface_setup && !material_classify && !classify_tiles && !reflection && !group_extrema
&& (straight_exchange || wave_sum::portable_single_wave(&em.insns));
if straight_exchange && !em.portable_wave64 {
return Err("straight DPP8 exchange conflicts with a specialized lowering".into());
&& !em.single_wave32 && !em.one_word_vector_mask() && required_wave_size != Some(32);
if straight_exchange {
let straight_ok = portable_eligible && !allocation_wave64 && !requires_ballot
&& !em.compute_swizzle && !em.indexed_lane && !em.compute_heap
&& em.packed.is_none() && scan_width.is_none() && em.lockstep.is_none();
if !straight_ok {
return Err("straight DPP8 exchange conflicts with a specialized lowering".into());
}
em.portable_wave64 = true;
em.portable_w64 = Some(portable_wave64::Plan::straight_exchange());
} else if portable_eligible {
match portable_wave64::analyze(&em, required_wave_size, scan_width, shared_mask) {
portable_wave64::Analysis::Portable(plan) => {
if em.reflected_lds_words.is_none()
&& wave_cull::lds_words(&em).is_none()
&& em.insns.iter().any(|i| i.enc == Enc::Ds && i.mnemonic != "ds_swizzle_b32")
{
em.reflected_lds_words =
tuxbox_dxbc::fssm::compute_lds_bytes(&ex.fssm).map(|b| b / 4);
}
em.portable_wave64 = true;
em.portable_w64 = Some(plan);
}
portable_wave64::Analysis::RefuseDivergent(reason) => return Err(reason),
portable_wave64::Analysis::KeepNative | portable_wave64::Analysis::NotNeeded => {}
}
}
if is_ps {
em.find_export_epilogs();
Expand Down Expand Up @@ -466,7 +485,7 @@ pub(in crate::hlsl) fn translate_outputs_once(ex: &ShaderExtract, feeds_ps: Opti
hlsl.insert_str(0, instance_cull_hzb::HIDDEN_DECLARATIONS);
}
if light_grid { hlsl.insert_str(0, light_grid::DECLARATIONS); }
if em.portable_wave64 { hlsl.insert_str(0, wave_sum::PORTABLE_DECLARATIONS); }
if let Some(plan) = &em.portable_w64 { hlsl.insert_str(0, &plan.declarations()); }
if shared_mask {
hlsl.insert_str(0, &page_waves::ballot_declarations(compute_group(ex).into_iter().product()));
}
Expand Down Expand Up @@ -504,6 +523,8 @@ pub(in crate::hlsl) fn translate_outputs_once(ex: &ShaderExtract, feeds_ps: Opti
"cs_6_5"
} else if is_ps && !em.uav.is_empty() {
"ps_5_1"
} else if em.portable_wave64 && em.portable_w64.as_ref().is_some_and(|p| p.uses_ballot) {
"cs_6_0"
} else {
profile
};
Expand Down
23 changes: 22 additions & 1 deletion tools/shader-compiler/src/hlsl/emitter/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ impl<'a> Emitter<'a> {
self.line(&code);
}
}
if self.portable_wave64 && !active.is_empty() {
if let Some(code) = portable_wave64::hoist_before(self, i, ins)? {
self.end_guard(&mut open);
cur.clear();
self.line(&code);
self.hoisted_w64 = Some(i);
}
}
let mesh_condition = self.hoist_wave_exchanges && self.ex.stage == Stage::Mesh
&& reads_scc(ins) && !self.in_loop(i) && scc_from_mask(&insns, i);
if mesh_condition {
Expand All @@ -180,7 +188,7 @@ impl<'a> Emitter<'a> {
{
active.clear();
}
if self.compute_swizzle && exchanges(ins) && !active.is_empty() {
if self.compute_swizzle && exchanges(ins) && !active.is_empty() && self.hoisted_w64 != Some(i) {
self.end_guard(&mut open);
cur.clear();
self.emit_compute_swizzle(i, ins, &active)?;
Expand Down Expand Up @@ -312,8 +320,21 @@ impl<'a> Emitter<'a> {
// Publish every coarse luminance texel before this group takes its ticket.
self.line("AllMemoryBarrierWithGroupSync();");
}
self.in_active_guard = !cur.is_empty();
self.emit_insn(i, ins, is_ps, target.get(&i).copied())
.map_err(|e| format!("{e} [{} @ {:#x}]", ins.mnemonic, ins.offset))?;
self.hoisted_w64 = None;
self.in_active_guard = false;
if open {
if let Some((barriers, applies)) = portable_wave64::hoist_after(self, i, ins)? {
self.end_guard(&mut open);
self.line(&barriers);
let c: Vec<String> = cur.iter().map(|t| format!("j{t}")).collect();
self.line(&format!("if (!({})) {{", c.join(" || ")));
open = true;
self.line(&applies);
}
}
if open && target.get(&i).is_some_and(|t| cur.contains(t)) {
// Another branch to an enclosing target just updated its flag.
// The following instruction must reevaluate the guard.
Expand Down
4 changes: 4 additions & 0 deletions tools/shader-compiler/src/hlsl/emitter/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use super::*;
impl<'a> Emitter<'a> {
pub(in crate::hlsl) fn emit_insn(&mut self, idx: usize, ins: &Insn, is_ps: bool, br: Option<usize>) -> Result<(), String> {
self.emit_insn_body(idx, ins, is_ps, br)?;
if portable_wave64::emit_post_votes(self, idx, ins)? {
return Ok(());
}
if let Some(vote) = self.lockstep.as_ref().and_then(|votes| votes.get(&idx)).cloned() {
let mask = r(&vote.mask)?;
let not = if vote.none { "!" } else { "" };
Expand Down Expand Up @@ -49,6 +52,7 @@ impl<'a> Emitter<'a> {
if wave_cull::emit(self, idx, ins, br)? { return Ok(()); }
}
if self.page_waves && page_waves::emit(self, idx, ins, br)? { return Ok(()); }
if self.portable_wave64 && portable_wave64::emit(self, idx, ins, br)? { return Ok(()); }
match m {
"tb_light_grid_reserve" => {
self.line(light_grid::RESERVE);
Expand Down
7 changes: 6 additions & 1 deletion tools/shader-compiler/src/hlsl/emitter/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,12 @@ void main(uint3 gid : SV_GroupID, uint3 gtid : SV_GroupThreadID) {{");
writeln!(h, " uint tb_wave_tid = gtid.x + {tx}u * (gtid.y + {ty_}u * gtid.z);").unwrap();
}
let masked_dpp = self.insns.iter().any(|i| i.op == OP_DPP_BINARY && group_extrema::destination_mask(i).is_some());
if self.compute_swizzle || self.indexed_lane || self.single_wave32 || masked_dpp {
if self.compute_swizzle
|| self.indexed_lane
|| self.single_wave32
|| masked_dpp
|| self.portable_w64.as_ref().is_some_and(|p| p.needs_tb_lane)
{
writeln!(h, " uint tb_lane = gtid.x + {tx}u * (gtid.y + {ty_}u * gtid.z);").unwrap();
}
if self.resolve_wave32 {
Expand Down
3 changes: 3 additions & 0 deletions tools/shader-compiler/src/hlsl/emitter/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ impl<'a> Emitter<'a> {
lockstep: None,
wave_bounded_latches: BTreeSet::new(),
portable_wave64: false,
portable_w64: None,
hoisted_w64: None,
in_active_guard: false,
msel: HashMap::new(),
bsel: HashMap::new(),
dsel_loads: HashMap::new(),
Expand Down
26 changes: 20 additions & 6 deletions tools/shader-compiler/src/hlsl/emitter/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ impl<'a> Emitter<'a> {
let mut m = ins.mnemonic.clone();
let s = &ins.src;
let d = ins.dst.first().cloned().ok_or("no dst")?;
if self.required_wave_size == Some(64) && self.packed.as_ref().is_some_and(|p| p.lane_masks.contains(&self.cur_idx)) {
if (self.required_wave_size == Some(64) || self.portable_wave64)
&& (self.packed.as_ref().is_some_and(|p| p.lane_masks.contains(&self.cur_idx))
|| self.portable_w64.as_ref().is_some_and(|p| p.ballot_mask_reads.contains(&self.cur_idx)))
{
self.touch(&hi(&s[2]));
}
if m == "tb_reflection_prefix32" {
Expand Down Expand Up @@ -128,8 +131,12 @@ impl<'a> Emitter<'a> {
format!("({lane} & {}u)", width - 1)
} else { lane };
let value = r(&s[0])?;
let read = if self.portable_wave64 {
format!("tb_reduction_read(gtid.x, {value}, {lane})")
let read = if let Some(plan) = &self.portable_w64 {
if self.hoisted_w64 == Some(self.cur_idx) {
format!("w64_r{}", self.cur_idx)
} else {
format!("tb_reduction_read({}, {value}, {lane})", plan.lane_expr())
}
} else { format!("WaveReadLaneAt({value}, {lane})") };
return self.ws(&d, &read);
}
Expand Down Expand Up @@ -558,11 +565,18 @@ impl<'a> Emitter<'a> {
}
let mask = if matches!(self.exec_snapshots.sites.get(&self.cur_idx), Some(exec_snapshot::Site::LaneMask(..))) {
format!("select_mask{}", self.cur_idx)
} else if self.packed.as_ref().is_some_and(|p| p.lane_masks.contains(&self.cur_idx)) {
let upper = if self.required_wave_size == Some(64) {
} else if self.portable_w64.as_ref().is_some_and(|p| p.ballot_mask_reads.contains(&self.cur_idx))
|| self.packed.as_ref().is_some_and(|p| p.lane_masks.contains(&self.cur_idx))
{
let upper = if self.required_wave_size == Some(64) || self.portable_wave64 {
r(&hi(&s[2]))?
} else { "0u".into() };
format!("(((WaveGetLaneIndex() < 32u) ? {c} : {upper}) >> (WaveGetLaneIndex() & 31u))")
if let Some(plan) = &self.portable_w64 {
let lane = plan.lane_expr();
format!("(((({lane} & 63u) < 32u) ? {c} : {upper}) >> ({lane} & 31u))")
} else {
format!("(((WaveGetLaneIndex() < 32u) ? {c} : {upper}) >> (WaveGetLaneIndex() & 31u))")
}
} else if vop3 { c.clone() } else { "vcc_lo".to_string() };
let e = match m.as_str() {
"v_mov_b32" => a,
Expand Down
11 changes: 8 additions & 3 deletions tools/shader-compiler/src/hlsl/emitter/vector_exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<'a> Emitter<'a> {
self.pixel_sum = m.starts_with("v_add");
return self.wv(&d, &acc);
}
let id = if self.portable_wave64 { "gtid.x" } else { "WaveGetLaneIndex()" };
let id = self.portable_w64.as_ref().map_or("WaveGetLaneIndex()", |p| p.lane_expr());
let control = ins.raw[1] >> 8; // The reduction matcher verified this word.
let lane = match (ins.aux.imm, control) {
(0, _) => format!("{id} ^ 1u"),
Expand All @@ -162,7 +162,11 @@ impl<'a> Emitter<'a> {
_ => return Err("invalid integer sum step".into()),
};
let partner = if self.portable_wave64 {
format!("tb_reduction_read(gtid.x, {acc}, {lane})")
if self.hoisted_w64 == Some(self.cur_idx) {
format!("w64_r{}", self.cur_idx)
} else {
format!("tb_reduction_read({id}, {acc}, {lane})")
}
} else { format!("WaveReadLaneAt({acc}, {lane})") };
let combined = match m.as_str() {
"v_add_nc_u32" => format!("{partner} + {acc}"),
Expand Down Expand Up @@ -190,7 +194,8 @@ impl<'a> Emitter<'a> {
let selector = ins.aux.imm as u32;
let t = self.tmp;
self.tmp += 1;
self.line(&format!("uint t{t} = tb_reduction_read(gtid.x, {a}, (gtid.x & ~7u) | (({selector:#x}u >> (3u * (gtid.x & 7u))) & 7u));"));
let id = self.portable_w64.as_ref().map_or("gtid.x", |p| p.lane_expr());
self.line(&format!("uint t{t} = tb_reduction_read({id}, {a}, ({id} & ~7u) | (({selector:#x}u >> (3u * ({id} & 7u))) & 7u));"));
let value = match m.as_str() {
"v_add_nc_u32" => format!("t{t} + {b}"),
"v_and_b32" => format!("t{t} & {b}"),
Expand Down
21 changes: 14 additions & 7 deletions tools/shader-compiler/src/hlsl/emitter/vector_wide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,23 @@ impl<'a> Emitter<'a> {
return self.wv(&dh, &format!("t{t}h"));
}

/// Vector compares into a lane mask.
pub(in crate::hlsl) fn emit_compare(&mut self, m: String, s: &[Opnd], d: Opnd, ins: &Insn) -> Result<(), String> {
/// Boolean condition expression for a `v_cmp_*` instruction.
pub(in crate::hlsl) fn compare_cond(&mut self, ins: &Insn) -> Result<String, String> {
let m = ins.mnemonic.as_str();
let s = &ins.src;
let mask = ins.aux.mask;
let c = if m.ends_with("_i64") || m.ends_with("_u64") {
self.compare_int64(ins, mask)?
if m.ends_with("_i64") || m.ends_with("_u64") {
self.compare_int64(ins, mask)
} else if m.ends_with("_f64") || m.ends_with("_f32") || m.ends_with("_f16") {
self.compare_float(ins, mask)?
self.compare_float(ins, mask)
} else {
self.int_compare(&m, &s[0], &s[1])?
};
self.int_compare(m, &s[0], &s[1])
}
}

/// Vector compares into a lane mask.
pub(in crate::hlsl) fn emit_compare(&mut self, m: String, _s: &[Opnd], d: Opnd, ins: &Insn) -> Result<(), String> {
let c = self.compare_cond(ins)?;
if self.resolve_wave32 {
let vote = format!("WaveActiveBallot((exec_lo & tb_lane_bit) != 0u && {c}).x");
return self.ws(if m.starts_with("v_cmpx_") { &Opnd::ExecLo } else { &d }, &vote);
Expand Down
Loading
Loading