[llvm] [HLSLSemanticSignatures] Implement the indexed packing of elements (PR #218061)

Finn Plummer via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 10:03:57 PDT 2026


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

>From a65fc0f147327678e8d04aafe63059ae1a1d7891 Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Fri, 21 Aug 2026 20:13:01 +0000
Subject: [PATCH 01/13] add a stub indexed signature packing API

---
 .../llvm/Frontend/HLSL/SemanticSignaturePacking.h     | 11 +++++++++++
 llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp   |  6 ++++++
 2 files changed, 17 insertions(+)

diff --git a/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h b/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
index bcdb6ec71050c..bd816b44aca17 100644
--- a/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
+++ b/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
@@ -62,6 +62,17 @@ LLVM_ABI Expected<unsigned>
 packSignatureStacked(MutableArrayRef<SemanticSignatureElement> Elements,
                      Triple::EnvironmentType ShaderStage, IOType IOTy);
 
+/// Packs each eligible element at the row denoted by its semantic index and at
+/// column zero. Declaration order does not affect placement, and gaps between
+/// semantic indices remain unused. An element is left unallocated if it is not
+/// part of the signature.
+///
+/// Returns a SignaturePackingError that denotes the first element that cannot
+/// be placed, or success if all eligible elements were placed.
+LLVM_ABI Error
+packSignatureIndexed(MutableArrayRef<SemanticSignatureElement> Elements,
+                     Triple::EnvironmentType ShaderStage, IOType IOTy);
+
 } // namespace llvm::hlsl
 
 #endif // LLVM_FRONTEND_HLSL_SEMANTICSIGNATUREPACKING_H
diff --git a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
index 278c6a4f06d7d..eecd1979f5965 100644
--- a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
+++ b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
@@ -65,3 +65,9 @@ Expected<unsigned> llvm::hlsl::packSignatureStacked(
 
   return NextRow;
 }
+
+Error llvm::hlsl::packSignatureIndexed(
+    MutableArrayRef<SemanticSignatureElement>, Triple::EnvironmentType,
+    IOType) {
+  return Error::success();
+}

>From a8a8b68e097ecc0feeb7c1d2942727e807bd6f6f Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Fri, 21 Aug 2026 20:14:13 +0000
Subject: [PATCH 02/13] add indexed signature packing tests

---
 .../HLSLSemanticSignaturePackingTest.cpp      | 134 ++++++++++++++++--
 1 file changed, 126 insertions(+), 8 deletions(-)

