blob: 89e47774fdf20e0e0678bb45ac27ae47adc67cd4 [file] [log] [blame]
Rong Xuf430ae42015-12-09 18:08:16 +00001//===-- PGOInstrumentation.cpp - MST-based PGO Instrumentation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements PGO instrumentation using a minimum spanning tree based
11// on the following paper:
12// [1] Donald E. Knuth, Francis R. Stevenson. Optimal measurement of points
13// for program frequency counts. BIT Numerical Mathematics 1973, Volume 13,
14// Issue 3, pp 313-322
15// The idea of the algorithm based on the fact that for each node (except for
16// the entry and exit), the sum of incoming edge counts equals the sum of
17// outgoing edge counts. The count of edge on spanning tree can be derived from
18// those edges not on the spanning tree. Knuth proves this method instruments
19// the minimum number of edges.
20//
21// The minimal spanning tree here is actually a maximum weight tree -- on-tree
22// edges have higher frequencies (more likely to execute). The idea is to
23// instrument those less frequently executed edges to reduce the runtime
24// overhead of instrumented binaries.
25//
26// This file contains two passes:
27// (1) Pass PGOInstrumentationGen which instruments the IR to generate edge
28// count profile, and
29// (2) Pass PGOInstrumentationUse which reads the edge count profile and
30// annotates the branch weights.
31// To get the precise counter information, These two passes need to invoke at
32// the same compilation point (so they see the same IR). For pass
33// PGOInstrumentationGen, the real work is done in instrumentOneFunc(). For
34// pass PGOInstrumentationUse, the real work in done in class PGOUseFunc and
35// the profile is opened in module level and passed to each PGOUseFunc instance.
36// The shared code for PGOInstrumentationGen and PGOInstrumentationUse is put
37// in class FuncPGOInstrumentation.
38//
39// Class PGOEdge represents a CFG edge and some auxiliary information. Class
40// BBInfo contains auxiliary information for each BB. These two classes are used
41// in pass PGOInstrumentationGen. Class PGOUseEdge and UseBBInfo are the derived
42// class of PGOEdge and BBInfo, respectively. They contains extra data structure
43// used in populating profile counters.
44// The MST implementation is in Class CFGMST (CFGMST.h).
45//
46//===----------------------------------------------------------------------===//
47
Rong Xuf430ae42015-12-09 18:08:16 +000048#include "CFGMST.h"
49#include "llvm/ADT/DenseMap.h"
50#include "llvm/ADT/STLExtras.h"
51#include "llvm/ADT/Statistic.h"
Rong Xu33c76c02016-02-10 17:18:30 +000052#include "llvm/ADT/Triple.h"
Rong Xuf430ae42015-12-09 18:08:16 +000053#include "llvm/Analysis/BlockFrequencyInfo.h"
54#include "llvm/Analysis/BranchProbabilityInfo.h"
55#include "llvm/Analysis/CFG.h"
Rong Xued9fec72016-01-21 18:11:44 +000056#include "llvm/IR/CallSite.h"
Rong Xuf430ae42015-12-09 18:08:16 +000057#include "llvm/IR/DiagnosticInfo.h"
58#include "llvm/IR/IRBuilder.h"
59#include "llvm/IR/InstIterator.h"
Rong Xued9fec72016-01-21 18:11:44 +000060#include "llvm/IR/InstVisitor.h"
Rong Xuf430ae42015-12-09 18:08:16 +000061#include "llvm/IR/Instructions.h"
62#include "llvm/IR/IntrinsicInst.h"
63#include "llvm/IR/MDBuilder.h"
64#include "llvm/IR/Module.h"
65#include "llvm/Pass.h"
66#include "llvm/ProfileData/InstrProfReader.h"
67#include "llvm/Support/BranchProbability.h"
68#include "llvm/Support/Debug.h"
69#include "llvm/Support/JamCRC.h"
Rong Xued9fec72016-01-21 18:11:44 +000070#include "llvm/Transforms/Instrumentation.h"
Rong Xuf430ae42015-12-09 18:08:16 +000071#include "llvm/Transforms/Utils/BasicBlockUtils.h"
72#include <string>
73#include <utility>
74#include <vector>
75
76using namespace llvm;
77
78#define DEBUG_TYPE "pgo-instrumentation"
79
80STATISTIC(NumOfPGOInstrument, "Number of edges instrumented.");
81STATISTIC(NumOfPGOEdge, "Number of edges.");
82STATISTIC(NumOfPGOBB, "Number of basic-blocks.");
83STATISTIC(NumOfPGOSplit, "Number of critical edge splits.");
84STATISTIC(NumOfPGOFunc, "Number of functions having valid profile counts.");
85STATISTIC(NumOfPGOMismatch, "Number of functions having mismatch profile.");
86STATISTIC(NumOfPGOMissing, "Number of functions without profile.");
Rong Xued9fec72016-01-21 18:11:44 +000087STATISTIC(NumOfPGOICall, "Number of indirect call value instrumentation.");
Rong Xuf430ae42015-12-09 18:08:16 +000088
89// Command line option to specify the file to read profile from. This is
90// mainly used for testing.
91static cl::opt<std::string>
92 PGOTestProfileFile("pgo-test-profile-file", cl::init(""), cl::Hidden,
93 cl::value_desc("filename"),
94 cl::desc("Specify the path of profile data file. This is"
95 "mainly for test purpose."));
96
Rong Xued9fec72016-01-21 18:11:44 +000097// Command line options to disable value profiling. The default is false:
98// i.e. vaule profiling is enabled by default. This is for debug purpose.
99static cl::opt<bool>
100DisableValueProfiling("disable-vp", cl::init(false),
101 cl::Hidden,
102 cl::desc("Disable Value Profiling"));
103
Rong Xuf430ae42015-12-09 18:08:16 +0000104namespace {
105class PGOInstrumentationGen : public ModulePass {
106public:
107 static char ID;
108
109 PGOInstrumentationGen() : ModulePass(ID) {
110 initializePGOInstrumentationGenPass(*PassRegistry::getPassRegistry());
111 }
112
113 const char *getPassName() const override {
114 return "PGOInstrumentationGenPass";
115 }
116
117private:
118 bool runOnModule(Module &M) override;
119
120 void getAnalysisUsage(AnalysisUsage &AU) const override {
121 AU.addRequired<BlockFrequencyInfoWrapperPass>();
122 }
123};
124
125class PGOInstrumentationUse : public ModulePass {
126public:
127 static char ID;
128
129 // Provide the profile filename as the parameter.
130 PGOInstrumentationUse(std::string Filename = "")
131 : ModulePass(ID), ProfileFileName(Filename) {
132 if (!PGOTestProfileFile.empty())
133 ProfileFileName = PGOTestProfileFile;
134 initializePGOInstrumentationUsePass(*PassRegistry::getPassRegistry());
135 }
136
137 const char *getPassName() const override {
138 return "PGOInstrumentationUsePass";
139 }
140
141private:
142 std::string ProfileFileName;
143 std::unique_ptr<IndexedInstrProfReader> PGOReader;
144 bool runOnModule(Module &M) override;
145
146 void getAnalysisUsage(AnalysisUsage &AU) const override {
147 AU.addRequired<BlockFrequencyInfoWrapperPass>();
148 }
149};
150} // end anonymous namespace
151
152char PGOInstrumentationGen::ID = 0;
153INITIALIZE_PASS_BEGIN(PGOInstrumentationGen, "pgo-instr-gen",
154 "PGO instrumentation.", false, false)
155INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
156INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
157INITIALIZE_PASS_END(PGOInstrumentationGen, "pgo-instr-gen",
158 "PGO instrumentation.", false, false)
159
160ModulePass *llvm::createPGOInstrumentationGenPass() {
161 return new PGOInstrumentationGen();
162}
163
164char PGOInstrumentationUse::ID = 0;
165INITIALIZE_PASS_BEGIN(PGOInstrumentationUse, "pgo-instr-use",
166 "Read PGO instrumentation profile.", false, false)
167INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
168INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
169INITIALIZE_PASS_END(PGOInstrumentationUse, "pgo-instr-use",
170 "Read PGO instrumentation profile.", false, false)
171
172ModulePass *llvm::createPGOInstrumentationUsePass(StringRef Filename) {
173 return new PGOInstrumentationUse(Filename.str());
174}
175
176namespace {
177/// \brief An MST based instrumentation for PGO
178///
179/// Implements a Minimum Spanning Tree (MST) based instrumentation for PGO
180/// in the function level.
181struct PGOEdge {
182 // This class implements the CFG edges. Note the CFG can be a multi-graph.
183 // So there might be multiple edges with same SrcBB and DestBB.
184 const BasicBlock *SrcBB;
185 const BasicBlock *DestBB;
186 uint64_t Weight;
187 bool InMST;
188 bool Removed;
189 bool IsCritical;
190 PGOEdge(const BasicBlock *Src, const BasicBlock *Dest, unsigned W = 1)
191 : SrcBB(Src), DestBB(Dest), Weight(W), InMST(false), Removed(false),
192 IsCritical(false) {}
193 // Return the information string of an edge.
194 const std::string infoString() const {
195 return (Twine(Removed ? "-" : " ") + (InMST ? " " : "*") +
196 (IsCritical ? "c" : " ") + " W=" + Twine(Weight)).str();
197 }
198};
199
200// This class stores the auxiliary information for each BB.
201struct BBInfo {
202 BBInfo *Group;
203 uint32_t Index;
204 uint32_t Rank;
205
206 BBInfo(unsigned IX) : Group(this), Index(IX), Rank(0) {}
207
208 // Return the information string of this object.
209 const std::string infoString() const {
210 return (Twine("Index=") + Twine(Index)).str();
211 }
212};
213
214// This class implements the CFG edges. Note the CFG can be a multi-graph.
215template <class Edge, class BBInfo> class FuncPGOInstrumentation {
216private:
217 Function &F;
218 void computeCFGHash();
219
220public:
221 std::string FuncName;
222 GlobalVariable *FuncNameVar;
223 // CFG hash value for this function.
224 uint64_t FunctionHash;
225
226 // The Minimum Spanning Tree of function CFG.
227 CFGMST<Edge, BBInfo> MST;
228
229 // Give an edge, find the BB that will be instrumented.
230 // Return nullptr if there is no BB to be instrumented.
231 BasicBlock *getInstrBB(Edge *E);
232
233 // Return the auxiliary BB information.
234 BBInfo &getBBInfo(const BasicBlock *BB) const { return MST.getBBInfo(BB); }
235
236 // Dump edges and BB information.
237 void dumpInfo(std::string Str = "") const {
238 MST.dumpEdges(dbgs(), Twine("Dump Function ") + FuncName + " Hash: " +
Rong Xued9fec72016-01-21 18:11:44 +0000239 Twine(FunctionHash) + "\t" + Str);
Rong Xuf430ae42015-12-09 18:08:16 +0000240 }
241
242 FuncPGOInstrumentation(Function &Func, bool CreateGlobalVar = false,
243 BranchProbabilityInfo *BPI = nullptr,
244 BlockFrequencyInfo *BFI = nullptr)
245 : F(Func), FunctionHash(0), MST(F, BPI, BFI) {
246 FuncName = getPGOFuncName(F);
247 computeCFGHash();
248 DEBUG(dumpInfo("after CFGMST"));
249
250 NumOfPGOBB += MST.BBInfos.size();
251 for (auto &E : MST.AllEdges) {
252 if (E->Removed)
253 continue;
254 NumOfPGOEdge++;
255 if (!E->InMST)
256 NumOfPGOInstrument++;
257 }
258
259 if (CreateGlobalVar)
260 FuncNameVar = createPGOFuncNameVar(F, FuncName);
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000261 }
Rong Xuf430ae42015-12-09 18:08:16 +0000262};
263
264// Compute Hash value for the CFG: the lower 32 bits are CRC32 of the index
265// value of each BB in the CFG. The higher 32 bits record the number of edges.
266template <class Edge, class BBInfo>
267void FuncPGOInstrumentation<Edge, BBInfo>::computeCFGHash() {
268 std::vector<char> Indexes;
269 JamCRC JC;
270 for (auto &BB : F) {
271 const TerminatorInst *TI = BB.getTerminator();
272 for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) {
273 BasicBlock *Succ = TI->getSuccessor(I);
274 uint32_t Index = getBBInfo(Succ).Index;
275 for (int J = 0; J < 4; J++)
276 Indexes.push_back((char)(Index >> (J * 8)));
277 }
278 }
279 JC.update(Indexes);
280 FunctionHash = (uint64_t)MST.AllEdges.size() << 32 | JC.getCRC();
281}
282
283// Given a CFG E to be instrumented, find which BB to place the instrumented
284// code. The function will split the critical edge if necessary.
285template <class Edge, class BBInfo>
286BasicBlock *FuncPGOInstrumentation<Edge, BBInfo>::getInstrBB(Edge *E) {
287 if (E->InMST || E->Removed)
288 return nullptr;
289
290 BasicBlock *SrcBB = const_cast<BasicBlock *>(E->SrcBB);
291 BasicBlock *DestBB = const_cast<BasicBlock *>(E->DestBB);
292 // For a fake edge, instrument the real BB.
293 if (SrcBB == nullptr)
294 return DestBB;
295 if (DestBB == nullptr)
296 return SrcBB;
297
298 // Instrument the SrcBB if it has a single successor,
299 // otherwise, the DestBB if this is not a critical edge.
300 TerminatorInst *TI = SrcBB->getTerminator();
301 if (TI->getNumSuccessors() <= 1)
302 return SrcBB;
303 if (!E->IsCritical)
304 return DestBB;
305
306 // For a critical edge, we have to split. Instrument the newly
307 // created BB.
308 NumOfPGOSplit++;
309 DEBUG(dbgs() << "Split critical edge: " << getBBInfo(SrcBB).Index << " --> "
310 << getBBInfo(DestBB).Index << "\n");
311 unsigned SuccNum = GetSuccessorNumber(SrcBB, DestBB);
312 BasicBlock *InstrBB = SplitCriticalEdge(TI, SuccNum);
313 assert(InstrBB && "Critical edge is not split");
314
315 E->Removed = true;
316 return InstrBB;
317}
318
Rong Xued9fec72016-01-21 18:11:44 +0000319// Visitor class that finds all indirect call sites.
320struct PGOIndirectCallSiteVisitor
321 : public InstVisitor<PGOIndirectCallSiteVisitor> {
322 std::vector<CallInst *> IndirectCallInsts;
323 PGOIndirectCallSiteVisitor() {}
324
325 void visitCallInst(CallInst &I) {
326 CallSite CS(&I);
327 if (CS.getCalledFunction() || !CS.getCalledValue())
328 return;
329 IndirectCallInsts.push_back(&I);
330 }
331};
332
333// Visit all edge and instrument the edges not in MST, and do value profiling.
Rong Xuf430ae42015-12-09 18:08:16 +0000334// Critical edges will be split.
335static void instrumentOneFunc(Function &F, Module *M,
336 BranchProbabilityInfo *BPI,
337 BlockFrequencyInfo *BFI) {
338 unsigned NumCounters = 0;
339 FuncPGOInstrumentation<PGOEdge, BBInfo> FuncInfo(F, true, BPI, BFI);
340 for (auto &E : FuncInfo.MST.AllEdges) {
341 if (!E->InMST && !E->Removed)
342 NumCounters++;
343 }
344
345 uint32_t I = 0;
Rong Xued9fec72016-01-21 18:11:44 +0000346 Type *I8PtrTy = Type::getInt8PtrTy(M->getContext());
Rong Xuf430ae42015-12-09 18:08:16 +0000347 for (auto &E : FuncInfo.MST.AllEdges) {
348 BasicBlock *InstrBB = FuncInfo.getInstrBB(E.get());
349 if (!InstrBB)
350 continue;
351
352 IRBuilder<> Builder(InstrBB, InstrBB->getFirstInsertionPt());
353 assert(Builder.GetInsertPoint() != InstrBB->end() &&
354 "Cannot get the Instrumentation point");
Rong Xuf430ae42015-12-09 18:08:16 +0000355 Builder.CreateCall(
356 Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment),
357 {llvm::ConstantExpr::getBitCast(FuncInfo.FuncNameVar, I8PtrTy),
358 Builder.getInt64(FuncInfo.FunctionHash), Builder.getInt32(NumCounters),
359 Builder.getInt32(I++)});
360 }
Rong Xued9fec72016-01-21 18:11:44 +0000361
362 if (DisableValueProfiling)
363 return;
364
365 unsigned NumIndirectCallSites = 0;
366 PGOIndirectCallSiteVisitor ICV;
367 ICV.visit(F);
368 for (auto &I : ICV.IndirectCallInsts) {
369 CallSite CS(I);
370 Value *Callee = CS.getCalledValue();
371 DEBUG(dbgs() << "Instrument one indirect call: CallSite Index = "
372 << NumIndirectCallSites << "\n");
373 IRBuilder<> Builder(I);
374 assert(Builder.GetInsertPoint() != I->getParent()->end() &&
375 "Cannot get the Instrumentation point");
376 Builder.CreateCall(
377 Intrinsic::getDeclaration(M, Intrinsic::instrprof_value_profile),
378 {llvm::ConstantExpr::getBitCast(FuncInfo.FuncNameVar, I8PtrTy),
379 Builder.getInt64(FuncInfo.FunctionHash),
380 Builder.CreatePtrToInt(Callee, Builder.getInt64Ty()),
381 Builder.getInt32(llvm::InstrProfValueKind::IPVK_IndirectCallTarget),
382 Builder.getInt32(NumIndirectCallSites++)});
383 }
384 NumOfPGOICall += NumIndirectCallSites;
Rong Xuf430ae42015-12-09 18:08:16 +0000385}
386
387// This class represents a CFG edge in profile use compilation.
388struct PGOUseEdge : public PGOEdge {
389 bool CountValid;
390 uint64_t CountValue;
391 PGOUseEdge(const BasicBlock *Src, const BasicBlock *Dest, unsigned W = 1)
392 : PGOEdge(Src, Dest, W), CountValid(false), CountValue(0) {}
393
394 // Set edge count value
395 void setEdgeCount(uint64_t Value) {
396 CountValue = Value;
397 CountValid = true;
398 }
399
400 // Return the information string for this object.
401 const std::string infoString() const {
402 if (!CountValid)
403 return PGOEdge::infoString();
404 return (Twine(PGOEdge::infoString()) + " Count=" + Twine(CountValue)).str();
405 }
406};
407
408typedef SmallVector<PGOUseEdge *, 2> DirectEdges;
409
410// This class stores the auxiliary information for each BB.
411struct UseBBInfo : public BBInfo {
412 uint64_t CountValue;
413 bool CountValid;
414 int32_t UnknownCountInEdge;
415 int32_t UnknownCountOutEdge;
416 DirectEdges InEdges;
417 DirectEdges OutEdges;
418 UseBBInfo(unsigned IX)
419 : BBInfo(IX), CountValue(0), CountValid(false), UnknownCountInEdge(0),
420 UnknownCountOutEdge(0) {}
421 UseBBInfo(unsigned IX, uint64_t C)
422 : BBInfo(IX), CountValue(C), CountValid(true), UnknownCountInEdge(0),
423 UnknownCountOutEdge(0) {}
424
425 // Set the profile count value for this BB.
426 void setBBInfoCount(uint64_t Value) {
427 CountValue = Value;
428 CountValid = true;
429 }
430
431 // Return the information string of this object.
432 const std::string infoString() const {
433 if (!CountValid)
434 return BBInfo::infoString();
435 return (Twine(BBInfo::infoString()) + " Count=" + Twine(CountValue)).str();
436 }
437};
438
439// Sum up the count values for all the edges.
440static uint64_t sumEdgeCount(const ArrayRef<PGOUseEdge *> Edges) {
441 uint64_t Total = 0;
442 for (auto &E : Edges) {
443 if (E->Removed)
444 continue;
445 Total += E->CountValue;
446 }
447 return Total;
448}
449
450class PGOUseFunc {
451private:
452 Function &F;
453 Module *M;
454 // This member stores the shared information with class PGOGenFunc.
455 FuncPGOInstrumentation<PGOUseEdge, UseBBInfo> FuncInfo;
456
457 // Return the auxiliary BB information.
458 UseBBInfo &getBBInfo(const BasicBlock *BB) const {
459 return FuncInfo.getBBInfo(BB);
460 }
461
462 // The maximum count value in the profile. This is only used in PGO use
463 // compilation.
464 uint64_t ProgramMaxCount;
465
466 // Find the Instrumented BB and set the value.
467 void setInstrumentedCounts(const std::vector<uint64_t> &CountFromProfile);
468
469 // Set the edge counter value for the unknown edge -- there should be only
470 // one unknown edge.
471 void setEdgeCount(DirectEdges &Edges, uint64_t Value);
472
473 // Return FuncName string;
474 const std::string getFuncName() const { return FuncInfo.FuncName; }
475
476 // Set the hot/cold inline hints based on the count values.
477 // FIXME: This function should be removed once the functionality in
478 // the inliner is implemented.
479 void applyFunctionAttributes(uint64_t EntryCount, uint64_t MaxCount) {
480 if (ProgramMaxCount == 0)
481 return;
482 // Threshold of the hot functions.
483 const BranchProbability HotFunctionThreshold(1, 100);
484 // Threshold of the cold functions.
485 const BranchProbability ColdFunctionThreshold(2, 10000);
486 if (EntryCount >= HotFunctionThreshold.scale(ProgramMaxCount))
487 F.addFnAttr(llvm::Attribute::InlineHint);
488 else if (MaxCount <= ColdFunctionThreshold.scale(ProgramMaxCount))
489 F.addFnAttr(llvm::Attribute::Cold);
490 }
491
492public:
493 PGOUseFunc(Function &Func, Module *Modu, BranchProbabilityInfo *BPI = nullptr,
494 BlockFrequencyInfo *BFI = nullptr)
495 : F(Func), M(Modu), FuncInfo(Func, false, BPI, BFI) {}
496
497 // Read counts for the instrumented BB from profile.
498 bool readCounters(IndexedInstrProfReader *PGOReader);
499
500 // Populate the counts for all BBs.
501 void populateCounters();
502
503 // Set the branch weights based on the count values.
504 void setBranchWeights();
505};
506
507// Visit all the edges and assign the count value for the instrumented
508// edges and the BB.
509void PGOUseFunc::setInstrumentedCounts(
510 const std::vector<uint64_t> &CountFromProfile) {
511
512 // Use a worklist as we will update the vector during the iteration.
513 std::vector<PGOUseEdge *> WorkList;
514 for (auto &E : FuncInfo.MST.AllEdges)
515 WorkList.push_back(E.get());
516
517 uint32_t I = 0;
518 for (auto &E : WorkList) {
519 BasicBlock *InstrBB = FuncInfo.getInstrBB(E);
520 if (!InstrBB)
521 continue;
522 uint64_t CountValue = CountFromProfile[I++];
523 if (!E->Removed) {
524 getBBInfo(InstrBB).setBBInfoCount(CountValue);
525 E->setEdgeCount(CountValue);
526 continue;
527 }
528
529 // Need to add two new edges.
530 BasicBlock *SrcBB = const_cast<BasicBlock *>(E->SrcBB);
531 BasicBlock *DestBB = const_cast<BasicBlock *>(E->DestBB);
532 // Add new edge of SrcBB->InstrBB.
533 PGOUseEdge &NewEdge = FuncInfo.MST.addEdge(SrcBB, InstrBB, 0);
534 NewEdge.setEdgeCount(CountValue);
535 // Add new edge of InstrBB->DestBB.
536 PGOUseEdge &NewEdge1 = FuncInfo.MST.addEdge(InstrBB, DestBB, 0);
537 NewEdge1.setEdgeCount(CountValue);
538 NewEdge1.InMST = true;
539 getBBInfo(InstrBB).setBBInfoCount(CountValue);
540 }
541}
542
543// Set the count value for the unknown edge. There should be one and only one
544// unknown edge in Edges vector.
545void PGOUseFunc::setEdgeCount(DirectEdges &Edges, uint64_t Value) {
546 for (auto &E : Edges) {
547 if (E->CountValid)
548 continue;
549 E->setEdgeCount(Value);
550
551 getBBInfo(E->SrcBB).UnknownCountOutEdge--;
552 getBBInfo(E->DestBB).UnknownCountInEdge--;
553 return;
554 }
555 llvm_unreachable("Cannot find the unknown count edge");
556}
557
558// Read the profile from ProfileFileName and assign the value to the
559// instrumented BB and the edges. This function also updates ProgramMaxCount.
560// Return true if the profile are successfully read, and false on errors.
561bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader) {
562 auto &Ctx = M->getContext();
563 ErrorOr<InstrProfRecord> Result =
564 PGOReader->getInstrProfRecord(FuncInfo.FuncName, FuncInfo.FunctionHash);
565 if (std::error_code EC = Result.getError()) {
566 if (EC == instrprof_error::unknown_function)
567 NumOfPGOMissing++;
568 else if (EC == instrprof_error::hash_mismatch ||
569 EC == llvm::instrprof_error::malformed)
570 NumOfPGOMismatch++;
571
572 std::string Msg = EC.message() + std::string(" ") + F.getName().str();
573 Ctx.diagnose(
574 DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning));
575 return false;
576 }
577 std::vector<uint64_t> &CountFromProfile = Result.get().Counts;
578
579 NumOfPGOFunc++;
580 DEBUG(dbgs() << CountFromProfile.size() << " counts\n");
581 uint64_t ValueSum = 0;
582 for (unsigned I = 0, S = CountFromProfile.size(); I < S; I++) {
583 DEBUG(dbgs() << " " << I << ": " << CountFromProfile[I] << "\n");
584 ValueSum += CountFromProfile[I];
585 }
586
587 DEBUG(dbgs() << "SUM = " << ValueSum << "\n");
588
589 getBBInfo(nullptr).UnknownCountOutEdge = 2;
590 getBBInfo(nullptr).UnknownCountInEdge = 2;
591
592 setInstrumentedCounts(CountFromProfile);
593 ProgramMaxCount = PGOReader->getMaximumFunctionCount();
594 return true;
595}
596
597// Populate the counters from instrumented BBs to all BBs.
598// In the end of this operation, all BBs should have a valid count value.
599void PGOUseFunc::populateCounters() {
600 // First set up Count variable for all BBs.
601 for (auto &E : FuncInfo.MST.AllEdges) {
602 if (E->Removed)
603 continue;
604
605 const BasicBlock *SrcBB = E->SrcBB;
606 const BasicBlock *DestBB = E->DestBB;
607 UseBBInfo &SrcInfo = getBBInfo(SrcBB);
608 UseBBInfo &DestInfo = getBBInfo(DestBB);
609 SrcInfo.OutEdges.push_back(E.get());
610 DestInfo.InEdges.push_back(E.get());
611 SrcInfo.UnknownCountOutEdge++;
612 DestInfo.UnknownCountInEdge++;
613
614 if (!E->CountValid)
615 continue;
616 DestInfo.UnknownCountInEdge--;
617 SrcInfo.UnknownCountOutEdge--;
618 }
619
620 bool Changes = true;
621 unsigned NumPasses = 0;
622 while (Changes) {
623 NumPasses++;
624 Changes = false;
625
626 // For efficient traversal, it's better to start from the end as most
627 // of the instrumented edges are at the end.
628 for (auto &BB : reverse(F)) {
629 UseBBInfo &Count = getBBInfo(&BB);
630 if (!Count.CountValid) {
631 if (Count.UnknownCountOutEdge == 0) {
632 Count.CountValue = sumEdgeCount(Count.OutEdges);
633 Count.CountValid = true;
634 Changes = true;
635 } else if (Count.UnknownCountInEdge == 0) {
636 Count.CountValue = sumEdgeCount(Count.InEdges);
637 Count.CountValid = true;
638 Changes = true;
639 }
640 }
641 if (Count.CountValid) {
642 if (Count.UnknownCountOutEdge == 1) {
643 uint64_t Total = Count.CountValue - sumEdgeCount(Count.OutEdges);
644 setEdgeCount(Count.OutEdges, Total);
645 Changes = true;
646 }
647 if (Count.UnknownCountInEdge == 1) {
648 uint64_t Total = Count.CountValue - sumEdgeCount(Count.InEdges);
649 setEdgeCount(Count.InEdges, Total);
650 Changes = true;
651 }
652 }
653 }
654 }
655
656 DEBUG(dbgs() << "Populate counts in " << NumPasses << " passes.\n");
657 // Assert every BB has a valid counter.
658 uint64_t FuncEntryCount = getBBInfo(&*F.begin()).CountValue;
659 uint64_t FuncMaxCount = FuncEntryCount;
660 for (auto &BB : F) {
661 assert(getBBInfo(&BB).CountValid && "BB count is not valid");
662 uint64_t Count = getBBInfo(&BB).CountValue;
663 if (Count > FuncMaxCount)
664 FuncMaxCount = Count;
665 }
666 applyFunctionAttributes(FuncEntryCount, FuncMaxCount);
667
668 DEBUG(FuncInfo.dumpInfo("after reading profile."));
669}
670
671// Assign the scaled count values to the BB with multiple out edges.
672void PGOUseFunc::setBranchWeights() {
673 // Generate MD_prof metadata for every branch instruction.
674 DEBUG(dbgs() << "\nSetting branch weights.\n");
675 MDBuilder MDB(M->getContext());
676 for (auto &BB : F) {
677 TerminatorInst *TI = BB.getTerminator();
678 if (TI->getNumSuccessors() < 2)
679 continue;
680 if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
681 continue;
682 if (getBBInfo(&BB).CountValue == 0)
683 continue;
684
685 // We have a non-zero Branch BB.
686 const UseBBInfo &BBCountInfo = getBBInfo(&BB);
687 unsigned Size = BBCountInfo.OutEdges.size();
688 SmallVector<unsigned, 2> EdgeCounts(Size, 0);
689 uint64_t MaxCount = 0;
690 for (unsigned s = 0; s < Size; s++) {
691 const PGOUseEdge *E = BBCountInfo.OutEdges[s];
692 const BasicBlock *SrcBB = E->SrcBB;
693 const BasicBlock *DestBB = E->DestBB;
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000694 if (DestBB == nullptr)
Rong Xuf430ae42015-12-09 18:08:16 +0000695 continue;
696 unsigned SuccNum = GetSuccessorNumber(SrcBB, DestBB);
697 uint64_t EdgeCount = E->CountValue;
698 if (EdgeCount > MaxCount)
699 MaxCount = EdgeCount;
700 EdgeCounts[SuccNum] = EdgeCount;
701 }
702 assert(MaxCount > 0 && "Bad max count");
703 uint64_t Scale = calculateCountScale(MaxCount);
704 SmallVector<unsigned, 4> Weights;
705 for (const auto &ECI : EdgeCounts)
706 Weights.push_back(scaleBranchCount(ECI, Scale));
707
708 TI->setMetadata(llvm::LLVMContext::MD_prof,
709 MDB.createBranchWeights(Weights));
710 DEBUG(dbgs() << "Weight is: ";
711 for (const auto &W : Weights) { dbgs() << W << " "; }
712 dbgs() << "\n";);
713 }
714}
715} // end anonymous namespace
716
Rong Xu33c76c02016-02-10 17:18:30 +0000717// Create a COMDAT variable IR_LEVEL_PROF_VARNAME to make the runtime
718// aware this is an ir_level profile so it can set the version flag.
719static void createIRLevelProfileFlagVariable(Module &M) {
720 Type *IntTy64 = Type::getInt64Ty(M.getContext());
721 uint64_t ProfileVersion = (INSTR_PROF_RAW_VERSION | VARIANT_MASK_IR_PROF);
722 auto IRLevelVersionVariable =
723 new GlobalVariable(M, IntTy64, true, GlobalVariable::ExternalLinkage,
724 Constant::getIntegerValue(IntTy64, APInt(64, ProfileVersion)),
725 INSTR_PROF_QUOTE(IR_LEVEL_PROF_VERSION_VAR));
726 IRLevelVersionVariable->setVisibility(GlobalValue::DefaultVisibility);
727 Triple TT(M.getTargetTriple());
728 if (TT.isOSBinFormatMachO())
729 IRLevelVersionVariable->setLinkage(GlobalValue::LinkOnceODRLinkage);
730 else
731 IRLevelVersionVariable->setComdat(
732 M.getOrInsertComdat(StringRef(INSTR_PROF_QUOTE(IR_LEVEL_PROF_VERSION_VAR))));
733}
734
Rong Xuf430ae42015-12-09 18:08:16 +0000735bool PGOInstrumentationGen::runOnModule(Module &M) {
Rong Xu33c76c02016-02-10 17:18:30 +0000736 createIRLevelProfileFlagVariable(M);
Rong Xuf430ae42015-12-09 18:08:16 +0000737 for (auto &F : M) {
738 if (F.isDeclaration())
739 continue;
740 BranchProbabilityInfo *BPI =
741 &(getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI());
742 BlockFrequencyInfo *BFI =
743 &(getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI());
744 instrumentOneFunc(F, &M, BPI, BFI);
745 }
746 return true;
747}
748
749static void setPGOCountOnFunc(PGOUseFunc &Func,
750 IndexedInstrProfReader *PGOReader) {
751 if (Func.readCounters(PGOReader)) {
752 Func.populateCounters();
753 Func.setBranchWeights();
754 }
755}
756
757bool PGOInstrumentationUse::runOnModule(Module &M) {
758 DEBUG(dbgs() << "Read in profile counters: ");
759 auto &Ctx = M.getContext();
760 // Read the counter array from file.
761 auto ReaderOrErr = IndexedInstrProfReader::create(ProfileFileName);
762 if (std::error_code EC = ReaderOrErr.getError()) {
763 Ctx.diagnose(
764 DiagnosticInfoPGOProfile(ProfileFileName.data(), EC.message()));
765 return false;
766 }
767
768 PGOReader = std::move(ReaderOrErr.get());
769 if (!PGOReader) {
770 Ctx.diagnose(DiagnosticInfoPGOProfile(ProfileFileName.data(),
771 "Cannot get PGOReader"));
772 return false;
773 }
Rong Xu33c76c02016-02-10 17:18:30 +0000774 // TODO: might need to change the warning once the clang option is finalized.
775 if (!PGOReader->isIRLevelProfile()) {
776 Ctx.diagnose(DiagnosticInfoPGOProfile(
777 ProfileFileName.data(), "Not an IR level instrumentation profile"));
778 return false;
779 }
780
Rong Xuf430ae42015-12-09 18:08:16 +0000781
782 for (auto &F : M) {
783 if (F.isDeclaration())
784 continue;
785 BranchProbabilityInfo *BPI =
786 &(getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI());
787 BlockFrequencyInfo *BFI =
788 &(getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI());
789 PGOUseFunc Func(F, &M, BPI, BFI);
790 setPGOCountOnFunc(Func, PGOReader.get());
791 }
792 return true;
793}