llvm-project/bolt/lib/Passes/AllocCombiner.cpp
Amir Ayupov 52cf07116b
[BOLT][NFC] Log through JournalingStreams (#81524)
Make core BOLT functionality more friendly to being used as a
library instead of in our standalone driver llvm-bolt. To
accomplish this, we augment BinaryContext with journaling streams
that are to be used by most BOLT code whenever something needs to
be logged to the screen. Users of the library can decide if logs
should be printed to a file, no file or to the screen, as
before. To illustrate this, this patch adds a new option
`--log-file` that allows the user to redirect BOLT logging to a
file on disk or completely hide it by using
`--log-file=/dev/null`. Future BOLT code should now use
`BinaryContext::outs()` for printing important messages instead of
`llvm::outs()`. A new test log.test enforces this by verifying that
no strings are print to screen once the `--log-file` option is
used.

In previous patches we also added a new BOLTError class to report
common and fatal errors, so code shouldn't call exit(1) now. To
easily handle problems as before (by quitting with exit(1)),
callers can now use
`BinaryContext::logBOLTErrorsAndQuitOnFatal(Error)` whenever code
needs to deal with BOLT errors. To test this, we have fatal.s
that checks we are correctly quitting and printing a fatal error
to the screen.

Because this is a significant change by itself, not all code was
yet ported. Code from Profiler libs (DataAggregator and friends)
still print errors directly to screen.

Co-authored-by: Rafael Auler <rafaelauler@fb.com>

Test Plan: NFC
2024-02-12 14:53:53 -08:00

122 lines
3.6 KiB
C++

//===- bolt/Passes/AllocCombiner.cpp --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the AllocCombinerPass class.
//
//===----------------------------------------------------------------------===//
#include "bolt/Passes/AllocCombiner.h"
#define DEBUG_TYPE "alloccombiner"
using namespace llvm;
namespace opts {
extern cl::opt<bolt::FrameOptimizationType> FrameOptimization;
} // end namespace opts
namespace llvm {
namespace bolt {
static bool getStackAdjustmentSize(const BinaryContext &BC, const MCInst &Inst,
int64_t &Adjustment) {
return BC.MIB->evaluateStackOffsetExpr(
Inst, Adjustment, std::make_pair(BC.MIB->getStackPointer(), 0LL),
std::make_pair(0, 0LL));
}
static bool isIndifferentToSP(const MCInst &Inst, const BinaryContext &BC) {
if (BC.MIB->isCFI(Inst))
return true;
const MCInstrDesc &II = BC.MII->get(Inst.getOpcode());
if (BC.MIB->isTerminator(Inst) ||
II.hasImplicitDefOfPhysReg(BC.MIB->getStackPointer(), BC.MRI.get()) ||
II.hasImplicitUseOfPhysReg(BC.MIB->getStackPointer()))
return false;
for (const MCOperand &Operand : MCPlus::primeOperands(Inst))
if (Operand.isReg() && Operand.getReg() == BC.MIB->getStackPointer())
return false;
return true;
}
static bool shouldProcess(const BinaryFunction &Function) {
return Function.isSimple() && Function.hasCFG() && !Function.isIgnored();
}
static void runForAllWeCare(std::map<uint64_t, BinaryFunction> &BFs,
std::function<void(BinaryFunction &)> Task) {
for (auto &It : BFs) {
BinaryFunction &Function = It.second;
if (shouldProcess(Function))
Task(Function);
}
}
void AllocCombinerPass::combineAdjustments(BinaryFunction &BF) {
BinaryContext &BC = BF.getBinaryContext();
for (BinaryBasicBlock &BB : BF) {
MCInst *Prev = nullptr;
for (MCInst &Inst : llvm::reverse(BB)) {
if (isIndifferentToSP(Inst, BC))
continue; // Skip updating Prev
int64_t Adjustment = 0LL;
if (!Prev || !BC.MIB->isStackAdjustment(Inst) ||
!BC.MIB->isStackAdjustment(*Prev) ||
!getStackAdjustmentSize(BC, *Prev, Adjustment)) {
Prev = &Inst;
continue;
}
LLVM_DEBUG({
dbgs() << "At \"" << BF.getPrintName() << "\", combining: \n";
Inst.dump();
Prev->dump();
dbgs() << "Adjustment: " << Adjustment << "\n";
});
if (BC.MIB->isSUB(Inst))
Adjustment = -Adjustment;
BC.MIB->addToImm(Inst, Adjustment, BC.Ctx.get());
LLVM_DEBUG({
dbgs() << "After adjustment:\n";
Inst.dump();
});
BB.eraseInstruction(BB.findInstruction(Prev));
++NumCombined;
DynamicCountCombined += BB.getKnownExecutionCount();
FuncsChanged.insert(&BF);
Prev = &Inst;
}
}
}
Error AllocCombinerPass::runOnFunctions(BinaryContext &BC) {
if (opts::FrameOptimization == FOP_NONE)
return Error::success();
runForAllWeCare(BC.getBinaryFunctions(), [&](BinaryFunction &Function) {
combineAdjustments(Function);
});
BC.outs() << "BOLT-INFO: Allocation combiner: " << NumCombined
<< " empty spaces coalesced (dyn count: " << DynamicCountCombined
<< ").\n";
return Error::success();
}
} // end namespace bolt
} // end namespace llvm