diff --git a/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp b/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp
index 5ee3223313b50..8fd4a66fd85fa 100644
--- a/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp
+++ b/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp
@@ -27,6 +27,7 @@ class HLSLSemanticSignaturePackingTest : public testing::Test {
     uint8_t Cols;
     dxil::ElementType CompType;
     dxbc::PSV::InterpolationMode InterpMode;
+    uint32_t SemanticIndex = 0;
   };
 
   struct ExpectedLocation {
@@ -53,7 +54,7 @@ class HLSLSemanticSignaturePackingTest : public testing::Test {
     for (const ElementConfig &Element : Config.Elements) {
       SmallVector<uint32_t> SemanticIndices;
       for (uint32_t Row = 0; Row != Element.Rows; ++Row)
-        SemanticIndices.push_back(Row);
+        SemanticIndices.push_back(Element.SemanticIndex + Row);
 
       Elements.emplace_back(
           /*SigId=*/static_cast<uint32_t>(Elements.size()),
@@ -73,12 +74,28 @@ class HLSLSemanticSignaturePackingTest : public testing::Test {
     return packSignatureStacked(Elements, Config.ShaderStage, Config.IOTy);
   }
 
-  void verifyPacking(const TestConfig &Config, unsigned ExpectedRows,
-                     std::initializer_list<ExpectedLocation> Locations) {
+  Expected<unsigned>
+  packIndexed(SmallVectorImpl<SemanticSignatureElement> &Elements,
+              const TestConfig &Config) {
+    if (Error E =
+            packSignatureIndexed(Elements, Config.ShaderStage, Config.IOTy))
+      return std::move(E);
+
+    unsigned Rows = 0;
+    for (const SemanticSignatureElement &Element : Elements)
+      if (Element.isAllocated())
+        Rows = std::max(Rows, Element.StartRow + Element.Rows);
+    return Rows;
+  }
+
+  void verifyPackingImpl(const TestConfig &Config, unsigned ExpectedRows,
+                         std::initializer_list<ExpectedLocation> Locations,
+                         bool Indexed) {
     SmallVector<SemanticSignatureElement> Elements = makeSignature(Config);
     ASSERT_EQ(Elements.size(), Locations.size());
 
-    Expected<unsigned> Rows = packStacked(Elements, Config);
+    Expected<unsigned> Rows =
+        Indexed ? packIndexed(Elements, Config) : packStacked(Elements, Config);
     ASSERT_THAT_EXPECTED(Rows, Succeeded());
     EXPECT_EQ(*Rows, ExpectedRows);
 
@@ -90,11 +107,22 @@ class HLSLSemanticSignaturePackingTest : public testing::Test {
     }
   }
 
-  void verifyPackingError(const TestConfig &Config,
-                          SignaturePackingError::ErrorKind ExpectedKind,
-                          unsigned ExpectedElementIndex) {
+  void verifyPacking(const TestConfig &Config, unsigned ExpectedRows,
+                     std::initializer_list<ExpectedLocation> Locations) {
+    verifyPackingImpl(Config, ExpectedRows, Locations, /*Indexed=*/false);
+  }
+
+  void verifyIndexedPacking(const TestConfig &Config, unsigned ExpectedRows,
+                            std::initializer_list<ExpectedLocation> Locations) {
+    verifyPackingImpl(Config, ExpectedRows, Locations, /*Indexed=*/true);
+  }
+
+  void verifyPackingErrorImpl(const TestConfig &Config,
+                              SignaturePackingError::ErrorKind ExpectedKind,
+                              unsigned ExpectedElementIndex, bool Indexed) {
     SmallVector<SemanticSignatureElement> Elements = makeSignature(Config);
-    Expected<unsigned> Rows = packStacked(Elements, Config);
+    Expected<unsigned> Rows =
+        Indexed ? packIndexed(Elements, Config) : packStacked(Elements, Config);
     if (Rows) {
       ADD_FAILURE() << "expected a SignaturePackingError";
       return;
@@ -110,6 +138,20 @@ class HLSLSemanticSignaturePackingTest : public testing::Test {
                         << Other.message();
         });
   }
+
+  void verifyPackingError(const TestConfig &Config,
+                          SignaturePackingError::ErrorKind ExpectedKind,
+                          unsigned ExpectedElementIndex) {
+    verifyPackingErrorImpl(Config, ExpectedKind, ExpectedElementIndex,
+                           /*Indexed=*/false);
+  }
+
+  void verifyIndexedPackingError(const TestConfig &Config,
+                                 SignaturePackingError::ErrorKind ExpectedKind,
+                                 unsigned ExpectedElementIndex) {
+    verifyPackingErrorImpl(Config, ExpectedKind, ExpectedElementIndex,
+                           /*Indexed=*/true);
+  }
 };
 
 TEST_F(HLSLSemanticSignaturePackingTest, CreatesSignatureFromConfig) {
@@ -344,4 +386,80 @@ TEST_F(HLSLSemanticSignaturePackingTest, RejectsMultiRowSignatureOverflow) {
                      /*ExpectedElementIndex=*/1);
 }
 
+//===----------------------------------------------------------------------===//
+// Indexed packing tests
+//===----------------------------------------------------------------------===//
+
+TEST_F(HLSLSemanticSignaturePackingTest, IndexedUsesSemanticIndices) {
+  // Target elements are assigned the row denoted by their semantic index, not
+  // their declaration order. Every target starts at column zero.
+
+  // struct PSOut {
+  //   float4 Color3 : SV_Target3;
+  //   float Color0  : SV_Target0;
+  //   float2 Color2 : SV_Target2;
+  // };
+  TestConfig Config(
+      Triple::EnvironmentType::Pixel, IOType::Out,
+      {{dxbc::PSV::SemanticKind::Target, /*Rows=*/1, /*Cols=*/4,
+        dxil::ElementType::F32, dxbc::PSV::InterpolationMode::Undefined,
+        /*SemanticIndex=*/3},
+       {dxbc::PSV::SemanticKind::Target, /*Rows=*/1, /*Cols=*/1,
+        dxil::ElementType::F32, dxbc::PSV::InterpolationMode::Undefined,
+        /*SemanticIndex=*/0},
+       {dxbc::PSV::SemanticKind::Target, /*Rows=*/1, /*Cols=*/2,
+        dxil::ElementType::F32, dxbc::PSV::InterpolationMode::Undefined,
+        /*SemanticIndex=*/2}});
+
+  // Expected layout:
+  // reg0: Color0.x    | unused.yzw
+  // reg1: unused.xyzw
+  // reg2: Color2.xy   | unused.zw
+  // reg3: Color3.xyzw
+  verifyIndexedPacking(
+      Config, /*ExpectedRows=*/4,
+      {{/*Row=*/3, /*Col=*/0}, {/*Row=*/0, /*Col=*/0}, {/*Row=*/2, /*Col=*/0}});
+}
+
+TEST_F(HLSLSemanticSignaturePackingTest, IndexedLeavesSemanticIndexGaps) {
+  // Rows without a corresponding target semantic remain unused.
+
+  // struct PSOut {
+  //   float4 Color1 : SV_Target1;
+  //   float4 Color7 : SV_Target7;
+  // };
+  TestConfig Config(
+      Triple::EnvironmentType::Pixel, IOType::Out,
+      {{dxbc::PSV::SemanticKind::Target, /*Rows=*/1, /*Cols=*/4,
+        dxil::ElementType::F32, dxbc::PSV::InterpolationMode::Undefined,
+        /*SemanticIndex=*/1},
+       {dxbc::PSV::SemanticKind::Target, /*Rows=*/1, /*Cols=*/4,
+        dxil::ElementType::F32, dxbc::PSV::InterpolationMode::Undefined,
+        /*SemanticIndex=*/7}});
+
+  // Expected layout:
+  // reg0: unused.xyzw
+  // reg1: Color1.xyzw
+  // reg2-6: unused.xyzw
+  // reg7: Color7.xyzw
+  verifyIndexedPacking(Config, /*ExpectedRows=*/8,
+                       {{/*Row=*/1, /*Col=*/0}, {/*Row=*/7, /*Col=*/0}});
+}
+
+TEST_F(HLSLSemanticSignaturePackingTest, IndexedRejectsSemanticIndexOverflow) {
+  // A semantic index outside the 32-row signature cannot be allocated.
+
+  // struct PSOut {
+  //   float4 Color32 : SV_Target32;
+  // };
+  TestConfig Config(
+      Triple::EnvironmentType::Pixel, IOType::Out,
+      {{dxbc::PSV::SemanticKind::Target, /*Rows=*/1, /*Cols=*/4,
+        dxil::ElementType::F32, dxbc::PSV::InterpolationMode::Undefined,
+        /*SemanticIndex=*/MaxSignatureRows}});
+
+  verifyIndexedPackingError(Config, SignaturePackingError::SignatureOverflow,
+                            /*ExpectedElementIndex=*/0);
+}
+
 } // namespace

>From d863c5fa5c7a31fd9652acd1311427d6bec1561e Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Fri, 21 Aug 2026 20:16:27 +0000
Subject: [PATCH 03/13] implement indexed signature packing

---
 .../HLSL/SemanticSignaturePacking.cpp         | 31 +++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
index eecd1979f5965..a499284e4332d 100644
--- a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
+++ b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
@@ -67,7 +67,34 @@ Expected<unsigned> llvm::hlsl::packSignatureStacked(
 }
 
 Error llvm::hlsl::packSignatureIndexed(
-    MutableArrayRef<SemanticSignatureElement>, Triple::EnvironmentType,
-    IOType) {
+    MutableArrayRef<SemanticSignatureElement> Elements,
+    Triple::EnvironmentType ShaderStage, IOType IOTy) {
+  for (const auto &[Index, Element] : enumerate(Elements)) {
+    assert(Element.StartRow == UnallocatedRow &&
+           Element.StartCol == UnallocatedCol && "already allocated?");
+    assert(Element.Rows > 0 && "signature element must have at least one row");
+    assert(Element.Cols > 0 && Element.Cols <= MaxSignatureCols &&
+           "signature element must have between 1 and 4 columns");
+
+    SemanticInterpretation Interpretation =
+        getInterpretationKind(Element.SemanticKind, ShaderStage, IOTy);
+    if (Interpretation == SemanticInterpretation::NotAllocated)
+      continue;
+
+    assert(Interpretation == SemanticInterpretation::Target &&
+           "unexpected semantic interpretation for indexed packing");
+    assert(Element.Rows == 1 && Element.SemanticIndices.size() == 1 &&
+           "target elements must occupy one semantic row");
+
+    const uint32_t Row = Element.SemanticIndices.front();
+    if (Row >= MaxSignatureRows)
+      return make_error<SignaturePackingError>(
+          SignaturePackingError::SignatureOverflow,
+          static_cast<unsigned>(Index));
+
+    Element.StartRow = Row;
+    Element.StartCol = 0;
+  }
+
   return Error::success();
 }

>From 024e2e3572214be9b80e4ef450e2975d8ff9af43 Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Fri, 21 Aug 2026 20:21:45 +0000
Subject: [PATCH 04/13] document indexed signature packing

---
 llvm/docs/DirectX/SemanticSignatures.md       | 27 +++++++++++++++++++
 .../Frontend/HLSL/SemanticSignaturePacking.h  |  8 ++----
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/llvm/docs/DirectX/SemanticSignatures.md b/llvm/docs/DirectX/SemanticSignatures.md
index a30199f8ccf45..d5d7cfb2a2330 100644
--- a/llvm/docs/DirectX/SemanticSignatures.md
+++ b/llvm/docs/DirectX/SemanticSignatures.md
@@ -174,3 +174,30 @@ reg1: B[0].xyz   | unused.w
 reg2: B[1].xyz   | unused.w
 reg3: VertexID.x | unused.yzw
 ```
+
+### Indexed Packing
+
+Indexed packing is used for a pixel shader output signature. Each eligible
+`SV_Target` element occupies one row and starts at column zero. Its semantic
+index directly selects that row, so declaration order does not affect placement
+and rows without a corresponding semantic index remain unused. Elements that do
+not contribute to the target register space remain unallocated.
+
+For example:
+
+```hlsl
+struct PSOut {
+  float4 Color3 : SV_Target3;
+  float Color0  : SV_Target0;
+  float2 Color2 : SV_Target2;
+};
+```
+
+The signature is allocated as:
+
+```text
+reg0: Color0.x  | unused.yzw
+reg1: unused.xyzw
+reg2: Color2.xy | unused.zw
+reg3: Color3.xyzw
+```
diff --git a/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h b/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
index bd816b44aca17..e21c2df90545d 100644
--- a/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
+++ b/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
@@ -62,13 +62,9 @@ LLVM_ABI Expected<unsigned>
 packSignatureStacked(MutableArrayRef<SemanticSignatureElement> Elements,
                      Triple::EnvironmentType ShaderStage, IOType IOTy);
 
-/// Packs each eligible element at the row denoted by its semantic index and at
-/// column zero. Declaration order does not affect placement, and gaps between
-/// semantic indices remain unused. An element is left unallocated if it is not
-/// part of the signature.
+/// Packs eligible signature elements at rows selected by semantic index.
 ///
-/// Returns a SignaturePackingError that denotes the first element that cannot
-/// be placed, or success if all eligible elements were placed.
+/// See llvm/docs/DirectX/SemanticSignatures.md#indexed-packing for details.
 LLVM_ABI Error
 packSignatureIndexed(MutableArrayRef<SemanticSignatureElement> Elements,
                      Triple::EnvironmentType ShaderStage, IOType IOTy);

>From c199354d77a8a915351a1ba813bf6a3049fd4c96 Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Fri, 21 Aug 2026 20:42:32 +0000
Subject: [PATCH 05/13] simplify signature packing test harness

---
 .../HLSLSemanticSignaturePackingTest.cpp      | 118 ++++++++----------
 1 file changed, 51 insertions(+), 67 deletions(-)

diff --git a/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp b/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp
index 8fd4a66fd85fa..85f4520792eb7 100644
--- a/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp
+++ b/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp
@@ -48,6 +48,11 @@ class HLSLSemanticSignaturePackingTest : public testing::Test {
         : ShaderStage(ShaderStage), IOTy(IOTy), Elements(Elements) {}
   };
 
+  enum class PackingMethod {
+    Stacked,
+    Indexed,
+  };
+
   SmallVector<SemanticSignatureElement>
   makeSignature(const TestConfig &Config) {
     SmallVector<SemanticSignatureElement> Elements;
@@ -68,34 +73,34 @@ class HLSLSemanticSignaturePackingTest : public testing::Test {
     return Elements;
   }
 
-  Expected<unsigned>
-  packStacked(SmallVectorImpl<SemanticSignatureElement> &Elements,
-              const TestConfig &Config) {
-    return packSignatureStacked(Elements, Config.ShaderStage, Config.IOTy);
-  }
-
-  Expected<unsigned>
-  packIndexed(SmallVectorImpl<SemanticSignatureElement> &Elements,
-              const TestConfig &Config) {
-    if (Error E =
-            packSignatureIndexed(Elements, Config.ShaderStage, Config.IOTy))
-      return std::move(E);
-
-    unsigned Rows = 0;
-    for (const SemanticSignatureElement &Element : Elements)
-      if (Element.isAllocated())
-        Rows = std::max(Rows, Element.StartRow + Element.Rows);
-    return Rows;
+  Expected<unsigned> pack(PackingMethod Method,
+                          SmallVectorImpl<SemanticSignatureElement> &Elements,
+                          const TestConfig &Config) {
+    switch (Method) {
+    case PackingMethod::Stacked:
+      return packSignatureStacked(Elements, Config.ShaderStage, Config.IOTy);
+    case PackingMethod::Indexed: {
+      if (Error E =
+              packSignatureIndexed(Elements, Config.ShaderStage, Config.IOTy))
+        return std::move(E);
+
+      unsigned Rows = 0;
+      for (const SemanticSignatureElement &Element : Elements)
+        if (Element.isAllocated())
+          Rows = std::max(Rows, Element.StartRow + Element.Rows);
+      return Rows;
+    }
+    }
+    llvm_unreachable("invalid packing method");
   }
 
-  void verifyPackingImpl(const TestConfig &Config, unsigned ExpectedRows,
-                         std::initializer_list<ExpectedLocation> Locations,
-                         bool Indexed) {
+  void verifyPacking(PackingMethod Method, const TestConfig &Config,
+                     unsigned ExpectedRows,
+                     std::initializer_list<ExpectedLocation> Locations) {
     SmallVector<SemanticSignatureElement> Elements = makeSignature(Config);
     ASSERT_EQ(Elements.size(), Locations.size());
 
-    Expected<unsigned> Rows =
-        Indexed ? packIndexed(Elements, Config) : packStacked(Elements, Config);
+    Expected<unsigned> Rows = pack(Method, Elements, Config);
     ASSERT_THAT_EXPECTED(Rows, Succeeded());
     EXPECT_EQ(*Rows, ExpectedRows);
 
@@ -107,22 +112,11 @@ class HLSLSemanticSignaturePackingTest : public testing::Test {
     }
   }
 
-  void verifyPacking(const TestConfig &Config, unsigned ExpectedRows,
-                     std::initializer_list<ExpectedLocation> Locations) {
-    verifyPackingImpl(Config, ExpectedRows, Locations, /*Indexed=*/false);
-  }
-
-  void verifyIndexedPacking(const TestConfig &Config, unsigned ExpectedRows,
-                            std::initializer_list<ExpectedLocation> Locations) {
-    verifyPackingImpl(Config, ExpectedRows, Locations, /*Indexed=*/true);
-  }
-
-  void verifyPackingErrorImpl(const TestConfig &Config,
-                              SignaturePackingError::ErrorKind ExpectedKind,
-                              unsigned ExpectedElementIndex, bool Indexed) {
+  void verifyPackingError(PackingMethod Method, const TestConfig &Config,
+                          SignaturePackingError::ErrorKind ExpectedKind,
+                          unsigned ExpectedElementIndex) {
     SmallVector<SemanticSignatureElement> Elements = makeSignature(Config);
-    Expected<unsigned> Rows =
-        Indexed ? packIndexed(Elements, Config) : packStacked(Elements, Config);
+    Expected<unsigned> Rows = pack(Method, Elements, Config);
     if (Rows) {
       ADD_FAILURE() << "expected a SignaturePackingError";
       return;
@@ -138,20 +132,6 @@ class HLSLSemanticSignaturePackingTest : public testing::Test {
                         << Other.message();
         });
   }
-
-  void verifyPackingError(const TestConfig &Config,
-                          SignaturePackingError::ErrorKind ExpectedKind,
-                          unsigned ExpectedElementIndex) {
-    verifyPackingErrorImpl(Config, ExpectedKind, ExpectedElementIndex,
-                           /*Indexed=*/false);
-  }
-
-  void verifyIndexedPackingError(const TestConfig &Config,
-                                 SignaturePackingError::ErrorKind ExpectedKind,
-                                 unsigned ExpectedElementIndex) {
-    verifyPackingErrorImpl(Config, ExpectedKind, ExpectedElementIndex,
-                           /*Indexed=*/true);
-  }
 };
 
 TEST_F(HLSLSemanticSignaturePackingTest, CreatesSignatureFromConfig) {
@@ -217,7 +197,7 @@ TEST_F(HLSLSemanticSignaturePackingTest, SkipsNotAllocatedElements) {
   // Expected layout:
   // reg0: A.xy  | unused.zw
   // reg1: B.xyz | unused.w
-  verifyPacking(Config, /*ExpectedRows=*/2,
+  verifyPacking(PackingMethod::Stacked, Config, /*ExpectedRows=*/2,
                 {{/*Row=*/0, /*Col=*/0}, Unallocated, {/*Row=*/1, /*Col=*/0}});
 }
 
@@ -244,7 +224,7 @@ TEST_F(HLSLSemanticSignaturePackingTest, StacksInDeclarationOrder) {
   // reg1: Data.xy          | unused.zw
   // reg2: ClipDistance.xyz | unused.w
   verifyPacking(
-      Config, /*ExpectedRows=*/3,
+      PackingMethod::Stacked, Config, /*ExpectedRows=*/3,
       {{/*Row=*/0, /*Col=*/0}, {/*Row=*/1, /*Col=*/0}, {/*Row=*/2, /*Col=*/0}});
 }
 
@@ -273,7 +253,7 @@ TEST_F(HLSLSemanticSignaturePackingTest, DoesNotCoPackElements) {
   // reg1: B.x | unused.yzw
   // reg2: C.x | unused.yzw
   // reg3: D.x | unused.yzw
-  verifyPacking(Config, /*ExpectedRows=*/4,
+  verifyPacking(PackingMethod::Stacked, Config, /*ExpectedRows=*/4,
                 {{/*Row=*/0, /*Col=*/0},
                  {/*Row=*/1, /*Col=*/0},
                  {/*Row=*/2, /*Col=*/0},
@@ -305,7 +285,7 @@ TEST_F(HLSLSemanticSignaturePackingTest, StacksMultiRowElements) {
   // reg4: B[1].xyz | unused.w
   // reg5: C.xyzw
   verifyPacking(
-      Config, /*ExpectedRows=*/6,
+      PackingMethod::Stacked, Config, /*ExpectedRows=*/6,
       {{/*Row=*/0, /*Col=*/0}, {/*Row=*/3, /*Col=*/0}, {/*Row=*/5, /*Col=*/0}});
 }
 
@@ -323,8 +303,8 @@ TEST_F(HLSLSemanticSignaturePackingTest, ExactlyFillsSignature) {
 
   // Expected layout:
   // reg0-31: A[0-31].xyzw
-  verifyPacking(Config, /*ExpectedRows=*/MaxSignatureRows,
-                {{/*Row=*/0, /*Col=*/0}});
+  verifyPacking(PackingMethod::Stacked, Config,
+                /*ExpectedRows=*/MaxSignatureRows, {{/*Row=*/0, /*Col=*/0}});
 }
 
 //===----------------------------------------------------------------------===//
@@ -347,7 +327,8 @@ TEST_F(HLSLSemanticSignaturePackingTest, RejectsSignatureOverflow) {
                                dxbc::PSV::InterpolationMode::Linear});
 
   // The last element is the one that no longer fits.
-  verifyPackingError(Config, SignaturePackingError::SignatureOverflow,
+  verifyPackingError(PackingMethod::Stacked, Config,
+                     SignaturePackingError::SignatureOverflow,
                      /*ExpectedElementIndex=*/MaxSignatureRows);
 }
 
@@ -363,7 +344,8 @@ TEST_F(HLSLSemanticSignaturePackingTest, RejectsSingleElementOverflow) {
                       /*Cols=*/MaxSignatureCols, dxil::ElementType::F32,
                       dxbc::PSV::InterpolationMode::Linear}});
 
-  verifyPackingError(Config, SignaturePackingError::SignatureOverflow,
+  verifyPackingError(PackingMethod::Stacked, Config,
+                     SignaturePackingError::SignatureOverflow,
                      /*ExpectedElementIndex=*/0);
 }
 
@@ -382,7 +364,8 @@ TEST_F(HLSLSemanticSignaturePackingTest, RejectsMultiRowSignatureOverflow) {
                       /*Cols=*/MaxSignatureCols, dxil::ElementType::F32,
                       dxbc::PSV::InterpolationMode::Linear}});
 
-  verifyPackingError(Config, SignaturePackingError::SignatureOverflow,
+  verifyPackingError(PackingMethod::Stacked, Config,
+                     SignaturePackingError::SignatureOverflow,
                      /*ExpectedElementIndex=*/1);
 }
 
@@ -416,8 +399,8 @@ TEST_F(HLSLSemanticSignaturePackingTest, IndexedUsesSemanticIndices) {
   // reg1: unused.xyzw
   // reg2: Color2.xy   | unused.zw
   // reg3: Color3.xyzw
-  verifyIndexedPacking(
-      Config, /*ExpectedRows=*/4,
+  verifyPacking(
+      PackingMethod::Indexed, Config, /*ExpectedRows=*/4,
       {{/*Row=*/3, /*Col=*/0}, {/*Row=*/0, /*Col=*/0}, {/*Row=*/2, /*Col=*/0}});
 }
 
@@ -442,8 +425,8 @@ TEST_F(HLSLSemanticSignaturePackingTest, IndexedLeavesSemanticIndexGaps) {
   // reg1: Color1.xyzw
   // reg2-6: unused.xyzw
   // reg7: Color7.xyzw
-  verifyIndexedPacking(Config, /*ExpectedRows=*/8,
-                       {{/*Row=*/1, /*Col=*/0}, {/*Row=*/7, /*Col=*/0}});
+  verifyPacking(PackingMethod::Indexed, Config, /*ExpectedRows=*/8,
+                {{/*Row=*/1, /*Col=*/0}, {/*Row=*/7, /*Col=*/0}});
 }
 
 TEST_F(HLSLSemanticSignaturePackingTest, IndexedRejectsSemanticIndexOverflow) {
@@ -458,8 +441,9 @@ TEST_F(HLSLSemanticSignaturePackingTest, IndexedRejectsSemanticIndexOverflow) {
         dxil::ElementType::F32, dxbc::PSV::InterpolationMode::Undefined,
         /*SemanticIndex=*/MaxSignatureRows}});
 
-  verifyIndexedPackingError(Config, SignaturePackingError::SignatureOverflow,
-                            /*ExpectedElementIndex=*/0);
+  verifyPackingError(PackingMethod::Indexed, Config,
+                     SignaturePackingError::SignatureOverflow,
+                     /*ExpectedElementIndex=*/0);
 }
 
 } // namespace

>From f92a53060bcf9eb8cfdee4740040ebf1ded532f4 Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Wed, 16 Sep 2026 16:27:41 +0000
Subject: [PATCH 06/13] review: assert indexed packing is used for pixel shader
 outputs

Apply the stage and I/O precondition from stacked-packing review 1333db46be701be2bb24ecd2be1af5a45d11bc4b to indexed packing.
---
 llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
index a499284e4332d..f93284b31b1e0 100644
--- a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
+++ b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
@@ -69,6 +69,9 @@ Expected<unsigned> llvm::hlsl::packSignatureStacked(
 Error llvm::hlsl::packSignatureIndexed(
     MutableArrayRef<SemanticSignatureElement> Elements,
     Triple::EnvironmentType ShaderStage, IOType IOTy) {
+  assert(ShaderStage == Triple::Pixel && IOTy == IOType::Out &&
+         "indexed packing is only valid for a pixel shader output signature");
+
   for (const auto &[Index, Element] : enumerate(Elements)) {
     assert(Element.StartRow == UnallocatedRow &&
            Element.StartCol == UnallocatedCol && "already allocated?");

>From bed2f08057a13173557afdaa1e60181d0a9bb391 Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Wed, 16 Sep 2026 16:27:48 +0000
Subject: [PATCH 07/13] review: remove misleading const from indexed packing

Apply stacked-packing review 2f98b3ef30d33882316d1c2685f00adc8dc52184. The loop updates each element's allocated location, so use the same mutable binding as stacked packing.
---
 llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
index f93284b31b1e0..d6da9e5c76df9 100644
--- a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
+++ b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
@@ -72,7 +72,7 @@ Error llvm::hlsl::packSignatureIndexed(
   assert(ShaderStage == Triple::Pixel && IOTy == IOType::Out &&
          "indexed packing is only valid for a pixel shader output signature");
 
-  for (const auto &[Index, Element] : enumerate(Elements)) {
+  for (auto &&[Index, Element] : enumerate(Elements)) {
     assert(Element.StartRow == UnallocatedRow &&
            Element.StartCol == UnallocatedCol && "already allocated?");
     assert(Element.Rows > 0 && "signature element must have at least one row");

>From f779434c2a22d37533911d821007a2162845e988 Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Wed, 16 Sep 2026 16:27:57 +0000
Subject: [PATCH 08/13] review: document partial allocation on indexed packing
 failure

Apply stacked-packing review 538ce4ada60086f347d1d5f098969f03603830b7. Indexed packing also leaves earlier elements allocated when a later element overflows, preserving their locations for diagnostics.
---
 llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h b/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
index e21c2df90545d..908682154023e 100644
--- a/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
+++ b/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
@@ -65,6 +65,11 @@ packSignatureStacked(MutableArrayRef<SemanticSignatureElement> Elements,
 /// Packs eligible signature elements at rows selected by semantic index.
 ///
 /// See llvm/docs/DirectX/SemanticSignatures.md#indexed-packing for details.
+///
+/// On failure, Elements is left partially packed: the elements preceding the
+/// one reported by the returned SignaturePackingError keep the locations
+/// they were assigned, while that element and the ones following it retain the
+/// unallocated row and column sentinels.
 LLVM_ABI Error
 packSignatureIndexed(MutableArrayRef<SemanticSignatureElement> Elements,
                      Triple::EnvironmentType ShaderStage, IOType IOTy);

>From e874ea2f768b433ee5aef7a281dd0079f90f33d8 Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Wed, 16 Sep 2026 16:28:58 +0000
Subject: [PATCH 09/13] review: return the indexed signature row extent

Apply stacked-packing review 2c35388619dbde079a3bfda52493cb12974a9bfc by returning Expected<unsigned> from indexed packing as well.

Return one past the highest allocated row, including semantic-index gaps, or zero for an empty signature. Check the returned extent directly in the shared test harness and cover the empty and last-row boundaries.
---
 .../Frontend/HLSL/SemanticSignaturePacking.h  |  5 ++-
 .../HLSL/SemanticSignaturePacking.cpp         |  7 ++--
 .../HLSLSemanticSignaturePackingTest.cpp      | 33 ++++++++++++-------
 3 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h b/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
index 908682154023e..5c1d340472ece 100644
--- a/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
+++ b/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
@@ -66,11 +66,14 @@ packSignatureStacked(MutableArrayRef<SemanticSignatureElement> Elements,
 ///
 /// See llvm/docs/DirectX/SemanticSignatures.md#indexed-packing for details.
 ///
+/// Returns one past the highest allocated row, or zero if no elements were
+/// allocated. Gaps between semantic indices count towards this row extent.
+///
 /// On failure, Elements is left partially packed: the elements preceding the
 /// one reported by the returned SignaturePackingError keep the locations
 /// they were assigned, while that element and the ones following it retain the
 /// unallocated row and column sentinels.
-LLVM_ABI Error
+LLVM_ABI Expected<unsigned>
 packSignatureIndexed(MutableArrayRef<SemanticSignatureElement> Elements,
                      Triple::EnvironmentType ShaderStage, IOType IOTy);
 
diff --git a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
index d6da9e5c76df9..8ce6f6007e013 100644
--- a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
+++ b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
@@ -12,6 +12,7 @@
 
 #include "llvm/Frontend/HLSL/SemanticSignaturePacking.h"
 #include "llvm/ADT/STLExtras.h"
+#include <algorithm>
 #include <cassert>
 
 using namespace llvm;
@@ -66,12 +67,13 @@ Expected<unsigned> llvm::hlsl::packSignatureStacked(
   return NextRow;
 }
 
-Error llvm::hlsl::packSignatureIndexed(
+Expected<unsigned> llvm::hlsl::packSignatureIndexed(
     MutableArrayRef<SemanticSignatureElement> Elements,
     Triple::EnvironmentType ShaderStage, IOType IOTy) {
   assert(ShaderStage == Triple::Pixel && IOTy == IOType::Out &&
          "indexed packing is only valid for a pixel shader output signature");
 
+  unsigned NumRows = 0;
   for (auto &&[Index, Element] : enumerate(Elements)) {
     assert(Element.StartRow == UnallocatedRow &&
            Element.StartCol == UnallocatedCol && "already allocated?");
@@ -97,7 +99,8 @@ Error llvm::hlsl::packSignatureIndexed(
 
     Element.StartRow = Row;
     Element.StartCol = 0;
+    NumRows = std::max(NumRows, Row + 1);
   }
 
-  return Error::success();
+  return NumRows;
 }
diff --git a/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp b/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp
index 85f4520792eb7..8dd641720b3f3 100644
--- a/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp
+++ b/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp
@@ -11,7 +11,6 @@
 #include "llvm/TargetParser/Triple.h"
 #include "llvm/Testing/Support/Error.h"
 #include "gtest/gtest.h"
-#include <algorithm>
 #include <initializer_list>
 
 using namespace llvm;
@@ -79,17 +78,8 @@ class HLSLSemanticSignaturePackingTest : public testing::Test {
     switch (Method) {
     case PackingMethod::Stacked:
       return packSignatureStacked(Elements, Config.ShaderStage, Config.IOTy);
-    case PackingMethod::Indexed: {
-      if (Error E =
-              packSignatureIndexed(Elements, Config.ShaderStage, Config.IOTy))
-        return std::move(E);
-
-      unsigned Rows = 0;
-      for (const SemanticSignatureElement &Element : Elements)
-        if (Element.isAllocated())
-          Rows = std::max(Rows, Element.StartRow + Element.Rows);
-      return Rows;
-    }
+    case PackingMethod::Indexed:
+      return packSignatureIndexed(Elements, Config.ShaderStage, Config.IOTy);
     }
     llvm_unreachable("invalid packing method");
   }
@@ -373,6 +363,25 @@ TEST_F(HLSLSemanticSignaturePackingTest, RejectsMultiRowSignatureOverflow) {
 // Indexed packing tests
 //===----------------------------------------------------------------------===//
 
+TEST_F(HLSLSemanticSignaturePackingTest, IndexedEmptySignature) {
+  TestConfig Config(Triple::EnvironmentType::Pixel, IOType::Out, {});
+
+  verifyPacking(PackingMethod::Indexed, Config, /*ExpectedRows=*/0, {});
+}
+
+TEST_F(HLSLSemanticSignaturePackingTest, IndexedUsesLastSignatureRow) {
+  // The row extent includes the unused rows before the target's semantic index.
+  TestConfig Config(
+      Triple::EnvironmentType::Pixel, IOType::Out,
+      {{dxbc::PSV::SemanticKind::Target, /*Rows=*/1, /*Cols=*/4,
+        dxil::ElementType::F32, dxbc::PSV::InterpolationMode::Undefined,
+        /*SemanticIndex=*/MaxSignatureRows - 1}});
+
+  verifyPacking(PackingMethod::Indexed, Config,
+                /*ExpectedRows=*/MaxSignatureRows,
+                {{/*Row=*/MaxSignatureRows - 1, /*Col=*/0}});
+}
+
 TEST_F(HLSLSemanticSignaturePackingTest, IndexedUsesSemanticIndices) {
   // Target elements are assigned the row denoted by their semantic index, not
   // their declaration order. Every target starts at column zero.

>From f2dbc42def72c70635adafafd686e6f733b42040 Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Wed, 16 Sep 2026 16:29:18 +0000
Subject: [PATCH 10/13] review: clarify indexed packing semantic assertions

Apply stacked-packing review 5c084c38c5079df28eb6bf4040926bbd6401cdd1. Unexpected semantic interpretations should already have been diagnosed by Sema, so the indexed packer only needs an assertion here.
---
 llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
index 8ce6f6007e013..89d8e097e4d18 100644
--- a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
+++ b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
@@ -87,7 +87,8 @@ Expected<unsigned> llvm::hlsl::packSignatureIndexed(
       continue;
 
     assert(Interpretation == SemanticInterpretation::Target &&
-           "unexpected semantic interpretation for indexed packing");
+           "unexpected semantic interpretation for indexed packing, should "
+           "have been diagnosed by Sema");
     assert(Element.Rows == 1 && Element.SemanticIndices.size() == 1 &&
            "target elements must occupy one semantic row");
 

>From 235c6129ddcfd911a6334b1ee3f4bf236a0715af Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Wed, 16 Sep 2026 16:33:45 +0000
Subject: [PATCH 11/13] review: document indexed packing preconditions

Require eligible elements to occupy one row with one semantic index, and require those indices to be unique.

Address Icohedron's comment:
https://github.com/llvm/llvm-project/pull/218061#discussion_r3876942224
---
 llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h b/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
index 5c1d340472ece..7063b48435900 100644
--- a/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
+++ b/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
@@ -66,6 +66,9 @@ packSignatureStacked(MutableArrayRef<SemanticSignatureElement> Elements,
 ///
 /// See llvm/docs/DirectX/SemanticSignatures.md#indexed-packing for details.
 ///
+/// Requires each eligible element to occupy exactly one row and have exactly
+/// one semantic index. Semantic indices must be unique among eligible elements.
+///
 /// Returns one past the highest allocated row, or zero if no elements were
 /// allocated. Gaps between semantic indices count towards this row extent.
 ///

>From 84026a5dee7a83424264d66adb41a894ee81f75c Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Wed, 16 Sep 2026 16:35:38 +0000
Subject: [PATCH 12/13] review: distinguish out-of-range semantic indices from
 signature overflow

Add SemanticIndexOutOfRange with a diagnostic stating the semantic-index bound. Keep SignatureOverflow for stacked signatures that exceed the available row capacity, and check the indexed error kind and message in the unit test.

Address Icohedron's comment:
https://github.com/llvm/llvm-project/pull/218061#discussion_r3876942229
---
 .../llvm/Frontend/HLSL/SemanticSignaturePacking.h    |  1 +
 llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp  |  5 ++++-
 .../Frontend/HLSLSemanticSignaturePackingTest.cpp    | 12 ++++++++++--
 3 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h b/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
index 7063b48435900..2a67d580bf970 100644
--- a/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
+++ b/llvm/include/llvm/Frontend/HLSL/SemanticSignaturePacking.h
@@ -29,6 +29,7 @@ class LLVM_ABI SignaturePackingError : public ErrorInfo<SignaturePackingError> {
 public:
   enum ErrorKind {
     SignatureOverflow,
+    SemanticIndexOutOfRange,
   };
 
   static char ID;
diff --git a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
index 89d8e097e4d18..29c4c3a2abd1c 100644
--- a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
+++ b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
@@ -25,6 +25,9 @@ void SignaturePackingError::log(raw_ostream &OS) const {
   case SignatureOverflow:
     OS << "signature elements do not fit in " << MaxSignatureRows << " rows";
     break;
+  case SemanticIndexOutOfRange:
+    OS << "semantic index must be less than " << MaxSignatureRows;
+    break;
   }
   OS << " (element " << ElementIndex << ")";
 }
@@ -95,7 +98,7 @@ Expected<unsigned> llvm::hlsl::packSignatureIndexed(
     const uint32_t Row = Element.SemanticIndices.front();
     if (Row >= MaxSignatureRows)
       return make_error<SignaturePackingError>(
-          SignaturePackingError::SignatureOverflow,
+          SignaturePackingError::SemanticIndexOutOfRange,
           static_cast<unsigned>(Index));
 
     Element.StartRow = Row;
diff --git a/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp b/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp
index 8dd641720b3f3..b70442392f37b 100644
--- a/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp
+++ b/llvm/unittests/Frontend/HLSLSemanticSignaturePackingTest.cpp
@@ -12,6 +12,7 @@
 #include "llvm/Testing/Support/Error.h"
 #include "gtest/gtest.h"
 #include <initializer_list>
+#include <string>
 
 using namespace llvm;
 using namespace llvm::hlsl;
@@ -438,7 +439,8 @@ TEST_F(HLSLSemanticSignaturePackingTest, IndexedLeavesSemanticIndexGaps) {
                 {{/*Row=*/1, /*Col=*/0}, {/*Row=*/7, /*Col=*/0}});
 }
 
-TEST_F(HLSLSemanticSignaturePackingTest, IndexedRejectsSemanticIndexOverflow) {
+TEST_F(HLSLSemanticSignaturePackingTest,
+       IndexedRejectsOutOfRangeSemanticIndex) {
   // A semantic index outside the 32-row signature cannot be allocated.
 
   // struct PSOut {
@@ -451,8 +453,14 @@ TEST_F(HLSLSemanticSignaturePackingTest, IndexedRejectsSemanticIndexOverflow) {
         /*SemanticIndex=*/MaxSignatureRows}});
 
   verifyPackingError(PackingMethod::Indexed, Config,
-                     SignaturePackingError::SignatureOverflow,
+                     SignaturePackingError::SemanticIndexOutOfRange,
                      /*ExpectedElementIndex=*/0);
+
+  SmallVector<SemanticSignatureElement> Elements = makeSignature(Config);
+  EXPECT_THAT_EXPECTED(pack(PackingMethod::Indexed, Elements, Config),
+                       FailedWithMessage("semantic index must be less than " +
+                                         std::to_string(MaxSignatureRows) +
+                                         " (element 0)"));
 }
 
 } // namespace

>From 0b8f47bd506a74a818c84b38d91e9c3e1457ea38 Mon Sep 17 00:00:00 2001
From: Finn Plummer <mail at inbelic.dev>
Date: Wed, 16 Sep 2026 16:36:38 +0000
Subject: [PATCH 13/13] review: assert indexed semantic indices are unique

Track allocated rows with a 32-bit bitmask and assert before reusing a row. Check the semantic-index bound before shifting, and guard the mask width with a static assertion.

Address Icohedron's comment:
https://github.com/llvm/llvm-project/pull/218061#discussion_r3876942233
---
 llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
index 29c4c3a2abd1c..0421285b4ea62 100644
--- a/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
+++ b/llvm/lib/Frontend/HLSL/SemanticSignaturePacking.cpp
@@ -14,6 +14,8 @@
 #include "llvm/ADT/STLExtras.h"
 #include <algorithm>
 #include <cassert>
+#include <cstdint>
+#include <limits>
 
 using namespace llvm;
 using namespace llvm::hlsl;
@@ -76,6 +78,9 @@ Expected<unsigned> llvm::hlsl::packSignatureIndexed(
   assert(ShaderStage == Triple::Pixel && IOTy == IOType::Out &&
          "indexed packing is only valid for a pixel shader output signature");
 
+  static_assert(MaxSignatureRows <= std::numeric_limits<uint32_t>::digits,
+                "row allocation mask is too small");
+  [[maybe_unused]] uint32_t AllocatedRows = 0;
   unsigned NumRows = 0;
   for (auto &&[Index, Element] : enumerate(Elements)) {
     assert(Element.StartRow == UnallocatedRow &&
@@ -101,6 +106,11 @@ Expected<unsigned> llvm::hlsl::packSignatureIndexed(
           SignaturePackingError::SemanticIndexOutOfRange,
           static_cast<unsigned>(Index));
 
+    const uint32_t RowMask = uint32_t{1} << Row;
+    assert(!(AllocatedRows & RowMask) &&
+           "target semantic indices must be unique, verified in SemaHLSL");
+    AllocatedRows |= RowMask;
+
     Element.StartRow = Row;
     Element.StartCol = 0;
     NumRows = std::max(NumRows, Row + 1);



More information about the llvm-commits mailing list