[llvm] [AMDGPU] Make sort ordering in `AMDGPURewriteAGPRCopyMFMAImpl::eliminateSpillsOfReassignedVGPRs()` strict weak. (PR #162493)

weiwei chen via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 8 08:05:16 PDT 2025


================
@@ -490,7 +490,9 @@ void AMDGPURewriteAGPRCopyMFMAImpl::eliminateSpillsOfReassignedVGPRs() const {
       return true;
 
     // Tie breaker by number to avoid need for stable sort
-    return A->reg().stackSlotIndex() < B->reg().stackSlotIndex();
+    // The ordering have to be strictly weak.
+    return (A->reg().stackSlotIndex() < B->reg().stackSlotIndex()) &&
+           ((A->weight() <= B->weight()) && (A->getSize() <= B->getSize()));
----------------
weiweichen wrote:

> Consider using: return A->reg().stackSlotIndex() < B->reg().stackSlotIndex();

As mentioned in the issue, this is not the right solution for making strict weak ordering. 

But it's also true that my change has the issue with when `comp(a,b)` and `comp(b,a)` can both be `false` 🤦 . Updating the logic to use the `lexicographic ordering` suggestion. 

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


More information about the llvm-commits mailing list