Skip to content

[SPIR-V] Fix component writes through .sample and evaluate texel accesses once - #8947

Merged
Gregory Roth (pow2clk) merged 7 commits into
microsoft:mainfrom
dnovillo:fix-5244
Dec 2, 2026
Merged

Gregory Roth (pow2clk) merged 7 commits into
microsoft:mainfrom
dnovillo:fix-5244

Conversation

@dnovillo

@dnovillo Diego Novillo (dnovillo) commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

Initially, this PR added RWTexture2DMS and RWTexture2DMSArray support 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.0 produces invalid SPIR-V, an OpStore to the texel value, because one-component writes accepted only obj[coord].
  • A partial write reads the texel and writes it back, but evaluated its index for each, so buf[i++] += 1 reads element i and writes element i + 1. Swizzles, compound assignments and increments on any RWBuffer or RWTexture do the same.

This change:

  1. Accepts .sample[idx][coord] in one-component writes.
  2. Evaluates a texel's resource, coordinate and sample once, for both the read and the write.
  3. Evaluates the sample before the coordinate in writes, as reads do.

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: that RWTexture2DMS alone does not require ImageMSArray.
  • type.rwtexture2dms.hlsl: the format of a signed RWTexture2DMSArray.
  • decoration.user-type.hlsl: the -fspv-reflect strings.
  • vk.subpass-input.hlsl: that SubpassInputMS does not require StorageImageMultisample.
  • binary-op.assign.image.hlsl: a swizzle write with side effects in the resource index, coordinate and a compound assignment to <scalar>.x with 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.hlsl and unary-op.postfix-dec.hlsl, the write of tex[index]++ now reuses the coordinate that the read loaded instead of loading it again.

Copilot AI balanced review requested due to automatic review settings September 22, 2026 18:45
@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.

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 Medium severity

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.

Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved

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

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

Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
@dnovillo

Copy link
Copy Markdown
Collaborator Author

Chris B (@llvm-beanz) PTAL. Could you run CI for me? 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 are reported that would block approval.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@dnovillo Diego Novillo (dnovillo) changed the title [SPIR-V] Add support for RWTexture2DMS and RWTexture2DMSArray [SPIR-V] Fix component writes through .sample and evaluate texel accesses once Nov 18, 2026
@dnovillo

Copy link
Copy Markdown
Collaborator Author

Gregory Roth (@pow2clk) could you review? It also needs a CI run. Thanks.

@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) added the api Issues related to compiler library API label Nov 30, 2026

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

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!

Comment on lines 7986 to 7987
if (!lhsPtr)
lhsPtr = doExpr(lhs, range);

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

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.

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.

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?

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.

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.

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.

Ah, I see. That works for a float4 element, but:

  • For narrower or bool elements, lhsPtr is an extract, shuffle or conversion of the OpImageRead.
  • The swizzle and ++ paths never get an lhsPtr.

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.

Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Comment thread tools/clang/lib/SPIRV/SpirvEmitter.h Resolved
@@ -0,0 +1,16 @@
// RUN: %dxc -T cs_6_7 -E main -fcgl %s -spirv | FileCheck %s --implicit-check-not="OpCapability ImageMSArray"

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'm not sure why ImageMSArray is at risk of being included here. Was it mistakenly included at some point?

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.

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.

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

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.

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.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 21:01

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

Thanks for the review! PTAL.

Comment on lines 7986 to 7987
if (!lhsPtr)
lhsPtr = doExpr(lhs, range);

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.

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.

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?

Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved
@@ -0,0 +1,16 @@
// RUN: %dxc -T cs_6_7 -E main -fcgl %s -spirv | FileCheck %s --implicit-check-not="OpCapability ImageMSArray"

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.

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.

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

Human assessment is needed because scalar .x compound assignments on RWBuffer can still evaluate side-effecting indices twice and write to the wrong element.

No open issues 🎉

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.

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

Comment on lines 7986 to 7987
if (!lhsPtr)
lhsPtr = doExpr(lhs, range);

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.

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"

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

@pow2clk

Copy link
Copy Markdown
Collaborator

Thanks Diego!
/azp run

@pow2clk

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

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

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

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.

🟡 Changes recommended

A parenthesized scalar swizzle bypasses operand reuse, causing (buf[i++]).x += 1 to increment i twice and write to the wrong buffer element.

1 open issue

Review effort: Balanced

Comment thread tools/clang/lib/SPIRV/SpirvEmitter.cpp Resolved

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

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

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.

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 recorded for the SPIR-V texel-write and operand-reuse changes.

No open issues 🎉

1 resolved since last review

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.

Thanks!

@pow2clk
Gregory Roth (pow2clk) enabled auto-merge (squash) December 2, 2026 18:57
@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) could you review this? I think it affects code that you've worked on before. Thanks.

@dnovillo

Copy link
Copy Markdown
Collaborator Author

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.

@pow2clk
Gregory Roth (pow2clk) merged commit 2c91234 into microsoft:main Dec 2, 2026
13 checks passed
@github-project-automation github-project-automation Bot moved this from New to Done in HLSL Roadmap Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api Issues related to compiler library API

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants