Skip to content

Fix #11506: accept heap texel pointer as atomic destination#11507

Draft
nv-slang-bot[bot] wants to merge 2 commits into
masterfrom
fix/issue-11506
Draft

Fix #11506: accept heap texel pointer as atomic destination#11507
nv-slang-bot[bot] wants to merge 2 commits into
masterfrom
fix/issue-11506

Conversation

@nv-slang-bot
Copy link
Copy Markdown
Contributor

@nv-slang-bot nv-slang-bot Bot commented Jun 7, 2026

Summary

InterlockedAdd (any atomic) on a texel of a DescriptorHandle<RWTexture2D<uint>> failed to compile with error[E41403]: invalid atomic destination — but only under -capability spvDescriptorHeapEXT. A plain RWTexture2D<uint> (or the same DescriptorHandle without the capability) did not hit E41403. This is a false-negative in the IR atomic-destination validator; the fix teaches it about the descriptor-heap-lowered texel pointer.

Diagnosis (root cause)

  • Under spvDescriptorHeapEXT, processImageSubscript (source/slang/slang-ir-spirv-legalize.cpp:1357-1370) replaces the IRImageSubscript atomic destination with an IRSPIRVLoadTexelPointerFromHeap (a pointer in AddressSpace::Image). The plain-texture branch (:1352-1355) leaves the IRImageSubscript in place.
  • Atomic-operation validation is deliberately deferred for SPIR-V (source/slang/slang-emit.cpp:1917-1921) and runs at the end of legalization (source/slang/slang-ir-spirv-legalize.cpp:2789) — i.e. after that rewrite.
  • isValidAtomicDest (source/slang/slang-ir-validate.cpp:455-527) accepts IRImageSubscript (:463) and an address-space allowlist (:468-487), but has no case for IRSPIRVLoadTexelPointerFromHeap and no AddressSpace::Image case → falls through to return false (:526) → InvalidAtomicDestinationPointer / E41403.
  • The op is genuinely a valid atomic target: it already emits as OpUntypedImageTexelPointerEXT (source/slang/slang-emit-spirv.cpp:4934-4959), atomicable just like OpImageTexelPointer. So emission was already correct — only the IR-level validator was out of date.

Approach

Accept IRSPIRVLoadTexelPointerFromHeap in isValidAtomicDest, immediately after the existing IRImageSubscript case — the post-lowering l-value form of the same construct the validator already accepts pre-lowering. One added check.

Alternatives considered and rejected:

  • Broad case AddressSpace::Image: return true; — would bless any Image-space pointer as an atomic destination; less safe than recognizing the exact heap-texel-pointer opcode. (Independent review concurred: prefer the opcode check.)
  • Reorder validation / change the heap rewrite — SPIR-V atomic validation is intentionally deferred until after address-space specialization so groupshared function-parameter atomics validate; reordering would regress that.

Files changed

  • source/slang/slang-ir-validate.cpp — accept IRSPIRVLoadTexelPointerFromHeap as a valid atomic destination (+ a short rationale comment).
  • tests/bugs/gh-11506-descriptor-handle-texture-atomic.slang — new compile-only regression test.

Tests

  • Negative proof (without the fix): the repro fails with error[E41403]: invalid atomic destination.
  • Positive proof (with the fix): compiles cleanly and emits OpUntypedImageTexelPointerEXT + OpAtomicIAdd.
  • New test passes via slang-test.
  • Regression: 36/36 atomic + descriptor-heap + image-atomic tests pass (tests/spirv/atomic*, tests/spirv/descriptor-heap*, tests/spirv/image-atomic*, tests/bugs/vk-image-atomics, etc.), 8 GPU-only tests ignored.
  • Note: clang-format is not available in this environment, so extras/formatting.sh was not run locally; the change matches the surrounding style and CI's format check covers it.

⚠️ Known limitation — bindless image format (#11130)

This PR fixes only the spurious E41403 atomic-validator error. The front-end / IR path now compiles and emits the correct ops, but end-to-end binary SPIR-V still fails spirv-val for bindless texture atomics, because the bindless image type carries image format Unknown:

Expected the Image Format in Image to be R64i, R64ui, R32f, R32i, or R32ui
for Vulkan environment ... OpUntypedImageTexelPointerEXT

This missing-format defect is orthogonal to the atomic validator and is tracked separately by #11130. It reproduces for DescriptorHandle<RWTexture2D<uint>> atomics both with and without spvDescriptorHeapEXT (a plain RWTexture2D<uint> infers R32ui and validates; bindless handles get Unknown). It lives on the bindless image-format surface (see also #11499, #11503), not the atomic validator. Accordingly, the regression test uses -skip-spirv-validation and FileChecks the emitted opcodes (format-agnostic, so it stays green once #11130 lands).

Risk

Validation-only; the change widens what counts as a valid atomic destination by exactly one opcode that is produced solely on the texture-atomic heap-lowering path, so it cannot newly reject any existing program. No public header / ABI / language-surface change.

Fixes #11506.

🤖 Generated by an automated Slang coworker — may be inaccurate. A human maintainer should verify.

Under -capability spvDescriptorHeapEXT, the descriptor-heap lowering
rewrites an image-subscript atomic destination into an
IRSPIRVLoadTexelPointerFromHeap. The deferred atomic-destination
validator isValidAtomicDest, which runs after that rewrite, accepted
IRImageSubscript but had no case for the heap texel pointer, so it
rejected the atomic with error E41403 ("invalid atomic destination").
A plain RWTexture2D keeps its IRImageSubscript and always validated.

Accept IRSPIRVLoadTexelPointerFromHeap as a valid atomic destination,
mirroring the existing IRImageSubscript case; the op already emits as
the atomicable OpUntypedImageTexelPointerEXT.

Adds a compile-only regression test. The emitted SPIR-V still fails
binary validation because bindless image handles carry image format
Unknown (tracked separately by #11130, with and without the
capability), so the test skips binary validation and FileChecks the
emitted opcodes.

Co-authored-by: Harsh Aggarwal <haaggarwal@nvidia.com>
@nv-slang-bot nv-slang-bot Bot added the pr: non-breaking PRs without breaking changes label Jun 7, 2026
Address advisory clarity nits from PR #11507 review: broaden the
function's contract comment to cover image/texel destinations (it
omitted IRImageSubscript even before this change), and expand the
heap-texel-pointer comment with the deferred-validation order and
sole-producer facts that make unconditional acceptance safe.

Comment-only; no behavior change.

Co-authored-by: Harsh Aggarwal <haaggarwal@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: non-breaking PRs without breaking changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Atomic operations don't work with DescriptorHandle<RWTexture2D<uint>> when -capability spvDescriptorHeapEXT is used

0 participants