Skip to content

fix(translator): drop image_store writes outside the image (EGEN-IMGSTORE-OOB) - #5250

Open
evilclanker9000 wants to merge 2 commits into
ItsNotRexed:mainfrom
evilclanker9000:vonsek3/imgstore-oob
Open

evilclanker9000 wants to merge 2 commits into
ItsNotRexed:mainfrom
evilclanker9000:vonsek3/imgstore-oob

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

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

What and why

On RDNA2 an image_store whose coordinate is outside the image writes nothing. The translation emitted OpImageWrite with the raw coordinates; out of range that is undefined in Vulkan (the runtime does not enable robustImageAccess), so a store could land in another texel or the row padding.

Every image_store (1D, 2D, 2D array, 3D; any view class, d16) now runs only for active lanes whose coordinates are < OpImageQuerySize (unsigned per axis, so -1 is out too). Same idea as the image-atomic guard in #5059, which this does not depend on (it touches a different function).

Measurement (gfx1036, Windows driver 32.0.21045.16010, tools/hw_oracle, linear 4x4 R32_UINT T#, image, padding and the bytes after it seeded): stores at x = width, x = -1, y = height, y = -1, x = row pitch and x = 1000 changed nothing; the in-range control stored. image_load out of range returned 0 for x = width, -1, y = height and the pitch; that path (OpImageFetch/OpImageRead) is not touched here.

Tested

  • python -I tests/semantic/test_image_store_bounds.py (new): before the change 4 of 4 dimensions fail ("no OpImageQuerySize before the writes"); after, OK.
  • test_image_store_d16, test_image_store_dmask, test_buffer_store_format, test_image_coherent, tests/test_image_load_typed.py, tests/test_ab_control_flow.py: OK.
  • spirv-as/spirv-val (vulkan1.3), 4 dimensions x plain/glc/d16, plain and --wave64-emulate: 24/24 valid.
  • No GPU needed for the tests; the measurement ran on the RDNA2 iGPU of a Ryzen 9 9950X3D. Not run on the game corpus.

AI-assisted: yes

Notes

  • The guard queries the size through the float view of the same descriptor index; all views of one T# have the same size.
  • Follow-up (not done here): guard the image_load fetch path the same way.

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.

…TORE-OOB)

RDNA2 (gfx1036, tools/hw_oracle) leaves memory unchanged for an image_store at x/y past the edge, at -1 or at the row pitch. OpImageWrite out of range is undefined in Vulkan, so every write is now behind an unsigned coordinate < OpImageQuerySize test per axis (1D, 2D, 2D array, 3D).
@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

At 5dd8051, the new bounds check drops valid stores to integer images. On both the RTX 4090 and the AMD Radeon iGPU, 63 of 64 texels failed for each 8×8 integer image. All float texels passed. With the merge-base translator and the same descriptor layout, all three images passed on both GPUs.

The generated shader queries the float storage descriptor before it selects the write class. In runtime/shims/d3d12_vulkan_device.cpp the runtime fills only the storage view for the selected numeric class. In runtime/src/vulkan_bindless.h the other classes get a 1×1 fallback. The current vk_store_test fills every slot of each class with an 8×8 view. That hides the regression.

To reproduce with the synthetic cs_imgstore fixture on Windows:

  1. In runtime/test/vk_store_test.cpp, give img[0] four mip levels. Keep view[0] at mip 0. Create an additional float view of mip 3 (one level, 1×1). Include all four float mips in the initial image barrier.
  2. In the dii[k][e] initializer, use the 1×1 view for k == 0 and e == 2 or 3 (the float slots for the two integer descriptors). Keep the actual integer views at 8×8. Leave the shader and expected texels unchanged.
  3. Rebuild vk_store_test. Run py -3.14 -I scripts/run_gpu_tests.py --build E:/Leonida-review-work-build/RelWithDebInfo --filter cs_imgstore --require-pass with LEONIDA_W64_EMULATE=1.

At texel (5,3), the unsigned image should contain 129 and the signed image should contain -29. Both read 0 on the head. Repeating with only tools/rdna2spirv.py from 3faac55 gives 129 and -29. Please query the same typed image used by the write and keep the different-sized fallback in the regression test.

AI-assisted.

@CharlyRien

Copy link
Copy Markdown

Tested 5dd8051 (merged on main 3faac55) on RTX 3090 (NVIDIA 616.56, Windows 11 23H2): fail. AI-assisted.

This confirms @shdann's finding with a test that already exists. d3d12_m4_compute is in the default ctest run, so the regression shows up without modifying vk_store_test. That test goes through the D3D12 layer with an R32_UINT RWTexture2D. It fails at the head and passes on main 3faac55 in the same run:

image word 1: got 00000000 want b54ddba7
...
image: 255 of 256 words differ
indirect dispatch output: 382 of 384 words differ
line 385: !mismatches

Everything else matches main (ctest: the same three tests fail on this 23H2 host, plus this one; run_gpu_tests 170/217, 47 skipped). Querying the typed image that the write uses, as @shdann suggests, should make this test pass again.

