Skip to content

Recompiler: read shader rodata on sub-dword global/flat loads in HLSL and Direct DXIL - #3368

Merged
throny merged 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/flat-subdword-rodata
Dec 1, 2026
Merged

throny merged 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/flat-subdword-rodata

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

What changed

  • crates/xcl/src/gpu/dxil/lower_mem.rs: pass rodata: bool into ld_raw_u32_guarded so flat sub-dword loads (global_load_ubyte, global_load_sbyte, global_load_ushort, global_load_sshort, global_load_short_d16, global_load_short_d16_hi, and the corresponding flat_load_* ops) route their aligned dword reads through rodata_loads before sub-dword shift/merge, while keeping mubuf callers on rodata = false.
  • crates/xcl/src/gpu/recomp/emit.rs: emit an xcl_gload_u32 helper in rodata_loader that combines xcl_gload2(a & ~3u) across aligned and unaligned byte offsets, and use it in flat sub-dword loads whenever shader .rodata is present.
  • crates/xcl/src/gpu/recomp/tests.rs: add subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil, covering HLSL codegen (with and without .rodata), Direct DXIL translation and validation (1-window and 2-window), and GPU execution across all six sub-dword load opcodes, aligned and unaligned 4-byte boundary crossings, .rodata hits, and non-rodata global memory on HLSL and Direct DXIL paths.

Why

Dword-wide global/flat loads (global_load_dword*, flat_load_dword*) already checked embedded shader .rodata (xcl_gload* in HLSL and rodata_loads in Direct DXIL) when reading constant tables placed after the shader program, because on PC those addresses are not backed by the shader binary in VRAM. Sub-dword global/flat loads (flat_small) bypassed that lookup and read ld_raw directly, returning wrong bytes whenever a shader indexed a byte or 16-bit constant table in .rodata (including unaligned 16-bit loads spanning two dwords).

Tested

Hardware: NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074), AMD Ryzen 9 9950X, Windows 11 Pro (Build 26200), rustc 1.99.0, DXC 1.9.2609.5 (dxil 1.9.2609.5).

  • cargo check --workspace --all-targets: passed with 0 errors and 0 new warnings.
  • Targeted GPU tests (cargo test -p xcl --lib rodata_in_hlsl_and_direct_dxil gpu_loads_read_the_constant_data_after_the_program): 2 passed, 0 failed.
  • Full workspace test suite (cargo test --workspace -- --test-threads=4, GPU enabled): exited 0 (0 failed).
  • Release build (cargo build --release -p xcl): succeeded.
  • 150 s real-title smoke run on 8645f312c3c868b40f25774e3f0055f74db12123 (XCL_DIRECT_DXIL=all, XCL_HEAP_WINDOWS=1, XCL_DRED=1; 0 minidumps, 0 System display events):
xcl-smoke v1
commit: 8645f312c3c868b40f25774e3f0055f74db12123
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1587 (PresentX 1587)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 33496 MB
  • 150 s main ancestor baseline smoke run (f0e8fdd5c3942ddd399d5ee11384c8595c5aacff):
xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB

