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

Merged
Foniass merged 2 commits from upstream-pr/3607 into main 2026-12-01 22:07:18 +00:00
Foniass commented 2026-12-01 19:12:39 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T19:12:39Z.


Replaces #3368 so it can be merged as is: it carries Kalmarv's commit from #3368 unchanged (same SHA, author kept) plus one test-only commit that fixes the new test's expectation (with several memory windows a code address reads 0 beside the block, in both backends; details in https://github.com/xcl-dev/xcl/pull/3368#issuecomment-7320289131). All credit for the fix goes to Kalmarv.

Sub-dword global/flat loads (flat_small) skipped the lookup in the shader's embedded .rodata that dword loads already do, so a shader indexing a byte or 16-bit constant table placed after its program read the wrong bytes. This routes them through the same lookup in HLSL and Direct DXIL.

What is in it

  • 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.
  • crates/xcl/src/gpu/recomp/tests.rs (second commit, Tests: expect 0 beside the rodata block with several memory windows): with several memory windows a code address is in no window and reads 0 (the window lookup in emit.rs / lower_window.rs; main's gpu_loads_read_the_constant_data_after_the_program relies on the same rule), so the ushort that straddles the block's start or end takes 0 there, not window 0's decoy word. Before it, the test failed on the HLSL 2-window run ("ushort across the block's start edge", 1024 against 1185) although the loads were right.

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

Run by Foniass on head fb986aecec94a839d72cf5bcaf2d5f26fabf6edc merged into main 6bfb5c5153ccc23517daf30aa1071ddb0fadb53c (clean merge): NVIDIA GeForce RTX 5070 Ti (driver 621.14), AMD Ryzen 7 7800X3D, Windows 11 Pro build 26300, rustc 1.100.0 (0ef1bc386 2026-11-09) (stable-x86_64-pc-windows-msvc), DXC 1.9.2602.17, debug tests / release smoke.

  • cargo build --workspace: ok.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 2905 passed, 0 failed, 51 ignored (43 test binaries). subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil passes on all five paths. A first run lost the lib binary to a 0xc0000005 in the NVIDIA driver right after gpu::agility::tests::create_device_sets_dred_first, a crash this machine also has on main; the rerun passed.
  • SKIPPED (28): 22 × no wave64 (the GPU runs wave64 split; lower_dxil, lower, kernel and two recomp::tests GPU comparisons), 2 × no heap windows (heap_window_out_of_memory_falls_back_to_the_switch_form, two_pass_binds_set_the_shared_view_heap), 3 × no D3D12 debug layer (uploads_start_from_each_subresources_state, the title under the debug layer with and without DRED), 1 × pso_measurement_compiles_an_exact_synthetic_record (cannot disable the driver cache without Developer Mode).
  • The new test fails without the fix: with emit.rs and lower_mem.rs at main it stops at "expected xcl_gload_u32 in HLSL"; with only lower_mem.rs at main it fails on the GPU at "Direct DXIL 1-window lane 0 off 0: ubyte" (0 against 5).
  • Earlier, by the author, on 8645f312 (before the rebase): cargo check --workspace --all-targets, the two targeted GPU tests, the full suite (exit 0) and cargo build --release -p xcl on an RTX 5070 Ti (driver 32.0.16.1074), Ryzen 9 9950X, Windows 11 build 26200, rustc 1.99.0; a 150 s smoke run with XCL_DIRECT_DXIL=all, XCL_HEAP_WINDOWS=1, XCL_DRED=1 showed 1587 frames against 1695 on main f0e8fdd5, no device removal, no crash.

Regression run

The same title for 60 s, release builds of main and of this PR's head with no uncommitted changes. Both blocks were made with main's scripts/smoke.ps1: the branch's own copy predates main's fix that hands XCL_PREPARE=0 / XCL_RUN_SECONDS to the process, so with it the title ran without a time limit and was killed.

main:

xcl-smoke v1
commit: 6bfb5c5153ccc23517daf30aa1071ddb0fadb53c
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1517 (PresentX 1517)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 17092 MB

this PR:

xcl-smoke v1
commit: fb986aecec94a839d72cf5bcaf2d5f26fabf6edc
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1612 (PresentX 1612)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17819 MB

Not tested

  • AMD or Intel GPUs and native wave64 hardware (the RTX 5070 Ti runs wave64 split; the 22 no wave64 tests above did not run).
  • Titles beyond the one smoke title; the smoke run does not show whether that title reaches a sub-dword .rodata load.
  • 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.

The test fix (second commit), the test run and the smoke pair above were done with Claude Code (Claude Opus 5.5) for Foniass.

🤖 Generated with Claude Code

<!-- greyfinch-sync:ivall/xcl:issue:5592673413 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3607). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T19:12:39Z. --- Replaces #3368 so it can be merged as is: it carries Kalmarv's commit from #3368 unchanged (same SHA, author kept) plus one test-only commit that fixes the new test's expectation (with several memory windows a code address reads 0 beside the block, in both backends; details in https://github.com/xcl-dev/xcl/pull/3368#issuecomment-7320289131). All credit for the fix goes to Kalmarv. Sub-dword global/flat loads (`flat_small`) skipped the lookup in the shader's embedded `.rodata` that dword loads already do, so a shader indexing a byte or 16-bit constant table placed after its program read the wrong bytes. This routes them through the same lookup in HLSL and Direct DXIL. **What is in it** - `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. - `crates/xcl/src/gpu/recomp/tests.rs` (second commit, `Tests: expect 0 beside the rodata block with several memory windows`): with several memory windows a code address is in no window and reads 0 (the window lookup in `emit.rs` / `lower_window.rs`; main's `gpu_loads_read_the_constant_data_after_the_program` relies on the same rule), so the ushort that straddles the block's start or end takes 0 there, not window 0's decoy word. Before it, the test failed on the HLSL 2-window run ("ushort across the block's start edge", 1024 against 1185) although the loads were right. **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** Run by Foniass on head `fb986aecec94a839d72cf5bcaf2d5f26fabf6edc` merged into main `6bfb5c5153ccc23517daf30aa1071ddb0fadb53c` (clean merge): NVIDIA GeForce RTX 5070 Ti (driver 621.14), AMD Ryzen 7 7800X3D, Windows 11 Pro build 26300, `rustc 1.100.0 (0ef1bc386 2026-11-09)` (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2602.17, debug tests / release smoke. - `cargo build --workspace`: ok. - `cargo test --workspace --no-fail-fast -- --test-threads=4`: 2905 passed, 0 failed, 51 ignored (43 test binaries). `subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil` passes on all five paths. A first run lost the lib binary to a 0xc0000005 in the NVIDIA driver right after `gpu::agility::tests::create_device_sets_dred_first`, a crash this machine also has on main; the rerun passed. - SKIPPED (28): 22 × `no wave64` (the GPU runs wave64 split; `lower_dxil`, `lower`, `kernel` and two `recomp::tests` GPU comparisons), 2 × `no heap windows` (`heap_window_out_of_memory_falls_back_to_the_switch_form`, `two_pass_binds_set_the_shared_view_heap`), 3 × no D3D12 debug layer (`uploads_start_from_each_subresources_state`, the title under the debug layer with and without DRED), 1 × `pso_measurement_compiles_an_exact_synthetic_record` (cannot disable the driver cache without Developer Mode). - The new test fails without the fix: with `emit.rs` and `lower_mem.rs` at main it stops at "expected xcl_gload_u32 in HLSL"; with only `lower_mem.rs` at main it fails on the GPU at "Direct DXIL 1-window lane 0 off 0: ubyte" (0 against 5). - Earlier, by the author, on `8645f312` (before the rebase): `cargo check --workspace --all-targets`, the two targeted GPU tests, the full suite (exit 0) and `cargo build --release -p xcl` on an RTX 5070 Ti (driver `32.0.16.1074`), Ryzen 9 9950X, Windows 11 build 26200, `rustc 1.99.0`; a 150 s smoke run with `XCL_DIRECT_DXIL=all`, `XCL_HEAP_WINDOWS=1`, `XCL_DRED=1` showed 1587 frames against 1695 on main `f0e8fdd5`, no device removal, no crash. **Regression run** The same title for 60 s, release builds of main and of this PR's head with no uncommitted changes. Both blocks were made with main's `scripts/smoke.ps1`: the branch's own copy predates main's fix that hands `XCL_PREPARE=0` / `XCL_RUN_SECONDS` to the process, so with it the title ran without a time limit and was killed. main: ``` xcl-smoke v1 commit: 6bfb5c5153ccc23517daf30aa1071ddb0fadb53c gpu: NVIDIA GeForce RTX 5070 Ti title: 3cb1d8e0 seconds: 60 exit: 0x0 frames shown: 1517 (PresentX 1517) presents per 2 s (last 3): 60 61 60 device removals: 0 crashes: 0 peak commit: 17092 MB ``` this PR: ``` xcl-smoke v1 commit: fb986aecec94a839d72cf5bcaf2d5f26fabf6edc gpu: NVIDIA GeForce RTX 5070 Ti title: 3cb1d8e0 seconds: 60 exit: 0x0 frames shown: 1612 (PresentX 1612) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 17819 MB ``` **Not tested** - AMD or Intel GPUs and native wave64 hardware (the RTX 5070 Ti runs wave64 split; the 22 `no wave64` tests above did not run). - Titles beyond the one smoke title; the smoke run does not show whether that title reaches a sub-dword `.rodata` load. - 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. The test fix (second commit), the test run and the smoke pair above were done with Claude Code (Claude Opus 5.5) for Foniass. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
throny commented 2026-12-01 19:26:22 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-12-01T19:26:22Z.


