Skip to content

Diagnose non-bool SV_CullPrimitive mesh outputs in Sema - #9073

Open
Abir Deol (apollo-2006) wants to merge 12 commits into
microsoft:mainfrom
apollo-2006:spirv-cullprimitive-bool
Open

Abir Deol (apollo-2006) wants to merge 12 commits into
microsoft:mainfrom
apollo-2006:spirv-cullprimitive-bool

Conversation

@apollo-2006

@apollo-2006 Abir Deol (apollo-2006) commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

A non-bool SV_CullPrimitive on a mesh shader primitive output used to crash SPIR-V codegen with the "please file a bug report" error. DXIL only caught it at validation. It's now a Sema error for both:

error: SV_CullPrimitive must be applied to a scalar bool, but 'cull' has type 'uint'

Following Gregory Roth (@pow2clk)'s suggestion, the check runs in DiagnoseMeshEntry on out primitives params (nested structs and arrays included). bool1 and bool1x1 count as scalar bools, and pixel shader inputs are left alone since DXIL replaces them with 0.

Tests:

  • SemaHLSL/cullprimitive-type.hlsl covers the rejected and accepted types (fails without the change)
  • meshshading.ext.cullprimitive.bool1.hlsl checks that bool1 still lowers to the builtin
  • meshshading.ext.triangle.mesh.hlsl used int (it only passed because -fcgl skips validation), so it's bool now

Happy to change anything :)

The DXIL path rejects a non-bool SV_CullPrimitive with "SV_CullPrimitive
must be bool.", but the SPIR-V backend passed the declared type through to
the CullPrimitiveEXT builtin, so spirv-val failed with
VUID-CullPrimitiveEXT-CullPrimitiveEXT-07036 and DXC asked the user to file
a bug.

Emit an error for a non-bool SV_CullPrimitive, and make
meshshading.ext.triangle.mesh.hlsl declare it as bool. That test only passed
with an int because it runs with -fcgl.
Copilot AI balanced review requested due to automatic review settings October 27, 2026 15:16
@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.

🟡 Changes recommended

The new check rejects valid scalar-equivalent bool1 and bool1x1 declarations that already lower to SPIR-V boolean scalars.

1 open issue
What changed in this PR

This PR adds an early SPIR-V diagnostic for non-boolean SV_CullPrimitive declarations, replacing a fatal validation failure with a normal compiler error. The related NV mesh-shader limitations remain outside its scope.

Changes:

  • Validate the semantic’s element type before stage-specific handling.
  • Add a negative regression test and correct the existing mesh test to use bool.
  • Document the behavior change in the upcoming release notes.
File Description
tools/​clang/​test/​CodeGenSPIRV/​meshshading.ext.triangle.mesh.hlsl Use boolean cull values and update SPIR-V checks.
tools/​clang/​test/​CodeGenSPIRV/​meshshading.ext.cullprimitive.non-bool.hlsl Test the diagnostic for a uint cull declaration.
tools/​clang/​lib/​SPIRV/​DeclResultIdMapper.cpp Check SV_CullPrimitive types before creating stage variables.
docs/​ReleaseNotes.md Document the new diagnostic.

Review effort: Balanced

@damyanp

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

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

int layer : SV_RenderTargetArrayIndex; // -> Builtin Layer
int vpIdx : SV_ViewportArrayIndex; // -> Builtin ViewportIndex
int cullPrim : SV_CullPrimitive; // -> BUiltin CullPrimitiveEXT
bool cullPrim : SV_CullPrimitive; // -> BUiltin CullPrimitiveEXT

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.

super nit:

Suggested change
bool cullPrim : SV_CullPrimitive; // -> BUiltin CullPrimitiveEXT
bool cullPrim : SV_CullPrimitive; // -> Builtin CullPrimitiveEXT

