Half reads: a zero of either sign, or 2^-14, as NativeHalfQuantize's bound says; the conversion note's flush too

The RX 550 run of e5553fa2a0 (potluck 11647) found two answers the first change held too tightly:

- tile_normalized: RG16Snorm's code 0xffff (-1/32767, -3.05e-5) read as +0. NativeHalfQuantize's
  recorded bound is a zero of either sign for a value below 2^-14, or 2^-14 from just below it
  (NIR's fquantize2f16 flushes every one); flushed() now takes exactly that.
- ycbcr: read_half's stage on the RX 550 does not quantize: it narrows by the device's conversion,
  recording NativeHalfConversion, whose bound lets a half denormal read as a zero on a device that
  may flush 16-bit denormals (no DenormPreserve for 16 bits, as there). Kernels::flushing now holds
  the half kernels recording either note, and prints each half kernel's notes (HALF-KERNEL).

(cherry picked from commit 6c80165070a97febfb95f5ec0a4fedf63204fb58)
This commit is contained in:
Anees Iqbal committed 2026-11-09 05:12:12 -08:00
1 parent 8c19e8bf97
commit 19276dd2a5
3 files changed
+45 -38

No files matched your search

@@ -33,8 +33,8 @@
//! and the transfer's `Pow`, `1/8190 + 2^-14`), a `half` one within one more `half` step, a code
//! within one (a NaN channel's the device's). Where the function rounds a `half` by the device's
//! `OpQuantizeToF16` (`PrecisionNote::NativeHalfQuantize`, on a device without `shaderFloat16`),
//! a value below the smallest normal `half` may read as the zero of its sign, Vulkan's choice
//! for the quantize, which the device makes ([`flushed`]).
//! a value below the smallest normal `half` may read as a zero of either sign, or `2^-14` from
//! just below it, as the note's bound lets the device answer ([`flushed`]).
//!
//! A format the device does not render to, or whose staging view it cannot store to, is refused
//! typed (`Unsupported`) and said.
@@ -194,16 +194,18 @@ fn conversion_bound(channel: Channel, want: f32) -> f32 {
}
/// Whether `have`, a `channel` read through a `half` the device's `OpQuantizeToF16` rounded
/// (`PrecisionNote::NativeHalfQuantize`), is the zero of the sign of Metal's `want` that the
/// quantize may give a value below the smallest normal `half` (`2^-14`): the device's `float`
/// value, within [`conversion_bound`] of `want`, may lie below it.
/// (`PrecisionNote::NativeHalfQuantize`), is what the note's bound lets the quantize give a value
/// below the smallest normal `half` (`2^-14`): a zero of either sign, or `2^-14` of its own sign
/// from just below it, where the device's `float` value, within [`conversion_bound`] of Metal's
/// `want`, may lie below `2^-14`.
fn flushed(channel: Option<Channel>, have: f32, want: f32) -> bool {
let Some(channel) = channel else {
return false;
};
have == 0.0
&& have.is_sign_negative() == want.is_sign_negative()
&& want.abs() - conversion_bound(channel, want) < f32::powi(2.0, -14)
let smallest = f32::powi(2.0, -14);
let below = want.abs() - conversion_bound(channel, want) < smallest;
let normal = have.abs() == smallest && have.is_sign_negative() == want.is_sign_negative();
below && (have == 0.0 || normal)
}
/// Whether code `a` of `channel` lies within one of code `b` (signed codes as their values).
@@ -690,8 +692,8 @@ fn a_normalized_attachment_reads_through_halves_as_metal_does() {
assert_eq!(after, texels, "{name}: {entry} over {:?} keeps", format.pf);
if quantized {
println!(
"HALF-QUANTIZE {name}: {entry} over {:?}: {zeros} channels read the zero \
of a value below 2^-14",
"HALF-QUANTIZE {name}: {entry} over {:?}: {zeros} channels read a zero \
or 2^-14 for a value below 2^-14",
format.pf
);
}
@@ -82,14 +82,14 @@ fn encoded(v: f64) -> f64 {
/// `want` ([`vulkan_read`] of each texel) within [`DEVICE_BOUND`], an `_sRGB` twin's through the
/// inverse of the curve, alpha 1. A converted format's texels, which its image stores as
/// `half`s, and `half` reads also take the `half`'s rounding of each value, within one unit in
/// the last place (`PrecisionNote::NativeHalfConversion`). A `half` read whose stage rounds by
/// the device's `OpQuantizeToF16` (`quantized`, `PrecisionNote::NativeHalfQuantize`) may read a
/// value below the smallest normal `half` as the zero of its sign. The largest distance, in units
/// of `1/2048`, is printed.
/// the last place (`PrecisionNote::NativeHalfConversion`). A `half` read whose stage rounds or
/// narrows by the device's own form (`flushed`: `PrecisionNote::NativeHalfQuantize` or
/// `NativeHalfConversion`) may read a value below the smallest normal `half` as a zero. The largest
/// distance, in units of `1/2048`, is printed.
pub(super) fn assert_device_reads(
texels: &[u8],
want: &[[f64; 3]],
(output, half, quantized): (YCbCrOutput, bool, bool),
(output, half, flushed): (YCbCrOutput, bool, bool),
case: &str,
) {
assert_ne!(
@@ -112,11 +112,9 @@ pub(super) fn assert_device_reads(
YCbCrOutput::Srgb => encoded(v),
_ => v,
};
let (low, high) = match quantized && got == 0.0 {
// The quantize's zero: the value it rounded lies below the smallest normal
// `half`, of the zero's sign.
true if got.is_sign_negative() => (-(2f64.powi(-14)), 0.0),
true => (0.0, 2f64.powi(-14)),
let (low, high) = match flushed && got == 0.0 {
// The device's zero: the value it rounded lies below the smallest normal `half`.
true => (-(2f64.powi(-14)), 2f64.powi(-14)),
false => (got - ulp, got + ulp),
};
let (low, high) = (decode(low), decode(high));
@@ -467,10 +467,11 @@ struct Kernels {
/// YCbCr texel's step on the grid `8192` with ties away from zero).
native_half: bool,
/// The `half` kernels (`read_half`, `sample_half`, `gather_red_half`, `gather_blue_half`) whose
/// stage rounds a `half` by the device's `OpQuantizeToF16` (`PrecisionNote::NativeHalfQuantize`,
/// on a device without `shaderFloat16`): a value below the smallest normal `half` may come out
/// as the zero of its sign.
quantizing: Vec<&'static str>,
/// stage rounds or narrows a `half` by the device's own form: `PrecisionNote::NativeHalfQuantize`
/// (its `OpQuantizeToF16`, on a device without `shaderFloat16`) or `NativeHalfConversion` (its
/// conversion, or its `PackHalf2x16` and `UnpackHalf2x16`), whose bounds let a value below the
/// smallest normal `half` come out as a zero.
flushing: Vec<&'static str>,
/// Whether `write_masked_half`'s stage records `PrecisionNote::NativeNanEncoding`, announced
/// as its state is made: not where the device's moves of a `half` NaN are Metal's
/// (`DriverDeviation::MetalNanMoves`) and its write's store accounts for the texel's NaN.
@@ -514,7 +515,7 @@ impl Kernels {
]
.iter()
.any(|note| stage_notes(device, &bitcode, "read_half").contains(note)),
quantizing: [
flushing: [
"read_half",
"sample_half",
"gather_red_half",
@@ -522,7 +523,14 @@ impl Kernels {
]
.into_iter()
.filter(|entry| {
stage_notes(device, &bitcode, entry).contains(&PrecisionNote::NativeHalfQuantize)
let notes = stage_notes(device, &bitcode, entry);
println!("HALF-KERNEL {}: {entry} records {notes:?}", device.name());
notes.iter().any(|note| {
matches!(
note,
PrecisionNote::NativeHalfQuantize | PrecisionNote::NativeHalfConversion
)
})
})
.collect(),
half_write_nans: stage_notes(device, &bitcode, "write_masked_half")
@@ -530,10 +538,10 @@ impl Kernels {
}
}
/// Whether the `half` kernel `entry`'s stage rounds by the device's quantize
/// ([`Kernels::quantizing`]).
fn quantizes(&self, entry: &str) -> bool {
self.quantizing.contains(&entry)
/// Whether the `half` kernel `entry`'s stage may give a value below the smallest normal `half`
/// as a zero ([`Kernels::flushing`]).
fn flushes(&self, entry: &str) -> bool {
self.flushing.contains(&entry)
}
}
@@ -4058,8 +4066,8 @@ fn srgb_twins_read_what_metal_reads() {
match (by_device, half) {
(true, _) => {
let entry = if slot { "sample_half" } else { "read_half" };
let quantized = half && kernels.quantizes(entry);
let read = (codes.output, half, quantized);
let flushed = half && kernels.flushes(entry);
let read = (codes.output, half, flushed);
assert_device_reads(texels, &vulkan, read, &what)
}
(false, true) if slot => {
@@ -4784,8 +4792,8 @@ fn assert_family_read(
match (by_device, half) {
(true, _) => {
let want = family_vulkan(bytes, codes, conversion);
let quantized = half && kernels.quantizes("read_half");
assert_device_reads(texels, &want, (codes.output, half, quantized), case);
let flushed = half && kernels.flushes("read_half");
assert_device_reads(texels, &want, (codes.output, half, flushed), case);
}
(false, true) => {
let want = family_modelled(bytes, codes, conversion);
@@ -5202,11 +5210,10 @@ fn ycbcr_half_gathers_round_ties_away() {
v => v.log2().floor() as i32 - 10,
};
let off = (f64::from(h) - nearest).abs();
// Or the zero of its sign the device's quantize gave a value below
// the smallest normal `half` (`PrecisionNote::NativeHalfQuantize`).
let zero = kernels.quantizes(&format!("gather_{channel}_half"))
// Or a zero the device's rounding gave a value below the smallest normal
// `half` (`Kernels::flushing`).
let zero = kernels.flushes(&format!("gather_{channel}_half"))
&& h == 0.0
&& h.is_sign_negative() == f.is_sign_negative()
&& nearest.abs() < 2f64.powi(-14);
assert!(
off <= 2f64.powi(exponent) || zero,