shader: lower ds_append, ds_consume and the addtid LDS accesses #134

Manually merged
github merged 1 commit from github-bot/gh-134 into main 2026-11-26 19:08:27 +00:00 AGit
Owner

@⁠Olima34 on GitHub · 2026-11-26 18:25 UTC · original
Olima34:fix/shader-lds-address-causes → main · +284 −1 · 4 files · 1 commits · merged 2026-11-26 19:01 UTC

"an LDS address" refused 3 unique compute shaders in tester log #123. It covers the DS instructions that take their address from M0 rather than from a VGPR. They are now lowered, from AMD's RDNA2 ISA (the PDF on docs.amd.com) and LLVM.

Sources.

  • ISA 12.13, DS_APPEND (62) / DS_CONSUME (61): "addr = M0.base + offset; rtnval = LDS(addr); LDS(addr) = LDS(addr) ± countbits(valid mask); GPR[VDST] = rtnval; // return to all valid threads", done as "one atomic add for first valid lane and broadcast result to all valid lanes".
  • For LDS, M0 holds the byte address itself: LLVM's AMDGPUDAGToDAGISel::SelectDSAppendConsume copies the pointer into M0 and puts the constant in the offset field.
  • ISA 10.4 / 12.13, ADDTID: LDS_BASE + {OFFSET1,OFFSET0} + M0[15:0] + TID*4.

Lowering (lower.rs, ds_by_m0, inside the instruction's guard):

  • ds_append/ds_consume:
    • take the count of the active lanes (popcount of a ballot);
    • one atomicrmw add/sub of it at M0 + offset, done by waveIsFirstLane;
    • the pre-op value is broadcast with waveReadLaneFirst.
  • ds_read_addtid_b32/ds_write_addtid_b32 read or write the dword at (M0 & 0xFFFF) + offset + 4 × the console lane. With a 32-lane host the lane is console_flat & 63, so a console wave64 split into two host waves still addresses by console lane.
  • These opcodes set console_lanes. An append or consume where the host splits a console wave stays refused, as "ds_append or ds_consume in a console wave the host splits": the console makes one atomic for the whole wave, and two host waves would make two.
  • ds_gws stays refused by name. Addresses are taken as aligned and in bounds, like the other LDS accesses. This is a marked shortcut:: the ISA clamps append/consume by M0's size.

Tests.

  • gfx_refusal_tests::lds_instructions_addressed_by_m0_translate: wave32 append, consume and read_addtid; a wave64 program of 32 threads on a 32-lane host lowers; wave64 with 64 threads on a 32-lane host is refused.
  • victory-gpu addr_tests::lds_append_consume_and_addtid_follow_m0_on_the_gpu, run on Metal:
    • wave32: 20 of 32 lanes append twice at M0 = 8 and get 0, then 20; all 32 lanes consume and get 40;
    • addtid with M0 = 64 and offset 4 writes each lane id, read back both by address and by read_addtid;
    • a wave64 group of 64 (two host waves) gets its addtid ids 0..63 right.
      Without the change it fails with the refusal.

victory-shader and victory-gpu tests (arm64, Metal), Windows clippy and cargo xtask policy pass.

(This PR first only split the refusal by opcode; with the ISA in hand it now lowers them.)

**@⁠Olima34** on GitHub · 2026-11-26 18:25 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/134) `Olima34:fix/shader-lds-address-causes` → `main` · +284 −1 · 4 files · 1 commits · merged 2026-11-26 19:01 UTC "an LDS address" refused 3 unique compute shaders in tester log #123. It covers the DS instructions that take their address from M0 rather than from a VGPR. They are now lowered, from AMD's RDNA2 ISA (the PDF on docs.amd.com) and LLVM. **Sources.** - ISA 12.13, `DS_APPEND` (62) / `DS_CONSUME` (61): "addr = M0.base + offset; rtnval = LDS(addr); LDS(addr) = LDS(addr) ± countbits(valid mask); GPR[VDST] = rtnval; // return to all valid threads", done as "one atomic add for first valid lane and broadcast result to all valid lanes". - For LDS, M0 holds the byte address itself: LLVM's `AMDGPUDAGToDAGISel::SelectDSAppendConsume` copies the pointer into M0 and puts the constant in the offset field. - ISA 10.4 / 12.13, `ADDTID`: `LDS_BASE + {OFFSET1,OFFSET0} + M0[15:0] + TID*4`. **Lowering** (`lower.rs`, `ds_by_m0`, inside the instruction's guard): - `ds_append`/`ds_consume`: - take the count of the active lanes (popcount of a ballot); - one `atomicrmw add`/`sub` of it at `M0 + offset`, done by `waveIsFirstLane`; - the pre-op value is broadcast with `waveReadLaneFirst`. - `ds_read_addtid_b32`/`ds_write_addtid_b32` read or write the dword at `(M0 & 0xFFFF) + offset + 4 × the console lane`. With a 32-lane host the lane is `console_flat & 63`, so a console wave64 split into two host waves still addresses by console lane. - These opcodes set `console_lanes`. An append or consume where the host splits a console wave stays refused, as "ds_append or ds_consume in a console wave the host splits": the console makes **one** atomic for the whole wave, and two host waves would make two. - `ds_gws` stays refused by name. Addresses are taken as aligned and in bounds, like the other LDS accesses. This is a marked `shortcut:`: the ISA clamps append/consume by M0's size. **Tests.** - `gfx_refusal_tests::lds_instructions_addressed_by_m0_translate`: wave32 append, consume and read_addtid; a wave64 program of 32 threads on a 32-lane host lowers; wave64 with 64 threads on a 32-lane host is refused. - victory-gpu `addr_tests::lds_append_consume_and_addtid_follow_m0_on_the_gpu`, run on Metal: - wave32: 20 of 32 lanes append twice at M0 = 8 and get 0, then 20; all 32 lanes consume and get 40; - addtid with M0 = 64 and offset 4 writes each lane id, read back both by address and by `read_addtid`; - a wave64 group of 64 (two host waves) gets its addtid ids 0..63 right. Without the change it fails with the refusal. victory-shader and victory-gpu tests (arm64, Metal), Windows clippy and `cargo xtask policy` pass. (This PR first only split the refusal by opcode; with the ISA in hand it now lowers them.)
ds_append, ds_consume, the addtid pair and the ds_gws group take their LDS address from M0 (and the lane), not an address operand, and were all refused as "an LDS address". The refusal now names the opcode, so the next tester log tells which one the three compute programs of issue #123 use. Nothing is lowered.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 18:25:36 +00:00
github-bot changed title from shader: name the LDS instruction without an address when it is refused to shader: lower ds_append, ds_consume and the addtid LDS accesses 2026-11-26 18:43:14 +00:00
github-bot force-pushed github-bot/gh-134 from 33a3a61be8 to 6d535ab275 2026-11-26 18:43:15 +00:00 Compare
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 18:56 UTC · original · approved these changes

Reviewed and tested together with the other open PRs on main (macOS x86_64 tests for shader, hle, loader and gpu, the setup shell tests, and Windows clippy). Thanks!

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 18:56 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/134#pullrequestreview-6079230212) · **approved these changes** Reviewed and tested together with the other open PRs on main (macOS x86_64 tests for shader, hle, loader and gpu, the setup shell tests, and Windows clippy). Thanks!
github manually merged commit bc2e2e334c into main 2026-11-26 19:08:27 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.