Recompiler: read shader rodata on sub-dword global/flat loads in HLSL and Direct DXIL #3607
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3607"
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?
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.rodatathat 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: passrodata: boolintold_raw_u32_guardedsoflatsub-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 correspondingflat_load_*ops) route their aligned dword reads throughrodata_loadsbefore sub-dword shift/merge, while keepingmubufcallers onrodata = false.crates/xcl/src/gpu/recomp/emit.rs: emit anxcl_gload_u32helper inrodata_loaderthat combinesxcl_gload2(a & ~3u)across aligned and unaligned byte offsets, and use it inflatsub-dword loads whenever shader.rodatais present.crates/xcl/src/gpu/recomp/tests.rs: addsubdword_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,.rodatahits, 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 inemit.rs/lower_window.rs; main'sgpu_loads_read_the_constant_data_after_the_programrelies 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 androdata_loadsin 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 readld_rawdirectly, 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
fb986aecec94a839d72cf5bcaf2d5f26fabf6edcmerged into main6bfb5c5153ccc23517daf30aa1071ddb0fadb53c(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_dxilpasses on all five paths. A first run lost the lib binary to a 0xc0000005 in the NVIDIA driver right aftergpu::agility::tests::create_device_sets_dred_first, a crash this machine also has on main; the rerun passed.no wave64(the GPU runs wave64 split;lower_dxil,lower,kerneland tworecomp::testsGPU 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).emit.rsandlower_mem.rsat main it stops at "expected xcl_gload_u32 in HLSL"; with onlylower_mem.rsat main it fails on the GPU at "Direct DXIL 1-window lane 0 off 0: ubyte" (0 against 5).8645f312(before the rebase):cargo check --workspace --all-targets, the two targeted GPU tests, the full suite (exit 0) andcargo build --release -p xclon an RTX 5070 Ti (driver32.0.16.1074), Ryzen 9 9950X, Windows 11 build 26200,rustc 1.99.0; a 150 s smoke run withXCL_DIRECT_DXIL=all,XCL_HEAP_WINDOWS=1,XCL_DRED=1showed 1587 frames against 1695 on mainf0e8fdd5, 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 handsXCL_PREPARE=0/XCL_RUN_SECONDSto the process, so with it the title ran without a time limit and was killed.main:
this PR:
Not tested
no wave64tests above did not run)..rodataload.flat_load_*hitting LDS via the flat aperture (unchanged;rodatacheck only matches the shader binary's.rodataGPU VA range).AI use
Used an AI coding assistant to audit
lower_mem.rsandemit.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
Imported from ivall/xcl. Originally posted by throny on 2026-12-01T19:26:22Z.
merge-check claim
head:
fb986aececImported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-01T20:13:47Z.
merge-check
base:
6bfb5c5153head:
fb986aececmerge: 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.
flat/globalloads now take their aligned dwords through the.rodatalookup, as dword loads already do:ld_raw_u32_guarded(.., rodata: true)->rodata_loads.mubufkeepsfalse.xcl_gload_u32, which looks up each of the two dwords on its own.6232c2a0keeps its author; Foniass's test-only commit is on top.gpu::recomp::tests::subdword_global_loads_read_rodata_in_hlsl_and_direct_dxilwithXCL_REQUIRE_GPU=meshon this wave32 GPU: 3 of 3 passed, not skipped.rodata: falsefails the test in its GPU readback check (tests.rs:5747). Good.ld_raw(..).xwhen.rodataexists fails it at the same check. Good.performance: not measured. Sub-dword loads in programs with
.rodatagain the same range check that dword loads already pay; programs without.rodataemit 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)
Imported from ivall/xcl. Originally posted by throny on 2026-12-01T21:02:31Z.
merge-check
base:
f07e426dadhead:
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
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
mainf07e426d(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).