1 //=- SyntheticCountsPropagation.cpp - Propagate function counts --*- C++ -*-=//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a transformation that synthesizes entry counts for
10 // functions and attaches !prof metadata to functions with the synthesized
11 // counts. The presence of !prof metadata with counter name set to
12 // 'synthesized_function_entry_count' indicate that the value of the counter is
13 // an estimation of the likely execution count of the function. This transform
14 // is applied only in non PGO mode as functions get 'real' profile-based
15 // function entry counts in the PGO mode.
16 //
17 // The transformation works by first assigning some initial values to the entry
18 // counts of all functions and then doing a top-down traversal of the
19 // callgraph-scc to propagate the counts. For each function the set of callsites
20 // and their relative block frequency is gathered. The relative block frequency
21 // multiplied by the entry count of the caller and added to the callee's entry
22 // count. For non-trivial SCCs, the new counts are computed from the previous
23 // counts and updated in one shot.
24 //
25 //===----------------------------------------------------------------------===//
26 
27 #include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
28 #include "llvm/Analysis/BlockFrequencyInfo.h"
29 #include "llvm/Analysis/CallGraph.h"
30 #include "llvm/Analysis/SyntheticCountsUtils.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/Instructions.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/Support/CommandLine.h"
35 
36 using namespace llvm;
37 using Scaled64 = ScaledNumber<uint64_t>;
38 using ProfileCount = Function::ProfileCount;
39 
40 #define DEBUG_TYPE "synthetic-counts-propagation"
41 
42 namespace llvm {
43 cl::opt<int>
44     InitialSyntheticCount("initial-synthetic-count", cl::Hidden, cl::init(10),
45                           cl::ZeroOrMore,
46                           cl::desc("Initial value of synthetic entry count"));
47 } // namespace llvm
48 
49 /// Initial synthetic count assigned to inline functions.
50 static cl::opt<int> InlineSyntheticCount(
51     "inline-synthetic-count", cl::Hidden, cl::init(15), cl::ZeroOrMore,
52     cl::desc("Initial synthetic entry count for inline functions."));
53 
54 /// Initial synthetic count assigned to cold functions.
55 static cl::opt<int> ColdSyntheticCount(
56     "cold-synthetic-count", cl::Hidden, cl::init(5), cl::ZeroOrMore,
57     cl::desc("Initial synthetic entry count for cold functions."));
58 
59 // Assign initial synthetic entry counts to functions.
60 static void
61 initializeCounts(Module &M, function_ref<void(Function *, uint64_t)> SetCount) {
62   auto MayHaveIndirectCalls = [](Function &F) {
63     for (auto *U : F.users()) {
64       if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
65         return true;
66     }
67     return false;
68   };
69 
70   for (Function &F : M) {
71     uint64_t InitialCount = InitialSyntheticCount;
72     if (F.isDeclaration())
73       continue;
74     if (F.hasFnAttribute(Attribute::AlwaysInline) ||
75         F.hasFnAttribute(Attribute::InlineHint)) {
76       // Use a higher value for inline functions to account for the fact that
77       // these are usually beneficial to inline.
78       InitialCount = InlineSyntheticCount;
79     } else if (F.hasLocalLinkage() && !MayHaveIndirectCalls(F)) {
80       // Local functions without inline hints get counts only through
81       // propagation.
82       InitialCount = 0;
83     } else if (F.hasFnAttribute(Attribute::Cold) ||
84                F.hasFnAttribute(Attribute::NoInline)) {
85       // Use a lower value for noinline and cold functions.
86       InitialCount = ColdSyntheticCount;
87     }
88     SetCount(&F, InitialCount);
89   }
90 }
91 
92 PreservedAnalyses SyntheticCountsPropagation::run(Module &M,
93                                                   ModuleAnalysisManager &MAM) {
94   FunctionAnalysisManager &FAM =
95       MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
96   DenseMap<Function *, Scaled64> Counts;
97   // Set initial entry counts.
98   initializeCounts(
99       M, [&](Function *F, uint64_t Count) { Counts[F] = Scaled64(Count, 0); });
100 
101   // Edge includes information about the source. Hence ignore the first
102   // parameter.
103   auto GetCallSiteProfCount = [&](const CallGraphNode *,
104                                   const CallGraphNode::CallRecord &Edge) {
105     Optional<Scaled64> Res = None;
106     if (!Edge.first)
107       return Res;
108     CallBase &CB = *cast<CallBase>(*Edge.first);
109     Function *Caller = CB.getCaller();
110     auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(*Caller);
111 
112     // Now compute the callsite count from relative frequency and
113     // entry count:
114     BasicBlock *CSBB = CB.getParent();
115     Scaled64 EntryFreq(BFI.getEntryFreq(), 0);
116     Scaled64 BBCount(BFI.getBlockFreq(CSBB).getFrequency(), 0);
117     BBCount /= EntryFreq;
118     BBCount *= Counts[Caller];
119     return Optional<Scaled64>(BBCount);
120   };
121 
122   CallGraph CG(M);
123   // Propgate the entry counts on the callgraph.
124   SyntheticCountsUtils<const CallGraph *>::propagate(
125       &CG, GetCallSiteProfCount, [&](const CallGraphNode *N, Scaled64 New) {
126         auto F = N->getFunction();
127         if (!F || F->isDeclaration())
128           return;
129 
130         Counts[F] += New;
131       });
132 
133   // Set the counts as metadata.
134   for (auto Entry : Counts) {
135     Entry.first->setEntryCount(ProfileCount(
136         Entry.second.template toInt<uint64_t>(), Function::PCT_Synthetic));
137   }
138 
139   return PreservedAnalyses::all();
140 }
141