Skip to content

[SPIR-V] Fix vk::BufferPointer bitfield stores - #8713

Merged
Gregory Roth (pow2clk) merged 2 commits into
microsoft:mainfrom
dnovillo:fix-8402
Sep 10, 2026
Merged

Gregory Roth (pow2clk) merged 2 commits into
microsoft:mainfrom
dnovillo:fix-8402

Conversation

@dnovillo

Copy link
Copy Markdown
Collaborator

This fixes #8402.

Storing to a bitfield through vk::BufferPointer crashes in AlignmentSizeCalculator::getAlignmentAndSize. The bitfield update was losing the AST Type and alignment information.

This change preserves the original AST type while producing the replacement value. It calculates the physical-memory alignment from that type and applies it to both the generated load and store.

I added the original reproducer as a new test.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@dnovillo Diego Novillo (dnovillo) added the spirv Work related to SPIR-V label Jul 29, 2026
@dnovillo

Copy link
Copy Markdown
Collaborator Author

Gregory Roth (@pow2clk) I can't add reviewers yet. Could you please review?

@pow2clk

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@pow2clk Gregory Roth (pow2clk) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to convince myself that there was some way to reorganize things so the alignment value could be calculated sooner and the bitfieldLoad wouldn't have to be outside the conditional so the bitfield handling code could be consolidated, but I don't think that's reasonable if it is possible.

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.

Pull request overview

Fixes a SPIR-V codegen crash when storing into a bitfield through vk::BufferPointer by ensuring the replacement value keeps AST type information so alignment can be computed and applied to the generated memory ops.

Changes:

  • Preserve AST result type on the bitfield-inserted store value to avoid a null/invalid type flowing into alignment calculation.
  • Apply the computed PhysicalStorageBuffer alignment to both the generated load (for read-modify-write) and the final store.
  • Add a regression test covering pc.ptr.Get().a = 123 for a bitfield member.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tools/clang/lib/SPIRV/SpirvBuilder.cpp Keeps AST typing/alignment intact for bitfield RMW stores via vk::BufferPointer, preventing the alignment calculator crash.
tools/clang/test/CodeGenSPIRV/vk.buffer-pointer.bitfield.hlsl Adds a regression test asserting the expected OpLoad/OpBitFieldInsert/OpStore sequence with Aligned 4.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tools/clang/lib/SPIRV/SpirvBuilder.cpp Resolved
@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.

Preserve the bitfield AST type and align the generated load and store.

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread tools/clang/lib/SPIRV/SpirvBuilder.cpp Resolved
Copilot AI review requested due to automatic review settings August 25, 2026 19:06

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

@dnovillo

Copy link
Copy Markdown
Collaborator Author

[Auto-generated note from Damyan Pepper (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.

Added an entry in ReleaseNotes.md. Thanks.

@dnovillo

Copy link
Copy Markdown
Collaborator Author

Gregory Roth (@pow2clk). I've addressed all the outstanding comments. If this is now OK, would you mind submitting it for me? Thanks.

@pow2clk

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@pow2clk
Gregory Roth (pow2clk) enabled auto-merge (squash) September 8, 2026 17:37
@pow2clk
Gregory Roth (pow2clk) merged commit 56d52e8 into microsoft:main Sep 10, 2026
13 checks passed
@github-project-automation github-project-automation Bot moved this from New to Done in HLSL Roadmap Sep 10, 2026
Gregory Roth (pow2clk) added a commit that referenced this pull request Oct 9, 2026
…8715)

Since I was doing bitfield fixes in #8713, I took this one too.

This PR fixes #8526.

Copying a `ConstantBuffer` value with merged bitfields into a local
struct overwrites the members after the bitfields. For `struct MyData {
uint a : 16; uint b : 16; uint c; }`, reading `c` returns the word
holding `a` and `b`.

This change makes `processFlatConversion` pick the decomposition mode
from the source and destination AST types. A same-type conversion emits
one scalar per SPIR-V field. But a cast that changes the shape, keeps
one scalar per AST field.

I added the test from the original issue.

---------

Co-authored-by: Gregory Roth <[email protected]>
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: Done

Development

Successfully merging this pull request may close these issues.

[SPIR-V] Compiler SIGSEGV when trying to write bitfield through vk::BufferPointer

4 participants