case hlsl::Semantic::Kind::CullPrimitive: {
QualType elemType = type;
while (const auto *arrayType = astContext.getAsConstantArrayType(elemType))
elemType = arrayType->getElementType();

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 tried this code on aggregate types, and it looks like it doesn't quite handle that case.

From what I see, this fails to catch bool cull[2]: DXIL gives the error "rows for system value semantic SV_CullPrimitive must be 1" meanwhile SPIR-V gives "invalid SPIR-V, please file a bug"

If you could fix this and add a test, that be great.

// be used as MSOut.
case hlsl::Semantic::Kind::CullPrimitive: {
QualType elemType = type;
while (const auto *arrayType = astContext.getAsConstantArrayType(elemType))

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 bool check should apply only to MSPOut, I'll make a longer explanation below

@github-project-automation github-project-automation Bot moved this from New to In progress in HLSL Roadmap Oct 28, 2026
@jzakharovnv

Copy link
Copy Markdown
Collaborator

One interesting side effect of the current implementation is that it rejects uint SV_CullPrimitive on PS inputs, which is not what DXIL does.

Per hctdb.py and docs/DXIL.rst, CullPrimitive has the interpretations: PSIn = NotInSig, MSPOut = NotPacked, and NA elsewhere. NotInSig means the element isn't in the signature and is accessed through an intrinsic. For CullPrimitive specifically, HLSignatureLower.cpp replaces all uses of CullPrimitive with constant 0 regardless of the type. This all happens before DxilValidation.cpp so the bool check never actually get enforced in this case (hence why CullPrimitive is allowed to be uint in PSin).

However, the SPIRV backend doesn't implement this CullPrimitive PSIn replaced with constant 0 stuff, since to use CullPrimitiveEXT requires MeshShadingEXT which is illegal in fragment shaders per VUID 07034.

So, the 2 options are to mirror DXIL's replacement with constant 0 or conform to VUID 07034, but that is an issue out of scope for this PR. What do you think Diego Novillo (@dnovillo).

Suffice to say though Abir Deol (@apollo-2006), if you could just keep the bool check to only apply to MSPOut, that'd be great, thanks!

DXIL replaces SV_CullPrimitive on pixel shader inputs with a constant 0, so only MSPOut has to be bool. Check the type before the per-primitive array is added, so a bool[2] is rejected like DXIL does, and accept bool1 and bool1x1.
Copilot AI balanced review requested due to automatic review settings October 29, 2026 01:02
@apollo-2006

Abir Deol (apollo-2006) commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks a lot for this (and for the PSIn explanation, that cleared up what I was missing). Here's what changed:

  • MSPOut only now, so PS inputs are back to the old behavior (I left the 07034 question for its own discussion).
  • bool cull[2]: good catch, I missed this completely 😞 . The old check was stripping the field's own array along with the per-primitive one. It now checks the type before that array is added, so arrays are rejected like in DXIL (new test: cullprimitive.array.hlsl).
  • bool1 / bool1x1: accepted now through isScalarType() (new test: cullprimitive.bool1.hlsl).
  • Alignment nit: applied your suggestion (it fixed the "BUiltin" typo too).

The mesh output cases match DXIL now, and tests and clang-format are clean. Thanks again for making it so easy to fix lol

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 change that replaces fatal validation failures for invalid SV_CullPrimitive types with normal compiler errors.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@jzakharovnv

Copy link
Copy Markdown
Collaborator

Great! If you could add a bool2 negative test just to make certain aggregates don't get accepted, then it'd LGTM. Thanks!

@dnovillo

Copy link
Copy Markdown
Collaborator

So, the 2 options are to mirror DXIL's replacement with constant 0 or conform to VUID 07034, but that is an issue out of scope for this PR. What do you think Diego Novillo (Diego Novillo (@dnovillo)).

Agreed. I would mirror DXIL and replace it with false. It doesn't make sense for the SPIR-V backend to error out here since DXIL accepts it and we can do the obvious thing. Abir Deol (@apollo-2006), could you file an issue for it?

@dnovillo Diego Novillo (dnovillo) 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 for this fix. I agree with Jonathan Zakharov (@jzakharovnv) about negative tests for bool2.

Chris B (@llvm-beanz) or Gregory Roth (@pow2clk) or Damyan Pepper (@damyanp) could you please approve the CI bot? We still don't have maintainer access to trigger those. Thanks.

Comment on lines +3614 to +3623
if (stageVarData.semantic->getKind() == hlsl::Semantic::Kind::CullPrimitive &&
stageVarData.sigPoint->GetKind() == hlsl::SigPoint::Kind::MSPOut) {
QualType scalarType;
if (!isScalarType(stageVarData.type, &scalarType) ||
!scalarType->isBooleanType()) {
emitError("semantic CullPrimitive must be a bool scalar",
stageVarData.semantic->loc);
return nullptr;
}
}

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.

nit: Move this to validateShaderStageVarType? Next to SV_InnerCoverage so we do semantic type checks in one place.

…arType

Keeps the semantic type checks together, next to SV_InnerCoverage. Adds a bool2 negative test.
@apollo-2006

Copy link
Copy Markdown
Contributor Author

Moved the check into validateShaderStageVarType next to SV_InnerCoverage, and added a bool2 negative test (cullprimitive.vector.hlsl). Filed #9084 for the PSIn side. Thanks both :)

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 were identified for the mesh-output SV_CullPrimitive type-validation change.

No open issues 🎉

Review effort: Balanced

@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 30, 2026 19:56
@apollo-2006

Copy link
Copy Markdown
Contributor Author

Thanks for digging into this one, the incomplete struct crash was a really good find. The check was walking into structs that weren't defined yet, so it stops there now (Sema already reports the incomplete type). There are tests for a forward-declared struct, a never-defined one, an incomplete field, and SV_CullPrimitive directly on an incomplete param.

I went with deduping, so each error shows up once even when entry points share a struct. Inner in cullprimitive-type.hlsl is used by two entry points to pin that.

I added the tests you listed too: bool1 on the param, bool1x2, SV_CullPrimitive1, lowercase sv_cullprimitive, an enum and an array of structs. The -T ms_6_5 -E main one is its own file, cullprimitive-type-entry.hlsl.

And yep, skipping PS inputs and the vertices struct is on purpose. Both backends already reject SV_CullPrimitive on vertex outputs whatever the type, and PS inputs just get replaced with 0, so only primitive outputs get the check. I left a comment above it explaining that.

Moved the diag next to err_hlsl_load_from_mesh_out_arrays and matched the formatting. I also fixed the struct-wrapped bool case Copilot raised; more on that in its thread.

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 unpublished findings identify a critical infinite-recursion risk and premature rejection of dependent template types.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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.

LGTM!

@pow2clk

Copy link
Copy Markdown
Collaborator

/azp run

@pow2clk
Gregory Roth (pow2clk) enabled auto-merge (squash) November 17, 2026 12:52
@azure-pipelines

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

@pow2clk

Copy link
Copy Markdown
Collaborator

Something is up with the clang-format action. It fails to install clangformat. I've checked the formatting and it's consistent. Merging anyway.

@pow2clk

Copy link
Copy Markdown
Collaborator

Hmm. Seems I can't override that anymore.

@pow2clk

Copy link
Copy Markdown
Collaborator

I did some experimentation with the clang-format version here: #9136 It manages to install clang-format even with no actual changes. This didn't change anything about the way that check works. I really don't know why it's failing Damyan Pepper (@damyanp), do you have any insight here?

@pow2clk

Gregory Roth (pow2clk) commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Okay. I see that #9117 updated the version, but this run is still using the old version somehow. Actions are weird like that sometimes. Ashley Coleman (@V-FEXrt), could you suggest how to proceed? Can you override the failure? Does Abir Deol (@apollo-2006) need to resubmit this? Maybe rebasing would help?

ETA: The log shows this is still trying to install clang-format version 17.0.1, which isn't found, but newer runs are installing 17.0.6, which is.

@V-FEXrt

Ashley Coleman (V-FEXrt) commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Gregory Roth (@pow2clk) is #9117 in the base of this branch? Based on the age of the PR I'm assuming no and if so the solution should just be to rebase (there is a merge conflict as well that should be fixed along with the rebase)

Copilot AI balanced review requested due to automatic review settings November 17, 2026 18:02

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 new traversal can recurse indefinitely on invalid self-referential records and prematurely rejects dependent mesh-template types before substitution.

2 open issues

Review effort: Balanced

Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Resolved
Comment thread tools/clang/lib/Sema/SemaHLSL.cpp Resolved
@apollo-2006

Copy link
Copy Markdown
Contributor Author

Thanks for digging into the format check, both of you. You were right, #9117 wasn't in my branch, so I merged main in (the only conflict was the two new release note bullets landing on the same line, kept both). The new run should pick up 17.0.6 now :)

Copilot AI balanced review requested due to automatic review settings November 17, 2026 19:58

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 a minor release-note guideline concern was omitted from the published review.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 18, 2026 19:06

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 were identified for the change moving SV_CullPrimitive type checking into Sema.

No open issues 🎉

Review effort: Balanced

@pow2clk

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

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

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

None yet

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

7 participants