Recompiler: Em::scratch treats GFX10.30 SADDR == 0x7F (ST mode) as VADDR, adding v0 to fixed-offset spills #1351

Open
opened 2026-11-24 15:12:19 +00:00 by microwave805 · 2 comments
microwave805 commented 2026-11-24 15:12:19 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by microwave805 on 2026-11-24T15:12:19Z.


Checked on main (930e8b4).

Problem

Em::scratch (crates/xcl/src/gpu/recomp/emit.rs:3845-3853) treats SADDR == 0x7F identically to SADDR == 0x7D, using v[ADDR] (which is encoded as 0, i.e., v0) as the base address:

let saddr = f("SADDR");
let base = if saddr != 0x7D && saddr != 0x7F { self.sreg(saddr) } else { self.vreg(f("ADDR")) };
let raw = f("OFFSET") as u32;
let off = (((raw << 20) as i32) >> 20) as u32;
let t = self.t();
self.line(&format!("uint {t}_a = {base} + {};", hex(off)));

On RDNA2 / GFX10.30 (HasFlatScratchSTMode), SADDR == 0x7D (NULL) selects VADDR-only mode (vaddr + offset; see tests/data/llvm-amdgpu/gfx10_flat.txt:793: scratch_load_dword v1, v2, off → SADDR=0x7d, ADDR=2), whereas SADDR == 0x7F selects ST (immediate-only, off, off) mode where both vaddr and saddr are disabled (ADDR=0) and the effective address is purely 0 + offset (see tests/data/llvm-amdgpu/gfx1030_new.txt:174, 177, 240, e.g., scratch_store_dword off, v2, off offset:2047 → [0xff,0x47,0x70,0xdc,0x00,0x02,0x7f,0x00]).

Minimal trigger

Any GFX10.30 shader with constant-offset VGPR spills:

  1. scratch_store_dword off, v2, off offset:16 (SADDR=0x7F, ADDR=0, OFFSET=16)
  2. Modify v0
  3. scratch_load_dword v2, off, off offset:16

XCL emits uint t_a = v0 + 0x10u; for both instead of 0x10u, silently corrupting private scratch slots and reloading garbage whenever v0 changes between store and load (or initially holds non-zero values like SV_VertexID / PS barycentrics).

Suggested fix

Split 0x7F and 0x7D in Em::scratch:

let base = match saddr {
    0x7F => "0u".into(),
    0x7D => self.vreg(f("ADDR")),
    _ => self.sreg(saddr),
};

(Note: the existing test in crates/xcl/src/gpu/recomp/tests.rs around gpu_more_atomics_sub_dword_scratch_valu currently encodes VADDR mode with SADDR=0x7F, ADDR=2 and should be updated to use 0x7D for VADDR mode alongside a test of 0x7F ST mode.)


AI use: Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on 930e8b4 before opening.