@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 5dd8051 · Windows 11, Python 3.13.15

  • python tests/semantic/test_image_store_bounds.py -> 1 test OK (4 dims)
  • Main repro: same test with origin/main:tools/rdna2spirv.py -> FAILED (failures=4, "no OpImageQuerySize before the writes")
  • Regressions: test_image_store_d16 4 OK, test_image_store_dmask 13 OK, test_buffer_store_format 39 OK, test_image_coherent 10 OK
  • python -I -m pytest tests/test_image_load_typed.py tests/test_ab_control_flow.py -q -> 76 passed (82 subtests)
  • git diff --check clean

The guard emits one OpImageQuerySize on the float storage view of the same descriptor index, ANDs coordinate < size (unsigned, so -1 is out) per axis with the existing active mask, and puts all three format writes behind it. Unused axes are covered by the emitted vector width. docs/design.md gets the matching contract row.

Verdict: merge-ready.

@Kev-Ball

Copy link
Copy Markdown

Retail translation coverage at 5dd8051c on CachyOS: I searched all 26,503 listings from my Xbox 1.0.2.1 copy for the image_store instruction, then translated every matching listing with its original metadata and --wave64-emulate. All 2,449/2,449 translated and passed Vulkan 1.2 spirv-val (1,946 compute, 503 pixel). The merged index contains exactly those inputs and each has an emitted binary.

Store dimension Listings containing it Instructions
1D 7 7
2D 1,959 6,658
2D array 78 181
3D 480 765

A listing can use multiple dimensions. These counts cover the emitted modules. The integer-view GPU failures already reported above remain a separate runtime result; validation cannot detect a size query against the wrong bound view.

AI-assisted. CPU translation and validation only.

@Atomic258

Copy link
Copy Markdown

Tested 5dd8051 on Windows Server 2022, CPython 3.13.12: pass

  • python -I -m unittest discover -s tests/semantic -p "test_image_store_bounds.py" -> Ran 1 test, OK
  • Negative control (tools/rdna2spirv.py swapped to upstream/main): FAILED (failures=4)

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

@kormocicsaba

Copy link
Copy Markdown

Tested 5dd8051c (up to date with main 3faac557) on Windows 11 Pro 26300, VS 2026 (MSVC) + Ninja, RelWithDebInfo, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8, no game files: fail (d3d12_m4_compute)

GPU driver result
NVIDIA GeForce RTX 4070 621.01 (Vulkan 1.4.357) d3d12_m4_compute fails, plain and with validation
AMD Radeon iGPU 24.10.34 (Vulkan 1.3.287) d3d12_m4_compute fails, plain and with validation

This confirms what @shdann and @CharlyRien found on the same head (RTX 4090, RTX 3090 and an AMD iGPU), now also on an RTX 4070 and another AMD iGPU.

What I ran:

  • tests/semantic/test_image_store_bounds.py: OK. The other tests named in the PR body (test_image_store_d16, test_image_store_dmask, test_buffer_store_format, test_image_coherent, test_image_load_typed.py, test_ab_control_flow.py): OK.
  • Full semantic suite (unittest discover -s tests/semantic): 6246 tests, 3 failures and 2 errors, all in test_loop_ballot_barrier (emulate modes; the errors are NotImplementedError: Unsupported SPIR-V op OpFunctionCall). With main's translator that file fails in the same 5 places, so these failures were already on main.
  • run_gpu_tests.py --filter cs_im (cs_imgstore, cs_imgatomic, cs_imgatomic3d, cs_imgload, cs_imgload_d16) and --filter cs_resinfo, --require-pass: 6/6 on both GPUs, plain and with LEONIDA_VK_VALIDATION=1. cs_imgstore passes only because vk_store_test gives every view class an 8×8 image, as @shdann explained.
  • Full ctest -j4: the only new failure is d3d12_m4_compute, on both GPUs. Without validation: 167 passed, 1 failed, 10 not run. Output: image word 1: got 00000000 want b54ddba7 … indirect dispatch output: 382 of 384 words differ.
  • With validation, d3d12_queue_wait_timeout also hit its 10 s limit. It uses no translated shaders. When I ran it alone it took 2 s with validation and 25 s without, because 8 test sessions were sharing the CPU (84 % load). I count it as noise from the load.

Control: I put main's tools/rdna2spirv.py back and rebuilt d3d12_m4_compute_test, which translates cs_m4_fill/cs_m4_indirect at build time. d3d12_m4_compute then passes on both GPUs, plain and with validation. With main's translator, test_image_store_bounds.py fails 4 of 4 dimensions ("no OpImageQuerySize before the writes"). So the new test does detect the missing guard. It does not detect the wrong view.

Cause, as far as I can tell from the diff: at tools/rdna2spirv.py:11390 the guard always queries %stv0_{di}, the float storage view. The class is picked from the T# format only afterwards. On the D3D12 path an R32_UINT UAV gets a real uint view and a 1×1 fallback in the float slot. Every texel except (0,0) then fails x < 1 || y < 1 and is dropped. Querying the size inside each of the three branches, from the same %stv{cls}_{di} image that the write uses, should fix it.

AI-assisted (Claude Code).

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

@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