Skip to content
Merged
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
9 changes: 6 additions & 3 deletions bolt/include/bolt/Core/BinaryContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ class BinaryContext {
/// Store all functions in the binary, sorted by original address.
std::map<uint64_t, BinaryFunction> BinaryFunctions;

/// Functions to be considered for the output in a sorted order.
BinaryFunctionListType OutputFunctions;

/// A mutex that is used to control parallel accesses to BinaryFunctions.
mutable llvm::sys::RWMutex BinaryFunctionsMutex;

Expand Down Expand Up @@ -554,6 +557,9 @@ class BinaryContext {
return BinaryFunctions;
}

/// Return functions meant for the output in a sorted order.
BinaryFunctionListType &getOutputBinaryFunctions() { return OutputFunctions; }

/// Create BOLT-injected function
BinaryFunction *createInjectedBinaryFunction(const std::string &Name,
bool IsSimple = true);
Expand Down Expand Up @@ -1387,9 +1393,6 @@ class BinaryContext {
unsigned addDebugFilenameToUnit(const uint32_t DestCUID,
const uint32_t SrcCUID, unsigned FileIndex);

/// Return functions in output layout order
BinaryFunctionListType getSortedFunctions();

/// Do the best effort to calculate the size of the function by emitting
/// its code, and relaxing branch instructions. By default, branch
/// instructions are updated to match the layout. Pass \p FixBranches set to
Expand Down
9 changes: 9 additions & 0 deletions bolt/include/bolt/Passes/BinaryPasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ class FixupBranches : public BinaryFunctionPass {
Error runOnFunctions(BinaryContext &BC) override;
};

/// Initialize the output function list.
class PopulateOutputFunctions : public BinaryFunctionPass {
public:
explicit PopulateOutputFunctions() : BinaryFunctionPass(false) {}

const char *getName() const override { return "populate-output-functions"; }
Error runOnFunctions(BinaryContext &BC) override;
};

/// Fix the CFI state and exception handling information after all other
/// passes have completed.
class FinalizeFunctions : public BinaryFunctionPass {
Expand Down
14 changes: 4 additions & 10 deletions bolt/lib/Core/BinaryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1715,16 +1715,6 @@ unsigned BinaryContext::addDebugFilenameToUnit(const uint32_t DestCUID,
DestCUID, DstUnit->getVersion()));
}

BinaryFunctionListType BinaryContext::getSortedFunctions() {
BinaryFunctionListType SortedFunctions(BinaryFunctions.size());
llvm::transform(llvm::make_second_range(BinaryFunctions),
SortedFunctions.begin(),
[](BinaryFunction &BF) { return &BF; });

llvm::stable_sort(SortedFunctions, compareBinaryFunctionByIndex);
return SortedFunctions;
}

BinaryFunctionListType BinaryContext::getAllBinaryFunctions() {
BinaryFunctionListType AllFunctions;
AllFunctions.reserve(BinaryFunctions.size() + InjectedBinaryFunctions.size());
Expand Down Expand Up @@ -2569,6 +2559,10 @@ BinaryContext::createInjectedBinaryFunction(const std::string &Name,
BinaryFunction *BF = InjectedBinaryFunctions.back();
setSymbolToFunctionMap(BF->getSymbol(), BF);
BF->CurrentState = BinaryFunction::State::CFG;

if (!getOutputBinaryFunctions().empty())
getOutputBinaryFunctions().push_back(BF);

return BF;
}

Expand Down
6 changes: 1 addition & 5 deletions bolt/lib/Core/BinaryEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,7 @@ void BinaryEmitter::emitFunctions() {
}

// Emit functions in sorted order.
BinaryFunctionListType SortedFunctions = BC.getSortedFunctions();
emit(SortedFunctions);

// Emit functions added by BOLT.
emit(BC.getInjectedBinaryFunctions());
emit(BC.getOutputBinaryFunctions());

// Mark the end of hot text.
if (opts::HotText) {
Expand Down
22 changes: 22 additions & 0 deletions bolt/lib/Passes/BinaryPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,28 @@ Error FixupBranches::runOnFunctions(BinaryContext &BC) {
return Error::success();
}

Error PopulateOutputFunctions::runOnFunctions(BinaryContext &BC) {
BinaryFunctionListType &OutputFunctions = BC.getOutputBinaryFunctions();

assert(OutputFunctions.empty() && "Output function list already initialized");

OutputFunctions.reserve(BC.getBinaryFunctions().size() +
BC.getInjectedBinaryFunctions().size());
llvm::transform(llvm::make_second_range(BC.getBinaryFunctions()),
std::back_inserter(OutputFunctions),
[](BinaryFunction &BF) { return &BF; });

llvm::erase_if(OutputFunctions,
[&BC](BinaryFunction *BF) { return !BC.shouldEmit(*BF); });

llvm::stable_sort(OutputFunctions, compareBinaryFunctionByIndex);

llvm::copy(BC.getInjectedBinaryFunctions(),
std::back_inserter(OutputFunctions));

return Error::success();
}

Error FinalizeFunctions::runOnFunctions(BinaryContext &BC) {
std::atomic<bool> HasFatal{false};
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
Expand Down
2 changes: 1 addition & 1 deletion bolt/lib/Passes/LongJmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ Error LongJmpPass::runOnFunctions(BinaryContext &BC) {
}

BC.outs() << "BOLT-INFO: Starting stub-insertion pass\n";
BinaryFunctionListType Sorted = BC.getSortedFunctions();
BinaryFunctionListType Sorted = BC.getOutputBinaryFunctions();
bool Modified;
uint32_t Iterations = 0;
do {
Expand Down
6 changes: 3 additions & 3 deletions bolt/lib/Passes/SplitFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ struct SplitCacheDirected final : public SplitStrategy {
}

void initializeAuxiliaryVariables() {
for (BinaryFunction *BF : BC.getSortedFunctions()) {
for (BinaryFunction *BF : BC.getOutputBinaryFunctions()) {
if (!shouldConsiderForCallGraph(*BF))
continue;

Expand Down Expand Up @@ -234,7 +234,7 @@ struct SplitCacheDirected final : public SplitStrategy {
void buildCallGraph() {
Callers.resize(TotalNumBlocks);
Callees.resize(TotalNumBlocks);
for (const BinaryFunction *SrcFunction : BC.getSortedFunctions()) {
for (const BinaryFunction *SrcFunction : BC.getOutputBinaryFunctions()) {
if (!shouldConsiderForCallGraph(*SrcFunction))
continue;

Expand Down Expand Up @@ -337,7 +337,7 @@ struct SplitCacheDirected final : public SplitStrategy {
const BinaryBasicBlock *ThisBB = &(ThisBF->front());
const size_t ThisGI = GlobalIndices[ThisBB];

for (const BinaryFunction *DstBF : BC.getSortedFunctions()) {
for (const BinaryFunction *DstBF : BC.getOutputBinaryFunctions()) {
if (!shouldConsiderForCallGraph(*DstBF))
continue;

Expand Down
3 changes: 3 additions & 0 deletions bolt/lib/Rewrite/BinaryPassManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,9 @@ Error BinaryFunctionPassManager::runAllPasses(BinaryContext &BC) {
Manager.registerPass(
std::make_unique<ReorderFunctions>(PrintReorderedFunctions));

// Produce the list of functions for the output file in a sorted order.
Manager.registerPass(std::make_unique<PopulateOutputFunctions>());

// This is the second run of the SplitFunctions pass required by certain
// splitting strategies (e.g. cdsplit). Running the SplitFunctions pass again
// after ReorderFunctions allows the finalized function order to be utilized
Expand Down
1 change: 1 addition & 0 deletions bolt/lib/Rewrite/MachORewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ void MachORewriteInstance::runOptimizationPasses() {
std::make_unique<ReorderBasicBlocks>(opts::PrintReordered));
Manager.registerPass(
std::make_unique<FixupBranches>(opts::PrintAfterBranchFixup));
Manager.registerPass(std::make_unique<PopulateOutputFunctions>());
// This pass should always run last.*
Manager.registerPass(
std::make_unique<FinalizeFunctions>(opts::PrintFinalized));
Expand Down
2 changes: 1 addition & 1 deletion bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3996,7 +3996,7 @@ void RewriteInstance::emitAndLink() {

if (opts::PrintCacheMetrics) {
BC->outs() << "BOLT-INFO: cache metrics after emitting functions:\n";
CacheMetrics::printAll(BC->outs(), BC->getSortedFunctions());
CacheMetrics::printAll(BC->outs(), BC->getAllBinaryFunctions());
}
}

Expand Down
Loading