[llvm] [SPIR-V] Prevent adding duplicate binding instructions for implicit binding (PR #161299)

Steven Perron via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 30 06:48:12 PDT 2025


================
@@ -112,11 +112,22 @@ uint32_t SPIRVLegalizeImplicitBinding::getAndReserveFirstUnusedBinding(
 }
 
 void SPIRVLegalizeImplicitBinding::replaceImplicitBindingCalls(Module &M) {
+  std::unordered_map<uint32_t, uint32_t> OrderIdToBinding;
+
   for (CallInst *OldCI : ImplicitBindingCalls) {
     IRBuilder<> Builder(OldCI);
+    const uint32_t OrderId =
+        cast<ConstantInt>(OldCI->getArgOperand(0))->getZExtValue();
     const uint32_t DescSet =
         cast<ConstantInt>(OldCI->getArgOperand(1))->getZExtValue();
-    const uint32_t NewBinding = getAndReserveFirstUnusedBinding(DescSet);
+
+    // Reuse an existing binding for this order ID, if one was already assigned.
+    // Otherwise, assign a new binding.
+    const uint32_t NewBinding =
+        (OrderIdToBinding.find(OrderId) != OrderIdToBinding.end())
+            ? OrderIdToBinding[OrderId]
+            : getAndReserveFirstUnusedBinding(DescSet);
+    OrderIdToBinding[OrderId] = NewBinding;
----------------
s-perron wrote:

I'm not sure this is well specified, but I think we should make this more robust. What should we do if we have the same orderid, but for different descriptor sets? I'm thinking we assign it a new binding number.

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


More information about the llvm-commits mailing list