xref: /src/contrib/llvm-project/llvm/lib/Transforms/Utils/SampleProfileLoaderBaseUtil.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1344a3780SDimitry Andric //===- SampleProfileLoaderBaseUtil.cpp - Profile loader Util func ---------===//
2344a3780SDimitry Andric //
3344a3780SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4344a3780SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5344a3780SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6344a3780SDimitry Andric //
7344a3780SDimitry Andric //===----------------------------------------------------------------------===//
8344a3780SDimitry Andric //
9344a3780SDimitry Andric // This file implements the SampleProfileLoader base utility functions.
10344a3780SDimitry Andric //
11344a3780SDimitry Andric //===----------------------------------------------------------------------===//
12344a3780SDimitry Andric 
13344a3780SDimitry Andric #include "llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h"
14145449b1SDimitry Andric #include "llvm/Analysis/ProfileSummaryInfo.h"
15145449b1SDimitry Andric #include "llvm/IR/Constants.h"
16145449b1SDimitry Andric #include "llvm/IR/Module.h"
17145449b1SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
18344a3780SDimitry Andric 
19344a3780SDimitry Andric namespace llvm {
20344a3780SDimitry Andric 
21344a3780SDimitry Andric cl::opt<unsigned> SampleProfileMaxPropagateIterations(
22344a3780SDimitry Andric     "sample-profile-max-propagate-iterations", cl::init(100),
23344a3780SDimitry Andric     cl::desc("Maximum number of iterations to go through when propagating "
24344a3780SDimitry Andric              "sample block/edge weights through the CFG."));
25344a3780SDimitry Andric 
26344a3780SDimitry Andric cl::opt<unsigned> SampleProfileRecordCoverage(
27344a3780SDimitry Andric     "sample-profile-check-record-coverage", cl::init(0), cl::value_desc("N"),
28344a3780SDimitry Andric     cl::desc("Emit a warning if less than N% of records in the input profile "
29344a3780SDimitry Andric              "are matched to the IR."));
30344a3780SDimitry Andric 
31344a3780SDimitry Andric cl::opt<unsigned> SampleProfileSampleCoverage(
32344a3780SDimitry Andric     "sample-profile-check-sample-coverage", cl::init(0), cl::value_desc("N"),
33344a3780SDimitry Andric     cl::desc("Emit a warning if less than N% of samples in the input profile "
34344a3780SDimitry Andric              "are matched to the IR."));
35344a3780SDimitry Andric 
36344a3780SDimitry Andric cl::opt<bool> NoWarnSampleUnused(
37344a3780SDimitry Andric     "no-warn-sample-unused", cl::init(false), cl::Hidden,
38344a3780SDimitry Andric     cl::desc("Use this option to turn off/on warnings about function with "
39344a3780SDimitry Andric              "samples but without debug information to use those samples. "));
40344a3780SDimitry Andric 
41f65dcba8SDimitry Andric cl::opt<bool> SampleProfileUseProfi(
42145449b1SDimitry Andric     "sample-profile-use-profi", cl::Hidden,
43f65dcba8SDimitry Andric     cl::desc("Use profi to infer block and edge counts."));
44f65dcba8SDimitry Andric 
45344a3780SDimitry Andric namespace sampleprofutil {
46344a3780SDimitry Andric 
47344a3780SDimitry Andric /// Return true if the given callsite is hot wrt to hot cutoff threshold.
48344a3780SDimitry Andric ///
49344a3780SDimitry Andric /// Functions that were inlined in the original binary will be represented
50344a3780SDimitry Andric /// in the inline stack in the sample profile. If the profile shows that
51344a3780SDimitry Andric /// the original inline decision was "good" (i.e., the callsite is executed
52344a3780SDimitry Andric /// frequently), then we will recreate the inline decision and apply the
53344a3780SDimitry Andric /// profile from the inlined callsite.
54344a3780SDimitry Andric ///
55344a3780SDimitry Andric /// To decide whether an inlined callsite is hot, we compare the callsite
56344a3780SDimitry Andric /// sample count with the hot cutoff computed by ProfileSummaryInfo, it is
57344a3780SDimitry Andric /// regarded as hot if the count is above the cutoff value.
58344a3780SDimitry Andric ///
59344a3780SDimitry Andric /// When ProfileAccurateForSymsInList is enabled and profile symbol list
60344a3780SDimitry Andric /// is present, functions in the profile symbol list but without profile will
61344a3780SDimitry Andric /// be regarded as cold and much less inlining will happen in CGSCC inlining
62344a3780SDimitry Andric /// pass, so we tend to lower the hot criteria here to allow more early
63344a3780SDimitry Andric /// inlining to happen for warm callsites and it is helpful for performance.
callsiteIsHot(const FunctionSamples * CallsiteFS,ProfileSummaryInfo * PSI,bool ProfAccForSymsInList)64344a3780SDimitry Andric bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI,
65344a3780SDimitry Andric                    bool ProfAccForSymsInList) {
66344a3780SDimitry Andric   if (!CallsiteFS)
67344a3780SDimitry Andric     return false; // The callsite was not inlined in the original binary.
68344a3780SDimitry Andric 
69344a3780SDimitry Andric   assert(PSI && "PSI is expected to be non null");
70344a3780SDimitry Andric   uint64_t CallsiteTotalSamples = CallsiteFS->getTotalSamples();
71344a3780SDimitry Andric   if (ProfAccForSymsInList)
72344a3780SDimitry Andric     return !PSI->isColdCount(CallsiteTotalSamples);
73344a3780SDimitry Andric   else
74344a3780SDimitry Andric     return PSI->isHotCount(CallsiteTotalSamples);
75344a3780SDimitry Andric }
76344a3780SDimitry Andric 
77344a3780SDimitry Andric /// Mark as used the sample record for the given function samples at
78344a3780SDimitry Andric /// (LineOffset, Discriminator).
79344a3780SDimitry Andric ///
80344a3780SDimitry 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)81344a3780SDimitry Andric bool SampleCoverageTracker::markSamplesUsed(const FunctionSamples *FS,
82344a3780SDimitry Andric                                             uint32_t LineOffset,
83344a3780SDimitry Andric                                             uint32_t Discriminator,
84344a3780SDimitry Andric                                             uint64_t Samples) {
85344a3780SDimitry Andric   LineLocation Loc(LineOffset, Discriminator);
86344a3780SDimitry Andric   unsigned &Count = SampleCoverage[FS][Loc];
87344a3780SDimitry Andric   bool FirstTime = (++Count == 1);
88344a3780SDimitry Andric   if (FirstTime)
89344a3780SDimitry Andric     TotalUsedSamples += Samples;
90344a3780SDimitry Andric   return FirstTime;
91344a3780SDimitry Andric }
92344a3780SDimitry Andric 
93344a3780SDimitry Andric /// Return the number of sample records that were applied from this profile.
94344a3780SDimitry Andric ///
95344a3780SDimitry Andric /// This count does not include records from cold inlined callsites.
96344a3780SDimitry Andric unsigned
countUsedRecords(const FunctionSamples * FS,ProfileSummaryInfo * PSI) const97344a3780SDimitry Andric SampleCoverageTracker::countUsedRecords(const FunctionSamples *FS,
98344a3780SDimitry Andric                                         ProfileSummaryInfo *PSI) const {
99344a3780SDimitry Andric   auto I = SampleCoverage.find(FS);
100344a3780SDimitry Andric 
101344a3780SDimitry Andric   // The size of the coverage map for FS represents the number of records
102344a3780SDimitry Andric   // that were marked used at least once.
103344a3780SDimitry Andric   unsigned Count = (I != SampleCoverage.end()) ? I->second.size() : 0;
104344a3780SDimitry Andric 
105344a3780SDimitry Andric   // If there are inlined callsites in this function, count the samples found
106344a3780SDimitry Andric   // in the respective bodies. However, do not bother counting callees with 0
107344a3780SDimitry Andric   // total samples, these are callees that were never invoked at runtime.
108344a3780SDimitry Andric   for (const auto &I : FS->getCallsiteSamples())
109344a3780SDimitry Andric     for (const auto &J : I.second) {
110344a3780SDimitry Andric       const FunctionSamples *CalleeSamples = &J.second;
111344a3780SDimitry Andric       if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
112344a3780SDimitry Andric         Count += countUsedRecords(CalleeSamples, PSI);
113344a3780SDimitry Andric     }
114344a3780SDimitry Andric 
115344a3780SDimitry Andric   return Count;
116344a3780SDimitry Andric }
117344a3780SDimitry Andric 
118344a3780SDimitry Andric /// Return the number of sample records in the body of this profile.
119344a3780SDimitry Andric ///
120344a3780SDimitry Andric /// This count does not include records from cold inlined callsites.
121344a3780SDimitry Andric unsigned
countBodyRecords(const FunctionSamples * FS,ProfileSummaryInfo * PSI) const122344a3780SDimitry Andric SampleCoverageTracker::countBodyRecords(const FunctionSamples *FS,
123344a3780SDimitry Andric                                         ProfileSummaryInfo *PSI) const {
124344a3780SDimitry Andric   unsigned Count = FS->getBodySamples().size();
125344a3780SDimitry Andric 
126344a3780SDimitry Andric   // Only count records in hot callsites.
127344a3780SDimitry Andric   for (const auto &I : FS->getCallsiteSamples())
128344a3780SDimitry Andric     for (const auto &J : I.second) {
129344a3780SDimitry Andric       const FunctionSamples *CalleeSamples = &J.second;
130344a3780SDimitry Andric       if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
131344a3780SDimitry Andric         Count += countBodyRecords(CalleeSamples, PSI);
132344a3780SDimitry Andric     }
133344a3780SDimitry Andric 
134344a3780SDimitry Andric   return Count;
135344a3780SDimitry Andric }
136344a3780SDimitry Andric 
137344a3780SDimitry Andric /// Return the number of samples collected in the body of this profile.
138344a3780SDimitry Andric ///
139344a3780SDimitry Andric /// This count does not include samples from cold inlined callsites.
140344a3780SDimitry Andric uint64_t
countBodySamples(const FunctionSamples * FS,ProfileSummaryInfo * PSI) const141344a3780SDimitry Andric SampleCoverageTracker::countBodySamples(const FunctionSamples *FS,
142344a3780SDimitry Andric                                         ProfileSummaryInfo *PSI) const {
143344a3780SDimitry Andric   uint64_t Total = 0;
144344a3780SDimitry Andric   for (const auto &I : FS->getBodySamples())
145344a3780SDimitry Andric     Total += I.second.getSamples();
146344a3780SDimitry Andric 
147344a3780SDimitry Andric   // Only count samples in hot callsites.
148344a3780SDimitry Andric   for (const auto &I : FS->getCallsiteSamples())
149344a3780SDimitry Andric     for (const auto &J : I.second) {
150344a3780SDimitry Andric       const FunctionSamples *CalleeSamples = &J.second;
151344a3780SDimitry Andric       if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
152344a3780SDimitry Andric         Total += countBodySamples(CalleeSamples, PSI);
153344a3780SDimitry Andric     }
154344a3780SDimitry Andric 
155344a3780SDimitry Andric   return Total;
156344a3780SDimitry Andric }
157344a3780SDimitry Andric 
158344a3780SDimitry Andric /// Return the fraction of sample records used in this profile.
159344a3780SDimitry Andric ///
160344a3780SDimitry Andric /// The returned value is an unsigned integer in the range 0-100 indicating
161344a3780SDimitry Andric /// the percentage of sample records that were used while applying this
162344a3780SDimitry Andric /// profile to the associated function.
computeCoverage(unsigned Used,unsigned Total) const163344a3780SDimitry Andric unsigned SampleCoverageTracker::computeCoverage(unsigned Used,
164344a3780SDimitry Andric                                                 unsigned Total) const {
165344a3780SDimitry Andric   assert(Used <= Total &&
166344a3780SDimitry Andric          "number of used records cannot exceed the total number of records");
167344a3780SDimitry Andric   return Total > 0 ? Used * 100 / Total : 100;
168344a3780SDimitry Andric }
169344a3780SDimitry Andric 
170344a3780SDimitry Andric /// Create a global variable to flag FSDiscriminators are used.
createFSDiscriminatorVariable(Module * M)171344a3780SDimitry Andric void createFSDiscriminatorVariable(Module *M) {
172344a3780SDimitry Andric   const char *FSDiscriminatorVar = "__llvm_fs_discriminator__";
173344a3780SDimitry Andric   if (M->getGlobalVariable(FSDiscriminatorVar))
174344a3780SDimitry Andric     return;
175344a3780SDimitry Andric 
176344a3780SDimitry Andric   auto &Context = M->getContext();
177344a3780SDimitry Andric   // Place this variable to llvm.used so it won't be GC'ed.
178344a3780SDimitry Andric   appendToUsed(*M, {new GlobalVariable(*M, Type::getInt1Ty(Context), true,
179344a3780SDimitry Andric                                        GlobalValue::WeakODRLinkage,
180344a3780SDimitry Andric                                        ConstantInt::getTrue(Context),
181344a3780SDimitry Andric                                        FSDiscriminatorVar)});
182344a3780SDimitry Andric }
183344a3780SDimitry Andric 
184344a3780SDimitry Andric } // end of namespace sampleprofutil
185344a3780SDimitry Andric } // end of namespace llvm
186