[llvm-branch-commits] [llvm] [HLSLSemanticSignatures] Implement the optimal packing of elements (PR #218064)

Deric C. via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Aug 25 16:50:10 PDT 2026


================
@@ -499,3 +535,54 @@ Error llvm::hlsl::packSignaturePrefixStable(
 
   return Error::success();
 }
+
+Error llvm::hlsl::packSignatureOptimized(
+    MutableArrayRef<SemanticSignatureElement> Elements,
+    Triple::EnvironmentType ShaderStage, IOType IOTy,
+    bool UseNative16BitTypes) {
+  SmallVector<unsigned> SortedIndices;
+  SortedIndices.reserve(Elements.size());
+  for (unsigned Index = 0; Index != Elements.size(); ++Index)
+    SortedIndices.push_back(Index);
+
+  llvm::sort(SortedIndices, [&](unsigned LeftIndex, unsigned RightIndex) {
+    const SemanticSignatureElement &Left = Elements[LeftIndex];
+    const SemanticSignatureElement &Right = Elements[RightIndex];
+    const unsigned LeftPriority =
+        getOptimizedPackingPriority(Left, ShaderStage, IOTy);
+    const unsigned RightPriority =
+        getOptimizedPackingPriority(Right, ShaderStage, IOTy);
+
+    if (LeftPriority != RightPriority)
+      return LeftPriority < RightPriority;
+    if (Left.InterpMode != Right.InterpMode)
+      return Left.InterpMode < Right.InterpMode;
+    if (Left.Rows != Right.Rows)
+      return Left.Rows > Right.Rows;
+    if (Left.Cols != Right.Cols)
+      return Left.Cols > Right.Cols;
+    return Left.SigId < Right.SigId;
+  });
+
+  // Pack a copy so Elements remains in its original signature order.
+  SmallVector<SemanticSignatureElement> SortedElements;
+  SortedElements.reserve(Elements.size());
+  for (unsigned Index : SortedIndices)
+    SortedElements.push_back(Elements[Index]);
+
+  Error PackingError = packSignaturePrefixStable(SortedElements, ShaderStage,
----------------
Icohedron wrote:

[ISSUE] The `packSignaturePrefixStable` reserves a whole row for clip/cull, but DXC's `-pack-optimized` does not do that.

https://hlsl.godbolt.org/z/KafosvY1e

Perhaps you should also add a test that exercises the limits of how much can be packed? 
A valid DXC `-pack-optimized` shader can be invalid under Clang's `packSignatureOptimized()` because clip/cull may require an extra row.

https://github.com/llvm/llvm-project/pull/218064


More information about the llvm-branch-commits mailing list