merge-check claim
head: fb986aecec

<!-- greyfinch-sync:ivall/xcl:comment:7322818914 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3607#issuecomment-7322818914). Originally posted by [throny](https://github.com/throny) on 2026-12-01T19:26:22Z. --- merge-check claim head: fb986aecec94a839d72cf5bcaf2d5f26fabf6edc
fromthemoonx commented 2026-12-01 20:13:47 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-01T20:13:47Z.


merge-check
base: 6bfb5c5153
head: fb986aecec
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2905 passed, 0 failed, 51 ignored (base: 2904 passed, 0 failed, 51 ignored); SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps), the same set as base; failing: none
review: read the whole diff (both commits; 3 files, +142 -6). No correctness problem found.

  • Sub-dword flat / global loads now take their aligned dwords through the .rodata lookup, as dword loads already do:
    • Direct DXIL: ld_raw_u32_guarded(.., rodata: true) -> rodata_loads. mubuf keeps false.
    • HLSL: xcl_gload_u32, which looks up each of the two dwords on its own.
  • Kalmarv's commit 6232c2a0 keeps its author; Foniass's test-only commit is on top.
  • gpu::recomp::tests::subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil with XCL_REQUIRE_GPU=mesh on this wave32 GPU: 3 of 3 passed, not skipped.
  • Mutation: Direct DXIL sub-dword path back to rodata: false fails the test in its GPU readback check (tests.rs:5747). Good.
  • Mutation: HLSL back to ld_raw(..).x when .rodata exists fails it at the same check. Good.
  • No retail title on this machine: a title's byte / 16-bit tables were not read here.
    performance: not measured. Sub-dword loads in programs with .rodata gain the same range check that dword loads already pay; programs without .rodata emit the same code as before.
    verdict: safe to merge
    hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
