Files
metal2vulkan/scripts/regen-spirv-grammar
Anees Iqbal 6b322fb50f Cut the cold making of half-heavy shaders on ANV and RADV: controlled half rounding and the device's half widening
On Intel's ANV, CoreAnimation's A2Xghfc fragment shader (313k words) took
about 14 s to make cold, for each of three states. The consumer's x86 macOS
guest stalled with "no completion in 10 s" on its first boot. Native Metal
makes it in 0.48-0.56 s on the M3. brw (Mesa's Intel backend) spends that
time in its register allocator. Its interference build is quadratic in
instruction count, it spills, and it retries several scheduling modes. Most
of its input was our half arithmetic. Two cuts, each the cheaper form, are
taken on ANV and RADV.

1. A half result computed in float rounds through the device's conversion
   pair under the float controls (QuantizeForm::Controlled). It replaces
   OpQuantizeToF16 plus a denormal arm, about 11 NIR operations per lane.
   - The form: an OpFConvert to 16 bits and back, each decorated
     NoContraction, in an entry that declares RoundingModeRTE,
     DenormPreserve and SignedZeroInfNanPreserve for 16 bits, plus the
     modes the device's independence ties to them (ANV: RTE for 32 and 64
     bits; RADV: RTE and DenormPreserve for 64 bits). The modes are
     declared post-lowering (new ExecMode kinds), and the requirements
     carry the FloatControl properties.
   - Exact by the Vulkan spec under either HalfRoundingPolicy: conversions
     are correctly rounded, and NoContraction forbids folding the pair. No
     fact, no note.
   - A new DriverWorkaround, HalfConversionPairFolded, keyed on NVIDIA's
     proprietary driver, keeps NVIDIA on its previous forms. NVIDIA
     615.71.09 on the RTX 3090 folds the pair even under NoContraction and
     every mode combination, and the PackHalf2x16 and u16-bitcast forms
     fold the same way: 2,231,288,832 of 2^32 narrowings unrounded. A
     plain-Vulkan repro gives 16,775,680 of 2^24 unrounded on the 3090 and
     0 on ANV.
2. A half read from its bits (a load, a reinterpretation, a texel's bits) is
   the device's widening under NanMinMaxPolicy::Native, the default: each
   u32 lane is bitcast to f16x2 and converted to 32 bits, in place of a
   12-op integer decode. A half texel takes that conversion's NaN (no keep).
   - Every non-NaN half is exact by the spec: DenormPreserve covers
     "values obtained via unpacking an integer into a vector of values
     with smaller bit width".
   - A NaN's bits are the device's, recorded as NativeNanEncoding
     (native-nan-encoding), now also announced on ANV and RADV pipelines
     that decode halves. A half stored back unchanged keeps its bits.
   - NanMinMaxPolicy::Exact keeps the integer decode and the keep. There,
     every half widened to float answers Metal's 0x7fc00000 for a NaN, as
     the M3 does over every pattern from memory, constant memory, selects
     and texels.

Where brw's time went, measured on A2X (a2x-s96): after cut 1, 49.6k NIR
operations, of which the rounding pairs were about 10.7k, half decodes from
memory 8.6k and texel NaN keeps 2.6k. The rest was below 5% each.

Exactness probes:
- Cut 1 (candidate half(x) over all 2^32 f32, and half + - * over every
  pair): 0 non-NaN mismatches on ANV Iris Xe (Mesa 26.2.4), RADV 780M
  (Mesa 26.2.4), and the 780M and RX 7900 XTX (Mesa 26.0.8).
- Cut 2 (every half pattern): 0 non-NaN differences on ANV and RADV.
- The M3 confirms Metal equals the integer reference everywhere.

Cold making (A2X, base against each cut):
- ANV Iris Xe: 13.6-14.3 s, then 5.75-6.16 s, then 4.06-4.36 s. Peak RSS
  1.28 GB, then 0.50, then 0.35. GPU time per draw 4.62 -> 4.48 ms.
- RADV 780M: 0.80-0.84 s, then 0.53-0.55 s, then 0.38-0.47 s. GPU time per
  draw 4.05 -> 3.43 ms after cut 1, then within its clock states' spread.
In the consumer's cold boots on ms01 (Iris Xe), A2X went from 14 s per
state, plus two 38 s remakes, to 4.0-4.3 s. The suite's pipeline making went
from 126 s to 16.7 s, with no stall. Repeat boots show metalwin unaffected:
its frame rate follows other GPU work at session start, not this change.

Tests:
- engine_native_half: the controlled conversion, and a half read from memory
  under both NaN settings, NaNs stored back.
- device::tests::profile::nvidia_carries_the_folded_half_conversion_pair.
- lower::arith::tests: narrow, round_texel, half_texel, texel_round.
- spirv::requirements; spirv::builder::ops.
- Updated expectations: engine_arith half rounding, engine_spirv_cross_forms
  fma, engine_metal_routines frexp (the device's NaN under the default,
  Metal's under Exact).
- The validation lanes driver_deviations and native_maths expect the
  controlled form.

Docs: the FAST-PATHS half-rounding row (scope) and a new row for the
widening; the NativeHalfConversion and NativeNanEncoding entries in API.md and
their rustdoc; HalfRoundingPolicy and NanMinMaxPolicy rustdoc; HOWTO's driver
note; REFUSALS regenerated. A known gap is added to the texel-NaN row: on
ANV a 16-bit float NaN texel kept as half reads back 0xFE00 from ANV's image
decode, even under Exact (announced as device-nan-encoding). SEMANTICS_VERSION
1759, runtime pin recomputed, RUNTIME_VERSION unchanged.

Tables: the golden hashes, 274-389 cells on each ANV and RADV column across
both cuts, and NVIDIA's profile fingerprints, whose words are unchanged. The
authored compile census: artifact hashes on the 780M and RPL-P ANV columns,
with no status change. No MoltenVK, lavapipe or floor cell moves.

Verified on development-fast (batches f18-f24): every lane green on the RTX
3090, ANV Iris Xe, RX 7900 XTX, both 780Ms, lavapipe and the M3 Max, plus the
CPU suites, case qualification, clippy in every configuration, docs and
packaging. Not run on the RTX 5080 Laptop (host offline; whether its driver
keeps the pair is unmeasured).
2026-10-17 11:14:51 +03:00
..

regen-spirv-grammar

Generates the translator's SPIR-V grammar items from Khronos's machine-readable SPIR-V grammar (SPIRV-Headers, MIT-licensed) with our own generator, generate.py. Nothing else feeds it.

Output Role
crates/metal2vulkan/src/spv.rs The opcodes (Op), enumerants (Capability, Decoration, BuiltIn, ...), masks and GlslStd450Op the translator names, with their grammar values
crates/metal2vulkan/src/spirv_binary/table.rs Test only: every core instruction's operands and every enumerant's parameters, which the test parser (spirv_binary/) reads

What spv.rs holds

Only what items.txt lists: each kind the code names, the values it names, and from_u32 where the code converts words back. Values that only tests name are cfg(test). The compiler checks the list: a value the code names that is missing fails to build, and a listed value nothing uses is dead code where rustc can see it. When code needs a new opcode or enumerant, add it to items.txt and regenerate.

Names follow the grammar's, with a few changes Rust's and Clippy's naming rules need (see variant_name and const_name in generate.py): an opcode drops its Op prefix, leading digits move to the end (Dim::D2), a name of four or more capitals only is written with one capital (Capability::Fmakhr), SpecConstantOp is SpecConstantOperation, and a mask's bits are capitalized consts (FPFastMathMode::NOT_NAN). Debug always prints the grammar's own name.

Revision

SPIRV-Headers tag vulkan-sdk-1.4.341.0 (SPIR-V 1.6 revision 4; GLSL.std.450 version 100 revision 2). HEADERS_REF=<tag> selects another. Every generated file's header names the source, its revision and Khronos's MIT notice.

Run

./scripts/regen-spirv-grammar/regen-spirv-grammar.sh
cargo nextest run -p metal2vulkan --all-features

The script fetches spirv.core.grammar.json, extinst.glsl.std.450.grammar.json and LICENSE once per tag into .cache/spirv-headers/<tag>/ (gitignored; METAL2VULKAN_GRAMMAR_CACHE names another directory), runs generate.py and formats both outputs with rustfmt. It needs curl, Python 3 and rustfmt, and network access for a tag not yet fetched.