Merge fork main a803c87
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
commit
fa967c0340
22 files changed
+895
-117
No files matched your search
@@ -1498,7 +1498,23 @@ impl<'a> Lower<'a> {
|
||||
.collect(),
|
||||
scalar: x.res.scalar.iter().copied().collect(),
|
||||
exports: x.io.exports.iter().map(|e| (e.site, e)).collect(),
|
||||
interps: x.io.interps.iter().map(|(s, i)| (*s, i)).collect(),
|
||||
interps: {
|
||||
let mut m: HashMap<Site, &Interp> = x.io.interps.iter().map(|(s, i)| (*s, i)).collect();
|
||||
// A first half whose result is also read as data (an f16 second half keeps
|
||||
// the other half of its dst, PS_DeferredTextured): evaluated as the pair it
|
||||
// starts. Approximate for those other bits (P0 + i P10 on the console); a
|
||||
// first half only the second half reads is gone before lowering.
|
||||
if std::env::var_os("VICTORY_FIRST_HALF").is_none_or(|v| v != "0") {
|
||||
for (_, i) in &x.io.interps {
|
||||
if let Interp::Interpolated { first, .. } = i {
|
||||
for s in first {
|
||||
m.entry(*s).or_insert(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m
|
||||
},
|
||||
handles: HashMap::new(),
|
||||
strides: HashMap::new(),
|
||||
edges: HashMap::new(),
|
||||
@@ -5679,6 +5695,15 @@ impl<'a> Lower<'a> {
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if std::env::var_os("VICTORY_DESC_WHY").is_some() {
|
||||
eprintln!(
|
||||
"DESC-WHY {:#x} {} scalar={:?} lane_copied={}",
|
||||
inst.machine.offset,
|
||||
inst.name(),
|
||||
address,
|
||||
self.x.res.lane_copied.contains(&site)
|
||||
);
|
||||
}
|
||||
return Err("a descriptor read only as data");
|
||||
};
|
||||
let src = Source::Loaded { load: site, first };
|
||||
@@ -7205,6 +7230,12 @@ impl<'a> Lower<'a> {
|
||||
let r = self.s.m.cast(c, op, a, self.s.t.i32);
|
||||
int(self, r)
|
||||
}
|
||||
"v_cvt_f32_f64" => {
|
||||
let (lo, hi) = self.f64_pair(inst, 0)?;
|
||||
let bits = self.f64_to_f32_bits(lo, hi);
|
||||
let r = self.as_float(bits);
|
||||
float(self, r)
|
||||
}
|
||||
"v_cvt_f32_i32" | "v_cvt_f32_u32" | "v_cvt_f32_ubyte0" | "v_cvt_f32_ubyte1"
|
||||
| "v_cvt_f32_ubyte2" | "v_cvt_f32_ubyte3" => {
|
||||
let mut a = s(self, 0)?;
|
||||
@@ -7222,12 +7253,6 @@ impl<'a> Lower<'a> {
|
||||
let r = self.s.m.cast(c, op, a, self.s.t.f32);
|
||||
float(self, r)
|
||||
}
|
||||
"v_cvt_f32_f64" => {
|
||||
let (lo, hi) = self.f64_pair(inst, 0)?;
|
||||
let bits = self.f64_to_f32_bits(lo, hi);
|
||||
let r = self.as_float(bits);
|
||||
float(self, r)
|
||||
}
|
||||
"v_fma_mix_f32" | "v_fma_mixlo_f16" | "v_fma_mixhi_f16" => {
|
||||
// Each source is f32, or with op_sel_hi the low (op_sel: high)
|
||||
// f16 half; neg_hi is the absolute value here.
|
||||
@@ -7593,6 +7618,14 @@ impl<'a> Lower<'a> {
|
||||
let halves = self.exchange.as_ref().is_some_and(|e| !e.1) && !self.local;
|
||||
let r = if halves && !alike {
|
||||
if !self.all_lanes(self.cur_ir) {
|
||||
// shortcut: `VICTORY_FIRSTLANE_OWN=1` takes the host wave's own
|
||||
// first lane, exact when the value is the same in every lane of
|
||||
// the console wave (the ray-tracing builder's scans read a sum of
|
||||
// scalars this way, inside a loop whose exit is per console wave).
|
||||
static OWN: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
if *OWN.get_or_init(|| std::env::var_os("VICTORY_FIRSTLANE_OWN").is_some_and(|v| v == "1")) {
|
||||
return int(self, mine);
|
||||
}
|
||||
need(self.cur_ir);
|
||||
return Err("a first-lane read where not every lane runs");
|
||||
}
|
||||
@@ -7826,6 +7859,69 @@ impl<'a> Lower<'a> {
|
||||
self.s.m.bin(c, Bin::Or, lo, hi)
|
||||
}
|
||||
|
||||
/// The bits of the float nearest the double whose dwords are `lo` and
|
||||
/// `hi`, on 32-bit integers: round to nearest even, infinities and NaN
|
||||
/// kept (a NaN stays quiet), a result below the smallest normal float
|
||||
/// signed zero (flushed, as the console's default float mode does).
|
||||
// The flush-to-zero form of v_cvt_f32_f64: kept beside the IEEE form in use until the console's
|
||||
// float mode for this instruction is checked.
|
||||
#[allow(dead_code)]
|
||||
fn f64_bits_to_f32_bits(&mut self, lo: V, hi: V) -> V {
|
||||
let c = self.cur;
|
||||
let k = |l: &mut Self, x: u32| l.s.m.i32(x);
|
||||
let (k20, k29, k3, k23) = (k(self, 20), k(self, 29), k(self, 3), k(self, 23));
|
||||
let sign_mask = k(self, 0x8000_0000);
|
||||
let sign = self.s.m.bin(c, Bin::And, hi, sign_mask);
|
||||
let e = self.s.m.bin(c, Bin::LShr, hi, k20);
|
||||
let e_mask = k(self, 0x7ff);
|
||||
let e = self.s.m.bin(c, Bin::And, e, e_mask);
|
||||
// The top 23 bits of the 52-bit mantissa, and the 29 below them.
|
||||
let m_mask = k(self, 0x000f_ffff);
|
||||
let mh = self.s.m.bin(c, Bin::And, hi, m_mask);
|
||||
let mh = self.s.m.bin(c, Bin::Shl, mh, k3);
|
||||
let ml = self.s.m.bin(c, Bin::LShr, lo, k29);
|
||||
let m = self.s.m.bin(c, Bin::Or, mh, ml);
|
||||
let rest_mask = k(self, 0x1fff_ffff);
|
||||
let rest = self.s.m.bin(c, Bin::And, lo, rest_mask);
|
||||
// Round to nearest even: up past half, or at half when odd.
|
||||
let half = k(self, 0x1000_0000);
|
||||
let one = k(self, 1);
|
||||
let above = self.s.m.cmp(c, pred::IUGT, rest, half);
|
||||
let at = self.s.m.cmp(c, pred::EQ, rest, half);
|
||||
let odd = self.s.m.bin(c, Bin::And, m, one);
|
||||
let zero = k(self, 0);
|
||||
let odd = self.s.m.cmp(c, pred::NE, odd, zero);
|
||||
let tie = self.s.m.bin(c, Bin::And, at, odd);
|
||||
let up = self.s.m.bin(c, Bin::Or, above, tie);
|
||||
let up = self.s.m.select(c, up, one, zero);
|
||||
// Rebias the exponent (1023 → 127); a carry out of the mantissa
|
||||
// steps the exponent, up to infinity.
|
||||
let bias = k(self, 1023 - 127);
|
||||
let e32 = self.s.m.bin(c, Bin::Sub, e, bias);
|
||||
let ex = self.s.m.bin(c, Bin::Shl, e32, k23);
|
||||
let normal = self.s.m.bin(c, Bin::Add, ex, m);
|
||||
let normal = self.s.m.bin(c, Bin::Add, normal, up);
|
||||
let normal = self.s.m.bin(c, Bin::Or, normal, sign);
|
||||
// Exponents out of the float's range (signed, after rebiasing).
|
||||
let inf = k(self, 0x7f80_0000);
|
||||
let inf = self.s.m.bin(c, Bin::Or, inf, sign);
|
||||
let k255 = k(self, 255);
|
||||
let big = self.s.m.cmp(c, pred::SGE, e32, k255);
|
||||
let small = self.s.m.cmp(c, pred::SLE, e32, zero);
|
||||
let r = self.s.m.select(c, big, inf, normal);
|
||||
let r = self.s.m.select(c, small, sign, r);
|
||||
// Infinity and NaN (a NaN with any mantissa bit set stays a NaN).
|
||||
let all = k(self, 0x7ff);
|
||||
let special = self.s.m.cmp(c, pred::EQ, e, all);
|
||||
let any = self.s.m.bin(c, Bin::Or, mh, ml);
|
||||
let any = self.s.m.bin(c, Bin::Or, any, rest);
|
||||
let nan = self.s.m.cmp(c, pred::NE, any, zero);
|
||||
let quiet = k(self, 0x0040_0000);
|
||||
let quiet = self.s.m.select(c, nan, quiet, zero);
|
||||
let special_bits = self.s.m.bin(c, Bin::Or, inf, quiet);
|
||||
self.s.m.select(c, special, special_bits, r)
|
||||
}
|
||||
|
||||
/// The low and high dwords of an i64.
|
||||
fn split64(&mut self, x: V) -> (V, V) {
|
||||
let c = self.cur;
|
||||
|
||||
@@ -117,7 +117,7 @@ pub use rect::rect_list_gs;
|
||||
pub use shader::{
|
||||
MEMORY_FAULTS, MEMORY_PAGES, ROOT_CBV_SPACE, SAMPLER_SPACE, SLOT_FAULTS, SLOT_MEMORY, SLOT_STRIDES,
|
||||
SLOT_STRIDES_COUNT, USER_DATA_SPACE, USER_MEMORY_BASE, USER_VIEW_ADDRESS, USER_VIEW_BIAS,
|
||||
set_view_heap_limit,
|
||||
set_view_heap_limit, view_heap_limit,
|
||||
};
|
||||
|
||||
/// The loop limit without an override: 2^20.
|
||||
|
||||
@@ -113,11 +113,11 @@ pub(crate) struct Geometry {
|
||||
pub max_vertices: u32,
|
||||
}
|
||||
|
||||
/// The null slot a host view heap keeps at its end when the title's heap did not fit in it
|
||||
/// (0: no limit). Shaders translated while it is set read that slot for any view index at or
|
||||
/// past it: a host that caps its heap (NVIDIA at 1 000 000 views; a retail game's heap is
|
||||
/// larger) hung its GPU on reads past the end (GPU-based validation: "descriptor heap index
|
||||
/// out of bounds"), which AMD's driver forgives.
|
||||
/// The null slot every host view heap keeps at its end (0: no limit). Shaders translated while
|
||||
/// it is set read that slot for any view index at or past it. A read past a heap's end is
|
||||
/// undefined: a host that caps its heap (NVIDIA at 1 000 000 views; a retail game's heap is
|
||||
/// larger) hung its GPU (GPU-based validation: "descriptor heap index out of bounds"), and AMD
|
||||
/// RDNA3 took a page fault and removed the device.
|
||||
static VIEW_LIMIT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
|
||||
|
||||
/// See [`VIEW_LIMIT`]: `null_slot` is the last slot of the host's view heap, kept null.
|
||||
@@ -125,8 +125,8 @@ pub fn set_view_heap_limit(null_slot: u32) {
|
||||
VIEW_LIMIT.store(null_slot, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// See [`set_view_heap_limit`].
|
||||
pub(super) fn view_heap_limit() -> u32 {
|
||||
/// See [`VIEW_LIMIT`]: the null slot shaders translated now clamp view indices to (0: none).
|
||||
pub fn view_heap_limit() -> u32 {
|
||||
VIEW_LIMIT.load(std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
|
||||
|
||||
@@ -308,6 +308,110 @@ fn packed_add_selects_halves_of_inline_constants() {
|
||||
}
|
||||
}
|
||||
|
||||
/// The float `v_cvt_f32_f64` gives for `x`: Rust's rounding (to nearest
|
||||
/// even), with results below the smallest normal float flushed to signed
|
||||
/// zero as the console's default float mode does.
|
||||
fn cvt_f32_f64_reference(x: f64) -> u32 {
|
||||
let r = x as f32;
|
||||
if r.is_subnormal() || (r == 0.0 && x != 0.0) {
|
||||
r.to_bits() & 0x8000_0000
|
||||
} else {
|
||||
r.to_bits()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "expects results below the smallest normal float flushed to zero; the IEEE lowering (v_cvt_f32_f64_matches_ieee754) is kept until the console's mode is checked"]
|
||||
fn v_cvt_f32_f64_rounds_to_nearest_even() {
|
||||
// v_cvt_f32_f64 v0, v[1:2] (VOP1 opcode 0x0f)
|
||||
let shader = build_valu_cs(&[vop1(0x00f, 0, vgpr(1))]);
|
||||
let tie_even = 1.0f64 + f64::from(f32::EPSILON) / 2.0; // ties to 1.0
|
||||
let tie_odd = 1.0f64 + 1.5 * f64::from(f32::EPSILON); // ties up to 1 + 2 ulp
|
||||
let values = [
|
||||
0.0,
|
||||
-0.0,
|
||||
1.0,
|
||||
-2.5,
|
||||
std::f64::consts::PI,
|
||||
1.0e-3,
|
||||
123_456_789.123,
|
||||
tie_even,
|
||||
tie_odd,
|
||||
1.0 + f64::from(f32::EPSILON) * 0.75,
|
||||
f64::from(f32::MAX),
|
||||
f64::from(f32::MAX) * 1.000_000_1, // rounds to infinity
|
||||
1.0e300,
|
||||
-1.0e300,
|
||||
1.0e-40, // a float denormal: flushed
|
||||
-1.0e-300,
|
||||
f64::from(f32::MIN_POSITIVE),
|
||||
f64::INFINITY,
|
||||
f64::NEG_INFINITY,
|
||||
f64::NAN,
|
||||
];
|
||||
let inputs: Vec<[u32; 4]> = values
|
||||
.iter()
|
||||
.map(|x| {
|
||||
let b = x.to_bits();
|
||||
[b as u32, (b >> 32) as u32, 0, 0]
|
||||
})
|
||||
.collect();
|
||||
run_cases(&shader, &inputs, |backend, i, _, got| {
|
||||
let x = values[i];
|
||||
if x.is_nan() {
|
||||
assert!(f32::from_bits(got).is_nan(), "{backend}: cvt({x}) = {got:#010x}, want NaN");
|
||||
return;
|
||||
}
|
||||
let want = cvt_f32_f64_reference(x);
|
||||
assert_eq!(got, want, "{backend}: cvt({x:e}) = {got:#010x}, want {want:#010x}");
|
||||
});
|
||||
}
|
||||
|
||||
/// The lane `v_permlane16_b32` (`cross` false) or `v_permlanex16_b32` reads
|
||||
/// for `lane`, with selector dwords `s1` (lanes 0-7 of a row) and `s2`.
|
||||
fn permlane_source(lane: u32, s1: u32, s2: u32, cross: bool) -> u32 {
|
||||
let sel = u64::from(s1) | u64::from(s2) << 32;
|
||||
let pick = (sel >> (4 * (lane & 15)) & 15) as u32;
|
||||
let row = if cross {
|
||||
(lane & !31) | ((lane & 16) ^ 16)
|
||||
} else {
|
||||
lane & !15
|
||||
};
|
||||
row | pick
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v_permlane16_and_x16_read_the_lanes_their_selectors_pick() {
|
||||
// 32 lanes, each with its lane number in v0; v1 = permute(v0, s0, s1);
|
||||
// lane `read` of v1 into s12, then into v0 for the store.
|
||||
let selectors: [(u32, u32); 3] = [
|
||||
(0x7654_3210, 0xfedc_ba98), // identity within the row
|
||||
(0x89ab_cdef, 0x0123_4567), // reversed
|
||||
(0x1111_1111, 0xf0f0_f0f0), // repeats
|
||||
];
|
||||
for (op, cross) in [(0x377u16, false), (0x378, true)] {
|
||||
for read in [0u8, 5, 15, 16, 22, 31] {
|
||||
let mut body = Vec::new();
|
||||
body.extend_from_slice(&vop3(op, 1, vgpr(0), sgpr(0), sgpr(1)));
|
||||
// v_readlane_b32 s12, v1, read (inline constant 128 + read)
|
||||
body.extend_from_slice(&vop3(0x360, 12, vgpr(1), 128 + u16::from(read), 0));
|
||||
body.push(vop1(0x001, 0, sgpr(12))); // v_mov_b32 v0, s12
|
||||
let shader = build_cs(&body, 32, 0);
|
||||
let inputs: Vec<[u32; 4]> = selectors.iter().map(|&(a, b)| [a, b, 0, 0]).collect();
|
||||
// Cross-lane code declares WaveSize(32), which WARP's pipeline
|
||||
// creation refuses: hardware only.
|
||||
run_cases_on(&shader, &inputs, &[("hardware", false)], |backend, i, inp, got| {
|
||||
let want = permlane_source(u32::from(read), inp[0], inp[1], cross);
|
||||
assert_eq!(
|
||||
got, want,
|
||||
"{backend}: op {op:#x} selectors {:08x?}, lane {read}",
|
||||
selectors[i]
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v_med3_f32_matches_rdna2_with_nan() {
|
||||
// v_med3_f32 v0, v1, v2, v3 (VOP3 opcode 0x157)
|
||||
|
||||
@@ -446,14 +446,63 @@ fn unpack(blob: &[u8]) -> Result<Vec<u8>, ShaderError> {
|
||||
}
|
||||
let stream =
|
||||
bytes(blob, XBCH_HEADER as u64, packed).ok_or(Malformed("XBCH stream out of bounds"))?;
|
||||
let out = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(stream, unpacked as usize)
|
||||
.map_err(|_| Malformed("XBCH stream does not inflate to its stated size"))?;
|
||||
if out.len() as u64 != unpacked {
|
||||
return Err(Malformed("XBCH stream does not inflate to its stated size"));
|
||||
}
|
||||
let out = inflate_zlib_exact(stream, unpacked as usize)
|
||||
.ok_or(Malformed("XBCH stream does not inflate to its stated size"))?;
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Inflates a zlib stream incrementally up to `expected` bytes, rejecting
|
||||
/// truncated, corrupt, under-filled, or oversized streams without eagerly
|
||||
/// allocating the full declared size up front.
|
||||
fn inflate_zlib_exact(source: &[u8], expected: usize) -> Option<Vec<u8>> {
|
||||
let mut state = zlib_rs::Inflate::new(true, 15);
|
||||
let mut out = Vec::new();
|
||||
let (mut in_pos, mut out_pos) = (0usize, 0usize);
|
||||
loop {
|
||||
if out_pos == expected {
|
||||
let mut extra = [0u8; 1];
|
||||
let status = state
|
||||
.decompress(
|
||||
&source[in_pos..],
|
||||
&mut extra,
|
||||
zlib_rs::InflateFlush::NoFlush,
|
||||
)
|
||||
.ok()?;
|
||||
return (status == zlib_rs::Status::StreamEnd
|
||||
&& state.total_out() as usize == expected)
|
||||
.then_some(out);
|
||||
}
|
||||
if out_pos == out.len() {
|
||||
let next_len = if out.is_empty() {
|
||||
expected.min(source.len().saturating_mul(4).max(4096))
|
||||
} else {
|
||||
out.len().saturating_mul(2).min(expected)
|
||||
};
|
||||
out.resize(next_len, 0);
|
||||
}
|
||||
let status = state
|
||||
.decompress(
|
||||
&source[in_pos..],
|
||||
&mut out[out_pos..],
|
||||
zlib_rs::InflateFlush::NoFlush,
|
||||
)
|
||||
.ok()?;
|
||||
let next_in = state.total_in() as usize;
|
||||
let next_out = state.total_out() as usize;
|
||||
match status {
|
||||
zlib_rs::Status::StreamEnd => {
|
||||
out.truncate(next_out);
|
||||
return (next_out == expected).then_some(out);
|
||||
}
|
||||
zlib_rs::Status::Ok if next_in != in_pos || next_out != out_pos => {
|
||||
in_pos = next_in;
|
||||
out_pos = next_out;
|
||||
}
|
||||
zlib_rs::Status::Ok | zlib_rs::Status::BufError => return None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn programs(fssm: &[u8]) -> Result<Vec<NativeProgram>, ShaderError> {
|
||||
if fssm.get(0..4) != Some(b"FSSM") {
|
||||
return Err(Malformed("native blob lacks FSSM magic"));
|
||||
@@ -729,12 +778,15 @@ mod tests {
|
||||
}
|
||||
|
||||
fn xbch(unpacked: &[u8]) -> Vec<u8> {
|
||||
let stream = miniz_oxide::deflate::compress_to_vec_zlib(unpacked, 6);
|
||||
let mut buf = vec![0u8; zlib_rs::compress_bound(unpacked.len())];
|
||||
let (stream, rc) =
|
||||
zlib_rs::compress_slice(&mut buf, unpacked, zlib_rs::DeflateConfig::new(6));
|
||||
assert_eq!(rc, zlib_rs::ReturnCode::Ok);
|
||||
let mut out = b"XBCH".to_vec();
|
||||
out.extend_from_slice(&XBCH_VERSION.to_le_bytes());
|
||||
out.extend_from_slice(&(stream.len() as u64).to_le_bytes());
|
||||
out.extend_from_slice(&(unpacked.len() as u64).to_le_bytes());
|
||||
out.extend_from_slice(&stream);
|
||||
out.extend_from_slice(stream);
|
||||
out
|
||||
}
|
||||
|
||||
@@ -1020,4 +1072,32 @@ mod tests {
|
||||
bad_record[at..at + 8].copy_from_slice(&u64::MAX.to_le_bytes());
|
||||
assert!(native_programs(&part(&[(KIND_NATIVE, &xbch(&bad_record))])).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_truncated_oversized_and_bad_checksum_xbch_streams() {
|
||||
let plain = fssm(&[(HwStage::Vs, &VS_CODE)]);
|
||||
let good = xbch(&plain);
|
||||
assert_eq!(unpack(&good).unwrap(), plain);
|
||||
|
||||
// Truncated zlib stream (drop last byte, update packed length).
|
||||
let mut truncated = good[..good.len() - 1].to_vec();
|
||||
let new_packed = (truncated.len() - XBCH_HEADER) as u64;
|
||||
truncated[8..16].copy_from_slice(&new_packed.to_le_bytes());
|
||||
assert!(unpack(&truncated).is_err());
|
||||
|
||||
// Stated unpacked size smaller than the inflated output.
|
||||
let mut oversized = good.clone();
|
||||
oversized[16..24].copy_from_slice(&((plain.len() - 1) as u64).to_le_bytes());
|
||||
assert!(unpack(&oversized).is_err());
|
||||
|
||||
// Stated unpacked size larger than the inflated output.
|
||||
let mut underfilled = good.clone();
|
||||
underfilled[16..24].copy_from_slice(&((plain.len() + 1) as u64).to_le_bytes());
|
||||
assert!(unpack(&underfilled).is_err());
|
||||
|
||||
// Corrupt Adler-32 trailer.
|
||||
let mut bad_adler = good.clone();
|
||||
*bad_adler.last_mut().unwrap() ^= 1;
|
||||
assert!(unpack(&bad_adler).is_err());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user