aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/IPO/SampleProfile.cpp
diff options
context:
space:
mode:
authorDiego Novillo <dnovillo@google.com>2015-10-26 18:52:53 +0000
committerDiego Novillo <dnovillo@google.com>2015-10-26 18:52:53 +0000
commit7963ea19964b9bf5db32c269fc131c188364ac8b (patch)
tree56342debc481579e5c0cc09ab7e7487271f21f9f /llvm/lib/Transforms/IPO/SampleProfile.cpp
parentcd236b8cc69a123aa155de208c11a17b15e92786 (diff)
downloadllvm-7963ea19964b9bf5db32c269fc131c188364ac8b.zip
llvm-7963ea19964b9bf5db32c269fc131c188364ac8b.tar.gz
llvm-7963ea19964b9bf5db32c269fc131c188364ac8b.tar.bz2
SamplePGO - Add optimization reports.
This adds a couple of optimization remarks to the SamplePGO transformation. When it decides to inline a hot function (to mimic the inline stack and repeat useful inline decisions in the original build). It will also report branch destinations. For instance, given the code fragment: 6 if (i < 1000) 7 sum -= i; 8 else 9 sum += -i * rand(); If the 'else' branch is taken most of the time, building this code with -Rpass=sample-profile will produce: a.cc:9:14: remark: most popular destination for conditional branches at small.cc:6:9 [-Rpass=sample-profile] sum += -i * rand(); ^ llvm-svn: 251330
Diffstat (limited to 'llvm/lib/Transforms/IPO/SampleProfile.cpp')
-rw-r--r--llvm/lib/Transforms/IPO/SampleProfile.cpp36
1 files changed, 30 insertions, 6 deletions
diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp
index b797321..29fc926 100644
--- a/llvm/lib/Transforms/IPO/SampleProfile.cpp
+++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp
@@ -387,6 +387,7 @@ SampleProfileLoader::findFunctionSamples(const Instruction &Inst) const {
/// \returns True if there is any inline happened.
bool SampleProfileLoader::inlineHotFunctions(Function &F) {
bool Changed = false;
+ LLVMContext &Ctx = F.getContext();
while (true) {
bool LocalChanged = false;
SmallVector<CallInst *, 10> CIS;
@@ -403,8 +404,17 @@ bool SampleProfileLoader::inlineHotFunctions(Function &F) {
}
for (auto CI : CIS) {
InlineFunctionInfo IFI;
- if (InlineFunction(CI, IFI))
+ Function *CalledFunction = CI->getCalledFunction();
+ DebugLoc DLoc = CI->getDebugLoc();
+ uint64_t NumSamples = findCalleeFunctionSamples(*CI)->getTotalSamples();
+ if (InlineFunction(CI, IFI)) {
LocalChanged = true;
+ emitOptimizationRemark(Ctx, DEBUG_TYPE, F, DLoc,
+ Twine("inlined hot callee '") +
+ CalledFunction->getName() + "' with " +
+ Twine(NumSamples) + " samples into '" +
+ F.getName() + "'");
+ }
}
if (LocalChanged) {
Changed = true;
@@ -724,7 +734,8 @@ void SampleProfileLoader::propagateWeights(Function &F) {
// Generate MD_prof metadata for every branch instruction using the
// edge weights computed during propagation.
DEBUG(dbgs() << "\nPropagation complete. Setting branch weights\n");
- MDBuilder MDB(F.getContext());
+ LLVMContext &Ctx = F.getContext();
+ MDBuilder MDB(Ctx);
for (auto &BI : F) {
BasicBlock *BB = &BI;
TerminatorInst *TI = BB->getTerminator();
@@ -736,7 +747,9 @@ void SampleProfileLoader::propagateWeights(Function &F) {
DEBUG(dbgs() << "\nGetting weights for branch at line "
<< TI->getDebugLoc().getLine() << ".\n");
SmallVector<uint32_t, 4> Weights;
- bool AllWeightsZero = true;
+ uint32_t MaxWeight = 0;
+ BasicBlock *MaxDestBB = nullptr;
+ DebugLoc MaxDestLoc;
for (unsigned I = 0; I < TI->getNumSuccessors(); ++I) {
BasicBlock *Succ = TI->getSuccessor(I);
Edge E = std::make_pair(BB, Succ);
@@ -750,16 +763,27 @@ void SampleProfileLoader::propagateWeights(Function &F) {
Weight = std::numeric_limits<uint32_t>::max();
}
Weights.push_back(static_cast<uint32_t>(Weight));
- if (Weight != 0)
- AllWeightsZero = false;
+ if (Weight != 0) {
+ if (Weight > MaxWeight) {
+ MaxWeight = Weight;
+ MaxDestBB = Succ;
+ MaxDestLoc = Succ->getFirstNonPHIOrDbgOrLifetime()->getDebugLoc();
+ }
+ }
}
// Only set weights if there is at least one non-zero weight.
// In any other case, let the analyzer set weights.
- if (!AllWeightsZero) {
+ if (MaxWeight > 0) {
DEBUG(dbgs() << "SUCCESS. Found non-zero weights.\n");
TI->setMetadata(llvm::LLVMContext::MD_prof,
MDB.createBranchWeights(Weights));
+ DebugLoc BranchLoc = TI->getDebugLoc();
+ emitOptimizationRemark(
+ Ctx, DEBUG_TYPE, F, MaxDestLoc,
+ Twine("most popular destination for conditional branches at ") +
+ BranchLoc->getFilename() + ":" + Twine(BranchLoc.getLine()) +
+ ":" + Twine(BranchLoc.getCol()));
} else {
DEBUG(dbgs() << "SKIPPED. All branch weights are zero.\n");
}