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
2 changes: 1 addition & 1 deletion crates/xcl/src/gpu/dxil/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,7 @@ pub fn is_f64_rounded_op(name: &str) -> bool {
name.contains("_f64")
&& !name.starts_with("v_cmp_")
&& !name.starts_with("v_cmpx_")
&& !matches!(name, "v_cvt_f32_f64" | "v_cvt_i32_f64" | "v_cvt_u32_f64" | "v_frexp_exp_i32_f64")
&& !matches!(name, "v_cvt_f32_f64" | "v_cvt_i32_f64" | "v_cvt_u32_f64" | "v_frexp_exp_i32_f64" | "v_trig_preop_f64")
}

/// The recompiler's `configure_round_modes`: (dynamic f16 rounding, has f64 rounding ops).
Expand Down
88 changes: 88 additions & 0 deletions crates/xcl/src/gpu/dxil/lower_wide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,13 @@ impl Lw {
_ => self.bin(BinOp::AShr, v, s),
}
}
"v_trig_preop_f64" => {
if m.omod != 0 || m.clamp {
return err(d, "v_trig_preop_f64 with an output modifier");
}
// only the exponent of S0 is read, so ABS / NEG do not matter
self.trig_preop(lh[0].1, lh[1].0)
}
"v_mad_u64_u32" | "v_mad_i64_i32" => {
let p = if name == "v_mad_u64_u32" {
let (x, y) = (self.zext64(lh[0].0), self.zext64(lh[1].0));
Expand Down Expand Up @@ -1089,6 +1096,87 @@ impl Lw {
Ok(())
}

/// `v_trig_preop_f64` as the HLSL backend's `trig_preop` (recomp/emit.rs): the 53 bits of
/// 2/pi below b(1200 - shift), scaled; exact, so built from integers. The table is read
/// through a chain of selects (37 words).
pub fn trig_preop(&mut self, s0_hi: Value, s1: Value) -> Value {
use crate::gpu::recomp::emit::TRIG_PREOP_2_OVER_PI as T;
let e = self.lshrc(s0_hi, 20);
let e = self.andc(e, 0x7FF);
let big = self.cmpc(Pred::UGt, e, 1077);
let ex = self.addc(e, (-1077i32) as u32);
let z0 = self.c(0);
let ex = self.sel(big, ex, z0);
let s1 = self.andc(s1, 31);
let k53 = self.c(53);
let sh = self.mul(s1, k53);
let sh = self.add(sh, ex);
let huge = self.cmpc(Pred::UGe, e, 1968);
let base = self.c(75);
let neg = self.c((-53i32) as u32);
let base = self.sel(huge, base, neg);
let sc = self.sub(base, sh);
let w = self.lshrc(sh, 5);
let b = self.andc(sh, 31);
let (mut t0, mut t1, mut t2) = (self.c(0), self.c(0), self.c(0));
for k in 0..T.len() {
let at = self.cmpc(Pred::Eq, w, k as u32);
let v0 = self.c(T[k]);
let v1 = self.c(T.get(k + 1).copied().unwrap_or(0));
let v2 = self.c(T.get(k + 2).copied().unwrap_or(0));
t0 = self.sel(at, v0, t0);
t1 = self.sel(at, v1, t1);
t2 = self.sel(at, v2, t2);
}
// (x << b) | ((y >> 1) >> (31 - b)): no shift by 32 when b is 0
let k31 = self.c(31);
let nb = self.sub(k31, b);
let join = |s: &mut Self, x: Value, y: Value| {
let hi = s.shl(x, b);
let y1 = s.lshrc(y, 1);
let lo = s.lshr(y1, nb);
s.or(hi, lo)
};
let a = join(self, t0, t1);
let c = join(self, t1, t2);
let vh = self.lshrc(a, 11);
let al = self.shlc(a, 21);
let ch = self.lshrc(c, 11);
let vl = self.or(al, ch);
let v = self.pack64(vl, vh);
let fh = self.firstbithigh(vh);
let fh = self.addc(fh, 32);
let fl = self.firstbithigh(vl);
let hz = self.nz(vh);
let n = self.sel(hz, fh, fl);
let e2 = self.add(n, sc);
let normal = self.cmpc(Pred::SGe, e2, (-1022i32) as u32);
let k52 = self.c(52);
let kn = self.sub(k52, n);
let kd = self.addc(sc, 1074);
let k = self.sel(normal, kn, kd);
let ku = self.zext64(k);
let l = self.shl(v, ku);
let z = self.c(0);
let nk = self.sub(z, k);
let nku = self.zext64(nk);
let r = self.lshr(v, nku);
let z64 = self.c64(0);
let small = self.cmpc(Pred::SLt, nk, 64);
let r = self.sel(small, r, z64);
let kpos = self.cmpc(Pred::SGe, k, 0);
let mm = self.sel(kpos, l, r);
let mask = self.c64((1u64 << 52) - 1);
let mant = self.and(mm, mask);
let be = self.addc(e2, 1023);
let be = self.zext64(be);
let s52 = self.c64(52);
let be = self.bin(BinOp::Shl, be, s52);
let nbits = self.or(be, mant);
// a zero window (shift >= 1184) has e2 < -1022 and comes out of the denormal form as 0
self.sel(normal, nbits, mm)
}

/// Double division (D3D11.1 double extensions).
pub fn ddiv(&mut self, a: Value, b: Value) -> Value {
self.s.require(Feature::DoubleExtensions);
Expand Down
60 changes: 59 additions & 1 deletion crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub(super) struct Em {
uses_lds: bool,
/// scratch_* used: declare the per-lane private memory
uses_scratch: bool,
/// v_trig_preop_f64 used: declare the 2/pi table (`PRELUDE_TRIG_PREOP`)
uses_trig_preop: bool,
/// split wave64 (`x64`): words one exchange carries (1: masks and lane reads, up to 16:
/// coherent scalar loads, 32: `ds_bpermute`)
x64_words: usize,
Expand Down Expand Up @@ -573,7 +575,7 @@ fn is_f64_rounded_op(name: &str) -> bool {
name.contains("_f64")
&& !name.starts_with("v_cmp_")
&& !name.starts_with("v_cmpx_")
&& !matches!(name, "v_cvt_f32_f64" | "v_cvt_i32_f64" | "v_cvt_u32_f64" | "v_frexp_exp_i32_f64")
&& !matches!(name, "v_cvt_f32_f64" | "v_cvt_i32_f64" | "v_cvt_u32_f64" | "v_frexp_exp_i32_f64" | "v_trig_preop_f64")
}

/// HLSL expression converting float expression `e` to a 16-bit half (`0..=0xFFFF`).
Expand Down Expand Up @@ -612,6 +614,7 @@ impl Em {
ps_writes_mask: false,
uses_lds: false,
uses_scratch: false,
uses_trig_preop: false,
x64_words: 1,
mask_whole_live: BTreeMap::new(),
upper: None,
Expand Down Expand Up @@ -3697,6 +3700,14 @@ impl Em {
"v_lshlrev_b64" => format!("({} << ({} & 63u))", u64v(1), lo_hi[0].0),
"v_lshrrev_b64" => format!("({} >> ({} & 63u))", u64v(1), lo_hi[0].0),
"v_ashrrev_i64" => format!("(uint64_t)((int64_t){} >> ({} & 63u))", u64v(1), lo_hi[0].0),
"v_trig_preop_f64" => {
if m.omod != 0 || m.clamp {
return Err("v_trig_preop_f64 with an output modifier");
}
self.uses_trig_preop = true;
// only the exponent of S0 is read, so ABS / NEG do not matter
format!("trig_preop({}, {})", lo_hi[0].1, lo_hi[1].0)
}
"v_mad_u64_u32" | "v_mad_i64_i32" => {
let p = if name == "v_mad_u64_u32" {
format!("((uint64_t){} * (uint64_t){})", lo_hi[0].0, lo_hi[1].0)
Expand Down Expand Up @@ -4873,6 +4884,9 @@ impl Em {
if self.uses_scratch {
h.push_str(PRELUDE_SCRATCH);
}
if self.uses_trig_preop {
h.push_str(&trig_preop_prelude());
}
if self.dynamic_f16_rm {
h.push_str("static uint xcl_fp16_rm = 0u;\n");
}
Expand Down Expand Up @@ -5939,6 +5953,50 @@ fn d16_merge(v: &str, raw: &str, n: u32, signed: bool, half: u32) -> String {
}
}

/// `v_trig_preop_f64`: the 53 bits of 2/pi that start `shift` bits below b1200, times
/// 2^(-53 - shift), plus 2^128 when the exponent of S0 is at least 1968, where
/// `shift = S1[4:0] * 53`, plus `exp(S0) - 1077` when that exponent is above 1077 (RDNA2
/// ISA). The product is exact (the table ends at b17, so no bit falls below 2^-1074), so
/// the double is built from integers. Measured on an RDNA2 gfx1036.
fn trig_preop_prelude() -> String {
let words: Vec<String> = TRIG_PREOP_2_OVER_PI.iter().map(|w| format!("0x{w:08X}u")).collect();
format!(
r#"static const uint TRIG_2_OVER_PI[{n}] = {{ {} }};
uint trig_word(uint i) {{ return select(i < {n}u, TRIG_2_OVER_PI[min(i, {m}u)], 0u); }}
uint64_t trig_preop(uint s0_hi, uint s1) {{
uint e = (s0_hi >> 20) & 0x7FFu;
uint sh = (s1 & 31u) * 53u + select(e > 1077u, e - 1077u, 0u);
int sc = select(e >= 1968u, 75, -53) - (int)sh;
uint w = sh >> 5, b = sh & 31u;
uint t0 = trig_word(w), t1 = trig_word(w + 1u), t2 = trig_word(w + 2u);
uint a = (t0 << b) | ((t1 >> 1) >> (31u - b)), c = (t1 << b) | ((t2 >> 1) >> (31u - b));
uint vh = a >> 11, vl = (a << 21) | (c >> 11);
uint64_t v = ((uint64_t)vh << 32) | (uint64_t)vl;
int n = select(vh != 0u, 32 + (int)firstbithigh(vh), (int)firstbithigh(vl));
int e2 = n + sc;
bool normal = e2 >= -1022;
int k = select(normal, 52 - n, sc + 1074);
uint64_t l = v << (uint64_t)((uint)k & 63u), r = v >> (uint64_t)((uint)(-k) & 63u);
uint64_t m = select(k >= 0, l, select(-k < 64, r, (uint64_t)0));
// a zero window (shift >= 1184) has e2 < -1022 and comes out of the denormal form as 0
return select(normal, ((uint64_t)(uint)(e2 + 1023) << 52) | (m & 0xFFFFFFFFFFFFFull), m);
}}
"#,
words.join(", "),
n = TRIG_PREOP_2_OVER_PI.len(),
m = TRIG_PREOP_2_OVER_PI.len() - 1
)
}

/// The fraction bits of 2/pi that `v_trig_preop_f64` reads, b1200 (the first after the point)
/// down to b17, most significant first. The gfx1036 returns zeros for b16..b0.
pub(crate) const TRIG_PREOP_2_OVER_PI: [u32; 37] = [
0xA2F9836E, 0x4E441529, 0xFC2757D1, 0xF534DDC0, 0xDB629599, 0x3C439041, 0xFE5163AB, 0xDEBBC561, 0xB7246E3A, 0x424DD2E0,
0x06492EEA, 0x09D1921C, 0xFE1DEB1C, 0xB129A73E, 0xE88235F5, 0x2EBB4484, 0xE99C7026, 0xB45F7E41, 0x3991D639, 0x835339F4,
0x9C845F8B, 0xBDF9283B, 0x1FF897FF, 0xDE05980F, 0xEF2F118B, 0x5A0A6D1F, 0x6D367ECF, 0x27CB09B7, 0x4F463F66, 0x9E5FEA2D,
0x7527BAC7, 0xEBE5F17B, 0x3D0739F7, 0x8A5292EA, 0x6BFB5FB1, 0x1F8D5D08, 0x56033046,
];

/// Per-lane private memory of scratch_* (static = thread-private in HLSL; 4 KB per lane).
const PRELUDE_SCRATCH: &str = r#"
static uint xcl_scratch[1024];
Expand Down
112 changes: 112 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,118 @@ fn f64_roots(wave64: bool) {
assert!(bad.is_empty(), "wave{lanes}:\n{}", bad.join("\n"));
}

/// `v_trig_preop_f64` (S0, S1) → result, measured on an RDNA2 gfx1036 (Radeon iGPU, HIP). The
/// rows cover S0 exponents up to and past 1077 and 1968, NaN / inf / negative S0, S1 with bits
/// above [4:0], zero results (the window past the table), denormal results and windows that
/// reach the end of the hardware's table (b17).
const TRIG_PREOP_RDNA2: [[u64; 3]; 64] = [
[0x7cf123456789abcd, 0x00000005, 0x0000000000000000],
[0x7d5123456789abcd, 0x00000004, 0x00aa10ac06608c00],
[0xfd10000000005555, 0x00000005, 0x0000000000000000],
[0x8400c6fa74beb8ae, 0x688af7b3, 0x00ff17b3d0739f78],
[0x00039c7ad0cf26b9, 0x29d1556a, 0x1ec8135a2fbf209c],
[0xfff6d15c28c4c757, 0xffffffe3, 0x012aba10ac066088],
[0x7bc0000000005555, 0x00000004, 0x02311f8d5d085603],
[0xa36ee3eb2ad33454, 0x3e9397c3, 0x35f3c439041fe516],
[0xfe30000000005555, 0x00000004, 0x0000580cc1180000],
[0x7c20000000005555, 0x00000000, 0x0f18fd9a797fa8b5],
[0x7da0000000005555, 0x00000000, 0x0d997fa8b5d49eeb],
[0xdb495bce98e0848b, 0x84382365, 0x17608bf177bf2507],
[0x7b00000000005555, 0x00000004, 0x02f5fb11f8d5d085],
[0x7b0ddb53b7d64c3d, 0x00000005, 0x0000180cc1180000],
[0x7b00000000005555, 0x00000002, 0x098f2f8bd9e839ce],
[0xffb0000000005555, 0x00000001, 0x083b3d0739f78a52],
[0xfc50000000005555, 0x00000004, 0x01af1aba10ac0660],
[0xfdd0000000005555, 0x00000000, 0x0d57fa8b5d49eeb0],
[0xccbad29d5644a528, 0xb98d92f1, 0x00000000005e294a],
[0x7e7123456789abcd, 0x00000004, 0x0000080cc1180000],
[0x7ec0000000005555, 0x00000004, 0x0000000cc1180000],
[0x7cc123456789abcd, 0x00000004, 0x012aba10ac06608c],
[0x7f9123456789abcd, 0x00000004, 0x0000000001180000],
[0x7de123456789abcd, 0x00000004, 0x0001580cc1180000],
[0x0002f273319e70c3, 0x00000000, 0x3fe45f306dc9c882],
[0x80069e880ac2d426, 0x00000002, 0x394a6ee06db14acc],
[0x000dee014709d7fd, 0x00000015, 0x0000000000000000],
[0x80024e23b6e62dcb, 0x00000016, 0x0000000000000000],
[0x999f919275185ba1, 0x6e84eeb4, 0x000000000000294a],
[0x2accbeb57d0e7eb3, 0x4651513a, 0x0000000000000000],
[0xa9ad789b77bfb85e, 0x81037655, 0x0000000000000000],
[0x4366b711acc929de, 0x00000005, 0x2f42371d2126e970],
[0xc36ca5d5a2fdf398, 0x00000000, 0x3fc17cc1b727220a],
[0xcb07fb9c25cecdcf, 0x00000011, 0x0000f41ce7de294a],
[0x4363f77e06a26789, 0x00000015, 0x0000000000000000],
[0xc36c18a2b3786189, 0x00000016, 0x0000000000000000],
[0x4368966afa02fa69, 0xffffffe3, 0x35ce21c820ff28b0],
[0x7af3c9a8da6091c9, 0xffffffe3, 0x0000000001de294a],
[0x599ed57effe819d5, 0x39d272f8, 0x0000000000000000],
[0xdb13fce4a7087ca3, 0x287d0037, 0x0000000000000000],
[0x7b0cae0559eebce6, 0xffffffe3, 0x064bc529497535fd],
[0xfb0b31cb10c632f1, 0x00000020, 0x10374f463f669e5f],
[0xfb3403ad530e178f, 0xf1321dc5, 0x0000000cc1180000],
[0x7b3d03b8e7c459c9, 0x4f229637, 0x0000000000000000],
[0xfb0629a6ca876f6e, 0x33a8008f, 0x0000000000000000],
[0x7ff3fa01314903b7, 0x00000001, 0x07f3d0739f78a529],
[0xfffa97b158e5a673, 0x00000000, 0x0b43dd63f5f2f8bd],
[0x7ff123456789abcd, 0x00000004, 0x0000000000080000],
[0x7ff3eeac59b732cb, 0x00000015, 0x0000000000000000],
[0xfff7a8bc73cdbfdc, 0x00000005, 0x0000000000000000],
[0x7b00000000005555, 0x00000019, 0x0000000000000000],
[0xf35f1337656a5f06, 0x91890b73, 0x0000000000000000],
[0xfbf0000000005555, 0x0000000a, 0x0000000000000000],
[0xfef0000000005555, 0x00000017, 0x0000000000000000],
[0x708251e0152c4fb9, 0x00000015, 0x0000000000000000],
[0x7da0000000005555, 0x0000000b, 0x0000000000000000],
[0x7d04bf147c026ad5, 0x00000015, 0x0000000000000000],
[0xfb90000000005555, 0x0000000e, 0x0000000000000000],
[0xfe30000000005555, 0x0000000b, 0x0000000000000000],
[0x44cb1a43d8986a1e, 0x0000001f, 0x0000000000000000],
[0x7d40000000005555, 0x00000011, 0x0000000000000000],
[0xaf48f4a2a702f9ce, 0x4ad6eb2f, 0x0e26b414da3eda6c],
[0xc34940811959a65a, 0xffffffe3, 0x35f3c439041fe516],
[0xfb1c5a0d1d20125e, 0x00000000, 0x101d3d18fd9a797e],];

/// v_trig_preop_f64 in both backends against the gfx1036's results.
#[test]
fn gpu_trig_preop_f64_matches_rdna2() {
if !gpu_ready() {
return;
}
let code = prog(&[("V_TRIG_PREOP_F64", "ENC_VOP3", &[("VDST", 10), ("SRC0", V + 1), ("SRC1", V + 3)], None)]);
let wave64 = true;
let mut st = KernelState::default();
for (l, r) in st.vgpr.iter_mut().take(64).enumerate() {
let [s0, s1, _] = TRIG_PREOP_RDNA2[l];
(r[1], r[2], r[3]) = (s0 as u32, (s0 >> 32) as u32, s1 as u32);
}
for direct in [false, true] {
let dxil = if direct {
let o = crate::gpu::dxil::kernel::KernelOptions { wave64, split: !gpu_waves().contains(&true), mem_windows: 1, sampler_base: 0 };
crate::gpu::dxil::kernel::translate(&code, o).unwrap_or_else(|e| panic!("direct DXIL: {e}"))
} else {
let t = translate(&code, &Options { wave64, split_wave64: !gpu_waves().contains(&true), ..Default::default() }).unwrap();
assert!(t.complete(), "{:?}
{}", t.unsupported_why, t.hlsl);
super::dxc::compile(&t.hlsl, "main", "cs_6_6", &[]).unwrap_or_else(|e| panic!("{e}
{}", t.hlsl))
};
let r = runner::run(&dxil, &st, &[]).unwrap();
for (l, [s0, s1, want]) in TRIG_PREOP_RDNA2.iter().enumerate() {
let got = r.vgpr[l][10] as u64 | (r.vgpr[l][11] as u64) << 32;
assert_eq!(got, *want, "direct={direct} lane {l}: v_trig_preop_f64({s0:#018x}, {s1:#010x})");
}
}
}

/// An output modifier on v_trig_preop_f64 is refused (not measured).
#[test]
fn trig_preop_f64_with_omod_is_refused() {
let code = prog(&[("V_TRIG_PREOP_F64", "ENC_VOP3", &[("VDST", 10), ("SRC0", V + 1), ("SRC1", V + 3), ("OMOD", 1)], None)]);
let t = translate(&code, &Options::default()).unwrap();
assert!(!t.complete());
let o = crate::gpu::dxil::kernel::KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 };
assert!(crate::gpu::dxil::kernel::translate(&code, o).is_err());
}

/// v_ldexp_f64 = x · 2^n, rounded once, for every i32 n: results that over- or underflow,
/// denormal results, exponents beyond the f32 range of 2^n.
#[test]
Expand Down
Loading