xshader: deterministic HLSL emission; GDS base from M0[31:16] per ISA #21
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
library
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/omonkeyz--xbox-emulator!21
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/21/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@QuentinDanblon on GitHub · 2026-11-21 17:37 UTC · original
QuentinDanblon:fix/hlsl-deterministic→main· +61 −11 · 4 files · 1 commits · closed without mergingLibrary / 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_bodybuilds its loop list by iterating aHashMap, so thebool 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 otherHashMap/HashSetiteration inhlsl.rsandhlsl/*.rs; none of them affects the emitted text.New test
tests/synthetic.rs::loops_translate_deterministicallytranslates a 6-loop compute shader 17 times. It fails without the sort and passes with it.2. GDS base = M0[31:16]
hlsl/gds.rsusedm0 & 0xffffas 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 useM0.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(onlyoffsetfor append/consume);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.rsnow builds M0 asbase << 16 | size. It covers nonzero bases (0x100, 0x200, 0xfbf8 at the buffer limit) and a size-clamp case where half the lanes fall outsidesize. The new tests fail against the oldgds.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/2cargo test -p tombox-xshader --release --test synthetic: 8/8cargo test -p tombox-xshader --release --lib: 8/8full
--test exec: no new failures. The 4vop::basicfailures are already present at226938cand are fixed in a separate PR.--test fixtureswas not run:analysis/shadersis not in the repo (see #13).cargo test -p tombox-xshaderpasses (except the pre-existing failures above)No game or console files and no binaries included
No fake success; anything not possible is left
missingwith a noteDeterministic 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,2c729eff5dThe 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.@QuentinDanblon on GitHub · 2026-11-21 21:03 UTC · original
Merged in the source sync
1159c96(adapted). Thanks! Closing.Pull request closed