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
68 changes: 67 additions & 1 deletion crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ pub(super) struct Em {
safe_barrier: bool,
/// the T# SGPRs of the program's image stores and atomics (`image_write_srsrcs`)
img_writes: Vec<u64>,
/// split wave64: what lies between the basic block's previous LDS access and here
x64_lds: LdsGap,
/// `XCL_WAVE64_LDS_ORDER=0`: no meetings before LDS accesses (to compare in a title)
x64_lds_order: bool,
/// first instructions of the basic blocks (branch targets, after branches)
block_starts: BTreeSet<u64>,
}

/// Split wave64 (`x64_lds_meet`): since the basic block's previous LDS access
#[derive(Clone, Copy, PartialEq)]
enum LdsGap {
/// that was a write or an atomic, or there was none in the block
Open,
/// that was a read, and no exchange since
Read,
/// the halves met in an exchange since
Met,
}

/// The T# SGPRs (SRSRC) of a program's image stores and atomics, at most 16, sorted. An
Expand Down Expand Up @@ -591,7 +608,9 @@ fn to_f16_rounded_expr(dynamic_rm: bool, e: &str) -> String {
}

impl Em {
pub fn new(opt: Options) -> Self {
/// `block_starts`: the first instructions of the program's basic blocks (branch targets and
/// the instructions after branches): split wave64 elides no LDS meeting across them.
pub fn new(opt: Options, block_starts: BTreeSet<u64>) -> Self {
Em {
opt,
reductions: Default::default(),
Expand Down Expand Up @@ -623,6 +642,9 @@ impl Em {
track_frexp_denorm: false,
safe_barrier: false,
img_writes: Vec::new(),
x64_lds: LdsGap::Open,
x64_lds_order: std::env::var("XCL_WAVE64_LDS_ORDER").map_or(true, |v| v.trim() != "0"),
block_starts,
}
}

Expand Down Expand Up @@ -979,6 +1001,9 @@ impl Em {
ballot = "ballot_px_local";
}
}
if ballot == "xcl_x_ballot" {
self.x64_met();
}
match if ballot == "ballot_px" { quad_bits(a, cond, &m) } else { None } {
Some((reads, q)) => {
if !reads.is_empty() {
Expand Down Expand Up @@ -1248,6 +1273,9 @@ impl Em {
let text = rdna2::format(d);
self.line(&format!("// {:04x}: {text}", d.pc));
self.cur_pc = d.pc;
if self.block_starts.contains(&d.pc) {
self.x64_lds = LdsGap::Open;
}
// a far jump (`rdna2::resolve_far_jumps`) is an unconditional branch
if let Some(t) = d.far_target {
return Flow::Jump(t);
Expand Down Expand Up @@ -1987,6 +2015,7 @@ impl Em {
}
let get: Vec<String> = vals.iter().enumerate().map(|(i, v)| format!("{v} = xcl_x_get({i}u);")).collect();
self.line(&format!("if (xcl_x_meet() && xcl_half != 0u) {{ {} }}", get.join(" ")));
self.x64_met();
}
for (i, v) in vals.iter().enumerate() {
let s = self.sreg(sdata + i as u64);
Expand Down Expand Up @@ -4012,6 +4041,7 @@ impl Em {
let rla = self.rla_uniform_fn();
// (a live lane of EXEC where there is one: `first_lane_live`)
self.line(&format!("uint {t} = {rla}({a}, first_lane_live({el}, {eh}));"));
self.x64_met();
self.write32(&dst, &t);
return Ok(());
}
Expand Down Expand Up @@ -4039,6 +4069,8 @@ impl Em {
(Some(Read::Shadow), Some((v, l))) => self.line(&format!("uint {t} = xcl_sh{v}_{l}; if (!xcl_hw32) {t} = {read};")),
_ => self.line(&format!("uint {t} = {read};")),
}
// split wave64 (no `upper` there): the last arm, an exchange
self.x64_met();
self.write32(&dst, &t);
return Ok(());
}
Expand Down Expand Up @@ -4444,6 +4476,33 @@ impl Em {
}

/// LDS (`groupshared`, compute only) and the DS cross-lane permutes.
/// Split wave64: the console runs an instruction in all 64 lanes before the next, so a lane
/// reads the LDS the other half wrote an instruction earlier with no s_barrier – the halves
/// meet before every LDS access (nothing exchanged: the meeting is the order). Called once
/// the access is known to be translated. Not in geometry mode: ES lanes write their own
/// items and read no other lane's before the barrier where the phases meet.
/// No meeting for a read right after a read of the same basic block, nor when the halves
/// met in an exchange since the block's previous access (`LdsGap`). Still in order: for
/// accesses j < i of the stream, one a write, the halves meet between them (the access
/// after a write and every write meet, or an exchange lies before them; a block's first
/// access meets), and both halves pass the same meetings: the other half's j is done
/// before this half's i. Two reads need no order. `XCL_WAVE64_LDS_ORDER=0`: no meetings.
fn x64_lds_meet(&mut self, write: bool) {
if self.x64() && !matches!(self.opt.mode, Mode::Geometry(_)) && self.x64_lds_order {
if self.x64_lds == LdsGap::Open || (write && self.x64_lds == LdsGap::Read) {
self.line("xcl_x_meet();");
}
self.x64_lds = if write { LdsGap::Open } else { LdsGap::Read };
}
}

/// An exchange (`xcl_x_meet`) the current instruction always runs, in the split form.
fn x64_met(&mut self) {
if self.x64() {
self.x64_lds = LdsGap::Met;
}
}

fn ds(&mut self, d: &Decoded, name: &str) -> Result<(), &'static str> {
let f = |n: &str| rdna2::field_value(d.enc, &d.words, n).unwrap_or(0);
let act = self.act();
Expand Down Expand Up @@ -4475,6 +4534,7 @@ impl Em {
self.line(&format!("uint {t}_l = (({a} + {off16}u) >> 2) & {m}u;"));
let rla = if self.x64() {
self.x64_words = 32;
self.x64_met();
"xcl_x_rla_any"
} else {
"rla"
Expand Down Expand Up @@ -4505,6 +4565,7 @@ impl Em {
}
// ADDTID forms have no ADDR VGPR: byte address = M0[15:0] + offset16 + 4 * lane_id.
if matches!(name, "ds_write_addtid_b32" | "ds_read_addtid_b32") {
self.x64_lds_meet(name == "ds_write_addtid_b32");
self.uses_lds = true;
let m0 = self.sreg(124);
let at = format!("(({m0} & 0xFFFFu) + {off16}u + 4u * lane_id)");
Expand Down Expand Up @@ -4569,6 +4630,7 @@ impl Em {
};
// d16 8 / 16-bit LDS accesses (half of the VGPR)
if let Some((bits, signed, write, half)) = ds_d16(name) {
self.x64_lds_meet(write);
let at = format!("{a} + {off16}u");
let slot = lds(at.clone());
let sh = format!("((({at}) & 3u) * 8u)");
Expand All @@ -4594,6 +4656,7 @@ impl Em {
_ => None,
};
if let Some((bits, signed, write)) = small {
self.x64_lds_meet(write);
let at = format!("{a} + {off16}u");
let slot = lds(at.clone());
let sh = format!("((({at}) & 3u) * 8u)");
Expand All @@ -4615,6 +4678,7 @@ impl Em {
return Ok(());
}
if let Some((write, n, stride, two)) = rw(name) {
self.x64_lds_meet(write);
let addrs: Vec<String> = if two {
vec![format!("{a} + {}u", o0 * stride), format!("{a} + {}u", o1 * stride)]
} else {
Expand Down Expand Up @@ -4647,6 +4711,7 @@ impl Em {
};
// two exchanges (ds_wrxchg2(st64)_rtn_b32): old values to VDST, VDST + 1
if op == "wrxchg2_b32" || op == "wrxchg2st64_b32" {
self.x64_lds_meet(true);
let stride = if op == "wrxchg2_b32" { 4 } else { 256 };
let (s0, s1) = (lds(format!("{a} + {}u", o0 * stride)), lds(format!("{a} + {}u", o1 * stride)));
let (v0, v1) = (self.vreg(f("DATA0")), self.vreg(f("DATA1")));
Expand All @@ -4662,6 +4727,7 @@ impl Em {
let (v, v2) = if op.starts_with("cmpst_") { (d1, d0) } else { (d0, d1) };
let slot = lds(format!("{a} + {off16}u"));
let body = rmw32(&op, &At::Ref(slot), &v, &v2, &format!("{t}_o"), &t).ok_or("DS")?;
self.x64_lds_meet(true);
self.line(&format!("uint {t}_o = 0u; if ({act}) {{ {body} }}"));
if rtn {
let r = self.vreg(f("VDST"));
Expand Down
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/recomp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ pub fn translate(code: &[u8], opt: &Options) -> Result<Translation, String> {
leaders.insert(next);
}
}
let mut em = emit::Em::new(opt.clone());
let mut em = emit::Em::new(opt.clone(), leaders.clone());
em.configure_round_modes(&insts, branches == 0);
em.static_ss = constant_samplers(&insts, &leaders);
// split wave64: masks only read lane by lane skip the halves' exchange (geometry mode
Expand Down
4 changes: 2 additions & 2 deletions crates/xcl/src/gpu/recomp/pso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ fn cached_shader(
}

fn cache_allowed(stage: Stage, present: impl Fn(&str) -> bool) -> bool {
!["XCL_RECOMP_NO_STRUCTURED", "XCL_RECOMP_WHY", "XCL_X64_ALL_MASKS", "XCL_WAVE64_SPIN", "XCL_DXIL_NO_OPT"]
!["XCL_RECOMP_NO_STRUCTURED", "XCL_RECOMP_WHY", "XCL_X64_ALL_MASKS", "XCL_WAVE64_SPIN", "XCL_DXIL_NO_OPT", "XCL_WAVE64_LDS_ORDER"]
.iter()
.any(|name| present(name))
&& (stage != Stage::Pixel
Expand Down Expand Up @@ -1850,7 +1850,7 @@ mod shader_cache_tests {

#[test]
fn shader_cache_bypasses_debug_overrides() {
for name in ["XCL_RECOMP_NO_STRUCTURED", "XCL_RECOMP_WHY", "XCL_X64_ALL_MASKS", "XCL_WAVE64_SPIN", "XCL_DXIL_NO_OPT"] {
for name in ["XCL_RECOMP_NO_STRUCTURED", "XCL_RECOMP_WHY", "XCL_X64_ALL_MASKS", "XCL_WAVE64_SPIN", "XCL_DXIL_NO_OPT", "XCL_WAVE64_LDS_ORDER"] {
assert!(!cache_allowed(Stage::Compute, |v| v == name));
assert!(!cache_allowed(Stage::Vertex, |v| v == name));
assert!(!cache_allowed(Stage::Pixel, |v| v == name));
Expand Down
Loading
Loading