llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir

Author: Finn Plummer (inbelic)

<details>
<summary>Changes</summary>

Resolves: https://github.com/llvm/llvm-project/issues/212566

Assisted by: Claude Opus 5.5

---

Patch is 60.19 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/236259.diff


30 Files Affected:

- (modified) clang/lib/CodeGen/CGHLSLRuntime.cpp (+58-6) 
- (modified) clang/lib/Sema/SemaHLSL.cpp (+11-2) 
- (added) clang/test/CodeGenHLSL/semantics/SV_SampleIndex.ps.hlsl (+51) 
- (added) clang/test/CodeGenHLSL/semantics/SV_ViewID.hlsl (+36) 
- (added) clang/test/SemaHLSL/Semantics/sampleindex.ps.hlsl (+57) 
- (added) clang/test/SemaHLSL/Semantics/sampleindex.stages.hlsl (+23) 
- (added) clang/test/SemaHLSL/Semantics/viewid.model.hlsl (+14) 
- (added) clang/test/SemaHLSL/Semantics/viewid.ps.hlsl (+49) 
- (added) clang/test/SemaHLSL/Semantics/viewid.stages.hlsl (+34) 
- (modified) llvm/include/llvm/Frontend/HLSL/SemanticSignatures.h (+2) 
- (modified) llvm/include/llvm/IR/IntrinsicsDirectX.td (+6) 
- (modified) llvm/lib/Frontend/HLSL/SemanticSignatures.cpp (+6-3) 
- (modified) llvm/lib/Target/DirectX/DXContainerGlobals.cpp (+8-2) 
- (modified) llvm/lib/Target/DirectX/DXIL.td (+19) 
- (modified) llvm/lib/Target/DirectX/DXILShaderFlags.cpp (+8-3) 
- (modified) llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp (+47-5) 
- (modified) llvm/lib/Target/DirectX/DXILSignatureAnalysis.h (+15-8) 
- (modified) llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp (+2-1) 
- (modified) llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp (+5-1) 
- (added) 
llvm/test/CodeGen/DirectX/ContainerData/SemanticSignatures-sample-index.ll 
(+62) 
- (added) llvm/test/CodeGen/DirectX/ContainerData/SemanticSignatures-view-id.ll 
(+47) 
- (added) 
llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-sample-index-errors.ll 
(+78) 
- (added) llvm/test/CodeGen/DirectX/ShaderFlags/unpacked-low-precision.ll (+40) 
- (added) llvm/test/CodeGen/DirectX/ShaderFlags/view-id.ll (+23) 
- (added) llvm/test/CodeGen/DirectX/sample_index.ll (+28) 
- (added) llvm/test/CodeGen/DirectX/view_id.ll (+47) 
- (added) llvm/test/CodeGen/SPIRV/semantics/sample-index.ps.ll (+32) 
- (added) llvm/test/CodeGen/SPIRV/semantics/view-index.ps.ll (+42) 
- (modified) llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp (+15) 
- (modified) llvm/unittests/Frontend/HLSLSemanticStagesTest.cpp (+33-1) 


