Repository navigation
[SPIR-V] Fix component writes through .sample and evaluate texel accesses once - #8947
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.
Copilot review overview
🟡 Changes recommended
Two expression-evaluation bugs can produce incorrect coordinates, samples, and side effects.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Adds SPIR-V support for RWTexture2DMS and RWTexture2DMSArray.
Changes:
- Lowers writable multisampled textures to SPIR-V storage images with required capabilities.
- Supports sample-indexed reads, writes, queries, reflection, and sample-zero subscripting.
- Adds documentation and regression tests.
Required fixes:
- Cache coordinate and sample expressions during component/swizzle read-modify-write operations to avoid duplicate evaluation.
- Evaluate sample expressions before coordinate expressions on writes, matching source order and read behavior.
| File | Description |
|---|---|
tools/clang/test/CodeGenSPIRV/vk.subpass-input.hlsl |
Guards subpass-input capability behavior. |
tools/clang/test/CodeGenSPIRV/type.rwtexture2dmsarray.hlsl |
Tests arrayed type lowering and capabilities. |
tools/clang/test/CodeGenSPIRV/type.rwtexture2dms.hlsl |
Tests non-arrayed type lowering. |
tools/clang/test/CodeGenSPIRV/op.rwtexture2dms.ps.hlsl |
Tests validated pixel-shader output. |
tools/clang/test/CodeGenSPIRV/op.rwtexture2dms.access.hlsl |
Tests access forms and dimension queries. |
tools/clang/test/CodeGenSPIRV/decoration.user-type.hlsl |
Tests reflection metadata. |
tools/clang/lib/SPIRV/SpirvEmitter.cpp |
Implements multisampled access; contains the expression-evaluation issues above. |
tools/clang/lib/SPIRV/SpirvBuilder.cpp |
Adds sample operands to image writes. |
tools/clang/lib/SPIRV/LowerTypeVisitor.cpp |
Lowers writable multisampled texture types. |
tools/clang/lib/SPIRV/DeclResultIdMapper.cpp |
Classifies the resources as UAVs. |
tools/clang/lib/SPIRV/CapabilityVisitor.cpp |
Infers required SPIR-V capabilities. |
tools/clang/lib/SPIRV/AstTypeProbe.cpp |
Recognizes writable multisampled textures. |
tools/clang/include/clang/SPIRV/SpirvBuilder.h |
Updates the image-write API. |
tools/clang/include/clang/SPIRV/AstTypeProbe.h |
Declares the new type probe. |
docs/SPIR-V.rst |
Documents resource lowering and operations. |
docs/ReleaseNotes.md |
Announces the new support. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
303fdf4 to
833f319
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Critical reuse-scope flaws can evaluate side-effecting sample indices twice, incorrectly share default-argument coordinates, and produce invalid SPIR-V across conditional branches.
2 open issues
1 resolved since last review
Review effort: Balanced
|
Chris B (@llvm-beanz) PTAL. Could you run CI for me? Thanks. |
|
Gregory Roth (@pow2clk) could you review? It also needs a CI run. Thanks. |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Gregory Roth (pow2clk)
left a comment
There was a problem hiding this comment.
It all looks correct. I made a very large suggestion that I won't insist on, but wanted to make sure you thought about. I've asked a few questions and made some very small suggestions, but I don't think any of it is essential.
The unhelpful GitHub diffs on this made it a beast to review!
| if (!lhsPtr) | ||
| lhsPtr = doExpr(lhs, range); |
There was a problem hiding this comment.
I wanted to draw your attention to how repetition of LHS side effects is avoided in other cases. Including in the cases this change fixes, lhsPtr is passed in with the right instruction. The issue is that it isn't passed along to tryToAssignToRWBufferRWTexture above, so it can't use it. I think a more consistent approach would be to change that rather than cache it within the emitter, but if that is the approach taken, then perhaps this code should be updated to make use of it as well and retire the 1lhsPtr parameter in favor of the more general purpose cache.
There was a problem hiding this comment.
I wanted to draw your attention to how repetition of LHS side effects is avoided in other cases. Including in the cases this change fixes,
lhsPtris passed in with the right instruction.
For a buffer or texture element, lhsPtr is the loaded value, so it doesn't have the image, coordinate and sample that OpImageWrite needs. Am I missing a way to get them from it?
There was a problem hiding this comment.
It's an OpImageRead, which has the coordinate that was used to retrieve it. Though it may be that it could be other things. Again, I'm not insisting on anything, just suggesting another possibility in case it appealed to you.
There was a problem hiding this comment.
Ah, I see. That works for a float4 element, but:
- For narrower or
boolelements,lhsPtris an extract, shuffle or conversion of theOpImageRead. - The swizzle and
++paths never get anlhsPtr.
ISTM the cache is simpler, since it saves the operands before the read is emitted and doesn't depend on any of those IR shapes. But I'm happy to change it if you prefer the other way.
| @@ -0,0 +1,16 @@ | |||
| // RUN: %dxc -T cs_6_7 -E main -fcgl %s -spirv | FileCheck %s --implicit-check-not="OpCapability ImageMSArray" | |||
There was a problem hiding this comment.
I'm not sure why ImageMSArray is at risk of being included here. Was it mistakenly included at some point?
There was a problem hiding this comment.
I'm not sure why ImageMSArray is at risk of being included here. Was it mistakenly included at some point?
No. ImageMSArray is only for arrayed multisampled storage images. This test catches a change that adds it to every RWTexture2DMS.
There was a problem hiding this comment.
I get that, which is why I don't necessarily understand why ensuring that it isn't included as opposed to various other capabilities that might be incorrectly included. I realize they are somewhat related, but it seems just a bit arbitrary to fear that it might be mistakenly enabled unnecessarily
There was a problem hiding this comment.
I get that, which is why I don't necessarily understand why ensuring that it isn't included as opposed to various other capabilities that might be incorrectly included. I realize they are somewhat related, but it seems just a bit arbitrary to fear that it might be mistakenly enabled unnecessarily
Fair enough. Dropped it.
Diego Novillo (dnovillo)
left a comment
There was a problem hiding this comment.
Thanks for the review! PTAL.
| if (!lhsPtr) | ||
| lhsPtr = doExpr(lhs, range); |
There was a problem hiding this comment.
I wanted to draw your attention to how repetition of LHS side effects is avoided in other cases. Including in the cases this change fixes,
lhsPtris passed in with the right instruction.
For a buffer or texture element, lhsPtr is the loaded value, so it doesn't have the image, coordinate and sample that OpImageWrite needs. Am I missing a way to get them from it?
| @@ -0,0 +1,16 @@ | |||
| // RUN: %dxc -T cs_6_7 -E main -fcgl %s -spirv | FileCheck %s --implicit-check-not="OpCapability ImageMSArray" | |||
There was a problem hiding this comment.
I'm not sure why ImageMSArray is at risk of being included here. Was it mistakenly included at some point?
No. ImageMSArray is only for arrayed multisampled storage images. This test catches a change that adds it to every RWTexture2DMS.
Gregory Roth (pow2clk)
left a comment
There was a problem hiding this comment.
I think it makes sense to add some mips tests. I thought of that yesterday, but forgot to add it. Sorry.
Just as an FYI, nearly all of these tests fail with internal compiler errors or crashes when targeting DXIL. 😞
| if (!lhsPtr) | ||
| lhsPtr = doExpr(lhs, range); |
There was a problem hiding this comment.
It's an OpImageRead, which has the coordinate that was used to retrieve it. Though it may be that it could be other things. Again, I'm not insisting on anything, just suggesting another possibility in case it appealed to you.
| @@ -0,0 +1,16 @@ | |||
| // RUN: %dxc -T cs_6_7 -E main -fcgl %s -spirv | FileCheck %s --implicit-check-not="OpCapability ImageMSArray" | |||
There was a problem hiding this comment.
I get that, which is why I don't necessarily understand why ensuring that it isn't included as opposed to various other capabilities that might be incorrectly included. I realize they are somewhat related, but it seems just a bit arbitrary to fear that it might be mistakenly enabled unnecessarily
|
Thanks Diego! |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
- Reuse only the written texel access, including in *= scaling
77fdf15 to
14228b6
Compare
Diego Novillo (dnovillo)
left a comment
There was a problem hiding this comment.
Gregory Roth (@pow2clk) could you verify the CHECK patterns I added? I think they DTRT to verify that there is no double increment. The fix for Copilot's comment was simple enough otherwise.
Gregory Roth (pow2clk)
left a comment
There was a problem hiding this comment.
The CHECKs look good. I hate to give copilot another opportunity to nondeterministically point out something else, but I think an additional vector paren test is valuable.
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
Sarah Spall (@spall) could you review this? I think it affects code that you've worked on before. Thanks. |
Never mind. I meant #9025. Sorry for the confusion. |