<!-- greyfinch-sync:ivall/xcl:comment:7324295512 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3607#issuecomment-7324295512). Originally posted by [fromthemoonx](https://github.com/fromthemoonx) on 2026-12-01T20:13:47Z. --- merge-check base: 6bfb5c5153ccc23517daf30aa1071ddb0fadb53c head: fb986aecec94a839d72cf5bcaf2d5f26fabf6edc merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2905 passed, 0 failed, 51 ignored (base: 2904 passed, 0 failed, 51 ignored); SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps), the same set as base; failing: none review: read the whole diff (both commits; 3 files, +142 -6). No correctness problem found. - Sub-dword `flat` / `global` loads now take their aligned dwords through the `.rodata` lookup, as dword loads already do: - Direct DXIL: `ld_raw_u32_guarded(.., rodata: true)` -> `rodata_loads`. `mubuf` keeps `false`. - HLSL: `xcl_gload_u32`, which looks up each of the two dwords on its own. - Kalmarv's commit `6232c2a0` keeps its author; Foniass's test-only commit is on top. - `gpu::recomp::tests::subdword_global_loads_read_rodata_in_hlsl_and_direct_dxil` with `XCL_REQUIRE_GPU=mesh` on this wave32 GPU: 3 of 3 passed, not skipped. - Mutation: Direct DXIL sub-dword path back to `rodata: false` fails the test in its GPU readback check (`tests.rs:5747`). Good. - Mutation: HLSL back to `ld_raw(..).x` when `.rodata` exists fails it at the same check. Good. - No retail title on this machine: a title's byte / 16-bit tables were not read here. performance: not measured. Sub-dword loads in programs with `.rodata` gain the same range check that dword loads already pay; programs without `.rodata` emit the same code as before. verdict: safe to merge hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
throny commented 2026-12-01 21:02:31 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-12-01T21:02:31Z.


