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