Initially, this PR added
RWTexture2DMSandRWTexture2DMSArraysupport to the SPIR-V backend. Chris B (@llvm-beanz) has since added it in #8755 (thanks!), so this PR no longer needs to. Looking at #5244 again, I found two more problems:t.sample[s][coord].x = 9.0produces invalid SPIR-V, anOpStoreto the texel value, because one-component writes accepted onlyobj[coord].buf[i++] += 1reads elementiand writes elementi + 1. Swizzles, compound assignments and increments on anyRWBufferorRWTexturedo the same.This change:
.sample[idx][coord]in one-component writes.Tests:
op.rwtexture2dms.access.hlsl: every access form, plus component writes, a compound assignment and a*=scaling.op.rwtexture2dms.ps.hlsl: the shader from [SPIR-V][SM6.7] Add support for RWTexture2DMS in the SPIR-V backend #5244 plus a component write, compiled without-fcgl.binary-op.assign.image.default-arg.hlsl: that a default argument read in two calls is evaluated for each call.type.rwtexture2dms.capabilities.hlsl: thatRWTexture2DMSalone does not requireImageMSArray.type.rwtexture2dms.hlsl: the format of a signedRWTexture2DMSArray.decoration.user-type.hlsl: the-fspv-reflectstrings.vk.subpass-input.hlsl: thatSubpassInputMSdoes not requireStorageImageMultisample.binary-op.assign.image.hlsl: a swizzle write with side effects in the resource index, coordinate and a compound assignment to<scalar>.xwith an incremented index.I also had to update a few existing tests, because the generated code changed shape. In
unary-op.prefix-inc.hlsl,unary-op.prefix-dec.hlsl,unary-op.postfix-inc.hlslandunary-op.postfix-dec.hlsl, the write oftex[index]++now reuses the coordinate that the read loaded instead of loading it again.