Not tested

  • AMD or Intel GPUs, native Wave64 hardware (RTX 5070 Ti runs split wave64), and titles beyond the single 150 s smoke title.
  • Sub-dword flat_load_* hitting LDS via the flat aperture (unchanged; rodata check only matches the shader binary's .rodata GPU VA range).

AI use

Used an AI coding assistant to audit lower_mem.rs and emit.rs, draft the fix and unit/GPU tests, and run the verification suite. All diffs, test assertions, and hardware results were reviewed and verified locally.

@github-actions github-actions Bot added area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Nov 29, 2026
@tiagonleal

tiagonleal commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

merge-check
base: f97120d
head: 8645f31
merge: clean
build: cargo build --workspace -> ok (also cargo build --release -p xcl)
tests: cargo test --workspace --no-fail-fast -j 8 -- --test-threads=4 -> 2842 passed, 0 failed, 51 ignored; SKIPPED: 24 lines (22 no wave64, 2 no heap windows), the same as on the base; failing: none. Base alone on this PC: 2841 passed, 0 failed, 51 ignored. The new test gpu::recomp::tests::subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil ran on this card (no SKIPPED line for it) and passed.
review: whole diff read (+122/-6, gpu/dxil/lower_mem.rs, gpu/recomp/emit.rs, the test). Byte and word global and flat loads now go through the embedded rodata in both backends, as the dword loads already did; before they read plain memory. Both backends take the two dwords from the aligned address and select the bytes little-endian, and each of the two dwords gets its own rodata range check, so a load at the end of the rodata window reads its upper bytes from memory in both. MUBUF and scratch loads are left alone. The test compiles and runs the HLSL and the direct DXIL explicitly, with 1 and 2 windows and with heap windows, so it covers both backends here. It does not depend on #3434.
performance: one extra branch per sub-dword global load, only in programs that have rodata. Measured with scripts/smoke.ps1, 60 s of a retail title, release: 1707 frames and 16127 MB peak commit against 1635 and 1680 frames, 15366 and 16673 MB on the base. No device removal, no crash.
verdict: safe to merge
hardware / tool and model: RTX 5070 Ti 16 GB (617.42, wave32), Ryzen 7 9800X3D, 32 GB, Windows 11 26300, rustc 1.100.0 / Claude Code (Claude Fable 5.2 ran the build, suite, tests and smoke and wrote this; Claude Opus 5.5 read the diff first, and I checked its main points in the merged source)

  • Tiago

@github-actions github-actions Bot added needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU labels Nov 30, 2026
@Edgaras0404

Copy link
Copy Markdown

Tested

  • Hardware & OS: AMD Radeon RX 6700 XT (RDNA2, driver 32.0.21045.29003), AMD Ryzen 5 5600X 6-Core Processor, Windows 10 Pro 22H2 (build 19045)
  • Toolchain & build: rustc 1.100.1 (20ddd0171 2026-11-25), dxcompiler 1.9.2609.5 (01b62ad4) / dxil 1.9.2609.5, D3D12 Agility SDK, debug
  • Commits: PR #3368 head 8645f312 trial-merged onto main 9dfbe3ea (crates/xcl/src/gpu/dxil/lower_mem.rs, crates/xcl/src/gpu/recomp/emit.rs, crates/xcl/src/gpu/recomp/tests.rs: 3 files changed, 122 insertions(+), 6 deletions(-))
  • cargo test -p xcl --lib subdword -- --test-threads=1 --nocapture: 1 passed; 0 failed; 0 ignored; 0 measured; 2769 filtered out (0 SKIPPED — gpu::recomp::tests::subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil ran on RX 6700 XT via DXC)
  • Single-threaded GPU module reruns (--test-threads=1):
    • cargo test -p xcl --lib gpu::dxil -- --test-threads=1: 305 passed; 2 failed; 3 ignored; 0 measured; 2460 filtered out (failures match main 9dfbe3ea baseline: gpu_compact_form_with_micro_blocks_and_literal_table_runs_as_the_plain_form, gpu_direct_image_get_resinfo_d16)
    • cargo test -p xcl --lib gpu::recomp -- --test-threads=1: 521 passed; 3 failed; 21 ignored; 0 measured; 2225 filtered out (failures match main 9dfbe3ea baseline: f32_programs_on_the_gpu, gpu_f32_compares_with_nan, gpu_float_compares_random)
  • cargo test --workspace --no-fail-fast -- --test-threads=4:
    • All passing workspace binaries finished with 0 failed (including tests\gpu_compute_e2e.rs: 9 passed; 0 failed with SKIPPED heap_window_out_of_memory_falls_back_to_the_switch_form: no heap windows; tests\console_shaders.rs: 3 passed; 0 failed; 4 ignored; tests\heap_probe.rs: 0 passed; 0 failed; 4 ignored; tests\xg_oracle.rs: 6 passed; 0 failed; 12 ignored).
    • tests\gpu_two_pass_cache_e2e.rs: 0 passed; 1 failed; 0 ignored (two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs, pre-existing failure on main 9dfbe3ea).
    • -p xcl --lib (2770 tests): under --test-threads=4, exited abnormally after gpu::dxil::cfg::tests::loop_parents_of_a_loop_at_the_entry (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION); when rerun single-threaded above, gpu::dxil and gpu::recomp both completed with only the 5 pre-existing 9dfbe3ea failures.

Not tested

  • Remaining non-gpu::dxil/non-gpu::recomp -p xcl --lib tests on the merged tree (since -p xcl --lib exited early under --test-threads=4)
  • Opt-in XCL_HEAP_WINDOWS=1 tests
  • Release build and scripts/smoke.ps1
  • NVIDIA, Intel, RDNA3, and RDNA4 GPUs

AI use

  • Gemini formatted this test report from the author's directly run commands and outputs (no code or diffs written).

@saintwolf

Copy link
Copy Markdown
Contributor

merge-check claim
head: 8645f31

@saintwolf

Copy link
Copy Markdown
Contributor

merge-check
base: 9dfbe3e
head: 8645f31
merge: clean (trial merge 6be33c1741cc1432b36f8b2325493bcf9506fd72; git diff --check clean; +122/-6 in crates/xcl/src/gpu/dxil/lower_mem.rs, crates/xcl/src/gpu/recomp/emit.rs, tests.rs)
build: cargo build --workspace -> ok (39 pre-existing warnings, none from the PR's files)
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2886 passed, 1 failed, 51 ignored; SKIPPED: 27 (22 "no wave64" — wave32-only GPU, 2 "no heap windows", 1 "cannot disable the driver cache (Developer Mode)", 2 "no xgameruntime.dll"); failing: gpu::dxil::msaa_tests::gpu_direct_image_get_resinfo_d16, pre-existing on this machine (fails identically on base 9dfbe3e, same "2D D16 resinfo clobbered v2/v3" assertion). The new test subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil ran and passed here (wave64 in split two-wave32 form, 1-window and 2-window, heap windows on and off, decoy bytes planted at the rodata offset in window 0).
review: read the whole diff and rodata_loads in the merged tree. Sub-dword global/flat loads (ubyte/sbyte/ushort/sshort/short_d16/short_d16_hi) now go through the embedded shader .rodata like the dword loads already did: in Direct DXIL, ld_raw_u32_guarded takes the loaded dword pair to rodata_loads only for the FLAT_GLBL path (the MUBUF and d16-store callers pass false, unchanged); rodata_loads range-checks each of the two dwords individually, so a load straddling the rodata window boundary reads its out-of-range dword from memory. In HLSL, the d16 global-load path uses a new xcl_gload_u32 (aligned xcl_gload2 + rotate, identical to ld_raw's unaligned extraction) only when a non-empty rodata blob exists; the test asserts xcl_gload_u32 is absent without rodata and present with it. The test's decoy bytes prove the values come from the rodata table, not the raw window. No unsafe, no new dependencies. #3434 (same author, straddling multi-dword rodata loads) is a distinct change that touches the same rodata_loader/lower_window region and will need a small rebase against this one, whichever lands second.
performance: none measurable beyond one extra branch per sub-dword global load in programs that have rodata; author's smoke pair 1707 vs 1635 frames.
verdict: safe to merge
hardware / tool and model: RTX 4070 SUPER (driver 32.0.16.2140), Ryzen 7 7800X3D, Windows 11 build 26300, Rust 1.100.0 / OpenCode, Qwen 3.8 27B

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Nov 30, 2026

@andresd-cerebras andresd-cerebras left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Withdrawn.

… Direct DXIL

HLSL's xcl_gload_u32 looks up each of the two dwords on its own, as Direct DXIL's rodata_loads does: through xcl_gload2 it decided with the first dword's address only, so a 16-bit load starting 1 byte before a rodata block took its upper byte from memory in HLSL and from the block in Direct DXIL. The test adds 16-bit loads across the block's start and end edges.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Kalmarv
Kalmarv force-pushed the fix/flat-subdword-rodata branch from 8645f31 to 6232c2a Compare November 30, 2026 23:30
@github-actions github-actions Bot removed the ready-to-merge Has 2 independent safe merge-checks on current head (#3041) label Nov 30, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: d0832e6
head: 6232c2a
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2890 passed, 1 failed, 51 ignored (lib binary from a rerun of cargo test -p xcl --lib --no-fail-fast -- --test-threads=4: on the first full run it aborted with 0xc0000409 after a non-unwinding panic in thread xcl-pso-ahead-1 (windows-core interface.rs:41, misaligned pointer dereference); code the PR does not touch, not seen on the rerun, not checked on base); SKIPPED: 28 (22 "no wave64", 2 "no heap windows", 2 "no d3d12SDKLayers.dll", 1 "no D3D12 debug layer", 1 "cannot disable the driver cache (Developer Mode)"); failing: gpu::recomp::tests::subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil, the PR's new test (does not exist on base), fails every run: "HLSL 2-window lane 0: ushort across the block's start edge, left: 1024, right: 1185"
review: read the whole diff. (1) The edge lanes added in this push expect, in the 2-window and heap-window runs, the memory bytes next to the rodata block from BEFORE/AFTER, planted at w0 + block - 4 (tests.rs:5047, assertion tests.rs:5015); with several windows a code address is in none of them, so both backends read 0 there. Probed on this GPU: HLSL 2-window, heap windows and Direct DXIL 2-window all return 0x0400 / 0x0011 for the start / end edge, 1-window returns the expected 0x04A1 / 0x0511 in both backends. The backends agree; the test's multi-window expectation is wrong and the test fails on any GPU that runs it (CI has no GPU and skips it). (2) The code is right: xcl_gload_u32 (emit.rs:5981) now looks up each of the two dwords on its own like rodata_loads, which fixes the start-edge HLSL/DXIL mismatch from the earlier review; MUBUF and d16-store callers keep rodata=false (lower_mem.rs:1304, 1321), only FLAT/GLOBAL loads pass true (lower_mem.rs:2021). (3) The PR description check fails: both smoke blocks and the Tested section are for the old head 8645f31; AI use does not name the tool or model; the description still says xcl_gload_u32 uses xcl_gload2. (4) Nits: commit prefix "recomp:" instead of "Recompiler:"; ubyte_d16/sbyte_d16/hi and non-global flat_load* are not exercised by the test. No retail title on this machine, so no smoke run.
performance: reasoned only: two rodata branch chains per sub-dword global/flat load, only in shaders that have rodata (replaces one Load4 + Load). No smoke run here (no retail title); the author's smoke pair is for the old head 8645f31, not this one.
verdict: not yet: the new GPU test fails at 6232c2a (wrong multi-window expectation for the bytes outside the block at a code address), and there is no smoke run for the current head (PR description check red)
hardware / tool and model: NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.2114 / 621.14), AMD Ryzen 7 7800X3D, Windows 11 Pro 10.0.26300, rustc 1.100.0 MSVC, DXC 1.9.2602.17 / Claude Code, Claude Opus 5.5

@github-actions github-actions Bot added the needs-pr-check Failing the PR description check (CONTRIBUTING.md) label Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head 6232c2a, diff read, not a merge-check: the PR description check fails, so not built or run here yet):

Fine: the sub-dword handling is right (u8/i8/u16/i16 with sign extension, d16 hi/lo, the byte offset within the dword), and a load across the end of a block takes its last byte from the block and the next from memory. buffer_load_* is left alone on purpose, and the HLSL helper is only emitted when .rodata has data. The checks stay limited to programs with s_getpc_b64 (f80cc55). The test fails without the fix, covers both backends with one and two memory windows plus HLSL heap windows, and uses skip_test.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Test report (not a merge-check: PR description red)
base: b2b1f10
head: 6232c2a
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2902 passed, 1 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: gpu::recomp::tests::subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil
review: not repeated here; my review of this same head is at #3368 (comment), and its findings stand. This run adds what reading couldn't: whether the change merges, builds and passes the whole suite on current main.
performance: no smoke run.
assessment: its own new test fails on this GPU: gpu::recomp::tests::subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil panics at tests.rs ~5754, "HLSL 2-window lane 0: ushort across the block's start edge", left 1024, right 1185. Correction to an earlier version of this comment: the PR's code is right and the test's expectation is wrong. With several windows a code address reads 0 outside the block, by design, in both backends. Details, measurements and a test-only fix: #3368 (comment). The rest of the suite passes, and the findings in my review stand
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Follow-up to my test report (head 6232c2a, merged onto main 6bfb5c5): the failing check is in the new test, not in the PR's code or in main.

  • Why it fails: in subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil, lane 0's ushort at block - 1 takes its low byte from the word just before the block. That address is a code address (s_getpc_b64, high dword 0xC0DE). With several memory windows a code address is in none of them, so it reads 0 by design (emit.rs ~2041-2049, lower_window.rs ~54-58). Main's own test relies on the same rule (tests.rs ~5650, "a code address is in none of them"). So the right value with 2 windows is 0x0400 for lane 0 and 0x0011 for lane 1. The test expects window 0's bytes beside the block (BEFORE / AFTER), which neither backend reads.
  • Measured here (RTX 5070 Ti, wave32): HLSL 1-window and Direct DXIL 1-window give 0x04A1 / 0x0511, as worked out by hand. HLSL 2-window, HLSL heap windows and Direct DXIL 2-window all give 0x0400 / 0x0011. Direct DXIL 2-window only looked fine in my report because the HLSL 2-window check panicked before it ran.
  • Suggested change (test only): expect 0 beside the block when there are several windows. The decoys stay in window 0, so the test still fails if a code address ever starts reading window 0:
-    let check = |r: &runner::KernelResult, what: &str| {
+    // `windows`: several memory windows, where a code address is in none of them, so the
+    // bytes beside the block read 0
+    let check = |r: &runner::KernelResult, what: &str, windows: bool| {
@@
-            let edge = if l % 2 == 0 { (BEFORE >> 24) | 0x0400 } else { (table[3] >> 24) | (AFTER & 0xFF) << 8 };
+            let (before, after) = if windows { (0, 0) } else { (BEFORE, AFTER) };
+            let edge = if l % 2 == 0 { (before >> 24) | 0x0400 } else { (table[3] >> 24) | (after & 0xFF) << 8 };
@@
-    check(&runner::run(&dxil_hlsl, &st, &mem).unwrap(), "HLSL 1-window");
+    check(&runner::run(&dxil_hlsl, &st, &mem).unwrap(), "HLSL 1-window", false);
@@
-    check(&runner::run(&k1.dxil, &st, &mem).unwrap(), "Direct DXIL 1-window");
+    check(&runner::run(&k1.dxil, &st, &mem).unwrap(), "Direct DXIL 1-window", false);
@@
-        check(&runner::run_in_arena(&dxil2, &st, &arena).unwrap(), if heap { "HLSL heap windows" } else { "HLSL 2-window" });
+        check(&runner::run_in_arena(&dxil2, &st, &arena).unwrap(), if heap { "HLSL heap windows" } else { "HLSL 2-window" }, true);
@@
-    check(&runner::run_in_arena(&k2.dxil, &st, &arena).unwrap(), "Direct DXIL 2-window");
+    check(&runner::run_in_arena(&k2.dxil, &st, &arena).unwrap(), "Direct DXIL 2-window", true);
  • With the change: the test passes 3 runs out of 3 on the merged tree, and cargo test -p xcl --lib gpu::recomp -- --test-threads=4 gives 530 passed, 0 failed, 21 ignored, with 21 SKIPPED (no wave64). With the PR's emit.rs / lower_mem.rs reset to main, the changed test still fails (first on the xcl_gload_u32 check, then on the GPU at HLSL 1-window lane 0 off 0: ubyte, 0 vs 5), so it still covers the fix. Not run on wave64 / AMD.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

To get this merged without waiting on another round, I opened #3607: it carries your commit from this PR unchanged (same SHA, you stay the author) plus the one test-only commit from my follow-up above, with a full suite run and a smoke pair on that exact head. If you'd rather keep it here, pushing fb986aec from Foniass/xcl branch ready/3368 onto this branch (a fast-forward) gives the same result, and I'll close #3607.

AI-assisted: Claude Code (Claude Opus 5.5), posted by Foniass.

@throny
throny merged commit 6232c2a into xcl-dev:main Dec 1, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) needs-pr-check Failing the PR description check (CONTRIBUTING.md)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants