Skip to content
Closed
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
43 changes: 42 additions & 1 deletion crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ impl Em {
/// and multi-warp `s_barrier` usage (for early-exit-safe, bounded `xcl_s_barrier`).
pub fn configure_round_modes(&mut self, insts: &[Decoded], stop_at_endpgm: bool) {
let mut writes_nonzero_dp = false;
let mut reads_dp = false;
let mut has_s_barrier = false;
for d in insts {
let name = d.inst.name.to_ascii_lowercase();
Expand Down Expand Up @@ -544,10 +545,17 @@ impl Em {
writes_nonzero_dp = true;
}
}
"s_getreg_b32" => {
let simm = rdna2::field_value(d.enc, &d.words, "SIMM16").unwrap_or(0) as u16;
if hwreg_mode_round_masks(simm).1 != 0 {
reads_dp = true;
}
}
_ => {}
}
}
self.dynamic_f16_rm = writes_nonzero_dp && self.has_f16_round_op && !self.has_f64_round_op;
// MODE[3:2] is followed at run time where an f16 op rounds by it or the program reads it back
self.dynamic_f16_rm = writes_nonzero_dp && (self.has_f16_round_op || reads_dp) && !self.has_f64_round_op;
self.safe_barrier = has_s_barrier
&& self.compute_or_mesh_hw_waves().is_some_and(|w| w > 1)
&& std::env::var("XCL_SAFE_BARRIER").map_or(true, |v| v.trim() != "0");
Expand Down Expand Up @@ -1047,10 +1055,43 @@ impl Em {
Ok(())
}

/// `s_getreg_b32`: SDST = the hardware register's field, shifted down. Only MODE's round
/// modes are known here – `[1:0]` (f32) is always 0 (anything else is refused) and `[3:2]`
/// is `xcl_fp16_rm` where the program changes it, else 0. The other registers and MODE
/// bits (denormals, clamps, HW_ID, cycle counters …) depend on the console's hardware and
/// set-up: refused, naming the register.
fn getreg(&mut self, d: &Decoded) -> Result<(), &'static str> {
let simm = rdna2::field_value(d.enc, &d.words, "SIMM16").unwrap_or(0) as u16;
let (id, offset, width) = decode_hwreg(simm);
match id {
1 => {}
2 => return Err("HW_REG_STATUS"),
3 => return Err("HW_REG_TRAPSTS"),
5 => return Err("HW_REG_GPR_ALLOC"),
6 => return Err("HW_REG_LDS_ALLOC"),
7 => return Err("HW_REG_IB_STS"),
23 => return Err("HW_REG_HW_ID1"),
24 => return Err("HW_REG_HW_ID2"),
29 => return Err("HW_REG_SHADER_CYCLES"),
_ => return Err("hardware register"),
}
if offset + width > 4 {
return Err("HW_REG_MODE bits other than the round modes");
}
let mode = if self.dynamic_f16_rm { "(xcl_fp16_rm << 2)" } else { "0u" };
let mask = (1u32 << width) - 1;
let dst = Dst::S(Self::field(d, "SDST"));
self.write32(&dst, &format!("(({mode} >> {offset}u) & {mask}u)"));
Ok(())
}

fn salu(&mut self, d: &Decoded, name: &str) -> Result<(), &'static str> {
if matches!(name, "s_setreg_imm32_b32" | "s_setreg_b32") {
return self.setreg(d, name);
}
if name == "s_getreg_b32" {
return self.getreg(d);
}
if matches!(name, "s_movrels_b32" | "s_movrels_b64" | "s_movreld_b32" | "s_movreld_b64" | "s_movrelsd_2_b32") {
return self.smovrel(d, name);
}
Expand Down
56 changes: 56 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6654,6 +6654,62 @@ fn round_mode_classification_and_lowering() {
assert!(t.complete(), "{:?}", t.unsupported_why);
}

/// `s_getreg_b32` reads MODE's round modes (`[1:0]` always 0, `[3:2]` followed through
/// `xcl_fp16_rm` once the program reads them, with or without f16 ops) and refuses the
/// registers and MODE bits XCL does not model, naming them.
#[test]
fn s_getreg_b32_reads_the_round_modes_and_refuses_the_rest() {
let getreg = |id, offset, width, sdst| ("S_GETREG_B32", "ENC_SOPK", vec![("SIMM16", hwreg_imm(id, offset, width)), ("SDST", sdst)], None::<u32>);
let build = |list: Vec<(&'static str, &'static str, Vec<(&'static str, u64)>, Option<u32>)>| {
let v: Vec<(&str, &str, &[(&str, u64)], Option<u32>)> = list.iter().map(|(n, e, f, l)| (*n, *e, f.as_slice(), *l)).collect();
translate(&prog(&v), &Options::default()).unwrap()
};
// MODE never changed: a constant 0
let t = build(vec![getreg(1, 0, 4, 2)]);
assert!(t.complete(), "{:?}", t.unsupported_why);
assert!(t.hlsl.contains("((0u >> 0u) & 15u)") && !t.hlsl.contains("xcl_fp16_rm"), "{}", t.hlsl);
// changed and read back without f16 ops: followed at run time
let t = build(vec![("S_ROUND_MODE", "ENC_SOPP", vec![("SIMM16", 0xC)], None), getreg(1, 2, 2, 3)]);
assert!(t.complete(), "{:?}", t.unsupported_why);
assert!(t.hlsl.contains("xcl_fp16_rm = 3u;") && t.hlsl.contains("(((xcl_fp16_rm << 2) >> 2u) & 3u)"), "{}", t.hlsl);
// refused, naming what was read
for (id, offset, width, why) in [
(23, 0, 32, "s_getreg_b32: HW_REG_HW_ID1"),
(29, 0, 20, "s_getreg_b32: HW_REG_SHADER_CYCLES"),
(2, 0, 1, "s_getreg_b32: HW_REG_STATUS"),
(1, 0, 8, "s_getreg_b32: HW_REG_MODE bits other than the round modes"),
(1, 4, 4, "s_getreg_b32: HW_REG_MODE bits other than the round modes"),
] {
let t = build(vec![getreg(id, offset, width, 2)]);
assert!(!t.complete());
assert!(t.unsupported_why.contains_key(why), "{id}/{offset}/{width}: {:?}", t.unsupported_why);
}
}

/// `s_getreg_b32` of MODE on the GPU, before and after `s_round_mode` / `s_setreg_imm32_b32`
/// change the f16 / f64 round mode, in a program with no f16 op.
#[test]
fn gpu_s_getreg_b32_round_modes() {
if !gpu_ready() {
return;
}
let code = prog(&[
("S_GETREG_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 0, 4)), ("SDST", 2)], None),
("S_ROUND_MODE", "ENC_SOPP", &[("SIMM16", 0xC)], None),
("S_GETREG_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 0, 4)), ("SDST", 3)], None),
("S_GETREG_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 3, 1)), ("SDST", 4)], None),
("S_SETREG_IMM32_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 2, 2))], Some(1)),
("S_GETREG_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 2, 2)), ("SDST", 5)], None),
("S_GETREG_B32", "ENC_SOPK", &[("SIMM16", hwreg_imm(1, 1, 3)), ("SDST", 6)], None),
]);
let mut st = KernelState::default();
for s in 2..=6 {
st.sgpr[s] = 0xDEAD_BEEF;
}
let r = runner::run_code(&code, &st, &[]).unwrap();
assert_eq!(&r.sgpr[2..=6], &[0, 0xC, 1, 1, 2], "MODE[3:0] = 0, then 0xC, then 0x4");
}

#[test]
fn gpu_nonfused_mad_and_mac_prevent_fma_contraction() {
if !gpu_ready() {
Expand Down
Loading