<div dir="ltr">Thanks, Alexey. I'll take a look.</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 25, 2015 at 7:44 PM, Alexey Samsonov <span dir="ltr"><<a href="mailto:vonosmas@gmail.com" target="_blank">vonosmas@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">ASan complaints about the memory leaks, which are likely related to this change: <a href="http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-bootstrap/builds/8251/steps/check-llvm%20asan/logs/stdio" target="_blank">http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-bootstrap/builds/8251/steps/check-llvm%20asan/logs/stdio</a></div><div class="gmail_extra"><div><div class="h5"><br><div class="gmail_quote">On Tue, Aug 25, 2015 at 8:25 AM, Diego Novillo via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: dnovillo<br>
Date: Tue Aug 25 10:25:11 2015<br>
New Revision: 245940<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=245940&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=245940&view=rev</a><br>
Log:<br>
Convert SampleProfile pass into a Module pass.<br>
<br>
Eventually, we will need sample profiles to be incorporated into the<br>
inliner's cost models. To do this, we need the sample profile pass to<br>
be a module pass.<br>
<br>
This patch makes no functional changes beyond the mechanical adjustments<br>
needed to run SampleProfile as a module pass.<br>
<br>
Added:<br>
llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp<br>
- copied, changed from r245925, llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp<br>
Removed:<br>
llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp<br>
Modified:<br>
llvm/trunk/include/llvm/Transforms/IPO.h<br>
llvm/trunk/include/llvm/Transforms/Scalar.h<br>
llvm/trunk/lib/Transforms/IPO/CMakeLists.txt<br>
llvm/trunk/lib/Transforms/IPO/IPO.cpp<br>
llvm/trunk/lib/Transforms/Scalar/CMakeLists.txt<br>
llvm/trunk/lib/Transforms/Scalar/Scalar.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/IPO.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO.h?rev=245940&r1=245939&r2=245940&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO.h?rev=245940&r1=245939&r2=245940&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/IPO.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/IPO.h Tue Aug 25 10:25:11 2015<br>
@@ -16,6 +16,7 @@<br>
#define LLVM_TRANSFORMS_IPO_H<br>
<br>
#include "llvm/ADT/ArrayRef.h"<br>
+#include "llvm/ADT/StringRef.h"<br>
<br>
namespace llvm {<br>
<br>
@@ -209,6 +210,12 @@ ModulePass *createBarrierNoopPass();<br>
/// to bitsets.<br>
ModulePass *createLowerBitSetsPass();<br>
<br>
+//===----------------------------------------------------------------------===//<br>
+// SampleProfilePass - Loads sample profile data from disk and generates<br>
+// IR metadata to reflect the profile.<br>
+ModulePass *createSampleProfileLoaderPass();<br>
+ModulePass *createSampleProfileLoaderPass(StringRef Name);<br>
+<br>
} // End llvm namespace<br>
<br>
#endif<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/Scalar.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar.h?rev=245940&r1=245939&r2=245940&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar.h?rev=245940&r1=245939&r2=245940&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/Scalar.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/Scalar.h Tue Aug 25 10:25:11 2015<br>
@@ -408,13 +408,6 @@ FunctionPass *createPartiallyInlineLibCa<br>
<br>
//===----------------------------------------------------------------------===//<br>
//<br>
-// SampleProfilePass - Loads sample profile data from disk and generates<br>
-// IR metadata to reflect the profile.<br>
-FunctionPass *createSampleProfileLoaderPass();<br>
-FunctionPass *createSampleProfileLoaderPass(StringRef Name);<br>
-<br>
-//===----------------------------------------------------------------------===//<br>
-//<br>
// ScalarizerPass - Converts vector operations into scalar operations<br>
//<br>
FunctionPass *createScalarizerPass();<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/CMakeLists.txt?rev=245940&r1=245939&r2=245940&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/CMakeLists.txt?rev=245940&r1=245939&r2=245940&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/CMakeLists.txt (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/CMakeLists.txt Tue Aug 25 10:25:11 2015<br>
@@ -20,6 +20,7 @@ add_llvm_library(LLVMipo<br>
PartialInlining.cpp<br>
PassManagerBuilder.cpp<br>
PruneEH.cpp<br>
+ SampleProfile.cpp<br>
StripDeadPrototypes.cpp<br>
StripSymbols.cpp<br>
<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/IPO.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/IPO.cpp?rev=245940&r1=245939&r2=245940&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/IPO.cpp?rev=245940&r1=245939&r2=245940&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/IPO.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/IPO.cpp Tue Aug 25 10:25:11 2015<br>
@@ -47,6 +47,7 @@ void llvm::initializeIPO(PassRegistry &R<br>
initializeStripNonDebugSymbolsPass(Registry);<br>
initializeBarrierNoopPass(Registry);<br>
initializeEliminateAvailableExternallyPass(Registry);<br>
+ initializeSampleProfileLoaderPass(Registry);<br>
}<br>
<br>
void LLVMInitializeIPO(LLVMPassRegistryRef R) {<br>
<br>
Copied: llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp (from r245925, llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp)<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp?p2=llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp&p1=llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp&r1=245925&r2=245940&rev=245940&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp?p2=llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp&p1=llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp&r1=245925&r2=245940&rev=245940&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/SampleProfile.cpp Tue Aug 25 10:25:11 2015<br>
@@ -22,7 +22,6 @@<br>
//<br>
//===----------------------------------------------------------------------===//<br>
<br>
-#include "llvm/Transforms/Scalar.h"<br>
#include "llvm/ADT/DenseMap.h"<br>
#include "llvm/ADT/SmallPtrSet.h"<br>
#include "llvm/ADT/SmallSet.h"<br>
@@ -45,6 +44,7 @@<br>
#include "llvm/Support/CommandLine.h"<br>
#include "llvm/Support/Debug.h"<br>
#include "llvm/Support/raw_ostream.h"<br>
+#include "llvm/Transforms/IPO.h"<br>
#include <cctype><br>
<br>
using namespace llvm;<br>
@@ -74,14 +74,15 @@ typedef DenseMap<BasicBlock *, SmallVect<br>
/// This pass reads profile data from the file specified by<br>
/// -sample-profile-file and annotates every affected function with the<br>
/// profile information found in that file.<br>
-class SampleProfileLoader : public FunctionPass {<br>
+class SampleProfileLoader : public ModulePass {<br>
public:<br>
// Class identification, replacement for typeinfo<br>
static char ID;<br>
<br>
SampleProfileLoader(StringRef Name = SampleProfileFile)<br>
- : FunctionPass(ID), DT(nullptr), PDT(nullptr), LI(nullptr), Ctx(nullptr),<br>
- Reader(), Samples(nullptr), Filename(Name), ProfileIsValid(false) {<br>
+ : ModulePass(ID), DT(nullptr), PDT(nullptr), LI(nullptr),<br>
+ Ctx(nullptr), Reader(), Samples(nullptr), Filename(Name),<br>
+ ProfileIsValid(false) {<br>
initializeSampleProfileLoaderPass(*PassRegistry::getPassRegistry());<br>
}<br>
<br>
@@ -91,16 +92,23 @@ public:<br>
<br>
const char *getPassName() const override { return "Sample profile pass"; }<br>
<br>
- bool runOnFunction(Function &F) override;<br>
+ bool runOnModule(Module &M) override;<br>
<br>
void getAnalysisUsage(AnalysisUsage &AU) const override {<br>
AU.setPreservesCFG();<br>
+<br>
AU.addRequired<LoopInfoWrapperPass>();<br>
+ AU.addPreserved<LoopInfoWrapperPass>();<br>
+<br>
AU.addRequired<DominatorTreeWrapperPass>();<br>
+ AU.addPreserved<DominatorTreeWrapperPass>();<br>
+<br>
AU.addRequired<PostDominatorTree>();<br>
+ AU.addPreserved<PostDominatorTree>();<br>
}<br>
<br>
protected:<br>
+ bool runOnFunction(Function &F);<br>
unsigned getFunctionLoc(Function &F);<br>
bool emitAnnotations(Function &F);<br>
unsigned getInstWeight(Instruction &I);<br>
@@ -743,10 +751,11 @@ INITIALIZE_PASS_END(SampleProfileLoader,<br>
"Sample Profile loader", false, false)<br>
<br>
bool SampleProfileLoader::doInitialization(Module &M) {<br>
- auto ReaderOrErr = SampleProfileReader::create(Filename, M.getContext());<br>
+ auto& Ctx = M.getContext();<br>
+ auto ReaderOrErr = SampleProfileReader::create(Filename, Ctx);<br>
if (std::error_code EC = ReaderOrErr.getError()) {<br>
std::string Msg = "Could not open profile: " + EC.message();<br>
- M.getContext().diagnose(DiagnosticInfoSampleProfile(Filename.data(), Msg));<br>
+ Ctx.diagnose(DiagnosticInfoSampleProfile(Filename.data(), Msg));<br>
return false;<br>
}<br>
Reader = std::move(ReaderOrErr.get());<br>
@@ -754,21 +763,29 @@ bool SampleProfileLoader::doInitializati<br>
return true;<br>
}<br>
<br>
-FunctionPass *llvm::createSampleProfileLoaderPass() {<br>
+ModulePass *llvm::createSampleProfileLoaderPass() {<br>
return new SampleProfileLoader(SampleProfileFile);<br>
}<br>
<br>
-FunctionPass *llvm::createSampleProfileLoaderPass(StringRef Name) {<br>
+ModulePass *llvm::createSampleProfileLoaderPass(StringRef Name) {<br>
return new SampleProfileLoader(Name);<br>
}<br>
<br>
+bool SampleProfileLoader::runOnModule(Module &M) {<br>
+ bool retval = false;<br>
+ for (auto &F : M)<br>
+ if (!F.isDeclaration())<br>
+ retval |= runOnFunction(F);<br>
+ return retval;<br>
+}<br>
+<br>
bool SampleProfileLoader::runOnFunction(Function &F) {<br>
if (!ProfileIsValid)<br>
return false;<br>
<br>
- DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();<br>
- PDT = &getAnalysis<PostDominatorTree>();<br>
- LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();<br>
+ DT = &getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();<br>
+ PDT = &getAnalysis<PostDominatorTree>(F);<br>
+ LI = &getAnalysis<LoopInfoWrapperPass>(F).getLoopInfo();<br>
Ctx = &F.getParent()->getContext();<br>
Samples = Reader->getSamplesFor(F);<br>
if (!Samples->empty())<br>
<br>
Modified: llvm/trunk/lib/Transforms/Scalar/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/CMakeLists.txt?rev=245940&r1=245939&r2=245940&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/CMakeLists.txt?rev=245940&r1=245939&r2=245940&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/CMakeLists.txt (original)<br>
+++ llvm/trunk/lib/Transforms/Scalar/CMakeLists.txt Tue Aug 25 10:25:11 2015<br>
@@ -38,7 +38,6 @@ add_llvm_library(LLVMScalarOpts<br>
RewriteStatepointsForGC.cpp<br>
SCCP.cpp<br>
SROA.cpp<br>
- SampleProfile.cpp<br>
Scalar.cpp<br>
ScalarReplAggregates.cpp<br>
Scalarizer.cpp<br>
<br>
Removed: llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp?rev=245939&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp?rev=245939&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Scalar/SampleProfile.cpp (removed)<br>
@@ -1,777 +0,0 @@<br>
-//===- SampleProfile.cpp - Incorporate sample profiles into the IR --------===//<br>
-//<br>
-// The LLVM Compiler Infrastructure<br>
-//<br>
-// This file is distributed under the University of Illinois Open Source<br>
-// License. See LICENSE.TXT for details.<br>
-//<br>
-//===----------------------------------------------------------------------===//<br>
-//<br>
-// This file implements the SampleProfileLoader transformation. This pass<br>
-// reads a profile file generated by a sampling profiler (e.g. Linux Perf -<br>
-// <a href="http://perf.wiki.kernel.org/" rel="noreferrer" target="_blank">http://perf.wiki.kernel.org/</a>) and generates IR metadata to reflect the<br>
-// profile information in the given profile.<br>
-//<br>
-// This pass generates branch weight annotations on the IR:<br>
-//<br>
-// - prof: Represents branch weights. This annotation is added to branches<br>
-// to indicate the weights of each edge coming out of the branch.<br>
-// The weight of each edge is the weight of the target block for<br>
-// that edge. The weight of a block B is computed as the maximum<br>
-// number of samples found in B.<br>
-//<br>
-//===----------------------------------------------------------------------===//<br>
-<br>
-#include "llvm/Transforms/Scalar.h"<br>
-#include "llvm/ADT/DenseMap.h"<br>
-#include "llvm/ADT/SmallPtrSet.h"<br>
-#include "llvm/ADT/SmallSet.h"<br>
-#include "llvm/ADT/StringRef.h"<br>
-#include "llvm/Analysis/LoopInfo.h"<br>
-#include "llvm/Analysis/PostDominators.h"<br>
-#include "llvm/IR/Constants.h"<br>
-#include "llvm/IR/DebugInfo.h"<br>
-#include "llvm/IR/DiagnosticInfo.h"<br>
-#include "llvm/IR/Dominators.h"<br>
-#include "llvm/IR/Function.h"<br>
-#include "llvm/IR/InstIterator.h"<br>
-#include "llvm/IR/Instructions.h"<br>
-#include "llvm/IR/LLVMContext.h"<br>
-#include "llvm/IR/MDBuilder.h"<br>
-#include "llvm/IR/Metadata.h"<br>
-#include "llvm/IR/Module.h"<br>
-#include "llvm/Pass.h"<br>
-#include "llvm/ProfileData/SampleProfReader.h"<br>
-#include "llvm/Support/CommandLine.h"<br>
-#include "llvm/Support/Debug.h"<br>
-#include "llvm/Support/raw_ostream.h"<br>
-#include <cctype><br>
-<br>
-using namespace llvm;<br>
-using namespace sampleprof;<br>
-<br>
-#define DEBUG_TYPE "sample-profile"<br>
-<br>
-// Command line option to specify the file to read samples from. This is<br>
-// mainly used for debugging.<br>
-static cl::opt<std::string> SampleProfileFile(<br>
- "sample-profile-file", cl::init(""), cl::value_desc("filename"),<br>
- cl::desc("Profile file loaded by -sample-profile"), cl::Hidden);<br>
-static cl::opt<unsigned> SampleProfileMaxPropagateIterations(<br>
- "sample-profile-max-propagate-iterations", cl::init(100),<br>
- cl::desc("Maximum number of iterations to go through when propagating "<br>
- "sample block/edge weights through the CFG."));<br>
-<br>
-namespace {<br>
-typedef DenseMap<BasicBlock *, unsigned> BlockWeightMap;<br>
-typedef DenseMap<BasicBlock *, BasicBlock *> EquivalenceClassMap;<br>
-typedef std::pair<BasicBlock *, BasicBlock *> Edge;<br>
-typedef DenseMap<Edge, unsigned> EdgeWeightMap;<br>
-typedef DenseMap<BasicBlock *, SmallVector<BasicBlock *, 8>> BlockEdgeMap;<br>
-<br>
-/// \brief Sample profile pass.<br>
-///<br>
-/// This pass reads profile data from the file specified by<br>
-/// -sample-profile-file and annotates every affected function with the<br>
-/// profile information found in that file.<br>
-class SampleProfileLoader : public FunctionPass {<br>
-public:<br>
- // Class identification, replacement for typeinfo<br>
- static char ID;<br>
-<br>
- SampleProfileLoader(StringRef Name = SampleProfileFile)<br>
- : FunctionPass(ID), DT(nullptr), PDT(nullptr), LI(nullptr), Ctx(nullptr),<br>
- Reader(), Samples(nullptr), Filename(Name), ProfileIsValid(false) {<br>
- initializeSampleProfileLoaderPass(*PassRegistry::getPassRegistry());<br>
- }<br>
-<br>
- bool doInitialization(Module &M) override;<br>
-<br>
- void dump() { Reader->dump(); }<br>
-<br>
- const char *getPassName() const override { return "Sample profile pass"; }<br>
-<br>
- bool runOnFunction(Function &F) override;<br>
-<br>
- void getAnalysisUsage(AnalysisUsage &AU) const override {<br>
- AU.setPreservesCFG();<br>
- AU.addRequired<LoopInfoWrapperPass>();<br>
- AU.addRequired<DominatorTreeWrapperPass>();<br>
- AU.addRequired<PostDominatorTree>();<br>
- }<br>
-<br>
-protected:<br>
- unsigned getFunctionLoc(Function &F);<br>
- bool emitAnnotations(Function &F);<br>
- unsigned getInstWeight(Instruction &I);<br>
- unsigned getBlockWeight(BasicBlock *BB);<br>
- void printEdgeWeight(raw_ostream &OS, Edge E);<br>
- void printBlockWeight(raw_ostream &OS, BasicBlock *BB);<br>
- void printBlockEquivalence(raw_ostream &OS, BasicBlock *BB);<br>
- bool computeBlockWeights(Function &F);<br>
- void findEquivalenceClasses(Function &F);<br>
- void findEquivalencesFor(BasicBlock *BB1,<br>
- SmallVector<BasicBlock *, 8> Descendants,<br>
- DominatorTreeBase<BasicBlock> *DomTree);<br>
- void propagateWeights(Function &F);<br>
- unsigned visitEdge(Edge E, unsigned *NumUnknownEdges, Edge *UnknownEdge);<br>
- void buildEdges(Function &F);<br>
- bool propagateThroughEdges(Function &F);<br>
-<br>
- /// \brief Line number for the function header. Used to compute absolute<br>
- /// line numbers from the relative line numbers found in the profile.<br>
- unsigned HeaderLineno;<br>
-<br>
- /// \brief Map basic blocks to their computed weights.<br>
- ///<br>
- /// The weight of a basic block is defined to be the maximum<br>
- /// of all the instruction weights in that block.<br>
- BlockWeightMap BlockWeights;<br>
-<br>
- /// \brief Map edges to their computed weights.<br>
- ///<br>
- /// Edge weights are computed by propagating basic block weights in<br>
- /// SampleProfile::propagateWeights.<br>
- EdgeWeightMap EdgeWeights;<br>
-<br>
- /// \brief Set of visited blocks during propagation.<br>
- SmallPtrSet<BasicBlock *, 128> VisitedBlocks;<br>
-<br>
- /// \brief Set of visited edges during propagation.<br>
- SmallSet<Edge, 128> VisitedEdges;<br>
-<br>
- /// \brief Equivalence classes for block weights.<br>
- ///<br>
- /// Two blocks BB1 and BB2 are in the same equivalence class if they<br>
- /// dominate and post-dominate each other, and they are in the same loop<br>
- /// nest. When this happens, the two blocks are guaranteed to execute<br>
- /// the same number of times.<br>
- EquivalenceClassMap EquivalenceClass;<br>
-<br>
- /// \brief Dominance, post-dominance and loop information.<br>
- DominatorTree *DT;<br>
- PostDominatorTree *PDT;<br>
- LoopInfo *LI;<br>
-<br>
- /// \brief Predecessors for each basic block in the CFG.<br>
- BlockEdgeMap Predecessors;<br>
-<br>
- /// \brief Successors for each basic block in the CFG.<br>
- BlockEdgeMap Successors;<br>
-<br>
- /// \brief LLVM context holding the debug data we need.<br>
- LLVMContext *Ctx;<br>
-<br>
- /// \brief Profile reader object.<br>
- std::unique_ptr<SampleProfileReader> Reader;<br>
-<br>
- /// \brief Samples collected for the body of this function.<br>
- FunctionSamples *Samples;<br>
-<br>
- /// \brief Name of the profile file to load.<br>
- StringRef Filename;<br>
-<br>
- /// \brief Flag indicating whether the profile input loaded successfully.<br>
- bool ProfileIsValid;<br>
-};<br>
-}<br>
-<br>
-/// \brief Print the weight of edge \p E on stream \p OS.<br>
-///<br>
-/// \param OS Stream to emit the output to.<br>
-/// \param E Edge to print.<br>
-void SampleProfileLoader::printEdgeWeight(raw_ostream &OS, Edge E) {<br>
- OS << "weight[" << E.first->getName() << "->" << E.second->getName()<br>
- << "]: " << EdgeWeights[E] << "\n";<br>
-}<br>
-<br>
-/// \brief Print the equivalence class of block \p BB on stream \p OS.<br>
-///<br>
-/// \param OS Stream to emit the output to.<br>
-/// \param BB Block to print.<br>
-void SampleProfileLoader::printBlockEquivalence(raw_ostream &OS,<br>
- BasicBlock *BB) {<br>
- BasicBlock *Equiv = EquivalenceClass[BB];<br>
- OS << "equivalence[" << BB->getName()<br>
- << "]: " << ((Equiv) ? EquivalenceClass[BB]->getName() : "NONE") << "\n";<br>
-}<br>
-<br>
-/// \brief Print the weight of block \p BB on stream \p OS.<br>
-///<br>
-/// \param OS Stream to emit the output to.<br>
-/// \param BB Block to print.<br>
-void SampleProfileLoader::printBlockWeight(raw_ostream &OS, BasicBlock *BB) {<br>
- OS << "weight[" << BB->getName() << "]: " << BlockWeights[BB] << "\n";<br>
-}<br>
-<br>
-/// \brief Get the weight for an instruction.<br>
-///<br>
-/// The "weight" of an instruction \p Inst is the number of samples<br>
-/// collected on that instruction at runtime. To retrieve it, we<br>
-/// need to compute the line number of \p Inst relative to the start of its<br>
-/// function. We use HeaderLineno to compute the offset. We then<br>
-/// look up the samples collected for \p Inst using BodySamples.<br>
-///<br>
-/// \param Inst Instruction to query.<br>
-///<br>
-/// \returns The profiled weight of I.<br>
-unsigned SampleProfileLoader::getInstWeight(Instruction &Inst) {<br>
- DebugLoc DLoc = Inst.getDebugLoc();<br>
- if (!DLoc)<br>
- return 0;<br>
-<br>
- unsigned Lineno = DLoc.getLine();<br>
- if (Lineno < HeaderLineno)<br>
- return 0;<br>
-<br>
- const DILocation *DIL = DLoc;<br>
- int LOffset = Lineno - HeaderLineno;<br>
- unsigned Discriminator = DIL->getDiscriminator();<br>
- unsigned Weight = Samples->samplesAt(LOffset, Discriminator);<br>
- DEBUG(dbgs() << " " << Lineno << "." << Discriminator << ":" << Inst<br>
- << " (line offset: " << LOffset << "." << Discriminator<br>
- << " - weight: " << Weight << ")\n");<br>
- return Weight;<br>
-}<br>
-<br>
-/// \brief Compute the weight of a basic block.<br>
-///<br>
-/// The weight of basic block \p BB is the maximum weight of all the<br>
-/// instructions in BB. The weight of \p BB is computed and cached in<br>
-/// the BlockWeights map.<br>
-///<br>
-/// \param BB The basic block to query.<br>
-///<br>
-/// \returns The computed weight of BB.<br>
-unsigned SampleProfileLoader::getBlockWeight(BasicBlock *BB) {<br>
- // If we've computed BB's weight before, return it.<br>
- std::pair<BlockWeightMap::iterator, bool> Entry =<br>
- BlockWeights.insert(std::make_pair(BB, 0));<br>
- if (!Entry.second)<br>
- return Entry.first->second;<br>
-<br>
- // Otherwise, compute and cache BB's weight.<br>
- unsigned Weight = 0;<br>
- for (auto &I : BB->getInstList()) {<br>
- unsigned InstWeight = getInstWeight(I);<br>
- if (InstWeight > Weight)<br>
- Weight = InstWeight;<br>
- }<br>
- Entry.first->second = Weight;<br>
- return Weight;<br>
-}<br>
-<br>
-/// \brief Compute and store the weights of every basic block.<br>
-///<br>
-/// This populates the BlockWeights map by computing<br>
-/// the weights of every basic block in the CFG.<br>
-///<br>
-/// \param F The function to query.<br>
-bool SampleProfileLoader::computeBlockWeights(Function &F) {<br>
- bool Changed = false;<br>
- DEBUG(dbgs() << "Block weights\n");<br>
- for (auto &BB : F) {<br>
- unsigned Weight = getBlockWeight(&BB);<br>
- Changed |= (Weight > 0);<br>
- DEBUG(printBlockWeight(dbgs(), &BB));<br>
- }<br>
-<br>
- return Changed;<br>
-}<br>
-<br>
-/// \brief Find equivalence classes for the given block.<br>
-///<br>
-/// This finds all the blocks that are guaranteed to execute the same<br>
-/// number of times as \p BB1. To do this, it traverses all the<br>
-/// descendants of \p BB1 in the dominator or post-dominator tree.<br>
-///<br>
-/// A block BB2 will be in the same equivalence class as \p BB1 if<br>
-/// the following holds:<br>
-///<br>
-/// 1- \p BB1 is a descendant of BB2 in the opposite tree. So, if BB2<br>
-/// is a descendant of \p BB1 in the dominator tree, then BB2 should<br>
-/// dominate BB1 in the post-dominator tree.<br>
-///<br>
-/// 2- Both BB2 and \p BB1 must be in the same loop.<br>
-///<br>
-/// For every block BB2 that meets those two requirements, we set BB2's<br>
-/// equivalence class to \p BB1.<br>
-///<br>
-/// \param BB1 Block to check.<br>
-/// \param Descendants Descendants of \p BB1 in either the dom or pdom tree.<br>
-/// \param DomTree Opposite dominator tree. If \p Descendants is filled<br>
-/// with blocks from \p BB1's dominator tree, then<br>
-/// this is the post-dominator tree, and vice versa.<br>
-void SampleProfileLoader::findEquivalencesFor(<br>
- BasicBlock *BB1, SmallVector<BasicBlock *, 8> Descendants,<br>
- DominatorTreeBase<BasicBlock> *DomTree) {<br>
- for (auto *BB2 : Descendants) {<br>
- bool IsDomParent = DomTree->dominates(BB2, BB1);<br>
- bool IsInSameLoop = LI->getLoopFor(BB1) == LI->getLoopFor(BB2);<br>
- if (BB1 != BB2 && VisitedBlocks.insert(BB2).second && IsDomParent &&<br>
- IsInSameLoop) {<br>
- EquivalenceClass[BB2] = BB1;<br>
-<br>
- // If BB2 is heavier than BB1, make BB2 have the same weight<br>
- // as BB1.<br>
- //<br>
- // Note that we don't worry about the opposite situation here<br>
- // (when BB2 is lighter than BB1). We will deal with this<br>
- // during the propagation phase. Right now, we just want to<br>
- // make sure that BB1 has the largest weight of all the<br>
- // members of its equivalence set.<br>
- unsigned &BB1Weight = BlockWeights[BB1];<br>
- unsigned &BB2Weight = BlockWeights[BB2];<br>
- BB1Weight = std::max(BB1Weight, BB2Weight);<br>
- }<br>
- }<br>
-}<br>
-<br>
-/// \brief Find equivalence classes.<br>
-///<br>
-/// Since samples may be missing from blocks, we can fill in the gaps by setting<br>
-/// the weights of all the blocks in the same equivalence class to the same<br>
-/// weight. To compute the concept of equivalence, we use dominance and loop<br>
-/// information. Two blocks B1 and B2 are in the same equivalence class if B1<br>
-/// dominates B2, B2 post-dominates B1 and both are in the same loop.<br>
-///<br>
-/// \param F The function to query.<br>
-void SampleProfileLoader::findEquivalenceClasses(Function &F) {<br>
- SmallVector<BasicBlock *, 8> DominatedBBs;<br>
- DEBUG(dbgs() << "\nBlock equivalence classes\n");<br>
- // Find equivalence sets based on dominance and post-dominance information.<br>
- for (auto &BB : F) {<br>
- BasicBlock *BB1 = &BB;<br>
-<br>
- // Compute BB1's equivalence class once.<br>
- if (EquivalenceClass.count(BB1)) {<br>
- DEBUG(printBlockEquivalence(dbgs(), BB1));<br>
- continue;<br>
- }<br>
-<br>
- // By default, blocks are in their own equivalence class.<br>
- EquivalenceClass[BB1] = BB1;<br>
-<br>
- // Traverse all the blocks dominated by BB1. We are looking for<br>
- // every basic block BB2 such that:<br>
- //<br>
- // 1- BB1 dominates BB2.<br>
- // 2- BB2 post-dominates BB1.<br>
- // 3- BB1 and BB2 are in the same loop nest.<br>
- //<br>
- // If all those conditions hold, it means that BB2 is executed<br>
- // as many times as BB1, so they are placed in the same equivalence<br>
- // class by making BB2's equivalence class be BB1.<br>
- DominatedBBs.clear();<br>
- DT->getDescendants(BB1, DominatedBBs);<br>
- findEquivalencesFor(BB1, DominatedBBs, PDT->DT);<br>
-<br>
- // Repeat the same logic for all the blocks post-dominated by BB1.<br>
- // We are looking for every basic block BB2 such that:<br>
- //<br>
- // 1- BB1 post-dominates BB2.<br>
- // 2- BB2 dominates BB1.<br>
- // 3- BB1 and BB2 are in the same loop nest.<br>
- //<br>
- // If all those conditions hold, BB2's equivalence class is BB1.<br>
- DominatedBBs.clear();<br>
- PDT->getDescendants(BB1, DominatedBBs);<br>
- findEquivalencesFor(BB1, DominatedBBs, DT);<br>
-<br>
- DEBUG(printBlockEquivalence(dbgs(), BB1));<br>
- }<br>
-<br>
- // Assign weights to equivalence classes.<br>
- //<br>
- // All the basic blocks in the same equivalence class will execute<br>
- // the same number of times. Since we know that the head block in<br>
- // each equivalence class has the largest weight, assign that weight<br>
- // to all the blocks in that equivalence class.<br>
- DEBUG(dbgs() << "\nAssign the same weight to all blocks in the same class\n");<br>
- for (auto &BI : F) {<br>
- BasicBlock *BB = &BI;<br>
- BasicBlock *EquivBB = EquivalenceClass[BB];<br>
- if (BB != EquivBB)<br>
- BlockWeights[BB] = BlockWeights[EquivBB];<br>
- DEBUG(printBlockWeight(dbgs(), BB));<br>
- }<br>
-}<br>
-<br>
-/// \brief Visit the given edge to decide if it has a valid weight.<br>
-///<br>
-/// If \p E has not been visited before, we copy to \p UnknownEdge<br>
-/// and increment the count of unknown edges.<br>
-///<br>
-/// \param E Edge to visit.<br>
-/// \param NumUnknownEdges Current number of unknown edges.<br>
-/// \param UnknownEdge Set if E has not been visited before.<br>
-///<br>
-/// \returns E's weight, if known. Otherwise, return 0.<br>
-unsigned SampleProfileLoader::visitEdge(Edge E, unsigned *NumUnknownEdges,<br>
- Edge *UnknownEdge) {<br>
- if (!VisitedEdges.count(E)) {<br>
- (*NumUnknownEdges)++;<br>
- *UnknownEdge = E;<br>
- return 0;<br>
- }<br>
-<br>
- return EdgeWeights[E];<br>
-}<br>
-<br>
-/// \brief Propagate weights through incoming/outgoing edges.<br>
-///<br>
-/// If the weight of a basic block is known, and there is only one edge<br>
-/// with an unknown weight, we can calculate the weight of that edge.<br>
-///<br>
-/// Similarly, if all the edges have a known count, we can calculate the<br>
-/// count of the basic block, if needed.<br>
-///<br>
-/// \param F Function to process.<br>
-///<br>
-/// \returns True if new weights were assigned to edges or blocks.<br>
-bool SampleProfileLoader::propagateThroughEdges(Function &F) {<br>
- bool Changed = false;<br>
- DEBUG(dbgs() << "\nPropagation through edges\n");<br>
- for (auto &BI : F) {<br>
- BasicBlock *BB = &BI;<br>
-<br>
- // Visit all the predecessor and successor edges to determine<br>
- // which ones have a weight assigned already. Note that it doesn't<br>
- // matter that we only keep track of a single unknown edge. The<br>
- // only case we are interested in handling is when only a single<br>
- // edge is unknown (see setEdgeOrBlockWeight).<br>
- for (unsigned i = 0; i < 2; i++) {<br>
- unsigned TotalWeight = 0;<br>
- unsigned NumUnknownEdges = 0;<br>
- Edge UnknownEdge, SelfReferentialEdge;<br>
-<br>
- if (i == 0) {<br>
- // First, visit all predecessor edges.<br>
- for (auto *Pred : Predecessors[BB]) {<br>
- Edge E = std::make_pair(Pred, BB);<br>
- TotalWeight += visitEdge(E, &NumUnknownEdges, &UnknownEdge);<br>
- if (E.first == E.second)<br>
- SelfReferentialEdge = E;<br>
- }<br>
- } else {<br>
- // On the second round, visit all successor edges.<br>
- for (auto *Succ : Successors[BB]) {<br>
- Edge E = std::make_pair(BB, Succ);<br>
- TotalWeight += visitEdge(E, &NumUnknownEdges, &UnknownEdge);<br>
- }<br>
- }<br>
-<br>
- // After visiting all the edges, there are three cases that we<br>
- // can handle immediately:<br>
- //<br>
- // - All the edge weights are known (i.e., NumUnknownEdges == 0).<br>
- // In this case, we simply check that the sum of all the edges<br>
- // is the same as BB's weight. If not, we change BB's weight<br>
- // to match. Additionally, if BB had not been visited before,<br>
- // we mark it visited.<br>
- //<br>
- // - Only one edge is unknown and BB has already been visited.<br>
- // In this case, we can compute the weight of the edge by<br>
- // subtracting the total block weight from all the known<br>
- // edge weights. If the edges weight more than BB, then the<br>
- // edge of the last remaining edge is set to zero.<br>
- //<br>
- // - There exists a self-referential edge and the weight of BB is<br>
- // known. In this case, this edge can be based on BB's weight.<br>
- // We add up all the other known edges and set the weight on<br>
- // the self-referential edge as we did in the previous case.<br>
- //<br>
- // In any other case, we must continue iterating. Eventually,<br>
- // all edges will get a weight, or iteration will stop when<br>
- // it reaches SampleProfileMaxPropagateIterations.<br>
- if (NumUnknownEdges <= 1) {<br>
- unsigned &BBWeight = BlockWeights[BB];<br>
- if (NumUnknownEdges == 0) {<br>
- // If we already know the weight of all edges, the weight of the<br>
- // basic block can be computed. It should be no larger than the sum<br>
- // of all edge weights.<br>
- if (TotalWeight > BBWeight) {<br>
- BBWeight = TotalWeight;<br>
- Changed = true;<br>
- DEBUG(dbgs() << "All edge weights for " << BB->getName()<br>
- << " known. Set weight for block: ";<br>
- printBlockWeight(dbgs(), BB););<br>
- }<br>
- if (VisitedBlocks.insert(BB).second)<br>
- Changed = true;<br>
- } else if (NumUnknownEdges == 1 && VisitedBlocks.count(BB)) {<br>
- // If there is a single unknown edge and the block has been<br>
- // visited, then we can compute E's weight.<br>
- if (BBWeight >= TotalWeight)<br>
- EdgeWeights[UnknownEdge] = BBWeight - TotalWeight;<br>
- else<br>
- EdgeWeights[UnknownEdge] = 0;<br>
- VisitedEdges.insert(UnknownEdge);<br>
- Changed = true;<br>
- DEBUG(dbgs() << "Set weight for edge: ";<br>
- printEdgeWeight(dbgs(), UnknownEdge));<br>
- }<br>
- } else if (SelfReferentialEdge.first && VisitedBlocks.count(BB)) {<br>
- unsigned &BBWeight = BlockWeights[BB];<br>
- // We have a self-referential edge and the weight of BB is known.<br>
- if (BBWeight >= TotalWeight)<br>
- EdgeWeights[SelfReferentialEdge] = BBWeight - TotalWeight;<br>
- else<br>
- EdgeWeights[SelfReferentialEdge] = 0;<br>
- VisitedEdges.insert(SelfReferentialEdge);<br>
- Changed = true;<br>
- DEBUG(dbgs() << "Set self-referential edge weight to: ";<br>
- printEdgeWeight(dbgs(), SelfReferentialEdge));<br>
- }<br>
- }<br>
- }<br>
-<br>
- return Changed;<br>
-}<br>
-<br>
-/// \brief Build in/out edge lists for each basic block in the CFG.<br>
-///<br>
-/// We are interested in unique edges. If a block B1 has multiple<br>
-/// edges to another block B2, we only add a single B1->B2 edge.<br>
-void SampleProfileLoader::buildEdges(Function &F) {<br>
- for (auto &BI : F) {<br>
- BasicBlock *B1 = &BI;<br>
-<br>
- // Add predecessors for B1.<br>
- SmallPtrSet<BasicBlock *, 16> Visited;<br>
- if (!Predecessors[B1].empty())<br>
- llvm_unreachable("Found a stale predecessors list in a basic block.");<br>
- for (pred_iterator PI = pred_begin(B1), PE = pred_end(B1); PI != PE; ++PI) {<br>
- BasicBlock *B2 = *PI;<br>
- if (Visited.insert(B2).second)<br>
- Predecessors[B1].push_back(B2);<br>
- }<br>
-<br>
- // Add successors for B1.<br>
- Visited.clear();<br>
- if (!Successors[B1].empty())<br>
- llvm_unreachable("Found a stale successors list in a basic block.");<br>
- for (succ_iterator SI = succ_begin(B1), SE = succ_end(B1); SI != SE; ++SI) {<br>
- BasicBlock *B2 = *SI;<br>
- if (Visited.insert(B2).second)<br>
- Successors[B1].push_back(B2);<br>
- }<br>
- }<br>
-}<br>
-<br>
-/// \brief Propagate weights into edges<br>
-///<br>
-/// The following rules are applied to every block BB in the CFG:<br>
-///<br>
-/// - If BB has a single predecessor/successor, then the weight<br>
-/// of that edge is the weight of the block.<br>
-///<br>
-/// - If all incoming or outgoing edges are known except one, and the<br>
-/// weight of the block is already known, the weight of the unknown<br>
-/// edge will be the weight of the block minus the sum of all the known<br>
-/// edges. If the sum of all the known edges is larger than BB's weight,<br>
-/// we set the unknown edge weight to zero.<br>
-///<br>
-/// - If there is a self-referential edge, and the weight of the block is<br>
-/// known, the weight for that edge is set to the weight of the block<br>
-/// minus the weight of the other incoming edges to that block (if<br>
-/// known).<br>
-void SampleProfileLoader::propagateWeights(Function &F) {<br>
- bool Changed = true;<br>
- unsigned i = 0;<br>
-<br>
- // Add an entry count to the function using the samples gathered<br>
- // at the function entry.<br>
- F.setEntryCount(Samples->getHeadSamples());<br>
-<br>
- // Before propagation starts, build, for each block, a list of<br>
- // unique predecessors and successors. This is necessary to handle<br>
- // identical edges in multiway branches. Since we visit all blocks and all<br>
- // edges of the CFG, it is cleaner to build these lists once at the start<br>
- // of the pass.<br>
- buildEdges(F);<br>
-<br>
- // Propagate until we converge or we go past the iteration limit.<br>
- while (Changed && i++ < SampleProfileMaxPropagateIterations) {<br>
- Changed = propagateThroughEdges(F);<br>
- }<br>
-<br>
- // Generate MD_prof metadata for every branch instruction using the<br>
- // edge weights computed during propagation.<br>
- DEBUG(dbgs() << "\nPropagation complete. Setting branch weights\n");<br>
- MDBuilder MDB(F.getContext());<br>
- for (auto &BI : F) {<br>
- BasicBlock *BB = &BI;<br>
- TerminatorInst *TI = BB->getTerminator();<br>
- if (TI->getNumSuccessors() == 1)<br>
- continue;<br>
- if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))<br>
- continue;<br>
-<br>
- DEBUG(dbgs() << "\nGetting weights for branch at line "<br>
- << TI->getDebugLoc().getLine() << ".\n");<br>
- SmallVector<unsigned, 4> Weights;<br>
- bool AllWeightsZero = true;<br>
- for (unsigned I = 0; I < TI->getNumSuccessors(); ++I) {<br>
- BasicBlock *Succ = TI->getSuccessor(I);<br>
- Edge E = std::make_pair(BB, Succ);<br>
- unsigned Weight = EdgeWeights[E];<br>
- DEBUG(dbgs() << "\t"; printEdgeWeight(dbgs(), E));<br>
- Weights.push_back(Weight);<br>
- if (Weight != 0)<br>
- AllWeightsZero = false;<br>
- }<br>
-<br>
- // Only set weights if there is at least one non-zero weight.<br>
- // In any other case, let the analyzer set weights.<br>
- if (!AllWeightsZero) {<br>
- DEBUG(dbgs() << "SUCCESS. Found non-zero weights.\n");<br>
- TI->setMetadata(llvm::LLVMContext::MD_prof,<br>
- MDB.createBranchWeights(Weights));<br>
- } else {<br>
- DEBUG(dbgs() << "SKIPPED. All branch weights are zero.\n");<br>
- }<br>
- }<br>
-}<br>
-<br>
-/// \brief Get the line number for the function header.<br>
-///<br>
-/// This looks up function \p F in the current compilation unit and<br>
-/// retrieves the line number where the function is defined. This is<br>
-/// line 0 for all the samples read from the profile file. Every line<br>
-/// number is relative to this line.<br>
-///<br>
-/// \param F Function object to query.<br>
-///<br>
-/// \returns the line number where \p F is defined. If it returns 0,<br>
-/// it means that there is no debug information available for \p F.<br>
-unsigned SampleProfileLoader::getFunctionLoc(Function &F) {<br>
- if (DISubprogram *S = getDISubprogram(&F))<br>
- return S->getLine();<br>
-<br>
- // If could not find the start of \p F, emit a diagnostic to inform the user<br>
- // about the missed opportunity.<br>
- F.getContext().diagnose(DiagnosticInfoSampleProfile(<br>
- "No debug information found in function " + F.getName() +<br>
- ": Function profile not used",<br>
- DS_Warning));<br>
- return 0;<br>
-}<br>
-<br>
-/// \brief Generate branch weight metadata for all branches in \p F.<br>
-///<br>
-/// Branch weights are computed out of instruction samples using a<br>
-/// propagation heuristic. Propagation proceeds in 3 phases:<br>
-///<br>
-/// 1- Assignment of block weights. All the basic blocks in the function<br>
-/// are initial assigned the same weight as their most frequently<br>
-/// executed instruction.<br>
-///<br>
-/// 2- Creation of equivalence classes. Since samples may be missing from<br>
-/// blocks, we can fill in the gaps by setting the weights of all the<br>
-/// blocks in the same equivalence class to the same weight. To compute<br>
-/// the concept of equivalence, we use dominance and loop information.<br>
-/// Two blocks B1 and B2 are in the same equivalence class if B1<br>
-/// dominates B2, B2 post-dominates B1 and both are in the same loop.<br>
-///<br>
-/// 3- Propagation of block weights into edges. This uses a simple<br>
-/// propagation heuristic. The following rules are applied to every<br>
-/// block BB in the CFG:<br>
-///<br>
-/// - If BB has a single predecessor/successor, then the weight<br>
-/// of that edge is the weight of the block.<br>
-///<br>
-/// - If all the edges are known except one, and the weight of the<br>
-/// block is already known, the weight of the unknown edge will<br>
-/// be the weight of the block minus the sum of all the known<br>
-/// edges. If the sum of all the known edges is larger than BB's weight,<br>
-/// we set the unknown edge weight to zero.<br>
-///<br>
-/// - If there is a self-referential edge, and the weight of the block is<br>
-/// known, the weight for that edge is set to the weight of the block<br>
-/// minus the weight of the other incoming edges to that block (if<br>
-/// known).<br>
-///<br>
-/// Since this propagation is not guaranteed to finalize for every CFG, we<br>
-/// only allow it to proceed for a limited number of iterations (controlled<br>
-/// by -sample-profile-max-propagate-iterations).<br>
-///<br>
-/// FIXME: Try to replace this propagation heuristic with a scheme<br>
-/// that is guaranteed to finalize. A work-list approach similar to<br>
-/// the standard value propagation algorithm used by SSA-CCP might<br>
-/// work here.<br>
-///<br>
-/// Once all the branch weights are computed, we emit the MD_prof<br>
-/// metadata on BB using the computed values for each of its branches.<br>
-///<br>
-/// \param F The function to query.<br>
-///<br>
-/// \returns true if \p F was modified. Returns false, otherwise.<br>
-bool SampleProfileLoader::emitAnnotations(Function &F) {<br>
- bool Changed = false;<br>
-<br>
- // Initialize invariants used during computation and propagation.<br>
- HeaderLineno = getFunctionLoc(F);<br>
- if (HeaderLineno == 0)<br>
- return false;<br>
-<br>
- DEBUG(dbgs() << "Line number for the first instruction in " << F.getName()<br>
- << ": " << HeaderLineno << "\n");<br>
-<br>
- // Compute basic block weights.<br>
- Changed |= computeBlockWeights(F);<br>
-<br>
- if (Changed) {<br>
- // Find equivalence classes.<br>
- findEquivalenceClasses(F);<br>
-<br>
- // Propagate weights to all edges.<br>
- propagateWeights(F);<br>
- }<br>
-<br>
- return Changed;<br>
-}<br>
-<br>
-char SampleProfileLoader::ID = 0;<br>
-INITIALIZE_PASS_BEGIN(SampleProfileLoader, "sample-profile",<br>
- "Sample Profile loader", false, false)<br>
-INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)<br>
-INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)<br>
-INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)<br>
-INITIALIZE_PASS_DEPENDENCY(AddDiscriminators)<br>
-INITIALIZE_PASS_END(SampleProfileLoader, "sample-profile",<br>
- "Sample Profile loader", false, false)<br>
-<br>
-bool SampleProfileLoader::doInitialization(Module &M) {<br>
- auto ReaderOrErr = SampleProfileReader::create(Filename, M.getContext());<br>
- if (std::error_code EC = ReaderOrErr.getError()) {<br>
- std::string Msg = "Could not open profile: " + EC.message();<br>
- M.getContext().diagnose(DiagnosticInfoSampleProfile(Filename.data(), Msg));<br>
- return false;<br>
- }<br>
- Reader = std::move(ReaderOrErr.get());<br>
- ProfileIsValid = (Reader->read() == sampleprof_error::success);<br>
- return true;<br>
-}<br>
-<br>
-FunctionPass *llvm::createSampleProfileLoaderPass() {<br>
- return new SampleProfileLoader(SampleProfileFile);<br>
-}<br>
-<br>
-FunctionPass *llvm::createSampleProfileLoaderPass(StringRef Name) {<br>
- return new SampleProfileLoader(Name);<br>
-}<br>
-<br>
-bool SampleProfileLoader::runOnFunction(Function &F) {<br>
- if (!ProfileIsValid)<br>
- return false;<br>
-<br>
- DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();<br>
- PDT = &getAnalysis<PostDominatorTree>();<br>
- LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();<br>
- Ctx = &F.getParent()->getContext();<br>
- Samples = Reader->getSamplesFor(F);<br>
- if (!Samples->empty())<br>
- return emitAnnotations(F);<br>
- return false;<br>
-}<br>
<br>
Modified: llvm/trunk/lib/Transforms/Scalar/Scalar.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/Scalar.cpp?rev=245940&r1=245939&r2=245940&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/Scalar.cpp?rev=245940&r1=245939&r2=245940&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/Scalar.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Scalar/Scalar.cpp Tue Aug 25 10:25:11 2015<br>
@@ -33,7 +33,6 @@ void llvm::initializeScalarOpts(PassRegi<br>
initializeADCEPass(Registry);<br>
initializeBDCEPass(Registry);<br>
initializeAlignmentFromAssumptionsPass(Registry);<br>
- initializeSampleProfileLoaderPass(Registry);<br>
initializeConstantHoistingPass(Registry);<br>
initializeConstantPropagationPass(Registry);<br>
initializeCorrelatedValuePropagationPass(Registry);<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br><br clear="all"><div><br></div></div></div><span class="HOEnZb"><font color="#888888">-- <br><div><div dir="ltr">Alexey Samsonov<br><a href="mailto:vonosmas@gmail.com" target="_blank">vonosmas@gmail.com</a></div></div>
</font></span></div>
</blockquote></div><br></div>