[BOLT][NFC] Minor cleanup in ICP getCallTargets and canPromoteCallsite

Minor refactoring. NFC.

Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D124898
This commit is contained in:
Amir Ayupov 2022-05-04 14:03:24 -07:00
parent 9a8d33dbd8
commit 2ad1c7540e
1 changed files with 17 additions and 14 deletions

View File

@ -206,7 +206,7 @@ std::vector<IndirectCallPromotion::Callsite>
IndirectCallPromotion::getCallTargets(BinaryBasicBlock &BB, IndirectCallPromotion::getCallTargets(BinaryBasicBlock &BB,
const MCInst &Inst) const { const MCInst &Inst) const {
BinaryFunction &BF = *BB.getFunction(); BinaryFunction &BF = *BB.getFunction();
BinaryContext &BC = BF.getBinaryContext(); const BinaryContext &BC = BF.getBinaryContext();
std::vector<Callsite> Targets; std::vector<Callsite> Targets;
if (const JumpTable *JT = BF.getJumpTable(Inst)) { if (const JumpTable *JT = BF.getJumpTable(Inst)) {
@ -512,6 +512,7 @@ IndirectCallPromotion::findCallTargetSymbols(std::vector<Callsite> &Targets,
return SymTargets; return SymTargets;
} }
// Use memory profile to select hot targets.
JumpTableInfoType HotTargets = JumpTableInfoType HotTargets =
maybeGetHotJumpTableTargets(BB, CallInst, TargetFetchInst, JT); maybeGetHotJumpTableTargets(BB, CallInst, TargetFetchInst, JT);
@ -917,25 +918,28 @@ IndirectCallPromotion::fixCFG(BinaryBasicBlock &IndCallBlock,
size_t IndirectCallPromotion::canPromoteCallsite( size_t IndirectCallPromotion::canPromoteCallsite(
const BinaryBasicBlock &BB, const MCInst &Inst, const BinaryBasicBlock &BB, const MCInst &Inst,
const std::vector<Callsite> &Targets, uint64_t NumCalls) { const std::vector<Callsite> &Targets, uint64_t NumCalls) {
BinaryFunction *BF = BB.getFunction();
const BinaryContext &BC = BF->getBinaryContext();
if (BB.getKnownExecutionCount() < opts::ExecutionCountThreshold) if (BB.getKnownExecutionCount() < opts::ExecutionCountThreshold)
return 0; return 0;
const bool IsJumpTable = BB.getFunction()->getJumpTable(Inst); const bool IsJumpTable = BF->getJumpTable(Inst);
auto computeStats = [&](size_t N) { auto computeStats = [&](size_t N) {
for (size_t I = 0; I < N; ++I) for (size_t I = 0; I < N; ++I)
if (!IsJumpTable) if (IsJumpTable)
TotalNumFrequentCalls += Targets[I].Branches;
else
TotalNumFrequentJmps += Targets[I].Branches; TotalNumFrequentJmps += Targets[I].Branches;
else
TotalNumFrequentCalls += Targets[I].Branches;
}; };
// If we have no targets (or no calls), skip this callsite. // If we have no targets (or no calls), skip this callsite.
if (Targets.empty() || !NumCalls) { if (Targets.empty() || !NumCalls) {
if (opts::Verbosity >= 1) { if (opts::Verbosity >= 1) {
const ptrdiff_t InstIdx = &Inst - &(*BB.begin()); const ptrdiff_t InstIdx = &Inst - &(*BB.begin());
outs() << "BOLT-INFO: ICP failed in " << *BB.getFunction() << " @ " outs() << "BOLT-INFO: ICP failed in " << *BF << " @ " << InstIdx << " in "
<< InstIdx << " in " << BB.getName() << ", calls = " << NumCalls << BB.getName() << ", calls = " << NumCalls
<< ", targets empty or NumCalls == 0.\n"; << ", targets empty or NumCalls == 0.\n";
} }
return 0; return 0;
@ -950,7 +954,6 @@ size_t IndirectCallPromotion::canPromoteCallsite(
const size_t TrialN = TopN ? std::min(TopN, Targets.size()) : Targets.size(); const size_t TrialN = TopN ? std::min(TopN, Targets.size()) : Targets.size();
if (opts::ICPTopCallsites > 0) { if (opts::ICPTopCallsites > 0) {
BinaryContext &BC = BB.getFunction()->getBinaryContext();
if (!BC.MIB->hasAnnotation(Inst, "DoICP")) if (!BC.MIB->hasAnnotation(Inst, "DoICP"))
return 0; return 0;
} }
@ -978,8 +981,8 @@ size_t IndirectCallPromotion::canPromoteCallsite(
if (TopNFrequency == 0 || TopNFrequency < opts::ICPMispredictThreshold) { if (TopNFrequency == 0 || TopNFrequency < opts::ICPMispredictThreshold) {
if (opts::Verbosity >= 1) { if (opts::Verbosity >= 1) {
const ptrdiff_t InstIdx = &Inst - &(*BB.begin()); const ptrdiff_t InstIdx = &Inst - &(*BB.begin());
outs() << "BOLT-INFO: ICP failed in " << *BB.getFunction() << " @ " outs() << "BOLT-INFO: ICP failed in " << *BF << " @ " << InstIdx
<< InstIdx << " in " << BB.getName() << ", calls = " << NumCalls << " in " << BB.getName() << ", calls = " << NumCalls
<< ", top N mis. frequency " << format("%.1f", TopNFrequency) << ", top N mis. frequency " << format("%.1f", TopNFrequency)
<< "% < " << opts::ICPMispredictThreshold << "%\n"; << "% < " << opts::ICPMispredictThreshold << "%\n";
} }
@ -1024,9 +1027,9 @@ size_t IndirectCallPromotion::canPromoteCallsite(
if (TopNMispredictFrequency < opts::ICPMispredictThreshold) { if (TopNMispredictFrequency < opts::ICPMispredictThreshold) {
if (opts::Verbosity >= 1) { if (opts::Verbosity >= 1) {
const ptrdiff_t InstIdx = &Inst - &(*BB.begin()); const ptrdiff_t InstIdx = &Inst - &(*BB.begin());
outs() << "BOLT-INFO: ICP failed in " << *BB.getFunction() << " @ " outs() << "BOLT-INFO: ICP failed in " << *BF << " @ " << InstIdx
<< InstIdx << " in " << BB.getName() << " in " << BB.getName() << ", calls = " << NumCalls
<< ", calls = " << NumCalls << ", top N mispredict frequency " << ", top N mispredict frequency "
<< format("%.1f", TopNMispredictFrequency) << "% < " << format("%.1f", TopNMispredictFrequency) << "% < "
<< opts::ICPMispredictThreshold << "%\n"; << opts::ICPMispredictThreshold << "%\n";
} }
@ -1038,7 +1041,7 @@ size_t IndirectCallPromotion::canPromoteCallsite(
// Filter functions that can have ICP applied (for debugging) // Filter functions that can have ICP applied (for debugging)
if (!opts::ICPFuncsList.empty()) { if (!opts::ICPFuncsList.empty()) {
for (std::string &Name : opts::ICPFuncsList) for (std::string &Name : opts::ICPFuncsList)
if (BB.getFunction()->hasName(Name)) if (BF->hasName(Name))
return N; return N;
return 0; return 0;
} }