fix(shader): round v_cvt_pk_u8_f32 in the FLOAT_MODE f32 round mode #6011
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6011"
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 zmeizy on 2026-10-14T19:15:01Z.
What
v_cvt_pk_u8_f32always rounded its source to nearest even. On RDNA2 it rounds in the f32 round mode ofFLOAT_MODE(bits 1:0) before it clamps to 0..255: to nearest with ties to even, toward +infinity, toward -infinity, or toward zero. It also flushes a denormal source when f32 denormals are flushed. So with a directed f32 round mode the recompiler wrote wrong bytes for every non-integer source in range. For example, 0.125 gives 1 toward +infinity, and 254.875 gives 254 toward zero.vCvtPkU8F32now picksFPRoundEven32,FPCeil32,FPFloor32orFPTrunc32from the shader's f32 round mode, and flushes its source by the f32 denormal flush the instruction already declares inf32DenormalFlushFor. With round to nearest even nothing changes. The denormal flush only matters toward +infinity: 2^-149 gives 0 with f32 denormal mode 0 and 1 with mode 3.Out of scope, and unchanged:
s_round_modeors_setregstill throws:RoundingMode.cppkeeps the instruction as one that doesn't model run-time rounding.Source of semantics: measured with the hardware oracle's kernel template on gfx1030 (AMD Radeon RX 6800, AMD Windows driver 32.0.21045.5002 through its HIP runtime
amdhip64_6.dll):v_cvt_pk_u8_f32 v10, v4, 0, 0.A new entry in
docs/dev/debt/shader.mdrecords it.Tested
Ubuntu 24.04 (WSL2), GCC 13.3, lavapipe, on
main73efd940.agc_driver_vcvt_pk_u8_f32_round_mode(new) pins 38 measured sources (ties, fractions near 0 and 255, out-of-range values, denormals, infinities and NaNs). It checks every round mode with f32 denormals flushed and kept, at IEEE 0 and 1.mainit fails: round mode 1 of 0.125 gives 0 instead of 1.main, round to nearest matched and the directed modes missed 893 to 897 rows each.agc_driver_f32_pack_rounding,agc_driver_float16_misc,agc_driver_vop3_legacy_source_modifiers,agc_driver_f32_denormal_flushandagc_driver_integer_to_f32_roundingpass.anyps5-gpu-probes-v3-gfx1030.json.gz: probespk_u8_roundingandpack_f32_denormal_modes.anyps5-gpu-probes-v2-gfx1030.json.gz: probeunary_f32_denormal_modes, output v23.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)🤖 Generated with Claude Code
anyps5-gpu-probes-v2-gfx1030.json.gz
anyps5-gpu-probes-v3-gfx1030.json.gz
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-14T20:33:25Z.
Review: COMMENTED
Run on an Apple GPU, a device without f32 denormal support (
shaderDenormPreserveFloat32 = false).GPUs tested: Apple M2 Max, MoltenVK 1.4.2 (Vulkan 1.4.357), macOS 27.2, x86-64 under Rosetta; also MoltenVK main with SPIRV-Cross #2704 (keeps the sign of -0)
Method:
macos-portatb7eb6b6b(mugurc/AnyPS5) + ImportWatched + this PR's diff (merge-base..head, withoutdocs/), notmain, becausemaindoes not run the GPU tests on macOS yet. Release, LunarG Vulkan SDK 1.4.363.0, x86-64 under Rosetta. Suite:ctest -R "^agc" -E "agc_display_buffer_dcc|agc_driver_flip|agc_driver_lds_atomics64" -j1 --timeout 30; without the PR it has 13 known macOS failures with MoltenVK 1.4.2 and 7 with the #2704 build (#1). Headab42b22db; the diff applies cleanly.agc_driver_vcvt_pk_u8_f32_round_modefails on both drivers. With theRequireturned into a print locally, there are 4 mismatches, the same on both drivers: round mode 1 (toward +infinity) with f32 denormals kept, sources0x00000001and0x007fffff, at IEEE 0 and 1. These give 0 where the rows expect 1, because the device flushes the denormal source before the ceil.round mode 1, f32 denormals flushed, IEEE=0 of 0x3e000000 is 0x00000000, expected 0x00000001.Skipping the two kept-denormal sources on devices without f32 DenormPreserve would let the test pass there.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-15T01:02:55Z.
Ran this on PR head ab42b22db (merge base
73efd9407, 290 commits behind mainab9a47684). This confirms jotitadev's MoltenVK result above on a second driver.Setup: PR head checked out as-is (not rebased), default CMake build (CMAKE_BUILD_TYPE empty, unoptimized), Linux, ANYPS5_NO_SHADER_CACHE=1, GPU test run on lavapipe (Mesa 26.2.4 llvmpipe, LLVM 23.1.1).
"AGC graphics: v_cvt_pk_u8_f32 round mode 1, f32 denormals kept, IEEE=0 of 0x00000001 is 0x00000000, expected 0x00000001"
All flushed-denormal cases ran first and passed in each run. My log shows only the first kept mismatch, since the test stops there.
lavapipe reports shaderDenormPreserveFloat32 = false and shaderDenormFlushToZeroFloat32 = false. On main I find denormal-preserve handling only for f16 and f64 (VulkanDevice.cpp lines 1212-1213, 1222 and 1410), and no f32 DenormPreserve execution mode emitted, by reading. So the kept-denormal result depends on the device, which fits this failure, though I have not proven that lavapipe flushes this input. The test could skip the kept cases when the device reports no f32 denormal preservation, or the translation could produce the kept result without relying on the device. I have not tried either.
Not checked: NVIDIA, AMD, Windows, or any title.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T21:13:24Z.
Review: CHANGES_REQUESTED
Measured on the hardware oracle (gfx1035, RDNA2):
v_cvt_pk_u8_f32 v10, v4, 0, 0over 2,137 sources (the test's 38 plus k/8 for k = -16 to 2,099, values next to 0.5 and 255.5, ±1e10, denormals, infinities, NaNs) at IEEE 0/1 × f32 denormal mode 0/3 × f32 round mode 0-3. The flush → round → clamp model in the description and every row of the test match it, 0 mismatches.One thing to fix before merging:
ConvertInstructions.cpp:213-220: with f32 denormals kept, round mode 1 of a positive denormal relies on the driver keeping the denormal throughFPCeil32and the<= 0compare. The recompiler only requestsDenormPreservefor f16 and f64, so Vulkan lets the driver flush a 32-bit denormal there. This is the failure @jotitadev (MoltenVK) and @Dextroid17 (lavapipe, Mesa 26.2.4) reported above, and it also fails on RADV here (Radeon 660M, Mesa 26.2.4):round mode 1, f32 denormals kept, IEEE=0 of 0x00000001 is 0x00000000, expected 0x00000001. CI's lavapipe (Mesa 25.2.8) keeps the denormal and passes, so a green CI won't show it.Skipping those rows on such devices would leave the translation wrong on them, RADV included. Please decide this case on the bits instead: with round mode 1 and f32 denormals kept, a source whose bits are in
0x00000001..0x007fffffgives 1. Every other denormal case (modes 0, 2 and 3, and negative denormals in mode 1) already gives 0 whatever the driver does with the input, so the test's rows can stay as they are.Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-21T17:10:24Z.
Re-ran on head
059d3f2a1(rebased ontocecb540a6). Setup differs from my 10-15 run: Release build,-DANYPS5_AGC_EXECUTION_TESTS=VCvtPkU8F32RoundMode,-DANYPS5_ENABLE_SPIRV_TOOLS=OFF(CI uses ON), shader cache neither cleared nor disabled, lavapipe viaVK_ICD_FILENAMES(Mesa 26.2.4 llvmpipe, LLVM 23.1.1, 256 bits).agc_driver_vcvt_pk_u8_f32_round_modepasses 3 of 3 ctest runs, and a direct run of the binary printsv_cvt_pk_u8_f32 round mode tests passed.The failure I measured on 10-15 on this driver ("round mode 1, f32 denormals kept, IEEE=0 of 0x00000001 is 0x00000000, expected 0x00000001") no longer reproduces. By reading, the select at
ConvertInstructions.cpp:230-233keys on the source bits (0x00000001..0x007fffffgives 1), so the result no longer depends on the driver preserving the denormal throughFPCeil32. I did not run RADV or MoltenVK, so the open change request and the 10-14 MoltenVK result are not answered by this run.Not checked: RADV, MoltenVK, NVIDIA, Windows, any title. All CI build jobs are skipped on this head. My APPROVED review above went up with an empty body before this run; it stands for this lavapipe result only.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by zmeizy on 2026-10-21T17:17:11Z.
Thanks, done in
059d3f2a1(also rebased onto maincecb540a).With round mode 1 and f32 denormals kept, a source whose bits are in 0x00000001..0x007fffff now gives 1 on the bits, so the result no longer depends on whether the driver keeps the denormal through FPCeil32 and the <= 0 compare. Every other denormal case already gives 0 whatever the driver does, so the test's rows are unchanged. The debt entry says so and is now under 1,000 characters.
Re-checked on Ubuntu 24.04 (WSL2), GCC 13.3, lavapipe (Mesa 25.2.8):
All 2,133 measured sources match in all 16 modes (IEEE 0/1 × f32 denormal mode 0/3 × f32 round mode 0–3).
agc_driver_vcvt_pk_u8_f32_round_mode fails on main and passes with the change.
It and agc_driver_f32_pack_rounding, agc_driver_float16_misc, agc_driver_vop3_legacy_source_modifiers, agc_driver_f32_denormal_flush and agc_driver_integer_to_f32_rounding pass with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1.
No validation messages beyond main's (VUID-VkDeviceCreateInfo-pNext-pNext only).
lavapipe 25.2.8 keeps f32 denormals, so it can't show the old failure. A run on RADV or MoltenVK would confirm the new path.
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T08:26:09Z.
Review: COMMENTED
MoltenVK run of
059d3f2a1, for the new path. MoltenVK flushes the denormal throughFPCeil32here, so it takes the case the bit select was added for: it fails without the select and passes with it. This head also clears the 4 denormal mismatches I reported on ab42b22db.Machine: Apple M2 Max, macOS 27.2, x86_64 under Rosetta 2. Release,
BUILD_TESTING=ON. The device reportsshaderDenormPreserveFloat32 = false.Drivers: MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), and MoltenVK main + SPIRV-Cross #2704.
Base/method: PR #8658
feat/macos-port@ee0a438415(currentmainplus the macOS port), notmain, which does not build on macOS. This PR's diff (git diff merge-base..059d3f2a1 -- . ':!docs') applied on top, clean. Full rebuild before each run.agc_driver_vcvt_pk_u8_f32_round_modepasses 3 of 3 on both drivers.if (roundMode == 1u && (f32DenormalFlush & 1u) == 0u)select removed (the ab42b22db behavior): fails 3 of 3 on both drivers withv_cvt_pk_u8_f32 round mode 1, f32 denormals kept, IEEE=0 of 0x00000001 is 0x00000000, expected 0x00000001. This is the same failure as on RADV and the old lavapipe, so the bit select is what makes the result independent of the driver on Apple too.^agcsuite with the PR:-j1 --timeout 30, 857 tests. MoltenVK 1.4.2 matches the base exactly (17 failures). MoltenVK main + #2704 has the base's 16 plusagc_driver_cross_workgroup_flag, which also fails intermittently on the base without this PR (a workgroup did not see another workgroup's flag store). No timeouts.GPUs tested: Apple M2 Max (MoltenVK 1.4.2; MoltenVK main + SPIRV-Cross #2704)
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.