[llvm] ae14fae - [SCEVExpander] Use stable_sort to sort loop Phis in SCEVExpander::replaceCongruentIVs

Dmitry Makogon via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 9 01:30:23 PST 2021


Author: Dmitry Makogon
Date: 2021-11-09T16:29:57+07:00
New Revision: ae14fae0ff4304022beda5ab484f84ac0fdda807

URL: https://github.com/llvm/llvm-project/commit/ae14fae0ff4304022beda5ab484f84ac0fdda807
DIFF: https://github.com/llvm/llvm-project/commit/ae14fae0ff4304022beda5ab484f84ac0fdda807.diff

LOG: [SCEVExpander] Use stable_sort to sort loop Phis in SCEVExpander::replaceCongruentIVs

This is a fix for test failures on expensive checks build caused by db289340c841990055a164e8eb2a3b5ff25677bf.

With LLVM_ENABLE_EXPENSIVE_CHECKS enabled the llvm::sort shuffles the given container.
However, the sort is only called when the TTI is passed to replaceCongruentIVs.
In the mentioned patch we pass it TTI, so the sort happens. But due to shuffling
equivalent Phis may appear in different order from run to run.
With the stable_sort instead of sort this is impossible - the order of sorted Phis
is preserved.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
index 3abd1890a4ab..a042146d7ace 100644
--- a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -2022,7 +2022,9 @@ SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT,
     Phis.push_back(&PN);
 
   if (TTI)
-    llvm::sort(Phis, [](Value *LHS, Value *RHS) {
+    // Use stable_sort to preserve order of equivalent PHIs, so the order
+    // of the sorted Phis is the same from run to run on the same loop.
+    llvm::stable_sort(Phis, [](Value *LHS, Value *RHS) {
       // Put pointers at the back and make sure pointer < pointer = false.
       if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
         return RHS->getType()->isIntegerTy() && !LHS->getType()->isIntegerTy();


        


More information about the llvm-commits mailing list