Skip to content

fix(shader): address ds_append and ds_consume on LDS by the instruction offset alone - #523

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
DotDebian:fix/ds-append-consume-lds-offset
Oct 6, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
DotDebian:fix/ds-append-consume-lds-offset

Conversation

@DotDebian

Copy link
Copy Markdown
Contributor

What

ds_append and ds_consume on LDS updated the wrong dword, or nothing at all, depending on M0.

The emitter computed the counter address as M0[31:16] + offset and required offset + 3 < M0[15:0] for both data shares. On LDS the counter is at the instruction offset: M0 holds the base only for GDS. With this change the LDS path uses the instruction offset alone and is bounded by the LDS allocation, like every other LDS access in the emitter. The GDS path is unchanged.

Source: AMD's machine-readable ISA specification (RDNA1, RDNA2 and RDNA3 agree), DS_APPEND / DS_CONSUME: "Add (count_bits(exec_mask)) to the value stored in DS memory at (M0.base + instr_offset) if GDS, or at instr_offset if LDS. Return the pre-operation value to VGPRs."

The specification does not describe an M0 size limit on LDS, and LLVM's ldsRequiresM0Init() is false from GFX9 on, so the M0[15:0] check is dropped for LDS. Not measured on hardware.

Test agc_driver_ds_append_consume, the first test of these two instructions: one wave32 compute shader, counters at fixed LDS offsets written by lane 0. It checks the value returned to every lane, the counter afterwards, and that the dword at M0.base + offset is left alone, for:

  • ds_append and ds_consume with all 32 lanes active and M0 = 0x0040ffff
  • ds_append and ds_consume with EXEC = low 16 lanes and M0 = 0x00400000 (size 0): inactive lanes keep their destination register

It is skipped on devices whose subgroups are narrower than the wave, as agc_driver_ds_permute is. GDS is not covered: a GDS binding cannot be created on main.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, NVIDIA (32-lane subgroups), full ctest: 184/184 pass.

  • Without the change agc_driver_ds_append_consume fails with lane 0 ds_append result is 5000, expected 1000 (the dword at M0.base + offset was used).
  • With the address fixed but the M0[15:0] check kept on LDS it fails with lane 0 masked ds_append result is 0, expected 3000.

No title was run.

Checklist

🤖 Generated with Claude Code

…on offset alone

On LDS the counter is at the instruction offset. The emitter added
M0[31:16] to it and required the offset to be below M0[15:0], which is
the GDS base and a size no other LDS access reads. The GDS path is
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks, this is right. AMD's machine-readable ISA (RDNA1, RDNA2, RDNA3) puts the LDS counter at the instruction offset, and Mesa agrees in practice: ACO emits ds_append/ds_consume with the address in the offset field and leaves M0 uninitialized on GFX9+ (load_lds_size_m0), and radv turns every constant-address shared atomicAdd(±1) into those through ac_nir_opt_shared_append. That only works if LDS ignores both M0.base and M0.size, so dropping the size check is right too. For the record, the older RDNA2 PDF (section 12.13) still says M0.base + offset for LDS; the newer XML and Mesa both disagree with it.

The new test fails on main here with lane 0 ds_append result is 5000, expected 1000 and passes with the change. Since this changes the result of an existing path, I'll run it through my PPSA21564 check before merging; ready to merge from my side otherwise.

@adriadam10

Copy link
Copy Markdown
Collaborator

@oneandonlydean measured on RDNA2 hardware (gfx1035), confirming the fix: on LDS, ds_append and ds_consume update the dword at the instruction offset and M0 plays no part, with M0 = 0, 0x0040ffff, 0x00400000 (size 0), 0x00100008, 0x00000004 and 0xffffffff alike. Every active lane gets the pre-operation value, the counter moves by the active lane count (32 or 16), and inactive lanes keep their destination. That is what this PR does.

@adriadam10

Copy link
Copy Markdown
Collaborator

Checked against gfx1035 hardware (M0 plays no part on LDS), build + full ctest on RADV, and the new test fails without the change; merging.

@adriadam10
adriadam10 merged commit 8ccb159 into boykopovar:main Oct 6, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants