[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:09 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);
----------------
Icohedron wrote:

[SUGGESTION] `getOptimizedPackingPriority` is a non-trivial function that is called 2 times for every comparison the sort performs (O(n log n)). 
Consider precomputing and constructing a sorting key struct for each element before performing the sort.

Example:
```cpp
struct SortKey {
 unsigned Priority;
 dxbc::PSV::InterpolationMode InterpMode;
 uint32_t Rows;
 uint8_t Cols;
 uint32_t SigId;
 unsigned OriginalIndex;
};
```

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


More information about the llvm-branch-commits mailing list