diff --git a/crates/xcl/src/gpu/dxil/core_tests.rs b/crates/xcl/src/gpu/dxil/core_tests.rs index e1421c69bc..a9618b79d5 100644 --- a/crates/xcl/src/gpu/dxil/core_tests.rs +++ b/crates/xcl/src/gpu/dxil/core_tests.rs @@ -980,7 +980,11 @@ fn window_at_lowers_to_32bit_carry_without_int64() { assert!(!dis.contains("64-bit integers") && !body.contains("i64"), "window_at with adds must not emit i64:\n{dis}"); assert!(!dis.contains("binaryWithCarryOrBorrow"), "window_at should avoid uaddc struct overhead:\n{dis}"); assert!(body.contains("icmp ult i32"), "expected scalar 32-bit carry compare in DXIL:\n{dis}"); - assert!(dis.contains("dx.op.binaryWithTwoOuts.i32"), "expected UMul in DXIL:\n{dis}"); + if crate::util::mac_host() { + assert!(!dis.contains("dx.op.binaryWithTwoOuts"), "UMul on a Mac (metal-shaderconverter stops on it):\n{dis}"); + } else { + assert!(dis.contains("dx.op.binaryWithTwoOuts.i32"), "expected UMul in DXIL:\n{dis}"); + } } let zero_off = prog(&[ diff --git a/crates/xcl/src/gpu/dxil/ops.rs b/crates/xcl/src/gpu/dxil/ops.rs index f7c23eb0df..55616d2e6b 100644 --- a/crates/xcl/src/gpu/dxil/ops.rs +++ b/crates/xcl/src/gpu/dxil/ops.rs @@ -437,10 +437,34 @@ impl Shader { pub fn imul_wide(&mut self, a: Value, b: Value) -> (Value, Value) { self.two_outs(41, a, b) } - /// Full 64-bit product of two unsigned i32 → `(hi, lo)`. + /// Full 64-bit product of two unsigned i32 → `(hi, lo)`. On a Mac, `umul_wide_halves`: Apple's + /// metal-shaderconverter under D3DMetal (CrossOver 26.3) stops on some programs with `UMul` + /// ("patchBinaryWith2Outs: need to add support for opcode: 42") and the pipeline is refused. pub fn umul_wide(&mut self, a: Value, b: Value) -> (Value, Value) { + if crate::util::mac_host() { + return self.umul_wide_halves(a, b); + } self.two_outs(42, a, b) } + /// `umul_wide` in 32-bit operations, without `UMul` or `Int64Ops`: the high half from the + /// 16-bit parts, as `mul_hi_u32` of the HLSL kernel (`recomp::emit`). + pub fn umul_wide_halves(&mut self, a: Value, b: Value) -> (Value, Value) { + let (m16, s16) = (self.m.i32c(0xFFFF), self.m.i32c(16)); + let lo = self.m.bin(BinOp::Mul, a, b); + let (al, ah) = (self.m.bin(BinOp::And, a, m16), self.m.bin(BinOp::LShr, a, s16)); + let (bl, bh) = (self.m.bin(BinOp::And, b, m16), self.m.bin(BinOp::LShr, b, s16)); + let (ll, lh, hl, hh) = (self.m.bin(BinOp::Mul, al, bl), self.m.bin(BinOp::Mul, al, bh), self.m.bin(BinOp::Mul, ah, bl), self.m.bin(BinOp::Mul, ah, bh)); + let mut mid = self.m.bin(BinOp::LShr, ll, s16); + let mut hi = hh; + for p in [lh, hl] { + let low = self.m.bin(BinOp::And, p, m16); + mid = self.m.bin(BinOp::Add, mid, low); + let high = self.m.bin(BinOp::LShr, p, s16); + hi = self.m.bin(BinOp::Add, hi, high); + } + let carry = self.m.bin(BinOp::LShr, mid, s16); + (self.m.bin(BinOp::Add, hi, carry), lo) + } /// Unsigned divide → `(quotient, remainder)` (division by zero gives all ones). pub fn udiv_rem(&mut self, a: Value, b: Value) -> (Value, Value) { self.two_outs(43, a, b) diff --git a/crates/xcl/src/gpu/dxil/ops_tests.rs b/crates/xcl/src/gpu/dxil/ops_tests.rs index 7c25de5091..53471abca1 100644 --- a/crates/xcl/src/gpu/dxil/ops_tests.rs +++ b/crates/xcl/src/gpu/dxil/ops_tests.rs @@ -941,6 +941,28 @@ fn double_unary_legality_matches_dxil_tables() { assert_eq!(legal, DOUBLE_UNARY); } +/// `umul_wide` on a Mac: the full product in 32-bit operations, with no `UMul` (opcode 42) for +/// Apple's metal-shaderconverter to stop on and no `Int64Ops`. +#[test] +fn umul_wide_without_umul_matches_cpu() { + let mut k = kernel(); + let a = k.input(0); + let b = k.input(1); + let (hi, lo) = k.s.umul_wide_halves(a, b); + k.out(0, hi); + k.out(1, lo); + let c = k.finish([32, 1, 1]); + let dis = validate::disassemble(&c).unwrap(); + assert!(!dis.contains("binaryWithTwoOuts"), "UMul left in the kernel:\n{dis}"); + assert!(!dis.contains("64-bit integers"), "the product needs Int64Ops:\n{dis}"); + let bs = uvals2(); + let Some(r) = gpu(&c, &mem_of(&[UVALS.to_vec(), bs.clone()]), &[], &[]) else { return }; + for l in 0..32 { + let p = UVALS[l] as u64 * bs[l] as u64; + assert_eq!([outv(&r, 0)[l], outv(&r, 1)[l]], [(p >> 32) as u32, p as u32], "lane {l}: {:#x} * {:#x}", UVALS[l], bs[l]); + } +} + #[test] fn int64_bit_ops_match_cpu() { const MAX_INPUT: u64 = 0x8000_0000_8000_0000; diff --git a/crates/xcl/src/util.rs b/crates/xcl/src/util.rs index a7609fe3ff..37b05d9354 100644 --- a/crates/xcl/src/util.rs +++ b/crates/xcl/src/util.rs @@ -132,6 +132,36 @@ pub fn under_wine() -> bool { }) } +/// The process runs under Wine on a Mac (ntdll's `wine_get_host_version` says Darwin): CrossOver +/// or the Game Porting Toolkit, where D3D12 is Apple's D3DMetal and, on Apple Silicon, the +/// title's code runs through Rosetta 2. Known from the start of the run, before there is a +/// device. `XCL_MAC_HOST=1` / `0` overrides the detection. +pub fn mac_host() -> bool { + use windows_sys::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress}; + static MAC: std::sync::OnceLock = std::sync::OnceLock::new(); + *MAC.get_or_init(|| { + let forced = std::env::var("XCL_MAC_HOST").ok(); + // SAFETY: ntdll is always loaded; Wine's export fills two pointers to static strings. + let sysname = unsafe { + GetProcAddress(GetModuleHandleA(c"ntdll.dll".as_ptr() as *const u8), c"wine_get_host_version".as_ptr() as *const u8).and_then(|f| { + let f: unsafe extern "C" fn(*mut *const u8, *mut *const u8) = std::mem::transmute(f); + let (mut sysname, mut release) = (std::ptr::null(), std::ptr::null()); + f(&mut sysname, &mut release); + from_cstr_ptr(sysname) + }) + }; + mac_host_from(forced.as_deref(), sysname.as_deref()) + }) +} + +/// `mac_host`'s answer from `XCL_MAC_HOST` (`forced`) and the host's system name, if Wine gave one. +fn mac_host_from(forced: Option<&str>, sysname: Option<&str>) -> bool { + match forced { + Some(v) => v.trim() != "0", + None => sysname.is_some_and(|s| s.eq_ignore_ascii_case("Darwin")), + } +} + /// Normalised DLL key: lower-case file name, ".dll" appended when no extension. pub fn dll_key(name: &str) -> String { let base = name.rsplit(['\\', '/']).next().unwrap_or(name); @@ -321,6 +351,20 @@ unsafe impl Sync for SendPtr {} mod tests { use super::*; + /// Darwin from Wine's host version is a Mac (in any case); Linux, Windows and no answer are + /// not; `XCL_MAC_HOST` decides when it is set, "0" for no and anything else for yes. + #[test] + fn a_mac_host_is_darwin_under_wine_unless_forced() { + assert!(mac_host_from(None, Some("Darwin"))); + assert!(mac_host_from(None, Some("darwin"))); + assert!(!mac_host_from(None, Some("Linux"))); + assert!(!mac_host_from(None, None)); + assert!(!mac_host_from(Some("0"), Some("Darwin"))); + assert!(!mac_host_from(Some(" 0 "), Some("Darwin"))); + assert!(mac_host_from(Some("1"), None)); + assert!(mac_host_from(Some("1"), Some("Linux"))); + } + #[test] fn dll_keys() { assert_eq!(dll_key("KERNELX"), "kernelx.dll");