fix(shader): flush the operands of image, buffer and global float atomics #8055
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8055"
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 thuanquang on 2026-10-19T19:08:38Z.
What
On RDNA 2, the memory float atomics flush denormal operands the way the LDS ones do. This covers
image_atomic_fmin,image_atomic_fmaxandimage_atomic_fcmpswap, the same three asbuffer_atomic_*andglobal_atomic_*, and the_x2forms of the buffer and global ones. They flush both operands to a signed zero when bit 0 of the shader's denormal field is clear: the f32 field for the 32-bit forms, the f16/f64 field for_x2.The emulator flushed only the LDS forms. Under
FLOAT_MODE0xc0, which most titles use, a denormal operand of a memory float atomic kept its value:fmaxof old +0 and data 0x00000001 stored 0x00000001; the hardware stores +0.fcmpswapof old +0 with a comparand of 0x80000001 left memory unchanged; the hardware compares the flushed -0, finds it equal and swaps.The change:
TranslationContext::floatAtomicFlush, renamed fromsharedFloatFlush, now also setsMemoryInfo::flushDenormalsfor the image, buffer and flat float atomic opcodes. It keeps the same bits and the same default for a shader without a known float mode: flush f32, keep f64.AtomicFloatBitsapplies the flag inminMaxandcompareSwap.The LDS emitters now pass the flag the same way instead of flushing at each call site, so the rule is written once. Without the flag, the emitted SPIR-V is unchanged. The new debt entry records the rule and what it was measured on. The
flat_atomic_*forms take the same path, including the LDS and scratch apertures, but are neither measured nor run by the new test.The three existing tests from #332, #506 and #562 (
agc_driver_buffer_atomics_float,agc_driver_global_atomics_float,agc_driver_image_atomics_float) were measured with denormals kept. They ran with no float mode, which the translator treats as flushing f32, as for the LDS forms and as in the 0xc0 most titles use. They now setFLOAT_MODE0xf0, the mode of their tables. Without that they fail with this change:lane 1 fmax_mem0 is 0x00000000, expected 0x00000001.The rule, measured on every row below:
fminandfmaxcompare the flushed operands and write the flushed winner, also when the old value wins.fcmpswapcompares the flushed old value with the flushed comparand (both zeros equal, NaN never equal). On a match it stores the source as given; otherwise memory is unchanged.Tested
Hardware: RX 6700 XT (gfx1031), raw ISA through the
tools/hw-oracletemplate, run with the comgr and HIP runtime of AMD's Windows driver 32.0.21045.5002.fcmpswapcomparand: alternately the old value and the source.glcand reads the dword back:main)fmin/fmax/fcmpswap_x2forms_x2formsIn fields 0 and 2, the flush changes 352 of the 1,600 rows per mode for
fmin, 288 forfmaxand 56 forfcmpswap(32-bit); for_x2, 352, 256 and 56 of 1,296. Every returned value is the old value.Emulator: the new
agc_driver_memory_float_atomic_modesruns the buffer and image forms and the buffer_x2forms over 32 of those pairs in six float modes: no float mode, 0xc0, 0xf0, 0x00, 0x50 and 0xa0. The global forms run inFLOAT_MODE0x00 only, where both widths flush, withglobal_atomic_fminandglobal_atomic_fmin_x2. Their stored value depends on the flush in 13 of the 32 rows at each width. Its expected stored values are the GPU's own results for those rows. Programs are assembled for gfx1013.CI cost: llvmpipe needs a long time to compile the global float atomics, which is what pushed the first version of this test past the CI timeout. Cold runs on lavapipe (Mesa 25.2.8 with LLVM 20.1.2, as on Linux CI,
MESA_SHADER_CACHE_DISABLE=true,ANYPS5_NO_SHADER_CACHE=1) on one machine under the same heavy load:FLOAT_MODE0x00 onlyfminandfmin_x2in 0x00 only)Two controls, both on lavapipe:
global, FLOAT_MODE 0x00, lane 4 fmin stored 0x80000001, expected 0x80000000.buffer, no float mode, lane 3 fmax stored 0x00000001, expected 0x00000000.This push (rebased onto
main49c9bdf37, head06c1c0300) ran on the RX 6700 XT with AMD's driver 32.0.21045.5002:agc_driver_memory_float_atomic_modes,agc_driver_global_atomics_float,agc_driver_buffer_atomics_float,agc_driver_image_atomics_float,agc_driver_lds_add_f32,agc_driver_lds_float_atomic_modes,agc_driver_lds_atomicsandagc_driver_lds_mixed_width_atomics.VUID-lines onmaina07e985c8(the same tests without the new one) and on this head. The layer was inserted in every test process.Windows 11 26200, RX 6700 XT, AMD's driver 32.0.21045.5002, Release:
main38de0a470with only the new test fails 3 of 3:buffer, no float mode, lane 3 fmax stored 0x00000001, expected 0x00000000. Local variants that start with the image or the global path fail the same way onmain(lane 3fmaxstores 0x00000001, no float mode). Variants that check only the_x2forms fail inFLOAT_MODE0x00, wherebuffer_atomic_fmax_x2andglobal_atomic_fmax_x2store 0x0000000000000001 for an expected 0.*atomic*test,agc_shader_disk_cache,agc_shader_memoryandagc_driver_image_cache_bits.shaderBufferInt64Atomics, asBufferAtomicsFloatdoes.ctest --parallel 4: 1685 tests, 8 failed, none from this change:log_macrosandlog_macros_timestamps: ccache rewrites__FILE__in local builds.agc_trimmed_import: #6499.agc_driver_draw_cache_reuse_off,agc_driver_layered_volume_vertex_no_gplandagc_driver_stencil_replay: each lost the device because its driver cache was filled in the other library mode first (#7809, which #7828 fixes).main38de0a470fails the same group here, withagc_driver_stencil_replay_no_gplinstead ofagc_driver_stencil_replay, since the order depends on the cache.agc_driver_bda_deviceandguest_nanosleep_timer_self_testtimed out in the first minute while another build held the CPU. Rerun alone, both pass 3 of 3.guest_memoryfailed in an earlier run of this branch withflexible memory exhausted. It fails the same way onmainhere, and it passed in this run and 3 of 3 alone.Validation layer A/B: Khronos validation layer (
VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, layer built from Vulkan-ValidationLayers, loader debug confirmsInsert instance layer "VK_LAYER_KHRONOS_validation"),ANYPS5_NO_SHADER_CACHE=1,ANYPS5_REQUIRE_VULKAN=1,ctest -Vonagc_driver_memory_float_atomic_modes,agc_driver_buffer_atomics_float,agc_driver_global_atomics_float,agc_driver_image_atomics_floatandagc_driver_lds_float_atomic_modes.main38de0a470(with the new test file added): 4 pass, the new test fails as above, 0VUID-messages. This PR: 5 of 5 pass, 0VUID-messages. Neither log haswas not found but was requested.wachi35676 checked the rule independently on a second RDNA 2 chip (gfx1036, Windows driver 32.0.21042.62) and ran the tests on an RTX 5070 Ti and a Radeon iGPU, with the same red/green; see the comment below.
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
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T01:56:30Z.
Independent check on a second RDNA2 chip, plus Windows runs on NVIDIA and an AMD iGPU.
Hardware rule, gfx1036 (Ryzen 5 9600X iGPU, AMD Windows driver 32.0.21042.62). I ran raw ISA through the driver's own comgr 2.9 and HIP runtime (
amd_comgr_2.dllandamdhip64_6.dll). The harness is a small Windows loader around a kernel liketools/hw-oracle/template.s, with wave32, DX10_CLAMP 1, round to nearest andhipMalloc(coarse) memory. For each row it stores the old value, runs the atomic withoutglcand reads the dword or qword back. I compared every stored value with the rule in this PR's debt entry and with main's no-flush lowering:global_atomic_fmin/fmax,buffer_atomic_fmin/fmax(32-bit)global_atomic_fcmpswap,buffer_atomic_fcmpswapglobal/buffer_atomic_fmin_x2/fmax_x2The operands were signed zeros, ±min and max denormals, ±1, the smallest normal and a quiet NaN on either side. The
fcmpswaprows include comparand 0x80000001 against old +0, a denormal old against a comparand of +0, and a denormal source, which is stored unflushed. Two examples with denorm32 0:fmax(old 0x7fc00000, data 0x00000001)stores 0x00000000, andfmax(old 0x00000001, data 0x7fc00000)stores 0x00000000, the flushed old value, as the entry says. With denorm32 1 and 3 both store 0x00000001. Image and flat forms were not run here. Sources and raw rows:winoracle.c,run.py,run64.py,model.py,gfx1036_results.txtandgfx1036_x2.txt(available on request).Emulator, Windows 11 26300, MinGW GCC 15.2 Release,
maineb7500680+ this PR (285642c2b, merges cleanly):agc_driver_memory_float_atomic_modesand the three changed*_atomics_floattests passed 3 of 3 on the RTX 5070 Ti (NVIDIA 617.42) and 3 of 3 on the Radeon iGPU (ANYPS5_GPU=AMD, 32.0.21042.62), withANYPS5_REQUIRE_VULKAN=1.buffer, no float mode, lane 3 fmax stored 0x00000001, expected 0x00000000. The three updated tests still pass with main's sources, because they now setFLOAT_MODE0xf0.ctest -R "agc|shader|spirv|recomp"(824 tests). On NVIDIA, 3 failed:msaa_dcc_clear_4,pass_input_hazardandsgpr_pattern_fill_keys, which also fail on main here. On AMD, 5 failed:agc_trimmed_import,msaa_dcc_clear_4,layered_volume_vertex_no_gpl,draw_cache_reuse_offandstencil_replay_no_gpl, which also fail on main here (main additionally failed the flakysgpr_pattern_fill_keys). No new failures.check_conventions.py: 0 errors and 2debt-lengthwarnings, one of them the moved LDS float-atomic entry at 2457 characters.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-20T05:26:33Z.
@wachi35676 thank you, that's the second RDNA 2 chip this needed, and the NVIDIA and iGPU runs cover the hosts I can't. I added a pointer to your comment and a Khronos validation-layer A/B to the description (0
VUID-messages onmainand with the PR, layer load confirmed).Imported from boykopovar/AnyPS5. Originally posted by DavidAmsellem on 2026-10-20T19:00:37Z.
First Linux run of this PR, on AMD with RADV. Head
285642c2bmerged ontomain2fee39f1b(clean merge), built in full and compared with the samemain, on the same machine.AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 (Ubuntu 24.04 base), kernel 7.0.0-38, GCC 13.3, Release.
Full suite (
ANYPS5_REQUIRE_VULKAN=1 ctest -j8) at66ff8e999: 1716/1717 with this PR, against 1715/1716 onmain. The one failure is the same on both, so this PR adds none:agc_driver_guest_memory_lifetime(#7784, which #7849 addresses).Comparing the JUnit output with
main: the new entry,agc_driver_memory_float_atomic_modes, runs and passes (memory float atomic modes tests passed). No other entry is added, and none is skipped or removed. With the Khronos validation layer, it adds no VUID (the only message isVkDeviceCreateInfo-pNext-pNext, which my older 1.3.275 layer prints for every device because it doesn't knowmaintenance8).One thing to keep in mind: on this device the float-controls probe reports
f32 denormal flush: hardware (0 of 5472 probe results differ from the emulated flush). I haven't checked which path the new operand flush takes when the hardware flush is chosen, so this run may not exercise the emulated case.GPUs tested: AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 / kernel 7.0.0-38
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T02:28:04Z.
@thuanquang thanks. I looked into the Linux CI timeout of
agc_driver_memory_float_atomic_modeson 285642c2b. It isn't a hang, but the test is too expensive to compile on llvmpipe, and that needs fixing before this can merge.I ran it locally on lavapipe with the same Mesa as Linux CI (25.2.8, LLVM 20.1.2), merged into current main, 10 runs in a row with the shader caches kept between runs:
globalcases (no float mode, 0xf0 and 0x00), each of which prints "recorded batch still running on the GPU after 15-20 s". So the cost is compiling those shaders on llvmpipe, not executing them.CI runners start with empty caches, so every Linux run pays the ~75 s under
--timeout 120and--test-load. On a busy runner that goes past the limit, which fits the timeout you saw and the green rerun. Raising the timeout isn't allowed (CONTRIBUTING), so could you cut the compile cost of the global cases instead? For example, use fewer distinct global shaders or float-mode combinations per run, or one shader that takes the mode from a buffer value instead of one shader per mode. Under about 20 s cold on llvmpipe would leave plenty of margin.Separately,
agc_driver_global_atomics_floatwent from about 0 s to 6 s on lavapipe with this PR, probably the same compile cost of the new compare/min/max sequence. Is that expected to be noticeable at pipeline compile time on real drivers? On an RTX 4090 the new test takes 5 s.Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-21T03:38:55Z.
@oneandonlydean thank you for tracking that down, and for the reverted-change control. I'll fix both points in the next push:
agc_driver_global_atomics_floatgoing from about 0 s to 6 s: that is the operand flush this PR adds to every global float atomic. I'll compare the SPIR-V size and compile time of that test betweenmainand this head, shrink the sequence where I can, and post the numbers here. That includes a real-driver compile time on the RX 6700 XT.Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-21T03:51:55Z.
On the second point,
agc_driver_global_atomics_floatgoing from about 0 s to 6 s on lavapipe: the production change doesn't add that cost. The one line this PR adds to that test does:FLOAT_MODE 0xf0, which keeps denormals so the hardware-measured tables still apply.Measured on the RX 6700 XT (AMD driver 32.0.21045.5002), all on
maind0852defa:APS5_DUMP_COMPUTE_PIPELINES) ofmainwith only the test'sfloatMode = 0xf0line added, and of this PR rebased onto the samemain. Both give the same 14 SPIR-V modules, byte for byte. At 0xf0 the operand flush is off, so the atomics emit what they did before.ANYPS5_NO_SHADER_CACHE=1, 3 runs each): about 1.0 s onmainand about 1.05 s with the 0xf0 line. On this driver the float mode is barely noticeable at pipeline compile time.So on lavapipe the 6 s should come from how 0xf0 compiles there, and you should see it on
mainwith only that line added. I can't run lavapipe on this machine yet, so I haven't confirmed that.For
agc_driver_memory_float_atomic_modes, the plan above stands: one global shader instead of three. I'll measure it cold on lavapipe (Mesa 25.2.8, the same as Linux CI) before pushing.Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-21T06:19:23Z.
@oneandonlydean pushed
06c1c0300, rebased ontomain49c9bdf37. The production commit is unchanged; the test commit is new.I had to go further than the plan above. Cutting the global kind to one float mode wasn't enough: that one shader still took most of the time, because llvmpipe is slow to compile global float atomics. The global kind now runs only
FLOAT_MODE0x00, withglobal_atomic_fminandfmin_x2; their stored value depends on the flush in 13 of 32 rows at each width. Buffer and image keep all three operations in all six modes.Cold on lavapipe with Mesa 25.2.8 and LLVM 20.1.2 (as on Linux CI), measured back to back on one heavily loaded machine:
On an idle runner it should be well under your 20 s.
The controls still hold on lavapipe:
global, FLOAT_MODE 0x00, lane 4 fmin stored 0x80000001, expected 0x80000000.buffer, no float mode, lane 3 fmax stored 0x00000001, expected 0x00000000.On the RX 6700 XT, the 8 float atomic tests pass 3 of 3, and the validation layer is clean on both sides. The description has the details.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T06:20:59Z.
@thuanquang thanks, 22.5 s cold on a loaded machine is well within range, and keeping both controls is what we needed. We will review the new test commit and then run full CI on
06c1c0300.