Skip to content

Commit d2c9a19

Browse files
authored
[BOLT][NFC] Pass BF/BB hashes to BAT
Test Plan: NFC Reviewers: dcci, rafaelauler, maksfb, ayermolo Reviewed By: rafaelauler Pull Request: #76906
1 parent 55e6c19 commit d2c9a19

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

bolt/include/bolt/Profile/BoltAddressTranslation.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ class BoltAddressTranslation {
111111
/// addresses when aggregating profile
112112
bool enabledFor(llvm::object::ELFObjectFileBase *InputFile) const;
113113

114+
/// Save function and basic block hashes used for metadata dump.
115+
void saveMetadata(BinaryContext &BC);
116+
114117
private:
115118
/// Helper to update \p Map by inserting one or more BAT entries reflecting
116119
/// \p BB for function located at \p FuncAddress. At least one entry will be
@@ -140,6 +143,9 @@ class BoltAddressTranslation {
140143

141144
std::map<uint64_t, MapTy> Maps;
142145

146+
using BBHashMap = std::unordered_map<uint32_t, size_t>;
147+
std::unordered_map<uint64_t, std::pair<size_t, BBHashMap>> FuncHashes;
148+
143149
/// Links outlined cold bocks to their original function
144150
std::map<uint64_t, uint64_t> ColdPartSource;
145151

bolt/lib/Profile/BoltAddressTranslation.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,5 +424,20 @@ bool BoltAddressTranslation::enabledFor(
424424
}
425425
return false;
426426
}
427+
428+
void BoltAddressTranslation::saveMetadata(BinaryContext &BC) {
429+
for (BinaryFunction &BF : llvm::make_second_range(BC.getBinaryFunctions())) {
430+
// We don't need a translation table if the body of the function hasn't
431+
// changed
432+
if (BF.isIgnored() || (!BC.HasRelocations && !BF.isSimple()))
433+
continue;
434+
// Prepare function and block hashes
435+
FuncHashes[BF.getAddress()].first = BF.computeHash();
436+
BF.computeBlockHashes();
437+
for (const BinaryBasicBlock &BB : BF)
438+
FuncHashes[BF.getAddress()].second.emplace(BB.getInputOffset(),
439+
BB.getHash());
440+
}
441+
}
427442
} // namespace bolt
428443
} // namespace llvm

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,10 @@ Error RewriteInstance::run() {
748748

749749
processProfileData();
750750

751+
// Save input binary metadata if BAT section needs to be emitted
752+
if (opts::EnableBAT)
753+
BAT->saveMetadata(*BC);
754+
751755
postProcessFunctions();
752756

753757
processMetadataPostCFG();

0 commit comments

Comments
 (0)