https://github.com/inbelic updated 
https://github.com/llvm/llvm-project/pull/237027

>From 5ffeb222a3681f69513206cbefe28720b6470aef Mon Sep 17 00:00:00 2001
From: Finn Plummer <[email protected]>
Date: Thu, 29 Oct 2026 20:00:35 +0000
Subject: [PATCH 1/5] [HLSL] Complete SV_Target support

Implement the remaining SV_Target rules from the HLSL semantics overview
(wg-hlsl proposal 0040). SV_Target is a pixel shader output of up to 8
render targets, each a scalar or vector of up to four 16- or 32-bit floats,
where arrays are allowed and the semantic index must match the signature
row.

- In SPIR-V, the location of an SV_Target output is its semantic index, as in
  DXC, rather than the next free output location. An array covers the
  consecutive locations of its elements.
- DXIL pixel shader outputs are not indexable, and each render target must be
  in the row of its semantic index. An SV_Target array is therefore split into
  one single-row signature element per render target. This previously hit an
  assertion during signature packing.
- The DirectX backend validates that SV_Target elements use 16- or 32-bit
  floating-point components.

Signature elements now also take their semantic kind from the interpretation
of the semantic. A system-value name with the arbitrary interpretation, such
as SV_Position on a vertex input, has the arbitrary kind while keeping its
name. Only elements interpreted as a system value, such as SV_Target on a
pixel output, keep the system-value kind.
---
 clang/lib/CodeGen/CGHLSLRuntime.cpp           | 97 ++++++++++++++-----
 clang/lib/CodeGen/CGHLSLRuntime.h             |  6 +-
 .../semantics/SV_Target.index.ps.hlsl         | 64 ++++++++++++
 .../semantics/interpolation-aggregate.hlsl    |  4 +-
 .../semantic.arbitrary-system-value.hlsl      | 14 +++
 .../semantics/semantic.explicit-mix.lib.hlsl  |  5 +-
 .../test/SemaHLSL/Semantics/target.types.hlsl | 52 ++++++++++
 .../Target/DirectX/DXILSignatureAnalysis.cpp  |  5 +
 .../Metadata/semantic-signatures-errors.ll    | 13 +++
 9 files changed, 228 insertions(+), 32 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/semantics/SV_Target.index.ps.hlsl
 create mode 100644 
clang/test/CodeGenHLSL/semantics/semantic.arbitrary-system-value.hlsl
 create mode 100644 clang/test/SemaHLSL/Semantics/target.types.hlsl

diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 59f07e6d7df7a0..bf0e5c0d7dac7e 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -1333,19 +1333,24 @@ static void createSPIRVOutputStore(
 void CGHLSLRuntime::emitSPIRVUserSemanticStore(
     llvm::IRBuilder<> &B, llvm::Value *Source,
     const clang::DeclaratorDecl *Decl, HLSLAppliedSemanticAttr *Semantic,
-    std::optional<unsigned> Index, llvm::hlsl::InterpolationMode InterpMode) {
+    std::optional<unsigned> Index, llvm::hlsl::InterpolationMode InterpMode,
+    bool LocationFromIndex) {
   Twine BaseName = Twine(Semantic->getAttrName()->getName());
   Twine VariableName = BaseName.concat(Twine(Index.value_or(0)));
 
-  unsigned Location = SPIRVLastAssignedOutputSemanticLocation;
+  unsigned Location;
+  if (LocationFromIndex) {
+    Location = Index.value_or(0);
+  } else {
+    Location = SPIRVLastAssignedOutputSemanticLocation;
+    llvm::ArrayType *AT = dyn_cast<llvm::ArrayType>(Source->getType());
+    // In the case of an array, locations are assigned from the first semantic
+    // to the last.
+    SPIRVLastAssignedOutputSemanticLocation += AT ? AT->getNumElements() : 1;
+  }
   if (auto *L = Decl->getAttr<HLSLVkLocationAttr>())
     Location = L->getLocation();
 
-  // DXC completely ignores the semantic/index pair. Location are assigned from
-  // the first semantic to the last.
-  llvm::ArrayType *AT = dyn_cast<llvm::ArrayType>(Source->getType());
-  unsigned ElementCount = AT ? AT->getNumElements() : 1;
-  SPIRVLastAssignedOutputSemanticLocation += ElementCount;
   createSPIRVOutputStore(B, CGM.getModule(), Source, VariableName.str(),
                          llvm::hlsl::SPIRVInterfaceDecoration::Location,
                          Location, InterpMode);
@@ -1535,13 +1540,21 @@ void 
CGHLSLRuntime::emitDXILUserSemanticStore(llvm::IRBuilder<> &B,
                                               const clang::DeclaratorDecl 
*Decl,
                                               HLSLAppliedSemanticAttr 
*Semantic,
                                               std::optional<unsigned> Index,
-                                              SemanticSignatures &Signature) {
+                                              SemanticSignatures &Signature,
+                                              bool OneElementPerRow) {
   SemanticShape Shape =
       getSemanticShape(CGM.getContext(), getSemanticLeafType(Decl));
-
-  uint32_t SigId = Signature.size();
-  Signature.push_back(
-      createSemanticSignatureElement(CGM, SigId, Semantic, Index, Shape));
+  assert(!(OneElementPerRow && Shape.IsMatrix) &&
+         "matrices cannot be split into one element per row");
+
+  // DXIL pixel shader outputs are not indexable, and each render target must
+  // be in the signature row of its semantic index. An SV_Target array is
+  // therefore split into one single-row element per render target.
+  SemanticShape RowShape{{}, Shape.Cols, Shape.RowType};
+  uint32_t FirstSigId = Signature.size();
+  if (!OneElementPerRow)
+    Signature.push_back(createSemanticSignatureElement(CGM, FirstSigId,
+                                                       Semantic, Index, 
Shape));
 
   llvm::Type *RowTy = CGM.getTypes().ConvertTypeForMem(Shape.RowType);
 
@@ -1576,8 +1589,17 @@ void 
CGHLSLRuntime::emitDXILUserSemanticStore(llvm::IRBuilder<> &B,
       Val = B.CreateZExt(Val, RowTy, "storedv");
     }
 
+    uint32_t SigId = FirstSigId;
+    uint32_t SigRow = Row;
+    if (OneElementPerRow) {
+      SigId = Signature.size();
+      SigRow = 0;
+      Signature.push_back(createSemanticSignatureElement(
+          CGM, SigId, Semantic, Index.value_or(0) + Row, RowShape));
+    }
+
     std::array<Value *, 4> Args{/*SigElementId=*/B.getInt32(SigId),
-                                /*RowIndex=*/B.getInt32(Row),
+                                /*RowIndex=*/B.getInt32(SigRow),
                                 /*ColIndex=*/B.getInt8(0), /*Value=*/Val};
     B.CreateCall(IntrFn, Args, OB);
   }
@@ -1953,9 +1975,17 @@ void CGHLSLRuntime::emitSystemSemanticStore(
     assert(Stage == llvm::Triple::Pixel &&
            "SV_Target is in an unavailable stage and should have been "
            "diagnosed by Sema");
-    emitUserSemanticStore(B, Source, Decl, Semantic, Index, Signature,
-                          InterpMode);
-    return;
+    if (CGM.getTarget().getTriple().isDXIL()) {
+      emitDXILUserSemanticStore(B, Source, Decl, Semantic, Index, Signature,
+                                /*OneElementPerRow=*/true);
+      return;
+    }
+    if (CGM.getTarget().getTriple().isSPIRV()) {
+      emitSPIRVUserSemanticStore(B, Source, Decl, Semantic, Index, InterpMode,
+                                 /*LocationFromIndex=*/true);
+      return;
+    }
+    break;
   case llvm::dxbc::PSV::SemanticKind::ShadingRate:
     assert(llvm::is_contained({llvm::Triple::Vertex, llvm::Triple::Hull,
                                llvm::Triple::Domain, llvm::Triple::Geometry,
@@ -1990,6 +2020,16 @@ void CGHLSLRuntime::emitSystemSemanticStore(
       "Store hasn't been implemented yet for this system semantic. FIXME");
 }
 
+// A system-value name can have an arbitrary interpretation, for example
+// SV_Position on a vertex input. Its signature elements are then arbitrary,
+// although they keep the system-value name.
+static void markArbitrarySignatureElements(
+    SmallVectorImpl<llvm::hlsl::SemanticSignatureElement> &Signature,
+    size_t FirstElement) {
+  for (auto &Element : llvm::drop_begin(Signature, FirstElement))
+    Element.SemanticKind = llvm::dxbc::PSV::SemanticKind::Arbitrary;
+}
+
 llvm::Value *CGHLSLRuntime::handleScalarSemanticLoad(
     IRBuilder<> &B, const FunctionDecl *FD, llvm::Type *Type,
     const clang::DeclaratorDecl *Decl, HLSLAppliedSemanticAttr *Semantic,
@@ -2017,9 +2057,13 @@ llvm::Value *CGHLSLRuntime::handleScalarSemanticLoad(
   if (CGM.getTarget().getTriple().isSPIRV())
     InterpMode = getSPIRVInterpolationMode(
         InterpMode, Type, ShaderAttr->getType(), llvm::hlsl::IOType::In);
-  if (Interpretation == llvm::hlsl::SemanticInterpretation::Arbitrary)
-    return emitUserSemanticLoad(B, Type, Decl, Semantic, Index, Signature,
-                                InterpMode);
+  if (Interpretation == llvm::hlsl::SemanticInterpretation::Arbitrary) {
+    size_t FirstElement = Signature.size();
+    llvm::Value *Value = emitUserSemanticLoad(B, Type, Decl, Semantic, Index,
+                                              Signature, InterpMode);
+    markArbitrarySignatureElements(Signature, FirstElement);
+    return Value;
+  }
   return emitSystemSemanticLoad(B, Type, Decl, Semantic, SemanticKind,
                                 ShaderAttr->getType(), Index, Signature,
                                 InterpMode);
@@ -2054,9 +2098,13 @@ void CGHLSLRuntime::handleScalarSemanticStore(
                                            ShaderAttr->getType(),
                                            llvm::hlsl::IOType::Out);
 
-  if (Interpretation == llvm::hlsl::SemanticInterpretation::Arbitrary)
-    return emitUserSemanticStore(B, Source, Decl, Semantic, Index, Signature,
-                                 InterpMode);
+  if (Interpretation == llvm::hlsl::SemanticInterpretation::Arbitrary) {
+    size_t FirstElement = Signature.size();
+    emitUserSemanticStore(B, Source, Decl, Semantic, Index, Signature,
+                          InterpMode);
+    markArbitrarySignatureElements(Signature, FirstElement);
+    return;
+  }
   emitSystemSemanticStore(B, Source, Decl, Semantic, SemanticKind,
                           ShaderAttr->getType(), Index, Signature, InterpMode);
 }
@@ -2176,12 +2224,11 @@ CGHLSLRuntime::handleSemanticStore(
   size_t PreviousSize = Signature.size();
   auto InterpMode = llvm::hlsl::getInterpolationMode(Modifiers);
   handleScalarSemanticStore(B, FD, Source, Decl, Attr, Signature, InterpMode);
-  if (Signature.size() != PreviousSize) {
-    auto &Element = Signature.back();
+  // A leaf can add several elements, such as an SV_Target array.
+  for (auto &Element : llvm::drop_begin(Signature, PreviousSize))
     Element.InterpMode = llvm::hlsl::normalizeInterpolationMode(
         InterpMode, Element.CompType, Element.SemanticKind,
         FD->getAttr<HLSLShaderAttr>()->getType(), llvm::hlsl::IOType::Out);
-  }
   return AttrBegin;
 }
 
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h 
b/clang/lib/CodeGen/CGHLSLRuntime.h
index cda42c40bfe99d..269620fb79ad8d 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -400,12 +400,14 @@ class CGHLSLRuntime {
                                   const clang::DeclaratorDecl *Decl,
                                   HLSLAppliedSemanticAttr *Semantic,
                                   std::optional<unsigned> Index,
-                                  llvm::hlsl::InterpolationMode InterpMode);
+                                  llvm::hlsl::InterpolationMode InterpMode,
+                                  bool LocationFromIndex = false);
   void emitDXILUserSemanticStore(llvm::IRBuilder<> &B, llvm::Value *Source,
                                  const clang::DeclaratorDecl *Decl,
                                  HLSLAppliedSemanticAttr *Semantic,
                                  std::optional<unsigned> Index,
-                                 SemanticSignatures &Signature);
+                                 SemanticSignatures &Signature,
+                                 bool OneElementPerRow = false);
   void emitUserSemanticStore(llvm::IRBuilder<> &B, llvm::Value *Source,
                              const clang::DeclaratorDecl *Decl,
                              HLSLAppliedSemanticAttr *Semantic,
diff --git a/clang/test/CodeGenHLSL/semantics/SV_Target.index.ps.hlsl 
b/clang/test/CodeGenHLSL/semantics/SV_Target.index.ps.hlsl
new file mode 100644
index 00000000000000..45aa0263d13977
--- /dev/null
+++ b/clang/test/CodeGenHLSL/semantics/SV_Target.index.ps.hlsl
@@ -0,0 +1,64 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-pixel -x hlsl -emit-llvm 
-finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s 
--check-prefix=CHECK-DXIL
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-pixel -x hlsl -emit-llvm 
-finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s 
--check-prefix=CHECK-SPIRV
+
+// The semantic index of SV_Target selects the render target. An array covers
+// consecutive render targets.
+struct PSOut {
+  float4 Color : SV_Target3;
+  float4 Layers[2] : SV_Target1;
+};
+
+// In SPIR-V the location is the semantic index, not the next free location.
+// CHECK-SPIRV-DAG: @SV_Target3 = external hidden thread_local addrspace(8) 
global <4 x float>, !spirv.Decorations ![[#LOC3:]]
+// CHECK-SPIRV-DAG: @SV_Target1 = external hidden thread_local addrspace(8) 
global [2 x <4 x float>], !spirv.Decorations ![[#LOC1:]]
+
+// CHECK-DXIL-LABEL: define void @main()
+// CHECK-SPIRV-LABEL: define void @main()
+PSOut main(float4 P : SV_Position) {
+  // DXIL pixel outputs are not indexable, so the array is split into one
+  // element per render target.
+  // CHECK-DXIL: %[[RESULT:.*]] = load %struct.PSOut, ptr %{{.*}}, align 4
+  // CHECK-DXIL: %[[COLOR:.*]] = extractvalue %struct.PSOut %[[RESULT]], 0
+  // CHECK-DXIL: call void @llvm.dx.store.output.v4f32(i32 0, i32 0, i8 0, <4 
x float> %[[COLOR]])
+  // CHECK-DXIL: %[[LAYERS:.*]] = extractvalue %struct.PSOut %[[RESULT]], 1
+  // CHECK-DXIL: %[[LAYER0:.*]] = extractvalue [2 x <4 x float>] %[[LAYERS]], 0
+  // CHECK-DXIL: call void @llvm.dx.store.output.v4f32(i32 1, i32 0, i8 0, <4 
x float> %[[LAYER0]])
+  // CHECK-DXIL: %[[LAYER1:.*]] = extractvalue [2 x <4 x float>] %[[LAYERS]], 1
+  // CHECK-DXIL: call void @llvm.dx.store.output.v4f32(i32 2, i32 0, i8 0, <4 
x float> %[[LAYER1]])
+
+  // CHECK-SPIRV: %[[RESULT:.*]] = load %struct.PSOut, ptr %{{.*}}, align 4
+  // CHECK-SPIRV: %[[COLOR:.*]] = extractvalue %struct.PSOut %[[RESULT]], 0
+  // CHECK-SPIRV: store <4 x float> %[[COLOR]], ptr addrspace(8) @SV_Target3, 
align 4
+  // CHECK-SPIRV: %[[LAYERS:.*]] = extractvalue %struct.PSOut %[[RESULT]], 1
+  // CHECK-SPIRV: store [2 x <4 x float>] %[[LAYERS]], ptr addrspace(8) 
@SV_Target1, align 4
+  PSOut O;
+  O.Color = P;
+  O.Layers[0] = P * 2;
+  O.Layers[1] = P * 3;
+  return O;
+}
+
+// Each element has one row, at the row of its semantic index.
+// CHECK-DXIL: !dx.semantic.signatures = !{![[#SIG:]]}
+// CHECK-DXIL: ![[#SIG]] = !{ptr @main, ![[#IN:]], ![[#OUT:]]}
+// CHECK-DXIL: ![[#OUT]] = !{![[#T3:]], ![[#T1:]], ![[#T2:]]}
+// CHECK-DXIL: ![[#T3]] = !{i32 0, !"SV_Target", i32 9, i32 16, ![[#IDX3:]], 
i32 0, i32 1, i8 4, i32 3, i8 0, i8 0, i8 0, i32 0}
+//                                                 |      |                 |  
    |      |     |       `-> StartRow: semantic index
+//                                                 |      |                 |  
    |      |     `-> Cols
+//                                                 |      |                 |  
    |      `-> Rows
+//                                                 |      |                 |  
    `-> Interpolation: Undefined
+//                                                 |      |                 
`-> Semantic indices
+//                                                 |      `-> SemanticKind: 
Target
+//                                                 `-> CompType: F32
+// CHECK-DXIL: ![[#IDX3]] = !{i32 3}
+// CHECK-DXIL: ![[#T1]] = !{i32 1, !"SV_Target", i32 9, i32 16, ![[#IDX1:]], 
i32 0, i32 1, i8 4, i32 1, i8 0, i8 0, i8 0, i32 0}
+// CHECK-DXIL: ![[#IDX1]] = !{i32 1}
+// CHECK-DXIL: ![[#T2]] = !{i32 2, !"SV_Target", i32 9, i32 16, ![[#IDX2:]], 
i32 0, i32 1, i8 4, i32 2, i8 0, i8 0, i8 0, i32 0}
+// CHECK-DXIL: ![[#IDX2]] = !{i32 2}
+
+// CHECK-SPIRV-DAG: ![[#LOC3]] = !{![[#LOC3_MD:]]}
+// CHECK-SPIRV-DAG: ![[#LOC3_MD]] = !{i32 30, i32 3}
+// CHECK-SPIRV-DAG: ![[#LOC1]] = !{![[#LOC1_MD:]]}
+// CHECK-SPIRV-DAG: ![[#LOC1_MD]] = !{i32 30, i32 1}
+//                                        |       `-> Location index
+//                                        `-> SPIR-V decoration 'Location'
diff --git a/clang/test/CodeGenHLSL/semantics/interpolation-aggregate.hlsl 
b/clang/test/CodeGenHLSL/semantics/interpolation-aggregate.hlsl
index 12a0ad43c3d1ea..55674338aacbf4 100644
--- a/clang/test/CodeGenHLSL/semantics/interpolation-aggregate.hlsl
+++ b/clang/test/CodeGenHLSL/semantics/interpolation-aggregate.hlsl
@@ -61,7 +61,8 @@ noperspective centroid Inner vertex() : RESULT {
 // SPIRV-DAG: @C0 = {{.*}} !spirv.Decorations ![[C0:[0-9]+]]
 // SPIRV-DAG: @SV_Position = {{.*}} !spirv.Decorations ![[POS:[0-9]+]]
 // SPIRV-DAG: @RESULT0 = {{.*}} !spirv.Decorations ![[RESULT0:[0-9]+]]
-// SPIRV-DAG: @RESULT1 = {{.*}} !spirv.Decorations ![[RESULT1:[0-9]+]]
+// RESULT1 only has a location, which is the same as for A1.
+// SPIRV-DAG: @RESULT1 = {{.*}} !spirv.Decorations ![[A1]]
 // SPIRV-DAG: ![[A0]] = !{!{{[0-9]+}}, ![[FLAT:[0-9]+]]}
 // SPIRV-DAG: ![[A1]] = !{!{{[0-9]+}}}
 // SPIRV-DAG: ![[A2]] = !{!{{[0-9]+}}, ![[NP:[0-9]+]], ![[SAMPLE:[0-9]+]]}
@@ -70,7 +71,6 @@ noperspective centroid Inner vertex() : RESULT {
 // SPIRV-DAG: ![[C0]] = !{!{{[0-9]+}}}
 // SPIRV-DAG: ![[POS]] = !{![[BUILTIN:[0-9]+]], ![[SAMPLE]]}
 // SPIRV-DAG: ![[RESULT0]] = !{!{{[0-9]+}}, ![[NP]], ![[CENTROID]]}
-// SPIRV-DAG: ![[RESULT1]] = !{!{{[0-9]+}}}
 // SPIRV-DAG: ![[FLAT]] = !{i32 14}
 // SPIRV-DAG: ![[NP]] = !{i32 13}
 // SPIRV-DAG: ![[SAMPLE]] = !{i32 17}
diff --git 
a/clang/test/CodeGenHLSL/semantics/semantic.arbitrary-system-value.hlsl 
b/clang/test/CodeGenHLSL/semantics/semantic.arbitrary-system-value.hlsl
new file mode 100644
index 00000000000000..1b1eb3fe78a7f3
--- /dev/null
+++ b/clang/test/CodeGenHLSL/semantics/semantic.arbitrary-system-value.hlsl
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-vertex -x hlsl -emit-llvm 
-finclude-default-header -disable-llvm-passes -o - %s | FileCheck %s
+
+// SV_Position is an arbitrary semantic on a vertex input, so its input
+// signature element has the arbitrary semantic kind (0), while the output 
keeps
+// the SV_Position kind (3).
+float4 main(float4 P : SV_Position) : SV_Position {
+  return P;
+}
+
+// CHECK-DAG: !{ptr @main, ![[#IN:]], ![[#OUT:]]}
+// CHECK-DAG: ![[#IN]] = !{![[#IN_POS:]]}
+// CHECK-DAG: ![[#IN_POS]] = !{i32 0, !"SV_Position", i32 9, i32 0, 
![[#ZERO:]], i32 0, i32 1, i8 4, i32 0, i8 0, i8 0, i8 0, i32 0}
+// CHECK-DAG: ![[#OUT]] = !{![[#OUT_POS:]]}
+// CHECK-DAG: ![[#OUT_POS]] = !{i32 0, !"SV_Position", i32 9, i32 3, 
![[#ZERO]], i32 4, i32 1, i8 4, i32 0, i8 0, i8 0, i8 0, i32 0}
diff --git a/clang/test/CodeGenHLSL/semantics/semantic.explicit-mix.lib.hlsl 
b/clang/test/CodeGenHLSL/semantics/semantic.explicit-mix.lib.hlsl
index 793241a3295488..7c1e02f5d2a2d4 100644
--- a/clang/test/CodeGenHLSL/semantics/semantic.explicit-mix.lib.hlsl
+++ b/clang/test/CodeGenHLSL/semantics/semantic.explicit-mix.lib.hlsl
@@ -23,7 +23,8 @@ float4 not_an_entry([[vk::location(0)]] float4 a : A, float4 
b : B) : C {
 // CHECK: @SV_Position0 = external hidden thread_local addrspace(7) 
externally_initialized constant <4 x float>, !spirv.Decorations ![[#MD_0:]]
 // CHECK: @A0 = external hidden thread_local addrspace(8) global <4 x float>, 
!spirv.Decorations ![[#MD_0:]]
 // CHECK: @A0.1 = external hidden thread_local addrspace(7) 
externally_initialized constant <4 x float>, !spirv.Decorations ![[#MD_0:]]
-// CHECK: @SV_Target0 = external hidden thread_local addrspace(8) global <4 x 
float>, !spirv.Decorations ![[#MD_2:]]
+// SV_Target0 uses the location of its semantic index.
+// CHECK: @SV_Target0 = external hidden thread_local addrspace(8) global <4 x 
float>, !spirv.Decorations ![[#MD_0]]
 
 
 // CHECK: define void @vs_main()
@@ -36,5 +37,3 @@ float4 not_an_entry([[vk::location(0)]] float4 a : A, float4 
b : B) : C {
 
 // CHECK: ![[#MD_0]] = !{![[#MD_1:]]}
 // CHECK: ![[#MD_1]] = !{i32 30, i32 0}
-// CHECK: ![[#MD_2]] = !{![[#MD_3:]]}
-// CHECK: ![[#MD_3]] = !{i32 30, i32 1}
diff --git a/clang/test/SemaHLSL/Semantics/target.types.hlsl 
b/clang/test/SemaHLSL/Semantics/target.types.hlsl
new file mode 100644
index 00000000000000..60e5be814ffa78
--- /dev/null
+++ b/clang/test/SemaHLSL/Semantics/target.types.hlsl
@@ -0,0 +1,52 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type 
-finclude-default-header -x hlsl -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -fnative-half-type 
-finclude-default-header -x hlsl -fsyntax-only -verify %s
+
+// SV_Target: each render target is a scalar or vector of up to four 16- or
+// 32-bit floats. Arrays cover consecutive render targets.
+
+[shader("pixel")]
+float scalar() : SV_Target { return 0; }
+
+[shader("pixel")]
+half2 half_vector() : SV_Target { return 0; }
+
+struct Targets {
+  float4 Color : SV_Target0;
+  float3 Layers[2] : SV_Target1;
+};
+
+[shader("pixel")]
+Targets array() { return (Targets)0; }
+
+[shader("pixel")]
+int4 signed_int() : SV_Target { return 0; }
+// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'int4' (aka 
'vector<int, 4>'))}}
+
+[shader("pixel")]
+uint unsigned_int() : SV_Target { return 0; }
+// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'uint' (aka 'unsigned 
int'))}}
+
+[shader("pixel")]
+bool boolean() : SV_Target { return 0; }
+// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'bool')}}
+
+[shader("pixel")]
+float2x4 matrix() : SV_Target { return 0; }
+// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'float2x4' (aka 
'matrix<float, 2, 4>'))}}
+
+[shader("pixel")]
+vector<float, 5> too_wide() : SV_Target { return 0; }
+// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'vector<float, 5>' 
(vector of 5 'float' values))}}
+
+// An array uses the render targets of all its elements, so they must not
+// overlap with another SV_Target.
+struct Overlap {
+  float4 Layers[2] : SV_Target0;
+// expected-note@-1 {{previous use is here}}
+  float4 Color : SV_Target1;
+// expected-error@-1 {{semantic index overlap SV_Target1}}
+// expected-note@-2 {{'Color' used here}}
+};
+
+[shader("pixel")]
+Overlap overlap() { return (Overlap)0; }
diff --git a/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp 
b/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp
index 3f4e38d178a3c9..2b8f22e3b2b152 100644
--- a/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp
+++ b/llvm/lib/Target/DirectX/DXILSignatureAnalysis.cpp
@@ -71,6 +71,11 @@ static bool isIntegerComponent(ElementType Type) {
 
 static Error diagnoseSemanticType(const SemanticSignatureElement &E) {
   switch (E.SemanticKind) {
+  case dxbc::PSV::SemanticKind::Target:
+    if (E.CompType != ElementType::F16 && E.CompType != ElementType::F32)
+      return signatureError(
+          "SV_Target requires 16- or 32-bit floating-point components");
+    break;
   case dxbc::PSV::SemanticKind::Position:
     if (E.Rows != 1 || E.Cols != 4 ||
         (E.CompType != ElementType::F16 && E.CompType != ElementType::F32))
diff --git a/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-errors.ll 
b/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-errors.ll
index f8f2f108c1b010..219b571bbe0bd2 100644
--- a/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-errors.ll
+++ b/llvm/test/CodeGen/DirectX/Metadata/semantic-signatures-errors.ll
@@ -15,6 +15,7 @@
 ; RUN: not opt -disable-output -passes=dxil-translate-metadata 
%t/integer-undefined.ll 2>&1 | FileCheck %s --check-prefix=INTEGER-UNDEFINED
 ; RUN: not opt -disable-output -passes=dxil-translate-metadata 
%t/vertex-location.ll 2>&1 | FileCheck %s --check-prefix=VERTEX
 ; RUN: not opt -disable-output -passes=dxil-translate-metadata 
%t/target-location.ll 2>&1 | FileCheck %s --check-prefix=TARGET
+; RUN: not opt -disable-output -passes=dxil-translate-metadata 
%t/target-integer.ll 2>&1 | FileCheck %s --check-prefix=TARGET-INTEGER
 ; RUN: cat %t/access-common.ll %t/access-id.ll > %t/test.ll
 ; RUN: not opt -disable-output -passes=dxil-translate-metadata %t/test.ll 2>&1 
| FileCheck %s --check-prefix=ACCESS-ID
 ; RUN: cat %t/access-common.ll %t/access-component.ll > %t/test.ll
@@ -235,6 +236,18 @@ attributes #0 = { "hlsl.shader"="pixel" }
 !2 = !{i32 0, !"SV_Target", i32 9, i32 16, !3, i32 0, i32 1, i8 1, i32 1, i8 
0, i8 0, i8 0, i32 0}
 !3 = !{i32 3}
 
+;--- target-integer.ll
+; Invalid component type (operand 2): U32 (5), rather than a 16- or 32-bit 
float.
+target triple = "dxil-pc-shadermodel6.8-pixel"
+define void @main() #0 { ret void }
+attributes #0 = { "hlsl.shader"="pixel" }
+!dx.semantic.signatures = !{!0}
+!0 = !{ptr @main, null, !1}
+!1 = !{!2}
+; TARGET-INTEGER: entry 'main' output signature: SV_Target requires 16- or 
32-bit floating-point components
+!2 = !{i32 0, !"SV_Target", i32 5, i32 16, !3, i32 0, i32 1, i8 4, i32 0, i8 
0, i8 0, i8 0, i32 0}
+!3 = !{i32 0}
+
 ;--- access-common.ll
 target triple = "dxil-pc-shadermodel6.8-vertex"
 attributes #0 = { "hlsl.shader"="vertex" }

>From 7784dbb69ec09a798985621d07291219636e99d3 Mon Sep 17 00:00:00 2001
From: Finn Plummer <[email protected]>
Date: Fri, 30 Oct 2026 20:56:45 +0000
Subject: [PATCH 2/5] review: rename LocationFromIndex to UseLocationFromIndex

---
 clang/lib/CodeGen/CGHLSLRuntime.cpp | 6 +++---
 clang/lib/CodeGen/CGHLSLRuntime.h   | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index bf0e5c0d7dac7e..c2850f2ef9712b 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -1334,12 +1334,12 @@ void CGHLSLRuntime::emitSPIRVUserSemanticStore(
     llvm::IRBuilder<> &B, llvm::Value *Source,
     const clang::DeclaratorDecl *Decl, HLSLAppliedSemanticAttr *Semantic,
     std::optional<unsigned> Index, llvm::hlsl::InterpolationMode InterpMode,
-    bool LocationFromIndex) {
+    bool UseLocationFromIndex) {
   Twine BaseName = Twine(Semantic->getAttrName()->getName());
   Twine VariableName = BaseName.concat(Twine(Index.value_or(0)));
 
   unsigned Location;
-  if (LocationFromIndex) {
+  if (UseLocationFromIndex) {
     Location = Index.value_or(0);
   } else {
     Location = SPIRVLastAssignedOutputSemanticLocation;
@@ -1982,7 +1982,7 @@ void CGHLSLRuntime::emitSystemSemanticStore(
     }
     if (CGM.getTarget().getTriple().isSPIRV()) {
       emitSPIRVUserSemanticStore(B, Source, Decl, Semantic, Index, InterpMode,
-                                 /*LocationFromIndex=*/true);
+                                 /*UseLocationFromIndex=*/true);
       return;
     }
     break;
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h 
b/clang/lib/CodeGen/CGHLSLRuntime.h
index 269620fb79ad8d..8d52757b9f8b3a 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -401,7 +401,7 @@ class CGHLSLRuntime {
                                   HLSLAppliedSemanticAttr *Semantic,
                                   std::optional<unsigned> Index,
                                   llvm::hlsl::InterpolationMode InterpMode,
-                                  bool LocationFromIndex = false);
+                                  bool UseLocationFromIndex = false);
   void emitDXILUserSemanticStore(llvm::IRBuilder<> &B, llvm::Value *Source,
                                  const clang::DeclaratorDecl *Decl,
                                  HLSLAppliedSemanticAttr *Semantic,

>From 6b4f82096e3162d2410bd5645c14af3f371eb8c7 Mon Sep 17 00:00:00 2001
From: Finn Plummer <[email protected]>
Date: Fri, 30 Oct 2026 20:56:46 +0000
Subject: [PATCH 3/5] review: rename OneElementPerRow to
 EnforceOneElementPerRow

---
 clang/lib/CodeGen/CGHLSLRuntime.cpp | 10 +++++-----
 clang/lib/CodeGen/CGHLSLRuntime.h   |  2 +-
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index c2850f2ef9712b..9ec02ffdbf6e46 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -1541,10 +1541,10 @@ void 
CGHLSLRuntime::emitDXILUserSemanticStore(llvm::IRBuilder<> &B,
                                               HLSLAppliedSemanticAttr 
*Semantic,
                                               std::optional<unsigned> Index,
                                               SemanticSignatures &Signature,
-                                              bool OneElementPerRow) {
+                                              bool EnforceOneElementPerRow) {
   SemanticShape Shape =
       getSemanticShape(CGM.getContext(), getSemanticLeafType(Decl));
-  assert(!(OneElementPerRow && Shape.IsMatrix) &&
+  assert(!(EnforceOneElementPerRow && Shape.IsMatrix) &&
          "matrices cannot be split into one element per row");
 
   // DXIL pixel shader outputs are not indexable, and each render target must
@@ -1552,7 +1552,7 @@ void 
CGHLSLRuntime::emitDXILUserSemanticStore(llvm::IRBuilder<> &B,
   // therefore split into one single-row element per render target.
   SemanticShape RowShape{{}, Shape.Cols, Shape.RowType};
   uint32_t FirstSigId = Signature.size();
-  if (!OneElementPerRow)
+  if (!EnforceOneElementPerRow)
     Signature.push_back(createSemanticSignatureElement(CGM, FirstSigId,
                                                        Semantic, Index, 
Shape));
 
@@ -1591,7 +1591,7 @@ void 
CGHLSLRuntime::emitDXILUserSemanticStore(llvm::IRBuilder<> &B,
 
     uint32_t SigId = FirstSigId;
     uint32_t SigRow = Row;
-    if (OneElementPerRow) {
+    if (EnforceOneElementPerRow) {
       SigId = Signature.size();
       SigRow = 0;
       Signature.push_back(createSemanticSignatureElement(
@@ -1977,7 +1977,7 @@ void CGHLSLRuntime::emitSystemSemanticStore(
            "diagnosed by Sema");
     if (CGM.getTarget().getTriple().isDXIL()) {
       emitDXILUserSemanticStore(B, Source, Decl, Semantic, Index, Signature,
-                                /*OneElementPerRow=*/true);
+                                /*EnforceOneElementPerRow=*/true);
       return;
     }
     if (CGM.getTarget().getTriple().isSPIRV()) {
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h 
b/clang/lib/CodeGen/CGHLSLRuntime.h
index 8d52757b9f8b3a..e21668a632eec6 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -407,7 +407,7 @@ class CGHLSLRuntime {
                                  HLSLAppliedSemanticAttr *Semantic,
                                  std::optional<unsigned> Index,
                                  SemanticSignatures &Signature,
-                                 bool OneElementPerRow = false);
+                                 bool EnforceOneElementPerRow = false);
   void emitUserSemanticStore(llvm::IRBuilder<> &B, llvm::Value *Source,
                              const clang::DeclaratorDecl *Decl,
                              HLSLAppliedSemanticAttr *Semantic,

>From f870df2d1f4c786c0829fb2cf4323dd3df6a50f0 Mon Sep 17 00:00:00 2001
From: Finn Plummer <[email protected]>
Date: Fri, 30 Oct 2026 20:57:02 +0000
Subject: [PATCH 4/5] review: list the signature element operands in the
 SV_Target index test

---
 .../semantics/SV_Target.index.ps.hlsl            | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/clang/test/CodeGenHLSL/semantics/SV_Target.index.ps.hlsl 
b/clang/test/CodeGenHLSL/semantics/SV_Target.index.ps.hlsl
index 45aa0263d13977..db324a60041fe3 100644
--- a/clang/test/CodeGenHLSL/semantics/SV_Target.index.ps.hlsl
+++ b/clang/test/CodeGenHLSL/semantics/SV_Target.index.ps.hlsl
@@ -38,18 +38,18 @@ PSOut main(float4 P : SV_Position) {
   return O;
 }
 
-// Each element has one row, at the row of its semantic index.
+// Each element has one row, at the row of its semantic index. The operands of
+// an element are, in order:
+//   SigId, Name, CompType, SemanticKind, SemanticIndices, Interpolation, Rows,
+//   Cols, StartRow, StartCol, UsageMask, DynIndexMask, GSStream
+// Here each element has CompType F32 (9), SemanticKind Target (16), its own
+// semantic index in the SemanticIndices node (for example ![[#IDX3]] = {3}),
+// Interpolation Undefined (0), 1 row of 4 columns, and a StartRow equal to its
+// semantic index.
 // CHECK-DXIL: !dx.semantic.signatures = !{![[#SIG:]]}
 // CHECK-DXIL: ![[#SIG]] = !{ptr @main, ![[#IN:]], ![[#OUT:]]}
 // CHECK-DXIL: ![[#OUT]] = !{![[#T3:]], ![[#T1:]], ![[#T2:]]}
 // CHECK-DXIL: ![[#T3]] = !{i32 0, !"SV_Target", i32 9, i32 16, ![[#IDX3:]], 
i32 0, i32 1, i8 4, i32 3, i8 0, i8 0, i8 0, i32 0}
-//                                                 |      |                 |  
    |      |     |       `-> StartRow: semantic index
-//                                                 |      |                 |  
    |      |     `-> Cols
-//                                                 |      |                 |  
    |      `-> Rows
-//                                                 |      |                 |  
    `-> Interpolation: Undefined
-//                                                 |      |                 
`-> Semantic indices
-//                                                 |      `-> SemanticKind: 
Target
-//                                                 `-> CompType: F32
 // CHECK-DXIL: ![[#IDX3]] = !{i32 3}
 // CHECK-DXIL: ![[#T1]] = !{i32 1, !"SV_Target", i32 9, i32 16, ![[#IDX1:]], 
i32 0, i32 1, i8 4, i32 1, i8 0, i8 0, i8 0, i32 0}
 // CHECK-DXIL: ![[#IDX1]] = !{i32 1}

>From 0535fbe08a349900f65151b91b4d52c547963e3a Mon Sep 17 00:00:00 2001
From: Finn Plummer <[email protected]>
Date: Fri, 30 Oct 2026 20:57:17 +0000
Subject: [PATCH 5/5] review: split the SV_Target Sema type checks into valid
 and error tests

---
 .../Semantics/target.types.errors.hlsl        | 39 ++++++++++++++++++
 .../test/SemaHLSL/Semantics/target.types.hlsl | 40 +++----------------
 2 files changed, 44 insertions(+), 35 deletions(-)
 create mode 100644 clang/test/SemaHLSL/Semantics/target.types.errors.hlsl

diff --git a/clang/test/SemaHLSL/Semantics/target.types.errors.hlsl 
b/clang/test/SemaHLSL/Semantics/target.types.errors.hlsl
new file mode 100644
index 00000000000000..46f0e39951abd4
--- /dev/null
+++ b/clang/test/SemaHLSL/Semantics/target.types.errors.hlsl
@@ -0,0 +1,39 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type 
-finclude-default-header -x hlsl -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -fnative-half-type 
-finclude-default-header -x hlsl -fsyntax-only -verify %s
+
+// Invalid types of SV_Target: each render target must be a scalar or vector of
+// up to four 16- or 32-bit floats, and an array must not overlap another
+// SV_Target. The valid cases are covered by target.types.hlsl.
+
+[shader("pixel")]
+int4 signed_int() : SV_Target { return 0; }
+// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'int4' (aka 
'vector<int, 4>'))}}
+
+[shader("pixel")]
+uint unsigned_int() : SV_Target { return 0; }
+// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'uint' (aka 'unsigned 
int'))}}
+
+[shader("pixel")]
+bool boolean() : SV_Target { return 0; }
+// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'bool')}}
+
+[shader("pixel")]
+float2x4 matrix() : SV_Target { return 0; }
+// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'float2x4' (aka 
'matrix<float, 2, 4>'))}}
+
+[shader("pixel")]
+vector<float, 5> too_wide() : SV_Target { return 0; }
+// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'vector<float, 5>' 
(vector of 5 'float' values))}}
+
+// An array uses the render targets of all its elements, so they must not
+// overlap with another SV_Target.
+struct Overlap {
+  float4 Layers[2] : SV_Target0;
+// expected-note@-1 {{previous use is here}}
+  float4 Color : SV_Target1;
+// expected-error@-1 {{semantic index overlap SV_Target1}}
+// expected-note@-2 {{'Color' used here}}
+};
+
+[shader("pixel")]
+Overlap overlap() { return (Overlap)0; }
diff --git a/clang/test/SemaHLSL/Semantics/target.types.hlsl 
b/clang/test/SemaHLSL/Semantics/target.types.hlsl
index 60e5be814ffa78..912908d89a73ab 100644
--- a/clang/test/SemaHLSL/Semantics/target.types.hlsl
+++ b/clang/test/SemaHLSL/Semantics/target.types.hlsl
@@ -1,8 +1,11 @@
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -fnative-half-type 
-finclude-default-header -x hlsl -fsyntax-only -verify %s
 // RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-library -fnative-half-type 
-finclude-default-header -x hlsl -fsyntax-only -verify %s
 
-// SV_Target: each render target is a scalar or vector of up to four 16- or
-// 32-bit floats. Arrays cover consecutive render targets.
+// Valid types of SV_Target: each render target is a scalar or vector of up to
+// four 16- or 32-bit floats. Arrays cover consecutive render targets. The
+// invalid cases are covered by target.types.errors.hlsl.
+
+// expected-no-diagnostics
 
 [shader("pixel")]
 float scalar() : SV_Target { return 0; }
@@ -17,36 +20,3 @@ struct Targets {
 
 [shader("pixel")]
 Targets array() { return (Targets)0; }
-
-[shader("pixel")]
-int4 signed_int() : SV_Target { return 0; }
-// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'int4' (aka 
'vector<int, 4>'))}}
-
-[shader("pixel")]
-uint unsigned_int() : SV_Target { return 0; }
-// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'uint' (aka 'unsigned 
int'))}}
-
-[shader("pixel")]
-bool boolean() : SV_Target { return 0; }
-// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'bool')}}
-
-[shader("pixel")]
-float2x4 matrix() : SV_Target { return 0; }
-// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'float2x4' (aka 
'matrix<float, 2, 4>'))}}
-
-[shader("pixel")]
-vector<float, 5> too_wide() : SV_Target { return 0; }
-// expected-error@-1 {{semantic 'SV_Target' must be a scalar or vector of up 
to 4 components of 16 or 32 bit floating-point type (was 'vector<float, 5>' 
(vector of 5 'float' values))}}
-
-// An array uses the render targets of all its elements, so they must not
-// overlap with another SV_Target.
-struct Overlap {
-  float4 Layers[2] : SV_Target0;
-// expected-note@-1 {{previous use is here}}
-  float4 Color : SV_Target1;
-// expected-error@-1 {{semantic index overlap SV_Target1}}
-// expected-note@-2 {{'Color' used here}}
-};
-
-[shader("pixel")]
-Overlap overlap() { return (Overlap)0; }

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

Reply via email to