s_memtime goes backwards on RADV RDNA2/RDNA3: subgroup OpReadClockKHR is a 20-bit counter there #985
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 fisherjoey on 2026-10-06T21:50:07Z.
agc_driver_shader_clockfails intermittently on RADV with an RDNA2 GPU:s_memtimeis emitted asOpReadClockKHRwith subgroup scope (EmitShaderClockinSpirvModuleEmitter.cpp). On GFX10.3 and GFX11, ACO lowers a subgroup-scope clock to ans_getreg_b32ofSHADER_CYCLES, 20 bits wide, zero-extended to 64 (src/amd/compiler/instruction_selection/aco_select_nir_intrinsics.cpp, Mesa main):The counter wraps at 2^20 = 1048576. Both failures above read just under that and then a small value. GFX12 reads 64 bits and GFX10.1 and older use
s_memtime, so RDNA2 and RDNA3 on RADV are affected. On gfx1013,s_memtimeis a 64-bit counter, so a title that subtracts two reads can get a negative or very large delta. Device scope is 64-bit on RADV for every generation, but it reads the fixed-frequency realtime clock rather than shader cycles.Reproduce on
main(5b359092): it failed in 2 of 6 parallel runs and passed 5 of 5 when run alone, since a wrap has to fall between the two reads.Environment: Fedora 44, Mesa 26.2.1 (RADV), Ryzen 5 7600X iGPU (RAPHAEL_MENDOCINO, gfx1036), GCC 16.2.1. The same test passes on an RTX 5070 Ti (NVIDIA 595.91.07).
Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-06T22:20:23Z.
Measured on the gfx1036 with the hw-oracle:
s_memtimereturns a 64-bit counter on the hardware, so the 20-bit width comes from ACO's subgroup clock lowering, not from the GPU. It is not the same clock ass_memrealtime. Over a ~35 ms spin loop, three runs gaves_memtimedeltas of 68.7M to 72.4M againsts_memrealtimedeltas of 3.1M to 3.6M, a ratio of 20.2 to 22.2 (shader clock against the 100 MHz reference).Mapping
s_memtimeto device scope would therefore fix the wrap but run about 20x slow on every GPU, including ones whose subgroup clock is already 64-bit (NVIDIA here). Two options I can see:Which would you prefer? I can send a PR for either.
Imported from boykopovar/AnyPS5. Originally posted by EdouardSence on 2026-10-07T17:56:23Z.
Data point for GFX12 on RADV: RX 9070 XT (GFX1201), Mesa 26.2.4,
maincea1ef1.ctest --test-dir build -R agc_ -j6 --output-on-failure), 6 runs: 6 of 6 pass, and nowent backline.RADV_DEBUG=shadersonagc_driver_shader_clock: the subgroup-scope clock iss_getreg_b32 hwreg(HW_REG_SHADER_CYCLES_LO)plusHW_REG_SHADER_CYCLES_HI, with HI read twice, so it is 64 bits. The device-scope clock iss_sendmsg_rtn_b64 sendmsg(MSG_RTN_GET_REALTIME).So on RADV only GFX10.3 and GFX11 have the narrow subgroup clock, which fits option 2 (device scope only there).
AI-assisted: yes (Claude Code ran the tests and wrote this; the runs are on my machine).