From d2f494f236efc56ea45cd4edcd3652cc50f06999 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Mon, 17 Jun 2024 23:42:12 -0700 Subject: [PATCH 1/2] [llvm] Use llvm::sort (NFC) --- .../CodeGen/AssignmentTrackingAnalysis.cpp | 9 +++-- llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp | 6 ++-- llvm/lib/DWARFLinker/Parallel/ArrayList.h | 2 +- llvm/lib/ExecutionEngine/Orc/Core.cpp | 5 +-- .../Orc/Debugging/DebugInfoSupport.cpp | 2 +- llvm/lib/ProfileData/InstrProfReader.cpp | 2 +- llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 7 ++-- llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp | 4 +-- llvm/lib/Target/NVPTX/NVVMReflect.cpp | 2 +- .../lib/Target/PowerPC/PPCMergeStringPool.cpp | 2 +- .../IPO/MemProfContextDisambiguation.cpp | 6 ++-- llvm/lib/Transforms/Utils/CodeLayout.cpp | 33 +++++++++---------- llvm/tools/llvm-jitlink/llvm-jitlink.cpp | 7 ++-- llvm/utils/TableGen/ARMTargetDefEmitter.cpp | 2 +- llvm/utils/TableGen/ExegesisEmitter.cpp | 3 +- 15 files changed, 43 insertions(+), 49 deletions(-) diff --git a/llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp b/llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp index 8afd75069589e1..012417553ff194 100644 --- a/llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp +++ b/llvm/lib/CodeGen/AssignmentTrackingAnalysis.cpp @@ -2231,11 +2231,10 @@ static AssignmentTrackingLowering::OverlapMap buildOverlapMapAndRecordDeclares( // order of fragment size - there should be no duplicates. for (auto &Pair : FragmentMap) { SmallVector &Frags = Pair.second; - std::sort(Frags.begin(), Frags.end(), - [](const DebugVariable &Next, const DebugVariable &Elmt) { - return Elmt.getFragmentOrDefault().SizeInBits > - Next.getFragmentOrDefault().SizeInBits; - }); + llvm::sort(Frags, [](const DebugVariable &Next, const DebugVariable &Elmt) { + return Elmt.getFragmentOrDefault().SizeInBits > + Next.getFragmentOrDefault().SizeInBits; + }); // Check for duplicates. assert(std::adjacent_find(Frags.begin(), Frags.end()) == Frags.end()); } diff --git a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp index f3a961f883517f..b8b46791b47e9e 100644 --- a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp +++ b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp @@ -963,9 +963,9 @@ void extractInstructionFeatures( // frequency vector, mapping each instruction to its associated MBB. // Start off by sorting the segments based on the beginning slot index. - std::sort( - LRPosInfo.begin(), LRPosInfo.end(), - [](LRStartEndInfo A, LRStartEndInfo B) { return A.Begin < B.Begin; }); + llvm::sort(LRPosInfo, [](LRStartEndInfo A, LRStartEndInfo B) { + return A.Begin < B.Begin; + }); size_t InstructionIndex = 0; size_t CurrentSegmentIndex = 0; SlotIndex CurrentIndex = LRPosInfo[0].Begin; diff --git a/llvm/lib/DWARFLinker/Parallel/ArrayList.h b/llvm/lib/DWARFLinker/Parallel/ArrayList.h index c48f828609be2f..1dc7bfa11d1065 100644 --- a/llvm/lib/DWARFLinker/Parallel/ArrayList.h +++ b/llvm/lib/DWARFLinker/Parallel/ArrayList.h @@ -82,7 +82,7 @@ template class ArrayList { forEach([&](T &Item) { SortedItems.push_back(Item); }); if (SortedItems.size()) { - std::sort(SortedItems.begin(), SortedItems.end(), Comparator); + llvm::sort(SortedItems, Comparator); size_t SortedItemIdx = 0; forEach([&](T &Item) { Item = SortedItems[SortedItemIdx++]; }); diff --git a/llvm/lib/ExecutionEngine/Orc/Core.cpp b/llvm/lib/ExecutionEngine/Orc/Core.cpp index f03dd434b704b2..6ff009aec1390f 100644 --- a/llvm/lib/ExecutionEngine/Orc/Core.cpp +++ b/llvm/lib/ExecutionEngine/Orc/Core.cpp @@ -1155,8 +1155,9 @@ void JITDylib::dump(raw_ostream &OS) { std::vector> SymbolsSorted; for (auto &KV : Symbols) SymbolsSorted.emplace_back(KV.first, &KV.second); - std::sort(SymbolsSorted.begin(), SymbolsSorted.end(), - [](const auto &L, const auto &R) { return *L.first < *R.first; }); + llvm::sort(SymbolsSorted, [](const auto &L, const auto &R) { + return *L.first < *R.first; + }); for (auto &KV : SymbolsSorted) { OS << " \"" << *KV.first << "\": "; diff --git a/llvm/lib/ExecutionEngine/Orc/Debugging/DebugInfoSupport.cpp b/llvm/lib/ExecutionEngine/Orc/Debugging/DebugInfoSupport.cpp index 5a058bd712a3e3..60efa0cb6f53a8 100644 --- a/llvm/lib/ExecutionEngine/Orc/Debugging/DebugInfoSupport.cpp +++ b/llvm/lib/ExecutionEngine/Orc/Debugging/DebugInfoSupport.cpp @@ -50,7 +50,7 @@ static void preserveDWARFSection(LinkGraph &G, Section &Sec) { static SmallVector getSectionData(Section &Sec) { SmallVector SecData; SmallVector SecBlocks(Sec.blocks().begin(), Sec.blocks().end()); - std::sort(SecBlocks.begin(), SecBlocks.end(), [](Block *LHS, Block *RHS) { + llvm::sort(SecBlocks, [](Block *LHS, Block *RHS) { return LHS->getAddress() < RHS->getAddress(); }); // Convert back to what object file would have, one blob of section content diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp index e18ce5d373d1cb..987fbad30720b0 100644 --- a/llvm/lib/ProfileData/InstrProfReader.cpp +++ b/llvm/lib/ProfileData/InstrProfReader.cpp @@ -476,7 +476,7 @@ RawInstrProfReader::getTemporalProfTraces( return TemporalProfTraces; } // Sort functions by their timestamps to build the trace. - std::sort(TemporalProfTimestamps.begin(), TemporalProfTimestamps.end()); + llvm::sort(TemporalProfTimestamps); TemporalProfTraceTy Trace; if (Weight) Trace.Weight = *Weight; diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp index 86f28a5057694d..d65087edc6dc96 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp @@ -612,10 +612,9 @@ void PipelineSolver::populateReadyList( } if (UseCostHeur) { - std::sort(ReadyList.begin(), ReadyList.end(), - [](std::pair A, std::pair B) { - return A.second < B.second; - }); + llvm::sort(ReadyList, [](std::pair A, std::pair B) { + return A.second < B.second; + }); } assert(ReadyList.size() == CurrSU.second.size()); diff --git a/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp b/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp index e81e6bb6975886..ccb60348df3e24 100644 --- a/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp +++ b/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp @@ -6886,8 +6886,8 @@ bool ARMPipelinerLoopInfo::tooMuchRegisterPressure(SwingSchedulerDAG &SSD, ++Stage) { std::deque Instrs = SMS.getInstructions(Cycle + Stage * SMS.getInitiationInterval()); - std::sort(Instrs.begin(), Instrs.end(), - [](SUnit *A, SUnit *B) { return A->NodeNum > B->NodeNum; }); + llvm::sort(Instrs, + [](SUnit *A, SUnit *B) { return A->NodeNum > B->NodeNum; }); for (SUnit *SU : Instrs) ProposedSchedule.push_back(SU); } diff --git a/llvm/lib/Target/NVPTX/NVVMReflect.cpp b/llvm/lib/Target/NVPTX/NVVMReflect.cpp index 4024953bb51dbc..c02e4872e4b1a0 100644 --- a/llvm/lib/Target/NVPTX/NVVMReflect.cpp +++ b/llvm/lib/Target/NVPTX/NVVMReflect.cpp @@ -208,7 +208,7 @@ static bool runNVVMReflect(Function &F, unsigned SmVersion) { // Removing via isInstructionTriviallyDead may add duplicates to the ToRemove // array. Filter out the duplicates before starting to erase from parent. - std::sort(ToRemove.begin(), ToRemove.end()); + llvm::sort(ToRemove); auto NewLastIter = llvm::unique(ToRemove); ToRemove.erase(NewLastIter, ToRemove.end()); diff --git a/llvm/lib/Target/PowerPC/PPCMergeStringPool.cpp b/llvm/lib/Target/PowerPC/PPCMergeStringPool.cpp index 309938accdf4c3..af028de47d9372 100644 --- a/llvm/lib/Target/PowerPC/PPCMergeStringPool.cpp +++ b/llvm/lib/Target/PowerPC/PPCMergeStringPool.cpp @@ -244,7 +244,7 @@ bool PPCMergeStringPool::mergeModuleStringPool(Module &M) { return false; // Sort the global constants to make access more efficient. - std::sort(MergeableStrings.begin(), MergeableStrings.end(), CompareConstants); + llvm::sort(MergeableStrings, CompareConstants); SmallVector ConstantsInStruct; for (GlobalVariable *GV : MergeableStrings) diff --git a/llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp b/llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp index c1e5ab1a2b5618..0001d0750d9c1c 100644 --- a/llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp +++ b/llvm/lib/Transforms/IPO/MemProfContextDisambiguation.cpp @@ -2203,7 +2203,7 @@ void CallsiteContextGraph::ContextNode::print( // Make a copy of the computed context ids that we can sort for stability. auto ContextIds = getContextIds(); std::vector SortedIds(ContextIds.begin(), ContextIds.end()); - std::sort(SortedIds.begin(), SortedIds.end()); + llvm::sort(SortedIds); for (auto Id : SortedIds) OS << " " << Id; OS << "\n"; @@ -2238,7 +2238,7 @@ void CallsiteContextGraph::ContextEdge::print( << " AllocTypes: " << getAllocTypeString(AllocTypes); OS << " ContextIds:"; std::vector SortedIds(ContextIds.begin(), ContextIds.end()); - std::sort(SortedIds.begin(), SortedIds.end()); + llvm::sort(SortedIds); for (auto Id : SortedIds) OS << " " << Id; } @@ -2380,7 +2380,7 @@ struct DOTGraphTraits *> std::string IdString = "ContextIds:"; if (ContextIds.size() < 100) { std::vector SortedIds(ContextIds.begin(), ContextIds.end()); - std::sort(SortedIds.begin(), SortedIds.end()); + llvm::sort(SortedIds); for (auto Id : SortedIds) IdString += (" " + Twine(Id)).str(); } else { diff --git a/llvm/lib/Transforms/Utils/CodeLayout.cpp b/llvm/lib/Transforms/Utils/CodeLayout.cpp index 95edd27c675d24..dbadad3905ac7e 100644 --- a/llvm/lib/Transforms/Utils/CodeLayout.cpp +++ b/llvm/lib/Transforms/Utils/CodeLayout.cpp @@ -986,16 +986,15 @@ class ExtTSPImpl { } // Sorting chains by density in the decreasing order. - std::sort(SortedChains.begin(), SortedChains.end(), - [&](const ChainT *L, const ChainT *R) { - // Place the entry point at the beginning of the order. - if (L->isEntry() != R->isEntry()) - return L->isEntry(); - - // Compare by density and break ties by chain identifiers. - return std::make_tuple(-L->density(), L->Id) < - std::make_tuple(-R->density(), R->Id); - }); + llvm::sort(SortedChains, [&](const ChainT *L, const ChainT *R) { + // Place the entry point at the beginning of the order. + if (L->isEntry() != R->isEntry()) + return L->isEntry(); + + // Compare by density and break ties by chain identifiers. + return std::make_tuple(-L->density(), L->Id) < + std::make_tuple(-R->density(), R->Id); + }); // Collect the nodes in the order specified by their chains. std::vector Order; @@ -1355,14 +1354,12 @@ class CDSortImpl { } // Sort chains by density in the decreasing order. - std::sort(SortedChains.begin(), SortedChains.end(), - [&](const ChainT *L, const ChainT *R) { - const double DL = ChainDensity[L]; - const double DR = ChainDensity[R]; - // Compare by density and break ties by chain identifiers. - return std::make_tuple(-DL, L->Id) < - std::make_tuple(-DR, R->Id); - }); + llvm::sort(SortedChains, [&](const ChainT *L, const ChainT *R) { + const double DL = ChainDensity[L]; + const double DR = ChainDensity[R]; + // Compare by density and break ties by chain identifiers. + return std::make_tuple(-DL, L->Id) < std::make_tuple(-DR, R->Id); + }); // Collect the nodes in the order specified by their chains. std::vector Order; diff --git a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp index bff05b9ca4bebc..acf663a6c0e8f1 100644 --- a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp +++ b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp @@ -1259,10 +1259,9 @@ Error Session::FileInfo::registerMultiStubEntry( Sym.getTargetFlags()); // Let's keep stubs ordered by ascending address. - std::sort(Entry.begin(), Entry.end(), - [](const MemoryRegionInfo &L, const MemoryRegionInfo &R) { - return L.getTargetAddress() < R.getTargetAddress(); - }); + llvm::sort(Entry, [](const MemoryRegionInfo &L, const MemoryRegionInfo &R) { + return L.getTargetAddress() < R.getTargetAddress(); + }); return Error::success(); } diff --git a/llvm/utils/TableGen/ARMTargetDefEmitter.cpp b/llvm/utils/TableGen/ARMTargetDefEmitter.cpp index e22f353b451f9f..c4143747f37705 100644 --- a/llvm/utils/TableGen/ARMTargetDefEmitter.cpp +++ b/llvm/utils/TableGen/ARMTargetDefEmitter.cpp @@ -45,7 +45,7 @@ static void EmitARMTargetDef(RecordKeeper &RK, raw_ostream &OS) { const auto MarchB = B->getValueAsString("MArchName"); return MarchA.compare(MarchB) < 0; // A lexographically less than B }; - std::sort(SortedExtensions.begin(), SortedExtensions.end(), Alphabetical); + llvm::sort(SortedExtensions, Alphabetical); // The ARMProcFamilyEnum values are initialised by SubtargetFeature defs // which set the ARMProcFamily field. We can generate the enum from these defs diff --git a/llvm/utils/TableGen/ExegesisEmitter.cpp b/llvm/utils/TableGen/ExegesisEmitter.cpp index d48c7f3a480f24..be2c43047f6c0d 100644 --- a/llvm/utils/TableGen/ExegesisEmitter.cpp +++ b/llvm/utils/TableGen/ExegesisEmitter.cpp @@ -140,8 +140,7 @@ void ExegesisEmitter::emitPfmCountersInfo(const Record &Def, ValidationCounter->getValueAsDef("EventType")->getName(), getPfmCounterId(ValidationCounter->getValueAsString("Counter"))}); } - std::sort(ValidationCounters.begin(), ValidationCounters.end(), - EventNumberLess); + llvm::sort(ValidationCounters, EventNumberLess); OS << "\nstatic const std::pair " << Target << Def.getName() << "ValidationCounters[] = {\n"; for (const ValidationCounterInfo &VCI : ValidationCounters) { From 9bd413d17dffc3a4b95b167bbf840e9fc751088e Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sun, 23 Jun 2024 10:37:26 -0700 Subject: [PATCH 2/2] Use llvm::less_second. --- llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp index d65087edc6dc96..b26a15b7fd8997 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUIGroupLP.cpp @@ -612,9 +612,7 @@ void PipelineSolver::populateReadyList( } if (UseCostHeur) { - llvm::sort(ReadyList, [](std::pair A, std::pair B) { - return A.second < B.second; - }); + llvm::sort(ReadyList, llvm::less_second()); } assert(ReadyList.size() == CurrSU.second.size());