merge-check
base: f07e426dad
head: fb986aecec (same as the claim)
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2905 passed, 0 failed, 51 ignored; 25 SKIPPED lines (no wave64 / heap windows / driver-cache test on this adapter), no test binary ended abnormally
review: none found after reading the whole diff (213 lines). Sub-dword global/flat loads (ubyte/sbyte/ushort/sshort/short_d16/_hi) of unaligned 16-bit values now consult embedded shader rodata in HLSL (new xcl_gload_u32, only emitted/used when rodata is non-empty) and in direct DXIL (ld_raw_u32_guarded gets a rodata flag, true only on the d16 load site; the two store read-modify-write sites keep false). Observation: in lower_mem.rs only one of three call sites passes true, the other two are stores, which is plausible. Test: translate checks (no GPU, HLSL without rodata has no xcl_gload_u32; with rodata it does; direct DXIL translates and validates when DXC exists) run everywhere; the numeric checks run only after gpu_ready(), otherwise the test returns early, so on a machine without a GPU the value checks do not run. Scope: one change plus test; translator sources, so testers re-translate shaders.
performance: translator output changes only for shaders with rodata and sub-dword loads: one extra lookup of two dwords per such load. Shaders without rodata are unchanged (test asserts it for HLSL). Not measured.
verdict: safe to merge
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300; Claude Code, Claude Sonnet 5.5 ran the build and the suite and read the diff; Claude Opus 5.5 checked this report

<!-- greyfinch-sync:ivall/xcl:comment:7325653171 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3607#issuecomment-7325653171). Originally posted by [throny](https://github.com/throny) on 2026-12-01T21:02:31Z. --- merge-check base: f07e426dade72aa7112cf6bfe08ca6f6ff5a9d5c head: fb986aecec94a839d72cf5bcaf2d5f26fabf6edc (same as the claim) merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2905 passed, 0 failed, 51 ignored; 25 SKIPPED lines (no wave64 / heap windows / driver-cache test on this adapter), no test binary ended abnormally review: none found after reading the whole diff (213 lines). Sub-dword global/flat loads (ubyte/sbyte/ushort/sshort/short_d16/_hi) of unaligned 16-bit values now consult embedded shader rodata in HLSL (new xcl_gload_u32, only emitted/used when rodata is non-empty) and in direct DXIL (ld_raw_u32_guarded gets a rodata flag, true only on the d16 load site; the two store read-modify-write sites keep false). Observation: in lower_mem.rs only one of three call sites passes true, the other two are stores, which is plausible. Test: translate checks (no GPU, HLSL without rodata has no xcl_gload_u32; with rodata it does; direct DXIL translates and validates when DXC exists) run everywhere; the numeric checks run only after gpu_ready(), otherwise the test returns early, so on a machine without a GPU the value checks do not run. Scope: one change plus test; translator sources, so testers re-translate shaders. performance: translator output changes only for shaders with rodata and sub-dword loads: one extra lookup of two dwords per such load. Shaders without rodata are unchanged (test asserts it for HLSL). Not measured. verdict: safe to merge hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300; Claude Code, Claude Sonnet 5.5 ran the build and the suite and read the diff; Claude Opus 5.5 checked this report
throny commented 2026-12-01 22:07:30 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-12-01T22:07:30Z.


Merged at the checked head after a combined trial of #3600, #3493, #3608 and #3607 on main f07e426d (RTX 4080 16 GB, cargo test --workspace --no-fail-fast -- --test-threads=4: 2909 passed, 0 failed, 51 ignored, 25 SKIPPED lines). Two merge checks each.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:ivall/xcl:comment:7327303274 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3607#issuecomment-7327303274). Originally posted by [throny](https://github.com/throny) on 2026-12-01T22:07:30Z. --- Merged at the checked head after a combined trial of #3600, #3493, #3608 and #3607 on `main` `f07e426d` (RTX 4080 16 GB, `cargo test --workspace --no-fail-fast -- --test-threads=4`: 2909 passed, 0 failed, 51 ignored, 25 SKIPPED lines). Two merge checks each. AI use: Claude Code (Claude Opus 5.5).
Sign in to join this conversation.
No reviewers
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!3607
No description provided.