perf(shader): compute scalar f16 add, sub and mul with native f16 math #3557
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3557"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-11T13:11:09Z.
What
v_add_f16,v_sub_f16,v_subrev_f16andv_mul_f16now use native f16 math. The conditions are those the packed instructions use: the device has native f16 with round to nearest even and kept denormals, andFLOAT_MODEkeeps f16 denormals and rounds f16 to nearest even (or the shader has noFLOAT_MODE). Each instruction becomes one f16 add or mul on the source bits; sub adds the negated subtrahend. TheFP16_OVFLand clamp rules then run on the result bits. Other f16 modes keep the f32 path, including the denormal flushes from #2987 and the directed rounding from #3012.It uses the native f16 operations, the
nativeFloat16translation option andEmitNativeF16Modesfrom #3531, which has merged; the branch is rebased ontomain.Background. These instructions converted both sources to f32, computed in f32 and converted the result back to f16. NVIDIA (from Turing on), Intel and AMD GPUs execute f16 add and multiply directly.
v_fma_f16,v_fmac_f16,v_fmamk_f16andv_fmaak_f16keep their exact f32 path (#2000): Vulkan gives GLSLFmaonly the precision ofOpFMulfollowed byOpFAdd, so a native f16Fmamay round twice.OpFmaKHR(VK_KHR_shader_fma,shaderFmaFloat16) would allow it; none of the drivers I can test exposes it yet.Before / after on an RTX 4060 Laptop (Ada, driver 591.86). SASS instructions per instruction come from
APS5_PIPELINE_STATSbinary size over a chain of 16 instructions,FLOAT_MODE0xc0:v_add_f16v_sub_f16v_subrev_f16v_mul_f16All other opcodes in the sweep compile to the same size.
Runtime on the same GPU, one build: each instruction is compiled once for the device and once for the same target without
Float16(the f32 pathmaintakes), as a dependent chain of 2048 instructions over 16384 groups of 32 threads,FLOAT_MODE0xc0; median of 15 interleaved dispatches. Av_fma_f32chain takes 6.7 ms.v_add_f16v_mul_f16Why results don't change.
OpFAddandOpFMulto be correctly rounded; the module declares round to nearest even and preserved denormals for f16, andNoContractionkeeps the driver from fusing them.FP16_OVFLturns an infinite result from finite sources into ±0x7bff, asclampF16Overflowdoes in f32.clampF16Bits, which matchesapplyF16ResultModifiers: clamping commutes with round to nearest because 0 and 1 are representable, and withDX10_CLAMPa NaN clamps to +0.docs/dev/TechnicalDebt.mdalready lists the NaN payloads of scalar f16 arithmetic as the driver's, not the hardware's input propagation, and this doesn't change that. The payload now comes from the driver's f16 arithmetic instead of its f32 arithmetic.Where it matters. Shaders with scalar f16 math. Devices without native f16, and shaders whose
FLOAT_MODEflushes f16 denormals or rounds f16 another way, are unchanged.Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release, rebased onto
mainatbab7b328(with #4021'sSocket.cppfix applied locally, whichmainhas since merged). RTX 4060 Laptop (driver 591.86), Intel UHD Graphics (Raptor Lake, driver 101.6790) and lavapipe from Mesa 25.2.8 (the version CI installs; Windows build,GALLIUM_OVERRIDE_CPU_CAPS=avx).agc_driver_float16_native:Float16, and compares every result.v_add_f16,v_sub_f16,v_subrev_f16andv_mul_f16(VOP2, and VOP3 with abs, neg and clamp),v_sub_f16with an inline constant, and the fma forms (v_fma_f16withop_sel, abs, neg and clamp,v_fmac_f16,v_fmamk_f16with a literal,v_fmaak_f16with a denormal literal), which must keep the f32 path.FLOAT_MODE, every combination ofDX10_CLAMP,IEEE_MODEandFP16_OVFLat 0xc0, and 0x30 (f16 denormals flushed), where it checks that the f32 path is kept.Fma.f16 native: skipped, the device has no native f16 with RTE rounding and kept denormals, the formtools/check_skips.pyaccepts as a device limitation, asagc_driver_packed_float16_nativehas it.FP16_OVFLfix (v_mul_f16gives 0xfc00 where the f32 path gives 0xfbff), and leaving out the clamp (v_add_f16 ... clampgives 0x81f4 where the f32 path gives 0).fp16_*and SDWA tests (18) pass on all three devices.agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 338 tests). I left out the BDA tests that hang on driver 591.86 until #3291 lands.agc_driver_prepared_shaders_registered_workgroup_sync(added by #2261), which fails the same way on a build without this change.agc_driver_graphicsfailed once during the suite and passes on its own.agc_driver_ds_swizzleandagc_driver_f32_dot2_rounding(noFloat64; it throws instead of skipping), which fail the same way withmain's recompiler.agc_driver_depth_fast_clear_submitsegfault that also happens withmain's recompiler.mainc7601b797, with the skip message reworded (see above):agc_shader_memory/agc_shader_disk_cache(28) pass on all three devices or are skipped for a missing feature.tools/check_skips.pyon lavapipe's ctest JUnit file accepts all three skips,agc_driver_float16_nativeincluded. The old wording doesn't match its pattern, which is what failed the Linux job.agc_driver_ds_swizzlefails, as withmain's recompiler. On lavapipe 25.2.8 there are two segfaults: the intermittentagc_driver_depth_fast_clear_submit, andagc_driver_depth_storage_dispatch(new onmain,738ae9df0), which segfaults 4 of 5 times on its own and has no f16 code.Limits.
agc_driver_float16_nativepasses 3 of 3, and every non-NaN result is identical; 15,592 NaN results carry another payload. The same four tests fail with and without this change on that machine.op_selon the VOP3 forms ofv_add_f16,v_sub_f16,v_subrev_f16andv_mul_f16, as before; abs, neg and clamp on them go through the native path.v_ldexp_f16and the f16 transcendentals still compute in f32.v_min_f16andv_max_f16pick their result on the bits since #3590.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T13:12:18Z.
The PR body says this is stacked on #3531 and that only the last commit is new, but this PR currently targets
main, so its review diff includes #3531's entire packed-f16 implementation and test alongside the scalar-f16 changes. #3531 is still open as a separate PR. Could you set this PR's base toperf/shader-native-packed-f16(or retarget/rebase it after #3531 merges) so the review diff contains only the scalar follow-up? That keeps the dependency explicit and avoids presenting the packed changes twice for review/merge.Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-11T13:55:20Z.
Thanks.
perf/shader-native-packed-f16only exists in my fork, and a pull request here can only target a branch of this repository, so I can't set it as the base. Until #3531 merges, the scalar change on its own is the last commit, 16485b18. Once #3531 is in, I'll rebase this ontomainso the diff here is only that commit. If you'd rather not review it until then, I can mark it as a draft.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T13:56:56Z.
Thanks for clarifying. Since this repository cannot target your fork branch, please mark #3557 as a draft until #3531 merges, then rebase onto
mainso the review diff is limited to the scalar follow-up. I’ll review the scalar change once that diff is isolated.Imported from boykopovar/AnyPS5. Originally posted by jweidenh on 2026-10-11T16:13:17Z.
Additional Windows 11 / RTX 5090 correctness validation, with a small fix to the two new test scanners.
The tested integration
0911e71ais pinned maina11c8f7fplus Karan Vora’s existing packed-f16 commit from #3531 (317d23615) and scalar-f16 commit from this PR (16485b180), followed by the two-file scanner fix14841ce2. The implementation commits retain their original authorship, and range-diff confirms their patches are unchanged. The scanner fix is based directly on this PR’s head and can be picked independently.Both
FindF16x2Mathcopies readspirv[offset + 1]for every instruction after discovering the f16 vector type. A final one-wordOpFunctionEndtherefore reads past the span. The fix checks the opcode and minimum word count before reading operands. In a local harness using the exact scanner bodies, both originals report an ASan heap-buffer-overflow on this terminal-instruction case; both fixed scanners pass 23 boundary, arithmetic-classification and malformed-input cases each with ASan/UBSan. That check used macOS arm64, AppleClang 21,-O0 -fsanitize=address,undefined; it was a scanner harness, not a full-repository sanitizer run.Windows validation used WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON, SPIRV-Tools enabled, NVIDIA driver 617.42, and serial CTest with a 30-second per-test limit.ANYPS5_REQUIRE_VULKAN=1,ANYPS5_GPU=RTX 5090andANYPS5_NO_SHADER_CACHE=1were set; pipeline statistics were disabled. Each GPU test logged the selected NVIDIA GeForce RTX 5090 (vendor=0x10de,device=0x2b85).agc_driver_float16_nativeagc_driver_packed_float16_nativeBoth tests reached their completion markers without capability skips. Their structural checks also passed: native f16 vector add/multiply were emitted, the emulated target contained no such arithmetic, and f16 FMA remained excluded. The existing rounding, denormal, modifiers, overflow, FMA, SDWA, min/max, dot-product, conversion and interpolation checks in the selected scope passed too.
All 24 selected tests passed across the initial run and one isolated retest, with no skips. The initial serial run passed 23 tests in 33.91 seconds;
agc_masked_selectdid not start and CTest reportedProcess not started,[unknown error], andBAD_COMMAND. An unchanged isolated CTest rerun ofagc_masked_selectthen passed in 0.02 seconds. The initial launch failure is unexplained; no source or test change was made for the retest. Both raw logs are retained. This was 21 GPU tests and three host tests, not the full repository suite.These results validate the exercised native/emulated math outputs on Blackwell. No title, FPS, throughput or pipeline-compilation improvement was measured. The durations above are correctness-test runtimes.
AI-assisted: Codex prepared the scanner fix, review and validation harnesses; tests ran on the contributor’s machines.
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-11T16:44:24Z.
@MarcZX100 marked as draft. I'll rebase it onto
mainonce #3531 merges, so the diff is only the scalar change.@jweidenh thanks for the RTX 5090 run, and for catching the scanner reading past the last instruction. I took your fix as is, split between the two PRs with you as the author: the
Float16Native.cpphalf is 6b67a8e6 here and thePackedFloat16Native.cpphalf is f139b34b in #3531. With both, the two tests still pass on an RTX 4060 (327,680 results and 294,912 halves) and an Intel UHD.On performance: the descriptions give instruction counts, so here are runtimes as well. RTX 4060 Laptop, driver 591.86, one build. Each instruction is compiled once for the device and once for the same target without
Float16, which is the f32 pathmaintakes. Dependent chains of 2048 instructions over 16384 groups of 32 threads,FLOAT_MODE0xc0, median of 15 interleaved dispatches:main)v_add_f16v_mul_f16v_pk_add_f16(#3531)v_pk_mul_f16(#3531)v_fma_f32(reference)These are isolated chains. How much a title gains depends on how much of its GPU time goes to f16 arithmetic, and I have no title here to measure.
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-11T23:50:08Z.
@MarcZX100 rebased onto
mainnow that #3531 has merged, so the diff is only the scalar change (plus @jweidenh's scanner fix for its test), and it's out of draft. Retested on the RTX 4060, the Intel UHD and lavapipe; the results are in the description.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T05:02:27Z.
The full build fails on Linux:
agc_driver_float16_nativeskips on llvmpipe and the skip check stops on it (error: agc_driver_float16_native skipped). Please add it to the expected skips intools/check_skips.pywith its reason, or make it run there. Windows passed.Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-13T21:55:06Z.
Thanks @oneandonlydean. The skip itself is expected: lavapipe has no native f16 with RTE rounding and kept denormals. The check rejects it because of how the message is worded.
check_skips.pyacceptsskipped, ... the device has no ..., butFloat16Nativeprintsthe device has no ..., skipped.mainalready rewordedPackedFloat16Nativethis way, so I'll rewordFloat16Nativethe same and rebase.Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-14T16:36:10Z.
An AMD run, since the description has none: the native path is taken on RDNA2 and its results match.
Setup: AMD Radeon RX 6700 XT (gfx1031,
0x73df, subgroup 64), Adrenalin 26.8.1 (driver 32.0.21045.5002), Windows 11 26200, WinLibs GCC 15.2.0 r7, Release,BUILD_TESTING=ON,ANYPS5_REQUIRE_VULKAN=1. Head7237ec215merged intomaine34c47921.agc_driver_float16_nativeis not skipped, so the driver reports native f16 with RTE and kept denormals, and it passes 3/3:f16 native tests passed (327680 results, 15592 NaN results with another payload). Every non-NaN result is identical; 15,592 NaN results carry another payload, against none on the RTX 4060 and 604 on the Intel UHD in the description.ctest --parallel 4 --timeout 120:main1198 tests, this PR 1199. The same four fail on both:agc_driver_pixel_interlock,agc_driver_layered_volume_vertex_no_gplandagc_driver_lds_mixed_width_atomics, which fail on AMD Windows (#5271), andguest_sce_net, which times out on this machine's slow host DNS lookups.Not tested: speed on AMD, and any title.
AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-15T02:08:12Z.
@oneandonlydean, fixed in
05a2999a2.Float16Nativenow printsf16 native: skipped, the device has no native f16 with RTE rounding and kept denormals, the wordingPackedFloat16Nativeuses andtools/check_skips.pyaccepts. The branch is rebased ontomainc7601b797, and the description has the rerun on the RTX 4060, Intel UHD and lavapipe.@thuanquang, thanks for the RX 6700 XT run. I've added it to the description.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:05:49Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/shader/recompiler/Translation/include/Translation/TranslationContext.hppcore/shader/recompiler/Translation/src/FloatInstructions.cppcore/shader/recompiler/Translation/src/FloatInstructions.cppcore/shader/recompiler/Translation/include/Translation/TranslationContext.hppImported from boykopovar/AnyPS5. Originally posted by odntht on 2026-10-16T19:59:51Z.
A Gen9 Linux run, since the Intel data is Raptor Lake on Windows.
Setup: Intel HD Graphics 520 (Skylake GT2, Gen9), Mesa 25.2.8 ANV, Ubuntu 24.04 (container), kernel 6.12.111, GCC 13.3, Release. Head
05a2999a2merged ontomainf474fb776(clean).ANYPS5_GPU="HD Graphics 520"; the log shows it selected.agc_driver_float16_nativetakes the native path and passes 3 of 3:327680 results, 246 NaN results with another payload. All non-NaN results are identical. The NaN count is the same in every run (604 on the Raptor Lake UHD in the description).float16tests (agc_float16_conversion,agc_driver_float16_*,agc_driver_packed_float16_native) pass 3 of 3.AI-assisted: yes (Claude Code). The runs are on my machine.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T00:41:23Z.
The Gen9 run (@odntht, HD 520, ANV, 3/3) and the RDNA2 run (@thuanquang, RX 6700 XT, 3/3) fill the gaps the description had: the native path is taken and matches on Gen9, RDNA2, Raptor Lake and the RTX 4060. The reworded skip message in
05a2999a2uses the same wording asPackedFloat16Native, which is whattools/check_skips.pyaccepts, so the 10-13 point should be gone; the full build on this head will confirm it.