1 //===- PGOInstrumentation.cpp - MST-based PGO Instrumentation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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.
26 // This file contains two passes:
27 // (1) Pass PGOInstrumentationGen which instruments the IR to generate edge
28 // count profile, and generates the instrumentation for indirect call
30 // (2) Pass PGOInstrumentationUse which reads the edge count profile and
31 // annotates the branch weights. It also reads the indirect call value
32 // profiling records and annotate the indirect call instructions.
34 // To get the precise counter information, These two passes need to invoke at
35 // the same compilation point (so they see the same IR). For pass
36 // PGOInstrumentationGen, the real work is done in instrumentOneFunc(). For
37 // pass PGOInstrumentationUse, the real work in done in class PGOUseFunc and
38 // the profile is opened in module level and passed to each PGOUseFunc instance.
39 // The shared code for PGOInstrumentationGen and PGOInstrumentationUse is put
40 // in class FuncPGOInstrumentation.
42 // Class PGOEdge represents a CFG edge and some auxiliary information. Class
43 // BBInfo contains auxiliary information for each BB. These two classes are used
44 // in pass PGOInstrumentationGen. Class PGOUseEdge and UseBBInfo are the derived
45 // class of PGOEdge and BBInfo, respectively. They contains extra data structure
46 // used in populating profile counters.
47 // The MST implementation is in Class CFGMST (CFGMST.h).
49 //===----------------------------------------------------------------------===//
51 #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
53 #include "llvm/ADT/APInt.h"
54 #include "llvm/ADT/ArrayRef.h"
55 #include "llvm/ADT/STLExtras.h"
56 #include "llvm/ADT/SmallVector.h"
57 #include "llvm/ADT/Statistic.h"
58 #include "llvm/ADT/StringRef.h"
59 #include "llvm/ADT/Triple.h"
60 #include "llvm/ADT/Twine.h"
61 #include "llvm/ADT/iterator.h"
62 #include "llvm/ADT/iterator_range.h"
63 #include "llvm/Analysis/BlockFrequencyInfo.h"
64 #include "llvm/Analysis/BranchProbabilityInfo.h"
65 #include "llvm/Analysis/CFG.h"
66 #include "llvm/Analysis/IndirectCallVisitor.h"
67 #include "llvm/Analysis/LoopInfo.h"
68 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
69 #include "llvm/IR/Attributes.h"
70 #include "llvm/IR/BasicBlock.h"
71 #include "llvm/IR/CFG.h"
72 #include "llvm/IR/CallSite.h"
73 #include "llvm/IR/Comdat.h"
74 #include "llvm/IR/Constant.h"
75 #include "llvm/IR/Constants.h"
76 #include "llvm/IR/DiagnosticInfo.h"
77 #include "llvm/IR/Dominators.h"
78 #include "llvm/IR/Function.h"
79 #include "llvm/IR/GlobalAlias.h"
80 #include "llvm/IR/GlobalValue.h"
81 #include "llvm/IR/GlobalVariable.h"
82 #include "llvm/IR/IRBuilder.h"
83 #include "llvm/IR/InstVisitor.h"
84 #include "llvm/IR/InstrTypes.h"
85 #include "llvm/IR/Instruction.h"
86 #include "llvm/IR/Instructions.h"
87 #include "llvm/IR/IntrinsicInst.h"
88 #include "llvm/IR/Intrinsics.h"
89 #include "llvm/IR/LLVMContext.h"
90 #include "llvm/IR/MDBuilder.h"
91 #include "llvm/IR/Module.h"
92 #include "llvm/IR/PassManager.h"
93 #include "llvm/IR/ProfileSummary.h"
94 #include "llvm/IR/Type.h"
95 #include "llvm/IR/Value.h"
96 #include "llvm/Pass.h"
97 #include "llvm/ProfileData/InstrProf.h"
98 #include "llvm/ProfileData/InstrProfReader.h"
99 #include "llvm/Support/BranchProbability.h"
100 #include "llvm/Support/Casting.h"
101 #include "llvm/Support/CommandLine.h"
102 #include "llvm/Support/DOTGraphTraits.h"
103 #include "llvm/Support/Debug.h"
104 #include "llvm/Support/Error.h"
105 #include "llvm/Support/ErrorHandling.h"
106 #include "llvm/Support/GraphWriter.h"
107 #include "llvm/Support/JamCRC.h"
108 #include "llvm/Support/raw_ostream.h"
109 #include "llvm/Transforms/Instrumentation.h"
110 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
117 #include <unordered_map>
121 using namespace llvm;
122 using ProfileCount = Function::ProfileCount;
124 #define DEBUG_TYPE "pgo-instrumentation"
126 STATISTIC(NumOfPGOInstrument, "Number of edges instrumented.");
127 STATISTIC(NumOfPGOSelectInsts, "Number of select instruction instrumented.");
128 STATISTIC(NumOfPGOMemIntrinsics, "Number of mem intrinsics instrumented.");
129 STATISTIC(NumOfPGOEdge, "Number of edges.");
130 STATISTIC(NumOfPGOBB, "Number of basic-blocks.");
131 STATISTIC(NumOfPGOSplit, "Number of critical edge splits.");
132 STATISTIC(NumOfPGOFunc, "Number of functions having valid profile counts.");
133 STATISTIC(NumOfPGOMismatch, "Number of functions having mismatch profile.");
134 STATISTIC(NumOfPGOMissing, "Number of functions without profile.");
135 STATISTIC(NumOfPGOICall, "Number of indirect call value instrumentations.");
137 // Command line option to specify the file to read profile from. This is
138 // mainly used for testing.
139 static cl::opt<std::string>
140 PGOTestProfileFile("pgo-test-profile-file", cl::init(""), cl::Hidden,
141 cl::value_desc("filename"),
142 cl::desc("Specify the path of profile data file. This is"
143 "mainly for test purpose."));
144 static cl::opt<std::string> PGOTestProfileRemappingFile(
145 "pgo-test-profile-remapping-file", cl::init(""), cl::Hidden,
146 cl::value_desc("filename"),
147 cl::desc("Specify the path of profile remapping file. This is mainly for "
150 // Command line option to disable value profiling. The default is false:
151 // i.e. value profiling is enabled by default. This is for debug purpose.
152 static cl::opt<bool> DisableValueProfiling("disable-vp", cl::init(false),
154 cl::desc("Disable Value Profiling"));
156 // Command line option to set the maximum number of VP annotations to write to
157 // the metadata for a single indirect call callsite.
158 static cl::opt<unsigned> MaxNumAnnotations(
159 "icp-max-annotations", cl::init(3), cl::Hidden, cl::ZeroOrMore,
160 cl::desc("Max number of annotations for a single indirect "
163 // Command line option to set the maximum number of value annotations
164 // to write to the metadata for a single memop intrinsic.
165 static cl::opt<unsigned> MaxNumMemOPAnnotations(
166 "memop-max-annotations", cl::init(4), cl::Hidden, cl::ZeroOrMore,
167 cl::desc("Max number of preicise value annotations for a single memop"
170 // Command line option to control appending FunctionHash to the name of a COMDAT
171 // function. This is to avoid the hash mismatch caused by the preinliner.
172 static cl::opt<bool> DoComdatRenaming(
173 "do-comdat-renaming", cl::init(false), cl::Hidden,
174 cl::desc("Append function hash to the name of COMDAT function to avoid "
175 "function hash mismatch due to the preinliner"));
177 // Command line option to enable/disable the warning about missing profile
180 PGOWarnMissing("pgo-warn-missing-function", cl::init(false), cl::Hidden,
181 cl::desc("Use this option to turn on/off "
182 "warnings about missing profile data for "
185 // Command line option to enable/disable the warning about a hash mismatch in
188 NoPGOWarnMismatch("no-pgo-warn-mismatch", cl::init(false), cl::Hidden,
189 cl::desc("Use this option to turn off/on "
190 "warnings about profile cfg mismatch."));
192 // Command line option to enable/disable the warning about a hash mismatch in
193 // the profile data for Comdat functions, which often turns out to be false
194 // positive due to the pre-instrumentation inline.
196 NoPGOWarnMismatchComdat("no-pgo-warn-mismatch-comdat", cl::init(true),
198 cl::desc("The option is used to turn on/off "
199 "warnings about hash mismatch for comdat "
202 // Command line option to enable/disable select instruction instrumentation.
204 PGOInstrSelect("pgo-instr-select", cl::init(true), cl::Hidden,
205 cl::desc("Use this option to turn on/off SELECT "
206 "instruction instrumentation. "));
208 // Command line option to turn on CFG dot or text dump of raw profile counts
209 static cl::opt<PGOViewCountsType> PGOViewRawCounts(
210 "pgo-view-raw-counts", cl::Hidden,
211 cl::desc("A boolean option to show CFG dag or text "
212 "with raw profile counts from "
213 "profile data. See also option "
214 "-pgo-view-counts. To limit graph "
215 "display to only one function, use "
216 "filtering option -view-bfi-func-name."),
217 cl::values(clEnumValN(PGOVCT_None, "none", "do not show."),
218 clEnumValN(PGOVCT_Graph, "graph", "show a graph."),
219 clEnumValN(PGOVCT_Text, "text", "show in text.")));
221 // Command line option to enable/disable memop intrinsic call.size profiling.
223 PGOInstrMemOP("pgo-instr-memop", cl::init(true), cl::Hidden,
224 cl::desc("Use this option to turn on/off "
225 "memory intrinsic size profiling."));
227 // Emit branch probability as optimization remarks.
229 EmitBranchProbability("pgo-emit-branch-prob", cl::init(false), cl::Hidden,
230 cl::desc("When this option is on, the annotated "
231 "branch probability will be emitted as "
232 "optimization remarks: -{Rpass|"
233 "pass-remarks}=pgo-instrumentation"));
235 // Command line option to turn on CFG dot dump after profile annotation.
236 // Defined in Analysis/BlockFrequencyInfo.cpp: -pgo-view-counts
237 extern cl::opt<PGOViewCountsType> PGOViewCounts;
239 // Command line option to specify the name of the function for CFG dump
240 // Defined in Analysis/BlockFrequencyInfo.cpp: -view-bfi-func-name=
241 extern cl::opt<std::string> ViewBlockFreqFuncName;
243 // Return a string describing the branch condition that can be
244 // used in static branch probability heuristics:
245 static std::string getBranchCondString(Instruction *TI) {
246 BranchInst *BI = dyn_cast<BranchInst>(TI);
247 if (!BI || !BI->isConditional())
248 return std::string();
250 Value *Cond = BI->getCondition();
251 ICmpInst *CI = dyn_cast<ICmpInst>(Cond);
253 return std::string();
256 raw_string_ostream OS(result);
257 OS << CmpInst::getPredicateName(CI->getPredicate()) << "_";
258 CI->getOperand(0)->getType()->print(OS, true);
260 Value *RHS = CI->getOperand(1);
261 ConstantInt *CV = dyn_cast<ConstantInt>(RHS);
265 else if (CV->isOne())
267 else if (CV->isMinusOne())
278 /// The select instruction visitor plays three roles specified
279 /// by the mode. In \c VM_counting mode, it simply counts the number of
280 /// select instructions. In \c VM_instrument mode, it inserts code to count
281 /// the number times TrueValue of select is taken. In \c VM_annotate mode,
282 /// it reads the profile data and annotate the select instruction with metadata.
283 enum VisitMode { VM_counting, VM_instrument, VM_annotate };
286 /// Instruction Visitor class to visit select instructions.
287 struct SelectInstVisitor : public InstVisitor<SelectInstVisitor> {
289 unsigned NSIs = 0; // Number of select instructions instrumented.
290 VisitMode Mode = VM_counting; // Visiting mode.
291 unsigned *CurCtrIdx = nullptr; // Pointer to current counter index.
292 unsigned TotalNumCtrs = 0; // Total number of counters
293 GlobalVariable *FuncNameVar = nullptr;
294 uint64_t FuncHash = 0;
295 PGOUseFunc *UseFunc = nullptr;
297 SelectInstVisitor(Function &Func) : F(Func) {}
299 void countSelects(Function &Func) {
305 // Visit the IR stream and instrument all select instructions. \p
306 // Ind is a pointer to the counter index variable; \p TotalNC
307 // is the total number of counters; \p FNV is the pointer to the
308 // PGO function name var; \p FHash is the function hash.
309 void instrumentSelects(Function &Func, unsigned *Ind, unsigned TotalNC,
310 GlobalVariable *FNV, uint64_t FHash) {
311 Mode = VM_instrument;
313 TotalNumCtrs = TotalNC;
319 // Visit the IR stream and annotate all select instructions.
320 void annotateSelects(Function &Func, PGOUseFunc *UF, unsigned *Ind) {
327 void instrumentOneSelectInst(SelectInst &SI);
328 void annotateOneSelectInst(SelectInst &SI);
330 // Visit \p SI instruction and perform tasks according to visit mode.
331 void visitSelectInst(SelectInst &SI);
333 // Return the number of select instructions. This needs be called after
335 unsigned getNumOfSelectInsts() const { return NSIs; }
338 /// Instruction Visitor class to visit memory intrinsic calls.
339 struct MemIntrinsicVisitor : public InstVisitor<MemIntrinsicVisitor> {
341 unsigned NMemIs = 0; // Number of memIntrinsics instrumented.
342 VisitMode Mode = VM_counting; // Visiting mode.
343 unsigned CurCtrId = 0; // Current counter index.
344 unsigned TotalNumCtrs = 0; // Total number of counters
345 GlobalVariable *FuncNameVar = nullptr;
346 uint64_t FuncHash = 0;
347 PGOUseFunc *UseFunc = nullptr;
348 std::vector<Instruction *> Candidates;
350 MemIntrinsicVisitor(Function &Func) : F(Func) {}
352 void countMemIntrinsics(Function &Func) {
358 void instrumentMemIntrinsics(Function &Func, unsigned TotalNC,
359 GlobalVariable *FNV, uint64_t FHash) {
360 Mode = VM_instrument;
361 TotalNumCtrs = TotalNC;
367 std::vector<Instruction *> findMemIntrinsics(Function &Func) {
374 // Visit the IR stream and annotate all mem intrinsic call instructions.
375 void instrumentOneMemIntrinsic(MemIntrinsic &MI);
377 // Visit \p MI instruction and perform tasks according to visit mode.
378 void visitMemIntrinsic(MemIntrinsic &SI);
380 unsigned getNumOfMemIntrinsics() const { return NMemIs; }
383 class PGOInstrumentationGenLegacyPass : public ModulePass {
387 PGOInstrumentationGenLegacyPass() : ModulePass(ID) {
388 initializePGOInstrumentationGenLegacyPassPass(
389 *PassRegistry::getPassRegistry());
392 StringRef getPassName() const override { return "PGOInstrumentationGenPass"; }
395 bool runOnModule(Module &M) override;
397 void getAnalysisUsage(AnalysisUsage &AU) const override {
398 AU.addRequired<BlockFrequencyInfoWrapperPass>();
402 class PGOInstrumentationUseLegacyPass : public ModulePass {
406 // Provide the profile filename as the parameter.
407 PGOInstrumentationUseLegacyPass(std::string Filename = "")
408 : ModulePass(ID), ProfileFileName(std::move(Filename)) {
409 if (!PGOTestProfileFile.empty())
410 ProfileFileName = PGOTestProfileFile;
411 initializePGOInstrumentationUseLegacyPassPass(
412 *PassRegistry::getPassRegistry());
415 StringRef getPassName() const override { return "PGOInstrumentationUsePass"; }
418 std::string ProfileFileName;
420 bool runOnModule(Module &M) override;
422 void getAnalysisUsage(AnalysisUsage &AU) const override {
423 AU.addRequired<BlockFrequencyInfoWrapperPass>();
427 } // end anonymous namespace
429 char PGOInstrumentationGenLegacyPass::ID = 0;
431 INITIALIZE_PASS_BEGIN(PGOInstrumentationGenLegacyPass, "pgo-instr-gen",
432 "PGO instrumentation.", false, false)
433 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
434 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
435 INITIALIZE_PASS_END(PGOInstrumentationGenLegacyPass, "pgo-instr-gen",
436 "PGO instrumentation.", false, false)
438 ModulePass *llvm::createPGOInstrumentationGenLegacyPass() {
439 return new PGOInstrumentationGenLegacyPass();
442 char PGOInstrumentationUseLegacyPass::ID = 0;
444 INITIALIZE_PASS_BEGIN(PGOInstrumentationUseLegacyPass, "pgo-instr-use",
445 "Read PGO instrumentation profile.", false, false)
446 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
447 INITIALIZE_PASS_DEPENDENCY(BranchProbabilityInfoWrapperPass)
448 INITIALIZE_PASS_END(PGOInstrumentationUseLegacyPass, "pgo-instr-use",
449 "Read PGO instrumentation profile.", false, false)
451 ModulePass *llvm::createPGOInstrumentationUseLegacyPass(StringRef Filename) {
452 return new PGOInstrumentationUseLegacyPass(Filename.str());
457 /// An MST based instrumentation for PGO
459 /// Implements a Minimum Spanning Tree (MST) based instrumentation for PGO
460 /// in the function level.
462 // This class implements the CFG edges. Note the CFG can be a multi-graph.
463 // So there might be multiple edges with same SrcBB and DestBB.
464 const BasicBlock *SrcBB;
465 const BasicBlock *DestBB;
468 bool Removed = false;
469 bool IsCritical = false;
471 PGOEdge(const BasicBlock *Src, const BasicBlock *Dest, uint64_t W = 1)
472 : SrcBB(Src), DestBB(Dest), Weight(W) {}
474 // Return the information string of an edge.
475 const std::string infoString() const {
476 return (Twine(Removed ? "-" : " ") + (InMST ? " " : "*") +
477 (IsCritical ? "c" : " ") + " W=" + Twine(Weight)).str();
481 // This class stores the auxiliary information for each BB.
487 BBInfo(unsigned IX) : Group(this), Index(IX) {}
489 // Return the information string of this object.
490 const std::string infoString() const {
491 return (Twine("Index=") + Twine(Index)).str();
495 // This class implements the CFG edges. Note the CFG can be a multi-graph.
496 template <class Edge, class BBInfo> class FuncPGOInstrumentation {
500 // A map that stores the Comdat group in function F.
501 std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers;
503 void computeCFGHash();
504 void renameComdatFunction();
507 std::vector<std::vector<Instruction *>> ValueSites;
508 SelectInstVisitor SIVisitor;
509 MemIntrinsicVisitor MIVisitor;
510 std::string FuncName;
511 GlobalVariable *FuncNameVar;
513 // CFG hash value for this function.
514 uint64_t FunctionHash = 0;
516 // The Minimum Spanning Tree of function CFG.
517 CFGMST<Edge, BBInfo> MST;
519 // Give an edge, find the BB that will be instrumented.
520 // Return nullptr if there is no BB to be instrumented.
521 BasicBlock *getInstrBB(Edge *E);
523 // Return the auxiliary BB information.
524 BBInfo &getBBInfo(const BasicBlock *BB) const { return MST.getBBInfo(BB); }
526 // Return the auxiliary BB information if available.
527 BBInfo *findBBInfo(const BasicBlock *BB) const { return MST.findBBInfo(BB); }
529 // Dump edges and BB information.
530 void dumpInfo(std::string Str = "") const {
531 MST.dumpEdges(dbgs(), Twine("Dump Function ") + FuncName + " Hash: " +
532 Twine(FunctionHash) + "\t" + Str);
535 FuncPGOInstrumentation(
537 std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers,
538 bool CreateGlobalVar = false, BranchProbabilityInfo *BPI = nullptr,
539 BlockFrequencyInfo *BFI = nullptr)
540 : F(Func), ComdatMembers(ComdatMembers), ValueSites(IPVK_Last + 1),
541 SIVisitor(Func), MIVisitor(Func), MST(F, BPI, BFI) {
542 // This should be done before CFG hash computation.
543 SIVisitor.countSelects(Func);
544 MIVisitor.countMemIntrinsics(Func);
545 NumOfPGOSelectInsts += SIVisitor.getNumOfSelectInsts();
546 NumOfPGOMemIntrinsics += MIVisitor.getNumOfMemIntrinsics();
547 ValueSites[IPVK_IndirectCallTarget] = findIndirectCalls(Func);
548 ValueSites[IPVK_MemOPSize] = MIVisitor.findMemIntrinsics(Func);
550 FuncName = getPGOFuncName(F);
552 if (!ComdatMembers.empty())
553 renameComdatFunction();
554 LLVM_DEBUG(dumpInfo("after CFGMST"));
556 NumOfPGOBB += MST.BBInfos.size();
557 for (auto &E : MST.AllEdges) {
562 NumOfPGOInstrument++;
566 FuncNameVar = createPGOFuncNameVar(F, FuncName);
569 // Return the number of profile counters needed for the function.
570 unsigned getNumCounters() {
571 unsigned NumCounters = 0;
572 for (auto &E : this->MST.AllEdges) {
573 if (!E->InMST && !E->Removed)
576 return NumCounters + SIVisitor.getNumOfSelectInsts();
580 } // end anonymous namespace
582 // Compute Hash value for the CFG: the lower 32 bits are CRC32 of the index
583 // value of each BB in the CFG. The higher 32 bits record the number of edges.
584 template <class Edge, class BBInfo>
585 void FuncPGOInstrumentation<Edge, BBInfo>::computeCFGHash() {
586 std::vector<char> Indexes;
589 const Instruction *TI = BB.getTerminator();
590 for (unsigned I = 0, E = TI->getNumSuccessors(); I != E; ++I) {
591 BasicBlock *Succ = TI->getSuccessor(I);
592 auto BI = findBBInfo(Succ);
595 uint32_t Index = BI->Index;
596 for (int J = 0; J < 4; J++)
597 Indexes.push_back((char)(Index >> (J * 8)));
601 FunctionHash = (uint64_t)SIVisitor.getNumOfSelectInsts() << 56 |
602 (uint64_t)ValueSites[IPVK_IndirectCallTarget].size() << 48 |
603 (uint64_t)MST.AllEdges.size() << 32 | JC.getCRC();
604 LLVM_DEBUG(dbgs() << "Function Hash Computation for " << F.getName() << ":\n"
605 << " CRC = " << JC.getCRC()
606 << ", Selects = " << SIVisitor.getNumOfSelectInsts()
607 << ", Edges = " << MST.AllEdges.size() << ", ICSites = "
608 << ValueSites[IPVK_IndirectCallTarget].size()
609 << ", Hash = " << FunctionHash << "\n";);
612 // Check if we can safely rename this Comdat function.
613 static bool canRenameComdat(
615 std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers) {
616 if (!DoComdatRenaming || !canRenameComdatFunc(F, true))
619 // FIXME: Current only handle those Comdat groups that only containing one
620 // function and function aliases.
621 // (1) For a Comdat group containing multiple functions, we need to have a
622 // unique postfix based on the hashes for each function. There is a
623 // non-trivial code refactoring to do this efficiently.
624 // (2) Variables can not be renamed, so we can not rename Comdat function in a
625 // group including global vars.
626 Comdat *C = F.getComdat();
627 for (auto &&CM : make_range(ComdatMembers.equal_range(C))) {
628 if (dyn_cast<GlobalAlias>(CM.second))
630 Function *FM = dyn_cast<Function>(CM.second);
637 // Append the CFGHash to the Comdat function name.
638 template <class Edge, class BBInfo>
639 void FuncPGOInstrumentation<Edge, BBInfo>::renameComdatFunction() {
640 if (!canRenameComdat(F, ComdatMembers))
642 std::string OrigName = F.getName().str();
643 std::string NewFuncName =
644 Twine(F.getName() + "." + Twine(FunctionHash)).str();
645 F.setName(Twine(NewFuncName));
646 GlobalAlias::create(GlobalValue::WeakAnyLinkage, OrigName, &F);
647 FuncName = Twine(FuncName + "." + Twine(FunctionHash)).str();
649 Module *M = F.getParent();
650 // For AvailableExternallyLinkage functions, change the linkage to
651 // LinkOnceODR and put them into comdat. This is because after renaming, there
652 // is no backup external copy available for the function.
653 if (!F.hasComdat()) {
654 assert(F.getLinkage() == GlobalValue::AvailableExternallyLinkage);
655 NewComdat = M->getOrInsertComdat(StringRef(NewFuncName));
656 F.setLinkage(GlobalValue::LinkOnceODRLinkage);
657 F.setComdat(NewComdat);
661 // This function belongs to a single function Comdat group.
662 Comdat *OrigComdat = F.getComdat();
663 std::string NewComdatName =
664 Twine(OrigComdat->getName() + "." + Twine(FunctionHash)).str();
665 NewComdat = M->getOrInsertComdat(StringRef(NewComdatName));
666 NewComdat->setSelectionKind(OrigComdat->getSelectionKind());
668 for (auto &&CM : make_range(ComdatMembers.equal_range(OrigComdat))) {
669 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(CM.second)) {
670 // For aliases, change the name directly.
671 assert(dyn_cast<Function>(GA->getAliasee()->stripPointerCasts()) == &F);
672 std::string OrigGAName = GA->getName().str();
673 GA->setName(Twine(GA->getName() + "." + Twine(FunctionHash)));
674 GlobalAlias::create(GlobalValue::WeakAnyLinkage, OrigGAName, GA);
677 // Must be a function.
678 Function *CF = dyn_cast<Function>(CM.second);
680 CF->setComdat(NewComdat);
684 // Given a CFG E to be instrumented, find which BB to place the instrumented
685 // code. The function will split the critical edge if necessary.
686 template <class Edge, class BBInfo>
687 BasicBlock *FuncPGOInstrumentation<Edge, BBInfo>::getInstrBB(Edge *E) {
688 if (E->InMST || E->Removed)
691 BasicBlock *SrcBB = const_cast<BasicBlock *>(E->SrcBB);
692 BasicBlock *DestBB = const_cast<BasicBlock *>(E->DestBB);
693 // For a fake edge, instrument the real BB.
694 if (SrcBB == nullptr)
696 if (DestBB == nullptr)
699 // Instrument the SrcBB if it has a single successor,
700 // otherwise, the DestBB if this is not a critical edge.
701 Instruction *TI = SrcBB->getTerminator();
702 if (TI->getNumSuccessors() <= 1)
707 // For a critical edge, we have to split. Instrument the newly
710 LLVM_DEBUG(dbgs() << "Split critical edge: " << getBBInfo(SrcBB).Index
711 << " --> " << getBBInfo(DestBB).Index << "\n");
712 unsigned SuccNum = GetSuccessorNumber(SrcBB, DestBB);
713 BasicBlock *InstrBB = SplitCriticalEdge(TI, SuccNum);
714 assert(InstrBB && "Critical edge is not split");
720 // Visit all edge and instrument the edges not in MST, and do value profiling.
721 // Critical edges will be split.
722 static void instrumentOneFunc(
723 Function &F, Module *M, BranchProbabilityInfo *BPI, BlockFrequencyInfo *BFI,
724 std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers) {
725 // Split indirectbr critical edges here before computing the MST rather than
726 // later in getInstrBB() to avoid invalidating it.
727 SplitIndirectBrCriticalEdges(F, BPI, BFI);
728 FuncPGOInstrumentation<PGOEdge, BBInfo> FuncInfo(F, ComdatMembers, true, BPI,
730 unsigned NumCounters = FuncInfo.getNumCounters();
733 Type *I8PtrTy = Type::getInt8PtrTy(M->getContext());
734 for (auto &E : FuncInfo.MST.AllEdges) {
735 BasicBlock *InstrBB = FuncInfo.getInstrBB(E.get());
739 IRBuilder<> Builder(InstrBB, InstrBB->getFirstInsertionPt());
740 assert(Builder.GetInsertPoint() != InstrBB->end() &&
741 "Cannot get the Instrumentation point");
743 Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment),
744 {ConstantExpr::getBitCast(FuncInfo.FuncNameVar, I8PtrTy),
745 Builder.getInt64(FuncInfo.FunctionHash), Builder.getInt32(NumCounters),
746 Builder.getInt32(I++)});
749 // Now instrument select instructions:
750 FuncInfo.SIVisitor.instrumentSelects(F, &I, NumCounters, FuncInfo.FuncNameVar,
751 FuncInfo.FunctionHash);
752 assert(I == NumCounters);
754 if (DisableValueProfiling)
757 unsigned NumIndirectCalls = 0;
758 for (auto &I : FuncInfo.ValueSites[IPVK_IndirectCallTarget]) {
760 Value *Callee = CS.getCalledValue();
761 LLVM_DEBUG(dbgs() << "Instrument one indirect call: CallSite Index = "
762 << NumIndirectCalls << "\n");
763 IRBuilder<> Builder(I);
764 assert(Builder.GetInsertPoint() != I->getParent()->end() &&
765 "Cannot get the Instrumentation point");
767 Intrinsic::getDeclaration(M, Intrinsic::instrprof_value_profile),
768 {ConstantExpr::getBitCast(FuncInfo.FuncNameVar, I8PtrTy),
769 Builder.getInt64(FuncInfo.FunctionHash),
770 Builder.CreatePtrToInt(Callee, Builder.getInt64Ty()),
771 Builder.getInt32(IPVK_IndirectCallTarget),
772 Builder.getInt32(NumIndirectCalls++)});
774 NumOfPGOICall += NumIndirectCalls;
776 // Now instrument memop intrinsic calls.
777 FuncInfo.MIVisitor.instrumentMemIntrinsics(
778 F, NumCounters, FuncInfo.FuncNameVar, FuncInfo.FunctionHash);
783 // This class represents a CFG edge in profile use compilation.
784 struct PGOUseEdge : public PGOEdge {
785 bool CountValid = false;
786 uint64_t CountValue = 0;
788 PGOUseEdge(const BasicBlock *Src, const BasicBlock *Dest, uint64_t W = 1)
789 : PGOEdge(Src, Dest, W) {}
791 // Set edge count value
792 void setEdgeCount(uint64_t Value) {
797 // Return the information string for this object.
798 const std::string infoString() const {
800 return PGOEdge::infoString();
801 return (Twine(PGOEdge::infoString()) + " Count=" + Twine(CountValue))
806 using DirectEdges = SmallVector<PGOUseEdge *, 2>;
808 // This class stores the auxiliary information for each BB.
809 struct UseBBInfo : public BBInfo {
810 uint64_t CountValue = 0;
812 int32_t UnknownCountInEdge = 0;
813 int32_t UnknownCountOutEdge = 0;
815 DirectEdges OutEdges;
817 UseBBInfo(unsigned IX) : BBInfo(IX), CountValid(false) {}
819 UseBBInfo(unsigned IX, uint64_t C)
820 : BBInfo(IX), CountValue(C), CountValid(true) {}
822 // Set the profile count value for this BB.
823 void setBBInfoCount(uint64_t Value) {
828 // Return the information string of this object.
829 const std::string infoString() const {
831 return BBInfo::infoString();
832 return (Twine(BBInfo::infoString()) + " Count=" + Twine(CountValue)).str();
836 } // end anonymous namespace
838 // Sum up the count values for all the edges.
839 static uint64_t sumEdgeCount(const ArrayRef<PGOUseEdge *> Edges) {
841 for (auto &E : Edges) {
844 Total += E->CountValue;
853 PGOUseFunc(Function &Func, Module *Modu,
854 std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers,
855 BranchProbabilityInfo *BPI = nullptr,
856 BlockFrequencyInfo *BFIin = nullptr)
857 : F(Func), M(Modu), BFI(BFIin),
858 FuncInfo(Func, ComdatMembers, false, BPI, BFIin),
859 FreqAttr(FFA_Normal) {}
861 // Read counts for the instrumented BB from profile.
862 bool readCounters(IndexedInstrProfReader *PGOReader, bool &AllZeros);
864 // Populate the counts for all BBs.
865 void populateCounters();
867 // Set the branch weights based on the count values.
868 void setBranchWeights();
870 // Annotate the value profile call sites for all value kind.
871 void annotateValueSites();
873 // Annotate the value profile call sites for one value kind.
874 void annotateValueSites(uint32_t Kind);
876 // Annotate the irreducible loop header weights.
877 void annotateIrrLoopHeaderWeights();
879 // The hotness of the function from the profile count.
880 enum FuncFreqAttr { FFA_Normal, FFA_Cold, FFA_Hot };
882 // Return the function hotness from the profile.
883 FuncFreqAttr getFuncFreqAttr() const { return FreqAttr; }
885 // Return the function hash.
886 uint64_t getFuncHash() const { return FuncInfo.FunctionHash; }
888 // Return the profile record for this function;
889 InstrProfRecord &getProfileRecord() { return ProfileRecord; }
891 // Return the auxiliary BB information.
892 UseBBInfo &getBBInfo(const BasicBlock *BB) const {
893 return FuncInfo.getBBInfo(BB);
896 // Return the auxiliary BB information if available.
897 UseBBInfo *findBBInfo(const BasicBlock *BB) const {
898 return FuncInfo.findBBInfo(BB);
901 Function &getFunc() const { return F; }
903 void dumpInfo(std::string Str = "") const {
904 FuncInfo.dumpInfo(Str);
907 uint64_t getProgramMaxCount() const { return ProgramMaxCount; }
911 BlockFrequencyInfo *BFI;
913 // This member stores the shared information with class PGOGenFunc.
914 FuncPGOInstrumentation<PGOUseEdge, UseBBInfo> FuncInfo;
916 // The maximum count value in the profile. This is only used in PGO use
918 uint64_t ProgramMaxCount;
920 // Position of counter that remains to be read.
921 uint32_t CountPosition = 0;
923 // Total size of the profile count for this function.
924 uint32_t ProfileCountSize = 0;
926 // ProfileRecord for this function.
927 InstrProfRecord ProfileRecord;
929 // Function hotness info derived from profile.
930 FuncFreqAttr FreqAttr;
932 // Find the Instrumented BB and set the value.
933 void setInstrumentedCounts(const std::vector<uint64_t> &CountFromProfile);
935 // Set the edge counter value for the unknown edge -- there should be only
937 void setEdgeCount(DirectEdges &Edges, uint64_t Value);
939 // Return FuncName string;
940 const std::string getFuncName() const { return FuncInfo.FuncName; }
942 // Set the hot/cold inline hints based on the count values.
943 // FIXME: This function should be removed once the functionality in
944 // the inliner is implemented.
945 void markFunctionAttributes(uint64_t EntryCount, uint64_t MaxCount) {
946 if (ProgramMaxCount == 0)
948 // Threshold of the hot functions.
949 const BranchProbability HotFunctionThreshold(1, 100);
950 // Threshold of the cold functions.
951 const BranchProbability ColdFunctionThreshold(2, 10000);
952 if (EntryCount >= HotFunctionThreshold.scale(ProgramMaxCount))
954 else if (MaxCount <= ColdFunctionThreshold.scale(ProgramMaxCount))
959 } // end anonymous namespace
961 // Visit all the edges and assign the count value for the instrumented
963 void PGOUseFunc::setInstrumentedCounts(
964 const std::vector<uint64_t> &CountFromProfile) {
965 assert(FuncInfo.getNumCounters() == CountFromProfile.size());
966 // Use a worklist as we will update the vector during the iteration.
967 std::vector<PGOUseEdge *> WorkList;
968 for (auto &E : FuncInfo.MST.AllEdges)
969 WorkList.push_back(E.get());
972 for (auto &E : WorkList) {
973 BasicBlock *InstrBB = FuncInfo.getInstrBB(E);
976 uint64_t CountValue = CountFromProfile[I++];
978 getBBInfo(InstrBB).setBBInfoCount(CountValue);
979 E->setEdgeCount(CountValue);
983 // Need to add two new edges.
984 BasicBlock *SrcBB = const_cast<BasicBlock *>(E->SrcBB);
985 BasicBlock *DestBB = const_cast<BasicBlock *>(E->DestBB);
986 // Add new edge of SrcBB->InstrBB.
987 PGOUseEdge &NewEdge = FuncInfo.MST.addEdge(SrcBB, InstrBB, 0);
988 NewEdge.setEdgeCount(CountValue);
989 // Add new edge of InstrBB->DestBB.
990 PGOUseEdge &NewEdge1 = FuncInfo.MST.addEdge(InstrBB, DestBB, 0);
991 NewEdge1.setEdgeCount(CountValue);
992 NewEdge1.InMST = true;
993 getBBInfo(InstrBB).setBBInfoCount(CountValue);
995 ProfileCountSize = CountFromProfile.size();
999 // Set the count value for the unknown edge. There should be one and only one
1000 // unknown edge in Edges vector.
1001 void PGOUseFunc::setEdgeCount(DirectEdges &Edges, uint64_t Value) {
1002 for (auto &E : Edges) {
1005 E->setEdgeCount(Value);
1007 getBBInfo(E->SrcBB).UnknownCountOutEdge--;
1008 getBBInfo(E->DestBB).UnknownCountInEdge--;
1011 llvm_unreachable("Cannot find the unknown count edge");
1014 // Read the profile from ProfileFileName and assign the value to the
1015 // instrumented BB and the edges. This function also updates ProgramMaxCount.
1016 // Return true if the profile are successfully read, and false on errors.
1017 bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader, bool &AllZeros) {
1018 auto &Ctx = M->getContext();
1019 Expected<InstrProfRecord> Result =
1020 PGOReader->getInstrProfRecord(FuncInfo.FuncName, FuncInfo.FunctionHash);
1021 if (Error E = Result.takeError()) {
1022 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
1023 auto Err = IPE.get();
1024 bool SkipWarning = false;
1025 if (Err == instrprof_error::unknown_function) {
1027 SkipWarning = !PGOWarnMissing;
1028 } else if (Err == instrprof_error::hash_mismatch ||
1029 Err == instrprof_error::malformed) {
1032 NoPGOWarnMismatch ||
1033 (NoPGOWarnMismatchComdat &&
1035 F.getLinkage() == GlobalValue::AvailableExternallyLinkage));
1041 std::string Msg = IPE.message() + std::string(" ") + F.getName().str();
1043 DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning));
1047 ProfileRecord = std::move(Result.get());
1048 std::vector<uint64_t> &CountFromProfile = ProfileRecord.Counts;
1051 LLVM_DEBUG(dbgs() << CountFromProfile.size() << " counts\n");
1052 uint64_t ValueSum = 0;
1053 for (unsigned I = 0, S = CountFromProfile.size(); I < S; I++) {
1054 LLVM_DEBUG(dbgs() << " " << I << ": " << CountFromProfile[I] << "\n");
1055 ValueSum += CountFromProfile[I];
1057 AllZeros = (ValueSum == 0);
1059 LLVM_DEBUG(dbgs() << "SUM = " << ValueSum << "\n");
1061 getBBInfo(nullptr).UnknownCountOutEdge = 2;
1062 getBBInfo(nullptr).UnknownCountInEdge = 2;
1064 setInstrumentedCounts(CountFromProfile);
1065 ProgramMaxCount = PGOReader->getMaximumFunctionCount();
1069 // Populate the counters from instrumented BBs to all BBs.
1070 // In the end of this operation, all BBs should have a valid count value.
1071 void PGOUseFunc::populateCounters() {
1072 // First set up Count variable for all BBs.
1073 for (auto &E : FuncInfo.MST.AllEdges) {
1077 const BasicBlock *SrcBB = E->SrcBB;
1078 const BasicBlock *DestBB = E->DestBB;
1079 UseBBInfo &SrcInfo = getBBInfo(SrcBB);
1080 UseBBInfo &DestInfo = getBBInfo(DestBB);
1081 SrcInfo.OutEdges.push_back(E.get());
1082 DestInfo.InEdges.push_back(E.get());
1083 SrcInfo.UnknownCountOutEdge++;
1084 DestInfo.UnknownCountInEdge++;
1088 DestInfo.UnknownCountInEdge--;
1089 SrcInfo.UnknownCountOutEdge--;
1092 bool Changes = true;
1093 unsigned NumPasses = 0;
1098 // For efficient traversal, it's better to start from the end as most
1099 // of the instrumented edges are at the end.
1100 for (auto &BB : reverse(F)) {
1101 UseBBInfo *Count = findBBInfo(&BB);
1102 if (Count == nullptr)
1104 if (!Count->CountValid) {
1105 if (Count->UnknownCountOutEdge == 0) {
1106 Count->CountValue = sumEdgeCount(Count->OutEdges);
1107 Count->CountValid = true;
1109 } else if (Count->UnknownCountInEdge == 0) {
1110 Count->CountValue = sumEdgeCount(Count->InEdges);
1111 Count->CountValid = true;
1115 if (Count->CountValid) {
1116 if (Count->UnknownCountOutEdge == 1) {
1118 uint64_t OutSum = sumEdgeCount(Count->OutEdges);
1119 // If the one of the successor block can early terminate (no-return),
1120 // we can end up with situation where out edge sum count is larger as
1121 // the source BB's count is collected by a post-dominated block.
1122 if (Count->CountValue > OutSum)
1123 Total = Count->CountValue - OutSum;
1124 setEdgeCount(Count->OutEdges, Total);
1127 if (Count->UnknownCountInEdge == 1) {
1129 uint64_t InSum = sumEdgeCount(Count->InEdges);
1130 if (Count->CountValue > InSum)
1131 Total = Count->CountValue - InSum;
1132 setEdgeCount(Count->InEdges, Total);
1139 LLVM_DEBUG(dbgs() << "Populate counts in " << NumPasses << " passes.\n");
1141 // Assert every BB has a valid counter.
1142 for (auto &BB : F) {
1143 auto BI = findBBInfo(&BB);
1146 assert(BI->CountValid && "BB count is not valid");
1149 uint64_t FuncEntryCount = getBBInfo(&*F.begin()).CountValue;
1150 F.setEntryCount(ProfileCount(FuncEntryCount, Function::PCT_Real));
1151 uint64_t FuncMaxCount = FuncEntryCount;
1152 for (auto &BB : F) {
1153 auto BI = findBBInfo(&BB);
1156 FuncMaxCount = std::max(FuncMaxCount, BI->CountValue);
1158 markFunctionAttributes(FuncEntryCount, FuncMaxCount);
1160 // Now annotate select instructions
1161 FuncInfo.SIVisitor.annotateSelects(F, this, &CountPosition);
1162 assert(CountPosition == ProfileCountSize);
1164 LLVM_DEBUG(FuncInfo.dumpInfo("after reading profile."));
1167 // Assign the scaled count values to the BB with multiple out edges.
1168 void PGOUseFunc::setBranchWeights() {
1169 // Generate MD_prof metadata for every branch instruction.
1170 LLVM_DEBUG(dbgs() << "\nSetting branch weights.\n");
1171 for (auto &BB : F) {
1172 Instruction *TI = BB.getTerminator();
1173 if (TI->getNumSuccessors() < 2)
1175 if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) ||
1176 isa<IndirectBrInst>(TI)))
1178 if (getBBInfo(&BB).CountValue == 0)
1181 // We have a non-zero Branch BB.
1182 const UseBBInfo &BBCountInfo = getBBInfo(&BB);
1183 unsigned Size = BBCountInfo.OutEdges.size();
1184 SmallVector<uint64_t, 2> EdgeCounts(Size, 0);
1185 uint64_t MaxCount = 0;
1186 for (unsigned s = 0; s < Size; s++) {
1187 const PGOUseEdge *E = BBCountInfo.OutEdges[s];
1188 const BasicBlock *SrcBB = E->SrcBB;
1189 const BasicBlock *DestBB = E->DestBB;
1190 if (DestBB == nullptr)
1192 unsigned SuccNum = GetSuccessorNumber(SrcBB, DestBB);
1193 uint64_t EdgeCount = E->CountValue;
1194 if (EdgeCount > MaxCount)
1195 MaxCount = EdgeCount;
1196 EdgeCounts[SuccNum] = EdgeCount;
1198 setProfMetadata(M, TI, EdgeCounts, MaxCount);
1202 static bool isIndirectBrTarget(BasicBlock *BB) {
1203 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
1204 if (isa<IndirectBrInst>((*PI)->getTerminator()))
1210 void PGOUseFunc::annotateIrrLoopHeaderWeights() {
1211 LLVM_DEBUG(dbgs() << "\nAnnotating irreducible loop header weights.\n");
1212 // Find irr loop headers
1213 for (auto &BB : F) {
1214 // As a heuristic also annotate indrectbr targets as they have a high chance
1215 // to become an irreducible loop header after the indirectbr tail
1217 if (BFI->isIrrLoopHeader(&BB) || isIndirectBrTarget(&BB)) {
1218 Instruction *TI = BB.getTerminator();
1219 const UseBBInfo &BBCountInfo = getBBInfo(&BB);
1220 setIrrLoopHeaderMetadata(M, TI, BBCountInfo.CountValue);
1225 void SelectInstVisitor::instrumentOneSelectInst(SelectInst &SI) {
1226 Module *M = F.getParent();
1227 IRBuilder<> Builder(&SI);
1228 Type *Int64Ty = Builder.getInt64Ty();
1229 Type *I8PtrTy = Builder.getInt8PtrTy();
1230 auto *Step = Builder.CreateZExt(SI.getCondition(), Int64Ty);
1232 Intrinsic::getDeclaration(M, Intrinsic::instrprof_increment_step),
1233 {ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
1234 Builder.getInt64(FuncHash), Builder.getInt32(TotalNumCtrs),
1235 Builder.getInt32(*CurCtrIdx), Step});
1239 void SelectInstVisitor::annotateOneSelectInst(SelectInst &SI) {
1240 std::vector<uint64_t> &CountFromProfile = UseFunc->getProfileRecord().Counts;
1241 assert(*CurCtrIdx < CountFromProfile.size() &&
1242 "Out of bound access of counters");
1243 uint64_t SCounts[2];
1244 SCounts[0] = CountFromProfile[*CurCtrIdx]; // True count
1246 uint64_t TotalCount = 0;
1247 auto BI = UseFunc->findBBInfo(SI.getParent());
1249 TotalCount = BI->CountValue;
1251 SCounts[1] = (TotalCount > SCounts[0] ? TotalCount - SCounts[0] : 0);
1252 uint64_t MaxCount = std::max(SCounts[0], SCounts[1]);
1254 setProfMetadata(F.getParent(), &SI, SCounts, MaxCount);
1257 void SelectInstVisitor::visitSelectInst(SelectInst &SI) {
1258 if (!PGOInstrSelect)
1260 // FIXME: do not handle this yet.
1261 if (SI.getCondition()->getType()->isVectorTy())
1269 instrumentOneSelectInst(SI);
1272 annotateOneSelectInst(SI);
1276 llvm_unreachable("Unknown visiting mode");
1279 void MemIntrinsicVisitor::instrumentOneMemIntrinsic(MemIntrinsic &MI) {
1280 Module *M = F.getParent();
1281 IRBuilder<> Builder(&MI);
1282 Type *Int64Ty = Builder.getInt64Ty();
1283 Type *I8PtrTy = Builder.getInt8PtrTy();
1284 Value *Length = MI.getLength();
1285 assert(!dyn_cast<ConstantInt>(Length));
1287 Intrinsic::getDeclaration(M, Intrinsic::instrprof_value_profile),
1288 {ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
1289 Builder.getInt64(FuncHash), Builder.CreateZExtOrTrunc(Length, Int64Ty),
1290 Builder.getInt32(IPVK_MemOPSize), Builder.getInt32(CurCtrId)});
1294 void MemIntrinsicVisitor::visitMemIntrinsic(MemIntrinsic &MI) {
1297 Value *Length = MI.getLength();
1298 // Not instrument constant length calls.
1299 if (dyn_cast<ConstantInt>(Length))
1307 instrumentOneMemIntrinsic(MI);
1310 Candidates.push_back(&MI);
1313 llvm_unreachable("Unknown visiting mode");
1316 // Traverse all valuesites and annotate the instructions for all value kind.
1317 void PGOUseFunc::annotateValueSites() {
1318 if (DisableValueProfiling)
1321 // Create the PGOFuncName meta data.
1322 createPGOFuncNameMetadata(F, FuncInfo.FuncName);
1324 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
1325 annotateValueSites(Kind);
1328 // Annotate the instructions for a specific value kind.
1329 void PGOUseFunc::annotateValueSites(uint32_t Kind) {
1330 unsigned ValueSiteIndex = 0;
1331 auto &ValueSites = FuncInfo.ValueSites[Kind];
1332 unsigned NumValueSites = ProfileRecord.getNumValueSites(Kind);
1333 if (NumValueSites != ValueSites.size()) {
1334 auto &Ctx = M->getContext();
1335 Ctx.diagnose(DiagnosticInfoPGOProfile(
1336 M->getName().data(),
1337 Twine("Inconsistent number of value sites for kind = ") + Twine(Kind) +
1338 " in " + F.getName().str(),
1343 for (auto &I : ValueSites) {
1344 LLVM_DEBUG(dbgs() << "Read one value site profile (kind = " << Kind
1345 << "): Index = " << ValueSiteIndex << " out of "
1346 << NumValueSites << "\n");
1347 annotateValueSite(*M, *I, ProfileRecord,
1348 static_cast<InstrProfValueKind>(Kind), ValueSiteIndex,
1349 Kind == IPVK_MemOPSize ? MaxNumMemOPAnnotations
1350 : MaxNumAnnotations);
1355 // Create a COMDAT variable INSTR_PROF_RAW_VERSION_VAR to make the runtime
1356 // aware this is an ir_level profile so it can set the version flag.
1357 static void createIRLevelProfileFlagVariable(Module &M) {
1358 Type *IntTy64 = Type::getInt64Ty(M.getContext());
1359 uint64_t ProfileVersion = (INSTR_PROF_RAW_VERSION | VARIANT_MASK_IR_PROF);
1360 auto IRLevelVersionVariable = new GlobalVariable(
1361 M, IntTy64, true, GlobalVariable::ExternalLinkage,
1362 Constant::getIntegerValue(IntTy64, APInt(64, ProfileVersion)),
1363 INSTR_PROF_QUOTE(INSTR_PROF_RAW_VERSION_VAR));
1364 IRLevelVersionVariable->setVisibility(GlobalValue::DefaultVisibility);
1365 Triple TT(M.getTargetTriple());
1366 if (!TT.supportsCOMDAT())
1367 IRLevelVersionVariable->setLinkage(GlobalValue::WeakAnyLinkage);
1369 IRLevelVersionVariable->setComdat(M.getOrInsertComdat(
1370 StringRef(INSTR_PROF_QUOTE(INSTR_PROF_RAW_VERSION_VAR))));
1373 // Collect the set of members for each Comdat in module M and store
1374 // in ComdatMembers.
1375 static void collectComdatMembers(
1377 std::unordered_multimap<Comdat *, GlobalValue *> &ComdatMembers) {
1378 if (!DoComdatRenaming)
1380 for (Function &F : M)
1381 if (Comdat *C = F.getComdat())
1382 ComdatMembers.insert(std::make_pair(C, &F));
1383 for (GlobalVariable &GV : M.globals())
1384 if (Comdat *C = GV.getComdat())
1385 ComdatMembers.insert(std::make_pair(C, &GV));
1386 for (GlobalAlias &GA : M.aliases())
1387 if (Comdat *C = GA.getComdat())
1388 ComdatMembers.insert(std::make_pair(C, &GA));
1391 static bool InstrumentAllFunctions(
1392 Module &M, function_ref<BranchProbabilityInfo *(Function &)> LookupBPI,
1393 function_ref<BlockFrequencyInfo *(Function &)> LookupBFI) {
1394 createIRLevelProfileFlagVariable(M);
1395 std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers;
1396 collectComdatMembers(M, ComdatMembers);
1399 if (F.isDeclaration())
1401 auto *BPI = LookupBPI(F);
1402 auto *BFI = LookupBFI(F);
1403 instrumentOneFunc(F, &M, BPI, BFI, ComdatMembers);
1408 bool PGOInstrumentationGenLegacyPass::runOnModule(Module &M) {
1412 auto LookupBPI = [this](Function &F) {
1413 return &this->getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI();
1415 auto LookupBFI = [this](Function &F) {
1416 return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI();
1418 return InstrumentAllFunctions(M, LookupBPI, LookupBFI);
1421 PreservedAnalyses PGOInstrumentationGen::run(Module &M,
1422 ModuleAnalysisManager &AM) {
1423 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
1424 auto LookupBPI = [&FAM](Function &F) {
1425 return &FAM.getResult<BranchProbabilityAnalysis>(F);
1428 auto LookupBFI = [&FAM](Function &F) {
1429 return &FAM.getResult<BlockFrequencyAnalysis>(F);
1432 if (!InstrumentAllFunctions(M, LookupBPI, LookupBFI))
1433 return PreservedAnalyses::all();
1435 return PreservedAnalyses::none();
1438 static bool annotateAllFunctions(
1439 Module &M, StringRef ProfileFileName, StringRef ProfileRemappingFileName,
1440 function_ref<BranchProbabilityInfo *(Function &)> LookupBPI,
1441 function_ref<BlockFrequencyInfo *(Function &)> LookupBFI) {
1442 LLVM_DEBUG(dbgs() << "Read in profile counters: ");
1443 auto &Ctx = M.getContext();
1444 // Read the counter array from file.
1446 IndexedInstrProfReader::create(ProfileFileName, ProfileRemappingFileName);
1447 if (Error E = ReaderOrErr.takeError()) {
1448 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
1450 DiagnosticInfoPGOProfile(ProfileFileName.data(), EI.message()));
1455 std::unique_ptr<IndexedInstrProfReader> PGOReader =
1456 std::move(ReaderOrErr.get());
1458 Ctx.diagnose(DiagnosticInfoPGOProfile(ProfileFileName.data(),
1459 StringRef("Cannot get PGOReader")));
1462 // TODO: might need to change the warning once the clang option is finalized.
1463 if (!PGOReader->isIRLevelProfile()) {
1464 Ctx.diagnose(DiagnosticInfoPGOProfile(
1465 ProfileFileName.data(), "Not an IR level instrumentation profile"));
1469 std::unordered_multimap<Comdat *, GlobalValue *> ComdatMembers;
1470 collectComdatMembers(M, ComdatMembers);
1471 std::vector<Function *> HotFunctions;
1472 std::vector<Function *> ColdFunctions;
1474 if (F.isDeclaration())
1476 auto *BPI = LookupBPI(F);
1477 auto *BFI = LookupBFI(F);
1478 // Split indirectbr critical edges here before computing the MST rather than
1479 // later in getInstrBB() to avoid invalidating it.
1480 SplitIndirectBrCriticalEdges(F, BPI, BFI);
1481 PGOUseFunc Func(F, &M, ComdatMembers, BPI, BFI);
1482 bool AllZeros = false;
1483 if (!Func.readCounters(PGOReader.get(), AllZeros))
1486 F.setEntryCount(ProfileCount(0, Function::PCT_Real));
1487 if (Func.getProgramMaxCount() != 0)
1488 ColdFunctions.push_back(&F);
1491 Func.populateCounters();
1492 Func.setBranchWeights();
1493 Func.annotateValueSites();
1494 Func.annotateIrrLoopHeaderWeights();
1495 PGOUseFunc::FuncFreqAttr FreqAttr = Func.getFuncFreqAttr();
1496 if (FreqAttr == PGOUseFunc::FFA_Cold)
1497 ColdFunctions.push_back(&F);
1498 else if (FreqAttr == PGOUseFunc::FFA_Hot)
1499 HotFunctions.push_back(&F);
1500 if (PGOViewCounts != PGOVCT_None &&
1501 (ViewBlockFreqFuncName.empty() ||
1502 F.getName().equals(ViewBlockFreqFuncName))) {
1503 LoopInfo LI{DominatorTree(F)};
1504 std::unique_ptr<BranchProbabilityInfo> NewBPI =
1505 llvm::make_unique<BranchProbabilityInfo>(F, LI);
1506 std::unique_ptr<BlockFrequencyInfo> NewBFI =
1507 llvm::make_unique<BlockFrequencyInfo>(F, *NewBPI, LI);
1508 if (PGOViewCounts == PGOVCT_Graph)
1510 else if (PGOViewCounts == PGOVCT_Text) {
1511 dbgs() << "pgo-view-counts: " << Func.getFunc().getName() << "\n";
1512 NewBFI->print(dbgs());
1515 if (PGOViewRawCounts != PGOVCT_None &&
1516 (ViewBlockFreqFuncName.empty() ||
1517 F.getName().equals(ViewBlockFreqFuncName))) {
1518 if (PGOViewRawCounts == PGOVCT_Graph)
1519 if (ViewBlockFreqFuncName.empty())
1520 WriteGraph(&Func, Twine("PGORawCounts_") + Func.getFunc().getName());
1522 ViewGraph(&Func, Twine("PGORawCounts_") + Func.getFunc().getName());
1523 else if (PGOViewRawCounts == PGOVCT_Text) {
1524 dbgs() << "pgo-view-raw-counts: " << Func.getFunc().getName() << "\n";
1529 M.setProfileSummary(PGOReader->getSummary().getMD(M.getContext()));
1530 // Set function hotness attribute from the profile.
1531 // We have to apply these attributes at the end because their presence
1532 // can affect the BranchProbabilityInfo of any callers, resulting in an
1533 // inconsistent MST between prof-gen and prof-use.
1534 for (auto &F : HotFunctions) {
1535 F->addFnAttr(Attribute::InlineHint);
1536 LLVM_DEBUG(dbgs() << "Set inline attribute to function: " << F->getName()
1539 for (auto &F : ColdFunctions) {
1540 F->addFnAttr(Attribute::Cold);
1541 LLVM_DEBUG(dbgs() << "Set cold attribute to function: " << F->getName()
1547 PGOInstrumentationUse::PGOInstrumentationUse(std::string Filename,
1548 std::string RemappingFilename)
1549 : ProfileFileName(std::move(Filename)),
1550 ProfileRemappingFileName(std::move(RemappingFilename)) {
1551 if (!PGOTestProfileFile.empty())
1552 ProfileFileName = PGOTestProfileFile;
1553 if (!PGOTestProfileRemappingFile.empty())
1554 ProfileRemappingFileName = PGOTestProfileRemappingFile;
1557 PreservedAnalyses PGOInstrumentationUse::run(Module &M,
1558 ModuleAnalysisManager &AM) {
1560 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
1561 auto LookupBPI = [&FAM](Function &F) {
1562 return &FAM.getResult<BranchProbabilityAnalysis>(F);
1565 auto LookupBFI = [&FAM](Function &F) {
1566 return &FAM.getResult<BlockFrequencyAnalysis>(F);
1569 if (!annotateAllFunctions(M, ProfileFileName, ProfileRemappingFileName,
1570 LookupBPI, LookupBFI))
1571 return PreservedAnalyses::all();
1573 return PreservedAnalyses::none();
1576 bool PGOInstrumentationUseLegacyPass::runOnModule(Module &M) {
1580 auto LookupBPI = [this](Function &F) {
1581 return &this->getAnalysis<BranchProbabilityInfoWrapperPass>(F).getBPI();
1583 auto LookupBFI = [this](Function &F) {
1584 return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI();
1587 return annotateAllFunctions(M, ProfileFileName, "", LookupBPI, LookupBFI);
1590 static std::string getSimpleNodeName(const BasicBlock *Node) {
1591 if (!Node->getName().empty())
1592 return Node->getName();
1594 std::string SimpleNodeName;
1595 raw_string_ostream OS(SimpleNodeName);
1596 Node->printAsOperand(OS, false);
1600 void llvm::setProfMetadata(Module *M, Instruction *TI,
1601 ArrayRef<uint64_t> EdgeCounts,
1602 uint64_t MaxCount) {
1603 MDBuilder MDB(M->getContext());
1604 assert(MaxCount > 0 && "Bad max count");
1605 uint64_t Scale = calculateCountScale(MaxCount);
1606 SmallVector<unsigned, 4> Weights;
1607 for (const auto &ECI : EdgeCounts)
1608 Weights.push_back(scaleBranchCount(ECI, Scale));
1610 LLVM_DEBUG(dbgs() << "Weight is: "; for (const auto &W
1614 TI->setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
1615 if (EmitBranchProbability) {
1616 std::string BrCondStr = getBranchCondString(TI);
1617 if (BrCondStr.empty())
1621 std::accumulate(Weights.begin(), Weights.end(), (uint64_t)0,
1622 [](uint64_t w1, uint64_t w2) { return w1 + w2; });
1623 uint64_t TotalCount =
1624 std::accumulate(EdgeCounts.begin(), EdgeCounts.end(), (uint64_t)0,
1625 [](uint64_t c1, uint64_t c2) { return c1 + c2; });
1626 Scale = calculateCountScale(WSum);
1627 BranchProbability BP(scaleBranchCount(Weights[0], Scale),
1628 scaleBranchCount(WSum, Scale));
1629 std::string BranchProbStr;
1630 raw_string_ostream OS(BranchProbStr);
1632 OS << " (total count : " << TotalCount << ")";
1634 Function *F = TI->getParent()->getParent();
1635 OptimizationRemarkEmitter ORE(F);
1637 return OptimizationRemark(DEBUG_TYPE, "pgo-instrumentation", TI)
1638 << BrCondStr << " is true with probability : " << BranchProbStr;
1645 void setIrrLoopHeaderMetadata(Module *M, Instruction *TI, uint64_t Count) {
1646 MDBuilder MDB(M->getContext());
1647 TI->setMetadata(llvm::LLVMContext::MD_irr_loop,
1648 MDB.createIrrLoopHeaderWeight(Count));
1651 template <> struct GraphTraits<PGOUseFunc *> {
1652 using NodeRef = const BasicBlock *;
1653 using ChildIteratorType = succ_const_iterator;
1654 using nodes_iterator = pointer_iterator<Function::const_iterator>;
1656 static NodeRef getEntryNode(const PGOUseFunc *G) {
1657 return &G->getFunc().front();
1660 static ChildIteratorType child_begin(const NodeRef N) {
1661 return succ_begin(N);
1664 static ChildIteratorType child_end(const NodeRef N) { return succ_end(N); }
1666 static nodes_iterator nodes_begin(const PGOUseFunc *G) {
1667 return nodes_iterator(G->getFunc().begin());
1670 static nodes_iterator nodes_end(const PGOUseFunc *G) {
1671 return nodes_iterator(G->getFunc().end());
1675 template <> struct DOTGraphTraits<PGOUseFunc *> : DefaultDOTGraphTraits {
1676 explicit DOTGraphTraits(bool isSimple = false)
1677 : DefaultDOTGraphTraits(isSimple) {}
1679 static std::string getGraphName(const PGOUseFunc *G) {
1680 return G->getFunc().getName();
1683 std::string getNodeLabel(const BasicBlock *Node, const PGOUseFunc *Graph) {
1685 raw_string_ostream OS(Result);
1687 OS << getSimpleNodeName(Node) << ":\\l";
1688 UseBBInfo *BI = Graph->findBBInfo(Node);
1690 if (BI && BI->CountValid)
1691 OS << BI->CountValue << "\\l";
1695 if (!PGOInstrSelect)
1698 for (auto BI = Node->begin(); BI != Node->end(); ++BI) {
1700 if (!isa<SelectInst>(I))
1702 // Display scaled counts for SELECT instruction:
1703 OS << "SELECT : { T = ";
1705 bool HasProf = I->extractProfMetadata(TC, FC);
1707 OS << "Unknown, F = Unknown }\\l";
1709 OS << TC << ", F = " << FC << " }\\l";
1715 } // end namespace llvm