Repository navigation
Fix bitfields in flat conversions on SPIR-V and DXIL - #9025
Diego Novillo (dnovillo) wants to merge 5 commits into
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
🟢 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.
|
Gregory Roth (@pow2clk) this came out of #8715, could you take a look? Thanks |
|
/azp run |
|
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. |
|
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? |
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. |
5235896 to
3b10f53
Compare
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. |
ce59af5 to
2cedf68
Compare
|
Rebased to solve conflicts. Chris B (@llvm-beanz) could you please run CI and take a second look at this? Thanks. |
Diego Novillo (dnovillo)
left a comment
There was a problem hiding this comment.
Gregory Roth (@pow2clk) ping for review. Thanks.
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Sarah Spall (@spall) would you be able to review this PR? Particularly, the DXIL bits. Thanks. |
25d4a35 to
36ee39b
Compare
There was a problem hiding this comment.
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.
36ee39b to
ca39399
Compare
Done. I updated the description.
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.
Yeah, the problem is that the SPIR-V backend doesn't use Clang CodeGen, so it has to do these conversions itself. |
Diego Novillo (dnovillo)
left a comment
There was a problem hiding this comment.
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.


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; }andBits s = {1, 2, 3}:(uint3)sgives (131073, 131073, 3) on both DXIL and SPIR-V.sto a struct of the same shape fails validation withIndex is out of bounds, becauseBitshas 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:
boolbitfields use auintcontainer 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:boolbitfields in buffers andcbuffers.cast.flat-conversion.local-boolean-bitfield.hlsl:boolbitfields 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