[lld] r330702 - [ELF] - Never use std::sort.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 24 02:55:39 PDT 2018


Author: grimar
Date: Tue Apr 24 02:55:39 2018
New Revision: 330702

URL: http://llvm.org/viewvc/llvm-project?rev=330702&view=rev
Log:
[ELF] - Never use std::sort.

It turns out we should not use the std::sort anymore.
r327219 added a new wrapper llvm::sort (D39245).
When EXPENSIVE_CHECKS is defined, it shuffles the
input container and that helps to find non-deterministic
ordering.

Patch changes code to use llvm::sort and std::stable_sort
instead of std::sort

Differential revision: https://reviews.llvm.org/D45969

Modified:
    lld/trunk/ELF/CallGraphSort.cpp
    lld/trunk/ELF/MapFile.cpp
    lld/trunk/ELF/SyntheticSections.cpp
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/CallGraphSort.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/CallGraphSort.cpp?rev=330702&r1=330701&r2=330702&view=diff
==============================================================================
--- lld/trunk/ELF/CallGraphSort.cpp (original)
+++ lld/trunk/ELF/CallGraphSort.cpp Tue Apr 24 02:55:39 2018
@@ -219,10 +219,10 @@ void CallGraphSort::groupClusters() {
   });
 
   // Sort by density.
-  std::sort(Clusters.begin(), Clusters.end(),
-            [](const Cluster &A, const Cluster &B) {
-              return A.getDensity() > B.getDensity();
-            });
+  std::stable_sort(Clusters.begin(), Clusters.end(),
+                   [](const Cluster &A, const Cluster &B) {
+                     return A.getDensity() > B.getDensity();
+                   });
 }
 
 DenseMap<const InputSectionBase *, int> CallGraphSort::run() {

Modified: lld/trunk/ELF/MapFile.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/MapFile.cpp?rev=330702&r1=330701&r2=330702&view=diff
==============================================================================
--- lld/trunk/ELF/MapFile.cpp (original)
+++ lld/trunk/ELF/MapFile.cpp Tue Apr 24 02:55:39 2018
@@ -90,8 +90,9 @@ static SymbolMapTy getSectionSyms(ArrayR
   // in the input files.
   for (auto &It : Ret) {
     SmallVectorImpl<Symbol *> &V = It.second;
-    std::sort(V.begin(), V.end(),
-              [](Symbol *A, Symbol *B) { return A->getVA() < B->getVA(); });
+    std::stable_sort(V.begin(), V.end(), [](Symbol *A, Symbol *B) {
+      return A->getVA() < B->getVA();
+    });
   }
   return Ret;
 }

Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=330702&r1=330701&r2=330702&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Tue Apr 24 02:55:39 2018
@@ -1393,10 +1393,10 @@ bool AndroidPackedRelocationSection<ELFT
       NonRelatives.push_back(R);
   }
 
-  std::sort(Relatives.begin(), Relatives.end(),
-            [](const Elf_Rel &A, const Elf_Rel &B) {
-              return A.r_offset < B.r_offset;
-            });
+  llvm::sort(Relatives.begin(), Relatives.end(),
+             [](const Elf_Rel &A, const Elf_Rel &B) {
+               return A.r_offset < B.r_offset;
+             });
 
   // Try to find groups of relative relocations which are spaced one word
   // apart from one another. These generally correspond to vtable entries. The
@@ -1474,10 +1474,10 @@ bool AndroidPackedRelocationSection<ELFT
   }
 
   // Finally the non-relative relocations.
-  std::sort(NonRelatives.begin(), NonRelatives.end(),
-            [](const Elf_Rela &A, const Elf_Rela &B) {
-              return A.r_offset < B.r_offset;
-            });
+  llvm::sort(NonRelatives.begin(), NonRelatives.end(),
+             [](const Elf_Rela &A, const Elf_Rela &B) {
+               return A.r_offset < B.r_offset;
+             });
   if (!NonRelatives.empty()) {
     Add(NonRelatives.size());
     Add(HasAddendIfRela);

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=330702&r1=330701&r2=330702&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Apr 24 02:55:39 2018
@@ -1106,7 +1106,7 @@ sortISDBySectionOrder(InputSectionDescri
     }
     OrderedSections.push_back({IS, I->second});
   }
-  std::sort(
+  llvm::sort(
       OrderedSections.begin(), OrderedSections.end(),
       [&](std::pair<InputSection *, int> A, std::pair<InputSection *, int> B) {
         return A.second < B.second;
@@ -2056,10 +2056,10 @@ struct SectionOffset {
 // Check whether sections overlap for a specific address range (file offsets,
 // load and virtual adresses).
 static void checkOverlap(StringRef Name, std::vector<SectionOffset> &Sections) {
-  std::sort(Sections.begin(), Sections.end(),
-            [=](const SectionOffset &A, const SectionOffset &B) {
-              return A.Offset < B.Offset;
-            });
+  llvm::sort(Sections.begin(), Sections.end(),
+             [=](const SectionOffset &A, const SectionOffset &B) {
+               return A.Offset < B.Offset;
+             });
 
   // Finding overlap is easy given a vector is sorted by start position.
   // If an element starts before the end of the previous element, they overlap.




More information about the llvm-commits mailing list