Skip to content

Commit 60e4d24

Browse files
authored
[lld-macho,BalancedPartition] Simplify relocation hash and avoid xxHash
xxHash, inferior to xxh3, is discouraged. We try not to use xxhash in lld. Switch to read32le for content hash and xxh3/stable_hash_combine for relocation hash. Remove the intermediate std::string for relocation hash. Change the tail hashing scheme to consider individual bytes instead. This helps group 0102 and 0201 together. The benefit is negligible, though. Pull Request: #121729
1 parent 94fee13 commit 60e4d24

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

lld/MachO/BPSectionOrderer.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
#include "Symbols.h"
2020
#include "lld/Common/BPSectionOrdererBase.h"
2121
#include "llvm/ADT/DenseMap.h"
22+
#include "llvm/ADT/StableHashing.h"
2223
#include "llvm/ADT/StringRef.h"
24+
#include "llvm/Support/Endian.h"
25+
#include "llvm/Support/xxhash.h"
2326

2427
namespace lld::macho {
2528

@@ -90,23 +93,24 @@ class BPSectionMacho : public BPSectionBase {
9093
&sectionToIdx) const override {
9194
constexpr unsigned windowSize = 4;
9295

93-
// Calculate content hashes
94-
size_t dataSize = isec->data.size();
95-
for (size_t i = 0; i < dataSize; i++) {
96-
auto window = isec->data.drop_front(i).take_front(windowSize);
97-
hashes.push_back(xxHash64(window));
98-
}
96+
// Calculate content hashes: k-mers and the last k-1 bytes.
97+
ArrayRef<uint8_t> data = isec->data;
98+
if (data.size() >= windowSize)
99+
for (size_t i = 0; i <= data.size() - windowSize; ++i)
100+
hashes.push_back(llvm::support::endian::read32le(data.data() + i));
101+
for (uint8_t byte : data.take_back(windowSize - 1))
102+
hashes.push_back(byte);
99103

100104
// Calculate relocation hashes
101105
for (const auto &r : isec->relocs) {
102-
if (r.length == 0 || r.referent.isNull() || r.offset >= isec->data.size())
106+
if (r.length == 0 || r.referent.isNull() || r.offset >= data.size())
103107
continue;
104108

105109
uint64_t relocHash = getRelocHash(r, sectionToIdx);
106110
uint32_t start = (r.offset < windowSize) ? 0 : r.offset - windowSize + 1;
107111
for (uint32_t i = start; i < r.offset + r.length; i++) {
108-
auto window = isec->data.drop_front(i).take_front(windowSize);
109-
hashes.push_back(xxHash64(window) + relocHash);
112+
auto window = data.drop_front(i).take_front(windowSize);
113+
hashes.push_back(xxh3_64bits(window) ^ relocHash);
110114
}
111115
}
112116

@@ -124,19 +128,17 @@ class BPSectionMacho : public BPSectionBase {
124128
std::optional<uint64_t> sectionIdx;
125129
if (auto it = sectionToIdx.find(isec); it != sectionToIdx.end())
126130
sectionIdx = it->second;
127-
std::string kind;
131+
uint64_t kind = -1, value = 0;
128132
if (isec)
129-
kind = ("Section " + Twine(isec->kind())).str();
133+
kind = uint64_t(isec->kind());
130134

131135
if (auto *sym = reloc.referent.dyn_cast<Symbol *>()) {
132-
kind += (" Symbol " + Twine(sym->kind())).str();
133-
if (auto *d = llvm::dyn_cast<Defined>(sym)) {
134-
return BPSectionBase::getRelocHash(kind, sectionIdx.value_or(0),
135-
d->value, reloc.addend);
136-
}
136+
kind = (kind << 8) | uint8_t(sym->kind());
137+
if (auto *d = llvm::dyn_cast<Defined>(sym))
138+
value = d->value;
137139
}
138-
return BPSectionBase::getRelocHash(kind, sectionIdx.value_or(0), 0,
139-
reloc.addend);
140+
return llvm::stable_hash_combine(kind, sectionIdx.value_or(0), value,
141+
reloc.addend);
140142
}
141143
};
142144

lld/include/lld/Common/BPSectionOrdererBase.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "llvm/ADT/SmallVector.h"
1919
#include "llvm/ADT/StringRef.h"
2020
#include "llvm/ADT/Twine.h"
21-
#include "llvm/Support/xxhash.h"
2221
#include <memory>
2322
#include <optional>
2423

@@ -56,14 +55,6 @@ class BPSectionBase {
5655
return P1;
5756
}
5857

59-
static uint64_t getRelocHash(llvm::StringRef kind, uint64_t sectionIdx,
60-
uint64_t offset, uint64_t addend) {
61-
return llvm::xxHash64((kind + ": " + llvm::Twine::utohexstr(sectionIdx) +
62-
" + " + llvm::Twine::utohexstr(offset) + " + " +
63-
llvm::Twine::utohexstr(addend))
64-
.str());
65-
}
66-
6758
/// Reorders sections using balanced partitioning algorithm based on profile
6859
/// data.
6960
static llvm::DenseMap<const BPSectionBase *, int>

0 commit comments

Comments
 (0)