Skip to content

rdna2spirv: an out-of-range s_buffer_load reads through the buffer's base (llvmpipe segfault) - #4573

Open
LouisRiverstone wants to merge 4 commits into
ItsNotRexed:mainfrom
LouisRiverstone:smem-oob-safe-addr
Open

LouisRiverstone wants to merge 4 commits into
ItsNotRexed:mainfrom
LouisRiverstone:smem-oob-safe-addr

Conversation

@LouisRiverstone

@LouisRiverstone LouisRiverstone commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem. An out-of-range s_buffer_load segfaults llvmpipe, the CI's GPU. The translated code is correct: an s_buffer_load whose bound is only known at run time guards each dword with an OpSelectionMerge on off + 4·i + 4 <= NUM_RECORDS · stride and gives 0 outside it. llvmpipe (Mesa 26.0.8, LLVM 21.1.8) executes the guarded load anyway.

Found on #3039. Running its fixture cs_w32_late_ext on llvmpipe locally, vk_compute_test crashes with SIGSEGV (exit 139). Every 32-wide GPU passed it, because their stray read returns garbage that is thrown away. Word 6 is s12 = -4, offset:20: 0xFFFFFFFC + 20 = 0x100000010 bytes, past NUM_RECORDS = 0xFFFFFFFF, so it must read 0 (#1948). The guarded load at that address is 4 GB past the buffer.

Reduction. Delta debugging brought it down to 6 lines that still crash: that load, a v_cmpx, a ds_mskor_rtn_b32 and a store. It doesn't crash with s12 = 12, which is in range. The module passes spirv-val. Two other offsets (-0x100, 0x7ffffff0) also crash, but they are in range for NUM_RECORDS = 0xFFFFFFFF, so they are real reads 4 GB away from a small buffer, not a translator issue.

