xref: /freebsd-src/contrib/llvm-project/llvm/lib/Transforms/Utils/SampleProfileLoaderBaseUtil.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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 
45*81ad6265SDimitry Andric cl::opt<bool> SampleProfileInferEntryCount(
46*81ad6265SDimitry Andric     "sample-profile-infer-entry-count", cl::init(true), cl::Hidden,
47*81ad6265SDimitry Andric     cl::desc("Use profi to infer function entry count."));
48*81ad6265SDimitry Andric 
49fe6060f1SDimitry Andric namespace sampleprofutil {
50fe6060f1SDimitry Andric 
51fe6060f1SDimitry Andric /// Return true if the given callsite is hot wrt to hot cutoff threshold.
52fe6060f1SDimitry Andric ///
53fe6060f1SDimitry Andric /// Functions that were inlined in the original binary will be represented
54fe6060f1SDimitry Andric /// in the inline stack in the sample profile. If the profile shows that
55fe6060f1SDimitry Andric /// the original inline decision was "good" (i.e., the callsite is executed
56fe6060f1SDimitry Andric /// frequently), then we will recreate the inline decision and apply the
57fe6060f1SDimitry Andric /// profile from the inlined callsite.
58fe6060f1SDimitry Andric ///
59fe6060f1SDimitry Andric /// To decide whether an inlined callsite is hot, we compare the callsite
60fe6060f1SDimitry Andric /// sample count with the hot cutoff computed by ProfileSummaryInfo, it is
61fe6060f1SDimitry Andric /// regarded as hot if the count is above the cutoff value.
62fe6060f1SDimitry Andric ///
63fe6060f1SDimitry Andric /// When ProfileAccurateForSymsInList is enabled and profile symbol list
64fe6060f1SDimitry Andric /// is present, functions in the profile symbol list but without profile will
65fe6060f1SDimitry Andric /// be regarded as cold and much less inlining will happen in CGSCC inlining
66fe6060f1SDimitry Andric /// pass, so we tend to lower the hot criteria here to allow more early
67fe6060f1SDimitry Andric /// inlining to happen for warm callsites and it is helpful for performance.
68fe6060f1SDimitry Andric bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI,
69fe6060f1SDimitry Andric                    bool ProfAccForSymsInList) {
70fe6060f1SDimitry Andric   if (!CallsiteFS)
71fe6060f1SDimitry Andric     return false; // The callsite was not inlined in the original binary.
72fe6060f1SDimitry Andric 
73fe6060f1SDimitry Andric   assert(PSI && "PSI is expected to be non null");
74fe6060f1SDimitry Andric   uint64_t CallsiteTotalSamples = CallsiteFS->getTotalSamples();
75fe6060f1SDimitry Andric   if (ProfAccForSymsInList)
76fe6060f1SDimitry Andric     return !PSI->isColdCount(CallsiteTotalSamples);
77fe6060f1SDimitry Andric   else
78fe6060f1SDimitry Andric     return PSI->isHotCount(CallsiteTotalSamples);
79fe6060f1SDimitry Andric }
80fe6060f1SDimitry Andric 
81fe6060f1SDimitry Andric /// Mark as used the sample record for the given function samples at
82fe6060f1SDimitry Andric /// (LineOffset, Discriminator).
83fe6060f1SDimitry Andric ///
84fe6060f1SDimitry Andric /// \returns true if this is the first time we mark the given record.
85fe6060f1SDimitry Andric bool SampleCoverageTracker::markSamplesUsed(const FunctionSamples *FS,
86fe6060f1SDimitry Andric                                             uint32_t LineOffset,
87fe6060f1SDimitry Andric                                             uint32_t Discriminator,
88fe6060f1SDimitry Andric                                             uint64_t Samples) {
89fe6060f1SDimitry Andric   LineLocation Loc(LineOffset, Discriminator);
90fe6060f1SDimitry Andric   unsigned &Count = SampleCoverage[FS][Loc];
91fe6060f1SDimitry Andric   bool FirstTime = (++Count == 1);
92fe6060f1SDimitry Andric   if (FirstTime)
93fe6060f1SDimitry Andric     TotalUsedSamples += Samples;
94fe6060f1SDimitry Andric   return FirstTime;
95fe6060f1SDimitry Andric }
96fe6060f1SDimitry Andric 
97fe6060f1SDimitry Andric /// Return the number of sample records that were applied from this profile.
98fe6060f1SDimitry Andric ///
99fe6060f1SDimitry Andric /// This count does not include records from cold inlined callsites.
100fe6060f1SDimitry Andric unsigned
101fe6060f1SDimitry Andric SampleCoverageTracker::countUsedRecords(const FunctionSamples *FS,
102fe6060f1SDimitry Andric                                         ProfileSummaryInfo *PSI) const {
103fe6060f1SDimitry Andric   auto I = SampleCoverage.find(FS);
104fe6060f1SDimitry Andric 
105fe6060f1SDimitry Andric   // The size of the coverage map for FS represents the number of records
106fe6060f1SDimitry Andric   // that were marked used at least once.
107fe6060f1SDimitry Andric   unsigned Count = (I != SampleCoverage.end()) ? I->second.size() : 0;
108fe6060f1SDimitry Andric 
109fe6060f1SDimitry Andric   // If there are inlined callsites in this function, count the samples found
110fe6060f1SDimitry Andric   // in the respective bodies. However, do not bother counting callees with 0
111fe6060f1SDimitry Andric   // total samples, these are callees that were never invoked at runtime.
112fe6060f1SDimitry Andric   for (const auto &I : FS->getCallsiteSamples())
113fe6060f1SDimitry Andric     for (const auto &J : I.second) {
114fe6060f1SDimitry Andric       const FunctionSamples *CalleeSamples = &J.second;
115fe6060f1SDimitry Andric       if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
116fe6060f1SDimitry Andric         Count += countUsedRecords(CalleeSamples, PSI);
117fe6060f1SDimitry Andric     }
118fe6060f1SDimitry Andric 
119fe6060f1SDimitry Andric   return Count;
120fe6060f1SDimitry Andric }
121fe6060f1SDimitry Andric 
122fe6060f1SDimitry Andric /// Return the number of sample records in the body of this profile.
123fe6060f1SDimitry Andric ///
124fe6060f1SDimitry Andric /// This count does not include records from cold inlined callsites.
125fe6060f1SDimitry Andric unsigned
126fe6060f1SDimitry Andric SampleCoverageTracker::countBodyRecords(const FunctionSamples *FS,
127fe6060f1SDimitry Andric                                         ProfileSummaryInfo *PSI) const {
128fe6060f1SDimitry Andric   unsigned Count = FS->getBodySamples().size();
129fe6060f1SDimitry Andric 
130fe6060f1SDimitry Andric   // Only count records in hot callsites.
131fe6060f1SDimitry Andric   for (const auto &I : FS->getCallsiteSamples())
132fe6060f1SDimitry Andric     for (const auto &J : I.second) {
133fe6060f1SDimitry Andric       const FunctionSamples *CalleeSamples = &J.second;
134fe6060f1SDimitry Andric       if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
135fe6060f1SDimitry Andric         Count += countBodyRecords(CalleeSamples, PSI);
136fe6060f1SDimitry Andric     }
137fe6060f1SDimitry Andric 
138fe6060f1SDimitry Andric   return Count;
139fe6060f1SDimitry Andric }
140fe6060f1SDimitry Andric 
141fe6060f1SDimitry Andric /// Return the number of samples collected in the body of this profile.
142fe6060f1SDimitry Andric ///
143fe6060f1SDimitry Andric /// This count does not include samples from cold inlined callsites.
144fe6060f1SDimitry Andric uint64_t
145fe6060f1SDimitry Andric SampleCoverageTracker::countBodySamples(const FunctionSamples *FS,
146fe6060f1SDimitry Andric                                         ProfileSummaryInfo *PSI) const {
147fe6060f1SDimitry Andric   uint64_t Total = 0;
148fe6060f1SDimitry Andric   for (const auto &I : FS->getBodySamples())
149fe6060f1SDimitry Andric     Total += I.second.getSamples();
150fe6060f1SDimitry Andric 
151fe6060f1SDimitry Andric   // Only count samples in hot callsites.
152fe6060f1SDimitry Andric   for (const auto &I : FS->getCallsiteSamples())
153fe6060f1SDimitry Andric     for (const auto &J : I.second) {
154fe6060f1SDimitry Andric       const FunctionSamples *CalleeSamples = &J.second;
155fe6060f1SDimitry Andric       if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
156fe6060f1SDimitry Andric         Total += countBodySamples(CalleeSamples, PSI);
157fe6060f1SDimitry Andric     }
158fe6060f1SDimitry Andric 
159fe6060f1SDimitry Andric   return Total;
160fe6060f1SDimitry Andric }
161fe6060f1SDimitry Andric 
162fe6060f1SDimitry Andric /// Return the fraction of sample records used in this profile.
163fe6060f1SDimitry Andric ///
164fe6060f1SDimitry Andric /// The returned value is an unsigned integer in the range 0-100 indicating
165fe6060f1SDimitry Andric /// the percentage of sample records that were used while applying this
166fe6060f1SDimitry Andric /// profile to the associated function.
167fe6060f1SDimitry Andric unsigned SampleCoverageTracker::computeCoverage(unsigned Used,
168fe6060f1SDimitry Andric                                                 unsigned Total) const {
169fe6060f1SDimitry Andric   assert(Used <= Total &&
170fe6060f1SDimitry Andric          "number of used records cannot exceed the total number of records");
171fe6060f1SDimitry Andric   return Total > 0 ? Used * 100 / Total : 100;
172fe6060f1SDimitry Andric }
173fe6060f1SDimitry Andric 
174fe6060f1SDimitry Andric /// Create a global variable to flag FSDiscriminators are used.
175fe6060f1SDimitry Andric void createFSDiscriminatorVariable(Module *M) {
176fe6060f1SDimitry Andric   const char *FSDiscriminatorVar = "__llvm_fs_discriminator__";
177fe6060f1SDimitry Andric   if (M->getGlobalVariable(FSDiscriminatorVar))
178fe6060f1SDimitry Andric     return;
179fe6060f1SDimitry Andric 
180fe6060f1SDimitry Andric   auto &Context = M->getContext();
181fe6060f1SDimitry Andric   // Place this variable to llvm.used so it won't be GC'ed.
182fe6060f1SDimitry Andric   appendToUsed(*M, {new GlobalVariable(*M, Type::getInt1Ty(Context), true,
183fe6060f1SDimitry Andric                                        GlobalValue::WeakODRLinkage,
184fe6060f1SDimitry Andric                                        ConstantInt::getTrue(Context),
185fe6060f1SDimitry Andric                                        FSDiscriminatorVar)});
186fe6060f1SDimitry Andric }
187fe6060f1SDimitry Andric 
188fe6060f1SDimitry Andric } // end of namespace sampleprofutil
189fe6060f1SDimitry Andric } // end of namespace llvm
190