Skip to content

Commit 2f6e3df

Browse files
committed
BPSectionOrderer: stabilize iteration order and node order
Exposed by the test added in the reverted llvm#120514 * Fix libstdc++/libc++ differences due to nth_element. llvm#125450 (comment) * Fix LLVM_ENABLE_REVERSE_ITERATION=1 differences * Fix potential issue in `currentSize += D::getSize(*sections[*sectionIdxs.begin()])` where DenseSet was used, though not covered by a test
1 parent bac62ee commit 2f6e3df

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

lld/MachO/BPSectionOrderer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ DenseMap<const InputSection *, int> lld::macho::runBalancedPartitioning(
110110
bool compressionSortStartupFunctions, bool verbose) {
111111
// Collect candidate sections and associated symbols.
112112
SmallVector<InputSection *> sections;
113-
DenseMap<CachedHashStringRef, DenseSet<unsigned>> rootSymbolToSectionIdxs;
113+
DenseMap<CachedHashStringRef, std::set<unsigned>> rootSymbolToSectionIdxs;
114114
for (const auto *file : inputFiles) {
115115
for (auto *sec : file->sections) {
116116
for (auto &subsec : sec->subsections) {

lld/include/lld/Common/BPSectionOrdererBase.inc

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "lld/Common/ErrorHandler.h"
2323
#include "llvm/ADT/CachedHashString.h"
2424
#include "llvm/ADT/DenseMap.h"
25-
#include "llvm/ADT/DenseSet.h"
25+
#include "llvm/ADT/MapVector.h"
2626
#include "llvm/ADT/SetVector.h"
2727
#include "llvm/ADT/SmallSet.h"
2828
#include "llvm/ADT/SmallVector.h"
@@ -35,6 +35,7 @@
3535
#include "llvm/Support/VirtualFileSystem.h"
3636
#include <memory>
3737
#include <optional>
38+
#include <set>
3839

3940
#define DEBUG_TYPE "bp-section-orderer"
4041

@@ -60,7 +61,7 @@ template <class D> struct BPOrderer {
6061
bool forDataCompression,
6162
bool compressionSortStartupFunctions, bool verbose,
6263
llvm::ArrayRef<Section *> sections,
63-
const DenseMap<CachedHashStringRef, DenseSet<unsigned>>
64+
const DenseMap<CachedHashStringRef, std::set<unsigned>>
6465
&rootSymbolToSectionIdxs)
6566
-> llvm::DenseMap<const Section *, int>;
6667
};
@@ -73,7 +74,7 @@ static SmallVector<std::pair<unsigned, UtilityNodes>> getUnsForCompression(
7374
ArrayRef<const typename D::Section *> sections,
7475
const DenseMap<const void *, uint64_t> &sectionToIdx,
7576
ArrayRef<unsigned> sectionIdxs,
76-
DenseMap<unsigned, SmallVector<unsigned>> *duplicateSectionIdxs,
77+
DenseMap<unsigned, SmallVector<unsigned, 0>> *duplicateSectionIdxs,
7778
BPFunctionNode::UtilityNodeT &maxUN) {
7879
TimeTraceScope timeScope("Build nodes for compression");
7980

@@ -88,7 +89,7 @@ static SmallVector<std::pair<unsigned, UtilityNodes>> getUnsForCompression(
8889
hashes.clear();
8990
}
9091

91-
DenseMap<uint64_t, unsigned> hashFrequency;
92+
MapVector<uint64_t, unsigned> hashFrequency;
9293
for (auto &[sectionIdx, hashes] : sectionHashes)
9394
for (auto hash : hashes)
9495
++hashFrequency[hash];
@@ -156,7 +157,7 @@ auto BPOrderer<D>::computeOrder(
156157
StringRef profilePath, bool forFunctionCompression, bool forDataCompression,
157158
bool compressionSortStartupFunctions, bool verbose,
158159
ArrayRef<Section *> sections,
159-
const DenseMap<CachedHashStringRef, DenseSet<unsigned>>
160+
const DenseMap<CachedHashStringRef, std::set<unsigned>>
160161
&rootSymbolToSectionIdxs) -> DenseMap<const Section *, int> {
161162
TimeTraceScope timeScope("Setup Balanced Partitioning");
162163
DenseMap<const void *, uint64_t> sectionToIdx;
@@ -257,7 +258,7 @@ auto BPOrderer<D>::computeOrder(
257258

258259
// Map a section index (order directly) to a list of duplicate section indices
259260
// (not ordered directly).
260-
DenseMap<unsigned, SmallVector<unsigned>> duplicateSectionIdxs;
261+
DenseMap<unsigned, SmallVector<unsigned, 0>> duplicateSectionIdxs;
261262
auto unsForFunctionCompression = getUnsForCompression<D>(
262263
sections, sectionToIdx, sectionIdxsForFunctionCompression,
263264
&duplicateSectionIdxs, maxUN);

llvm/lib/Support/BalancedPartitioning.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ void BalancedPartitioning::split(const FunctionNodeRange Nodes,
305305
unsigned NumNodes = std::distance(Nodes.begin(), Nodes.end());
306306
auto NodesMid = Nodes.begin() + (NumNodes + 1) / 2;
307307

308-
std::nth_element(Nodes.begin(), NodesMid, Nodes.end(), [](auto &L, auto &R) {
308+
llvm::sort(Nodes.begin(), Nodes.end(), [](auto &L, auto &R) {
309309
return L.InputOrderIndex < R.InputOrderIndex;
310310
});
311311

0 commit comments

Comments
 (0)