``````````diff
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 6dfec8cd934580..981ec3e9c790b3 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -1183,14 +1183,19 @@ static llvm::Value *createSPIRVInputLoad(
     IRBuilder<> &B, llvm::Module &M, llvm::Type *Ty, const Twine &Name,
     llvm::hlsl::SPIRVInterfaceDecoration Decoration, unsigned Operand,
     llvm::hlsl::InterpolationMode InterpMode) {
+  llvm::Type *LoadTy = Ty;
+  if (Decoration == llvm::hlsl::SPIRVInterfaceDecoration::BuiltIn &&
+      Ty->isIntegerTy() && Ty->getIntegerBitWidth() < 32)
+    LoadTy = B.getInt32Ty();
   auto *GV = new llvm::GlobalVariable(
-      M, Ty, /* isConstant= */ true, llvm::GlobalValue::ExternalLinkage,
+      M, LoadTy, /* isConstant= */ true, llvm::GlobalValue::ExternalLinkage,
       /* Initializer= */ nullptr, Name, /* insertBefore= */ nullptr,
       llvm::GlobalVariable::GeneralDynamicTLSModel,
       /* AddressSpace */ 7, /* isExternallyInitialized= */ true);
   GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
   addSPIRVInterfaceDecorations(GV, Decoration, Operand, InterpMode);
-  return B.CreateLoad(Ty, GV);
+  llvm::Value *Value = B.CreateLoad(LoadTy, GV);
+  return LoadTy == Ty ? Value : B.CreateTrunc(Value, Ty);
 }
 
 llvm::Value *CGHLSLRuntime::emitSPIRVUserSemanticLoad(
@@ -1345,13 +1350,24 @@ static llvm::hlsl::SemanticSignatureElement 
createSemanticSignatureElement(
   for (uint32_t I = 0, E = Shape.getNumRows(); I < E; ++I)
     SemanticIndices.push_back(FirstSemanticIndex + I);
 
+  llvm::dxbc::PSV::SemanticKind Kind = llvm::hlsl::getSemanticKind(Name);
+  llvm::dxil::ElementType CompType =
+      getSignatureComponentType(CGM, Shape.RowType);
+  // These system values may be declared as int or uint, but DXIL signatures
+  // require an unsigned component type.
+  if (Kind == llvm::dxbc::PSV::SemanticKind::SampleIndex) {
+    if (CompType == llvm::dxil::ElementType::I16)
+      CompType = llvm::dxil::ElementType::U16;
+    else if (CompType == llvm::dxil::ElementType::I32)
+      CompType = llvm::dxil::ElementType::U32;
+  }
+
   // The remaining members keep their default value and will be filled at a
   // later stage, either during packing or analysis of usage. Interpolation
   // is set by the load/store traversal after visiting each leaf.
-  return llvm::hlsl::SemanticSignatureElement(
-      SigId, Name, getSignatureComponentType(CGM, Shape.RowType),
-      llvm::hlsl::getSemanticKind(Name), SemanticIndices,
-      static_cast<uint8_t>(Shape.Cols));
+  return llvm::hlsl::SemanticSignatureElement(SigId, Name, CompType, Kind,
+                                              SemanticIndices,
+                                              
static_cast<uint8_t>(Shape.Cols));
 }
 
 llvm::Value *CGHLSLRuntime::emitDXILUserSemanticLoad(
@@ -1687,6 +1703,42 @@ llvm::Value *CGHLSLRuntime::emitSystemSemanticLoad(
       return B.CreateCall(
           CGM.getIntrinsic(llvm::Intrinsic::dx_start_vertex_location));
     break;
+  case llvm::dxbc::PSV::SemanticKind::SampleIndex:
+    assert(Stage == llvm::Triple::Pixel &&
+           "SV_SampleIndex is in an unavailable stage and should have been "
+           "diagnosed by Sema");
+    if (CGM.getTarget().getTriple().isSPIRV())
+      return createSPIRVInputLoad(B, CGM.getModule(), Type,
+                                  Semantic->getAttrName()->getName(),
+                                  
llvm::hlsl::SPIRVInterfaceDecoration::BuiltIn,
+                                  /* SampleId */ 18, InterpMode);
+    if (CGM.getTarget().getTriple().isDXIL()) {
+      // The sample index is read with a dedicated operation, but an unpacked
+      // element is still added to the input signature for compatibility.
+      SemanticShape Shape =
+          getSemanticShape(CGM.getContext(), getSemanticLeafType(Decl));
+      Signature.push_back(createSemanticSignatureElement(
+          CGM, Signature.size(), Semantic, Index, Shape));
+      llvm::Value *SampleIndex =
+          B.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::dx_sample_index));
+      return B.CreateTrunc(SampleIndex, Type);
+    }
+    break;
+  case llvm::dxbc::PSV::SemanticKind::ViewID:
+    assert(llvm::is_contained({llvm::Triple::Vertex, llvm::Triple::Hull,
+                               llvm::Triple::Domain, llvm::Triple::Geometry,
+                               llvm::Triple::Pixel, llvm::Triple::Mesh},
+                              Stage) &&
+           "SV_ViewID is in an unavailable stage and should have been "
+           "diagnosed by Sema");
+    if (CGM.getTarget().getTriple().isSPIRV())
+      return createSPIRVInputLoad(B, CGM.getModule(), Type,
+                                  Semantic->getAttrName()->getName(),
+                                  
llvm::hlsl::SPIRVInterfaceDecoration::BuiltIn,
+                                  /* ViewIndex */ 4440, InterpMode);
+    if (CGM.getTarget().getTriple().isDXIL())
+      return B.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::dx_view_id));
+    break;
   case llvm::dxbc::PSV::SemanticKind::StartInstanceLocation:
     assert(Stage == llvm::Triple::Vertex &&
            "SV_StartInstanceLocation is in an unavailable stage and should "
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index e42f9bb21ff70f..708f74d71b0e3c 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -1015,10 +1015,13 @@ static bool isVkPipelineBuiltin(const ASTContext 
&AstContext, FunctionDecl *FD,
     return (ST == llvm::Triple::Pixel && IsInput) ||
            (ST == llvm::Triple::Vertex && !IsInput);
   case SemanticKind::Barycentrics:
+  case SemanticKind::SampleIndex:
     return ST == llvm::Triple::Pixel && IsInput;
   case SemanticKind::StartVertexLocation:
   case SemanticKind::StartInstanceLocation:
     return ST == llvm::Triple::Vertex && IsInput;
+  case SemanticKind::ViewID:
+    return IsInput;
   default:
     return false;
   }
@@ -1279,6 +1282,7 @@ void SemaHLSL::diagnoseSemanticShaderModel(const 
HLSLAppliedSemanticAttr *A,
 
   switch (Kind) {
   case SemanticKind::Barycentrics:
+  case SemanticKind::ViewID:
     if (Triple.getOSVersion() < VersionTuple(6, 1))
       Diag(A->getLoc(), diag::err_hlsl_semantic_in_wrong_shader_model)
           << A->getAttrName() << "6.1";
@@ -1411,6 +1415,7 @@ void SemaHLSL::diagnoseSemanticType(const Decl *D,
     return;
   case SemanticKind::StartVertexLocation:
   case SemanticKind::StartInstanceLocation:
+  case SemanticKind::ViewID:
     if (DeclaredTy != T || !T->isScalarType() ||
         !isIntElementOfWidth(Ctx, T, 32))
       Diag(A->getLoc(), diag::err_hlsl_semantic_invalid_type)
@@ -1436,6 +1441,12 @@ 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::SampleIndex:
+    if (DeclaredTy != T || !T->isScalarType() || !isIntUpTo32Element(Ctx, T))
+      Diag(A->getLoc(), diag::err_hlsl_semantic_invalid_type)
+          << A->getAttrName() << /* scalar */ 0 << 1
+          << /* 16 or 32 bit integer */ 0 << DeclaredTy;
+    return;
   case SemanticKind::InstanceID:
   case SemanticKind::ShadingRate:
     // DXIL permits U32 or U16. SPIR-V requires a 32-bit scalar per:
@@ -2253,7 +2264,6 @@ void SemaHLSL::handleSemanticAttr(Decl *D, const 
ParsedAttr &AL) {
   case SemanticKind::DomainLocation:
   case SemanticKind::PrimitiveID:
   case SemanticKind::GSInstanceID:
-  case SemanticKind::SampleIndex:
   case SemanticKind::IsFrontFace:
   case SemanticKind::Coverage:
   case SemanticKind::InnerCoverage:
@@ -2263,7 +2273,6 @@ void SemaHLSL::handleSemanticAttr(Decl *D, const 
ParsedAttr &AL) {
   case SemanticKind::StencilRef:
   case SemanticKind::TessFactor:
   case SemanticKind::InsideTessFactor:
-  case SemanticKind::ViewID:
   case SemanticKind::CullPrimitive:
     Diag(AL.getLoc(), diag::err_hlsl_unknown_semantic) << AL;
     return;
diff --git a/clang/test/CodeGenHLSL/semantics/SV_SampleIndex.ps.hlsl 
b/clang/test/CodeGenHLSL/semantics/SV_SampleIndex.ps.hlsl
new file mode 100644
index 00000000000000..ebbaf33f79e218
--- /dev/null
+++ b/clang/test/CodeGenHLSL/semantics/SV_SampleIndex.ps.hlsl
@@ -0,0 +1,51 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-pixel -x hlsl -emit-llvm 
-finclude-default-header -disable-llvm-passes -o - %s | FileCheck 
--check-prefixes=CHECK-DXIL,DXIL32 %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-pixel -x hlsl -emit-llvm 
-finclude-default-header -fnative-half-type -fnative-int16-type 
-disable-llvm-passes -DINT16 -o - %s | FileCheck 
--check-prefixes=CHECK-DXIL,DXIL16 %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm 
-finclude-default-header -disable-llvm-passes -o - %s | FileCheck 
--check-prefix=CHECK-SPIRV %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm 
-finclude-default-header -fnative-half-type -fnative-int16-type 
-disable-llvm-passes -DINT16 -o - %s | FileCheck --check-prefix=SPIRV16 %s
+
+#ifdef INT16
+#define TYPE int16_t
+#else
+#define TYPE int
+#endif
+
+// CHECK-SPIRV: @SV_SampleIndex = external hidden thread_local addrspace(7) 
externally_initialized constant i32, !spirv.Decorations ![[#MD_0:]]
+
+// SV_SampleIndex is read with a dedicated operation. It still adds an unpacked
+// element to the input signature that does not consume a row, so the user
+// input that follows it is packed at row 0.
+
+// CHECK-DXIL-LABEL: define void @main()
+// CHECK-DXIL: %[[SI:.*]] = call i32 @llvm.dx.sample.index()
+// DXIL32-NOT: trunc
+// DXIL16: %[[SI16:.*]] = trunc i32 %[[SI]] to i16
+// CHECK-DXIL: %[[A:.*]] = call float @llvm.dx.load.input.f32(i32 1, i32 0, i8 
0, i32 poison)
+// DXIL32: call float @_Z4mainif(i32 %[[SI]], float %[[A]])
+// DXIL16: call float @_Z4mainsf(i16 %[[SI16]], float %[[A]])
+
+// Signed declarations use an unsigned DXIL component type, with 
nointerpolation.
+// DXIL32-DAG: !{i32 0, !"SV_SampleIndex", i32 5, i32 12, ![[#ZERO:]], i32 1, 
i32 1, i8 1, i32 -1, i8 -1, i8 0, i8 0, i32 0}
+// DXIL16-DAG: !{i32 0, !"SV_SampleIndex", i32 3, i32 12, ![[#ZERO:]], i32 1, 
i32 1, i8 1, i32 -1, i8 -1, i8 0, i8 0, i32 0}
+// CHECK-DXIL-DAG: !{i32 1, !"A", i32 9, i32 0, ![[#ZERO]], i32 2, i32 1, i8 
1, i32 0, i8 0, i8 0, i8 0, i32 0}
+// CHECK-DXIL-DAG: ![[#ZERO]] = !{i32 0}
+
+// CHECK-SPIRV-LABEL: define void @main()
+// CHECK-SPIRV: %[[SI:.*]] = load i32, ptr addrspace(7) @SV_SampleIndex, align 
4
+// CHECK-SPIRV: call spir_func float @_Z4mainif(i32 %[[SI]],
+float main(TYPE s : SV_SampleIndex, float a : A) : SV_Target {
+  return a * s;
+}
+
+// CHECK-SPIRV-DAG: ![[#MD_0]] = !{![[#BUILTIN:]], ![[#FLAT:]]}
+// The metadata below contains SPIR-V decoration 'BuiltIn' followed by
+// BuiltIn SampleId.
+// CHECK-SPIRV-DAG: ![[#BUILTIN]] = !{i32 11, i32 18}
+// CHECK-SPIRV-DAG: ![[#FLAT]] = !{i32 14}
+
+// The SPIR-V built-in is a 32-bit integer, so a 16-bit declaration is
+// truncated.
+// SPIRV16: @SV_SampleIndex = {{.*}} addrspace(7) externally_initialized 
constant i32,
+// SPIRV16-LABEL: define void @main()
+// SPIRV16: %[[V32:.*]] = load i32, ptr addrspace(7) @SV_SampleIndex
+// SPIRV16: %[[V16:.*]] = trunc i32 %[[V32]] to i16
+// SPIRV16: call spir_func float @_Z4mainsf(i16 %[[V16]],
diff --git a/clang/test/CodeGenHLSL/semantics/SV_ViewID.hlsl 
b/clang/test/CodeGenHLSL/semantics/SV_ViewID.hlsl
new file mode 100644
index 00000000000000..71be4e2e6e00f4
--- /dev/null
+++ b/clang/test/CodeGenHLSL/semantics/SV_ViewID.hlsl
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.1-vertex -x hlsl -emit-llvm 
-finclude-default-header -disable-llvm-passes -o - %s | FileCheck 
--check-prefix=CHECK-DXIL %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.1-pixel -x hlsl -emit-llvm 
-finclude-default-header -disable-llvm-passes -o - %s | FileCheck 
--check-prefix=CHECK-DXIL %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-vertex -x hlsl -emit-llvm 
-finclude-default-header -disable-llvm-passes -o - %s | FileCheck 
--check-prefixes=CHECK-SPIRV,SPIRV-VS %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm 
-finclude-default-header -disable-llvm-passes -o - %s | FileCheck 
--check-prefixes=CHECK-SPIRV,SPIRV-PS %s
+
+#if __SHADER_TARGET_STAGE == __SHADER_STAGE_PIXEL
+#define OUTPUT SV_Target
+#else
+#define OUTPUT SV_Position
+#endif
+
+// CHECK-SPIRV: @SV_ViewID = external hidden thread_local addrspace(7) 
externally_initialized constant i32, !spirv.Decorations ![[#MD_0:]]
+
+// The view ID is read with a dedicated operation and is not part of the input
+// signature, so the user input that follows keeps signature element 0.
+
+// CHECK-DXIL-LABEL: define void @main()
+// CHECK-DXIL: %[[VIEW:.*]] = call i32 @llvm.dx.view.id()
+// CHECK-DXIL: %[[A:.*]] = call <4 x float> @llvm.dx.load.input.v4f32(i32 0, 
i32 0, i8 0, i32 poison)
+// CHECK-DXIL: call <4 x float> @_Z4mainjDv4_f(i32 %[[VIEW]], <4 x float> 
%[[A]])
+// CHECK-DXIL-NOT: !"SV_ViewID"
+
+// CHECK-SPIRV-LABEL: define void @main()
+// CHECK-SPIRV: %[[VIEW:.*]] = load i32, ptr addrspace(7) @SV_ViewID, align 4
+// CHECK-SPIRV: call spir_func <4 x float> @_Z4mainjDv4_f(i32 %[[VIEW]],
+float4 main(uint view : SV_ViewID, float4 a : A) : OUTPUT {
+  return a * view;
+}
+
+// The pixel shader input is flat because it is an integer.
+// SPIRV-VS-DAG: ![[#MD_0]] = !{![[#BUILTIN:]]}
+// SPIRV-PS-DAG: ![[#MD_0]] = !{![[#BUILTIN:]], ![[#FLAT:]]}
+// SPIRV-PS-DAG: ![[#FLAT]] = !{i32 14}
+// The metadata below contains SPIR-V decoration 'BuiltIn' followed by
+// BuiltIn ViewIndex.
+// CHECK-SPIRV-DAG: ![[#BUILTIN]] = !{i32 11, i32 4440}
diff --git a/clang/test/SemaHLSL/Semantics/sampleindex.ps.hlsl 
b/clang/test/SemaHLSL/Semantics/sampleindex.ps.hlsl
new file mode 100644
index 00000000000000..520893b5c4d9e1
--- /dev/null
+++ b/clang/test/SemaHLSL/Semantics/sampleindex.ps.hlsl
@@ -0,0 +1,57 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-library -fnative-half-type 
-fnative-int16-type -finclude-default-header -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -fnative-half-type 
-fnative-int16-type -finclude-default-header -fsyntax-only -verify %s
+
+// Type, index and interpolation checks for SV_SampleIndex. These are
+// independent of the shader stage and IO direction, which are covered by
+// sampleindex.stages.hlsl.
+
+[shader("pixel")]
+void valid_uint(uint s : SV_SampleIndex) {}
+[shader("pixel")]
+void valid_int(int s : sv_sampleindex0) {}
+[shader("pixel")]
+void valid_flat(nointerpolation uint s : SV_SampleIndex) {}
+
+// 16-bit integers are allowed. SPIR-V converts them to and from the 32-bit
+// built-in.
+[shader("pixel")]
+void uint16(uint16_t s : SV_SampleIndex) {}
+[shader("pixel")]
+void int16(int16_t s : SV_SampleIndex) {}
+
+struct Input {
+  float4 p : SV_Position;
+  uint s : SV_SampleIndex;
+};
+[shader("pixel")]
+void valid_struct(Input i) {}
+
+[shader("pixel")]
+void bad_float(float s : SV_SampleIndex) {}
+// expected-error@-1 {{semantic 'SV_SampleIndex' must be a scalar of 16 or 32 
bit integer type (was 'float')}}
+[shader("pixel")]
+void bad_bool(bool s : SV_SampleIndex) {}
+// expected-error@-1 {{semantic 'SV_SampleIndex' must be a scalar of 16 or 32 
bit integer type (was 'bool')}}
+[shader("pixel")]
+void bad_int64(uint64_t s : SV_SampleIndex) {}
+// expected-error@-1 {{semantic 'SV_SampleIndex' must be a scalar of 16 or 32 
bit integer type (was 'uint64_t' (aka 'unsigned long'))}}
+[shader("pixel")]
+void bad_vector(uint1 s : SV_SampleIndex) {}
+// expected-error@-1 {{semantic 'SV_SampleIndex' must be a scalar of 16 or 32 
bit integer type (was 'uint1' (aka 'vector<uint, 1>'))}}
+[shader("pixel")]
+void bad_array(uint s[1] : SV_SampleIndex) {}
+// expected-error@-1 {{semantic 'SV_SampleIndex' must be a scalar of 16 or 32 
bit integer type (was 'uint[1]'}}
+
+[shader("pixel")]
+void bad_index(uint s : SV_SampleIndex1) {}
+// expected-error@-1 {{semantic 'SV_SampleIndex' does not allow indexing}}
+
+[shader("pixel")]
+void same_index(uint s : SV_SampleIndex, // expected-note {{previous use is 
here}}
+                uint t : SV_SampleIndex0) {} // expected-note {{'t' declared 
here}}
+// expected-error@-1 {{semantic index overlap SV_SampleIndex0}}
+
+// Integer inputs can only use nointerpolation.
+[shader("pixel")]
+void bad_interpolation(sample uint s : SV_SampleIndex) {}
+// expected-error@-1 {{interpolation modifiers other than 'nointerpolation' 
cannot be used with type 'uint' (aka 'unsigned int')}}
diff --git a/clang/test/SemaHLSL/Semantics/sampleindex.stages.hlsl 
b/clang/test/SemaHLSL/Semantics/sampleindex.stages.hlsl
new file mode 100644
index 00000000000000..059328ccaf14ed
--- /dev/null
+++ b/clang/test/SemaHLSL/Semantics/sampleindex.stages.hlsl
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-library 
-finclude-default-header -x hlsl -verify -o - %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -finclude-default-header 
-x hlsl -verify -o - %s
+
+// Stage and IO direction checks for SV_SampleIndex: only a pixel input.
+// Type, index and interpolation checks are covered by sampleindex.ps.hlsl.
+
+[shader("pixel")]
+float4 ok_ps_in(uint s : SV_SampleIndex) : SV_Target {
+  return float4((float)s, 0, 0, 1);
+}
+
+[shader("pixel")]
+uint bad_ps_out() : SV_SampleIndex { return 0; }
+// expected-error@-1 {{semantic 'SV_SampleIndex' is not supported in pixel 
shader outputs}}
+
+[shader("vertex")]
+float4 bad_vs_in(uint s : SV_SampleIndex) : SV_Position { return 0; }
+// expected-error@-1 {{semantic 'SV_SampleIndex' is not supported in vertex 
shader inputs}}
+
+[shader("compute")]
+[numthreads(1, 1, 1)]
+void bad_cs_in(uint s : SV_SampleIndex) {}
+// expected-error@-1 {{semantic 'SV_SampleIndex' is not supported in compute 
shader inputs}}
diff --git a/clang/test/SemaHLSL/Semantics/viewid.model.hlsl 
b/clang/test/SemaHLSL/Semantics/viewid.model.hlsl
new file mode 100644
index 00000000000000..e74f73c43086bd
--- /dev/null
+++ b/clang/test/SemaHLSL/Semantics/viewid.model.hlsl
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library 
-finclude-default-header -x hlsl -verify=dxil -o - %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.1-library 
-finclude-default-header -x hlsl -verify=ok -o - %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -finclude-default-header 
-x hlsl -verify=ok -o - %s
+
+// SV_ViewID requires shader model 6.1 for DXIL.
+// The shader model restriction does not apply to SPIR-V, which is only run to 
check that no error is emitted.
+
+[shader("pixel")]
+float4 ps_in(uint v : SV_ViewID) : SV_Target {
+// dxil-error@-1 {{semantic 'SV_ViewID' requires shader model 6.1 or greater}}
+  return float4((float)v, 0, 0, 1);
+}
+
+// ok-no-diagnostics
diff --git a/clang/test/SemaHLSL/Semantics/viewid.ps.hlsl 
b/clang/test/SemaHLSL/Semantics/viewid.ps.hlsl
new file mode 100644
index 00000000000000..2db5ee557495b5
--- /dev/null
+++ b/clang/test/SemaHLSL/Semantics/viewid.ps.hlsl
@@ -0,0 +1,49 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-library -fnative-half-type 
-fnative-int16-type -finclude-default-header -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -fnative-half-type 
-fnative-int16-type -finclude-default-header -fsyntax-only -verify %s
+
+// Type, index and interpolation checks for SV_ViewID. These are independent of
+// the shader stage and IO direction, which are covered by viewid.stages.hlsl.
+
+[shader("pixel")]
+void valid_uint(uint v : SV_ViewID) {}
+[shader("pixel")]
+void valid_int(int v : sv_viewid0) {}
+[shader("pixel")]
+void valid_flat(nointerpolation uint v : SV_ViewID) {}
+
+struct Input {
+  float4 p : SV_Position;
+  uint v : SV_ViewID;
+};
+[shader("pixel")]
+void valid_struct(Input i) {}
+
+[shader("pixel")]
+void bad_float(float v : SV_ViewID) {}
+// expected-error@-1 {{semantic 'SV_ViewID' must be a scalar of 32 bit integer 
type (was 'float')}}
+[shader("pixel")]
+void bad_bool(bool v : SV_ViewID) {}
+// expected-error@-1 {{semantic 'SV_ViewID' must be a scalar of 32 bit integer 
type (was 'bool')}}
+[shader("pixel")]
+void bad_uint16(uint16_t v : SV_ViewID) {}
+// expected-error@-1 {{semantic 'SV_ViewID' must be a scalar of 32 bit integer 
type (was 'uint16_t' (aka 'unsigned short'))}}
+[shader("pixel")]
+void bad_vector(uint2 v : SV_ViewID) {}
+// expected-error@-1 {{semantic 'SV_ViewID' m...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/236259
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to