Skip to content

Disable 16bit Store with RWByteAddressBuffer for SPIRV - #8426

Open
Alexander Johnston (Alexander-Johnston) wants to merge 3 commits into
microsoft:mainfrom
Alexander-Johnston:16bitrwbabclobber
Open

Alexander Johnston (Alexander-Johnston) wants to merge 3 commits into
microsoft:mainfrom
Alexander-Johnston:16bitrwbabclobber

Conversation

@Alexander-Johnston

Copy link
Copy Markdown

As we represent RWByteAddressBuffers in SPIRV as an array of uints we have no correct way to represent a 16bit store to the buffer. Currently we load a uint, mask values from it, write in our new 16 bits, then write to the buffer. This works for single threaded cases but if multiple threads are accessing adjacent shorts in the uint data is clobbered.

This patch catches the write in Sema and errors instead of going on to produce invalid code.

The clobbering behaviour can be seen in this test.

@Alexander-Johnston

Copy link
Copy Markdown
Author

Alexander Johnston (@Alexander-Johnston) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree company="Advanced Micro Devices"

@dnovillo Diego Novillo (dnovillo) added the spirv Work related to SPIR-V label Jul 13, 2026
Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Resolved
@github-project-automation github-project-automation Bot moved this from New to In progress in HLSL Roadmap Jul 16, 2026
@damyanp

Copy link
Copy Markdown
Member

[Auto-generated note from Damyan Pepper (@damyanp)]

This looks like a user-visible bug fix/feature change. Please add (or point to) the corresponding entry in docs/ReleaseNotes.md.

If release-note coverage is planned in a related PR (including one that hasn’t been submitted yet), please mention that plan/link so we can avoid duplicate notes.

As we represent RWByteAddressBuffers in SPIRV as an array of uints we have no
correct way to represent a 16bit store to the buffer. Currently we load a uint,
mask values from it, write in our new 16 bits, then write to the buffer. This
works for single threaded cases but if multiple threads are accessing adjacent
shorts in the uint data is clobbered.

This patch catches the write in Sema and in the SPIRV emitter, and errors instead
of going on to produce invalid code with race conditions.

This is a non-ideal solution, but for RWByteAddressBuffers to support 16 bit
stores we require SPIRV Untyped Pointers which are not yet widely available.
The best we can do for now is to error on HLSL code which would generate the
invalid code.
The affected tests were testing the tight packing of 16 bit values in
RWByteAddressBuffers in SPIRV, ensuring that they matched the DX layout.
The load test is still legal and useful, but as the store tests are
testing something we are banning, they have been removed.

Copilot AI 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.

🟡 Changes recommended

Semantic validation misses inherited 16-bit fields and minimum-precision types using native 16-bit storage.

2 open issues
What changed in this PR

This PR rejects 16-bit RWByteAddressBuffer stores in SPIR-V compilation to prevent adjacent writes from clobbering data.

Changes:

  • Adds semantic validation and a backend diagnostic.
  • Replaces affected store tests with diagnostic coverage.
  • Documents the restriction in release notes.
File Description
tools/​clang/​test/​SemaHLSL/​rwbyteaddressbuffer-store-16bit-spirv.hlsl Adds rejection tests.
tools/​clang/​test/​CodeGenSPIRV/​method.byte-address-buffer.templated-store.struct2.hlsl Removes unsupported store expectations.
tools/​clang/​test/​CodeGenSPIRV/​method.byte-address-buffer.templated-store.struct.hlsl Removes unsupported aggregate-store expectations.
tools/​clang/​test/​CodeGenSPIRV/​method.byte-address-buffer.templated-load.struct3.hlsl Removes the unsupported store from a load test.
tools/​clang/​lib/​SPIRV/​RawBufferMethods.cpp Rejects 16-bit stores during code generation.
tools/​clang/​lib/​Sema/​SemaHLSL.cpp Adds recursive store-type validation.
tools/​clang/​include/​clang/​Basic/​DiagnosticSemaKinds.td Defines the semantic diagnostic.
docs/​ReleaseNotes.md Documents the behavior change.

Review effort: Balanced

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Resolved
Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Resolved
Catches minX BuiltinTypes
Catches base structs in inheritence chains
Copilot AI balanced review requested due to automatic review settings October 23, 2026 15:38

Copilot AI 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.

🔵 Needs a closer look

SPIR-V and DXIL regression validation remains unverified for the new semantic restriction and backend error path.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Low severity Use one sentence for the release-note description

docs/​ReleaseNotes.md:91

Minor: CONTRIBUTING.md:138 requires release-note descriptions to be a single sentence, but this entry uses two. Combine the restriction and its diagnostic into one sentence.

2 resolved since last review

Review effort: Balanced

This branch has not been deployed

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

Labels

spirv Work related to SPIR-V

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

4 participants