BPSectionOrderer: stabilize iteration order and node order

Exposed by the test added in the reverted #120514

* Fix libstdc++/libc++ differences due to nth_element. https://github.com/llvm/llvm-project/pull/125450#issuecomment-2631404178
* 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
This commit is contained in:
Fangrui Song
2025-02-03 10:36:51 -08:00
parent bac62ee5b4
commit 2f6e3df08a
3 changed files with 9 additions and 8 deletions

View File

@@ -110,7 +110,7 @@ DenseMap<const InputSection *, int> lld::macho::runBalancedPartitioning(
bool compressionSortStartupFunctions, bool verbose) {
// Collect candidate sections and associated symbols.
SmallVector<InputSection *> sections;
DenseMap<CachedHashStringRef, DenseSet<unsigned>> rootSymbolToSectionIdxs;
DenseMap<CachedHashStringRef, std::set<unsigned>> rootSymbolToSectionIdxs;
for (const auto *file : inputFiles) {
for (auto *sec : file->sections) {
for (auto &subsec : sec->subsections) {

View File

@@ -22,7 +22,7 @@
#include "lld/Common/ErrorHandler.h"
#include "llvm/ADT/CachedHashString.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
@@ -35,6 +35,7 @@
#include "llvm/Support/VirtualFileSystem.h"
#include <memory>
#include <optional>
#include <set>
#define DEBUG_TYPE "bp-section-orderer"
@@ -60,7 +61,7 @@ template <class D> struct BPOrderer {
bool forDataCompression,
bool compressionSortStartupFunctions, bool verbose,
llvm::ArrayRef<Section *> sections,
const DenseMap<CachedHashStringRef, DenseSet<unsigned>>
const DenseMap<CachedHashStringRef, std::set<unsigned>>
&rootSymbolToSectionIdxs)
-> llvm::DenseMap<const Section *, int>;
};
@@ -73,7 +74,7 @@ static SmallVector<std::pair<unsigned, UtilityNodes>> getUnsForCompression(
ArrayRef<const typename D::Section *> sections,
const DenseMap<const void *, uint64_t> &sectionToIdx,
ArrayRef<unsigned> sectionIdxs,
DenseMap<unsigned, SmallVector<unsigned>> *duplicateSectionIdxs,
DenseMap<unsigned, SmallVector<unsigned, 0>> *duplicateSectionIdxs,
BPFunctionNode::UtilityNodeT &maxUN) {
TimeTraceScope timeScope("Build nodes for compression");
@@ -88,7 +89,7 @@ static SmallVector<std::pair<unsigned, UtilityNodes>> getUnsForCompression(
hashes.clear();
}
DenseMap<uint64_t, unsigned> hashFrequency;
MapVector<uint64_t, unsigned> hashFrequency;
for (auto &[sectionIdx, hashes] : sectionHashes)
for (auto hash : hashes)
++hashFrequency[hash];
@@ -156,7 +157,7 @@ auto BPOrderer<D>::computeOrder(
StringRef profilePath, bool forFunctionCompression, bool forDataCompression,
bool compressionSortStartupFunctions, bool verbose,
ArrayRef<Section *> sections,
const DenseMap<CachedHashStringRef, DenseSet<unsigned>>
const DenseMap<CachedHashStringRef, std::set<unsigned>>
&rootSymbolToSectionIdxs) -> DenseMap<const Section *, int> {
TimeTraceScope timeScope("Setup Balanced Partitioning");
DenseMap<const void *, uint64_t> sectionToIdx;
@@ -257,7 +258,7 @@ auto BPOrderer<D>::computeOrder(
// Map a section index (order directly) to a list of duplicate section indices
// (not ordered directly).
DenseMap<unsigned, SmallVector<unsigned>> duplicateSectionIdxs;
DenseMap<unsigned, SmallVector<unsigned, 0>> duplicateSectionIdxs;
auto unsForFunctionCompression = getUnsForCompression<D>(
sections, sectionToIdx, sectionIdxsForFunctionCompression,
&duplicateSectionIdxs, maxUN);

View File

@@ -305,7 +305,7 @@ void BalancedPartitioning::split(const FunctionNodeRange Nodes,
unsigned NumNodes = std::distance(Nodes.begin(), Nodes.end());
auto NodesMid = Nodes.begin() + (NumNodes + 1) / 2;
std::nth_element(Nodes.begin(), NodesMid, Nodes.end(), [](auto &L, auto &R) {
llvm::sort(Nodes.begin(), Nodes.end(), [](auto &L, auto &R) {
return L.InputOrderIndex < R.InputOrderIndex;
});