1*5f7ddb14SDimitry Andric //===- SampleProfileLoaderBaseUtil.cpp - Profile loader Util func ---------===//
2*5f7ddb14SDimitry Andric //
3*5f7ddb14SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*5f7ddb14SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*5f7ddb14SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*5f7ddb14SDimitry Andric //
7*5f7ddb14SDimitry Andric //===----------------------------------------------------------------------===//
8*5f7ddb14SDimitry Andric //
9*5f7ddb14SDimitry Andric // This file implements the SampleProfileLoader base utility functions.
10*5f7ddb14SDimitry Andric //
11*5f7ddb14SDimitry Andric //===----------------------------------------------------------------------===//
12*5f7ddb14SDimitry Andric 
13*5f7ddb14SDimitry Andric #include "llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h"
14*5f7ddb14SDimitry Andric 
15*5f7ddb14SDimitry Andric namespace llvm {
16*5f7ddb14SDimitry Andric 
17*5f7ddb14SDimitry Andric cl::opt<unsigned> SampleProfileMaxPropagateIterations(
18*5f7ddb14SDimitry Andric     "sample-profile-max-propagate-iterations", cl::init(100),
19*5f7ddb14SDimitry Andric     cl::desc("Maximum number of iterations to go through when propagating "
20*5f7ddb14SDimitry Andric              "sample block/edge weights through the CFG."));
21*5f7ddb14SDimitry Andric 
22*5f7ddb14SDimitry Andric cl::opt<unsigned> SampleProfileRecordCoverage(
23*5f7ddb14SDimitry Andric     "sample-profile-check-record-coverage", cl::init(0), cl::value_desc("N"),
24*5f7ddb14SDimitry Andric     cl::desc("Emit a warning if less than N% of records in the input profile "
25*5f7ddb14SDimitry Andric              "are matched to the IR."));
26*5f7ddb14SDimitry Andric 
27*5f7ddb14SDimitry Andric cl::opt<unsigned> SampleProfileSampleCoverage(
28*5f7ddb14SDimitry Andric     "sample-profile-check-sample-coverage", cl::init(0), cl::value_desc("N"),
29*5f7ddb14SDimitry Andric     cl::desc("Emit a warning if less than N% of samples in the input profile "
30*5f7ddb14SDimitry Andric              "are matched to the IR."));
31*5f7ddb14SDimitry Andric 
32*5f7ddb14SDimitry Andric cl::opt<bool> NoWarnSampleUnused(
33*5f7ddb14SDimitry Andric     "no-warn-sample-unused", cl::init(false), cl::Hidden,
34*5f7ddb14SDimitry Andric     cl::desc("Use this option to turn off/on warnings about function with "
35*5f7ddb14SDimitry Andric              "samples but without debug information to use those samples. "));
36*5f7ddb14SDimitry Andric 
37*5f7ddb14SDimitry Andric namespace sampleprofutil {
38*5f7ddb14SDimitry Andric 
39*5f7ddb14SDimitry Andric /// Return true if the given callsite is hot wrt to hot cutoff threshold.
40*5f7ddb14SDimitry Andric ///
41*5f7ddb14SDimitry Andric /// Functions that were inlined in the original binary will be represented
42*5f7ddb14SDimitry Andric /// in the inline stack in the sample profile. If the profile shows that
43*5f7ddb14SDimitry Andric /// the original inline decision was "good" (i.e., the callsite is executed
44*5f7ddb14SDimitry Andric /// frequently), then we will recreate the inline decision and apply the
45*5f7ddb14SDimitry Andric /// profile from the inlined callsite.
46*5f7ddb14SDimitry Andric ///
47*5f7ddb14SDimitry Andric /// To decide whether an inlined callsite is hot, we compare the callsite
48*5f7ddb14SDimitry Andric /// sample count with the hot cutoff computed by ProfileSummaryInfo, it is
49*5f7ddb14SDimitry Andric /// regarded as hot if the count is above the cutoff value.
50*5f7ddb14SDimitry Andric ///
51*5f7ddb14SDimitry Andric /// When ProfileAccurateForSymsInList is enabled and profile symbol list
52*5f7ddb14SDimitry Andric /// is present, functions in the profile symbol list but without profile will
53*5f7ddb14SDimitry Andric /// be regarded as cold and much less inlining will happen in CGSCC inlining
54*5f7ddb14SDimitry Andric /// pass, so we tend to lower the hot criteria here to allow more early
55*5f7ddb14SDimitry Andric /// inlining to happen for warm callsites and it is helpful for performance.
callsiteIsHot(const FunctionSamples * CallsiteFS,ProfileSummaryInfo * PSI,bool ProfAccForSymsInList)56*5f7ddb14SDimitry Andric bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI,
57*5f7ddb14SDimitry Andric                    bool ProfAccForSymsInList) {
58*5f7ddb14SDimitry Andric   if (!CallsiteFS)
59*5f7ddb14SDimitry Andric     return false; // The callsite was not inlined in the original binary.
60*5f7ddb14SDimitry Andric 
61*5f7ddb14SDimitry Andric   assert(PSI && "PSI is expected to be non null");
62*5f7ddb14SDimitry Andric   uint64_t CallsiteTotalSamples = CallsiteFS->getTotalSamples();
63*5f7ddb14SDimitry Andric   if (ProfAccForSymsInList)
64*5f7ddb14SDimitry Andric     return !PSI->isColdCount(CallsiteTotalSamples);
65*5f7ddb14SDimitry Andric   else
66*5f7ddb14SDimitry Andric     return PSI->isHotCount(CallsiteTotalSamples);
67*5f7ddb14SDimitry Andric }
68*5f7ddb14SDimitry Andric 
69*5f7ddb14SDimitry Andric /// Mark as used the sample record for the given function samples at
70*5f7ddb14SDimitry Andric /// (LineOffset, Discriminator).
71*5f7ddb14SDimitry Andric ///
72*5f7ddb14SDimitry Andric /// \returns true if this is the first time we mark the given record.
markSamplesUsed(const FunctionSamples * FS,uint32_t LineOffset,uint32_t Discriminator,uint64_t Samples)73*5f7ddb14SDimitry Andric bool SampleCoverageTracker::markSamplesUsed(const FunctionSamples *FS,
74*5f7ddb14SDimitry Andric                                             uint32_t LineOffset,
75*5f7ddb14SDimitry Andric                                             uint32_t Discriminator,
76*5f7ddb14SDimitry Andric                                             uint64_t Samples) {
77*5f7ddb14SDimitry Andric   LineLocation Loc(LineOffset, Discriminator);
78*5f7ddb14SDimitry Andric   unsigned &Count = SampleCoverage[FS][Loc];
79*5f7ddb14SDimitry Andric   bool FirstTime = (++Count == 1);
80*5f7ddb14SDimitry Andric   if (FirstTime)
81*5f7ddb14SDimitry Andric     TotalUsedSamples += Samples;
82*5f7ddb14SDimitry Andric   return FirstTime;
83*5f7ddb14SDimitry Andric }
84*5f7ddb14SDimitry Andric 
85*5f7ddb14SDimitry Andric /// Return the number of sample records that were applied from this profile.
86*5f7ddb14SDimitry Andric ///
87*5f7ddb14SDimitry Andric /// This count does not include records from cold inlined callsites.
88*5f7ddb14SDimitry Andric unsigned
countUsedRecords(const FunctionSamples * FS,ProfileSummaryInfo * PSI) const89*5f7ddb14SDimitry Andric SampleCoverageTracker::countUsedRecords(const FunctionSamples *FS,
90*5f7ddb14SDimitry Andric                                         ProfileSummaryInfo *PSI) const {
91*5f7ddb14SDimitry Andric   auto I = SampleCoverage.find(FS);
92*5f7ddb14SDimitry Andric 
93*5f7ddb14SDimitry Andric   // The size of the coverage map for FS represents the number of records
94*5f7ddb14SDimitry Andric   // that were marked used at least once.
95*5f7ddb14SDimitry Andric   unsigned Count = (I != SampleCoverage.end()) ? I->second.size() : 0;
96*5f7ddb14SDimitry Andric 
97*5f7ddb14SDimitry Andric   // If there are inlined callsites in this function, count the samples found
98*5f7ddb14SDimitry Andric   // in the respective bodies. However, do not bother counting callees with 0
99*5f7ddb14SDimitry Andric   // total samples, these are callees that were never invoked at runtime.
100*5f7ddb14SDimitry Andric   for (const auto &I : FS->getCallsiteSamples())
101*5f7ddb14SDimitry Andric     for (const auto &J : I.second) {
102*5f7ddb14SDimitry Andric       const FunctionSamples *CalleeSamples = &J.second;
103*5f7ddb14SDimitry Andric       if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
104*5f7ddb14SDimitry Andric         Count += countUsedRecords(CalleeSamples, PSI);
105*5f7ddb14SDimitry Andric     }
106*5f7ddb14SDimitry Andric 
107*5f7ddb14SDimitry Andric   return Count;
108*5f7ddb14SDimitry Andric }
109*5f7ddb14SDimitry Andric 
110*5f7ddb14SDimitry Andric /// Return the number of sample records in the body of this profile.
111*5f7ddb14SDimitry Andric ///
112*5f7ddb14SDimitry Andric /// This count does not include records from cold inlined callsites.
113*5f7ddb14SDimitry Andric unsigned
countBodyRecords(const FunctionSamples * FS,ProfileSummaryInfo * PSI) const114*5f7ddb14SDimitry Andric SampleCoverageTracker::countBodyRecords(const FunctionSamples *FS,
115*5f7ddb14SDimitry Andric                                         ProfileSummaryInfo *PSI) const {
116*5f7ddb14SDimitry Andric   unsigned Count = FS->getBodySamples().size();
117*5f7ddb14SDimitry Andric 
118*5f7ddb14SDimitry Andric   // Only count records in hot callsites.
119*5f7ddb14SDimitry Andric   for (const auto &I : FS->getCallsiteSamples())
120*5f7ddb14SDimitry Andric     for (const auto &J : I.second) {
121*5f7ddb14SDimitry Andric       const FunctionSamples *CalleeSamples = &J.second;
122*5f7ddb14SDimitry Andric       if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
123*5f7ddb14SDimitry Andric         Count += countBodyRecords(CalleeSamples, PSI);
124*5f7ddb14SDimitry Andric     }
125*5f7ddb14SDimitry Andric 
126*5f7ddb14SDimitry Andric   return Count;
127*5f7ddb14SDimitry Andric }
128*5f7ddb14SDimitry Andric 
129*5f7ddb14SDimitry Andric /// Return the number of samples collected in the body of this profile.
130*5f7ddb14SDimitry Andric ///
131*5f7ddb14SDimitry Andric /// This count does not include samples from cold inlined callsites.
132*5f7ddb14SDimitry Andric uint64_t
countBodySamples(const FunctionSamples * FS,ProfileSummaryInfo * PSI) const133*5f7ddb14SDimitry Andric SampleCoverageTracker::countBodySamples(const FunctionSamples *FS,
134*5f7ddb14SDimitry Andric                                         ProfileSummaryInfo *PSI) const {
135*5f7ddb14SDimitry Andric   uint64_t Total = 0;
136*5f7ddb14SDimitry Andric   for (const auto &I : FS->getBodySamples())
137*5f7ddb14SDimitry Andric     Total += I.second.getSamples();
138*5f7ddb14SDimitry Andric 
139*5f7ddb14SDimitry Andric   // Only count samples in hot callsites.
140*5f7ddb14SDimitry Andric   for (const auto &I : FS->getCallsiteSamples())
141*5f7ddb14SDimitry Andric     for (const auto &J : I.second) {
142*5f7ddb14SDimitry Andric       const FunctionSamples *CalleeSamples = &J.second;
143*5f7ddb14SDimitry Andric       if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
144*5f7ddb14SDimitry Andric         Total += countBodySamples(CalleeSamples, PSI);
145*5f7ddb14SDimitry Andric     }
146*5f7ddb14SDimitry Andric 
147*5f7ddb14SDimitry Andric   return Total;
148*5f7ddb14SDimitry Andric }
149*5f7ddb14SDimitry Andric 
150*5f7ddb14SDimitry Andric /// Return the fraction of sample records used in this profile.
151*5f7ddb14SDimitry Andric ///
152*5f7ddb14SDimitry Andric /// The returned value is an unsigned integer in the range 0-100 indicating
153*5f7ddb14SDimitry Andric /// the percentage of sample records that were used while applying this
154*5f7ddb14SDimitry Andric /// profile to the associated function.
computeCoverage(unsigned Used,unsigned Total) const155*5f7ddb14SDimitry Andric unsigned SampleCoverageTracker::computeCoverage(unsigned Used,
156*5f7ddb14SDimitry Andric                                                 unsigned Total) const {
157*5f7ddb14SDimitry Andric   assert(Used <= Total &&
158*5f7ddb14SDimitry Andric          "number of used records cannot exceed the total number of records");
159*5f7ddb14SDimitry Andric   return Total > 0 ? Used * 100 / Total : 100;
160*5f7ddb14SDimitry Andric }
161*5f7ddb14SDimitry Andric 
162*5f7ddb14SDimitry Andric /// Create a global variable to flag FSDiscriminators are used.
createFSDiscriminatorVariable(Module * M)163*5f7ddb14SDimitry Andric void createFSDiscriminatorVariable(Module *M) {
164*5f7ddb14SDimitry Andric   const char *FSDiscriminatorVar = "__llvm_fs_discriminator__";
165*5f7ddb14SDimitry Andric   if (M->getGlobalVariable(FSDiscriminatorVar))
166*5f7ddb14SDimitry Andric     return;
167*5f7ddb14SDimitry Andric 
168*5f7ddb14SDimitry Andric   auto &Context = M->getContext();
169*5f7ddb14SDimitry Andric   // Place this variable to llvm.used so it won't be GC'ed.
170*5f7ddb14SDimitry Andric   appendToUsed(*M, {new GlobalVariable(*M, Type::getInt1Ty(Context), true,
171*5f7ddb14SDimitry Andric                                        GlobalValue::WeakODRLinkage,
172*5f7ddb14SDimitry Andric                                        ConstantInt::getTrue(Context),
173*5f7ddb14SDimitry Andric                                        FSDiscriminatorVar)});
174*5f7ddb14SDimitry Andric }
175*5f7ddb14SDimitry Andric 
176*5f7ddb14SDimitry Andric } // end of namespace sampleprofutil
177*5f7ddb14SDimitry Andric } // end of namespace llvm
178