summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorEaswaran Raman <eraman@google.com>2016-06-03 20:37:19 +0000
committerEaswaran Raman <eraman@google.com>2016-06-03 20:37:19 +0000
commitd142050f3a5e097f7c4a4c76a13d66a8173a3337 (patch)
treea56a14549f1676756a87a87cd51d947466e34b4d /llvm/lib
parentab1dfaae061a6df153c2726114dc132e65724b41 (diff)
downloadbcm5719-llvm-d142050f3a5e097f7c4a4c76a13d66a8173a3337.tar.gz
bcm5719-llvm-d142050f3a5e097f7c4a4c76a13d66a8173a3337.zip
Analysis pass to access profile summary info
Differential Revision: http://reviews.llvm.org/D20648 llvm-svn: 271728
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Analysis/CMakeLists.txt1
-rw-r--r--llvm/lib/Analysis/ProfileSummaryInfo.cpp161
-rw-r--r--llvm/lib/IR/ProfileSummary.cpp2
-rw-r--r--llvm/lib/Passes/PassBuilder.cpp3
-rw-r--r--llvm/lib/Passes/PassRegistry.def4
5 files changed, 169 insertions, 2 deletions
diff --git a/llvm/lib/Analysis/CMakeLists.txt b/llvm/lib/Analysis/CMakeLists.txt
index c598d9e4971..a4233df4fb4 100644
--- a/llvm/lib/Analysis/CMakeLists.txt
+++ b/llvm/lib/Analysis/CMakeLists.txt
@@ -57,6 +57,7 @@ add_llvm_library(LLVMAnalysis
OrderedBasicBlock.cpp
PHITransAddr.cpp
PostDominators.cpp
+ ProfileSummaryInfo.cpp
PtrUseVisitor.cpp
RegionInfo.cpp
RegionPass.cpp
diff --git a/llvm/lib/Analysis/ProfileSummaryInfo.cpp b/llvm/lib/Analysis/ProfileSummaryInfo.cpp
new file mode 100644
index 00000000000..f3dc8b48732
--- /dev/null
+++ b/llvm/lib/Analysis/ProfileSummaryInfo.cpp
@@ -0,0 +1,161 @@
+//===- ProfileSummaryInfo.cpp - Global profile summary information --------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains a pass that provides access to the global profile summary
+// information.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Analysis/ProfileSummaryInfo.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/ProfileSummary.h"
+using namespace llvm;
+
+// The following two parameters determine the threshold for a count to be
+// considered hot/cold. These two parameters are percentile values (multiplied
+// by 10000). If the counts are sorted in descending order, the minimum count to
+// reach ProfileSummaryCutoffHot gives the threshold to determine a hot count.
+// Similarly, the minimum count to reach ProfileSummaryCutoffCold gives the
+// threshold for determining cold count (everything <= this threshold is
+// considered cold).
+
+static cl::opt<int> ProfileSummaryCutoffHot(
+ "profile-summary-cutoff-hot", cl::Hidden, cl::init(999000), cl::ZeroOrMore,
+ cl::desc("A count is hot if it exceeds the minimum count to"
+ " reach this percentile of total counts."));
+
+static cl::opt<int> ProfileSummaryCutoffCold(
+ "profile-summary-cutoff-cold", cl::Hidden, cl::init(999999), cl::ZeroOrMore,
+ cl::desc("A count is cold if it is below the minimum count"
+ " to reach this percentile of total counts."));
+
+// Find the minimum count to reach a desired percentile of counts.
+static uint64_t getMinCountForPercentile(SummaryEntryVector &DS,
+ uint64_t Percentile) {
+ auto Compare = [](const ProfileSummaryEntry &Entry, uint64_t Percentile) {
+ return Entry.Cutoff < Percentile;
+ };
+ auto It = std::lower_bound(DS.begin(), DS.end(), Percentile, Compare);
+ // The required percentile has to be <= one of the percentiles in the
+ // detailed summary.
+ if (It == DS.end())
+ report_fatal_error("Desired percentile exceeds the maximum cutoff");
+ return It->MinCount;
+}
+
+// The profile summary metadata may be attached either by the frontend or by
+// any backend passes (IR level instrumentation, for example). This method
+// checks if the Summary is null and if so checks if the summary metadata is now
+// available in the module and parses it to get the Summary object.
+void ProfileSummaryInfo::computeSummary() {
+ if (Summary)
+ return;
+ auto *SummaryMD = M.getProfileSummary();
+ if (!SummaryMD)
+ return;
+ Summary.reset(ProfileSummary::getFromMD(SummaryMD));
+}
+
+// Returns true if the function is a hot function. If it returns false, it
+// either means it is not hot or it is unknown whether F is hot or not (for
+// example, no profile data is available).
+bool ProfileSummaryInfo::isHotFunction(const Function *F) {
+ computeSummary();
+ if (!F || !Summary)
+ return false;
+ auto FunctionCount = F->getEntryCount();
+ // FIXME: The heuristic used below for determining hotness is based on
+ // preliminary SPEC tuning for inliner. This will eventually be a
+ // convenience method that calls isHotCount.
+ return (FunctionCount &&
+ FunctionCount.getValue() >=
+ (uint64_t)(0.3 * (double)Summary->getMaxFunctionCount()));
+}
+
+// Returns true if the function is a cold function. If it returns false, it
+// either means it is not cold or it is unknown whether F is cold or not (for
+// example, no profile data is available).
+bool ProfileSummaryInfo::isColdFunction(const Function *F) {
+ computeSummary();
+ if (F->hasFnAttribute(Attribute::Cold)) {
+ return true;
+ }
+ auto FunctionCount = F->getEntryCount();
+ // FIXME: The heuristic used below for determining coldness is based on
+ // preliminary SPEC tuning for inliner. This will eventually be a
+ // convenience method that calls isHotCount.
+ return (FunctionCount &&
+ FunctionCount.getValue() <=
+ (uint64_t)(0.01 * (double)Summary->getMaxFunctionCount()));
+}
+
+// Compute the hot and cold thresholds.
+void ProfileSummaryInfo::computeThresholds() {
+ if (!Summary)
+ computeSummary();
+ if (!Summary)
+ return;
+ auto &DetailedSummary = Summary->getDetailedSummary();
+ HotCountThreshold =
+ getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffHot);
+ ColdCountThreshold =
+ getMinCountForPercentile(DetailedSummary, ProfileSummaryCutoffCold);
+}
+
+bool ProfileSummaryInfo::isHotCount(uint64_t C) {
+ if (!HotCountThreshold)
+ computeThresholds();
+ return HotCountThreshold && C >= HotCountThreshold.getValue();
+}
+
+bool ProfileSummaryInfo::isColdCount(uint64_t C) {
+ if (!ColdCountThreshold)
+ computeThresholds();
+ return ColdCountThreshold && C <= ColdCountThreshold.getValue();
+}
+
+ProfileSummaryInfo *ProfileSummaryInfoWrapperPass::getPSI(Module &M) {
+ if (!PSI)
+ PSI.reset(new ProfileSummaryInfo(M));
+ return PSI.get();
+}
+
+INITIALIZE_PASS(ProfileSummaryInfoWrapperPass, "profile-summary-info",
+ "Profile summary info", false, true)
+
+ProfileSummaryInfoWrapperPass::ProfileSummaryInfoWrapperPass()
+ : ImmutablePass(ID) {
+ initializeProfileSummaryInfoWrapperPassPass(*PassRegistry::getPassRegistry());
+}
+
+char ProfileSummaryAnalysis::PassID;
+ProfileSummaryInfo ProfileSummaryAnalysis::run(Module &M) {
+ return ProfileSummaryInfo(M);
+}
+
+// FIXME: This only tests isHotFunction and isColdFunction and not the
+// isHotCount and isColdCount calls.
+PreservedAnalyses ProfileSummaryPrinterPass::run(Module &M,
+ AnalysisManager<Module> &AM) {
+ ProfileSummaryInfo &PSI = AM.getResult<ProfileSummaryAnalysis>(M);
+
+ OS << "Functions in " << M.getName() << " with hot/cold annotations: \n";
+ for (auto &F : M) {
+ OS << F.getName();
+ if (PSI.isHotFunction(&F))
+ OS << " :hot ";
+ else if (PSI.isColdFunction(&F))
+ OS << " :cold ";
+ OS << "\n";
+ }
+ return PreservedAnalyses::all();
+}
+
+char ProfileSummaryInfoWrapperPass::ID = 0;
diff --git a/llvm/lib/IR/ProfileSummary.cpp b/llvm/lib/IR/ProfileSummary.cpp
index c35dfb09440..2b24d125112 100644
--- a/llvm/lib/IR/ProfileSummary.cpp
+++ b/llvm/lib/IR/ProfileSummary.cpp
@@ -144,6 +144,8 @@ static bool getSummaryFromMD(MDTuple *MD, SummaryEntryVector &Summary) {
}
ProfileSummary *ProfileSummary::getFromMD(Metadata *MD) {
+ if (!MD)
+ return nullptr;
if (!isa<MDTuple>(MD))
return nullptr;
MDTuple *Tuple = cast<MDTuple>(MD);
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 5076fefbf7b..7ca5c7b7b3f 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -35,6 +35,7 @@
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
#include "llvm/Analysis/PostDominators.h"
+#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/Analysis/RegionInfo.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
@@ -68,8 +69,8 @@
#include "llvm/Transforms/Scalar/DCE.h"
#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
#include "llvm/Transforms/Scalar/EarlyCSE.h"
-#include "llvm/Transforms/Scalar/GVN.h"
#include "llvm/Transforms/Scalar/GuardWidening.h"
+#include "llvm/Transforms/Scalar/GVN.h"
#include "llvm/Transforms/Scalar/LoopRotation.h"
#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
#include "llvm/Transforms/Scalar/LowerAtomic.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index ffd3bcbcf7e..fcc00bafcae 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -22,6 +22,7 @@
MODULE_ANALYSIS("callgraph", CallGraphAnalysis())
MODULE_ANALYSIS("lcg", LazyCallGraphAnalysis())
MODULE_ANALYSIS("no-op-module", NoOpModuleAnalysis())
+MODULE_ANALYSIS("profile-summary", ProfileSummaryAnalysis())
MODULE_ANALYSIS("targetlibinfo", TargetLibraryAnalysis())
MODULE_ANALYSIS("verify", VerifierAnalysis())
@@ -50,8 +51,9 @@ MODULE_PASS("no-op-module", NoOpModulePass())
MODULE_PASS("pgo-icall-prom", PGOIndirectCallPromotion())
MODULE_PASS("pgo-instr-gen", PGOInstrumentationGen())
MODULE_PASS("pgo-instr-use", PGOInstrumentationUse())
-MODULE_PASS("print", PrintModulePass(dbgs()))
+MODULE_PASS("print-profile-summary", ProfileSummaryPrinterPass(dbgs()))
MODULE_PASS("print-callgraph", CallGraphPrinterPass(dbgs()))
+MODULE_PASS("print", PrintModulePass(dbgs()))
MODULE_PASS("print-lcg", LazyCallGraphPrinterPass(dbgs()))
MODULE_PASS("sample-profile", SampleProfileLoaderPass())
MODULE_PASS("strip-dead-prototypes", StripDeadPrototypesPass())
OpenPOWER on IntegriCloud