perf(shader): convert f16 operands with UnpackHalf2x16 where f16 denormals are kept #3988
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3988"
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-11T20:43:52Z.
What
On devices that can keep f16 denormals, f16 operands are converted to f32 with
UnpackHalf2x16again, instead of the integer decode #1463 introduced. Results are unchanged on every device, NaN payloads included.Background.
58f596ee8) replacedUnpackHalf2x16inEmitF16BitsToF32with an integer decode of sign, exponent and mantissa. The reason was an AMD Windows driver that folds the mask of a byte-selected (SDWABYTE_n) half into the conversion and converts the unmasked register.EmitF16BitsToF32runs for every f16 operand read (IRConvertF32F16), f16 image and format data, and f16 interpolation, on every GPU. On NVIDIA the integer decode costs 8 to 12 instructions more per read thanUnpackHalf2x16.UnpackHalf2x16alone isn't exact everywhere:DenormPreservefor 16-bit floats,SPV_KHR_float_controlslets a driver flush subnormal halves. None of the three drivers here does, but nothing stops one.0x7ffffffffor every NaN half (OpFConvertfrom a real f16 does the same), and the Intel UHD quiets them (0x7c01→0x7fc02000). The integer decode keeps sign and payload (0x7f802000).How.
float16DenormPreserve, set when the device reportsshaderDenormPreserveFloat16anddenormBehaviorIndependenceisALL: the same rulefloat64DenormPreserveuses sinceebaa687d3. Like that flag, it is serialized (request version 17) and part of the cache key.EmitF16BitsToF32usesUnpackHalf2x16and declaresDenormPreserve 16.SPV_KHR_float_controlsrequires that mode to keep "denormalized values obtained via unpacking an integer". The mode is declared once per module, shared with the native f32 → f16 conversion of #1252.((int)(half << 16) >> 3) | 0x7f800000puts the sign at bit 31 and the payload at bits 22:13, which is what the integer decode returns.32_BIT_ONLYbecause AMD sets f16 and f64 denormals with one register field.BitFieldUExtract/SExtractof fewer than 16 bits under theConvertF32F16, which is how SDWA byte selects arrive) keep the integer decode on every device. #1463's case can't come back even if AMD's Windows driver reportsALL; I couldn't check what it reports.Which GPUs.
shaderDenormPreserveFloat16on Pascal through Blackwell, Pascal included even though it has noshaderFloat16, andINDEPENDENCE_ALL(NVK reportsALLfor every generation it supports). The Intel UHD here reports both too.agc_driver_f16_to_f32_unpackpasses with both decodes:32_BIT_ONLY, so it keeps the integer decode, and the test passes there with "integer decode only".Before / after on an RTX 4060 Laptop (Ada, driver 591.86). SASS instructions per instruction from
APS5_PIPELINE_STATSbinary size over a chain of 16,FLOAT_MODE0xc0:mainUnpackHalf2x16without the NaN rebuildv_cvt_f32_f16v_cmp_*_f16v_add/sub/mul_f16v_fma_f16v_max/min_f16v_med3_f16v_pk_add/mul_f16v_pk_fma_f16v_fma_mix_f32(f16 sources)v_cvt_u16_f16The f16 unary instructions (
v_floor/ceil/trunc/rndne/fract/rcp/sqrt/rsq/exp/log/sin/cos_f16) each lose 8. 55 opcodes change; every other opcode in the sweep compiles to the same size.Run time, dependent chains over 16,384 groups of 32 lanes, median of 15 interleaved dispatches in one process, flag off vs on:
v_cvt_f32_f16× 2048v_fma_mix_f32× 2048v_max_f16× 2048v_pk_add_f16× 2048v_mul_f16× 2048v_add_f16× 2048v_pk_fma_f16× 1024Why results don't change.
DenormPreserve 16,UnpackHalf2x16returns that exact value for every non-NaN half, subnormals included, and like the integer decode it reads only the low 16 bits.sign << 31 | 0x7f800000 | payload << 13, the integer decode's special-value branch.main.Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release,
mainata11c8f7f+ #3291's fix (b54fe623) so that the BDA tests don't hang on driver 591.86. RTX 4060 Laptop (driver 591.86), Intel UHD Graphics (Raptor Lake, driver 101.6790), lavapipe from Mesa 25.2.8 (the version CI installs) and 26.2.4 (Windows builds,GALLIUM_OVERRIDE_CPU_CAPS=avx).agc_driver_f16_to_f32_unpack:v_cvt_f32_f16, and the byte-selected half of each lane throughv_add_f16_sdwa ... src0_sel:BYTE_1with −0.agc_driver_f16_to_f32_unpack_spirv(no GPU needed, so CI runs it). With the flag, thev_cvt_f32_f16program declaresDenormPreserve 16and has oneUnpackHalf2x16. Thev_add_f16SDWA program has one too, for its whole-registerv7, while the byte-selectedv4is decoded with integers. Without the flag, neither program has either.agc_driver_f16_to_f32_unpackfails on the RTX 4060 (v_cvt_f32_f16 of 0xd9a47c01 is 0x7fffffff, expected 0x7f802000) and on the Intel UHD (is 0x7fc02000).DenormPreserve 16mode,agc_driver_f16_to_f32_unpack_spirvfails (v_cvt_f32_f16 lacks DenormPreserve 16 with float16DenormPreserve=true). The device test still passes on both GPUs, as neither driver flushes unpacked subnormal halves without the mode.UnpackHalf2x16makesagc_driver_f16_to_f32_unpack_spirvfail with oneUnpackHalf2x16more than expected.agc_shader_memoryandagc_shader_disk_cachecheck the flag's serialization, the version 17 prefix, legacy requests and the cache key.agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 325 tests) ona11c8f7f+ b54fe623:agc_driver_emulated_color_compare(#3526, fixed onmainsince by #3532).agc_driver_ds_swizzle,agc_driver_emulated_color_compare,agc_driver_f32_dot2_rounding(noFloat64; it throws instead of skipping) andagc_driver_vop1_float_unary(fract_f321 ulp off). The color compare is #3526; the other three fail the same way withmain's recompiler.agc_driver_emulated_color_compare,agc_driver_image_plane_address_on_line_implicit_lod(fixed onmainsince by #3922) and, on 25.2.8, the intermittentagc_driver_depth_fast_clear_submitsegfault.main0a8b1d32(this branch): the new tests,agc_shader_memory,agc_shader_disk_cacheand the f16, SDWA, VOPC compare table, interpolation and mix tests (29) pass on the RTX 4060 and lavapipe 25.2.8, and on the Intel UHD all pass or are skipped exceptagc_driver_vop1_float_unary, as above.Limits.
readF16AsF32, on top of this conversion, so the two don't conflict.agc_shader_memoryassertions; whichever lands later takes the next version.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-11T21:02:45Z.
Tested and verified on Linux (CachyOS x86_64) with an NVIDIA GeForce RTX 5060 Ti (Blackwell, driver 615.78.08, Vulkan 1.4):
agc_driver_f16_to_f32_unpack: PASSED (0.45s)agc_driver_f16_to_f32_unpack_spirv: PASSED (0.00s)agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 325 tests total): 100% passed (41.57s).tools/check_conventions.pypasses clean.Confirms support and correct behavior on Blackwell architecture and Linux. Great work! LGTM.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-11T21:42:54Z.
Review: COMMENTED
Verification of the new tests on two devices, and one correction to the merge-order note
What I built and ran. Your head
977116c3in a clean worktree, Release withANYPS5_ENABLE_SPIRV_TOOLS=ON, GCC 16. Ran on lavapipe (Mesa 26.2.4, LLVM 23.1.1 — the CI device) and on an RTX 3080 Ti (Ampere — a generation your body says you could not test).The one line worth putting in the body is the 3080 Ti's: "both decodes" means the device reported
float16DenormPreserve, so theUnpackHalf2x16path ran with its NaN rebuild and every one of the 65,536 halves was compared against the exact decode, payloads included. That is the half you could not run yourself, and it holds on a second NVIDIA generation.Negative control (mine). One token: force
EmitF16BitsToF32to always takeEmitF16BitsToF32Decoded, tests kept.That is the same failure your own DenormPreserve-off check reports — so this arm reproduces your control from an independent driver of the flag rather than adding a new failure mode. Worth saying plainly: what it establishes is that the SPIR-V test bites when the mode is dropped, not that it caught something new. In the same arm the device test still passes on lavapipe, which is correct rather than a gap: with the flag false the two forms are the same code there.
One correction: the version collision is a different PR. The body says "Request version 17 is also what #2440 used; whichever lands second takes 18." #2440 is closed and unmerged (2026-10-09) and never took 17 — its diff bumps the version 13 → 14, while its own body says 16.
mainis at 16 (version < 1u || version > 16u), so 17 is free today — but #1077 takes 17 as well, with the identical assertion stringNVNQQREAAAA=. Naming #1077 instead would point a reviewer at the PR that can actually collide.Series. #3557 is stacked on #3531 (its body says so: "Only the last commit is new"), and #4011 contains this PR's commit; #3590 merged earlier today. So one line in each body naming which of the others it overlaps would save a reviewer reconstructing the order — and #3531 was re-pushed during the day, so the shas a reader sees in an older review may no longer match.
What I could not verify. No AMD GPU here and no NVIDIA generation other than Ampere; and I verified equivalence, not your instruction counts — no
APS5_PIPELINE_STATSbefore/after, so the 8–12 instructions per read and the runtime medians are your measurements, quoted rather than reproduced.None of the above is a blocker on the devices I ran. The two things I could not close are the AMD gap (yours to state) and the version renumbering (#1077).
Hermes Agent 🤖 at your service — AI assistance: the build, the runs and this write-up were produced by an AI agent on the contributor's machine.
Imported from boykopovar/AnyPS5. Originally posted by TideOPL on 2026-10-11T21:57:33Z.
I read through this and tested it on Windows with an NVIDIA card and AMD's Windows driver, since the description mentions both as open points.
Code
(int)(bits << 16) >> 3puts the sign at bit 31 (its copies at bits 30 to 28 fall under0x7f800000), and the mantissa at bits 22:13. The result issign << 31 | 0x7f800000 | m << 13, the same as the integer decode.EmitF16DenormPreservealso stops a module that uses both from declaringDenormPreserve 16twice.NarrowF16Fieldonly catchesBitFieldU/SExtractwith a count under 16. A narrow half that reaches the conversion another way (aBitwiseAnd 0xff, or through a Phi) would still useUnpackHalf2x16on a device reportingINDEPENDENCE_ALL. As far as I can tell, no current AMD driver reports that (see below), so this is a note rather than a blocker.AMD Windows driver (the open question from #1463)
The integrated Radeon Graphics of a Ryzen 7 7800X3D (RDNA2, gfx1036, AMD proprietary driver 23.20.44) reports
denormBehaviorIndependence = SHADER_FLOAT_CONTROLS_INDEPENDENCE_32_BIT_ONLY(shaderDenormPreserveFloat16 = true). Sofloat16DenormPreservestays off there, and AMD on Windows keeps the integer decode.agc_driver_f16_to_f32_unpackpasses on it with "integer decode only", including the SDWABYTE_1case. The RTX 5070 below reportsINDEPENDENCE_ALL, so it takes the new path.Tested by reviewer
Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release. This PR merged onto
mainc5da8233a: currentmaindoesn't build on Windows right now (HostPollinSocket.cppis only defined under#ifndef _WIN32, from3ae258380).agc_driver_f16_to_f32_unpackagc_driver_f16_to_f32_unpack_spirvagc_shader_memory,agc_shader_disk_cache(33)mainwithout this PR*main: intro, then title menumain: title menu; the log ends with the same null call insideamdvlk64.dllas onmainmain: studio logos, then the existing wave64v_readlane_b32lane-63 stopmain: logos, then the existing packed pixel ancillary input stop* On that AMD driver, on
maintoo:agc_driver_div_fixup_f16,float16_dot,float16_fma_rounding,float16_rounding,float16_ternary_clamp,interpolation_f16andlds_mixed_width_atomics. They're unrelated to this PR; I'll raise them separately.This adds Blackwell to the Ada results in the description. I didn't measure performance: neither game gets far enough on
mainfor a meaningful A/B, and an iGPU says nothing about speed. Not run: Linux, discrete AMD, other NVIDIA generations.Looks good to me.
AI-assisted: yes. Written with AI assistance (Claude Code), with the tests run on my machine; I reviewed it.
Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-11T23:25:56Z.
Thanks @TideOPL, @Dextroid17 and @Kikoarl. Ampere and two Blackwell cards are now in the description.
@TideOPL, the AMD Windows run settles the open point from #1463: amdvlk reports
32_BIT_ONLY, so AMD keeps the integer decode.NarrowF16Fieldonly matters if some driver later reportsALL. Agreed on its limit: aBitwiseAnd 0xffor a Phi of byte fields would get past it. Translation produces SDWA byte selects only asBitFieldUExtract/SExtracttoday, so I'd leave it narrow unless you'd rather I widen it.@Dextroid17 you're right, #2440 never reached 17. #1077 and #4031 both take 17 and change the same
agc_shader_memoryassertions; the description now names them.Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-12T13:01:32Z.
Rebase handoff — rebased onto current
main, authorship unchanged.8 files conflicted, all the same shape:
mainlanded the registration-modules/fragment-descriptor-set plumbing through the recompiler's request/serializer/cache-key structures while this PR threadsfloat16DenormPreservethrough the same structures. The resolution keeps both — every field/serialization/cache-key entry from main plus this PR's f16 denorm field. The serializer tests keep both round-trip checks, and the legacy-trailer test now asserts both new flags unset.Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release,
BUILD_TESTING=ON, AMD RX 9070 XT / RADV):tools/check_conventions.py --base origin/mainpassesTo pick it up:
Head is
9b269a26d.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T10:28:47Z.
Checked on RADV and CI's lavapipe, plus the HW oracle (gfx1035):
v_cvt_f32_f16over all 65,536 halves with IEEE=0 and f16 denormals kept equals the integer decode bit for bit, NaN payloads included, so results are unchanged.mainconflicted in the request/target plumbing (registration modules, vertex subgroups, request version 20), so I rebased:float16DenormPreserveis request version 21 now, and theagc_shader_memoryassertions follow (version prefix, truncation range, unsupported version 22). Build and the agc tests pass, including the newagc_driver_f16_to_f32_unpack*and the f16/SDWA/compare tests on CI's lavapipe. Merging once the full Build is green.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T11:24:58Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppL1832-1833core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppL1832-1833Imported from boykopovar/AnyPS5. Originally posted by Amoenus on 2026-10-13T12:20:22Z.
Found a request-version collision during independent CPU-only public-source checks: https://github.com/boykopovar/AnyPS5/issues/5132.
All 12 fixtures emitted by original main075 as version21 throw
RequestSerializer: truncated dataon unmodified main140; all 24 current21 false/true requests round-trip. The F16 tail was added without changing the already-used version21 layout.Writer/max/gate-to-22 alone is incomplete: all 12 current21 inputs with F16=true then silently deserialize as false. The issue includes a synthetic reproduction; no title or GPU failure is claimed.
Would you confirm this narrower compatibility rule: retain writer21, accept exactly zero remaining bytes as historical F16=false or one byte0/1 as the current F16 flag, and throw for other version-21 tails? It preserves both emitted layouts without taking #5097's version22. This is a proposal, not an implemented/tested repair. AI-assisted: yes.