A float ldexp records no NativeNanEncoding: its lowering answers Metal's NaN itself

The 11-02 carve-out run flagged ldexp as possibly over-noted. A float
ldexp is lowered on the bits (math::ldexp_bits): an infinity or a NaN
operand is selected as the result bit for bit, which is Metal's answer
(NanAnswer::Operand: ldexp returns a NaN operand itself). No NaN of the
device's reaches the result on any device, yet float_call left the
answer to the device and recorded NativeNanEncoding everywhere,
MoltenVK included.

FloatFn::metal_nans now includes ldexp on float lanes, so the call
leaves no NaN answer and records no NaN note. A half ldexp multiplies by
2^k, so its NaN is still the device's product's where Metal answers the
canonical half NaN, and stays noted where the device does not answer
that. PrecisionNote::NativeNanEncoding's docs and docs/API.md's
catalogue say so.

Verified: lower::arith::tests::float_ldexp_answers_metals_nan_itself
(fails without the metal_nans change: call.nan is Some(Operand) and
NativeNanEncoding is recorded). SEMANTICS_VERSION 2014: a module
calling float ldexp records one note fewer.

(cherry picked from commit c4b748c1e63cee2a32400145a3665387a8ca72cf)
This commit is contained in:
Anees Iqbal committed 2026-11-10 02:59:23 -08:00
1 parent b72b34e36f
commit 5bb3d04838
4 files changed
+65 -5

No files matched your search

+6 -2
View File
@@ -1150,15 +1150,19 @@ impl FloatFn {
/// Whether the lowering on lanes of format `fmt` records no NaN note of its own: `saturate`,
/// which answers a number for one, `copysign`, which moves bits, `sign`, whose NaN answer is
/// its instruction's
/// ([`FloatFn::native_instruction`]), and the device's picks, `median3` and, on `float` and
/// ([`FloatFn::native_instruction`]), the device's picks, `median3` and, on `float` and
/// `half`, `fmin`, `fmax`, `min3`, `max3`, and `clamp`, which return an operand (two NaNs'
/// answer is `PrecisionNote::NativeMinMax`'s where the device is not measured to pick
/// Metal's).
/// Metal's), and `ldexp` on `float`, whose lowering on the bits answers a NaN operand itself,
/// bit for bit, as Metal does ([`ldexp_bits`]; `nan::NanAnswer::Operand`) on every device.
pub fn metal_nans(self, fmt: FloatFmt) -> bool {
use FloatFn as F;
if matches!(self, F::Min | F::Max | F::Clamp | F::Min3 | F::Max3) {
return metal_picks(Some(fmt));
}
if self == F::Ldexp {
return fmt == FloatFmt::F32;
}
matches!(
self,
F::Sign | F::Saturate | F::Median3 | F::FastMedian3 | F::CopySign
@@ -2525,6 +2525,59 @@ const KHR_FACTS: super::FloatFacts = super::FloatFacts {
nan_moves: false,
};
/// `ldexp` on `float` is lowered on the bits ([`math::ldexp_bits`]), which answer a NaN operand
/// itself, bit for bit, as Metal does (`NanAnswer::Operand`): no NaN of the device's reaches the
/// result, so the call records no `NativeNanEncoding` and leaves no NaN answer to the device, on a
/// device of either NaN encoding. A `half` `ldexp` multiplies, so its NaN is the device's product's
/// where Metal answers its canonical `half` NaN, and stays noted on a device not measured to answer
/// that.
#[test]
fn float_ldexp_answers_metals_nan_itself() {
use super::nan::NanAnswer as A;
use crate::air::ir::FastMath;
use crate::device::profile::NanEncoding;
use crate::pipeline::artifact::PrecisionNote;
let form = |fmt| Form {
held: Held::Float(fmt),
lanes: 1,
};
assert!(FloatFn::Ldexp.metal_nans(FloatFmt::F32));
assert!(!FloatFn::Ldexp.metal_nans(FloatFmt::Half));
let every = FastMath::ALL
.iter()
.fold(FastMath::NONE, |a, (b, _)| a.union(*b));
for nans in [NanEncoding::Native, NanEncoding::MetalCanonical] {
let facts = super::FloatFacts { nans, ..KHR_FACTS };
for fmf in [FastMath::NONE, every] {
let call = super::float_call(FloatFn::Ldexp, false, fmf, form(FloatFmt::F32), facts)
.expect("float ldexp");
assert_eq!(call.nan, None, "{nans:?} {fmf:?}");
let noted = (call.notes.always.iter())
.chain(&call.notes.unless_metal_fast)
.any(|n| matches!(n, PrecisionNote::NativeNanEncoding));
assert!(!noted, "{nans:?} {fmf:?}: {:?}", call.notes);
}
let half = super::float_call(
FloatFn::Ldexp,
false,
FastMath::NONE,
form(FloatFmt::Half),
facts,
)
.expect("half ldexp");
assert_eq!(half.nan, Some(A::Canonical), "{nans:?}");
if nans == NanEncoding::Native {
assert!(
half.notes
.always
.contains(&PrecisionNote::NativeNanEncoding),
"{:?}",
half.notes
);
}
}
}
/// Precise `pow` on `float` leaves its NaNs to the device (the device's `Pow`, and the device's sum
/// of NaN operands), where Metal's NaN is measured: the canonical NaN, but a NaN base to the
/// exponent one, which is the base itself. It records `NativeNanEncoding` as fast `pow` and `atan2`
+4 -2
View File
@@ -1945,10 +1945,12 @@ pub enum PrecisionNote {
/// Metal answers a NaN the device answers a NaN (the module keeps
/// `SignedZeroInfNanPreserve` where Metal's entry keeps NaNs), its sign and payload the
/// device's own (llvmpipe: `0xffc00000`), where Metal answers its canonical `0x7fc00000`
/// (`0x7e00` as `half`), an operand's bits (`round`, `ldexp`, `fast::tan`, and precise `pow`
/// (`0x7e00` as `half`), an operand's bits (`round`, `fast::tan`, and precise `pow`
/// of a NaN base to the exponent one), or the canonical
/// NaN with an operand's sign (`asin`, `atan`, `asinh`, `atanh`); a `float` `fneg` and `fabs`
/// keep a NaN operand's payload on Metal, and a `half` `fract` returns it. Every non-NaN result
/// keep a NaN operand's payload on Metal, and a `half` `fract` returns it. Not recorded for a
/// `float` `ldexp`, which the module computes on the bits and so answers a NaN operand itself,
/// as Metal does, on every device. Every non-NaN result
/// is as without it. Also recorded by every module that reads a `half` from its bits (a load, a
/// reinterpretation, a texel's bits), on every device: the read is the device's widening (an
/// `OpFConvert` of the bits read as a 16-bit float with `shaderFloat16`, `UnpackHalf2x16`
+2 -1
View File
@@ -4165,7 +4165,8 @@ bound.
Metal's canonical one on a device measured to answer that, MoltenVK on an Apple GPU): a `float`
`fneg` and `fabs`, which keep a NaN operand's payload on Metal, a `half` `fract`, which returns
it, and a precise `float` `pow` of a NaN base to the exponent one, which returns the base, among
them. Also
them. A `float` `ldexp`, which the module computes on the bits, returns a NaN operand itself as
Metal does, on every device, and records nothing. Also
recorded wherever a `half` is read from its bits, on every device, since the read is the
device's widening (its conversion with `shaderFloat16`, `UnpackHalf2x16` without): a NaN's bits
the device's, where Metal widens every `half` NaN to `0x7fc00000` and moves one it does not