Skip to content

Fix bitfields in flat conversions on SPIR-V and DXIL - #9025

Open
Diego Novillo (dnovillo) wants to merge 5 commits into
microsoft:mainfrom
dnovillo:fix-merged-bitfields
Open

Diego Novillo (dnovillo) wants to merge 5 commits into
microsoft:mainfrom
dnovillo:fix-merged-bitfields

Conversation

@dnovillo

@dnovillo Diego Novillo (dnovillo) commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

Flat conversions, splats, and aggregate initializer lists that flatten a struct with bitfields treat each bitfield as its whole storage unit. For struct Bits { uint a : 16; uint b : 16; uint c; } and Bits s = {1, 2, 3}:

  • (uint3)s gives (131073, 131073, 3) on both DXIL and SPIR-V.
  • On SPIR-V, casting s to a struct of the same shape fails validation with Index is out of bounds, because Bits has three fields and only two SPIR-V members.

#7074 reports this for DXIL, but it also occurs in SPIR-V.

Each bitfield is one element of the flattened struct and converts as its underlying type, as the HLSL spec (microsoft/hlsl-specs#743) and Clang (llvm/llvm-project#161263) define it. This change reads and writes each bitfield through its own bits:

  • DXIL: flat conversions, splats, and initializer lists load and store each bitfield through a bitfield lvalue.
  • SPIR-V: the same paths extract or insert each bitfield at its offset and width in the member it shares. Base classes are split first, and same-type conversions still copy each member whole.
  • SPIR-V: bool bitfields use a uint container in every layout, because the bitfield instructions take integers and merged bitfields share one member. Loads compare the extracted bit with zero, and stores select 1 or 0.

Tests:

  • cast.flat-conversion.merged-bitfield.hlsl: member indices and extracts for merged bitfields, including a base class.
  • cast.flat-conversion.bitfield-elements.hlsl: extracts and inserts for struct-to-vector, vector-to-struct, splat and signed bitfields.
  • cast.flat-conversion.boolean-bitfield.hlsl: bool bitfields in buffers and cbuffers.
  • cast.flat-conversion.local-boolean-bitfield.hlsl: bool bitfields in temporaries, return values and nested locals.
  • cast.struct-to-int.hlsl: each bitfield extracted at its own offset.
  • CodeGenDXIL/hlsl/types/struct-bitfield-flat-conversion.hlsl: element values for every conversion kind on DXIL.

Fixes #7074

Copilot AI balanced review requested due to automatic review settings October 9, 2026 19:17
@azure-pipelines

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

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 aligns splitting with destination construction and includes focused regression coverage.

0 open findings

What changed in this PR

Fixes SPIR-V struct casts when AST bitfields share lowered members, including inherited structs.

Changes:

  • Extracts members using lowered SPIR-V indices and bitfield ranges.
  • Adds regression coverage for merged bitfields and base classes.
  • Documents the fix in release notes.
File Description
tools/​clang/​lib/​SPIRV/​InitListHandler.cpp Corrects struct splitting for lowered members.
tools/​clang/​test/​CodeGenSPIRV/​cast.flat-conversion.merged-bitfield.hlsl Adds casting regression tests.
docs/​ReleaseNotes.md Records the SPIR-V fix.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@dnovillo

Copy link
Copy Markdown
Collaborator Author

Gregory Roth (@pow2clk) this came out of #8715, could you take a look? 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.

🟢 Approval recommended

The implementation matches the lowered layout contract and includes focused regression coverage.

0 open findings

🧠 Review effort: Balanced

@pow2clk

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

@llvm-beanz

Copy link
Copy Markdown
Collaborator

I think this is actually broken on the DXIL path too, so this change might actually make things worse by making the SPIRV side correct while the DXIL side is wrong.

@dnovillo

Copy link
Copy Markdown
Collaborator Author

I think this is actually broken on the DXIL path too, so this change might actually make things worse by making the SPIRV side correct while the DXIL side is wrong.

Fix the DXIL path in a separate PR or would you rather fix it in this one?

@llvm-beanz

Copy link
Copy Markdown
Collaborator

Fix the DXIL path in a separate PR or would you rather fix it in this one?

I'm okay with it either way, but if we're going to defer it we should file an issue and make sure it is prioritized. I'm more comfortable with the current state (broken in both codegen paths for DXC) than a place where SPIRV and DXIL generation disagree.

Copilot AI balanced review requested due to automatic review settings October 22, 2026 20:59

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 focused fix aligns source splitting with destination initialization, includes regression coverage for both layouts, and has no identified blocking issues.

No open issues 🎉

Review effort: Balanced

@dnovillo Diego Novillo (dnovillo) changed the title [SPIR-V] Split merged bitfields by SPIR-V member when casting structs Fix bitfields in flat conversions on SPIR-V and DXIL Oct 23, 2026
@dnovillo

Copy link
Copy Markdown
Collaborator Author

Fix the DXIL path in a separate PR or would you rather fix it in this one?

I'm okay with it either way, but if we're going to defer it we should file an issue and make sure it is prioritized. I'm more comfortable with the current state (broken in both codegen paths for DXC) than a place where SPIRV and DXIL generation disagree.

Extending the fix to DXIL was not a lot of code, so I rolled it in the latest commit. To keep both backends in agreement, the commit also fixes the remaining SPIR-V flat conversion paths: struct to vector, vector to struct and splats. The PR now fixes #7074.

I updated the title and description to match. PTAL.

Copilot AI balanced review requested due to automatic review settings October 23, 2026 18: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.

🔵 Needs a closer look

Cross-backend layout changes and unresolved SPIR-V boolean representation issues require fixes and human review backed by compiler regression results.

3 open issues

Review effort: Balanced

Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Copilot AI balanced review requested due to automatic review settings November 16, 2026 20:29

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

No active review issues are reported for the bitfield conversion fixes.

1 open issue
2 resolved since last review

Review effort: Balanced

@dnovillo

Copy link
Copy Markdown
Collaborator Author

Rebased to solve conflicts. Chris B (@llvm-beanz) could you please run CI and take a second look at this? 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.

🟢 Approval recommended

No active review issues block approval of the bitfield-handling changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@dnovillo Diego Novillo (dnovillo) left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Gregory Roth (@pow2clk) ping for review. Thanks.

@pow2clk

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

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

@dnovillo

Copy link
Copy Markdown
Collaborator Author

Sarah Spall (@spall) would you be able to review this PR? Particularly, the DXIL bits. Thanks.

Copilot AI balanced review requested due to automatic review settings December 2, 2026 20:01

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

The test’s exclusion misses the source-load interval before OpCompositeConstruct, allowing an unexpected OpINotEqual during the bitfield copy to go undetected.

1 open issue

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.

On the subject of the description

The DXIL side is #7074

At this point, I think we can declare #7074 to apply to both backends

On the subject of the need for this change at all

I have a lot of thoughts here. This is a change in behavior from the admittedly recent addition of bitfields in a situation that I think is at least somewhat ambiguous. Should the storage elements or field elements determine whether a structure is castable one to another? A bitcast demands one thing. An element cast demands the other. The C-style cast isn't either explicitly and the issue hasn't come up before.

What's more, once initializer lists are altered, none of this is going to be allowed. So we are just setting users up for confusion, broken behavior from the past, and a compiler error in the future. I would prefer casting between structures with bitfields just be disallowed until we can provide user copy constructors to handle them as they see fit or explicit bit or element wise casts where the behavior is clear.

On the subject of the implementation of this change

I was unsettled by how many additional places this adds conversions between booleans and integers to the SPIR-V backend when I thought CodeGen already took care of all of that. Since this is an HLSL characteristic, I would have expected clang to be the logical place for it. I saw several checks for the layout rule. I would still expect that to only apply to resources rather than how bools are represented within register variables.

Additionally, I saw a lot of code that adds support for flattened initialization lists which seems to already work just fine for structs with bitfields. Again, I'm not sure this is solving a problem or introducing dead code.

Comment thread docs/ReleaseNotes.md Resolved
Comment thread tools/clang/lib/CodeGen/CGHLSLMS.cpp Resolved
Comment thread tools/clang/lib/SPIRV/SpirvBuilder.cpp Resolved
Copilot AI balanced review requested due to automatic review settings December 3, 2026 13:51

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

No active review issues remain for the HLSL bitfield flat-conversion changes.

1 open issue

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 3, 2026 15:30
@dnovillo

Copy link
Copy Markdown
Collaborator Author

At this point, I think we can declare #7074 to apply to both backends

Done. I updated the description.

Should the storage elements or field elements determine whether a structure is castable one to another?

From what I understood reading the spec, it says that a bit-field converts as its underlying type (microsoft/hlsl-specs#743), and Clang does the same (llvm/llvm-project#161263). Rejecting these casts would break shaders that compile on DXIL today, so I went with the spec. The initializer list change is in HLSL 202y, which DXC doesn't support, so it wouldn't matter here.

That said, I defer to you and Sarah Spall (@spall) on this. If you'd rather reject them, I'll switch the PR over.

I was unsettled by how many additional places this adds conversions between booleans and integers to the SPIR-V backend

Yeah, the problem is that the SPIR-V backend doesn't use Clang CodeGen, so it has to do these conversions itself. bool bitfields need a uint container because the SPIR-V bitfield instructions only take integers. Casts and function returns create local ones, so Sema can't rule them out.

@dnovillo Diego Novillo (dnovillo) left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Gregory Roth (@pow2clk) thanks for the review! Could you run CI when you get a chance? I think I've addressed all your feedback. 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.

🟢 Approval recommended

The HLSL bitfield conversion changes have no active review issues.

No open issues 🎉

1 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: New

Development

Successfully merging this pull request may close these issues.

Bitfields generate bad code when casted

5 participants