The planner's ruling (T3). On a device with shaderFloat16, 16-bit RoundingModeRTE and 16-bit
SignedZeroInfNanPreserve whose only missing 16-bit control is DenormPreserve, the half operations
narrow::half_controls gates take the 16-bit route: a half + - * / sqrt and fused multiply-add on
the device's 16-bit instructions, half values held at 16 bits, the controlled pair for a float
rounded to half, and a half widened from and narrowed to its bits by the device's conversion.
It is the 16-bit floats query's existing form, read from one more of its properties.
narrow::controlled_modes declares RoundingModeRTE and SignedZeroInfNanPreserve for 16 bits there
and no denormal mode: not DenormPreserve for 16 bits (VUID 06296), nor the 64-bit one the
independence for 32 bits only ties to it (VUID 06289 asks for the same denormal mode at 16 and
64 bits). The independence rules decide which devices take it as before, and a device missing any
other 16-bit control keeps half in float.
MoltenVK on the M3 stays off the route: its denormal modes are not independent at all
(denormBehaviorIndependence NONE), where the form would tie float's denormal mode to half's and
change how the module's float arithmetic reads denormals, which Metal flushes; and its rounding
modes are not independent while it offers no 64-bit RoundingModeRTE, so the 16-bit
RoundingModeRTE the form declares cannot be declared at every width as VUID 06292 asks.
Every module that declares those controls on such a device (narrow::flushes_half_denormals)
records the new static note PrecisionNote::DeviceHalfDenormals, "half denormals may flush to
zero where Metal preserves them": the Vulkan spec leaves a 16-bit denormal to the device in an
entry declaring no 16-bit denormal mode. No other module records it. The conformance suite holds
its code, metal2vulkan-degrade-device-half-denormals, among the native notes beside
device-denormals (the device's own denormals, the one form built there); validation allows it
unbounded, as it does NativeHalfConversion.
Run on lavapipe, whose committed profile has this shape: its half arithmetic moves from float to
16 bits. Turnip on an Adreno GPU before the 8xx series reports the same controls (Mesa 26.3.1,
tu_device.cc lines 534, 1038-1048: shaderFloat16, RTE and SignedZeroInfNanPreserve for 16 bits,
DenormPreserve for 16 bits false below A8XX, independence ALL): not measured on a tile-based
phone GPU; no such device in the lab. No performance claim is made.
Tests: engine_native_half's new test (the red commit before this) now also asks for the note on
each such module and for none under all three controls or without another one. The tests that
held lavapipe as the conversion form now hold it under the controls (engine_native_half's
rounding kinds, memory and decode widenings and controlled modes; engine_half_hold's held values
and division; engine_arith's 16-bit fma profiles), and the conversion form on lavapipe's
description without 16-bit RoundingModeRTE.
SEMANTICS_VERSION 2325 (development holds 2323; 2324 is mad's), and ARTIFACT_FORMAT_VERSION
601 -> 602 (the new note's codec member). Golden rows are blessed at the pin.
Docs: the precision-note catalogue (docs/API.md, the new entry and the conversion entry's
controls), docs/HOWTO.md, the runtime README's shaderFloat16 row and its half fma line, the
NativeHalfConversion note's device list, api_signatures.
The conformance ledger. The form moves lavapipe, whose float controls lack only 16-bit
DenormPreserve, onto the 16-bit route. The conformance replay in the pinned lavapipe image (Mesa
26.2.4, LLVM 23.1.1) then answers 79 of the runtime ledger's llvmpipe rows otherwise:
- 47 now match Metal and are dropped: vertex-format-half4 28 and integer-vertex-attributes 16
(the rows said lavapipe folds the unrounded OpFConvert pair, with no 16-bit DenormPreserve to
make it Metal's; the pair now rounds under the 16-bit controls), and half-texel-nans-rg11b10f's
3 (nearest-sample, read, widened).
- 32 now answer otherwise and are replaced, each with the answer and a reason written from this
run's printout (first differing element):
- vertex-format-half4, 8 rows (Float..Float4, Half..Half4): only the NaN differs now, Metal
0x7fc00000, the device 0xffc00000; one element per component, where the rows recorded the
unrounded values (Float, the largest difference inf) or the kept payload (Half).
- half-texel-nans, 18 rows (nearest-sample, read and widened of r16f, rg16f, rgba16f, r32f,
rg32f, rgba32f): a 16-bit float texel's NaN now narrows to 0x7e00 (the payload dropped, as
read-through-sample already answered; Metal hands back the texel's code); a 32-bit one to
0xfe00 (Metal 0x7e00); the widening answers 0xffc00000 (Metal 0x7fc00000). Six rows count more
elements than before (the 16f nearest-sample and read rows: 215 -> 472, 428 -> 940,
852 -> 1874), twelve fewer.
- bicubic-texel-classes, 6 rows: fewer elements at every site, each count and largest
difference the one the 780M and RTX 5080 rows record; within the bicubic note's bound.
None of the 79 is a half denormal.
docs/CONFORMANCE-COVERAGE.md regenerated (scripts/conformance-coverage/coverage.py): ledger rows
7932 -> 7885. The image is the pinned one, so the ledger's llvmpipe line does not move.
Not replayed, a follow-up: the six bicubic-texel-classes rows of venue "llvmpipe (LLVM 23.1.4,
256 bits)", a lavapipe outside the pinned image; this commit leaves them as they were.
Verified: the replay of this tree without the ledger edit, job m3-max-80972 (pinned lavapipe
image): exit 1 with row differs 32, now answers otherwise 32, now matches 47; the rows are written
from its printout. The same counts in job m3-max-80871 (the pre-rebase fix, against its base,
which exits 0). coverage.py --check: as derived.
No lab card's translation moves. The golden blesses at development (15898e612e) and at this
change, over the committed profiles plus the RX 7900 XTX's vulkaninfo --json capture (job
m3-max-80916), compared cell for cell, job m3-max-81006 (macOS, every fixture, 3624 rows): every
column but lavapipe, gate-lavapipe and gate-lavapipe-x86_64 (471 cells each) is equal, among them
the XTX capture, amd-780m-radv, amd-780m-hawkpoint-radv, intel-irisxe-rplp-anv,
intel-irisxe-anv and moltenvk-m3; no profile fingerprint moves (job m3-max-80958, the
same over the 1910 rows a Linux host assembles, agrees). Not run on an RTX 3090: its column is
owed until the card is back.
Gate at this tree, job m3-max-80990: clippy -D warnings plain, under m2v_cpu_icd and under m2v_gpu
0; rustdoc -D warnings 0, and with private items for metal2vulkan 0; the runtime's suite on
lavapipe 945 passed, 4 skipped (whole suite); translator and AIR 3415 passed, 37 skipped (the
golden hash table, the fixture inventory and the engine printouts excluded: their bless is due at
the pin); validation 1319 passed, 3 skipped; conformance 1048 passed. CI's lavapipe steps in the
pinned image, job m3-max-81012: device tests 841 passed (843 listed), the conformance replay exit 0
(the 4640 gaps the ledger expects, exactly), public case qualification exit 0.
11 KiB
Integrating metal2vulkan
The checklist around the library: the tools each piece needs, caching, failures, isolating a compile, host memory a driver refuses, and checking an integration. The API itself is API.md; the runtime's is its README.
1. Tools
The libraries read AIR bitcode (a metallib, a bitcode wrapper or a raw module) and run no external
tool. The command line (feature cli) runs two, both optional:
llvm-as(LLVM 21 or newer) to assemble an LLVM text input (.ll). Not needed for a metallib or bitcode.spirv-val(SPIRV-Tools) to check each module it writes. When it is missing the tool says so and still writes the modules, which the library has verified itself.
Put them on PATH, or name each one:
export METAL2VULKAN_LLVM_AS=/path/to/llvm-as
export METAL2VULKAN_SPIRV_VAL=/path/to/spirv-val
The runtime opens the Vulkan loader when it makes a device (or takes the consumer's
vkGetInstanceProcAddr when it adopts one). Nothing links against Vulkan at build time.
2. Compile
From the command line, see the README. From Rust, API.md walks the flow in the order a consumer meets it:
- A device: describe the Vulkan device as created and choose a policy.
- A library: parse a metallib or AIR bitcode.
- A compute pipeline or
a render pipeline, under a
Budget. - The interface: the layout, what each slot binds, what a dispatch or draw needs.
- At bind time: a dispatch's regions, the levels a slot reads, and indirect command buffers.
Every air.simd_* operation keeps Metal's 32-lane simdgroup; there is no option to use the
driver's subgroup width instead.
A half computed in float rounds through the device's conversion to 16 bits and back, Metal's
half(float) under the float controls the entry declares, where the driver keeps that pair (ANV,
RADV). Elsewhere it rounds through the device's own conversion, or its OpQuantizeToF16 alone
without shaderFloat16 (which may flush a value below the smallest normal half), and the
pipeline records NativeHalfConversion or NativeHalfQuantize. NVIDIA's driver (615.71.09) folds the pair even under the float controls, so its
rounding there is the device's conversion, or none; its pipelines still declare the float controls
and compute a half +, -, *, / and sqrt with the device's 16-bit instructions, as ANV's
and RADV's do. RADV's compiler fuses a float addition, subtraction, multiply or fused
multiply-add into the conversion of its result and rounds the exact result once. On a device
Device::from_queries made (metal2vulkan-runtime), which reads no driver identity, every device with
the float controls takes the pair, NVIDIA's included, and every pipeline that rounds through it
records NativeHalfConversion, as a driver's compiler may fold the pair or fuse the operation so.
A device with shaderFloat16 and the 16-bit RoundingModeRTE and SignedZeroInfNanPreserve
controls but not the 16-bit DenormPreserve (lavapipe) computes half the same way under those
two, and every pipeline that declares them there records DeviceHalfDenormals: a half denormal
may flush to zero. A half read from memory or from its bits is the device's widening on every device (an
OpFConvert of the bits read as a 16-bit float with shaderFloat16, GLSL.std.450 UnpackHalf2x16 without it), and a half stored or reinterpreted as bits, unless they are the
bits it was loaded from, the device's narrowing (an OpFConvert to 16 bits, GLSL.std.450 PackHalf2x16 without shaderFloat16); the pipeline records NativeNanEncoding (a NaN's bits)
and, where that conversion is not under the float controls, NativeHalfConversion (a denormal the
device may flush).
To read AIR without translating it (list a metallib's functions, decode a module into typed IR,
read its entry points' bindings, canonicalize an entry under function-constant values), depend on
metal2vulkan-air. Use the same version as metal2vulkan, which
pins it exactly; the translator's input errors are that crate's types.
3. Cache
The translator keeps no cache; it gives you keys and encodings to keep one (API.md section 7):
- A key is a SHA-256 of the inputs,
cache::SEMANTICS_VERSIONand the device's profile fingerprint, known before compiling. - A decode checks the format and semantics versions, the key, the profile fingerprint and a
checksum. Every
ArtifactErroris a cache miss: compile again and replace the entry. - The versions move independently: the crate's (the API),
SEMANTICS_VERSION(the output; a bump misses every cached artifact),ARTIFACT_FORMAT_VERSION(the encoding) andRUNTIME_VERSION(what the runtime does with an unchanged artifact).
The runtime's pipeline cache
The runtime keeps its lowered functions and its pipeline cache in the directory
DeviceOptions::pipeline_cache_dir names, from one device to the next; with none, every boot
lowers every function and compiles every pipeline cold (its README, "The cache
directory").
4. Handle failures
Every failure is a typed value; match variants, never messages (API.md section 8).
- A refusal names what would change the answer: the feature the device lacks, the pipeline input the compile needs, or a limit.
- A deadline or a cancellation (
Error::Budget,Error::Cancelled) is transient: retry later. Every other error is the answer for that input and device and may be cached as one (error.retry()). - Never use a module from a compile that returned an error.
- The command line prints
metal2vulkan: FALLBACK (<why>), exits 2, and writes a repro bundle (the input, the profile, the descriptor files,repro.sh,failure.txt) under$TMPDIR/metal2vulkan-repros, orMETAL2VULKAN_REPRO_DIR. Its directory is named by its content (metal2vulkan-repro-<16 hex digits>), so the same failure writes it once;repro.shreplays the failure from the bundle alone, withmetal2vulkanonPATH.
5. Bound a compile
Every compile runs in-process under the caller's Budget: a wall deadline (Budget::standard()
is 20 seconds), a CancelToken any thread may trigger, a deterministic work-unit cap and a byte
ledger charged before each input-sized allocation. A panic inside a compile becomes
Error::Internal. That is enough for a consumer that trusts its process.
When an abort, a stack overflow or a failed allocation in the compiler must not take the consumer down, or a hard memory ceiling must hold, compile in a child process:
- Spawn one worker per compile in its own process group, with the input in a file and the result
written to a file. Send the library bytes, the entry and descriptor, and the device description
and policy (with feature
serdethey serialize; equal description and policy give the same profile fingerprint and keys). - In the child, install a counting global allocator that refuses allocations above a budget, build
the
Device, and compile with the sameBudget. - In the parent, poll the deadline, cancellation and the process group's resident memory. On a breach, kill the whole group and remove its scratch files. A result that arrives late is a timeout.
- Load the result with
PipelineArtifact::from_bytesagainst the parent's ownDevice, and check that its key is the one the parent computed (Device::compute_pipeline_key,render_pipeline_key). - Classify as in-process: a typed error is the answer; a deadline, cancellation or killed child is transient.
The validation crate's worker (crates/metal2vulkan-validation/src/worker/) implements this,
under 20 seconds and 500 MiB per attempt.
6. Host memory a driver refuses
The runtime imports a no-copy buffer's memory as a block, and the consumer places the buffer in
it: host memory by its address (Device::import_host_memory) or, on Linux, a dma-buf
(Device::import_dma_buf) or several (Device::import_dma_bufs). Which to try first, and what to
do where they refuse, is the consumer's; every refusal comes before any allocation is attempted,
with nothing imported (its README, "Imported blocks").
7. Memory another API writes
A block the consumer exports (an opaque fd or a dma-buf on a Unix host, an NT handle on Windows) is the memory another API writes in place, and an exported surface's planes are textures over its own exported memory; the contract for each is in the runtime's README, "Exported blocks" and "Exported surfaces".
8. Check the integration
spirv-val checks structural validity, not agreement with Metal:
spirv-val --target-env vulkan1.2 output.spv
device.profile().spirv_validation() names the environments and relaxations a module compiled
for the device is valid under; the command line runs each.
For a claim about behavior, follow VALIDATION.md: a synthetic regression test, the golden output hashes where output must not move, and an authored case run on Metal and Vulkan where it should.
A host note: RADV under Ubuntu 26.04's stock kernel 7.0.0-38 with linux-firmware 20260319 sometimes loses a GPU write into a newly made buffer under heavy multi-process GPU memory churn (about 1 in 10^4 to 10^5, with no error); kernel 7.2.6 with linux-firmware 20260916 loses none.
A host note: on an RTX 3090 behind a Thunderbolt 3 bridge (NVIDIA 620.70.06, Linux 7.3), several
processes making devices with extra compute and video decode queues at once now and then meet an
error the kernel logs on one of their channels: Xid 69 (a 3D class error at
SET_REPORT_SEMAPHORE_D, offset 0x1b0c, whose data word is a method header, so the GPU read the
stream one word short), Xid 32 (an invalid pushbuffer entry) or Xid 13 (a compute class exception).
Those processes' devices are lost, and a submission of another process's may never complete.
A host note: on a laptop whose discrete GPU the system suspends when idle (NVIDIA's runtime D3:
the PCI device's power/runtime_status reads suspended), the first driver call that needs the
GPU blocks the thread that makes it while the GPU resumes. On one RTX 5080 Laptop GPU (driver
615.71.09, Linux 7.2), each call made after 25 seconds idle with the GPU suspended: an empty
command buffer's vkQueueSubmit held its thread 982 ms, a host write to device-local host-visible
memory 986 ms, a device-local vkAllocateMemory 988 ms, and a command pool's reset and the
recording of 8192 dispatches 987 ms. A program that must not stall can keep the GPU from
suspending with GPU work of its own, at its own power cost, or make such calls from a thread it can
afford to block.
A host note: drop the runtime's devices before the process exits; what a device kept to the exit meets on NVIDIA's driver is in the runtime's README.