xshader: deterministic HLSL emission; GDS base from M0[31:16] per ISA #21

Closed
github-bot wants to merge 1 commit from gh-pr/21/head into gh-pr/21/base
Owner

@⁠QuentinDanblon on GitHub · 2026-11-21 17:37 UTC · original
QuentinDanblon:fix/hlsl-deterministic → main · +61 −11 · 4 files · 1 commits · closed without merging

Library / area: tombox-xshader (HLSL emission, GDS lowering)

Exports changed (exports.txt rows): none

Source of the behaviour: brcly/TomBox 2c729ef (GDAgent, crates declare MIT OR Apache-2.0); AMD RDNA2 ISA guide (30 Nov 2020) §3.7 and §10.5

1. Deterministic HLSL

emit_body builds its loop list by iterating a HashMap, so the bool tb_c{b} declarations come out in hash order. The same shader can then give different HLSL text, and so a different shader-cache key and a recompile. The list is now sorted before emission (port of 2c729ef). I checked every other HashMap/HashSet iteration in hlsl.rs and hlsl/*.rs; none of them affects the emitted text.
New test tests/synthetic.rs::loops_translate_deterministically translates a 6-loop compute shader 17 times. It fails without the sort and passes with it.

2. GDS base = M0[31:16]

hlsl/gds.rs used m0 & 0xffff as the GDS base. The ISA defines GDS M0 as {base[31:16], size[15:0]} in bytes (§3.7 M# descriptor, §10.5 "[15:0] holds SIZE … [31:16] holds BASE"; DS_APPEND/DS_CONSUME use M0.base). LLVM's GWS/append lowering also puts the base in the high half. The existing test was written to the same low-bits assumption. Now:

  • addr = ((m0 >> 16) + vaddr + offset) & ~3 (only offset for append/consume);
  • in range when vaddr + offset < (m0 & 0xffff) and inside the emulated buffer. Out of range: writes are dropped and reads return 0, which is the existing path (§3.6, §10.4.3).

tests/exec_mem/gds.rs now builds M0 as base << 16 | size. It covers nonzero bases (0x100, 0x200, 0xfbf8 at the buffer limit) and a size-clamp case where half the lanes fall outside size. The new tests fail against the old gds.rs (0/2) and pass with the fix (2/2).

I had no title GDS shader to check this against (none in the repo). If you have title evidence that M0 carries the base in the low bits, that would override the ISA reading.

Tests

  • cargo test -p tombox-xshader --release --test exec exec_mem::gds: 2/2

  • cargo test -p tombox-xshader --release --test synthetic: 8/8

  • cargo test -p tombox-xshader --release --lib: 8/8

  • full --test exec: no new failures. The 4 vop::basic failures are already present at 226938c and are fixed in a separate PR.

  • --test fixtures was not run: analysis/shaders is not in the repo (see #13).

  • cargo test -p tombox-xshader passes (except the pre-existing failures above)

  • No game or console files and no binaries included

  • No fake success; anything not possible is left missing with a note

**@⁠QuentinDanblon** on GitHub · 2026-11-21 17:37 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/21) `QuentinDanblon:fix/hlsl-deterministic` → `main` · +61 −11 · 4 files · 1 commits · closed without merging **Library / area:** tombox-xshader (HLSL emission, GDS lowering) **Exports changed (exports.txt rows):** none **Source of the behaviour:** brcly/TomBox [2c729ef](https://github.com/brcly/TomBox/commit/2c729eff5da2c2baa4b1a922df62d9dc9291c40b) (GDAgent, crates declare MIT OR Apache-2.0); AMD RDNA2 ISA guide (30 Nov 2020) §3.7 and §10.5 ### 1. Deterministic HLSL `emit_body` builds its loop list by iterating a `HashMap`, so the `bool tb_c{b}` declarations come out in hash order. The same shader can then give different HLSL text, and so a different shader-cache key and a recompile. The list is now sorted before emission (port of 2c729ef). I checked every other `HashMap`/`HashSet` iteration in `hlsl.rs` and `hlsl/*.rs`; none of them affects the emitted text. New test `tests/synthetic.rs::loops_translate_deterministically` translates a 6-loop compute shader 17 times. It fails without the sort and passes with it. ### 2. GDS base = M0[31:16] `hlsl/gds.rs` used `m0 & 0xffff` as the GDS base. The ISA defines GDS M0 as `{base[31:16], size[15:0]}` in bytes (§3.7 M# descriptor, §10.5 "[15:0] holds SIZE … [31:16] holds BASE"; DS_APPEND/DS_CONSUME use `M0.base`). LLVM's GWS/append lowering also puts the base in the high half. The existing test was written to the same low-bits assumption. Now: - `addr = ((m0 >> 16) + vaddr + offset) & ~3` (only `offset` for append/consume); - in range when `vaddr + offset < (m0 & 0xffff)` and inside the emulated buffer. Out of range: writes are dropped and reads return 0, which is the existing path (§3.6, §10.4.3). `tests/exec_mem/gds.rs` now builds M0 as `base << 16 | size`. It covers nonzero bases (0x100, 0x200, 0xfbf8 at the buffer limit) and a size-clamp case where half the lanes fall outside `size`. The new tests fail against the old `gds.rs` (0/2) and pass with the fix (2/2). I had no title GDS shader to check this against (none in the repo). If you have title evidence that M0 carries the base in the low bits, that would override the ISA reading. ### Tests - `cargo test -p tombox-xshader --release --test exec exec_mem::gds`: 2/2 - `cargo test -p tombox-xshader --release --test synthetic`: 8/8 - `cargo test -p tombox-xshader --release --lib`: 8/8 - full `--test exec`: no new failures. The 4 `vop::basic` failures are already present at 226938c and are fixed in a separate PR. - `--test fixtures` was not run: `analysis/shaders` is not in the repo (see #13). - [x] `cargo test -p tombox-xshader` passes (except the pre-existing failures above) - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note
Deterministic translation: emit_body built its loop list from a HashMap, so the
`bool tb_c{b}` declarations came out in hash order and one shader could translate
to differently ordered HLSL between runs (the compiled-shader cache is keyed by the
HLSL text). The loop list is now sorted. Ported from GDAgent's fix in brcly/TomBox
(MIT OR Apache-2.0), commit 2c729eff5da2c2baa4b1a922df62d9dc9291c40b,
2c729eff5d
The other HashMap iterations in hlsl.rs (back-edge max, continue rewrite,
descriptor join, label activity `any`) are order-independent. Regression test
tests/synthetic.rs::loops_translate_deterministically (six loops, 17 translations
in one process; fails without the sort) replaces upstream's fixture sweep, whose
analysis/shaders fixtures are not in this repo.

GDS: M0 for GDS is { base[31:16], size[15:0] } in bytes (RDNA2 ISA, 30 Nov 2020,
3.7 M# Memory Descriptor and 10.5 Global Data Share). The lowering took the base
from M0[15:0]. It now uses M0[31:16] and bounds the base-relative address by
M0[15:0]: out of range is a GDS address range error (3.11); 10.4.3 applies the LDS
text to GDS and 3.6 makes out-of-range LDS writes dropped and reads zero. The exec
tests put the base in [31:16] with a nonzero size, and add a size-clamp case.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:07:27 +00:00
github-bot closed this pull request 2026-11-26 06:07:27 +00:00
Author
Owner

@⁠QuentinDanblon on GitHub · 2026-11-21 21:03 UTC · original

Merged in the source sync 1159c96 (adapted). Thanks! Closing.

**@⁠QuentinDanblon** on GitHub · 2026-11-21 21:03 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/21#issuecomment-7069201721) Merged in the source sync 1159c96 (adapted). Thanks! Closing.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.