2017-10-21 02:57:46 +02:00
|
|
|
//===- IndirectCallPromotion.cpp - Optimizations based on value profiling -===//
|
2016-04-28 01:20:27 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// 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
|
2016-04-28 01:20:27 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the transformation that promotes indirect calls to
|
|
|
|
// conditional direct calls when the indirect-call value profile metadata is
|
|
|
|
// available.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-08-11 19:20:18 +02:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2017-10-21 02:57:46 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2016-04-28 01:20:27 +02:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2016-08-11 19:20:18 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2016-07-12 23:13:44 +02:00
|
|
|
#include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
|
2019-01-07 08:15:51 +01:00
|
|
|
#include "llvm/Analysis/IndirectCallVisitor.h"
|
2017-10-10 01:19:02 +02:00
|
|
|
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
|
2017-08-08 22:57:33 +02:00
|
|
|
#include "llvm/Analysis/ProfileSummaryInfo.h"
|
2017-10-21 02:57:46 +02:00
|
|
|
#include "llvm/IR/Attributes.h"
|
2016-08-11 19:20:18 +02:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2016-04-28 01:20:27 +02:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
2016-08-11 19:20:18 +02:00
|
|
|
#include "llvm/IR/Function.h"
|
2016-04-28 01:20:27 +02:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2016-08-11 19:20:18 +02:00
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
#include "llvm/IR/Instruction.h"
|
2016-04-28 01:20:27 +02:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2016-08-11 19:20:18 +02:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2016-04-28 01:20:27 +02:00
|
|
|
#include "llvm/IR/MDBuilder.h"
|
2016-08-11 19:20:18 +02:00
|
|
|
#include "llvm/IR/PassManager.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2017-10-21 02:57:46 +02:00
|
|
|
#include "llvm/IR/Value.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
2016-04-28 01:20:27 +02:00
|
|
|
#include "llvm/Pass.h"
|
2016-08-11 19:20:18 +02:00
|
|
|
#include "llvm/ProfileData/InstrProf.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2016-04-28 01:20:27 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
2019-01-07 08:15:51 +01:00
|
|
|
#include "llvm/Support/Error.h"
|
2017-10-21 02:57:46 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-04-28 01:20:27 +02:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2018-03-23 23:11:06 +01:00
|
|
|
#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
|
2016-04-28 01:20:27 +02:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2017-12-06 22:22:54 +01:00
|
|
|
#include "llvm/Transforms/Utils/CallPromotionUtils.h"
|
2016-08-11 19:20:18 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
2017-10-21 02:57:46 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
2016-04-28 01:20:27 +02:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-06-02 03:52:05 +02:00
|
|
|
#define DEBUG_TYPE "pgo-icall-prom"
|
2016-04-28 01:20:27 +02:00
|
|
|
|
|
|
|
STATISTIC(NumOfPGOICallPromotion, "Number of indirect call promotions.");
|
|
|
|
STATISTIC(NumOfPGOICallsites, "Number of indirect call candidate sites.");
|
|
|
|
|
|
|
|
// Command line option to disable indirect-call promotion with the default as
|
|
|
|
// false. This is for debug purpose.
|
|
|
|
static cl::opt<bool> DisableICP("disable-icp", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Disable indirect call promotion"));
|
|
|
|
|
|
|
|
// Set the cutoff value for the promotion. If the value is other than 0, we
|
|
|
|
// stop the transformation once the total number of promotions equals the cutoff
|
|
|
|
// value.
|
|
|
|
// For debug use only.
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
ICPCutOff("icp-cutoff", cl::init(0), cl::Hidden, cl::ZeroOrMore,
|
2017-05-06 00:31:11 +02:00
|
|
|
cl::desc("Max number of promotions for this compilation"));
|
2016-04-28 01:20:27 +02:00
|
|
|
|
|
|
|
// If ICPCSSkip is non zero, the first ICPCSSkip callsites will be skipped.
|
|
|
|
// For debug use only.
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
ICPCSSkip("icp-csskip", cl::init(0), cl::Hidden, cl::ZeroOrMore,
|
2017-05-06 00:31:11 +02:00
|
|
|
cl::desc("Skip Callsite up to this number for this compilation"));
|
2016-04-28 01:20:27 +02:00
|
|
|
|
|
|
|
// Set if the pass is called in LTO optimization. The difference for LTO mode
|
|
|
|
// is the pass won't prefix the source module name to the internal linkage
|
|
|
|
// symbols.
|
|
|
|
static cl::opt<bool> ICPLTOMode("icp-lto", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Run indirect-call promotion in LTO "
|
|
|
|
"mode"));
|
2016-07-12 23:13:44 +02:00
|
|
|
|
2017-02-23 23:15:18 +01:00
|
|
|
// Set if the pass is called in SamplePGO mode. The difference for SamplePGO
|
|
|
|
// mode is it will add prof metadatato the created direct call.
|
|
|
|
static cl::opt<bool>
|
|
|
|
ICPSamplePGOMode("icp-samplepgo", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Run indirect-call promotion in SamplePGO mode"));
|
|
|
|
|
2016-04-28 01:20:27 +02:00
|
|
|
// If the option is set to true, only call instructions will be considered for
|
|
|
|
// transformation -- invoke instructions will be ignored.
|
|
|
|
static cl::opt<bool>
|
|
|
|
ICPCallOnly("icp-call-only", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Run indirect-call promotion for call instructions "
|
|
|
|
"only"));
|
|
|
|
|
|
|
|
// If the option is set to true, only invoke instructions will be considered for
|
|
|
|
// transformation -- call instructions will be ignored.
|
|
|
|
static cl::opt<bool> ICPInvokeOnly("icp-invoke-only", cl::init(false),
|
|
|
|
cl::Hidden,
|
|
|
|
cl::desc("Run indirect-call promotion for "
|
|
|
|
"invoke instruction only"));
|
|
|
|
|
|
|
|
// Dump the function level IR if the transformation happened in this
|
|
|
|
// function. For debug use only.
|
|
|
|
static cl::opt<bool>
|
|
|
|
ICPDUMPAFTER("icp-dumpafter", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("Dump IR after transformation happens"));
|
|
|
|
|
|
|
|
namespace {
|
2017-10-21 02:57:46 +02:00
|
|
|
|
2016-05-15 03:04:24 +02:00
|
|
|
class PGOIndirectCallPromotionLegacyPass : public ModulePass {
|
2016-04-28 01:20:27 +02:00
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
2017-02-23 23:15:18 +01:00
|
|
|
PGOIndirectCallPromotionLegacyPass(bool InLTO = false, bool SamplePGO = false)
|
|
|
|
: ModulePass(ID), InLTO(InLTO), SamplePGO(SamplePGO) {
|
2016-05-15 03:04:24 +02:00
|
|
|
initializePGOIndirectCallPromotionLegacyPassPass(
|
|
|
|
*PassRegistry::getPassRegistry());
|
2016-04-28 01:20:27 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 22:57:33 +02:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<ProfileSummaryInfoWrapperPass>();
|
|
|
|
}
|
|
|
|
|
2016-10-01 04:56:57 +02:00
|
|
|
StringRef getPassName() const override { return "PGOIndirectCallPromotion"; }
|
2016-04-28 01:20:27 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool runOnModule(Module &M) override;
|
|
|
|
|
|
|
|
// If this pass is called in LTO. We need to special handling the PGOFuncName
|
|
|
|
// for the static variables due to LTO's internalization.
|
|
|
|
bool InLTO;
|
2017-02-23 23:15:18 +01:00
|
|
|
|
|
|
|
// If this pass is called in SamplePGO. We need to add the prof metadata to
|
|
|
|
// the promoted direct call.
|
|
|
|
bool SamplePGO;
|
2016-04-28 01:20:27 +02:00
|
|
|
};
|
2017-10-21 02:57:46 +02:00
|
|
|
|
2016-04-28 01:20:27 +02:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2016-05-15 03:04:24 +02:00
|
|
|
char PGOIndirectCallPromotionLegacyPass::ID = 0;
|
2017-10-21 02:57:46 +02:00
|
|
|
|
2017-08-15 01:25:21 +02:00
|
|
|
INITIALIZE_PASS_BEGIN(PGOIndirectCallPromotionLegacyPass, "pgo-icall-prom",
|
|
|
|
"Use PGO instrumentation profile to promote indirect "
|
|
|
|
"calls to direct calls.",
|
|
|
|
false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(PGOIndirectCallPromotionLegacyPass, "pgo-icall-prom",
|
|
|
|
"Use PGO instrumentation profile to promote indirect "
|
|
|
|
"calls to direct calls.",
|
|
|
|
false, false)
|
2016-04-28 01:20:27 +02:00
|
|
|
|
2017-02-23 23:15:18 +01:00
|
|
|
ModulePass *llvm::createPGOIndirectCallPromotionLegacyPass(bool InLTO,
|
|
|
|
bool SamplePGO) {
|
|
|
|
return new PGOIndirectCallPromotionLegacyPass(InLTO, SamplePGO);
|
2016-04-28 01:20:27 +02:00
|
|
|
}
|
|
|
|
|
2016-05-15 17:18:11 +02:00
|
|
|
namespace {
|
2017-10-21 02:57:46 +02:00
|
|
|
|
2016-04-28 01:20:27 +02:00
|
|
|
// The class for main data structure to promote indirect calls to conditional
|
|
|
|
// direct calls.
|
|
|
|
class ICallPromotionFunc {
|
|
|
|
private:
|
|
|
|
Function &F;
|
|
|
|
Module *M;
|
|
|
|
|
|
|
|
// Symtab that maps indirect call profile values to function names and
|
|
|
|
// defines.
|
|
|
|
InstrProfSymtab *Symtab;
|
|
|
|
|
2017-02-23 23:15:18 +01:00
|
|
|
bool SamplePGO;
|
|
|
|
|
2017-07-27 18:54:15 +02:00
|
|
|
OptimizationRemarkEmitter &ORE;
|
2016-04-28 01:20:27 +02:00
|
|
|
|
|
|
|
// A struct that records the direct target and it's call count.
|
|
|
|
struct PromotionCandidate {
|
|
|
|
Function *TargetFunction;
|
|
|
|
uint64_t Count;
|
2017-10-21 02:57:46 +02:00
|
|
|
|
2016-04-28 01:20:27 +02:00
|
|
|
PromotionCandidate(Function *F, uint64_t C) : TargetFunction(F), Count(C) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if the indirect-call call site should be promoted. Return the number
|
2016-07-12 23:13:44 +02:00
|
|
|
// of promotions. Inst is the candidate indirect call, ValueDataRef
|
|
|
|
// contains the array of value profile data for profiled targets,
|
|
|
|
// TotalCount is the total profiled count of call executions, and
|
|
|
|
// NumCandidates is the number of candidate entries in ValueDataRef.
|
2016-04-28 01:20:27 +02:00
|
|
|
std::vector<PromotionCandidate> getPromotionCandidatesForCallSite(
|
|
|
|
Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef,
|
2016-07-12 23:13:44 +02:00
|
|
|
uint64_t TotalCount, uint32_t NumCandidates);
|
2016-04-28 01:20:27 +02:00
|
|
|
|
|
|
|
// Promote a list of targets for one indirect-call callsite. Return
|
|
|
|
// the number of promotions.
|
2020-04-28 19:25:13 +02:00
|
|
|
uint32_t tryToPromote(CallBase &CB,
|
2016-04-28 01:20:27 +02:00
|
|
|
const std::vector<PromotionCandidate> &Candidates,
|
|
|
|
uint64_t &TotalCount);
|
|
|
|
|
|
|
|
public:
|
2017-02-23 23:15:18 +01:00
|
|
|
ICallPromotionFunc(Function &Func, Module *Modu, InstrProfSymtab *Symtab,
|
2017-07-27 18:54:15 +02:00
|
|
|
bool SamplePGO, OptimizationRemarkEmitter &ORE)
|
|
|
|
: F(Func), M(Modu), Symtab(Symtab), SamplePGO(SamplePGO), ORE(ORE) {}
|
2017-10-21 02:57:46 +02:00
|
|
|
ICallPromotionFunc(const ICallPromotionFunc &) = delete;
|
|
|
|
ICallPromotionFunc &operator=(const ICallPromotionFunc &) = delete;
|
2016-08-11 19:20:18 +02:00
|
|
|
|
2017-08-08 22:57:33 +02:00
|
|
|
bool processFunction(ProfileSummaryInfo *PSI);
|
2016-04-28 01:20:27 +02:00
|
|
|
};
|
2017-10-21 02:57:46 +02:00
|
|
|
|
2016-05-15 17:18:11 +02:00
|
|
|
} // end anonymous namespace
|
2016-04-28 01:20:27 +02:00
|
|
|
|
|
|
|
// Indirect-call promotion heuristic. The direct targets are sorted based on
|
|
|
|
// the count. Stop at the first target that is not promoted.
|
[llvm][NFC] Refactor uses of CallSite to CallBase - call promotion
Summary:
Updated CallPromotionUtils and impacted sites. Parameters that are
expected to be non-null, and return values that are guranteed non-null,
were replaced with CallBase references rather than pointers.
Left FIXME in places where more changes are facilitated by CallBase, but
aren't CallSites: Instruction* parameters or return values, for example,
where the contract that they are actually CallBase values.
Reviewers: davidxl, dblaikie, wmi
Reviewed By: dblaikie
Subscribers: arsenm, jvesely, nhaehnle, eraman, hiraditya, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77930
2020-04-12 03:07:50 +02:00
|
|
|
// FIXME(callsite): the Instruction* parameter can be changed to CallBase
|
2016-04-28 01:20:27 +02:00
|
|
|
std::vector<ICallPromotionFunc::PromotionCandidate>
|
|
|
|
ICallPromotionFunc::getPromotionCandidatesForCallSite(
|
|
|
|
Instruction *Inst, const ArrayRef<InstrProfValueData> &ValueDataRef,
|
2016-07-12 23:13:44 +02:00
|
|
|
uint64_t TotalCount, uint32_t NumCandidates) {
|
2016-04-28 01:20:27 +02:00
|
|
|
std::vector<PromotionCandidate> Ret;
|
|
|
|
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " \nWork on callsite #" << NumOfPGOICallsites << *Inst
|
|
|
|
<< " Num_targets: " << ValueDataRef.size()
|
|
|
|
<< " Num_candidates: " << NumCandidates << "\n");
|
2016-04-28 01:20:27 +02:00
|
|
|
NumOfPGOICallsites++;
|
|
|
|
if (ICPCSSkip != 0 && NumOfPGOICallsites <= ICPCSSkip) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " Skip: User options.\n");
|
2016-04-28 01:20:27 +02:00
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2016-07-12 23:13:44 +02:00
|
|
|
for (uint32_t I = 0; I < NumCandidates; I++) {
|
2016-04-28 01:20:27 +02:00
|
|
|
uint64_t Count = ValueDataRef[I].Count;
|
|
|
|
assert(Count <= TotalCount);
|
|
|
|
uint64_t Target = ValueDataRef[I].Value;
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
|
|
|
|
<< " Target_func: " << Target << "\n");
|
2016-04-28 01:20:27 +02:00
|
|
|
|
2019-04-05 18:16:23 +02:00
|
|
|
if (ICPInvokeOnly && isa<CallInst>(Inst)) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " Not promote: User options.\n");
|
2017-10-11 19:12:59 +02:00
|
|
|
ORE.emit([&]() {
|
|
|
|
return OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst)
|
|
|
|
<< " Not promote: User options";
|
|
|
|
});
|
2016-07-17 16:46:58 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-04-05 18:16:23 +02:00
|
|
|
if (ICPCallOnly && isa<InvokeInst>(Inst)) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " Not promote: User option.\n");
|
2017-10-11 19:12:59 +02:00
|
|
|
ORE.emit([&]() {
|
|
|
|
return OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst)
|
|
|
|
<< " Not promote: User options";
|
|
|
|
});
|
2016-07-17 16:46:58 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-04-28 01:20:27 +02:00
|
|
|
if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " Not promote: Cutoff reached.\n");
|
2017-10-11 19:12:59 +02:00
|
|
|
ORE.emit([&]() {
|
|
|
|
return OptimizationRemarkMissed(DEBUG_TYPE, "CutOffReached", Inst)
|
|
|
|
<< " Not promote: Cutoff reached";
|
|
|
|
});
|
2017-07-27 18:54:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Function *TargetFunction = Symtab->getFunction(Target);
|
|
|
|
if (TargetFunction == nullptr) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " Not promote: Cannot find the target\n");
|
2017-10-11 19:12:59 +02:00
|
|
|
ORE.emit([&]() {
|
|
|
|
return OptimizationRemarkMissed(DEBUG_TYPE, "UnableToFindTarget", Inst)
|
2018-08-24 23:38:24 +02:00
|
|
|
<< "Cannot promote indirect call: target with md5sum "
|
|
|
|
<< ore::NV("target md5sum", Target) << " not found";
|
2017-10-11 19:12:59 +02:00
|
|
|
});
|
2016-04-28 01:20:27 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-07-27 18:54:15 +02:00
|
|
|
|
2017-01-30 23:46:37 +01:00
|
|
|
const char *Reason = nullptr;
|
[llvm][NFC] Refactor uses of CallSite to CallBase - call promotion
Summary:
Updated CallPromotionUtils and impacted sites. Parameters that are
expected to be non-null, and return values that are guranteed non-null,
were replaced with CallBase references rather than pointers.
Left FIXME in places where more changes are facilitated by CallBase, but
aren't CallSites: Instruction* parameters or return values, for example,
where the contract that they are actually CallBase values.
Reviewers: davidxl, dblaikie, wmi
Reviewed By: dblaikie
Subscribers: arsenm, jvesely, nhaehnle, eraman, hiraditya, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77930
2020-04-12 03:07:50 +02:00
|
|
|
if (!isLegalToPromote(*cast<CallBase>(Inst), TargetFunction, &Reason)) {
|
2017-07-27 18:54:15 +02:00
|
|
|
using namespace ore;
|
2017-10-21 02:57:46 +02:00
|
|
|
|
2017-10-11 19:12:59 +02:00
|
|
|
ORE.emit([&]() {
|
|
|
|
return OptimizationRemarkMissed(DEBUG_TYPE, "UnableToPromote", Inst)
|
2017-07-27 18:54:15 +02:00
|
|
|
<< "Cannot promote indirect call to "
|
|
|
|
<< NV("TargetFunction", TargetFunction) << " with count of "
|
2017-10-11 19:12:59 +02:00
|
|
|
<< NV("Count", Count) << ": " << Reason;
|
|
|
|
});
|
2016-04-28 01:20:27 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-07-27 18:54:15 +02:00
|
|
|
|
2016-04-28 01:20:27 +02:00
|
|
|
Ret.push_back(PromotionCandidate(TargetFunction, Count));
|
|
|
|
TotalCount -= Count;
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2020-04-28 19:25:13 +02:00
|
|
|
CallBase &llvm::pgo::promoteIndirectCall(CallBase &CB, Function *DirectCallee,
|
|
|
|
uint64_t Count, uint64_t TotalCount,
|
|
|
|
bool AttachProfToDirectCall,
|
|
|
|
OptimizationRemarkEmitter *ORE) {
|
2016-04-28 01:20:27 +02:00
|
|
|
|
|
|
|
uint64_t ElseCount = TotalCount - Count;
|
|
|
|
uint64_t MaxCount = (Count >= ElseCount ? Count : ElseCount);
|
|
|
|
uint64_t Scale = calculateCountScale(MaxCount);
|
2020-04-28 19:25:13 +02:00
|
|
|
MDBuilder MDB(CB.getContext());
|
2016-04-28 01:20:27 +02:00
|
|
|
MDNode *BranchWeights = MDB.createBranchWeights(
|
|
|
|
scaleBranchCount(Count, Scale), scaleBranchCount(ElseCount, Scale));
|
|
|
|
|
2020-04-28 19:25:13 +02:00
|
|
|
CallBase &NewInst =
|
|
|
|
promoteCallWithIfThenElse(CB, DirectCallee, BranchWeights);
|
2016-04-28 01:20:27 +02:00
|
|
|
|
2017-02-23 23:15:18 +01:00
|
|
|
if (AttachProfToDirectCall) {
|
[llvm][NFC] Refactor uses of CallSite to CallBase - call promotion
Summary:
Updated CallPromotionUtils and impacted sites. Parameters that are
expected to be non-null, and return values that are guranteed non-null,
were replaced with CallBase references rather than pointers.
Left FIXME in places where more changes are facilitated by CallBase, but
aren't CallSites: Instruction* parameters or return values, for example,
where the contract that they are actually CallBase values.
Reviewers: davidxl, dblaikie, wmi
Reviewed By: dblaikie
Subscribers: arsenm, jvesely, nhaehnle, eraman, hiraditya, kerbowa, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77930
2020-04-12 03:07:50 +02:00
|
|
|
MDBuilder MDB(NewInst.getContext());
|
|
|
|
NewInst.setMetadata(
|
2019-02-05 01:18:38 +01:00
|
|
|
LLVMContext::MD_prof,
|
|
|
|
MDB.createBranchWeights({static_cast<uint32_t>(Count)}));
|
2017-02-23 23:15:18 +01:00
|
|
|
}
|
|
|
|
|
2017-07-27 18:54:15 +02:00
|
|
|
using namespace ore;
|
2017-10-21 02:57:46 +02:00
|
|
|
|
2017-07-27 18:54:15 +02:00
|
|
|
if (ORE)
|
2017-10-11 19:12:59 +02:00
|
|
|
ORE->emit([&]() {
|
2020-04-28 19:25:13 +02:00
|
|
|
return OptimizationRemark(DEBUG_TYPE, "Promoted", &CB)
|
2017-10-11 19:12:59 +02:00
|
|
|
<< "Promote indirect call to " << NV("DirectCallee", DirectCallee)
|
|
|
|
<< " with count " << NV("Count", Count) << " out of "
|
|
|
|
<< NV("TotalCount", TotalCount);
|
|
|
|
});
|
2020-04-28 19:25:13 +02:00
|
|
|
return NewInst;
|
2016-04-28 01:20:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Promote indirect-call to conditional direct-call for one callsite.
|
|
|
|
uint32_t ICallPromotionFunc::tryToPromote(
|
2020-04-28 19:25:13 +02:00
|
|
|
CallBase &CB, const std::vector<PromotionCandidate> &Candidates,
|
2016-04-28 01:20:27 +02:00
|
|
|
uint64_t &TotalCount) {
|
|
|
|
uint32_t NumPromoted = 0;
|
|
|
|
|
|
|
|
for (auto &C : Candidates) {
|
|
|
|
uint64_t Count = C.Count;
|
2020-04-28 19:25:13 +02:00
|
|
|
pgo::promoteIndirectCall(CB, C.TargetFunction, Count, TotalCount, SamplePGO,
|
|
|
|
&ORE);
|
2016-04-28 01:20:27 +02:00
|
|
|
assert(TotalCount >= Count);
|
|
|
|
TotalCount -= Count;
|
|
|
|
NumOfPGOICallPromotion++;
|
|
|
|
NumPromoted++;
|
|
|
|
}
|
|
|
|
return NumPromoted;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse all the indirect-call callsite and get the value profile
|
|
|
|
// annotation to perform indirect-call promotion.
|
2017-08-08 22:57:33 +02:00
|
|
|
bool ICallPromotionFunc::processFunction(ProfileSummaryInfo *PSI) {
|
2016-04-28 01:20:27 +02:00
|
|
|
bool Changed = false;
|
2016-07-12 23:13:44 +02:00
|
|
|
ICallPromotionAnalysis ICallAnalysis;
|
2020-04-28 19:25:13 +02:00
|
|
|
for (auto *CB : findIndirectCalls(F)) {
|
2016-07-12 23:13:44 +02:00
|
|
|
uint32_t NumVals, NumCandidates;
|
2016-04-28 01:20:27 +02:00
|
|
|
uint64_t TotalCount;
|
2016-07-12 23:13:44 +02:00
|
|
|
auto ICallProfDataRef = ICallAnalysis.getPromotionCandidatesForInstruction(
|
2020-04-28 19:25:13 +02:00
|
|
|
CB, NumVals, TotalCount, NumCandidates);
|
2017-08-08 22:57:33 +02:00
|
|
|
if (!NumCandidates ||
|
|
|
|
(PSI && PSI->hasProfileSummary() && !PSI->isHotCount(TotalCount)))
|
2016-04-28 01:20:27 +02:00
|
|
|
continue;
|
2016-07-12 23:13:44 +02:00
|
|
|
auto PromotionCandidates = getPromotionCandidatesForCallSite(
|
2020-04-28 19:25:13 +02:00
|
|
|
CB, ICallProfDataRef, TotalCount, NumCandidates);
|
|
|
|
uint32_t NumPromoted = tryToPromote(*CB, PromotionCandidates, TotalCount);
|
2016-04-28 01:20:27 +02:00
|
|
|
if (NumPromoted == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
// Adjust the MD.prof metadata. First delete the old one.
|
2020-04-28 19:25:13 +02:00
|
|
|
CB->setMetadata(LLVMContext::MD_prof, nullptr);
|
2016-04-28 01:20:27 +02:00
|
|
|
// If all promoted, we don't need the MD.prof metadata.
|
|
|
|
if (TotalCount == 0 || NumPromoted == NumVals)
|
|
|
|
continue;
|
|
|
|
// Otherwise we need update with the un-promoted records back.
|
2020-04-28 19:25:13 +02:00
|
|
|
annotateValueSite(*M, *CB, ICallProfDataRef.slice(NumPromoted), TotalCount,
|
2016-07-12 23:13:44 +02:00
|
|
|
IPVK_IndirectCallTarget, NumCandidates);
|
2016-04-28 01:20:27 +02:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A wrapper function that does the actual work.
|
2017-08-08 22:57:33 +02:00
|
|
|
static bool promoteIndirectCalls(Module &M, ProfileSummaryInfo *PSI,
|
|
|
|
bool InLTO, bool SamplePGO,
|
2017-07-27 18:54:15 +02:00
|
|
|
ModuleAnalysisManager *AM = nullptr) {
|
2016-04-28 01:20:27 +02:00
|
|
|
if (DisableICP)
|
|
|
|
return false;
|
|
|
|
InstrProfSymtab Symtab;
|
2017-06-20 03:38:56 +02:00
|
|
|
if (Error E = Symtab.create(M, InLTO)) {
|
|
|
|
std::string SymtabFailure = toString(std::move(E));
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Failed to create symtab: " << SymtabFailure << "\n");
|
2017-06-20 03:38:56 +02:00
|
|
|
(void)SymtabFailure;
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-28 01:20:27 +02:00
|
|
|
bool Changed = false;
|
|
|
|
for (auto &F : M) {
|
2019-04-05 00:40:06 +02:00
|
|
|
if (F.isDeclaration() || F.hasOptNone())
|
2016-04-28 01:20:27 +02:00
|
|
|
continue;
|
2017-07-27 18:54:15 +02:00
|
|
|
|
|
|
|
std::unique_ptr<OptimizationRemarkEmitter> OwnedORE;
|
|
|
|
OptimizationRemarkEmitter *ORE;
|
|
|
|
if (AM) {
|
|
|
|
auto &FAM =
|
|
|
|
AM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
|
|
|
|
ORE = &FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
|
|
|
|
} else {
|
2019-08-15 17:54:37 +02:00
|
|
|
OwnedORE = std::make_unique<OptimizationRemarkEmitter>(&F);
|
2017-07-27 18:54:15 +02:00
|
|
|
ORE = OwnedORE.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
ICallPromotionFunc ICallPromotion(F, &M, &Symtab, SamplePGO, *ORE);
|
2017-08-08 22:57:33 +02:00
|
|
|
bool FuncChanged = ICallPromotion.processFunction(PSI);
|
2016-04-28 01:20:27 +02:00
|
|
|
if (ICPDUMPAFTER && FuncChanged) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "\n== IR Dump After =="; F.print(dbgs()));
|
|
|
|
LLVM_DEBUG(dbgs() << "\n");
|
2016-04-28 01:20:27 +02:00
|
|
|
}
|
|
|
|
Changed |= FuncChanged;
|
|
|
|
if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) {
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << " Stop: Cutoff reached.\n");
|
2016-04-28 01:20:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2016-05-15 03:04:24 +02:00
|
|
|
bool PGOIndirectCallPromotionLegacyPass::runOnModule(Module &M) {
|
2017-08-08 22:57:33 +02:00
|
|
|
ProfileSummaryInfo *PSI =
|
2018-11-19 06:23:16 +01:00
|
|
|
&getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
|
2017-08-08 22:57:33 +02:00
|
|
|
|
2016-04-28 01:20:27 +02:00
|
|
|
// Command-line option has the priority for InLTO.
|
2017-08-08 22:57:33 +02:00
|
|
|
return promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode,
|
2017-02-23 23:15:18 +01:00
|
|
|
SamplePGO | ICPSamplePGOMode);
|
2016-04-28 01:20:27 +02:00
|
|
|
}
|
2016-05-16 18:31:07 +02:00
|
|
|
|
2017-02-23 23:15:18 +01:00
|
|
|
PreservedAnalyses PGOIndirectCallPromotion::run(Module &M,
|
|
|
|
ModuleAnalysisManager &AM) {
|
2017-08-08 22:57:33 +02:00
|
|
|
ProfileSummaryInfo *PSI = &AM.getResult<ProfileSummaryAnalysis>(M);
|
|
|
|
|
|
|
|
if (!promoteIndirectCalls(M, PSI, InLTO | ICPLTOMode,
|
|
|
|
SamplePGO | ICPSamplePGOMode, &AM))
|
2016-05-16 18:31:07 +02:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
|
|
|
return PreservedAnalyses::none();
|
|
|
|
}
|