Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bolt/include/bolt/Core/BinaryContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ using namespace object;
namespace bolt {

class BinaryFunction;
class BoltAddressTranslation;

/// Information on loadable part of the file.
struct SegmentInfo {
Expand Down Expand Up @@ -280,6 +281,8 @@ class BinaryContext {
/// Internal helper for removing section name from a lookup table.
void deregisterSectionName(const BinarySection &Section);

BoltAddressTranslation *BAT = nullptr;

public:
static Expected<std::unique_ptr<BinaryContext>> createBinaryContext(
Triple TheTriple, std::shared_ptr<orc::SymbolStringPool> SSP,
Expand Down Expand Up @@ -1559,6 +1562,10 @@ class BinaryContext {
return *IOAddressMap;
}

void setBAT(BoltAddressTranslation *B) { BAT = B; }

BoltAddressTranslation *getBAT() const { return BAT; }

raw_ostream &outs() const { return Logger.Out; }

raw_ostream &errs() const { return Logger.Err; }
Expand Down
9 changes: 9 additions & 0 deletions bolt/include/bolt/Core/BinaryFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2498,6 +2498,15 @@ class BinaryFunction {
void
computeBlockHashes(HashFunction HashFunction = HashFunction::Default) const;

// Updates the BAT BBHashMap for a list of newly inserted \p BBs by
// mapping the InputOffset of each new block to the Index and Hash of the
// \p OriginBB. This ensures that new blocks generated by optimizations (like
// ICP) are treated as part of the original block for address translation
// purposes.
void updateInsertedBBHashMap(
const BinaryBasicBlock &OriginBB,
const std::vector<std::unique_ptr<BinaryBasicBlock>> &BBs);

void addDWARFUnit(DWARFUnit *Unit) { DwarfUnitMap[Unit->getOffset()] = Unit; }

void removeDWARFUnit(DWARFUnit *Unit) {
Expand Down
1 change: 1 addition & 0 deletions bolt/lib/Passes/IndirectCallPromotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,7 @@ Error IndirectCallPromotion::runOnFunctions(BinaryContext &BC) {
std::vector<std::unique_ptr<BinaryBasicBlock>> NewBBs =
rewriteCall(*BB, Inst, std::move(ICPcode), MethodInfo.second);

Function.updateInsertedBBHashMap(*BB, NewBBs);
// Fix the CFG after inserting the new basic blocks.
BinaryBasicBlock *MergeBlock =
fixCFG(*BB, IsTailCall, IsJumpTable, std::move(NewBBs), Targets);
Expand Down
22 changes: 22 additions & 0 deletions bolt/lib/Profile/StaleProfileMatching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
//===----------------------------------------------------------------------===//

#include "bolt/Core/HashUtilities.h"
#include "bolt/Profile/BoltAddressTranslation.h"
#include "bolt/Profile/YAMLProfileReader.h"
#include "bolt/Utils/CommandLineOpts.h"
#include "llvm/ADT/Bitfields.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/MC/MCPseudoProbe.h"
Expand Down Expand Up @@ -440,6 +442,26 @@ void BinaryFunction::computeBlockHashes(HashFunction HashFunction) const {
BB->setHash(BlendedHashes[I].combine());
}
}

void BinaryFunction::updateInsertedBBHashMap(
const BinaryBasicBlock &OriginBB,
const std::vector<std::unique_ptr<BinaryBasicBlock>> &BBs) {
if (!opts::EnableBAT || !OriginBB.getFunction() || !OriginBB.getHash())
return;

auto &BBHashMap =
BC.getBAT()->getBBHashMap(OriginBB.getFunction()->getAddress());

for (const auto &BB : BBs) {
if (!BB)
continue;
// Update BB and BBHashMap using the original BB's index and hash.
BB->setHash(OriginBB.getHash());
Copy link
Contributor Author

@Jinjie-Huang Jinjie-Huang Dec 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update inserted BBs' information as well, since they may be used as OriginBB later.

BB->setIndex(OriginBB.getIndex());
BBHashMap.addEntry(BB->getInputOffset(), BB->getIndex(), BB->getHash());
}
}

// TODO: mediate the difference between flow function construction here in BOLT
// and in the compiler by splitting blocks with exception throwing calls at the
// call and adding the landing pad as the successor.
Expand Down
1 change: 1 addition & 0 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ RewriteInstance::RewriteInstance(ELFObjectFileBase *File, const int Argc,
BC->MII.get(), BC->MRI.get(), BC->STI.get())));

BAT = std::make_unique<BoltAddressTranslation>();
BC->setBAT(BAT.get());

if (opts::UpdateDebugSections)
DebugInfoRewriter = std::make_unique<DWARFRewriter>(*BC);
Expand Down
11 changes: 11 additions & 0 deletions bolt/test/X86/icp-inline.s
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@
# CHECK-ICP-INLINE: callq *(%rcx,%rax,8)
# CHECK-ICP-INLINE-NOT: callq bar
# CHECK-ICP-INLINE: End of Function "main"

# Checks BB hash has been updated correctly after ICP optimization
# RUN: llvm-bolt %t.exe --icp=calls --icp-calls-topn=1 --inline-small-functions\
# RUN: -o %t.null --lite=0 \
# RUN: --inline-small-functions-bytes=4 --icp-inline --print-icp \
# RUN: --data %t.fdata --enable-bat
# RUN: llvm-bat-dump --dump-all %t.null | FileCheck %s --check-prefix=CHECK-BB-HASH
# CHECK-BB-HASH: 0x17 -> 0xd hash: 0x5b24d9a60000
# CHECK-BB-HASH: 0x1d -> 0xd hash: 0x5b24d9a60000
# CHECK-BB-HASH: 0x1f -> 0xd hash: 0x5b24d9a60000

.globl bar
bar:
.cfi_startproc
Expand Down
Loading