<!-- greyfinch-sync:ivall/xcl:issue:6584982668 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/1361). Originally posted by [microwave805](https://github.com/microwave805) on 2026-11-24T15:12:19Z. --- Checked on `main` (`930e8b4`). ### Problem `Em::scratch` (`crates/xcl/src/gpu/recomp/emit.rs:3845-3853`) treats `SADDR == 0x7F` identically to `SADDR == 0x7D`, using `v[ADDR]` (which is encoded as `0`, i.e., `v0`) as the base address: ```rust let saddr = f("SADDR"); let base = if saddr != 0x7D && saddr != 0x7F { self.sreg(saddr) } else { self.vreg(f("ADDR")) }; let raw = f("OFFSET") as u32; let off = (((raw << 20) as i32) >> 20) as u32; let t = self.t(); self.line(&format!("uint {t}_a = {base} + {};", hex(off))); ``` On RDNA2 / GFX10.30 (`HasFlatScratchSTMode`), `SADDR == 0x7D` (`NULL`) selects VADDR-only mode (`vaddr + offset`; see `tests/data/llvm-amdgpu/gfx10_flat.txt:793`: `scratch_load_dword v1, v2, off` → `SADDR=0x7d, ADDR=2`), whereas **`SADDR == 0x7F`** selects ST (immediate-only, `off, off`) mode where both `vaddr` and `saddr` are disabled (`ADDR=0`) and the effective address is purely **`0 + offset`** (see `tests/data/llvm-amdgpu/gfx1030_new.txt:174, 177, 240`, e.g., `scratch_store_dword off, v2, off offset:2047` → `[0xff,0x47,0x70,0xdc,0x00,0x02,0x7f,0x00]`). ### Minimal trigger Any GFX10.30 shader with constant-offset VGPR spills: 1. `scratch_store_dword off, v2, off offset:16` (`SADDR=0x7F, ADDR=0, OFFSET=16`) 2. Modify `v0` 3. `scratch_load_dword v2, off, off offset:16` XCL emits `uint t_a = v0 + 0x10u;` for both instead of `0x10u`, silently corrupting private scratch slots and reloading garbage whenever `v0` changes between store and load (or initially holds non-zero values like `SV_VertexID` / PS barycentrics). ### Suggested fix Split `0x7F` and `0x7D` in `Em::scratch`: ```rust let base = match saddr { 0x7F => "0u".into(), 0x7D => self.vreg(f("ADDR")), _ => self.sreg(saddr), }; ``` *(Note: the existing test in `crates/xcl/src/gpu/recomp/tests.rs` around `gpu_more_atomics_sub_dword_scratch_valu` currently encodes VADDR mode with `SADDR=0x7F, ADDR=2` and should be updated to use `0x7D` for VADDR mode alongside a test of `0x7F` ST mode.)* --- **AI use:** Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on `930e8b4` before opening.
tiagonogueiraa-bit commented 2026-11-24 16:06:57 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by tiagonogueiraa-bit on 2026-11-24T15:14:21Z.


claiming: Em::scratch takes SADDR = 0x7F (ST mode) as address 0 + offset and keeps 0x7D as VADDR + offset. I checked both against llvm-mc -mcpu=gfx1030: scratch_load_dword v1, v2, off encodes SADDR=0x7d, ADDR=2, and scratch_load_dword v1, off, off offset:16 encodes SADDR=0x7f, ADDR=0. The existing test's VADDR case moves to 0x7D, and an ST-mode test is added. PR within the hour.

Written with Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:ivall/xcl:comment:7135686894 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/1361#issuecomment-7135686894). Originally posted by [tiagonogueiraa-bit](https://github.com/tiagonogueiraa-bit) on 2026-11-24T15:14:21Z. --- claiming: `Em::scratch` takes `SADDR = 0x7F` (ST mode) as address `0 + offset` and keeps `0x7D` as VADDR + offset. I checked both against `llvm-mc -mcpu=gfx1030`: `scratch_load_dword v1, v2, off` encodes `SADDR=0x7d, ADDR=2`, and `scratch_load_dword v1, off, off offset:16` encodes `SADDR=0x7f, ADDR=0`. The existing test's VADDR case moves to `0x7D`, and an ST-mode test is added. PR within the hour. Written with Claude Code (Claude Opus 5.5).
Rednaxeh commented 2026-11-24 16:07:15 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by Rednaxeh on 2026-11-24T15:23:49Z.


Withdrawn: @tiagonogueiraa-bit claimed this nine minutes before my comment and #1365 is already up; my search predated both. The reference-data pointers above still apply to that PR (gfx10_flat.txt:793, gfx1030_new.txt:240). Since #1365 has no GPU run yet, I will post one from RDNA3 there.

Written with Claude Code (Claude Fable 5.1).

<!-- greyfinch-sync:ivall/xcl:comment:7135954313 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/1361#issuecomment-7135954313). Originally posted by [Rednaxeh](https://github.com/Rednaxeh) on 2026-11-24T15:23:49Z. --- Withdrawn: @tiagonogueiraa-bit claimed this nine minutes before my comment and #1365 is already up; my search predated both. The reference-data pointers above still apply to that PR (`gfx10_flat.txt:793`, `gfx1030_new.txt:240`). Since #1365 has no GPU run yet, I will post one from RDNA3 there. Written with Claude Code (Claude Fable 5.1).
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
xcl-dev/xcl#1351
No description provided.