Skip to content

feat(agc): use native f16 arithmetic on Apple GPUs - #234

Merged
utkuhalis merged 1 commit into
AnyTogether:mainfrom
hedibravofox:feat/agc-moltenvk-native-f16
Dec 4, 2026
Merged

utkuhalis merged 1 commit into
AnyTogether:mainfrom
hedibravofox:feat/agc-moltenvk-native-f16

Conversation

@hedibravofox

Copy link
Copy Markdown

What

On macOS, every f16 instruction is emulated with 32-bit arithmetic. The driver only uses native f16 when shaderDenormPreserveFloat16 is reported, and MoltenVK doesn't report it. In Grand Theft Auto V, one full-screen compute shader with a lot of f16 maths grows to 1.4 million SPIR-V words.

Apple GPUs do keep f16 denormals. On an M5 Pro, Metal's half add, subtract, multiply, fma and conversions returned the correctly rounded result for about 16 million random inputs each, denormals included.

  • VulkanDevice enables native f16 on MoltenVK with an Apple GPU. No denormal preserve mode is declared for it, since the device doesn't report one.
  • MoltenVK lowers an exact multiply to fma(a, b, +0), which turns a -0 product into +0. On fusedExactArithmetic targets, f16 multiplies now use GLSL Fma with -0, as f32 multiplies already do.
  • The f16 tests agc_driver_float16_native and agc_driver_packed_float16_native run on MoltenVK instead of skipping.
  • A debt entry records that only the M5 Pro was measured.

Tested

macOS 27.2.1, Apple M5 Pro, MoltenVK 1.4.2, x86-64 build under Rosetta 2 (AppleClang 21.0.0, Release):

Emulated f16 Native f16
GPU time of that shader's dispatch about 98 ms about 27 ms
Median frame 173 ms 87 ms
  • These are observations from one scene, not a speed claim: I have no tools/trace_digest.py summaries of it.
  • Not run: the VK_LAYER_KHRONOS_validation comparison from AGENTS.md, because no validation layer is installed on this Mac. Other Apple GPUs weren't tested.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M5 Pro, MoltenVK 1.4.2, macOS 27.2.1
  • AI-assisted: yes

🤖 Generated with Claude Code

MoltenVK reports no f16 denormal preserve mode, so every f16 instruction
was emulated with 32-bit arithmetic there. Apple GPUs keep f16 denormals
and round correctly, so the driver now uses native f16 on them. MoltenVK
lowers an exact multiply to a fused multiply-add of +0, which loses the
sign of a zero product, so f16 multiplies add -0 through GLSL Fma, as f32
ones already do there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@utkuhalis

Copy link
Copy Markdown
Collaborator

Thanks, a big win for that shader. I checked how the gate holds up:

  • fusedExactArithmetic is true for every MoltenVK device, but Float16 only goes into the capability list when float16Rte holds. On MoltenVK that now needs the Apple vendor ID (when the driver reports no f16 denormal preservation). Every native-f16 path (nativeFloat16, NativeF16Rte, the probe, the two tests) also needs Float16, so another GPU under MoltenVK still gets the emulated path.
  • fma(a, b, -0) rounds once, like the multiply, and keeps the product's sign for a zero product (+0 + -0 = +0, -0 + -0 = -0). That is the same lowering the f32 multiply already uses on these targets.
  • Old shader cache entries aren't reused for the native path: the cache key digests the capability list and includes float16DenormPreserve, and both change on Apple GPUs.
  • The debt entry says only an M5 Pro was measured, which is fair. There is no switch to turn native f16 off, so if someone on M1 to M4 reports f16 output that looks wrong, the first follow-up would be an APS5_NO_* switch for this gate.

I'll merge once CI is green; I'll rerun the macOS job if it's cancelled for lack of a runner.

@utkuhalis
utkuhalis merged commit b4cf328 into AnyTogether:main Dec 4, 2026
18 of 20 checks passed
@utkuhalis

Copy link
Copy Markdown
Collaborator

Merged, thanks! Native f16 now runs on Apple GPUs under MoltenVK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants