[clang] 50f170b - [HLSL] Add support for SV_CullDistance (#247321)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 09:52:06 PST 2026
Author: Finn Plummer
Date: 2026-12-04T17:51:57Z
New Revision: 50f170b75e5046559819b638cb31fc2184affe08
URL: https://github.com/llvm/llvm-project/commit/50f170b75e5046559819b638cb31fc2184affe08
DIFF: https://github.com/llvm/llvm-project/commit/50f170b75e5046559819b638cb31fc2184affe08.diff
LOG: [HLSL] Add support for SV_CullDistance (#247321)
Note for SPIR-V: as in DXC, all cull distances of an input or output are
combined into one CullDistance built-in float array. Each semantic index
starts at an offset into the array, in increasing semantic index order.
This is what is responsible for the larger code change in
`CGHLSLRuntime`. Please see `CGHLSLRuntime.h` first for an overview of
what happens here.
The CullDistance capability is also made available for Vulkan as
required.
Resolves: https://github.com/llvm/llvm-project/issues/136973
Assisted by: Claude Opus 5.5 and Gemini Neon
Added:
clang/test/CodeGenHLSL/semantics/SV_CullDistance-half.hlsl
clang/test/CodeGenHLSL/semantics/SV_CullDistance-layout.hlsl
clang/test/CodeGenHLSL/semantics/SV_CullDistance.hlsl
clang/test/SemaHLSL/Semantics/culldistance.stages.hlsl
clang/test/SemaHLSL/Semantics/culldistance.vs-ps.hlsl
llvm/test/CodeGen/DirectX/ContainerData/SemanticSignatures-cull-distance.ll
llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-cull-distance-errors.ll
llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-cull-distance.ll
llvm/test/CodeGen/SPIRV/semantics/cull-distance.ll
Modified:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/CodeGen/CGHLSLRuntime.cpp
clang/lib/CodeGen/CGHLSLRuntime.h
clang/lib/Sema/SemaHLSL.cpp
llvm/include/llvm/Frontend/HLSL/SemanticSignatures.h
llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp
llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
llvm/unittests/Frontend/HLSLSemanticStagesTest.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index df55b87a4394fb..a4202149f85477 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -13982,7 +13982,8 @@ def err_hlsl_semantic_invalid_type
"%select{scalar|scalar or vector of up to %2 components|vector of %2 components}1 of "
"%select{16 or 32 bit integer|32 bit integer|"
"16 or 32 bit floating-point|16 or 32 bit unsigned integer|"
- "32 bit unsigned integer|bool or 32 bit integer}3 type "
+ "32 bit unsigned integer|bool or 32 bit integer|"
+ "32 bit floating-point}3 type "
"(was %4)">;
def err_hlsl_init_priority_unsupported : Error<
"initializer priorities are not supported in HLSL">;
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index db5a3b5d49cf45..5cce83278301fa 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -1415,6 +1415,8 @@ struct SemanticShape {
: MatrixRow * Cols + Col;
}
+ unsigned getNumCols() const { return Cols; }
+
unsigned getNumRows() const {
unsigned Rows = 1;
for (unsigned Dimension : Dimensions)
@@ -1469,6 +1471,179 @@ static SemanticShape getSemanticShape(ASTContext &Ctx, QualType Ty) {
return Shape;
}
+// Calls Fn on each semantic leaf of Decl, with the semantic attribute applied
+// to it. The leaves are visited in the order the semantic load and store
+// traversals consume the applied semantic attributes.
+static void
+forEachSemanticLeaf(const clang::DeclaratorDecl *Decl,
+ specific_attr_iterator<HLSLAppliedSemanticAttr> &It,
+ specific_attr_iterator<HLSLAppliedSemanticAttr> End,
+ llvm::function_ref<void(const clang::DeclaratorDecl *,
+ HLSLAppliedSemanticAttr *)>
+ Fn) {
+ if (const RecordDecl *RD = getSemanticLeafType(Decl)->getAsRecordDecl()) {
+ for (const FieldDecl *Field : RD->fields())
+ forEachSemanticLeaf(Field, It, End, Fn);
+ return;
+ }
+ assert(It != End && "semantic leaf has no applied semantic");
+ Fn(Decl, *It);
+ ++It;
+}
+
+CGHLSLRuntime::SPIRVClipCullLayout &
+CGHLSLRuntime::getSPIRVClipCullLayout(llvm::dxbc::PSV::SemanticKind Kind,
+ bool IsInput) {
+ bool IsCull = Kind == llvm::dxbc::PSV::SemanticKind::CullDistance;
+ assert(IsCull && "FIXME: SV_ClipDistance is not yet implemented");
+ return SPIRVClipCullLayouts[IsInput][IsCull];
+}
+
+void CGHLSLRuntime::computeSPIRVClipCullLayouts(const FunctionDecl *FD) {
+ ASTContext &Ctx = CGM.getContext();
+ llvm::Triple::EnvironmentType Stage =
+ FD->getAttr<HLSLShaderAttr>()->getType();
+ // Each semantic index is assigned the number of floats of its declaration,
+ // indexed like SPIRVClipCullLayouts. Sorted by semantic index before
+ // computing offsets so offsets follow semantic index order.
+ llvm::SmallVector<std::pair<unsigned, unsigned>> Counts[2][2];
+ auto Collect = [&](const clang::DeclaratorDecl *D, llvm::hlsl::IOType IO) {
+ auto It = D->specific_attr_begin<HLSLAppliedSemanticAttr>();
+ auto End = D->specific_attr_end<HLSLAppliedSemanticAttr>();
+ if (It == End)
+ return;
+ forEachSemanticLeaf(
+ D, It, End,
+ [&](const clang::DeclaratorDecl *Leaf,
+ HLSLAppliedSemanticAttr *Semantic) {
+ if (Leaf->hasAttr<HLSLVkBuiltInAttr>())
+ return;
+ auto Kind =
+ llvm::hlsl::getSemanticKind(Semantic->getAttrName()->getName());
+ if (Kind != llvm::dxbc::PSV::SemanticKind::CullDistance ||
+ llvm::hlsl::getInterpretationKind(Kind, Stage, IO) !=
+ llvm::hlsl::SemanticInterpretation::ClipCull)
+ return;
+ SemanticShape Shape =
+ getSemanticShape(Ctx, getSemanticLeafType(Leaf));
+ bool IsInput = IO == llvm::hlsl::IOType::In;
+ bool IsCull = Kind == llvm::dxbc::PSV::SemanticKind::CullDistance;
+ Counts[IsInput][IsCull].emplace_back(Semantic->getSemanticIndex(),
+ Shape.getNumRows() *
+ Shape.getNumCols());
+ });
+ };
+
+ for (const ParmVarDecl *PD : FD->parameters())
+ Collect(PD, llvm::hlsl::IOType::In);
+ Collect(FD, llvm::hlsl::IOType::Out);
+
+ for (bool IsInput : {false, true}) {
+ for (bool IsCull : {false, true}) {
+ auto &EntryCounts = Counts[IsInput][IsCull];
+ llvm::sort(EntryCounts);
+ SPIRVClipCullLayout &Layout = SPIRVClipCullLayouts[IsInput][IsCull];
+ Layout = SPIRVClipCullLayout();
+ for (auto [Index, Count] : EntryCounts) {
+ [[maybe_unused]] bool Inserted =
+ Layout.Offsets.try_emplace(Index, Layout.Size).second;
+ assert(Inserted && "overlapping semantic indices should have been "
+ "diagnosed by Sema");
+ Layout.Size += Count;
+ }
+ }
+ }
+}
+
+llvm::GlobalVariable *
+CGHLSLRuntime::getSPIRVClipCullVariable(llvm::dxbc::PSV::SemanticKind Kind,
+ bool IsInput) {
+ SPIRVClipCullLayout &Layout = getSPIRVClipCullLayout(Kind, IsInput);
+ assert(Layout.Size && "semantic is missing from the clip/cull layouts");
+ if (Layout.Variable)
+ return Layout.Variable;
+
+ // The built-in is an array of 32-bit floats (see
+ // VUID-CullDistance-CullDistance-04200).
+ llvm::Type *Ty = llvm::ArrayType::get(
+ llvm::Type::getFloatTy(CGM.getLLVMContext()), Layout.Size);
+ unsigned AddrSpace = CGM.getContext().getTargetAddressSpace(
+ IsInput ? LangAS::hlsl_input : LangAS::hlsl_output);
+ auto *GV = new llvm::GlobalVariable(
+ CGM.getModule(), Ty, /* isConstant= */ IsInput,
+ llvm::GlobalValue::ExternalLinkage, /* Initializer= */ nullptr,
+ "SV_CullDistance", /* insertBefore= */ nullptr,
+ llvm::GlobalVariable::GeneralDynamicTLSModel, AddrSpace,
+ /* isExternallyInitialized= */ IsInput);
+ GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
+ addSPIRVInterfaceDecorations(
+ GV, llvm::hlsl::SPIRVInterfaceDecoration::BuiltIn,
+ static_cast<unsigned>(llvm::hlsl::SPIRVBuiltIn::CullDistance));
+ Layout.Variable = GV;
+ return GV;
+}
+
+// The semantic is read from consecutive floats of the built-in array, one row
+// of its shape after another, starting at the offset of its semantic index.
+llvm::Value *
+CGHLSLRuntime::emitSPIRVClipCullLoad(IRBuilder<> &B, llvm::Type *Type,
+ const clang::DeclaratorDecl *Decl,
+ HLSLAppliedSemanticAttr *Semantic) {
+ SemanticShape Shape =
+ getSemanticShape(CGM.getContext(), getSemanticLeafType(Decl));
+ assert(!Shape.IsMatrix && "clip and cull distances cannot be matrices");
+ auto Kind = llvm::hlsl::getSemanticKind(Semantic->getAttrName()->getName());
+ llvm::GlobalVariable *GV =
+ getSPIRVClipCullVariable(Kind, /* IsInput= */ true);
+ unsigned Offset = getSPIRVClipCullLayout(Kind, /* IsInput= */ true)
+ .Offsets.at(Semantic->getSemanticIndex());
+ llvm::Type *RowTy = CGM.getTypes().ConvertType(Shape.RowType);
+ llvm::Value *Result = llvm::PoisonValue::get(Type);
+ for (unsigned Row = 0, E = Shape.getNumRows(); Row < E; ++Row) {
+ llvm::Value *RowValue = llvm::PoisonValue::get(RowTy);
+ for (unsigned Col = 0; Col < Shape.Cols; ++Col) {
+ llvm::Value *Ptr =
+ B.CreateStructuredGEP(GV->getValueType(), GV, {B.getInt32(Offset++)});
+ llvm::Value *Element = B.CreateLoad(B.getFloatTy(), Ptr);
+ RowValue = RowTy->isVectorTy()
+ ? B.CreateInsertElement(RowValue, Element, Col)
+ : Element;
+ }
+ SmallVector<unsigned> Indices = Shape.getArrayIndicesForRow(Row);
+ Result = Indices.empty() ? RowValue
+ : B.CreateInsertValue(Result, RowValue, Indices);
+ }
+ return Result;
+}
+
+// The semantic is written to consecutive floats of the built-in array, as in
+// emitSPIRVClipCullLoad.
+void CGHLSLRuntime::emitSPIRVClipCullStore(IRBuilder<> &B, llvm::Value *Source,
+ const clang::DeclaratorDecl *Decl,
+ HLSLAppliedSemanticAttr *Semantic) {
+ SemanticShape Shape =
+ getSemanticShape(CGM.getContext(), getSemanticLeafType(Decl));
+ assert(!Shape.IsMatrix && "clip and cull distances cannot be matrices");
+ auto Kind = llvm::hlsl::getSemanticKind(Semantic->getAttrName()->getName());
+ llvm::GlobalVariable *GV =
+ getSPIRVClipCullVariable(Kind, /* IsInput= */ false);
+ unsigned Offset = getSPIRVClipCullLayout(Kind, /* IsInput= */ false)
+ .Offsets.at(Semantic->getSemanticIndex());
+ for (unsigned Row = 0, E = Shape.getNumRows(); Row < E; ++Row) {
+ SmallVector<unsigned> Indices = Shape.getArrayIndicesForRow(Row);
+ llvm::Value *RowValue =
+ Indices.empty() ? Source : B.CreateExtractValue(Source, Indices);
+ for (unsigned Col = 0; Col < Shape.Cols; ++Col) {
+ llvm::Value *Element = RowValue->getType()->isVectorTy()
+ ? B.CreateExtractElement(RowValue, Col)
+ : RowValue;
+ llvm::Value *Ptr =
+ B.CreateStructuredGEP(GV->getValueType(), GV, {B.getInt32(Offset++)});
+ B.CreateStore(Element, Ptr);
+ }
+ }
+}
+
static llvm::dxil::ElementType getSignatureComponentType(CodeGenModule &CGM,
QualType Ty) {
if (const auto *VT = Ty->getAs<clang::VectorType>())
@@ -1906,6 +2081,21 @@ llvm::Value *CGHLSLRuntime::emitSystemSemanticLoad(
return emitDXILUserSemanticLoad(B, Type, Decl, Semantic, Index,
Signature);
break;
+ case llvm::dxbc::PSV::SemanticKind::CullDistance:
+ assert(llvm::is_contained({llvm::Triple::Hull, llvm::Triple::Domain,
+ llvm::Triple::Geometry, llvm::Triple::Pixel},
+ Stage) &&
+ "SV_CullDistance is not available in this stage and should have "
+ "been diagnosed by Sema");
+ assert(Stage == llvm::Triple::Pixel &&
+ "FIXME: loading SV_CullDistance is not yet implemented for this "
+ "shader stage");
+ if (CGM.getTarget().getTriple().isSPIRV())
+ return emitSPIRVClipCullLoad(B, Type, Decl, Semantic);
+ if (CGM.getTarget().getTriple().isDXIL())
+ return emitDXILUserSemanticLoad(B, Type, Decl, Semantic, Index,
+ Signature);
+ break;
case llvm::dxbc::PSV::SemanticKind::PrimitiveID:
assert(Stage == llvm::Triple::Pixel &&
"SV_PrimitiveID is in an unavailable stage and should have been "
@@ -2110,6 +2300,25 @@ void CGHLSLRuntime::emitSystemSemanticStore(
return;
}
break;
+ case llvm::dxbc::PSV::SemanticKind::CullDistance:
+ assert(llvm::is_contained({llvm::Triple::Vertex, llvm::Triple::Hull,
+ llvm::Triple::Domain, llvm::Triple::Geometry,
+ llvm::Triple::Mesh},
+ Stage) &&
+ "SV_CullDistance is not available in this stage and should have "
+ "been diagnosed by Sema");
+ assert(Stage == llvm::Triple::Vertex &&
+ "FIXME: storing SV_CullDistance is not yet implemented for this "
+ "shader stage");
+ if (CGM.getTarget().getTriple().isDXIL()) {
+ emitDXILUserSemanticStore(B, Source, Decl, Semantic, Index, Signature);
+ return;
+ }
+ if (CGM.getTarget().getTriple().isSPIRV()) {
+ emitSPIRVClipCullStore(B, Source, Decl, Semantic);
+ return;
+ }
+ break;
case llvm::dxbc::PSV::SemanticKind::StencilRef:
assert(Stage == llvm::Triple::Pixel &&
"SV_StencilRef is in an unavailable stage and should have been "
@@ -2455,6 +2664,9 @@ void CGHLSLRuntime::emitEntryFunction(const FunctionDecl *FD,
SmallVector<std::pair<llvm::Value *, llvm::Type *>> OutputSemantic;
+ if (CGM.getTarget().getTriple().isSPIRV())
+ computeSPIRVClipCullLayouts(FD);
+
unsigned SRetOffset = 0;
for (const auto &Param : Fn->args()) {
if (Param.hasStructRetAttr()) {
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h
index e21668a632eec6..99bc98798d0f0d 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -424,6 +424,29 @@ class CGHLSLRuntime {
llvm::DenseMap<const clang::RecordType *, llvm::StructType *> LayoutTypes;
unsigned SPIRVLastAssignedInputSemanticLocation = 0;
unsigned SPIRVLastAssignedOutputSemanticLocation = 0;
+
+ // SPIR-V combines all clip or cull distance semantics of one kind in an entry
+ // point input or output into one built-in float array. Each semantic index
+ // starts at an offset into that array, in increasing semantic index order.
+ struct SPIRVClipCullLayout {
+ llvm::DenseMap<unsigned, unsigned> Offsets;
+ unsigned Size = 0;
+ llvm::GlobalVariable *Variable = nullptr;
+ };
+ // Indexed by [IsInput][IsCull].
+ SPIRVClipCullLayout SPIRVClipCullLayouts[2][2];
+
+ SPIRVClipCullLayout &
+ getSPIRVClipCullLayout(llvm::dxbc::PSV::SemanticKind Kind, bool IsInput);
+ void computeSPIRVClipCullLayouts(const FunctionDecl *FD);
+ llvm::GlobalVariable *
+ getSPIRVClipCullVariable(llvm::dxbc::PSV::SemanticKind Kind, bool IsInput);
+ llvm::Value *emitSPIRVClipCullLoad(llvm::IRBuilder<> &B, llvm::Type *Type,
+ const clang::DeclaratorDecl *Decl,
+ HLSLAppliedSemanticAttr *Semantic);
+ void emitSPIRVClipCullStore(llvm::IRBuilder<> &B, llvm::Value *Source,
+ const clang::DeclaratorDecl *Decl,
+ HLSLAppliedSemanticAttr *Semantic);
};
} // namespace CodeGen
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 4c28eaa0fe50fc..d13e217d38f073 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1042,6 +1042,11 @@ static bool isVkPipelineBuiltin(const ASTContext &AstContext, FunctionDecl *FD,
return ST == llvm::Triple::Pixel;
case SemanticKind::InnerCoverage:
return ST == llvm::Triple::Pixel && IsInput;
+ case SemanticKind::CullDistance:
+ // All cull distances of a vertex output or pixel input share one
+ // CullDistance built-in array.
+ return (ST == llvm::Triple::Vertex && !IsInput) ||
+ (ST == llvm::Triple::Pixel && IsInput);
default:
return false;
}
@@ -1539,6 +1544,22 @@ void SemaHLSL::diagnoseSemanticType(const Decl *D,
<< A->getAttrName() << /* scalar or vector of up to */ 1 << 4
<< /* 16 or 32 bit floating-point */ 2 << DeclaredTy;
return;
+ case SemanticKind::CullDistance: {
+ // DXIL packs the elements into at most two signature rows and SPIR-V
+ // flattens them into a single float array, so arrays of scalars and vectors
+ // are allowed. DXIL accepts 16- or 32-bit floats. SPIR-V requires 32-bit
+ // floats (see VUID-CullDistance-CullDistance-04200), and half is rejected
+ // even when it is a 32-bit float so that its meaning does not depend on
+ // -fnative-half-type.
+ bool IsValidFloat =
+ IsSPIRV ? ElemTy->isFloat32Type() : isFloatOrHalfElement(ElemTy);
+ if (!IsValidFloat || Components > 4)
+ Diag(A->getLoc(), diag::err_hlsl_semantic_invalid_type)
+ << A->getAttrName() << /* scalar or vector of up to */ 1 << 4
+ << /* 32 bit floating-point / 16 or 32 bit floating-point */
+ (IsSPIRV ? 6 : 2) << DeclaredTy;
+ return;
+ }
case SemanticKind::SampleIndex:
case SemanticKind::PrimitiveID:
case SemanticKind::StencilRef:
@@ -2413,7 +2434,6 @@ void SemaHLSL::handleSemanticAttr(Decl *D, const ParsedAttr &AL) {
switch (Kind) {
// FIXME: These semantics do not yet have CodeGen support.
case SemanticKind::ClipDistance:
- case SemanticKind::CullDistance:
case SemanticKind::OutputControlPointID:
case SemanticKind::DomainLocation:
case SemanticKind::GSInstanceID:
diff --git a/clang/test/CodeGenHLSL/semantics/SV_CullDistance-half.hlsl b/clang/test/CodeGenHLSL/semantics/SV_CullDistance-half.hlsl
new file mode 100644
index 00000000000000..9b04a86df5301f
--- /dev/null
+++ b/clang/test/CodeGenHLSL/semantics/SV_CullDistance-half.hlsl
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefixes=CHECK,CHECK-F32
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefixes=CHECK,CHECK-F16
+
+// DXIL accepts half for SV_CullDistance. Without native 16-bit types it is a
+// 32-bit float (CompType 9), and with them it is a 16-bit float (CompType 8).
+// SPIR-V rejects half, which is covered by
+// clang/test/SemaHLSL/Semantics/culldistance.vs-ps.hlsl.
+
+struct VSOut {
+ float4 Pos : SV_Position;
+ half2 C : SV_CullDistance;
+};
+[shader("vertex")]
+VSOut vs_main(float4 In : A) {
+ VSOut O;
+ O.Pos = In;
+ O.C = (half2)In.xy;
+ return O;
+}
+[shader("pixel")]
+float4 ps_main(float4 Pos : SV_Position,
+ half2 C : SV_CullDistance) : SV_Target {
+ return Pos * C.x;
+}
+
+// CHECK-LABEL: define void @vs_main()
+// CHECK-F32: call void @llvm.dx.store.output.v2f32(i32 1, i32 0, i8 0, <2 x float>
+// CHECK-F16: call void @llvm.dx.store.output.v2f16(i32 1, i32 0, i8 0, <2 x half>
+// CHECK-LABEL: define void @ps_main()
+// CHECK-F32: call <2 x float> @llvm.dx.load.input.v2f32(i32 1, i32 0, i8 0, i32 poison)
+// CHECK-F16: call <2 x half> @llvm.dx.load.input.v2f16(i32 1, i32 0, i8 0, i32 poison)
+
+// CHECK-F32-DAG: !{i32 1, !"SV_CullDistance", i32 9, i32 7, ![[#]], i32 2, i32 1, i8 2, i32 1, i8 0, i8 0, i8 0, i32 0}
+// CHECK-F16-DAG: !{i32 1, !"SV_CullDistance", i32 8, i32 7, ![[#]], i32 2, i32 1, i8 2, i32 1, i8 0, i8 0, i8 0, i32 0}
diff --git a/clang/test/CodeGenHLSL/semantics/SV_CullDistance-layout.hlsl b/clang/test/CodeGenHLSL/semantics/SV_CullDistance-layout.hlsl
new file mode 100644
index 00000000000000..e5f9c3d0cb9fab
--- /dev/null
+++ b/clang/test/CodeGenHLSL/semantics/SV_CullDistance-layout.hlsl
@@ -0,0 +1,137 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK-DXIL
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK-SPIRV
+
+// Semantic indices of SV_CullDistance that are not contiguous, that are on the
+// fields of a nested struct, or that are applied to a struct-typed field.
+//
+// DXIL: each leaf is its own signature element with its semantic index.
+// SPIR-V: each leaf starts at an offset into the CullDistance array, in
+// increasing semantic index order, regardless of gaps or declaration order.
+
+// Indices 0 and 5: index 5 directly follows index 0 in the SPIR-V array.
+struct GapOut {
+ float4 Pos : SV_Position;
+ float C0 : SV_CullDistance0;
+ float2 C5 : SV_CullDistance5;
+};
+[shader("vertex")]
+GapOut vs_gap(float4 In : A) {
+ GapOut O;
+ O.Pos = In;
+ O.C0 = In.x;
+ O.C5 = In.yz;
+ return O;
+}
+
+// The fields of a nested struct, declared in decreasing index order: B (index
+// 1) is at 0 to 1 of the SPIR-V array and A (index 2) is at 2.
+struct Inner {
+ float A : SV_CullDistance2;
+ float2 B : SV_CullDistance1;
+};
+struct NestedOut {
+ float4 Pos : SV_Position;
+ Inner I;
+};
+[shader("vertex")]
+NestedOut vs_nested(float4 In : A) {
+ NestedOut O;
+ O.Pos = In;
+ O.I.A = In.x;
+ O.I.B = In.yz;
+ return O;
+}
+
+// A semantic on a struct-typed field applies to each member with consecutive
+// indices: A is index 3 and B is index 4.
+struct Plain {
+ float A;
+ float2 B;
+};
+struct FieldOut {
+ float4 Pos : SV_Position;
+ Plain C : SV_CullDistance3;
+};
+[shader("vertex")]
+FieldOut vs_field(float4 In : A) {
+ FieldOut O;
+ O.Pos = In;
+ O.C.A = In.x;
+ O.C.B = In.yz;
+ return O;
+}
+[shader("pixel")]
+float4 ps_field(float4 Pos : SV_Position,
+ Plain C : SV_CullDistance3) : SV_Target {
+ return Pos * C.A + C.B.y;
+}
+
+// CHECK-DXIL-LABEL: define void @vs_gap()
+// CHECK-DXIL: call void @llvm.dx.store.output.f32(i32 1, i32 0, i8 0,
+// CHECK-DXIL: call void @llvm.dx.store.output.v2f32(i32 2, i32 0, i8 0,
+// CHECK-DXIL-LABEL: define void @vs_nested()
+// CHECK-DXIL: call void @llvm.dx.store.output.f32(i32 1, i32 0, i8 0,
+// CHECK-DXIL: call void @llvm.dx.store.output.v2f32(i32 2, i32 0, i8 0,
+// CHECK-DXIL-LABEL: define void @vs_field()
+// CHECK-DXIL: call void @llvm.dx.store.output.f32(i32 1, i32 0, i8 0,
+// CHECK-DXIL: call void @llvm.dx.store.output.v2f32(i32 2, i32 0, i8 0,
+// CHECK-DXIL-LABEL: define void @ps_field()
+// CHECK-DXIL: call float @llvm.dx.load.input.f32(i32 1, i32 0, i8 0, i32 poison)
+// CHECK-DXIL: call <2 x float> @llvm.dx.load.input.v2f32(i32 2, i32 0, i8 0, i32 poison)
+
+// The operands of an element are, in order:
+// SigId, Name, CompType, SemanticKind, SemanticIndices, Interpolation, Rows,
+// Cols, StartRow, StartCol, UsageMask, DynIndexMask, GSStream
+// CHECK-DXIL-DAG: !{ptr @vs_gap, ![[#GAP_IN:]], ![[#GAP_OUT:]]}
+// CHECK-DXIL-DAG: ![[#GAP_OUT]] = !{![[#GAP_POS:]], ![[#GAP_C0:]], ![[#GAP_C5:]]}
+// CHECK-DXIL-DAG: ![[#GAP_C0]] = !{i32 1, !"SV_CullDistance", i32 9, i32 7, ![[#IDX0:]], i32 2, i32 1, i8 1,
+// CHECK-DXIL-DAG: ![[#GAP_C5]] = !{i32 2, !"SV_CullDistance", i32 9, i32 7, ![[#IDX5:]], i32 2, i32 1, i8 2,
+// CHECK-DXIL-DAG: !{ptr @vs_nested, ![[#NESTED_IN:]], ![[#NESTED_OUT:]]}
+// CHECK-DXIL-DAG: ![[#NESTED_OUT]] = !{![[#NESTED_POS:]], ![[#NESTED_A:]], ![[#NESTED_B:]]}
+// CHECK-DXIL-DAG: ![[#NESTED_A]] = !{i32 1, !"SV_CullDistance", i32 9, i32 7, ![[#IDX2:]], i32 2, i32 1, i8 1,
+// CHECK-DXIL-DAG: ![[#NESTED_B]] = !{i32 2, !"SV_CullDistance", i32 9, i32 7, ![[#IDX1:]], i32 2, i32 1, i8 2,
+// CHECK-DXIL-DAG: !{ptr @vs_field, ![[#FIELD_IN:]], ![[#FIELD_OUT:]]}
+// CHECK-DXIL-DAG: ![[#FIELD_OUT]] = !{![[#FIELD_POS:]], ![[#FIELD_A:]], ![[#FIELD_B:]]}
+// CHECK-DXIL-DAG: ![[#FIELD_A]] = !{i32 1, !"SV_CullDistance", i32 9, i32 7, ![[#IDX3:]], i32 2, i32 1, i8 1,
+// CHECK-DXIL-DAG: ![[#FIELD_B]] = !{i32 2, !"SV_CullDistance", i32 9, i32 7, ![[#IDX4:]], i32 2, i32 1, i8 2,
+// The pixel input signature is identical to the vertex output signature, so
+// they are the same metadata node.
+// CHECK-DXIL-DAG: !{ptr @ps_field, ![[#FIELD_OUT]], ![[#PS_FIELD_OUT:]]}
+// CHECK-DXIL-DAG: ![[#IDX0]] = !{i32 0}
+// CHECK-DXIL-DAG: ![[#IDX1]] = !{i32 1}
+// CHECK-DXIL-DAG: ![[#IDX2]] = !{i32 2}
+// CHECK-DXIL-DAG: ![[#IDX3]] = !{i32 3}
+// CHECK-DXIL-DAG: ![[#IDX4]] = !{i32 4}
+// CHECK-DXIL-DAG: ![[#IDX5]] = !{i32 5}
+
+// Each entry point has its own CullDistance array of three floats.
+
+// CHECK-SPIRV-LABEL: define void @vs_gap()
+// CHECK-SPIRV: %[[C0:.*]] = extractvalue %struct.GapOut %{{.*}}, 1
+// CHECK-SPIRV: %[[P0:.*]] = call ptr addrspace(8) {{.*}} @llvm.structured.gep.p8(ptr addrspace(8) elementtype([3 x float]) @[[GAP:SV_CullDistance]], i32 0)
+// CHECK-SPIRV: store float %[[C0]], ptr addrspace(8) %[[P0]]
+// CHECK-SPIRV: %[[C5:.*]] = extractvalue %struct.GapOut %{{.*}}, 2
+// CHECK-SPIRV: %[[C5_X:.*]] = extractelement <2 x float> %[[C5]], i64 0
+// CHECK-SPIRV: %[[P1:.*]] = call ptr addrspace(8) {{.*}} @llvm.structured.gep.p8(ptr addrspace(8) elementtype([3 x float]) @[[GAP]], i32 1)
+// CHECK-SPIRV: store float %[[C5_X]], ptr addrspace(8) %[[P1]]
+// CHECK-SPIRV: %[[C5_Y:.*]] = extractelement <2 x float> %[[C5]], i64 1
+// CHECK-SPIRV: %[[P2:.*]] = call ptr addrspace(8) {{.*}} @llvm.structured.gep.p8(ptr addrspace(8) elementtype([3 x float]) @[[GAP]], i32 2)
+// CHECK-SPIRV: store float %[[C5_Y]], ptr addrspace(8) %[[P2]]
+
+// CHECK-SPIRV-LABEL: define void @vs_nested()
+// CHECK-SPIRV: @llvm.structured.gep.p8(ptr addrspace(8) elementtype([3 x float]) @[[NESTED:SV_CullDistance.[0-9]+]], i32 2)
+// CHECK-SPIRV: @llvm.structured.gep.p8(ptr addrspace(8) elementtype([3 x float]) @[[NESTED]], i32 0)
+// CHECK-SPIRV: @llvm.structured.gep.p8(ptr addrspace(8) elementtype([3 x float]) @[[NESTED]], i32 1)
+
+// CHECK-SPIRV-LABEL: define void @vs_field()
+// CHECK-SPIRV: @llvm.structured.gep.p8(ptr addrspace(8) elementtype([3 x float]) @[[FIELD_OUT:SV_CullDistance.[0-9]+]], i32 0)
+// CHECK-SPIRV: @llvm.structured.gep.p8(ptr addrspace(8) elementtype([3 x float]) @[[FIELD_OUT]], i32 1)
+// CHECK-SPIRV: @llvm.structured.gep.p8(ptr addrspace(8) elementtype([3 x float]) @[[FIELD_OUT]], i32 2)
+
+// CHECK-SPIRV-LABEL: define void @ps_field()
+// CHECK-SPIRV: %[[Q0:.*]] = call ptr addrspace(7) {{.*}} @llvm.structured.gep.p7(ptr addrspace(7) elementtype([3 x float]) @[[FIELD_IN:SV_CullDistance.[0-9]+]], i32 0)
+// CHECK-SPIRV: %[[A:.*]] = load float, ptr addrspace(7) %[[Q0]]
+// CHECK-SPIRV: insertvalue %struct.Plain poison, float %[[A]], 0
+// CHECK-SPIRV: @llvm.structured.gep.p7(ptr addrspace(7) elementtype([3 x float]) @[[FIELD_IN]], i32 1)
+// CHECK-SPIRV: @llvm.structured.gep.p7(ptr addrspace(7) elementtype([3 x float]) @[[FIELD_IN]], i32 2)
+// CHECK-SPIRV: insertvalue %struct.Plain %{{.*}}, <2 x float> %{{.*}}, 1
diff --git a/clang/test/CodeGenHLSL/semantics/SV_CullDistance.hlsl b/clang/test/CodeGenHLSL/semantics/SV_CullDistance.hlsl
new file mode 100644
index 00000000000000..7d0598b62b30a0
--- /dev/null
+++ b/clang/test/CodeGenHLSL/semantics/SV_CullDistance.hlsl
@@ -0,0 +1,93 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK-DXIL
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -emit-llvm -finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s --check-prefix=CHECK-SPIRV
+
+struct VSOut {
+ float4 Pos : SV_Position;
+ float2 Cull1[2] : SV_CullDistance1;
+ float Cull0 : SV_CullDistance0;
+};
+
+// The vertex shader writes the cull distances as system values. Its own input
+// with the same name is an arbitrary semantic.
+[shader("vertex")]
+VSOut vs_main(float4 In : SV_CullDistance) {
+ VSOut O;
+ O.Pos = In;
+ O.Cull1[0] = In.xy;
+ O.Cull1[1] = In.zw;
+ O.Cull0 = In.x;
+ return O;
+}
+
+[shader("pixel")]
+float4 ps_main(float4 Pos : SV_Position, float Cull0 : SV_CullDistance0,
+ float2 Cull1[2] : SV_CullDistance1) : SV_Target {
+ return Pos * Cull0 + Cull1[1].y;
+}
+
+// CHECK-DXIL-LABEL: define void @vs_main()
+// CHECK-DXIL: call <4 x float> @llvm.dx.load.input.v4f32(i32 0, i32 0, i8 0, i32 poison)
+// CHECK-DXIL: call void @llvm.dx.store.output.v4f32(i32 0, i32 0, i8 0,
+// CHECK-DXIL: call void @llvm.dx.store.output.v2f32(i32 1, i32 0, i8 0,
+// CHECK-DXIL: call void @llvm.dx.store.output.v2f32(i32 1, i32 1, i8 0,
+// CHECK-DXIL: call void @llvm.dx.store.output.f32(i32 2, i32 0, i8 0,
+// CHECK-DXIL-LABEL: define void @ps_main()
+// CHECK-DXIL: call <4 x float> @llvm.dx.load.input.v4f32(i32 0, i32 0, i8 0, i32 poison)
+// CHECK-DXIL: call float @llvm.dx.load.input.f32(i32 1, i32 0, i8 0, i32 poison)
+// CHECK-DXIL: call <2 x float> @llvm.dx.load.input.v2f32(i32 2, i32 0, i8 0, i32 poison)
+// CHECK-DXIL: call <2 x float> @llvm.dx.load.input.v2f32(i32 2, i32 1, i8 0, i32 poison)
+
+// The operands of an element are, in order:
+// SigId, Name, CompType, SemanticKind, SemanticIndices, Interpolation, Rows,
+// Cols, StartRow, StartCol, UsageMask, DynIndexMask, GSStream
+// The vertex input is a float (9), arbitrary (kind 0) element. The outputs and
+// pixel inputs are float (9), linear (2) CullDistance (kind 7) elements, which
+// share the clip/cull rows after the position (StartRow 1): index 1 is two rows
+// of two columns, and index 0 one column packed at StartCol 2 of the outputs.
+// CHECK-DXIL-DAG: !{ptr @vs_main, ![[#VS_IN:]], ![[#VS_OUT:]]}
+// CHECK-DXIL-DAG: ![[#VS_IN]] = !{![[#VS_IN_CULL:]]}
+// CHECK-DXIL-DAG: ![[#VS_IN_CULL]] = !{i32 0, !"SV_CullDistance", i32 9, i32 0, ![[#ZERO:]], i32 0, i32 1, i8 4, i32 0, i8 0, i8 0, i8 0, i32 0}
+// CHECK-DXIL-DAG: ![[#VS_OUT]] = !{![[#VS_OUT_POS:]], ![[#VS_OUT_CULL1:]], ![[#VS_OUT_CULL0:]]}
+// CHECK-DXIL-DAG: ![[#VS_OUT_CULL1]] = !{i32 1, !"SV_CullDistance", i32 9, i32 7, ![[#ONE_TWO:]], i32 2, i32 2, i8 2, i32 1, i8 0, i8 0, i8 0, i32 0}
+// CHECK-DXIL-DAG: ![[#VS_OUT_CULL0]] = !{i32 2, !"SV_CullDistance", i32 9, i32 7, ![[#ZERO]], i32 2, i32 1, i8 1, i32 1, i8 2, i8 0, i8 0, i32 0}
+// CHECK-DXIL-DAG: !{ptr @ps_main, ![[#PS_IN:]], ![[#PS_OUT:]]}
+// CHECK-DXIL-DAG: ![[#PS_IN]] = !{![[#PS_IN_POS:]], ![[#PS_IN_CULL0:]], ![[#PS_IN_CULL1:]]}
+// CHECK-DXIL-DAG: ![[#PS_IN_CULL0]] = !{i32 1, !"SV_CullDistance", i32 9, i32 7, ![[#ZERO]], i32 2, i32 1, i8 1, i32 1, i8 0, i8 0, i8 0, i32 0}
+// CHECK-DXIL-DAG: ![[#PS_IN_CULL1]] = !{i32 2, !"SV_CullDistance", i32 9, i32 7, ![[#ONE_TWO]], i32 2, i32 2, i8 2, i32 1, i8 1, i8 0, i8 0, i32 0}
+// CHECK-DXIL-DAG: ![[#ZERO]] = !{i32 0}
+// CHECK-DXIL-DAG: ![[#ONE_TWO]] = !{i32 1, i32 2}
+
+// CHECK-SPIRV-DAG: @SV_CullDistance0 = {{.*}} addrspace(7) {{.*}} <4 x float>, !spirv.Decorations ![[#VS_IN_LOC:]]
+// CHECK-SPIRV-DAG: @SV_CullDistance = {{.*}} addrspace(8) global [5 x float], !spirv.Decorations ![[#CULL:]]
+// CHECK-SPIRV-DAG: @SV_CullDistance.{{[0-9]+}} = {{.*}} addrspace(7) {{.*}} [5 x float], !spirv.Decorations ![[#CULL]]
+
+// CHECK-SPIRV-LABEL: define void @vs_main()
+// CHECK-SPIRV: %[[CULL1:.*]] = extractvalue %struct.VSOut %{{.*}}, 1
+// CHECK-SPIRV: %[[CULL1_0:.*]] = extractvalue [2 x <2 x float>] %[[CULL1]], 0
+// CHECK-SPIRV: %[[X0:.*]] = extractelement <2 x float> %[[CULL1_0]], i64 0
+// CHECK-SPIRV: %[[P1:.*]] = call ptr addrspace(8) {{.*}} @llvm.structured.gep.p8(ptr addrspace(8) elementtype([5 x float]) @SV_CullDistance, i32 1)
+// CHECK-SPIRV: store float %[[X0]], ptr addrspace(8) %[[P1]]
+// CHECK-SPIRV: @llvm.structured.gep.p8(ptr addrspace(8) elementtype([5 x float]) @SV_CullDistance, i32 2)
+// CHECK-SPIRV: %[[CULL1_1:.*]] = extractvalue [2 x <2 x float>] %[[CULL1]], 1
+// CHECK-SPIRV: @llvm.structured.gep.p8(ptr addrspace(8) elementtype([5 x float]) @SV_CullDistance, i32 3)
+// CHECK-SPIRV: @llvm.structured.gep.p8(ptr addrspace(8) elementtype([5 x float]) @SV_CullDistance, i32 4)
+// CHECK-SPIRV: %[[CULL0:.*]] = extractvalue %struct.VSOut %{{.*}}, 2
+// CHECK-SPIRV: %[[P0:.*]] = call ptr addrspace(8) {{.*}} @llvm.structured.gep.p8(ptr addrspace(8) elementtype([5 x float]) @SV_CullDistance, i32 0)
+// CHECK-SPIRV: store float %[[CULL0]], ptr addrspace(8) %[[P0]]
+
+// CHECK-SPIRV-LABEL: define void @ps_main()
+// CHECK-SPIRV: %[[Q0:.*]] = call ptr addrspace(7) {{.*}} @llvm.structured.gep.p7(ptr addrspace(7) elementtype([5 x float]) @SV_CullDistance.{{[0-9]+}}, i32 0)
+// CHECK-SPIRV: %[[IN0:.*]] = load float, ptr addrspace(7) %[[Q0]]
+// CHECK-SPIRV: @llvm.structured.gep.p7(ptr addrspace(7) elementtype([5 x float]) @SV_CullDistance.{{[0-9]+}}, i32 1)
+// CHECK-SPIRV: @llvm.structured.gep.p7(ptr addrspace(7) elementtype([5 x float]) @SV_CullDistance.{{[0-9]+}}, i32 2)
+// CHECK-SPIRV: insertvalue [2 x <2 x float>] poison, <2 x float> %{{.*}}, 0
+// CHECK-SPIRV: @llvm.structured.gep.p7(ptr addrspace(7) elementtype([5 x float]) @SV_CullDistance.{{[0-9]+}}, i32 3)
+// CHECK-SPIRV: @llvm.structured.gep.p7(ptr addrspace(7) elementtype([5 x float]) @SV_CullDistance.{{[0-9]+}}, i32 4)
+// CHECK-SPIRV: insertvalue [2 x <2 x float>] %{{.*}}, <2 x float> %{{.*}}, 1
+// CHECK-SPIRV: call spir_func <4 x float> @{{.*}}ps_main{{.*}}(<4 x float> %{{.*}}, float %[[IN0]],
+
+// The built-in has no interpolation decorations.
+// CHECK-SPIRV-DAG: ![[#CULL]] = !{![[#CULL_BUILTIN:]]}
+// CHECK-SPIRV-DAG: ![[#CULL_BUILTIN]] = !{i32 11, i32 4}
+// | `-> BuiltIn CullDistance
+// `-> SPIR-V decoration 'BuiltIn'
diff --git a/clang/test/SemaHLSL/Semantics/culldistance.stages.hlsl b/clang/test/SemaHLSL/Semantics/culldistance.stages.hlsl
new file mode 100644
index 00000000000000..af7b889b220dd8
--- /dev/null
+++ b/clang/test/SemaHLSL/Semantics/culldistance.stages.hlsl
@@ -0,0 +1,38 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-library -finclude-default-header -verify -o - %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -finclude-default-header -verify -o - %s
+
+// Stage and IO direction checks for SV_CullDistance: a vertex output and a
+// pixel input. Type, index and interpolation checks are covered by
+// culldistance.vs-ps.hlsl.
+
+struct VSOut {
+ float4 p : SV_Position;
+ float c : SV_CullDistance;
+};
+
+[shader("vertex")]
+VSOut ok_vs_out() { return (VSOut)0; }
+
+// The cull distance can be the only vertex output, without SV_Position.
+[shader("vertex")]
+float ok_vs_out_only(float c : C) : SV_CullDistance { return c; }
+
+[shader("pixel")]
+float4 ok_ps_in(float c : SV_CullDistance) : SV_Target {
+ return float4(c, 0, 0, 1);
+}
+
+// A vertex input with this name is an arbitrary semantic, so any type works.
+[shader("vertex")]
+float4 ok_vs_in_arbitrary(uint4 c : SV_CullDistance) : SV_Position {
+ return (float4)c;
+}
+
+[shader("pixel")]
+float bad_ps_out() : SV_CullDistance { return 0; }
+// expected-error at -1 {{semantic 'SV_CullDistance' is not supported in pixel shader outputs}}
+
+[shader("compute")]
+[numthreads(1, 1, 1)]
+void bad_cs_in(float c : SV_CullDistance) {}
+// expected-error at -1 {{semantic 'SV_CullDistance' is not supported in compute shader inputs}}
diff --git a/clang/test/SemaHLSL/Semantics/culldistance.vs-ps.hlsl b/clang/test/SemaHLSL/Semantics/culldistance.vs-ps.hlsl
new file mode 100644
index 00000000000000..da24911ef2bf4a
--- /dev/null
+++ b/clang/test/SemaHLSL/Semantics/culldistance.vs-ps.hlsl
@@ -0,0 +1,71 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-library -finclude-default-header -fsyntax-only -verify=expected,dxil %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -finclude-default-header -fsyntax-only -verify=expected,spirv %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-library -fnative-half-type -fnative-int16-type -finclude-default-header -fsyntax-only -verify=expected,dxil %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -fnative-half-type -fnative-int16-type -finclude-default-header -fsyntax-only -verify=expected,spirv %s
+
+// Type, index and interpolation checks for SV_CullDistance on vertex outputs
+// and pixel inputs. These are independent of the shader stage and IO direction,
+// which are covered by culldistance.stages.hlsl.
+
+// Scalars, vectors of up to four components and arrays of these are allowed,
+// with any semantic index.
+struct VSOut {
+ float4 p : SV_Position;
+ float c0 : SV_CullDistance0;
+ float3 c1 : sv_culldistance1;
+ float2 c2[2] : SV_CullDistance2;
+};
+[shader("vertex")]
+VSOut vertex_output() { return (VSOut)0; }
+[shader("pixel")]
+void pixel_input(float4 c : SV_CullDistance7) {}
+
+// Interpolation modifiers are allowed on the pixel input.
+[shader("pixel")]
+void interpolation(noperspective centroid float c : SV_CullDistance) {}
+
+// DXIL accepts half, with or without native 16-bit types. SPIR-V requires
+// 32-bit floats and rejects half in both cases.
+[shader("pixel")]
+void half_input(half2 c : SV_CullDistance) {}
+// spirv-error at -1 {{semantic 'SV_CullDistance' must be a scalar or vector of up to 4 components of 32 bit floating-point type (was 'half2' (aka 'vector<half, 2>'))}}
+
+[shader("pixel")]
+void bad_double(double c : SV_CullDistance) {}
+// dxil-error at -1 {{semantic 'SV_CullDistance' must be a scalar or vector of up to 4 components of 16 or 32 bit floating-point type (was 'double')}}
+// spirv-error at -2 {{semantic 'SV_CullDistance' must be a scalar or vector of up to 4 components of 32 bit floating-point type (was 'double')}}
+[shader("pixel")]
+void bad_int(int c : SV_CullDistance) {}
+// dxil-error at -1 {{semantic 'SV_CullDistance' must be a scalar or vector of up to 4 components of 16 or 32 bit floating-point type (was 'int')}}
+// spirv-error at -2 {{semantic 'SV_CullDistance' must be a scalar or vector of up to 4 components of 32 bit floating-point type (was 'int')}}
+[shader("pixel")]
+void bad_bool(bool c : SV_CullDistance) {}
+// dxil-error at -1 {{semantic 'SV_CullDistance' must be a scalar or vector of up to 4 components of 16 or 32 bit floating-point type (was 'bool')}}
+// spirv-error at -2 {{semantic 'SV_CullDistance' must be a scalar or vector of up to 4 components of 32 bit floating-point type (was 'bool')}}
+[shader("vertex")]
+float2x2 bad_matrix() : SV_CullDistance { return 0; }
+// dxil-error at -1 {{semantic 'SV_CullDistance' must be a scalar or vector of up to 4 components of 16 or 32 bit floating-point type (was 'float2x2' (aka 'matrix<float, 2, 2>'))}}
+// spirv-error at -2 {{semantic 'SV_CullDistance' must be a scalar or vector of up to 4 components of 32 bit floating-point type (was 'float2x2' (aka 'matrix<float, 2, 2>'))}}
+[shader("pixel")]
+void bad_int_array(int c[2] : SV_CullDistance) {}
+// dxil-error at -1 {{semantic 'SV_CullDistance' must be a scalar or vector of up to 4 components of 16 or 32 bit floating-point type (was 'int[2]')}}
+// spirv-error at -2 {{semantic 'SV_CullDistance' must be a scalar or vector of up to 4 components of 32 bit floating-point type (was 'int[2]')}}
+
+// An array uses one semantic index per element, so c uses indices 0 and 1 and
+// overlaps d. This is rejected on both targets.
+struct Overlap {
+ float4 p : SV_Position;
+ float c[2] : SV_CullDistance0; // #overlap_out_c
+ float d : SV_CullDistance1; // #overlap_out_d
+};
+[shader("vertex")]
+Overlap overlapping_output() { return (Overlap)0; }
+// expected-error@#overlap_out_d {{semantic index overlap SV_CullDistance1}}
+// expected-note@#overlap_out_c {{previous use is here}}
+// expected-note@#overlap_out_d {{'d' used here}}
+[shader("pixel")]
+void overlapping_input(float c[2] : SV_CullDistance0, // #overlap_in_c
+ float d : SV_CullDistance1) {} // #overlap_in_d
+// expected-error@#overlap_in_d {{semantic index overlap SV_CullDistance1}}
+// expected-note@#overlap_in_c {{previous use is here}}
+// expected-note@#overlap_in_d {{'d' declared here}}
diff --git a/llvm/include/llvm/Frontend/HLSL/SemanticSignatures.h b/llvm/include/llvm/Frontend/HLSL/SemanticSignatures.h
index 98579ec6561e82..86f12c3c36abd3 100644
--- a/llvm/include/llvm/Frontend/HLSL/SemanticSignatures.h
+++ b/llvm/include/llvm/Frontend/HLSL/SemanticSignatures.h
@@ -84,6 +84,7 @@ enum class SPIRVInterfaceDecoration : uint32_t {
// SPIR-V BuiltIn enumerants used as the operand of a BuiltIn decoration. The
// values match the SPIR-V specification.
enum class SPIRVBuiltIn : uint32_t {
+ CullDistance = 4,
Layer = 9,
ViewportIndex = 10,
SampleMask = 20,
diff --git a/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp b/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp
index 683a42a466be22..ddef0e21a07123 100644
--- a/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp
+++ b/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp
@@ -147,6 +147,11 @@ static Error diagnoseSemanticType(const SemanticSignatureElement &E) {
return signatureError("SV_Barycentrics requires one row of three 16- or "
"32-bit floating-point components");
break;
+ case dxbc::PSV::SemanticKind::CullDistance:
+ if (E.CompType != ElementType::F16 && E.CompType != ElementType::F32)
+ return signatureError(
+ "SV_CullDistance requires 16- or 32-bit floating-point components");
+ break;
default:
break;
}
@@ -235,6 +240,8 @@ static Error parseSignature(Metadata *MD,
std::array<uint8_t, MaxSignatureRows> AllocatedColumns = {};
std::array<dxbc::PSV::InterpolationMode, MaxSignatureRows> RowModes = {};
std::array<bool, MaxSignatureRows> RowNative16 = {};
+ std::set<unsigned> ClipCullRows;
+ unsigned ClipCullComponents = 0;
for (const MDOperand &Op : List->operands()) {
auto *Node = dyn_cast_or_null<MDNode>(Op);
if (Error Err = checkElementNode(Node))
@@ -318,6 +325,21 @@ static Error parseSignature(Metadata *MD,
return signatureError(
"integer pixel inputs require constant interpolation");
+ if (Interpretation == SemanticInterpretation::ClipCull) {
+ // Clip and cull distances share at most two rows of four components.
+ for (unsigned Row = E.StartRow; Row != E.StartRow + E.Rows; ++Row)
+ ClipCullRows.insert(Row);
+ if (ClipCullRows.size() > MaxClipCullRows)
+ return signatureError("clip and cull distances must not use more "
+ "than two rows (element " +
+ Twine(E.SigId) + ")");
+ ClipCullComponents += E.Rows * E.Cols;
+ if (ClipCullComponents > MaxClipCullRows * MaxSignatureCols)
+ return signatureError("clip and cull distances must not use more "
+ "than eight components (element " +
+ Twine(E.SigId) + ")");
+ }
+
if (E.isAllocated()) {
if (++NumAllocated > MaxAllocatedElements)
return signatureError("too many signature elements");
diff --git a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
index 526a0eaf0bb9ad..9ed22fe3a7bbc0 100644
--- a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
@@ -1095,6 +1095,7 @@ void RequirementHandler::initAvailableCapabilitiesForVulkan(
addAvailableCaps({Capability::Int64,
Capability::Geometry,
Capability::MultiViewport,
+ Capability::CullDistance,
Capability::Float16,
Capability::Float64,
Capability::GroupNonUniform,
diff --git a/llvm/test/CodeGen/DirectX/ContainerData/SemanticSignatures-cull-distance.ll b/llvm/test/CodeGen/DirectX/ContainerData/SemanticSignatures-cull-distance.ll
new file mode 100644
index 00000000000000..bc014a95718578
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/ContainerData/SemanticSignatures-cull-distance.ll
@@ -0,0 +1,66 @@
+; RUN: llc -filetype=obj %s -o %t.dxbc
+; RUN: obj2yaml %t.dxbc | FileCheck %s
+
+; A vertex shader writing two SV_CullDistance elements that share a clip/cull
+; row: index 1 is a two-row float2 array and index 0 a float packed after it.
+; They are written to the OSG1 and PSV0 parts with the CullDistance system
+; value. Analysis, lowering and metadata are covered by
+; Metadata/semantic-signatures-cull-distance.ll.
+target triple = "dxil-pc-shadermodel6.0-vertex"
+
+define void @main() #0 {
+ %p = call <4 x float> @llvm.dx.load.input.v4f32(i32 0, i32 0, i8 0, i32 poison)
+ call void @llvm.dx.store.output.v4f32(i32 0, i32 0, i8 0, <4 x float> %p)
+ call void @llvm.dx.store.output.v2f32(i32 1, i32 0, i8 0, <2 x float> zeroinitializer)
+ call void @llvm.dx.store.output.v2f32(i32 1, i32 1, i8 0, <2 x float> zeroinitializer)
+ call void @llvm.dx.store.output.f32(i32 2, i32 0, i8 0, float 1.0)
+ ret void
+}
+attributes #0 = { "hlsl.shader"="vertex" }
+
+!dx.valver = !{!10}
+!10 = !{i32 1, i32 8}
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, !2}
+!1 = !{!3}
+!2 = !{!4, !5, !6}
+!3 = !{i32 0, !"POSITION", i32 9, i32 0, !7, i32 0, i32 1, i8 4, i32 0, i8 0, i8 0, i8 0, i32 0}
+!4 = !{i32 0, !"SV_Position", i32 9, i32 3, !7, i32 4, i32 1, i8 4, i32 0, i8 0, i8 0, i8 0, i32 0}
+!5 = !{i32 1, !"SV_CullDistance", i32 9, i32 7, !8, i32 2, i32 2, i8 2, i32 1, i8 0, i8 0, i8 0, i32 0}
+!6 = !{i32 2, !"SV_CullDistance", i32 9, i32 7, !7, i32 2, i32 1, i8 1, i32 1, i8 2, i8 0, i8 0, i32 0}
+!7 = !{i32 0}
+!8 = !{i32 1, i32 2}
+
+; Signature parameters are ordered by register, so the second row of index 1
+; follows index 0.
+; CHECK: Name: OSG1
+; CHECK: Name: SV_CullDistance
+; CHECK-NEXT: Index: 1
+; CHECK-NEXT: SystemValue: CullDistance
+; CHECK-NEXT: CompType: Float32
+; CHECK-NEXT: Register: 1
+; CHECK-NEXT: Mask: 3
+; CHECK: Name: SV_CullDistance
+; CHECK-NEXT: Index: 0
+; CHECK-NEXT: SystemValue: CullDistance
+; CHECK-NEXT: CompType: Float32
+; CHECK-NEXT: Register: 1
+; CHECK-NEXT: Mask: 4
+; CHECK: Name: SV_CullDistance
+; CHECK-NEXT: Index: 2
+; CHECK-NEXT: SystemValue: CullDistance
+; CHECK-NEXT: CompType: Float32
+; CHECK-NEXT: Register: 2
+; CHECK-NEXT: Mask: 3
+; CHECK: Name: PSV0
+; CHECK: SigOutputElements:
+; CHECK: - Name:
+; CHECK: - Name:
+; CHECK-NEXT: Indices: [ 1, 2 ]
+; CHECK-NEXT: StartRow: 1
+; CHECK-NEXT: Cols: 2
+; CHECK-NEXT: StartCol: 0
+; CHECK-NEXT: Allocated: true
+; CHECK-NEXT: Kind: CullDistance
+; CHECK-NEXT: ComponentType: Float32
+; CHECK-NEXT: Interpolation: Linear
diff --git a/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-cull-distance-errors.ll b/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-cull-distance-errors.ll
new file mode 100644
index 00000000000000..22ae981e4b3328
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-cull-distance-errors.ll
@@ -0,0 +1,72 @@
+; RUN: split-file %s %t
+; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/int.ll 2>&1 | FileCheck %s --check-prefix=CHECK-INT
+; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/rows.ll 2>&1 | FileCheck %s --check-prefix=CHECK-ROWS
+; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/components.ll 2>&1 | FileCheck %s --check-prefix=CHECK-COMPONENTS
+; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/kind.ll 2>&1 | FileCheck %s --check-prefix=CHECK-KIND
+; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/output.ll 2>&1 | FileCheck %s --check-prefix=CHECK-OUTPUT
+
+; Each case is a complete module with an invalid SV_CullDistance declaration.
+
+;--- int.ll
+; CHECK-INT: SV_CullDistance requires 16- or 32-bit floating-point components
+target triple = "dxil-pc-shadermodel6.0-pixel"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="pixel" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, null}
+!1 = !{!2}
+!2 = !{i32 0, !"SV_CullDistance", i32 5, i32 7, !3, i32 1, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!3 = !{i32 0}
+
+;--- rows.ll
+; Clip and cull distances share at most two rows.
+; CHECK-ROWS: clip and cull distances must not use more than two rows (element 1)
+target triple = "dxil-pc-shadermodel6.0-pixel"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="pixel" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, null}
+!1 = !{!2, !3}
+!2 = !{i32 0, !"SV_CullDistance", i32 9, i32 7, !4, i32 2, i32 2, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!3 = !{i32 1, !"SV_CullDistance", i32 9, i32 7, !5, i32 2, i32 1, i8 1, i32 2, i8 0, i8 0, i8 0, i32 0}
+!4 = !{i32 0, i32 1}
+!5 = !{i32 2}
+
+;--- components.ll
+; Clip and cull distances share at most eight components.
+; CHECK-COMPONENTS: clip and cull distances must not use more than eight components (element 2)
+target triple = "dxil-pc-shadermodel6.0-pixel"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="pixel" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, null}
+!1 = !{!2, !3, !4}
+!2 = !{i32 0, !"SV_CullDistance", i32 9, i32 7, !5, i32 2, i32 1, i8 4, i32 0, i8 0, i8 0, i8 0, i32 0}
+!3 = !{i32 1, !"SV_CullDistance", i32 9, i32 7, !6, i32 2, i32 1, i8 4, i32 1, i8 0, i8 0, i8 0, i32 0}
+!4 = !{i32 2, !"SV_CullDistance", i32 9, i32 7, !7, i32 2, i32 1, i8 1, i32 1, i8 0, i8 0, i8 0, i32 0}
+!5 = !{i32 0}
+!6 = !{i32 1}
+!7 = !{i32 2}
+
+;--- kind.ll
+; A vertex input with this name is an arbitrary semantic.
+; CHECK-KIND: semantic name and kind disagree at this signature point
+target triple = "dxil-pc-shadermodel6.0-vertex"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="vertex" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, null}
+!1 = !{!2}
+!2 = !{i32 0, !"SV_CullDistance", i32 9, i32 7, !3, i32 0, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!3 = !{i32 0}
+
+;--- output.ll
+; CHECK-OUTPUT: invalid or unsupported semantic for this stage and signature category
+target triple = "dxil-pc-shadermodel6.0-pixel"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="pixel" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, null, !1}
+!1 = !{!2}
+!2 = !{i32 0, !"SV_CullDistance", i32 9, i32 7, !3, i32 2, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!3 = !{i32 0}
diff --git a/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-cull-distance.ll b/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-cull-distance.ll
new file mode 100644
index 00000000000000..2e38b1ac8afa7a
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-cull-distance.ll
@@ -0,0 +1,90 @@
+; RUN: split-file %s %t
+; RUN: opt -disable-output -passes='print<dxil-signature>' %t/vertex.ll 2>&1 | FileCheck %s --check-prefix=VS-ANALYSIS
+; RUN: opt -S -dxil-op-lower %t/vertex.ll | FileCheck %s --check-prefix=VS-DXIL
+; RUN: opt -S -dxil-translate-metadata %t/vertex.ll | FileCheck %s --check-prefix=VS-MD
+; RUN: opt -disable-output -passes='print<dxil-signature>' %t/pixel.ll 2>&1 | FileCheck %s --check-prefix=PS-ANALYSIS
+; RUN: opt -S -dxil-op-lower %t/pixel.ll | FileCheck %s --check-prefix=PS-DXIL
+; RUN: opt -S -dxil-translate-metadata %t/pixel.ll | FileCheck %s --check-prefix=PS-MD
+
+; SV_CullDistance is a clip/cull element: it is written with storeOutput on
+; vertex outputs and read with loadInput on pixel inputs, sharing at most two
+; rows. Index 1 is a two-row float2 array and index 0 a float packed after it
+; in the first row. The container parts are covered by
+; ContainerData/SemanticSignatures-cull-distance.ll.
+
+;--- vertex.ll
+target triple = "dxil-pc-shadermodel6.0-vertex"
+
+define void @main() #0 {
+ %p = call float @llvm.dx.load.input.f32(i32 0, i32 0, i8 0, i32 poison)
+ call void @llvm.dx.store.output.f32(i32 0, i32 0, i8 0, float %p)
+ call void @llvm.dx.store.output.f32(i32 1, i32 0, i8 0, float 0.0)
+ call void @llvm.dx.store.output.f32(i32 1, i32 0, i8 1, float 0.0)
+ call void @llvm.dx.store.output.f32(i32 1, i32 1, i8 0, float 0.0)
+ call void @llvm.dx.store.output.f32(i32 1, i32 1, i8 1, float 0.0)
+ call void @llvm.dx.store.output.f32(i32 2, i32 0, i8 0, float 1.0)
+ ret void
+}
+attributes #0 = { "hlsl.shader"="vertex" }
+
+!dx.valver = !{!10}
+!10 = !{i32 1, i32 8}
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, !2}
+!1 = !{!3}
+!2 = !{!4, !5, !6}
+!3 = !{i32 0, !"POSITION", i32 9, i32 0, !7, i32 0, i32 1, i8 4, i32 0, i8 0, i8 0, i8 0, i32 0}
+!4 = !{i32 0, !"SV_Position", i32 9, i32 3, !7, i32 4, i32 1, i8 4, i32 0, i8 0, i8 0, i8 0, i32 0}
+!5 = !{i32 1, !"SV_CullDistance", i32 9, i32 7, !8, i32 2, i32 2, i8 2, i32 1, i8 0, i8 0, i8 0, i32 0}
+!6 = !{i32 2, !"SV_CullDistance", i32 9, i32 7, !7, i32 2, i32 1, i8 1, i32 1, i8 2, i8 0, i8 0, i32 0}
+!7 = !{i32 0}
+!8 = !{i32 1, i32 2}
+
+; VS-ANALYSIS: Outputs: 3 elements, 3 vectors
+; VS-ANALYSIS-NEXT: 0: SV_Position rows=1 cols=4 at 0:0 usage=1 dynamic=0
+; VS-ANALYSIS-NEXT: 1: SV_CullDistance rows=2 cols=2 at 1:0 usage=3 dynamic=0
+; VS-ANALYSIS-NEXT: 2: SV_CullDistance rows=1 cols=1 at 1:2 usage=4 dynamic=0
+
+; VS-DXIL: call void @dx.op.storeOutput.f32(i32 5, i32 1, i32 0, i8 0, float 0.000000e+00)
+; VS-DXIL: call void @dx.op.storeOutput.f32(i32 5, i32 1, i32 0, i8 1, float 0.000000e+00)
+; VS-DXIL: call void @dx.op.storeOutput.f32(i32 5, i32 1, i32 1, i8 0, float 0.000000e+00)
+; VS-DXIL: call void @dx.op.storeOutput.f32(i32 5, i32 1, i32 1, i8 1, float 0.000000e+00)
+; VS-DXIL: call void @dx.op.storeOutput.f32(i32 5, i32 2, i32 0, i8 0, float 1.000000e+00)
+
+; The operands are SigId, Name, CompType, SemanticKind, SemanticIndices,
+; Interpolation, Rows, Cols, StartRow, StartCol and properties: float (9)
+; CullDistance (7) elements with linear (2) interpolation.
+; VS-MD-DAG: !{i32 1, !"SV_CullDistance", i8 9, i8 7, ![[#IDX12:]], i8 2, i32 2, i8 2, i32 1, i8 0, !{{[0-9]+}}}
+; VS-MD-DAG: ![[#IDX12]] = !{i32 1, i32 2}
+; VS-MD-DAG: !{i32 2, !"SV_CullDistance", i8 9, i8 7, !{{[0-9]+}}, i8 2, i32 1, i8 1, i32 1, i8 2, !{{[0-9]+}}}
+
+;--- pixel.ll
+target triple = "dxil-pc-shadermodel6.0-pixel"
+
+define void @main() #0 {
+ %c = call float @llvm.dx.load.input.f32(i32 1, i32 1, i8 1, i32 poison)
+ call void @llvm.dx.store.output.f32(i32 0, i32 0, i8 0, float %c)
+ ret void
+}
+attributes #0 = { "hlsl.shader"="pixel" }
+
+!dx.valver = !{!10}
+!10 = !{i32 1, i32 8}
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, !1, !2}
+!1 = !{!3, !4}
+!2 = !{!5}
+!3 = !{i32 0, !"SV_Position", i32 9, i32 3, !7, i32 4, i32 1, i8 4, i32 0, i8 0, i8 0, i8 0, i32 0}
+!4 = !{i32 1, !"SV_CullDistance", i32 9, i32 7, !8, i32 2, i32 2, i8 2, i32 1, i8 0, i8 0, i8 0, i32 0}
+!5 = !{i32 0, !"SV_Target", i32 9, i32 16, !7, i32 0, i32 1, i8 1, i32 0, i8 0, i8 0, i8 0, i32 0}
+!7 = !{i32 0}
+!8 = !{i32 1, i32 2}
+
+; Only the second column of the array is read.
+; PS-ANALYSIS: Inputs: 2 elements, 3 vectors
+; PS-ANALYSIS-NEXT: 0: SV_Position rows=1 cols=4 at 0:0 usage=0 dynamic=0
+; PS-ANALYSIS-NEXT: 1: SV_CullDistance rows=2 cols=2 at 1:0 usage=2 dynamic=0
+
+; PS-DXIL: call float @dx.op.loadInput.f32(i32 4, i32 1, i32 1, i8 1, i32 poison)
+
+; PS-MD-DAG: !{i32 1, !"SV_CullDistance", i8 9, i8 7, !{{[0-9]+}}, i8 2, i32 2, i8 2, i32 1, i8 0, !{{[0-9]+}}}
diff --git a/llvm/test/CodeGen/SPIRV/semantics/cull-distance.ll b/llvm/test/CodeGen/SPIRV/semantics/cull-distance.ll
new file mode 100644
index 00000000000000..b1ed105971523f
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/semantics/cull-distance.ll
@@ -0,0 +1,63 @@
+; RUN: split-file %s %t
+; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv-unknown-vulkan1.3-vertex %t/vertex.ll -o - | FileCheck %s --check-prefix=VS
+; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv-unknown-vulkan1.3-pixel %t/pixel.ll -o - | FileCheck %s --check-prefix=PS --implicit-check-not=OpExtension
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-vulkan1.3-vertex %t/vertex.ll -o - -filetype=obj | spirv-val --target-env vulkan1.3 %}
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-vulkan1.3-pixel %t/pixel.ll -o - -filetype=obj | spirv-val --target-env vulkan1.3 %}
+
+; SV_CullDistance is lowered to the CullDistance built-in, a float array that
+; requires the CullDistance capability. The backend prints the built-in as
+; CullDistanceVariable to keep it distinct from the capability.
+
+; VS-DAG: OpCapability CullDistance
+; VS-DAG: OpDecorate %[[#CULL:]] BuiltIn CullDistanceVariable
+; VS-DAG: %[[#FLOAT:]] = OpTypeFloat 32
+; VS-DAG: %[[#ARRAY:]] = OpTypeArray %[[#FLOAT]] %[[#]]
+; VS-DAG: %[[#PTR:]] = OpTypePointer Output %[[#ARRAY]]
+; VS-DAG: %[[#CULL]] = OpVariable %[[#PTR]] Output
+; VS: OpInBoundsAccessChain %[[#]] %[[#CULL]] %[[#]]
+
+; PS-DAG: OpCapability CullDistance
+; PS-DAG: OpDecorate %[[#CULL:]] BuiltIn CullDistanceVariable
+; PS-DAG: %[[#FLOAT:]] = OpTypeFloat 32
+; PS-DAG: %[[#ARRAY:]] = OpTypeArray %[[#FLOAT]] %[[#]]
+; PS-DAG: %[[#PTR:]] = OpTypePointer Input %[[#ARRAY]]
+; PS-DAG: %[[#CULL]] = OpVariable %[[#PTR]] Input
+; PS: OpInBoundsAccessChain %[[#]] %[[#CULL]] %[[#]]
+
+;--- vertex.ll
+ at SV_Position = external hidden thread_local addrspace(8) global <4 x float>, !spirv.Decorations !0
+ at SV_CullDistance = external hidden thread_local addrspace(8) global [2 x float], !spirv.Decorations !2
+
+define void @main() #0 {
+entry:
+ store <4 x float> zeroinitializer, ptr addrspace(8) @SV_Position, align 16
+ %0 = call ptr addrspace(8) (ptr addrspace(8), ...) @llvm.structured.gep.p8(ptr addrspace(8) elementtype([2 x float]) @SV_CullDistance, i32 1)
+ store float 1.0, ptr addrspace(8) %0, align 4
+ ret void
+}
+
+attributes #0 = { "hlsl.shader"="vertex" }
+
+!0 = !{!1}
+!1 = !{i32 11, i32 0}
+!2 = !{!3}
+!3 = !{i32 11, i32 4}
+
+;--- pixel.ll
+ at SV_CullDistance = external hidden thread_local addrspace(7) externally_initialized constant [2 x float], !spirv.Decorations !0
+ at SV_Target0 = external hidden thread_local addrspace(8) global float, !spirv.Decorations !2
+
+define void @main() #0 {
+entry:
+ %0 = call ptr addrspace(7) (ptr addrspace(7), ...) @llvm.structured.gep.p7(ptr addrspace(7) elementtype([2 x float]) @SV_CullDistance, i32 1)
+ %1 = load float, ptr addrspace(7) %0, align 4
+ store float %1, ptr addrspace(8) @SV_Target0, align 4
+ ret void
+}
+
+attributes #0 = { "hlsl.shader"="pixel" }
+
+!0 = !{!1}
+!1 = !{i32 11, i32 4}
+!2 = !{!3}
+!3 = !{i32 30, i32 0}
diff --git a/llvm/unittests/Frontend/HLSLSemanticStagesTest.cpp b/llvm/unittests/Frontend/HLSLSemanticStagesTest.cpp
index 33f87cbae90111..620f6ff190065e 100644
--- a/llvm/unittests/Frontend/HLSLSemanticStagesTest.cpp
+++ b/llvm/unittests/Frontend/HLSLSemanticStagesTest.cpp
@@ -280,6 +280,29 @@ TEST(HLSLSemanticStagesTest, CoverageSignaturePoints) {
expectOnlySignaturePoints(SemanticKind::Coverage, Valid);
}
+// SV_CullDistance is written by the stages before the rasterizer and read by
+// the pixel shader, with the clip/cull packing rules. A vertex input or patch
+// constant with this name is an arbitrary semantic.
+TEST(HLSLSemanticStagesTest, CullDistanceSignaturePoints) {
+ constexpr SemanticInterpretation ClipCull = SemanticInterpretation::ClipCull;
+ constexpr SemanticInterpretation Arb = SemanticInterpretation::Arbitrary;
+ const SignaturePoint Valid[] = {
+ {Triple::Vertex, IOType::In, Arb},
+ {Triple::Vertex, IOType::Out, ClipCull},
+ {Triple::Hull, IOType::In, ClipCull},
+ {Triple::Hull, IOType::Out, ClipCull},
+ {Triple::Hull, IOType::PatchConstantOrPrimitive, Arb},
+ {Triple::Domain, IOType::In, ClipCull},
+ {Triple::Domain, IOType::Out, ClipCull},
+ {Triple::Domain, IOType::PatchConstantOrPrimitive, Arb},
+ {Triple::Geometry, IOType::In, ClipCull},
+ {Triple::Geometry, IOType::Out, ClipCull},
+ {Triple::Pixel, IOType::In, ClipCull},
+ {Triple::Mesh, IOType::Out, ClipCull},
+ };
+ expectOnlySignaturePoints(SemanticKind::CullDistance, Valid);
+}
+
// SV_RenderTargetArrayIndex is written by the vertex shader and read by the
// pixel shader. A vertex input with this name is an arbitrary semantic.
TEST(HLSLSemanticStagesTest, RenderTargetArrayIndexSignaturePoints) {
More information about the cfe-commits
mailing list