Skip to content

fix(translator): image_load outside the image returns zeros (EGEN-IMGLOAD-OOB) - #5307

Open
evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:vonsek3/imgload-oob
Open

evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:vonsek3/imgload-oob

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: EGEN-IMGLOAD-OOB (#4933 (comment))

What and why

On RDNA2 an image_load whose coordinate is outside the image returns 0. The translation emitted OpImageFetch (and OpImageRead for the coherent glc read) with the raw coordinates; out of range both are undefined in Vulkan (the runtime does not enable robustImageAccess), so a lane could read another texel, padding or garbage.

Every typed image_load / image_load_mip / coherent read now runs only when each coordinate is < OpImageQuerySizeLod (OpImageQuerySize for the storage view), unsigned per axis so -1 is out too, and for a mip load the level is also below the level count; a lane outside gets a zero vector through an OpPhi. A descriptor lookup miss (index 0, the fallback image) is read as before, unguarded. Sibling PRs for the same family: #5059 (image atomics) and #5250 (image_store); this one does not depend on them (it touches other functions).

Measurement (gfx1036, Windows driver 32.0.21045.16010, tools/hw_oracle, linear 4x4 R32_UINT T#): image_load at x = width, x = -1, y = height and x = row pitch returned 0; the in-range control returned the texel.

Tested

  • python -I tests/semantic/test_image_load_bounds.py (new): before the change 12 of 12 sub-cases fail ("OpImageQuerySize(Lod) missing from the guard"); after, OK.
  • tests/test_image_load_typed.py follows the fetch result through the new OpPhi (Module.texel); 23 tests OK. test_image_coherent, test_mem_crash_forms, test_fspx_rules, test_isa_coverage, test_shader_profile, test_docs_table_rows: OK. pytest test_cube_sample, test_image_fuzz, test_image_load_d16_typed, test_translate, test_desc_cache: 86 passed, 602 skipped.
  • spirv-as/spirv-val (vulkan1.3): 28/28 image_load forms (1D/2D/2D array/3D, plain/glc/d16, image_load_mip 2D and 3D), plain and --wave64-emulate.
  • Corpus (my own copy, counts only): the first 400 listings 400/400 translated and spirv-val valid; 150 listings that use image_load: 150 valid before and 150 after.
  • No GPU needed for the tests; the measurement ran on the RDNA2 iGPU of a Ryzen 9 9950X3D.

AI-assisted: yes

Notes

  • The OpPhi needs the zero vector built before the branch (found by spirv-val), so a guarded read adds one OpCompositeConstruct per view class.
  • Not touched: image_gather4, image_sample paths and the other OpImageFetch use (image_gather4 mip fetches clamp their own addresses).
  • Hardware behaviour for a mip level past the last level was not measured; it is also treated as out of range (returns 0), an inference noted in docs/design.md's row only as "for a mip load also a level past the last one".

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

…LOAD-OOB)

RDNA2 (gfx1036, tools/hw_oracle) returns 0 for an image_load at x/y past the edge, at -1 or at the row pitch. OpImageFetch and OpImageRead out of range are undefined in Vulkan, so each read is now behind an unsigned coordinate < OpImageQuerySize(Lod) test per axis (and level < levels for mip loads) and merged with a zero vector. A descriptor lookup miss keeps reading the fallback image.
@SweatierKey

Copy link
Copy Markdown
Contributor

Independent CPU verification on Linux (Python 3.13.5 + spirv-as/spirv-val, head 99e7e0f5, base main 3faac557):

CI-breaking test regression found (tests/test_image_load_d16_typed.py):

  • tests/test_image_load_d16_typed.py::test_d16_integer_fetch_saturates_before_packing fails on both parametrized cases (image_load 2D and image_load_mip 3D):
    • main 3faac557: 6/6 pass
    • #5307 99e7e0f5: 4 passed, 2 failed at line 54 (assert len(clamps) == 2).
    • Cause: guarded_image_read() emits OpExtInst %uint %glsl UMin {lod} {last} once per typed view (3×) to clamp safe_lod for OpImageQuerySizeLod, so re.findall(r"OpExtInst %\w+ %glsl UMin ...", text) now matches 5 UMin instructions (3 safe_lod clamps + 2 d16 65535 saturates) instead of 2. Updating that test to filter for the d16 65535 saturate UMins (or checking the OpPhi chain from the uint OpImageFetch) will fix it.

Everything else checked — pass:

  • git diff --check main...HEAD and py_compile — clean.
  • tests/semantic/test_image_load_bounds.py — 2/2 OK; tests/test_image_load_typed.py — 23/23 OK; tests/semantic/test_image_coherent.py — 10/10 OK; tests/semantic/test_image_atomics_32.py — 3/3 OK; tests/semantic/test_image_store_d16.py — 4/4 OK; tests/semantic/test_image_store_dmask.py — 13/13 OK; tests/test_design_contract_rows.py (4/4) & tests/test_docs_table_rows.py (2/2) — OK.
  • spirv-as + spirv-val --target-env vulkan1.3 across 13 synthetic shaders (image_load 1D/2D/2D_ARRAY/3D/CUBE/CUBE d16, glc 1D/2D/2D_ARRAY/3D, image_load_mip 2D/2D d16/3D) — 13/13 PASS.

@SirKingg

Copy link
Copy Markdown
Contributor

Independent Windows GPU verification (AI-assisted), head 99e7e0f5c7eb2dae99504611f00cb9264ff6f733: NVIDIA GeForce RTX 4070 Ti, driver 610.88, Windows 11 Pro build 26300; LLVM AMDGPU 23.1.6 and SPIRV-Tools 2026.4.

  • CPU: python -I tests\semantic\test_image_load_bounds.py 2/2, python -I tests\test_image_load_typed.py 23/23, python -m unittest discover -s tests\semantic -p test_image_coherent.py -v 10/10 passed. The latter two include their available SPIR-V validation checks.
  • Independently assembled and spirv-val --target-env vulkan1.3 validated 16/16 hand-generated compute forms: plain, d16 and glc for 1D, 2D, 2D array and 3D, plus plain and d16 mip loads for 2D and 3D.
  • Built the test programs from this head. python -I scripts\run_gpu_tests.py --llvm-mc <LLVM 23.1.6> --build runtime\build --work runtime\build\image-load-work --filter cs_imgload --require-pass: 2/2 passed (cs_imgload, cs_imgload_d16). These existing fixtures use in-range coordinates, not OOB coordinates.
  • Temporary synthetic OOB probe reused cs_imgload and vk_store_test in words mode: 8x8 R32_UINT sampled image; one lane reads (0,0) as a nonzero control (100), the other 63 read (8,0), (-1,0), or (0,8), in three separate runs. Each run returned 100 for the control and zero for all 63 OOB reads; all three translated and passed spirv-val Vulkan 1.2. The sampled results were stored into a separate signed image and checked against these explicit expected values.
  • Re-ran the two existing fixtures and the probe with VK_LAYER_KHRONOS_validation and GPU-assisted validation enabled, inspecting full harness output: no validation errors observed. The layer issued settings warnings, including forced device feature adjustments.

This checks the translated Vulkan path on NVIDIA, not native RDNA2 hardware or its oracle. The probe covers ordinary 2D uint loads only, not glc, d16, other dimensions or invalid mip levels. In particular it does not measure native behavior beyond the last mip. No game data/corpus was used. The separately reported d16 pytest regression was not retested here.

@sovljanskii

Copy link
Copy Markdown
Contributor

Tested 99e7e0f5 (on main 3faac557) on Apple M4 (Mac16,1), macOS 27.2.1, MoltenVK 1.4.2 (subgroup 32), Apple clang 21.0.0, Homebrew LLVM 23.1.6, Python 3.15.2. The runtime test programs were built from main (this PR changes no C++). GPU and corpus pass. I can confirm @SweatierKey's test regression on macOS.

  • GPU suite: 152/217 passed, 58 skipped, the same as main for every test. In-range image loads are unchanged on MoltenVK. No fixture in the suite reads outside the image, so this run doesn't exercise the guard.
  • Corpus: shard 0/64 of my copy of the game (415 shaders), 415 translated and 415/415 pass spirv-val on both sides.
    • 149 modules change. They are exactly the 149 whose listing has image_load or image_load_mip.
    • The other 266 are byte-identical.
    • Over the whole copy, 9,593 of 25,734 listings use image_load or image_load_mip.
  • CPU: tests/semantic/test_image_load_bounds.py OK, tests/test_image_load_typed.py 23 passed.
  • Regression: tests/test_image_load_d16_typed.py gives 2 failed, 4 passed, against 6/6 on main. Both parametrisations of test_d16_integer_fetch_saturates_before_packing fail, as SweatierKey describes. That test needs the fix before merge.

Counts only, no game files. AI-assisted.

@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.

Head 99e7e0f5c. git diff --check clean.

  • python -I tests/semantic/test_image_load_bounds.py -> 2 tests OK (12 sub-cases: 1D/2D/2D-array/3D, sampled/d16 fetch and coherent read).
  • Same file against origin/main's translator -> FAILED (failures=12), AssertionError: 0 != 1 : OpImageQuerySizeLod missing from the guard, as the PR says.
  • python -I -m pytest tests/test_image_load_typed.py -q -> 23 passed, 53 subtests.
  • python -I -m pytest tests/test_cube_sample.py tests/test_image_fuzz.py tests/test_image_load_d16_typed.py tests/test_translate.py tests/test_desc_cache.py -q -> 86 passed, 602 skipped.
  • semantic test_image_coherent 10 OK, test_mem_crash_forms 1 OK, test_fspx_rules 9 OK, test_isa_coverage 20 (4 skipped), test_shader_profile 38, test_docs_table_rows 2.
  • spirv-as + spirv-val --target-env vulkan1.3 on translated 1D, 2D, 2D-array-d16 and 3D-glc loads -> 4/4 valid.

Verdict: approve. The guard is per-axis unsigned and merged with a zero OpPhi; the past-the-last-level mip rule is still unmeasured and docs/design.md now calls it an inference.

@Kev-Ball

Copy link
Copy Markdown

At 99e7e0f5, I exercised the 3D explicit-mip guard on an RTX 3080 Ti (CachyOS, NVIDIA 620.73.04). I used the synthetic 4×4×4, three-level harness from #5292 and checked the red and alpha components (dmask:0x9) for unsigned, signed and float views.

  • The last valid texel of each mip (3,3,3 at L0; 1,1,1 at L1; 0,0,0 at L2) returned the expected values for all 18 component checks.
  • All 42 out-of-range component checks returned zero: negative x, x/y/z at the L0 extent, z at the smaller L1 extent, and mip 3 or −1. As a control I changed only the guard’s fallback vector to typed ones, retranslated and validated. Exactly those 42 words changed to the corresponding integer or float one. All 18 in-range words, the untouched destination texels and the two other readback buffers stayed exact. This distinguishes taking the guard from the driver coincidentally returning zero on an out-of-range fetch.

I also scanned all 26,503 retail listings from my Xbox 1.0.2.1 copy and translated every listing containing image_load or image_load_mip, with its original metadata and --wave64-emulate. 9,593/9,593 translated and passed Vulkan 1.2 spirv-val: 4,278 pixel, 3,088 mesh, 1,783 compute and 444 vertex. The merged index keys and emitted binaries matched the selected inputs exactly. The retail check covers translation and validation; the GPU readback above uses synthetic input. AI-assisted (OpenAI Codex).

@evilclanker9000

Copy link
Copy Markdown
Contributor Author

Update: the 3D case is now measured too, so the note "not measured on 3D" no longer applies. On gfx1036 (same driver), a 64x1x4 R32_UINT linear volume with 256-byte slices: image_load, image_store and image_atomic (umax/add/cmpswap) with x = 64, y = 1, z = 4 or z = -1 left every seeded word unchanged, and a load or a glc atomic returned 0; the in-range controls worked. docs/design.md says so in the commit pushed just now (docs only, no code change).

@evilclanker9000

Copy link
Copy Markdown
Contributor Author

Correction to my comment above: the docs commit I mentioned has not been pushed yet; my push was blocked in this environment. The 3D measurement itself stands (it is in the comment), and the matching docs/design.md sentence will follow in a separate docs-only commit once I can push.

@lunarminus

Copy link
Copy Markdown

Tested 99e7e0f5 merged onto main d0835dd9 on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: fail. tests/test_image_load_d16_typed.py::test_d16_integer_fetch_saturates_before_packing fails in both parametrisations, as reported above. The GPU suite and the PR's own tests pass.

  • Merge: the merge onto d0835dd9 was a fast-forward, so the tree tested is 99e7e0f5 itself, which already contains main up to 3faac557. I built the runtime test programs fresh from that tree. As a control I used a clean 3faac557 worktree run with the same binaries (this PR changes nothing under runtime/).
  • Regression: tests/test_image_load_d16_typed.py gives 2 failed, 4 passed, against 6 passed on 3faac557. Both image_load 2D and image_load_mip 3D fail at assert len(clamps) == 2 with 5 == 2. The UMin pattern now also matches the three safe_lod clamps from guarded_image_read; the other two matches are the %cffff saturates the test is looking for. The author's pytest list (test_cube_sample, test_image_fuzz, test_image_load_d16_typed, test_translate, test_desc_cache) gives 686 passed, 2 failed, 0 skipped here, against 688 passed on 3faac557.
  • New test: tests/semantic/test_image_load_bounds.py passes (2 tests). Against the 3faac557 translator it gives 12 failures ("OpImageQuerySize(Lod) missing from the guard").
  • Other CPU tests, all OK on both trees:
    • tests/test_image_load_typed.py 23/23.
    • Semantic test_image_*.py: 44 OK (42 on 3faac557, which lacks the new bounds file).
    • test_mem_crash_forms 1, test_fspx_rules 9, test_isa_coverage 20 (4 skipped), test_shader_profile 38, test_docs_table_rows 2, test_design_contract_rows 4.
  • spirv-val: 16 hand-built forms pass spirv-as + spirv-val --target-env vulkan1.3 (16/16): image_load plain/d16/glc for 1D, 2D, 2D array and 3D, plus image_load_mip plain/d16 for 2D and 3D. They ran on a wave32 compute stub, where the wave64-emulate switch left the output byte-identical, so the --wave64-emulate path is not covered by this sweep.
  • GPU suite: scripts/run_gpu_tests.py gives 202/217 passed, 15 skipped, 0 failed, with 221/221 shaders translated and spirv-val valid. That holds both with and without LEONIDA_W64_EMULATE=1; the emulated run produced 38 different modules.
  • Per-test comparison:
    • Against 3faac557 without the PR: same result on all 217 result lines in both modes.
    • Against d0835dd9 without the PR (196/210, 14 skipped): the 210 lines it shares are unchanged. The 7 extra lines (cs_smem_sconst_off, ps_far_branch, ps_sample_c, ps_sample_c_b, ps_texture_b_cl and ps_texture_cl PASS; ps_m2_literal SKIP) are fixtures that came in with main, not with this PR, which adds no GPU fixture.
  • cs_imgload filter: --filter cs_imgload --require-pass gives 2/2 (cs_imgload, cs_imgload_d16) in both modes, the same as on 3faac557.
  • Coverage limit: no fixture in the suite reads outside an image. On RADV this shows in-range loads are unchanged; it does not exercise the new guard.

No game files used.

AI-assisted (Claude Code).

@evilclanker9000

Copy link
Copy Markdown
Contributor Author

The docs/design.md sentence for the 3D measurement is now pushed (docs only; #5059 and #5250 also merged current main to resolve a docs/design.md conflict, no code changes).

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.

7 participants