Skip to content

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

Merged
Gregory Roth (pow2clk) merged 3 commits into
microsoft:mainfrom
dnovillo:fix-8526
Oct 9, 2026
Merged

Gregory Roth (pow2clk) merged 3 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.

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 Resolved
@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 thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
@dnovillo Diego Novillo (dnovillo) added the spirv Work related to SPIR-V label Sep 11, 2026
@dnovillo

Copy link
Copy Markdown
Collaborator Author

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

Done.

Copilot AI review requested due to automatic review settings September 22, 2026 15:56
@dnovillo

Copy link
Copy Markdown
Collaborator Author

Gregory Roth (@pow2clk) ready for another round of 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.

Copilot review overview

🟢 Approval recommended

The implementation is focused, recursively handles merged fields, and includes targeted regression coverage.

Review effort: Balanced
Findings: None

Resolved since last review (1)

A same-type flat conversion must decompose to one scalar per SPIR-V
field so that reconstruction consumes the same sequence.

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.

🟢 Approval recommended

The implementation is targeted, recursively consistent, and covered by a focused regression test.

0 open findings

🧠 Review effort: Balanced

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

Looks Good!

@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) October 9, 2026 17:51
@pow2clk
Gregory Roth (pow2clk) merged commit 6e0fab3 into microsoft:main Oct 9, 2026
12 checks passed
@github-project-automation github-project-automation Bot moved this from New to Done in HLSL Roadmap Oct 9, 2026
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] Bitfields can cause wrong struct member access

4 participants