Skip to content

fix(translator): skip image atomics outside the image, return 0 with glc (EGEN-IMGATOMIC-OOB) - #5059

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

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

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: EGEN-IMGATOMIC-OOB (finding and claim: #4933 (comment))

What and why

On RDNA2 an image_atomic_* whose texel coordinate is outside the image leaves memory untouched and returns 0 with glc. The translation built OpImageTexelPointer from the raw coordinates; an out-of-range texel pointer is undefined in Vulkan, so an atomic could land in row padding or in another texel.

Every 2D and 3D image_atomic_* (add/sub/and/or/xor/smin/smax/umin/umax/swap/inc/dec/fmin/fmax/cmpswap/fcmpswap) now runs only for active lanes whose coordinates are < OpImageQuerySize (unsigned per axis, so -1 is out too). With glc a lane that skipped gets 0 through an OpPhi; an inactive lane keeps vdata as before. The plain and cmpswap paths now share emit_image_atomic; the unused "neutral value" column of IMG_ATOMICS is gone.

Measurement (gfx1036, Windows driver 32.0.21045.16010, tools/hw_oracle, linear 4x4 R32_UINT T#, every word of the image, its row padding and the bytes after it seeded): umax/umin/or/add/swap/cmpswap at x = width, x = -1, y = height, y = -1, x = row pitch, x = 1000 and (width, height) changed nothing and returned 0; in-range controls updated the texel and returned the old value.

Tested

  • python -I tests/semantic/test_image_atomic_bounds.py (new): before the change 64 of 64 op/dim/glc combinations fail ("no OpImageQuerySize of the atomic's image"); after, OK.
  • test_image_atomics_32, test_fspx_rules: OK before and after.
  • spirv-as/spirv-val (vulkan1.3) on all 16 ops x 2D/3D x glc/no glc, plain and under --wave64-emulate: 128/128 valid.
  • Full python -m unittest discover -s tests/semantic: the same 6 failures with and without this change (test_loop_ballot_barrier 3 FAIL + 2 ERROR, test_vcmp16_opsel.test_spirv_val_compliance 1 ERROR; checked on a clean export of main).
  • 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

  • 3D follows the same rule per axis but was not measured natively (documented as such in docs/design.md).
  • image_store has the same undefined out-of-range case for OpImageWrite; I left it for a separate PR.

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.

…th glc (EGEN-IMGATOMIC-OOB)

RDNA2 (gfx1036, measured with tools/hw_oracle) leaves memory untouched and returns 0
with glc for an image_atomic_* at x/y past the edge, at -1, or at the row pitch. The
translation built OpImageTexelPointer from the raw coordinate, which is undefined in
Vulkan. Every 2D/3D image atomic is now guarded by an unsigned coordinate < OpImageQuerySize test.
@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 2025cb3 on AMD Radeon 610M (RADV RAPHAEL_MENDOCINO, subgroup 32..64), Mesa 26.2.6, CachyOS Linux, LEONIDA_VK_DEVICE=RADV: pass.

  • scripts/run_gpu_tests.py --filter imgatomic: 3/3 passed. PASS cs_imgatomic and cs_imgatomic3d (all 64 words of the uint image, the sint image and the volume exact); PASS ps_imgatomic (0 of 256 texels wrong, 0 of 4096 render-target pixels wrong). LEONIDA_VK_DEVICE=NoSuchDevice fails with "no Vulkan physical device matches", so the RADV device ran them.
  • Full scripts/run_gpu_tests.py after a full build: 202/217 passed, 15 skipped, no FAIL lines.
  • tests/semantic test_image_atomic_bounds (new): Ran 1 test, OK. test_image_atomics_32 and test_fspx_rules: OK.

The three imgatomic fixtures are the existing in-range checks; the out-of-bounds coordinates (x = width, -1, height) were not run on a GPU here, only in the new semantic test.

AI-assisted.

@Atomic258

Copy link
Copy Markdown

Independent check — pass (CPU only, no game files).

Environment: Windows 11, CPython 3.15.2.

  • python -I tests/semantic/test_image_atomic_bounds.py → 1/1 — OOB image atomics skip and return 0 with glc
  • Full python -m unittest discover -s tests/semantic -v → 6,246 tests; the only failures are the same 3 fails + 2 errors in test_loop_ballot_barrier.py I verified failing identically on main (wave64-emulate OpFunctionCall interpreter gap + "structured" vs "rounds" naming) — pre-existing, unrelated to image atomics
  • docs/design.md contract row added as required for a new translator rule

A whole-corpus translated/spirv-val count is still the usual gate for a rule change; a real-shader disasm corpus exists here and a slice check can follow if reviewers want it.

AI-assisted (Devin).

@CryingLaughing

Copy link
Copy Markdown
Contributor

Tested 2025cb3 on native Windows 11 build 26200, RTX 4080 (621.01 / 32.0.16.2101), MSVC 19.44 Release, Khronos validation 1.4.357. The three stock imgatomic fixtures passed. I also exercised the new bounds behavior on the GPU with one synthetic vk_store_test dispatch over seeded 2D and 3D images.

  • GLC: across 2D add, 3D add and 3D compare-swap, all 17 positions outside the images returned zero. All seven in-range controls returned the previous texel and made the expected update.
  • Without GLC: the 2D add preserved all eight input register values. Only its one in-range position updated the image.
  • Readback: all 192 texels across the two target images and the separate result image matched. The layer was active and logged zero validation errors or warnings.

The module passed spirv-val. Before GPU execution I verified that all four texel-pointer blocks were behind the active-lane check and the unsigned coordinate check for every axis. Both the new bounds semantic test and the existing image-atomic semantic tests passed. AI-assisted; synthetic inputs only.

@DenRakEiw

Copy link
Copy Markdown

Independent hardware review of 2025cb385f22c16db2d86bf26b0cdde4dc88942b (merged cleanly onto main 3faac557; git diff --check clean).

Note on overlap with #5077: This PR and #5077 (opened later by @milas6669) address the exact same finding (#4933) and modify the same image_atomic_* paths in tools/rdna2spirv.py and the same docs/design.md contract row — only one of the two should be merged. (This PR avoids a second OpSelectionMerge block on glc by using OpPhi + self.dst, drops the unused neutral-value column in IMG_ATOMICS, and covers all 64 op/dim/glc combinations in its semantic test.)

Environment: Windows 11 x64, Python 3.12.10, SPIRV-Tools v2026.4 (Vulkan SDK 1.4.363.0), LLVM_MC (LLVM 21 via WSL), LEONIDA_W64_EMULATE=1.
GPUs: NVIDIA GeForce RTX 5090 (driver 32.0.16.1088) and AMD Radeon(TM) Graphics RDNA2 iGPU (driver 31.0.22044.1).

Results:

  • python -m unittest discover -s tests/semantic -p test_image_atomic_bounds.py: 1/1 OK (all 64 op/dim/glc combinations)
  • test_image_atomics_32.py: 3/3 OK
  • test_fspx_rules.py: 9/9 OK
  • scripts/run_gpu_tests.py --filter imgatomic (4/4 shaders translated, 4/4 pass spirv-val):
    • RTX 5090: 3/3 PASS (cs_imgatomic, cs_imgatomic3d, ps_imgatomic)
    • AMD Radeon(TM) Graphics (RDNA2 iGPU): 3/3 PASS (cs_imgatomic, cs_imgatomic3d, ps_imgatomic)

All tested semantic and in-range GPU checks pass; out-of-bounds coordinates are verified structurally in the new semantic test (not executed on GPU by the stock imgatomic fixtures).

AI-assisted (Gemini 4 Neon).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 2025cb3 on Intel UHD 630 (CFL GT2, subgroup 8..32), Mesa 25.2.8 ANV, Ubuntu 24.04, LEONIDA_VK_DEVICE=Intel: pass.

  • scripts/run_gpu_tests.py --filter imgatomic after a full build: 3/3 passed. PASS cs_imgatomic and cs_imgatomic3d (all 64 words of the uint image, the sint image and the volume exact); PASS ps_imgatomic (0 of 256 texels wrong, 0 of 4096 render-target pixels wrong).
  • With LEONIDA_VK_DEVICE=NoSuchDevice the two cs_ rows fail with "no Vulkan physical device matches". ps_imgatomic still passes there because its runner, vk_bindless_test, ignores LEONIDA_VK_DEVICE; its own device line reads "device: Intel(R) UHD Graphics 630 (CFL GT2)", so the cs_ rows are the proof that the filter applied.
  • tests/semantic test_image_atomic_bounds: Ran 1 test, OK.

The full tests/semantic suite and the out-of-bounds coordinates on a GPU were not run here.

AI-assisted.

@Atomic258

Copy link
Copy Markdown

Tested 2025cb3 on Windows Server 2022, CPython 3.13.12: pass

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

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

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

5 participants