[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,
+                                                 IOTy, UseNative16BitTypes);
+
+  for (const auto &[SortedIndex, OriginalIndex] : enumerate(SortedIndices)) {
+    Elements[OriginalIndex].StartRow = SortedElements[SortedIndex].StartRow;
+    Elements[OriginalIndex].StartCol = SortedElements[SortedIndex].StartCol;
+  }
----------------
Icohedron wrote:

[QUESTION] Could it be an issue that this reordering proceeds even if an error has occurred (from `PackingError`)?

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


More information about the llvm-branch-commits mailing list