s_memtime goes backwards on RADV RDNA2/RDNA3: subgroup OpReadClockKHR is a 20-bit counter there #985

Closed
opened 2026-10-06 21:50:07 +00:00 by fisherjoey · 2 comments
fisherjoey commented 2026-10-06 21:50:07 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-06T21:50:07Z.


agc_driver_shader_clock fails intermittently on RADV with an RDNA2 GPU:

AGC graphics: memtime: lane 0 went back from 1027385 to 35615
AGC graphics: memtime: lane 0 went back from 1037082 to 32038

s_memtime is emitted as OpReadClockKHR with subgroup scope (EmitShaderClock in SpirvModuleEmitter.cpp). On GFX10.3 and GFX11, ACO lowers a subgroup-scope clock to an s_getreg_b32 of SHADER_CYCLES, 20 bits wide, zero-extended to 64 (src/amd/compiler/instruction_selection/aco_select_nir_intrinsics.cpp, Mesa main):

} else if (nir_intrinsic_memory_scope(instr) == SCOPE_SUBGROUP &&
           ctx->options->gfx_level >= GFX10_3) {
   /* "((size - 1) << 11) | register" (SHADER_CYCLES is encoded as register 29) */
   Temp clock = bld.sopk(aco_opcode::s_getreg_b32, bld.def(s1), ((20 - 1) << 11) | 29);

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_memtime is 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.

VK_DRIVER_FILES=/usr/share/vulkan/icd.d/radeon_icd.x86_64.json ctest --test-dir build -R agc_ -j6 --output-on-failure

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).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5735882233 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/985). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-06T21:50:07Z. --- `agc_driver_shader_clock` fails intermittently on RADV with an RDNA2 GPU: ``` AGC graphics: memtime: lane 0 went back from 1027385 to 35615 AGC graphics: memtime: lane 0 went back from 1037082 to 32038 ``` `s_memtime` is emitted as `OpReadClockKHR` with subgroup scope (`EmitShaderClock` in `SpirvModuleEmitter.cpp`). On GFX10.3 and GFX11, ACO lowers a subgroup-scope clock to an `s_getreg_b32` of `SHADER_CYCLES`, 20 bits wide, zero-extended to 64 (`src/amd/compiler/instruction_selection/aco_select_nir_intrinsics.cpp`, Mesa main): ```cpp } else if (nir_intrinsic_memory_scope(instr) == SCOPE_SUBGROUP && ctx->options->gfx_level >= GFX10_3) { /* "((size - 1) << 11) | register" (SHADER_CYCLES is encoded as register 29) */ Temp clock = bld.sopk(aco_opcode::s_getreg_b32, bld.def(s1), ((20 - 1) << 11) | 29); ``` 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_memtime` is 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. ``` VK_DRIVER_FILES=/usr/share/vulkan/icd.d/radeon_icd.x86_64.json ctest --test-dir build -R agc_ -j6 --output-on-failure ``` 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).
fisherjoey commented 2026-10-06 22:20:23 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-06T22:20:23Z.


Measured on the gfx1036 with the hw-oracle: s_memtime returns 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 as s_memrealtime. Over a ~35 ms spin loop, three runs gave s_memtime deltas of 68.7M to 72.4M against s_memrealtime deltas of 3.1M to 3.6M, a ratio of 20.2 to 22.2 (shader clock against the 100 MHz reference).

Mapping s_memtime to 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:

  1. Device scope everywhere: monotonic, wrong rate.
  2. Device scope only where the subgroup clock is narrow (RADV on GFX10.3/GFX11), which means passing that information into the recompiler.

Which would you prefer? I can send a PR for either.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6026506921 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/985#issuecomment-6026506921). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-06T22:20:23Z. --- Measured on the gfx1036 with the hw-oracle: `s_memtime` returns 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 as `s_memrealtime`. Over a ~35 ms spin loop, three runs gave `s_memtime` deltas of 68.7M to 72.4M against `s_memrealtime` deltas of 3.1M to 3.6M, a ratio of 20.2 to 22.2 (shader clock against the 100 MHz reference). Mapping `s_memtime` to 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: 1. Device scope everywhere: monotonic, wrong rate. 2. Device scope only where the subgroup clock is narrow (RADV on GFX10.3/GFX11), which means passing that information into the recompiler. Which would you prefer? I can send a PR for either.
EdouardSence commented 2026-10-07 17:56:23 +00:00 (Migrated from github.com)
Author
Contributor

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, main cea1ef1.

  • Repro as in the report (ctest --test-dir build -R agc_ -j6 --output-on-failure), 6 runs: 6 of 6 pass, and no went back line.
  • ISA from RADV_DEBUG=shaders on agc_driver_shader_clock: the subgroup-scope clock is s_getreg_b32 hwreg(HW_REG_SHADER_CYCLES_LO) plus HW_REG_SHADER_CYCLES_HI, with HI read twice, so it is 64 bits. The device-scope clock is s_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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6043700215 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/985#issuecomment-6043700215). Originally posted by [EdouardSence](https://github.com/EdouardSence) on 2026-10-07T17:56:23Z. --- Data point for GFX12 on RADV: RX 9070 XT (GFX1201), Mesa 26.2.4, `main` cea1ef1. - Repro as in the report (`ctest --test-dir build -R agc_ -j6 --output-on-failure`), 6 runs: 6 of 6 pass, and no `went back` line. - ISA from `RADV_DEBUG=shaders` on `agc_driver_shader_clock`: the subgroup-scope clock is `s_getreg_b32 hwreg(HW_REG_SHADER_CYCLES_LO)` plus `HW_REG_SHADER_CYCLES_HI`, with HI read twice, so it is 64 bits. The device-scope clock is `s_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).
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5#985
No description provided.