Skip to content

[SPIR-V] Preserve members after merged bitfields in flat conversions - #8715

Open
Diego Novillo (dnovillo) wants to merge 2 commits into
microsoft:mainfrom
dnovillo:fix-8526
Open

[SPIR-V] Preserve members after merged bitfields in flat conversions#8715
Diego Novillo (dnovillo) wants to merge 2 commits into
microsoft:mainfrom
dnovillo:fix-8526

Conversation

@dnovillo

Copy link
Copy Markdown
Collaborator

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.

A same-type flat conversion must decompose to one scalar per SPIR-V
field so that reconstruction consumes the same sequence.
Copilot AI balanced review requested due to automatic review settings July 29, 2026 22:01
@azure-pipelines

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

@dnovillo

Copy link
Copy Markdown
Collaborator Author

Gregory Roth (@pow2clk) please review. Thanks.

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 SPIR-V flat conversion of constant-buffer structs containing merged bitfields, preserving members that follow those bitfields.

Changes:

  • Selects decomposition mode based on source and destination AST types.
  • Supports decomposing merged bitfields as one physical field.
  • Adds regression coverage for copying into a local struct.

Reviewed changes

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

File Description
tools/clang/lib/SPIRV/SpirvEmitter.cpp Implements layout-aware scalar decomposition.
tools/clang/lib/SPIRV/SpirvEmitter.h Updates the decomposition API.
tools/clang/test/CodeGenSPIRV/type.constant-buffer.fn-var.bitfield.hlsl Verifies preservation of post-bitfield members.

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

@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 hate to harp on a comment, but I had this change all backwards expecting that bitfields were expected to make the comparison mismatch, leading to a true for mergebitfields, which was the existing behavior. I think this comment makes more clear why setting the boolean that way chooses the correct index.

Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Outdated
@damyanp

Copy link
Copy Markdown
Member

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

Did you consider adding a release note?

Per our CONTRIBUTING guidance, user-visible bug fixes and new features usually need an entry in docs/ReleaseNotes.md. If this PR is part of a larger multi-PR effort, a single shared release note across that effort is totally fine.

Copilot AI review requested due to automatic review settings September 8, 2026 15:40

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

Distinct compatible struct types can still shift members following merged bitfields.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment on lines +3961 to +3962
const bool includeMergedBitfields =
!astContext.hasSameUnqualifiedType(type, sourceType);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

[SPIR-V] Bitfields can cause wrong struct member access

4 participants