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

Open
opened 2026-11-26 04:51:57 +00:00 by github-bot · 2 comments
Owner

@⁠microwave805 on GitHub · 2026-11-24 15:12 UTC · original

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.

**@⁠microwave805** on GitHub · 2026-11-24 15:12 UTC · [original](https://github.com/xcl-dev/xcl/issues/1361) 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:51:57 +00:00
Author
Owner

@⁠tiagonogueiraa-bit on GitHub · 2026-11-24 15:14 UTC · original

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

**@⁠tiagonogueiraa-bit** on GitHub · 2026-11-24 15:14 UTC · [original](https://github.com/xcl-dev/xcl/issues/1361#issuecomment-7135686894) 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).
Author
Owner

@⁠Rednaxeh on GitHub · 2026-11-24 15:23 UTC · original · edited 2026-11-24 15:48 UTC

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

**@⁠Rednaxeh** on GitHub · 2026-11-24 15:23 UTC · [original](https://github.com/xcl-dev/xcl/issues/1361#issuecomment-7135954313) · edited 2026-11-24 15:48 UTC 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).
This discussion has been locked. Commenting is limited to contributors.
No description provided.