Change. In scalar_load's run-time-bounded path, the dwords are loaded with no branch, through OpSelect(dword 0 in range, the address, the heap's base). Each dword reads at index i when in range and 0 otherwise (main's indexing, since #3039-era main moved to one selection per load), and each value is OpSelect(dword i in range, loaded, 0). Out of range, the loads read the first dword of the heap (Xbox VA 0, always mapped) and the values are thrown away.

  • Why no branch: a load inside an OpSelectionMerge on the bound is hoisted by llvmpipe, both with one selection per dword (this PR's first version) and with one per load (current main). Inside the branch the compiler also knows the bound holds and folds a selected address back. Without the branch it can't.
  • Why the heap base, not the V# base: a descriptor with no readable bytes may point anywhere, and VA 0 is the start of the buffer the runtime maps, so it is always readable (@memorypressure). An earlier version of this PR used the V# base because I misread the heap-base variant's segfault; that variant still selected the address inside the branch.
  • No extra blocks: the per-load selection in main existed to cut blocks, and this has none.
  • Unchanged: constant bounds, unguarded loads and buffer_load*.

Tests.

  • CPU: tests/semantic/test_smem_oob_safe_address.py checks the no-branch shape (address and value selects on one condition, no OpBranchConditional around the load), that out-of-range dwords read only the V# base and the result is 0, that values are unchanged on a grid of offsets, and the new fixture on the CPU, with both runners expecting the same words.
  • Contract: test_scalar_mem_smem.py's evaluator models OpSelect %uint and keeps loads through the out-of-range side of an address select apart (discarded), so its reads assertions (the hardware rule's addresses) are unchanged. 41/41 pass.
  • New fixture: runtime/test/shaders/cs_smem_oob_far.asm covers far out-of-range offsets from registers and from constants. It passes on llvmpipe with this rule (LLVM 21 and 23), and with main's translator it segfaults vk_compute_test.
  • llvmpipe (Ubuntu 26.04, Mesa 26.0.8, runtime/ built from this tree):
  • Semantic suite: python -I -m unittest discover -s tests/semantic shows only main's known failures.

docs/design.md: new row after "Scalar/buffer memory" (updated for the no-branch form).

No game files. Claimed on #4421 before starting.

AI-assisted (Claude Code): I reviewed and ran the change and the tests.

@SweatierKey

Copy link
Copy Markdown
Contributor

Tested 76e312e2 on Linux (x86_64, Python 3.13.5, SPIRV-Tools on PATH, CPU/no-GPU): pass (code read + unit tests + spirv-val).

What I ran:

  • git diff --check d0835dd9..HEAD: clean.
  • PYTHONPATH=tests/semantic python3 -m unittest -v tests/semantic/test_smem_oob_safe_address.py tests/semantic/test_smem_const_offset.py: 6/6 OK.
  • Assembled and validated the s_buffer_load_dwordx2 test shader with spirv-as --target-env vulkan1.2 + spirv-val --target-env vulkan1.2: OK.

Code review:

  • Selecting between full and addr via OpSelect %ulong {ok} {full} {addr} before OpConvertUToPtr cleanly prevents llvmpipe from hoisting a 4 GiB out-of-range pointer read out of the OpBranchConditional.
  • Two small non-blocking edge-case notes:
    1. For wide loads (x4/x8/x16), OpAccessChain %_ptr_PSB_uint {sptr} {self.cu(i)} still steps 4*i bytes past addr (up to +60 bytes) when ok is false; if a tiny buffer sits at the end of a mapped page (or addr == 0 with NUM_RECORDS == 0), selecting full + 4*i vs addr and indexing sptr at 0 avoids stepping past addr + 0 (null-base V#s would still need a safe scratch address if llvmpipe hoists those too).
    2. In test_smem_oob_safe_address.py, checking that ptr_args[1] traces through host_addr to the OpSelect %ulong {cond} (instead of assertIn over the full spv text) would tighten the structural check.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested head 76e312e2b83f1a09af22112949d4bf24f80eb8bf on Linux, Python 3.10.12, standard library only, hand-written asm fixtures (no game files, no GPU, no SPIR-V tools).

  • python -I tests/semantic/test_smem_oob_safe_address.py on the PR head: Ran 2 tests — OK
  • Negative control, same test against main's tools/rdna2spirv.py: FAILED (failures=1) — the out-of-range s_buffer_load base-address rule is exercised by the new tests.

The llvmpipe segfault avoidance in the translator reads out-of-range buffer loads through the buffer's base as described. AI-assisted (Devin).

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ran this at 76e312e2 on Windows 11, RTX 5070 Ti.

  • tests/semantic/test_smem_oob_safe_address.py -> 2 tests, OK; git diff --check clean. The structural check confirms both dwords of a run-time-bounded s_buffer_load_dwordx2 now take their pointer from OpSelect(bound, address, base); the value check runs the six documented offsets and gets the same words as before, constant and run-time sOFF.
  • GPU, run_gpu_tests.py --filter smem with this translator -> 4/4 passed, 4 shaders spirv-val clean: cs_smem_bounds 0x0 0x5eed0010 0x5eed000c 0x0 0x5eed000c 0x5eed0010 0x0 0x0, cs_smem_sload_neg, cs_smem_soffset_imm, cs_smem_stride_align all match their expected words.

The change is in the guarded path only: the out-of-range address becomes dword i of the buffer base (at most 60 bytes on), while the OpPhi still yields 0. I cannot reproduce the llvmpipe hoist on this NVIDIA host, but the selected address is now in-bounds by construction.

@bobcatchris

Copy link
Copy Markdown
Contributor

Apple M2 no-regression check at head 76e312e (macOS 26.4, MoltenVK 1.4.2 direct, subgroup 4..32, llvm-mc 23.1.6, clean full build exit 0).

python -I scripts/run_gpu_tests.py --build runtime/build/linux: 213 shaders translated, 213 pass spirv-val; 127/210 passed, 76 skipped, 7 failed — identical to upstream/main aa2988c on this M2, with the same seven known MoltenVK expected failures. The llvmpipe SIGSEGV this fixes does not occur on this driver before or after; no new failure or skip. Head fetched read-only, no rebase/edit/push. Synthetic shaders and counts only; no game files.

AI-assisted.

@justr4mzi

Copy link
Copy Markdown
Contributor

Verified 76e312e2 on Windows 11 25H2 + AMD Radeon RX 6700 XT (gfx1031, Adrenalin 26.11.2, Vulkan 1.4.315, SPIRV-Tools v2026.4, llvm-mc 21.1.8).

GPU, semantic & negative-control checks (LEONIDA_REQUIRE_SPIRV_TOOLS=1)

  • LEONIDA_VK_VALIDATION=1 python -I scripts/run_gpu_tests.py --filter cs_w32 --require-pass: 13 shaders translated & spirv-val validated, 13/13 passed, 0 validation errors on AMD Radeon RX 6700 XT.
  • LEONIDA_VK_VALIDATION=1 python -I scripts/run_gpu_tests.py --filter cs_w64 --require-pass: 21 shaders translated & spirv-val validated, 21/21 passed, 0 validation errors.
  • test_smem_oob_safe_address.py: 2/2 OK (both direct python -I 0.186 s and unittest discover 0.119 s).
  • Negative control (HEAD~1 tools/rdna2spirv.py with the new test): FAILED (failures=1) (test_the_guarded_load_reads_through_a_selected_address: AssertionError: 'OpSelect %ulong %t65 ' not found).
  • Related suites: test_scalar_mem_smem.py 41/41 (0.015 s), test_smem_const_offset.py 4/4 (0.429 s), test_global_mem_offset.py 11/11 (0.021 s), tests/test_shader_checks.py 75/75 (1.361 s).
  • All 7 tests/test_docs_*.py suites: 7/7 passed; py_compile, scripts/check_no_game_data.py, and git diff --check clean (rc=0).

(Non-blocking code note: tools/rdna2spirv.py:9615 still emits ptr = OpConvertUToPtr %_ptr_PSB_arr ... unconditionally before the if/elif/else loop, so ptr is unused on the runtime-bounded else: branch where sptr is used.)

AI-assisted (Gemini, Antigravity).

LouisRiverstone added a commit to LouisRiverstone/Leonida that referenced this pull request Nov 23, 2026
@justr4mzi on ItsNotRexed#4573: the unconditional OpConvertUToPtr of the load address
was unused when every dword reads through its own selected pointer. Every
dword of one load takes the same path, so the base pointer is now emitted only
for the unguarded paths.
@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Thanks @justr4mzi (RX 6700 XT) and @bobcatchris (Apple M2). The unused pointer is fixed in 4c84d810. Every dword of one load takes the same path, so the unconditional OpConvertUToPtr of the load address is now emitted only for the unguarded paths, and the run-time-bounded dwords use only their selected pointer. The four smem test files pass, and the full semantic suite shows only main's known failures. On llvmpipe, #3039's cs_w32_late_ext still passes with this translator, and the full GPU suite is 139/210, identical to main.

@wavehs

wavehs commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Verdict: approve-with-nits.

The fix is right: the value still comes from OpPhi as 0 and OpSelect only replaces the address the driver may speculatively load, so no observable semantics change. The bounds arithmetic is sound — zext(sOFF & ~3) + imm in 64 bits with no 32-bit wrap, lim compared in 64 bits.

Verified: pytest on the new file plus test_smem_const_offset, test_scalar_mem_smem, test_global_mem_offset → 58 passed / 151 subtests; the new test fails on d0835dd9 as claimed; spirv-as + spirv-val --target-env vulkan1.2 on the translated module → 0.

Nits / follow-ups:

  1. tests/semantic/test_smem_oob_safe_address.py:41-42,56-59 extracts ptr_args but never uses it: it only asserts that an OpSelect %ulong exists somewhere in the module, so a dead select would pass. Better: walk the AccessChain pointer through its OpIAdd to the OpSelect and compare its condition with cond.
  2. tools/rdna2spirv.py:9662: out of range it still loads from base + 4*i (up to 64 bytes), which can be outside the V#'s allocation. OpSelect(ok, full, self.cu(0)) would read 4 bytes of heap base from push constants, which is always mapped, and would also let docs/design.md:246 drop "at most 60 bytes on".
  3. MUBUF (tools/rdna2spirv.py:3155-3158, same branch shape) has the same hoisting hazard, while docs/design.md:246 says buffer_load* "keeps its own guards" without that caveat — worth a finding: in the coordination issue.
  4. tools/rdna2spirv.py:9626-9627: OpConvertUToPtr(full) is now dead on fully guarded loads and still converts a 4 GiB address.
  5. host_addr inside the loop costs ~7 ops (two push-constant loads) per dword, up to ×16; the base can be hoisted out of the loop.
  6. The evidence in the description points at cs_w32_late_ext from the open GPU test cs_w32_late_ext: ds_mskor, v_add/sub_nc_i16 clamp, s_buffer_load constant offset #3039, and there is no fixture with a far runtime bound, so llvmpipe CI cannot catch a regression of this rule. Either a small cs_* fixture or a note that it waits on GPU test cs_w32_late_ext: ds_mskor, v_add/sub_nc_i16 clamp, s_buffer_load constant offset #3039.
  7. The description references the claim but not the item ID.

AI-assisted review.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 4c84d81 merged with main d0835dd on WSL2 Ubuntu (Python 3.12.16) and native Windows 11 (Python 3.13.1): pass. Earlier checks on this PR were at 76e312e; this is the head after the unused-pointer fix.

  • tests/semantic/test_smem_oob_safe_address.py: Linux 2 tests OK, Windows 2 OK.
  • Negative control (tools/rdna2spirv.py from main, PR test kept): 1 failure on both (the OpSelect %ulong of the safe address is missing).
  • Read: guarded is true exactly when the loop takes the run-time-bounded branch (lim set and not the static lim_val/no-register case), so ptr is emitted only for the loops that read through it and the guarded loops use their own sptr; I checked the three branches against that condition. I did not run the llvmpipe out-of-range case (no spirv-as/spirv-val on this Linux box).

LouisRiverstone added a commit to LouisRiverstone/Leonida that referenced this pull request Nov 23, 2026
…d form still hoisted)

@wavehs's review on ItsNotRexed#4573 led to a fixture with more loads (cs_smem_oob_far)
that still segfaulted llvmpipe with the guarded load: inside the branch the
compiler knows the bound holds and folds the selected address back to the far
one, so the first version only passed ItsNotRexed#3039's fixture by luck. A selected heap
base (as suggested) segfaults too: the heap base is 0 in vk_compute_test.

The dword is now loaded with no branch, through OpSelect(in range, its address,
the V#'s base), and its value is OpSelect(in range, loaded, 0). Both host
addresses are computed once, outside the per-dword loop.

- test_scalar_mem_smem.py's evaluator models OpSelect %uint and keeps loads
  through the out-of-range side of an address select out of reads (in
  discarded): reads stays the addresses the hardware rule reads.
- test_smem_oob_safe_address.py checks the shape (no branch, the address and
  value selects on one condition), the discarded reads at the V# base, values
  unchanged, and the new fixture on the CPU.
- runtime/test/shaders/cs_smem_oob_far.asm: passes on llvmpipe with this rule,
  segfaults vk_compute_test without it.
- docs/design.md row updated.
@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Thanks @wavehs, this review found a real problem with the fix itself. Changed in 19195e5b, point by point:

  1. The test now follows the pointer. Each bounded dword's OpLoad → OpAccessChain → OpConvertUToPtr → OpSelect %ulong, whose condition must also be the condition of the value's OpSelect %uint. A dead select no longer passes.
  2. A fixture of its own, cs_smem_oob_far. Writing it showed that the guarded load was not a fix. With more loads around it, the program still segfaulted llvmpipe. Inside the OpSelectionMerge the compiler knows the bound holds, so it folds the selected address back to the far one. The first version passed GPU test cs_w32_late_ext: ds_mskor, v_add/sub_nc_i16 clamp, s_buffer_load constant offset #3039's fixture only by luck of the code shape.
  3. The heap base doesn't work either. I tried OpSelect(ok, full, heap base) as you suggested, and it still segfaults: in vk_compute_test the heap base is 0. So the safe address stays the V# base.

What the change does now. A run-time-bounded dword is loaded with no branch, through OpSelect(in range, its address, the V#'s base), and its value is OpSelect(in range, loaded, 0). Without the branch the compiler can't assume the bound, and out of range the load reads the buffer's base and is thrown away. In this shape the new fixture, #3039's cs_w32_late_ext and the reduced programs all pass on llvmpipe (LLVM 21 and 23). With main's translator, cs_smem_oob_far segfaults vk_compute_test (exit 139), so the fixture catches a regression.

  1. One address computation per load. Both host addresses (host_addr(full), host_addr(V# base)) are computed once, outside the per-dword loop.
  2. The dead OpConvertUToPtr was already gone in a2b3c29d.
  3. MUBUF has the same branch shape, so I'll post it as a finding: in the coordination issue rather than widen this PR.
  4. Item ID: the claim was a "new SHADER item" without an ID. I'll ask there.

Contract. Reading the V# base when out of range is unobservable but a real memory read, so test_scalar_mem_smem.py's evaluator now keeps loads through the out-of-range side of an address select apart (discarded). reads stays exactly the addresses the hardware rule reads, and all 41 of its tests pass unchanged. test_smem_oob_safe_address.py checks that the discarded reads are dword i of the V# base and nothing else.

Results. python -I -m unittest discover -s tests/semantic shows only main's known failures. Full run_gpu_tests.py on llvmpipe: 140/211 passed, 71 skipped, 0 failed (the new fixture is the +1). I'll update the description.

@TreXito

TreXito commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested head 73f5082f (merge-base 2d3369a4) on Linux 6.18 (Docker on a TrueNAS host) on two devices. It passes on both. On lavapipe the base translator fails the new fixture in every run.

NVIDIA RTX 3060 Ti, driver 580.173.02

  • run_gpu_tests.py gives 171/218 passed, 47 skipped, 0 failed.
    • cs_smem_oob_far is PASS with 0 4008636142 0 0 0 4008636142 0 4008636142.
    • Main 2d3369a4 on the same card gives 170/217, 47 skipped.
  • ctest: 50/50 passed.
  • test_scalar_mem_smem.py and test_smem_oob_safe_address.py: 49 passed on the head. With the base tools/ they give 3 failed, as expected:
    • :54: 0 != 2 bounded loads;
    • :77, twice: no discarded reads.

lavapipe, Mesa 26.0.3 with LLVM 21.1.8 (AVX2). "Base" here means the same head build with only tools/rdna2spirv.py taken from 2d3369a4.

run head base translator
--filter cs_smem_oob_far, LP_NATIVE_VECTOR_WIDTH=256 (subgroup 8), 3 runs PASS x3 FAIL x3
the same at width 128 (subgroup 4), 3 runs PASS x3 FAIL x3
full run_gpu_tests.py 146/218, 72 skipped, 0 failed 145/218, fails cs_smem_oob_far only
LEONIDA_W64_EMULATE=1 --filter cs_ 82/105, 23 skipped, 0 failed 81/105, fails cs_smem_oob_far only

In each failing base run, vk_compute_test prints its device line and then nothing: no out[0..7] line. It dies before the dump. So the crash also happens on 26.0.3, not only on 26.0.8.

Nit: two comments still describe the branch version.

  • The cs_smem_oob_far.asm header says the translator "guards each dword with an OpSelectionMerge on the bound" and reads through "OpSelect(in range, address, heap base)". The head has no branch and selects the V# base.
  • The comment above host_full, host_base in rdna2spirv.py says a driver "may hoist one out of its branch" and "the OpPhi gives 0". The value is now OpSelect(in range, loaded, 0).

AI-assisted (Claude Code). Every number above is from a real run.

@cxchhh

cxchhh commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 73f5082f on Tesla V100-SXM2-32GB (Volta, subgroup 32..32), NVIDIA 580.95.05, Ubuntu 24.04 (kernel 6.14.11), Python 3.12 and on llvmpipe (Mesa 25.2.8, LLVM 20.1.2, same machine): pass. I reproduced the segfault on main, and this branch fixes it.

Setup: the PR diff applied to main 98fa503f (applies cleanly), Linux Vulkan test programs built from that tree (cmake -S runtime -G Ninja), compared against an untouched main 98fa503f built the same way.

  • V100, scripts/run_gpu_tests.py: native 162/218 passed, 56 skipped; LEONIDA_W64_EMULATE=1 LEONIDA_BARY_FALLBACK=1 195/218 passed, 23 skipped; 0 failures. The only difference from main is the new cs_smem_oob_far, which passes in both modes (0 4008636142 0 0 0 4008636142 0 4008636142).

  • llvmpipe (VK_ICD_FILENAMES=.../lvp_icd.json vk_compute_test <spv> 1 dump, subgroup 8). I translated the fixtures with each tree's build.py:

    fixture main's translator this branch
    cs_smem_oob_far (this PR) segfault, exit 139 exit 0, the 8 words above
    cs_w32_late_ext (GPU test cs_w32_late_ext: ds_mskor, v_add/sub_nc_i16 clamp, s_buffer_load constant offset #3039) segfault, exit 139 exit 0, 305419896 305441656 4294901761 32767 32768 32768 0 4008636142 (all eight right)

    On the V100 both translators give the right words for both fixtures (NVIDIA discards the stray read), so only llvmpipe shows the crash.

  • tests/semantic/test_smem_oob_safe_address.py 5 OK, test_scalar_mem_smem.py 44 OK.

  • Full semantic suite: 6250 tests, failures=3, errors=2: the same five test_loop_ballot_barrier cases that fail on main here, nothing new.

No game files. AI-assisted (Claude Code); I ran the commands above and checked the numbers against the logs.

@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Thanks @TreXito (RTX 3060 Ti + lavapipe, where main's translator fails cs_smem_oob_far in every run) and @cxchhh (V100 + llvmpipe): that's exactly the regression check the fixture was meant to be. Both ran the current head 73f5082f, so nothing is pending here.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 73f5082 merged with main 98fa503 on Linux (WSL2, Python 3.12.16): pass

  • test_smem_oob_safe_address.py: 5 tests OK. test_scalar_mem_smem.py: 44 tests OK. All test_smem*.py (14), all test_scalar*.py (444) and all test_*fixture*.py (20, including the new cs_smem_oob_far disasm fixture) OK. All 10 tests/test_docs_*.py OK.
  • Not run to completion: the whole tests/semantic directory (it did not finish inside my 23-minute limit), so the subsets above are what ran.
  • Negative control: with tools/rdna2spirv.py restored from main, test_smem_oob_safe_address.py fails 3 of 5 (the no-branch shape test and both out-of-range discard subtests) and test_scalar_mem_smem.py still passes 44. The Fixture tests in the new file (interpreter run of cs_smem_oob_far, expected words in check.py / run_gpu_tests.py) pass on main too, as expected, since the interpreter does not hoist loads; the llvmpipe segfault itself is not reproducible on CPU here and I did not run it. So the shape tests are what demonstrate the change.
  • Not run: GPU / llvmpipe (vk_compute_test, cs_smem_oob_far on a device); the diff to the runner lists is only additions of the new name.

No issues found in the diff.

…rom the V# base (llvmpipe segfault)

Rebuilt on main, which replaced one selection per dword with one per load (to
cut the blocks of the cascade-shadow CS). That still segfaults llvmpipe (Mesa
26.0.8, LLVM 21) on ItsNotRexed#3039's s12 = -4, offset:20 and on cs_smem_oob_far: the
load inside the selection is hoisted and reads 4 GB past the buffer.

The loads now have no branch: they read through OpSelect(dword 0 in range,
the address, the V#'s base), at index i when in range and 0 otherwise (main's
indexing), and each value is OpSelect(dword i in range, loaded, 0). Out of
range they read the buffer's first dword and the values are discarded; no
extra blocks at all.

- test_scalar_mem_smem.py's evaluator models OpSelect %uint and keeps loads
  through the out-of-range side of an address select out of reads.
- tests/semantic/test_smem_oob_safe_address.py: the no-branch shape (one
  selected pointer on dword 0's bound, a value select per dword), discarded
  reads only at the V# base's first dword, values unchanged, the fixture on
  the CPU.
- runtime/test/shaders/cs_smem_oob_far.asm: passes on llvmpipe with this rule,
  segfaults vk_compute_test with main's translator.
- docs/design.md row added.
@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Rebased onto main 3faac557 (6c91f851), which conflicted. main had replaced one selection per dword with one per load, to cut the blocks of the cascade-shadow CS. That version still segfaults llvmpipe: with main's translator both cs_smem_oob_far and the reduced 6-line program crash vk_compute_test (exit -11), because the load inside the selection is hoisted and reads 4 GB past the buffer.

So I kept main's indexing (each dword at index i when in range, else 0) and removed the branch:

  • the loads read through OpSelect(dword 0 in range, the address, the V#'s base)
  • each value is OpSelect(dword i in range, loaded, 0)
  • out of range they read only the buffer's first dword
  • no extra blocks at all, which is what main's change was after

Results on llvmpipe (Mesa 26.0.8):

  • cs_smem_oob_far passes with LLVM 21 and 23, and the reduced program gives 0.
  • The full run_gpu_tests.py gives 146/218 passed, 0 failed (main 145/217; the fixture is the +1).

test_smem_oob_safe_address.py (adjusted to the single selected pointer, and to discarded reads at the base's first dword only), test_scalar_mem_smem.py and test_smem_const_offset.py pass. The full semantic suite shows only main's three known test_loop_ballot_barrier failures. Thanks @kapitche for the run at 73f5082; this head changes the shape, so a fresh run would be welcome.

@Ayinaki

Ayinaki commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 6c91f85 on RTX 5070, driver 616.86, Windows 10.0.26200: pass.

Built runtime and the shader test programs (MSVC 14.52, Visual Studio 18 2026, x64, RelWithDebInfo), translated the fixtures with runtime/test/shaders/build.py (LLVM 21.1.8 llvm-mc, SPIRV-Tools 1.4.363.1), then ran runtime/test/shaders/check.py with LEONIDA_VK_DEVICE="RTX 5070".

  • cs_smem_oob_far ok — the out-of-range scalar buffer loads read back 0 4008636142 0 0 0 4008636142 0 4008636142 as the fixture's comment specifies (the in-bounds dwords that land on never-written bytes give the harness fill, the rest 0).
  • check.py exits 0 overall: 154 of 189 shaders pass, 35 skipped (the device-feature and full-wave64 cases the script documents), no failures.

This is the NVIDIA path, not the llvmpipe case the fixture's comment cites for the OpSelect on the guarded address.

@tunczzz

tunczzz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 6c91f851 (merge base 3faac557) on an NVIDIA GeForce RTX 5070 Ti (Blackwell, subgroup 32..32), driver 616.56, Windows 11 10.0.26300. Translator and fixtures came from this head; runtime test programs were built from main 3faac557 (MSVC 19.44, Vulkan-Headers fetched by CMake, loader from the NVIDIA driver); spirv-as/spirv-val and llvm-mc 21 came from WSL Ubuntu. Result: pass.

  • scripts/run_gpu_tests.py: 171/218 passed, 47 skipped (wave64 programs on a 32-wide GPU), 0 failed.
  • Compared test by test with main 3faac557 on the same build (170/217, 47 skipped): every result is the same. New: cs_smem_oob_far PASS (0 4008636142 0 0 0 4008636142 0 4008636142).

Not run: LEONIDA_W64_EMULATE=1, other vendors, Linux, llvmpipe (the device the segfault was on). No game files.

AI-assisted (Claude Code).

@Atomic258

Copy link
Copy Markdown
Contributor

Independent CPU check of 6c91f851 — pass on the semantic suite; GPU fixtures not run (see below).

Environment: Windows 11, CPython 3.15.2, CPU only, synthetic fixtures, no game files, no GPU.

  • python -I -m unittest discover -s tests/semantic → 6250 tests: 3 failures + 2 errors, all in test_loop_ballot_barrier.py (wave64-emulation area — OpFunctionCall unsupported in the CPU interpreter, emulated wave64 structured vs rounds naming). Verified identical on main 3faac557 (same file: 3F+2E) — these are main's known failures, not from this change. Your "only main's known failures" claim checks out.
  • The tests this PR adds/changes pass: test_smem_oob_safe_address.py 5/5, test_scalar_mem_smem.py 44/44 — the no-branch address-select shape and the V#-base bound rule are pinned.

Not run: the cs_smem_oob_far/cs_w32_late_ext GPU fixtures — they need a runtime build + llvm-mc assembled SPIR-V. llvm-mc is now available on this machine (~/.leonida/llvm-mc), so a GPU pass is possible in a follow-up if wanted; skipped here to keep the check light.

AI-assisted (Devin).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 6c91f85 on AMD Radeon 610M (RADV, gfx1036, Mesa 26.2.6), CachyOS Linux, LEONIDA_VK_DEVICE=RADV: pass.

  • scripts/run_gpu_tests.py --filter smem_oob: PASS cs_smem_oob_far 0 4008636142 0 0 0 4008636142 0 4008636142; 1/1 passed. LEONIDA_VK_DEVICE=NoSuchDevice fails with "no Vulkan physical device matches", so the RADV device ran it.
  • tests/semantic test_smem_oob_safe_address.py: Ran 5 tests, OK.
  • tests/semantic test_scalar_mem_smem.py: Ran 44 tests, OK (the PR body says 41).

There is no llvmpipe on this machine, so the segfault this PR fixes was not reproduced here.

AI-assisted.

@MatyEjsr

Copy link
Copy Markdown

Tested 6c91f851 on NVIDIA GeForce RTX 3090 (Ampere, driver 32.0.16.1742 / 617.42, subgroup 32..32), Windows 11 (10.0.26200), VS 2022 Build Tools (MSVC 19.44.35230.0, Ninja RelWithDebInfo), Windows SDK 10.0.26100.0, Vulkan SDK 1.4.363.0, llvm-mc 23.1.5 (WSL Arch): pass.

  • Semantic tests:
    • python -m unittest discover -s tests/semantic -p "test_smem_oob_safe_address.py" -v: 5/5 OK (0.18s).
    • python -m unittest discover -s tests/semantic -p "test_scalar_mem_smem.py" -v: 44/44 OK (0.01s).
  • Clean scripts\build_runtime.ps1 rebuild (re-translating CMake shader fixtures with the PR's rdna2spirv.py): exit 0, all fixtures passed spirv-val.
  • python -I scripts/run_gpu_tests.py --build runtime/build --filter smem: 6/6 passed (cs_smem_bounds, cs_smem_oob_far, cs_smem_sconst_off, cs_smem_sload_neg, cs_smem_soffset_imm, cs_smem_stride_align) in both default and LEONIDA_W64_EMULATE=1 modes.
  • Full python -I scripts/run_gpu_tests.py --build runtime/build:
    • Default: 171/218 passed, 47 skipped, 0 failed.
    • LEONIDA_W64_EMULATE=1: 202/218 passed, 16 skipped, 0 failed.
  • Targeted ctest -R "^(vk_sync_validation|d3d12_translated_draw|d3d12_m2_textured|d3d12_m3_frame|d3d12_m4_compute|d3d12_subgroup)$" with LEONIDA_VK_VALIDATION=1 and VK_LOADER_LAYERS_DISABLE=~implicit~: 6/6 pass, vk_sync_validation confirmed active, 0 validation errors.
  • Full ctest (validation off, no game files): 168 passed, 9 skipped, 1 disabled (gameinput), 0 failed (146.6s).

Code read of tools/rdna2spirv.py looks clean: replacing the OpSelectionMerge branch with branchless OpSelect of host_addr(full) vs host_addr(addr) (and per-dword index/value selects) keeps out-of-range loads pinned to the V# base inside the heap arena while eliminating extra control-flow blocks.

AI-assisted.

@KkelebekK

Copy link
Copy Markdown

Tested 6c91f851 (merge base 3faac557 = current main; merges cleanly) on AMD Radeon 780M (RADV PHOENIX, gfx1103 / RDNA3, Mesa 26.2.6) and NVIDIA GeForce RTX 4070 Laptop (620.70.06), CachyOS Linux 7.3.4, Python 3.14.10, llvm-mc 23.1.4: pass on these GPUs; the llvmpipe segfault itself was not reproduced here (no llvmpipe/lavapipe on this machine, only RADV and NVIDIA ICDs).

  • Full run_gpu_tests.py: 780M 203/218 passed, 15 skipped; 4070 171/218, 47 skipped; 4070 with LEONIDA_W64_EMULATE=1 202/218, 16 skipped. 0 failed. Against main 3faac557 on this machine (202/217, 170/217, 201/217) each run is one pass higher: the new cs_smem_oob_far, which passes with 0 4008636142 0 0 0 4008636142 0 4008636142 (the words in its header).
  • That fixture does not tell the two translators apart on these GPUs: main's translator (merge base, same fixture) gives the same eight words on the 780M and the 4070, as the PR describes for non-llvmpipe devices. Whether it fixes the crash is only evidenced by the PR's and other reviewers' llvmpipe runs.
  • Translated cs_smem_oob_far: OpSelectionMerge 16 -> 9 and OpLabel 33 -> 19 against the merge base.
  • Extra check of the new "read the V# base when out of range" rule (synthetic compute shader): V# words built at run time with v_readfirstlane, NUM_RECORDS = 0, base 0 (null) and base 0xdead0000 (unmapped), runtime offset register, s_buffer_load_dword. The PR's translator emits the unconditional loads (6 OpConvertUToPtr) and returns 0 for both on the 780M and the 4070, with no fault or hang, the same words as the merge base. So a null or unmapped V# did not fault on these two drivers; other drivers not checked.
  • Python: tests/semantic/test_smem_oob_safe_address.py, tests/semantic/test_scalar_mem_smem.py, tests/test_run_gpu_tests.py, tests/test_docs_*.py: 120 passed, 111 subtests passed. The PR's two test files on the merge base's tools/ and scripts/: 5 failed (the no-branch shape checks and the fixture-words check), 48 passed.
  • No runtime C++ changes; reused the runtime/build/linux build of af70fc78.

Not run: llvmpipe/lavapipe, Windows.

Synthetic inputs only, no game data. AI-assisted.

@memorypressure

Copy link
Copy Markdown

FAIL as a soundness question, with the delta verified against main 3faac55: every CPU suite passes (scalar_mem_smem 44, smem_oob 5, const_offset 4, w64x order 5, translate 598 in the venv, docs guards, run_gpu_tests 41 + paths 20), and the llvmpipe motivation is legitimate — main's guarded load really can be hoisted out of its selection. But the fix changes what happens at zero bound, verified in both sources: main wraps the loads in OpBranchConditional oks[0], so when dword 0 is out of range (limit 0 included) no read happens at all; your branchless form unconditionally loads through OpSelect(oks[0], addr, the V# base), so a zero-readable-bytes descriptor now performs a physical dword-0 read at the buffer base. Whether that is safe depends on a guarantee nothing in the diff establishes — if an empty V# can have an unmapped or zero-size allocation, this converts main's clean skip into a potential fault, which is exactly the class of bug #3039 was. Please establish that a readable dword is guaranteed at the V# base for zero-limit descriptors (a pointer to the runtime's reservation code would do), or pick a fallback address that carries that guarantee, and add a zero-bound regression case — the current tests all use a seeded readable base. Also: the .asm fixture header still describes the earlier branch/heap-base implementation and should be refreshed. The llvmpipe behaviour itself was not reproduced here. Checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

…p base, not the V# base

@memorypressure on ItsNotRexed#4573: with no readable bytes (dword 0 out of range,
num_records 0 included) main read nothing, while the branchless form read the
V# base, which such a descriptor may leave anywhere. The fallback is now Xbox
VA 0: the start of the host buffer the runtime maps the GPU window into
(host_addr), so always readable. The earlier claim that 'the heap base is 0 in
vk_compute_test' was wrong (its output buffer is the heap); the heap-base
variant had segfaulted because it still selected the address inside the
branch.

- test_smem_oob_safe_address.py: discarded reads at VA 0, and a zero-size
  descriptor with a far base (num_records 0) reads only VA 0 and gives 0.
- cs_smem_oob_far: word 4 is now that zero-size far-base case; header refreshed
  for the branchless form. Passes on llvmpipe with LLVM 21 and 23.
- docs/design.md row corrected.
@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Thanks @memorypressure, that's a real gap, and it also exposed a wrong claim of mine. Fixed in f5caa216:

  • The fallback is now the heap base, not the V# base. It is Xbox VA 0, which host_addr maps to the start of the host buffer the runtime maps the console's GPU window into, so it is always readable. A descriptor with no readable bytes (num_records 0) can point anywhere, and main read nothing there, so the V# base was the wrong choice.
  • My earlier "the heap base is 0 in vk_compute_test" was wrong. Its output buffer is the heap (vk_compute_test.cpp: "Xbox VA 0 maps to its device address"). The heap-base variant had segfaulted for the same reason as the other first attempt: it still selected the address inside the branch, where the compiler folds it. I've corrected the docs/design.md row to say so.
  • Zero-bound regression. test_smem_oob_safe_address.py adds a zero-size descriptor whose base is far from any memory: no visible read, the discarded loads at VA 0 and not the V# base, and the result is 0. In cs_smem_oob_far, word 4 is now that case on the GPU (a V# with num_records 0 at VA 0x10_70000000), and the header is refreshed for the branchless form.

What I could and couldn't show on llvmpipe:

  • The fixture passes with LLVM 21 and 23, and the full suite gives 146/218, 0 failed. main's translator still segfaults on it.
  • I could not make the V#-base version fault with a far zero-size base (VA 0x10_…, 0x4000_…, 0x7FFF_…, 0x8000_… all ran). So the hazard you describe is real by construction but not reproduced here, and the switch rests on the guarantee, not on a crash.

The full semantic suite shows only main's three known failures.

@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Thanks @Ayinaki (RTX 5070), @tunczzz (RTX 5070 Ti), @Atomic258, @gertify (RADV), @MatyEjsr and @KkelebekK (780M + 4070, one pass above main in every mode). Your runs were at 6c91f851. The current head f5caa216 changes only the out-of-range fallback (heap base instead of V# base, per @memorypressure above) and word 4 of cs_smem_oob_far, which still expects 0 there. A re-run on hardware would be welcome.

@MatyEjsr

MatyEjsr commented Nov 23, 2026 •

Copy link
Copy Markdown

Retested f5caa216386438887097d5438ac58fac81b53ce2 (f5caa216) on NVIDIA GeForce RTX 3090 (Ampere, driver 32.0.16.1742 / 617.42, subgroup 32..32), Windows 11 (10.0.26200), VS 2022 Build Tools (MSVC 19.44.35230.0, Ninja RelWithDebInfo), Windows SDK 10.0.26100.0, Vulkan SDK 1.4.363.0: pass.

  • Semantic unit tests: test_smem_oob_safe_address.py 6/6 OK (including the zero-bound / heap-base fallback test) and test_scalar_mem_smem.py 44/44 OK.
  • Clean scripts\build_runtime.ps1 rebuild (406/406, re-translating shader fixtures): exit 0, spirv-val clean.
  • run_gpu_tests.py --filter smem: 6/6 passed in both default and LEONIDA_W64_EMULATE=1 modes (including cs_smem_oob_far).
  • Full run_gpu_tests.py: 171/218 passed (default) and 202/218 passed (LEONIDA_W64_EMULATE=1), 0 failed.
  • Targeted ctest with LEONIDA_VK_VALIDATION=1 and VK_LOADER_LAYERS_DISABLE=~implicit~ (vk_sync_validation, d3d12_translated_draw, d3d12_m2_textured, d3d12_m3_frame, d3d12_m4_compute, d3d12_subgroup): 6/6 pass, vk_sync_validation active, 0 validation errors.
  • Full ctest (no game files): 168 passed, 0 failed.

AI-assisted.

@ricardotanaka

Copy link
Copy Markdown

Re-tested the current head f5caa216 (merged on main 3faac557) on AMD Radeon RX 7600 (RDNA3, subgroup 32..64), AMD driver 26.11.2 (Windows), Windows 11 Pro 26200, MSVC 19.44, LEONIDA_VK_VALIDATION=1: pass.

  • run_gpu_tests.py --filter smem: 6/6 natively and with LEONIDA_W64_EMULATE=1, all translated and passing spirv-val; cs_smem_oob_far read 0 4008636142 0 0 0 4008636142 0 4008636142.
  • Full run_gpu_tests.py (native): 203/218 passed, 15 skipped, 0 failed (main 3faac557 on this machine: 202/217, the same 15 skipped), no validation messages.

AI-assisted (Claude Code).

@lunarminus

Copy link
Copy Markdown

Tested f5caa216 merged onto main d0835dd9 on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: pass

  • The head already contains main 3faac557, so the merge onto d0835dd9 was a fast-forward to f5caa216 itself. The PR has no C++/CMake changes. The test programs came from a build whose runtime/ matches this head outside runtime/test/shaders: an up-to-date 3faac557-era build.
  • Full scripts/run_gpu_tests.py: 222 shaders translated, 222 pass spirv-val.
    • Native: 203/218 passed, 15 skipped, 0 failed.
    • LEONIDA_W64_EMULATE=1: 203/218 passed, 15 skipped, 0 failed.
  • Per-test comparison with d0835dd9 without the PR (196/210 passed, 14 skipped in both modes): all 210 common tests have the same PASS/SKIP result in both modes. There are 8 new tests:
    • cs_smem_oob_far (this PR's fixture): PASS in both modes, 0 4008636142 0 0 0 4008636142 0 4008636142.
    • Seven came from main between d0835dd9 and 3faac557, not from this PR: cs_smem_sconst_off, ps_far_branch, ps_sample_c, ps_sample_c_b, ps_texture_cl and ps_texture_b_cl PASS; ps_m2_literal SKIP (it runs under another test program).
  • --filter smem: 6/6 passed in both modes.
  • Same tree with only tools/rdna2spirv.py taken from 3faac557: --filter smem also 6/6 in both modes, with the same eight cs_smem_oob_far words. So on RADV this fixture doesn't tell the two translators apart. In the translated cs_smem_oob_far, OpSelectionMerge goes from 16 to 9.
  • CPU tests:
    • test_smem_oob_safe_address.py 6/6, test_scalar_mem_smem.py 44/44, test_smem_const_offset.py 4/4.
    • unittest discover -p 'test_smem*.py' 15 OK, -p '*fixture*' 20 OK.
    • tests/test_run_gpu_tests.py 41, tests/test_run_gpu_tests_paths.py 20, tests/test_shader_checks.py 79 and all ten tests/test_docs_*.py: OK.
  • Negative control: test_smem_oob_safe_address.py with 3faac557's tools/rdna2spirv.py fails 5 cases: the no-branch shape test, plus 2 subtests each of the out-of-range and zero-size-descriptor tests. test_scalar_mem_smem.py passes 44/44 there too.
  • Not run: llvmpipe/lavapipe (this machine has only the RADV ICD), so the segfault this PR fixes was not reproduced here. The full semantic suite was not run either.

Synthetic fixtures only, no game files.

AI-assisted (Claude Code).

@sovljanskii

Copy link
Copy Markdown
Contributor

Tested f5caa216 (base 3faac557, current main) on an Apple M4, macOS 27.2.1, MoltenVK 1.4.2 (Vulkan 1.4.357), AppleClang 21.0.0, Homebrew LLVM 23.1.6. The translator and fixtures came from this head; the runtime test programs came from a main build, since this PR doesn't touch them. Result: pass, including a whole-corpus count of the current head.

  • run_gpu_tests.py --filter smem_oob: PASS cs_smem_oob_far 0 4008636142 0 0 0 4008636142 0 4008636142. Word 4, the num_records 0 descriptor at VA 0x10_70000000, is 0. For comparison, main's translator on the same fixture also passes here (8 identical words): Metal doesn't fault on the stray read the way llvmpipe does, so the M4 can't show the crash, only that the branchless form is right.
  • Full run_gpu_tests.py: 153/218 passed, 58 skipped. main gives 152/217, 58 skipped; the +1 is cs_smem_oob_far. The 7 failures are the same names as on main.
  • Semantic tests, Python 3.14:
    • test_smem_oob*: 6 OK
    • test_scalar_mem_smem*: 44 OK
    • tests/test_run_gpu_tests.py: 41 OK
    • tests/test_docs_design_rows.py: OK
  • Whole corpus, from my own copy. I ran rdna2spirv.py --shard i/32 --no-asm --assembler python with main and with this head, then compared every .spv by SHA-256:
shaders translated pass spirv-val
main 3faac557 26,503 26,503 26,503
f5caa216 26,503 26,503 26,503

24,803 modules changed and 1,700 are byte-identical. The changed set is exactly the set of listings that contain s_buffer_load (24,803 by grep -l; set difference 0 both ways). So the rewrite reaches every scalar buffer load in the corpus and nothing else, and none of the changed modules loses translation or validity.

No game data is quoted; only counts.

AI-assisted.

# Conflicts:
#	docs/design.md
#	runtime/test/shaders/check.py
#	scripts/run_gpu_tests.py
#	tests/semantic/regen_fixtures.py
#	tests/semantic/test_scalar_mem_smem.py
@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested d47a4a5 on Intel UHD Graphics 630 (CFL GT2), Mesa ANV: run_gpu_tests.py --filter cs_smem_oob_far prints "PASS cs_smem_oob_far 0 4008636142 0 0 0 4008636142 0 4008636142" and "1/1 passed"; with LEONIDA_VK_DEVICE=NoSuchDevice it fails with "no Vulkan physical device matches". With tools/rdna2spirv.py from 3faac55 swapped in, the same row passes with the same words, so on this GPU the fixture does not tell the old and new translator apart and there is no crash to show; the llvmpipe crash was not run. The full suite prints "168/233 passed, 63 skipped" with FAIL cs_fmt_int_narrow ("word 34: expected 0xffffffff, got 0x00000000") and FAIL cs_signed_zero_nan. test_smem_oob_safe_address and test_scalar_mem_smem run 50 tests OK.

AI-assisted.

# Conflicts:
#	runtime/test/shaders/check.py
#	scripts/run_gpu_tests.py
#	tests/semantic/regen_fixtures.py
@Mortify7612

Copy link
Copy Markdown

Tested 4affd830 merged onto main 1cc6e3c1 (clean) on AMD Radeon RX 9060 XT (RDNA4, GFX1200, subgroup 32..64), RADV Mesa 25.0.7, Debian 12 Linux, llvm-mc 23.1.6: pass. I ran run_gpu_tests.py --filter <fixture> for the fixture(s) this PR adds or changes, not the full suite:

PASS cs_smem_oob_far 0 4008636142 0 0 0 4008636142 0 4008636142

AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.