fix(shader): read s_memtime from the device clock where RADV's subgroup clock is 20 bits #1383
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1383"
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 nisbenz on 2026-10-08T01:18:24Z.
What
s_memtimewent backwards on RADV with RDNA 2 and RDNA 3 GPUs (#985). It is emitted asOpReadClockKHRwith Subgroup scope. On GFX10.3 to GFX11.7, ACO lowers that tos_getreg_b32ofSHADER_CYCLES, which is 20 bits wide and zero-extended to 64. The counter therefore wraps every 2^20 cycles.This follows option 2 from the issue thread: on those GPUs only,
s_memtimereads the Device-scope clock. Every other device, including RADV GFX12, NVIDIA and Intel, emits exactly what it did before.src/amd/compiler/instruction_selection/aco_select_nir_intrinsics.cpp, casenir_intrinsic_shader_clock.gfx_level >= GFX12branch reads 64 bits (p_shader_cycles_hi_lo_hi).gfx_level >= GFX10_3branch readss_getreg_b32 ((20 - 1) << 11) | 29.src/amd/common/amd_family.h, familiesCHIP_NAVI21toCHIP_GFX1171. RADV reports the family in the device name as(RADV <family>), using the names fromac_get_family_name. I checked that these names appear in the installedlibvulkan_radeon.so(Mesa 26.2.4).s_memtimeis 64-bit on the GPU itself, so the narrowing comes from ACO.Changes:
NarrowSubgroupClock(driverID, deviceName)(newExecution/src/SubgroupClock.cpp) returns true only forVK_DRIVER_ID_MESA_RADVwith one of the GFX10.3 to GFX11.7 family names.VulkanDevicequeriesVkPhysicalDeviceDriverPropertiesthroughVK_KHR_driver_properties, because the instance is Vulkan 1.1.SpirvTarget::narrowSubgroupClockis passed to the emitter.EmitShaderClockusesScopeDevicewhen it is set.false.TechnicalDebt.md(Functional) records the trade-off. The device clock is monotonic and 64-bit but counts the 100 MHz reference clock, so on those GPUss_memtimedeltas are about 20 times smaller than on hardware. A family added to Mesa later keeps the subgroup clock until it is listed.agc_driver_shader_clocknow runs its dispatch 128 times. On an affected GPU, a wrap then lands between two reads almost every run, so the test catches a regression instead of failing about 5% of the time.Tests:
agc_subgroup_clock: family and driver matching, including malformed names, GFX10.1 and GFX12.agc_shader_memory:OpReadClockKHRscope fors_memtimewith the flag set and unset, and fors_memrealtime;agc_shader_disk_cache: the key changes with the flag.Overlap with #1182 and #1190 (flagged below): both also take request serializer version 10, for
scratchDwords, and append to the same trailer inRequestSerializer.cpp. This PR does not build on them and they do not build on it, soDepends onis none. Whichever merges second moves its field to version 11: write11u, acceptversion > 11u, read the field underversion >= 11u, and update the version pin inagc_shader_memory. I can rebase this one onto them if they land first.Fixes #985
Tested
Linux (CachyOS, kernel 7.2.9), AMD Radeon RX 6750 XT (RADV NAVI22, gfx1031), Mesa 26.2.4, GCC 16.2.1, Release.
main(df16c4c2)agc_driver_shader_clock, original single dispatch, 300 runsmemtime: lane 0 went back from 1020093 to 17981etc.)agc_driver_shader_clock, 128 dispatches per runctest -R agc_ -j6(the repro from the issue), 10 runswent backctest -j8Shader clock driver=3 narrowSubgroupClock=1.tools/check_conventions.py --base upstream/mainpasses.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 EdouardSence on 2026-10-08T12:31:38Z.
Tested on the two RADV devices in my machine. One is in the "not tested" list (GFX12); the other is one of the affected families. Same host as the PR: Linux 7.2.9 (CachyOS), Mesa 26.2.4, GCC, Release build of eb13861.
RX 9070 XT (RADV GFX1201, GFX12)
Shader clock driver=3 narrowSubgroupClock=0, so the emitted SPIR-V is unchanged, as expected.ctest -j8: 409/409 pass.agc_driver_shader_clock_tests(128 dispatches): 30/30 runs pass.Ryzen 7 7800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036, GFX10.3)
MESA_VK_DEVICE_SELECT=1002:164e MESA_VK_DEVICE_SELECT_FORCE_DEFAULT_DEVICE=1(Physical device count=1).Shader clock driver=3 narrowSubgroupClock=1.agc_driver_shader_clock_testswith this PR: 100/100 runs pass.main's single-dispatch test passed 100/100.NarrowSubgroupClockforced to return false locally (log showsnarrowSubgroupClock=0), passed 30/30, with nowent back.AI-assisted: the runs and this write-up were done by Claude (Claude Code) on my machine.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T12:51:24Z.
Rebased onto current
main: it conflicted inRequestSerializer.cppandtests/ShaderMemory.cppbecausemainis already at serialization version 11 (#1190 line of work addedscratchDwordsat 10,orderedPixelShaderat 11). As described in the PR, the field moved to version 12: written afterscratchDwords, read underversion >= 12u, limit> 12u, and the version pins inagc_shader_memoryupdated (NVNQQQwAAAA=, unsupportedNVNQQQ0AAAA=).Checked on the rebased head: build and full ctest green (453/453), the four PR tests pass, and they don't build without the change. On the hardware oracle (gfx1035)
s_memtimeadvances by ~5.5M over a short loop with a zero high dword, so the counter is wider than 20 bits on HW and the narrowing is ACO's, as measured in #985. The 20x rate difference of the device clock is recorded in TechnicalDebt; fine as a trade-off. Merging once CI is green.