Merge upstream main (42361b0) into shader/per-lane-phi

main moved under #307 and this branch. One conflict, in the test module of
emit/lower.rs: the imports take both sides' names (#117's `half_shift`
beside `Lowered`), and #117's `scalar_bit_tests_lower` sits after this
branch's waterfall tests. No code outside the test module was touched by
hand. A merge rather than a rebase so #307's commits keep their ids.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
r4.andClaude Fable 5.2 committed 2026-11-24 10:03:16 +02:00
commit 89639f014c
24 files changed
+1363 -191

No files matched your search

+207 -42
View File
@@ -107,33 +107,46 @@ fn buffer_format(format: u8) -> Option<(&'static [u32], Numeric)> {
const NORM: &[Numeric] = &[Unorm, Snorm, Uscaled, Sscaled, Uint, Sint];
const ALL: &[Numeric] = &[Unorm, Snorm, Uscaled, Sscaled, Uint, Sint, Float];
const WIDE: &[Numeric] = &[Uint, Sint, Float];
// `None`: a family not decoded (packed floats, 10_10_10_2).
const FAMILIES: [(Option<&[u32]>, &[Numeric]); 14] = [
(Some(&[8]), NORM),
(Some(&[16]), ALL),
(Some(&[8, 8]), NORM),
(Some(&[32]), WIDE),
(Some(&[16, 16]), ALL),
(None, ALL), // 10_11_11
(None, ALL), // 11_11_10
(None, NORM), // 10_10_10_2
(Some(&[10, 10, 10, 2]), NORM), // 2_10_10_10
(Some(&[8, 8, 8, 8]), NORM),
(Some(&[32, 32]), WIDE),
(Some(&[16, 16, 16, 16]), ALL),
(Some(&[32, 32, 32]), WIDE),
(Some(&[32, 32, 32, 32]), WIDE),
// Numbers and order from LLVM's `UfmtGFX10` (`SIDefines.h`); the GFX10
// buffer formats have no 5_9_9_9. Channel order from Mesa
// (`ac_formats.c`): R11G11B10_FLOAT is 10_11_11, and 10_10_10_2 has
// its 2-bit channel in the lowest bits.
const FAMILIES: [(&[u32], &[Numeric]); 14] = [
(&[8], NORM),
(&[16], ALL),
(&[8, 8], NORM),
(&[32], WIDE),
(&[16, 16], ALL),
(&[11, 11, 10], ALL), // 10_11_11
(&[10, 11, 11], ALL), // 11_11_10
(&[2, 10, 10, 10], NORM), // 10_10_10_2
(&[10, 10, 10, 2], NORM), // 2_10_10_10
(&[8, 8, 8, 8], NORM),
(&[32, 32], WIDE),
(&[16, 16, 16, 16], ALL),
(&[32, 32, 32], WIDE),
(&[32, 32, 32, 32], WIDE),
];
let mut n = usize::from(format.checked_sub(1)?);
for (bits, numerics) in FAMILIES {
if n < numerics.len() {
return Some((bits?, numerics[n]));
return Some((bits, numerics[n]));
}
n -= numerics.len();
}
None
}
/// How far a float channel of `width` bits (16, or the unsigned 11- and
/// 10-bit floats of the packed formats) shifts up to be a half:
/// the small floats are a half's exponent (5 bits, biased 15) and the top
/// of its mantissa, without the sign, so `legacyF16ToF32` widens them
/// exactly, denormals, infinities and NaNs included.
fn half_shift(width: u32) -> u32 {
debug_assert!(matches!(width, 10 | 11 | 16), "{width}-bit float");
if width == 16 { 0 } else { 15 - width }
}
struct Lower<'a> {
s: Shader,
x: &'a Parts<'a>,
@@ -2314,6 +2327,29 @@ impl<'a> Lower<'a> {
_ => Ok(l.s.m.i32(0)),
}
};
if let Some(op) = name.strip_prefix("s_bitcmp") {
// SCC is whether bit src1 (mod 32, or mod 64 for the 64-bit form)
// of src0 is clear (bitcmp0) or set (bitcmp1).
let (bit, k31) = (src32(self, 1)?, self.s.m.i32(31));
let idx = self.s.m.bin(c, Bin::And, bit, k31);
let word = if op.ends_with("_b64") {
let (a, b) = (half(self, 0, 0)?, half(self, 0, 1)?);
let k32 = self.s.m.i32(32);
let upper = self.s.m.bin(c, Bin::And, bit, k32);
let z = self.s.m.i32(0);
let upper = self.s.m.cmp(c, pred::NE, upper, z);
self.s.m.select(c, upper, b, a)
} else {
src32(self, 0)?
};
let shifted = self.s.m.bin(c, Bin::LShr, word, idx);
let one = self.s.m.i32(1);
let tested = self.s.m.bin(c, Bin::And, shifted, one);
let want = self.s.m.i32(u32::from(op.starts_with('1')));
let r = self.s.m.cmp(c, pred::EQ, tested, want);
self.set(scc, Val::Bit(r));
return Ok(());
}
if let Some(op) = name.strip_prefix("s_cmp_").filter(|o| o.ends_with("_u64")) {
// eq and lg only: both dwords.
let mut r = None;
@@ -3051,11 +3087,10 @@ impl<'a> Lower<'a> {
/// typed loads do (channels past the format's read 0, 0, 0, 1).
// shortcut: the descriptor's channel swizzle is taken as the identity
// (it is for raw views and the descriptors code builds), elements of 4
// bytes or more as dword-aligned, and the packed float and 10_10_10_2
// layouts are refused; decode them when a program needs them. Norm
// formats divide, which the AMD driver does 1 ULP off the hardware's
// correctly rounded conversion for some values; correct it with an
// integer check if a title compares converted values exactly.
// bytes or more as dword-aligned. Norm formats divide, which the AMD
// driver does 1 ULP off the hardware's correctly rounded conversion for
// some values; correct it with an integer check if a title compares
// converted values exactly.
fn format_load(&mut self, h: V, off: V, format: u8, channels: u32) -> Result<Vec<V>, Why> {
let (bits, numeric) = buffer_format(format).ok_or("a buffer format")?;
let c = self.cur;
@@ -3142,9 +3177,16 @@ impl<'a> Lower<'a> {
Numeric::Sint => signed(self),
Numeric::Float if w == 32 => field,
Numeric::Float => {
let h = match half_shift(w) {
0 => field,
k => {
let sh = self.s.m.i32(k);
self.s.m.bin(c, Bin::Shl, field, sh)
}
};
let f = self
.s
.op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[field]);
.op(c, "legacyF16ToF32", Attr::ReadNone, f32t, 131, &[h]);
float(self, f)
}
};
@@ -5098,28 +5140,50 @@ impl<'a> Lower<'a> {
let (op, ty) = rest.rsplit_once('_').ok_or("a compare")?;
let c = self.cur;
let r = match ty {
"f32" if op == "class" => {
"f32" | "f16" if op == "class" => {
// Bit k of src1 tests class k: signalling NaN, quiet NaN,
// -inf, -normal, -denormal, -0, +0, +denormal, +normal, +inf.
let x = self.fsrc(inst, 0)?;
let x = self.as_int(x);
let f16 = ty == "f16";
let x = if f16 {
let Fields::Valu(v) = &inst.fields else {
unreachable!()
};
let mut h = self.half_src(inst, 0)?;
if v.abs & 1 != 0 {
let m = self.s.m.i32(0x7FFF);
h = self.s.m.bin(c, Bin::And, h, m);
}
if v.neg & 1 != 0 {
let s = self.s.m.i32(0x8000);
h = self.s.m.bin(c, Bin::Xor, h, s);
}
h
} else {
let x = self.fsrc(inst, 0)?;
self.as_int(x)
};
let mask = self.src(inst, 1)?;
let k = |l: &mut Self, v: u32| l.s.m.i32(v);
let (k23, k31, kff, mant, quiet) = (
k(self, 23),
k(self, 31),
k(self, 0xFF),
k(self, 0x7F_FFFF),
k(self, 0x40_0000),
let (exp_sh, sign_sh, exp_mask, mant_mask, quiet_bit) = if f16 {
(10, 15, 0x1F, 0x03FF, 0x0200)
} else {
(23, 31, 0xFF, 0x7F_FFFF, 0x40_0000)
};
let (k_exp, k_sign, k_emax, mant, quiet) = (
k(self, exp_sh),
k(self, sign_sh),
k(self, exp_mask),
k(self, mant_mask),
k(self, quiet_bit),
);
let e = self.s.m.bin(c, Bin::LShr, x, k23);
let e = self.s.m.bin(c, Bin::And, e, kff);
let e = self.s.m.bin(c, Bin::LShr, x, k_exp);
let e = self.s.m.bin(c, Bin::And, e, k_emax);
let m = self.s.m.bin(c, Bin::And, x, mant);
let neg = self.s.m.bin(c, Bin::LShr, x, k31);
let neg = self.s.m.bin(c, Bin::LShr, x, k_sign);
let zero = k(self, 0);
let (e0, e255) = (
let (e0, emax) = (
self.s.m.cmp(c, pred::EQ, e, zero),
self.s.m.cmp(c, pred::EQ, e, kff),
self.s.m.cmp(c, pred::EQ, e, k_emax),
);
let m0 = self.s.m.cmp(c, pred::EQ, m, zero);
let q = self.s.m.bin(c, Bin::And, x, quiet);
@@ -5129,7 +5193,7 @@ impl<'a> Lower<'a> {
(k(self, 8), k(self, 9), k(self, 6), k(self, 7));
let t = self.s.m.select(c, m0, zero_c, denorm);
let t = self.s.m.select(c, e0, t, normal);
let t = self.s.m.select(c, e255, inf, t);
let t = self.s.m.select(c, emax, inf, t);
let eleven = k(self, 11);
let mirrored = self.s.m.bin(c, Bin::Sub, eleven, t);
let is_neg = self.s.m.cmp(c, pred::NE, neg, zero);
@@ -5138,7 +5202,7 @@ impl<'a> Lower<'a> {
let nan_class = self.s.m.select(c, q, one, snan);
let yes = self.s.m.bool(true);
let nan = self.s.m.bin(c, Bin::Xor, m0, yes);
let nan = self.s.m.bin(c, Bin::And, nan, e255);
let nan = self.s.m.bin(c, Bin::And, nan, emax);
let t = self.s.m.select(c, nan, nan_class, t);
let bit = self.s.m.bin(c, Bin::LShr, mask, t);
let bit = self.s.m.bin(c, Bin::And, bit, one);
@@ -6720,7 +6784,7 @@ impl<'a> Lower<'a> {
#[cfg(test)]
mod tests {
use super::{Lowered, Numeric, Parts, buffer_format, lower};
use super::{Lowered, Numeric, Parts, buffer_format, half_shift, lower};
use crate::native::{
ComputeIo, HwStage, NativeProgram, ResourceType, RootParam, RootType, StageIo, UsedResource,
};
@@ -6960,6 +7024,35 @@ mod tests {
}
}
#[test]
fn scalar_bit_tests_lower() {
// Each tests one bit of a constant into SCC and branches on it (only
// code with an effect needs its operands).
let tail = [
0xBF85_0001, // s_cbranch_scc1 -> s_endpgm
0xBE84_0380, // s_mov_b32 s4, 0
S_ENDPGM,
];
let bit_tests: [&[u32]; 4] = [
&[0xBE81_0387, 0xBF0C_8301], // s_mov_b32 s1, 7; s_bitcmp0_b32 s1, 3
&[0xBE81_0387, 0xBF0D_8301], // s_mov_b32 s1, 7; s_bitcmp1_b32 s1, 3
&[
0xBE80_0387, // s_mov_b32 s0, 7
0xBE81_0380, // s_mov_b32 s1, 0
0xBF0E_8300, // s_bitcmp0_b64 s[0:1], 3
],
&[
0xBE80_0387, // s_mov_b32 s0, 7
0xBE81_0380, // s_mov_b32 s1, 0
0xBF0F_8300, // s_bitcmp1_b64 s[0:1], 3
],
];
for head in bit_tests {
let words: Vec<u32> = head.iter().chain(&tail).copied().collect();
lowers(&words).unwrap();
}
}
#[test]
fn buffer_formats_by_number() {
// The numbers the disassembler names 8_8_8_8_UNORM, 16_16_SNORM,
@@ -6975,9 +7068,81 @@ mod tests {
buffer_format(77),
Some((&[32, 32, 32, 32][..], Numeric::Float))
);
// Invalid, a packed float, past the table.
// Invalid, past the table.
assert_eq!(buffer_format(0), None);
assert_eq!(buffer_format(36), None);
assert_eq!(buffer_format(78), None);
}
#[test]
fn packed_buffer_formats_by_number() {
// LLVM's `UfmtGFX10`: 10_11_11 is 30-36, 11_11_10 37-43,
// 10_10_10_2 44-49. Channels from the lowest bits: R11G11B10_FLOAT
// is 10_11_11 (Mesa), 10_10_10_2 has the 2-bit channel first.
assert_eq!(buffer_format(30), Some((&[11, 11, 10][..], Numeric::Unorm)));
assert_eq!(buffer_format(36), Some((&[11, 11, 10][..], Numeric::Float)));
assert_eq!(buffer_format(37), Some((&[10, 11, 11][..], Numeric::Unorm)));
assert_eq!(buffer_format(43), Some((&[10, 11, 11][..], Numeric::Float)));
assert_eq!(
buffer_format(44),
Some((&[2, 10, 10, 10][..], Numeric::Unorm))
);
assert_eq!(
buffer_format(45),
Some((&[2, 10, 10, 10][..], Numeric::Snorm))
);
assert_eq!(
buffer_format(46),
Some((&[2, 10, 10, 10][..], Numeric::Uscaled))
);
assert_eq!(
buffer_format(47),
Some((&[2, 10, 10, 10][..], Numeric::Sscaled))
);
assert_eq!(
buffer_format(48),
Some((&[2, 10, 10, 10][..], Numeric::Uint))
);
assert_eq!(
buffer_format(49),
Some((&[2, 10, 10, 10][..], Numeric::Sint))
);
}
/// A half's value (sign, 5-bit exponent biased 15, 10-bit mantissa), as
/// `legacyF16ToF32` widens it.
fn half(h: u32) -> f32 {
let (s, e, m) = ((h >> 15) & 1, (h >> 10) & 31, h & 1023);
let v = match e {
0 => m as f32 * 2f32.powi(-24),
31 if m == 0 => f32::INFINITY,
31 => f32::NAN,
_ => (1024 + m) as f32 * 2f32.powi(e as i32 - 25),
};
if s == 1 { -v } else { v }
}
#[test]
fn small_floats_widen_as_halves() {
// Unsigned 11- and 10-bit floats: a 5-bit exponent biased 15 over a
// 6- or 5-bit mantissa, no sign; values from the D3D/GL spec.
let f11 = |x: u32| half(x << half_shift(11));
let f10 = |x: u32| half(x << half_shift(10));
assert_eq!(f11(0), 0.0);
assert_eq!(f11(0x001), 2f32.powi(-20)); // smallest denormal
assert_eq!(f11(0x03F), 63.0 * 2f32.powi(-20)); // largest denormal
assert_eq!(f11(0x040), 2f32.powi(-14)); // smallest normal
assert_eq!(f11(0x3C0), 1.0);
assert_eq!(f11(0x3E0), 1.5);
assert_eq!(f11(0x7BF), 65024.0); // largest
assert_eq!(f11(0x7C0), f32::INFINITY);
assert!(f11(0x7C1).is_nan());
assert_eq!(f10(0x001), 2f32.powi(-19));
assert_eq!(f10(0x020), 2f32.powi(-14));
assert_eq!(f10(0x1E0), 1.0);
assert_eq!(f10(0x3DF), 64512.0);
assert_eq!(f10(0x3E0), f32::INFINITY);
assert!(f10(0x3FF).is_nan());
// A 16-bit float is a half already, sign included.
assert_eq!(half(0xBC00 << half_shift(16)), -1.0);
}
}