Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
SampleProfileLoaderBaseUtil.cpp
Go to the documentation of this file.
1//===- SampleProfileLoaderBaseUtil.cpp - Profile loader Util func ---------===//
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 the SampleProfileLoader base utility functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Transforms/Utils/SampleProfileLoaderBaseUtil.h"
14#include "llvm/Analysis/ProfileSummaryInfo.h"
15#include "llvm/IR/Constants.h"
16#include "llvm/IR/Module.h"
17#include "llvm/Transforms/Utils/ModuleUtils.h"
18
19namespacellvm {
20
21cl::opt<unsigned>SampleProfileMaxPropagateIterations(
22"sample-profile-max-propagate-iterations",cl::init(100),
23cl::desc("Maximum number of iterations to go through when propagating "
24"sample block/edge weights through the CFG."));
25
26cl::opt<unsigned>SampleProfileRecordCoverage(
27"sample-profile-check-record-coverage",cl::init(0),cl::value_desc("N"),
28cl::desc("Emit a warning if less than N% of records in the input profile "
29"are matched to the IR."));
30
31cl::opt<unsigned>SampleProfileSampleCoverage(
32"sample-profile-check-sample-coverage",cl::init(0),cl::value_desc("N"),
33cl::desc("Emit a warning if less than N% of samples in the input profile "
34"are matched to the IR."));
35
36cl::opt<bool>NoWarnSampleUnused(
37"no-warn-sample-unused",cl::init(false),cl::Hidden,
38cl::desc("Use this option to turn off/on warnings about function with "
39"samples but without debug information to use those samples. "));
40
41cl::opt<bool>SampleProfileUseProfi(
42"sample-profile-use-profi",cl::Hidden,
43cl::desc("Use profi to infer block and edge counts."));
44
45namespacesampleprofutil {
46
47/// Return true if the given callsite is hot wrt to hot cutoff threshold.
48///
49/// Functions that were inlined in the original binary will be represented
50/// in the inline stack in the sample profile. If the profile shows that
51/// the original inline decision was "good" (i.e., the callsite is executed
52/// frequently), then we will recreate the inline decision and apply the
53/// profile from the inlined callsite.
54///
55/// To decide whether an inlined callsite is hot, we compare the callsite
56/// sample count with the hot cutoff computed by ProfileSummaryInfo, it is
57/// regarded as hot if the count is above the cutoff value.
58///
59/// When ProfileAccurateForSymsInList is enabled and profile symbol list
60/// is present, functions in the profile symbol list but without profile will
61/// be regarded as cold and much less inlining will happen in CGSCC inlining
62/// pass, so we tend to lower the hot criteria here to allow more early
63/// inlining to happen for warm callsites and it is helpful for performance.
64boolcallsiteIsHot(constFunctionSamples *CallsiteFS,ProfileSummaryInfo *PSI,
65bool ProfAccForSymsInList) {
66if (!CallsiteFS)
67returnfalse;// The callsite was not inlined in the original binary.
68
69assert(PSI &&"PSI is expected to be non null");
70uint64_t CallsiteTotalSamples = CallsiteFS->getTotalSamples();
71if (ProfAccForSymsInList)
72return !PSI->isColdCount(CallsiteTotalSamples);
73else
74return PSI->isHotCount(CallsiteTotalSamples);
75}
76
77/// Mark as used the sample record for the given function samples at
78/// (LineOffset, Discriminator).
79///
80/// \returns true if this is the first time we mark the given record.
81boolSampleCoverageTracker::markSamplesUsed(constFunctionSamples *FS,
82uint32_t LineOffset,
83uint32_t Discriminator,
84uint64_t Samples) {
85LineLocation Loc(LineOffset, Discriminator);
86unsigned &Count = SampleCoverage[FS][Loc];
87bool FirstTime = (++Count == 1);
88if (FirstTime)
89 TotalUsedSamples += Samples;
90return FirstTime;
91}
92
93/// Return the number of sample records that were applied from this profile.
94///
95/// This count does not include records from cold inlined callsites.
96unsigned
97SampleCoverageTracker::countUsedRecords(constFunctionSamples *FS,
98ProfileSummaryInfo *PSI) const{
99autoI = SampleCoverage.find(FS);
100
101// The size of the coverage map for FS represents the number of records
102// that were marked used at least once.
103unsigned Count = (I != SampleCoverage.end()) ?I->second.size() : 0;
104
105// If there are inlined callsites in this function, count the samples found
106// in the respective bodies. However, do not bother counting callees with 0
107// total samples, these are callees that were never invoked at runtime.
108for (constauto &I : FS->getCallsiteSamples())
109for (constauto &J :I.second) {
110constFunctionSamples *CalleeSamples = &J.second;
111if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
112 Count +=countUsedRecords(CalleeSamples, PSI);
113 }
114
115return Count;
116}
117
118/// Return the number of sample records in the body of this profile.
119///
120/// This count does not include records from cold inlined callsites.
121unsigned
122SampleCoverageTracker::countBodyRecords(constFunctionSamples *FS,
123ProfileSummaryInfo *PSI) const{
124unsigned Count = FS->getBodySamples().size();
125
126// Only count records in hot callsites.
127for (constauto &I : FS->getCallsiteSamples())
128for (constauto &J :I.second) {
129constFunctionSamples *CalleeSamples = &J.second;
130if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
131 Count +=countBodyRecords(CalleeSamples, PSI);
132 }
133
134return Count;
135}
136
137/// Return the number of samples collected in the body of this profile.
138///
139/// This count does not include samples from cold inlined callsites.
140uint64_t
141SampleCoverageTracker::countBodySamples(constFunctionSamples *FS,
142ProfileSummaryInfo *PSI) const{
143uint64_tTotal = 0;
144for (constauto &I : FS->getBodySamples())
145Total +=I.second.getSamples();
146
147// Only count samples in hot callsites.
148for (constauto &I : FS->getCallsiteSamples())
149for (constauto &J :I.second) {
150constFunctionSamples *CalleeSamples = &J.second;
151if (callsiteIsHot(CalleeSamples, PSI, ProfAccForSymsInList))
152Total +=countBodySamples(CalleeSamples, PSI);
153 }
154
155returnTotal;
156}
157
158/// Return the fraction of sample records used in this profile.
159///
160/// The returned value is an unsigned integer in the range 0-100 indicating
161/// the percentage of sample records that were used while applying this
162/// profile to the associated function.
163unsignedSampleCoverageTracker::computeCoverage(unsigned Used,
164unsignedTotal) const{
165assert(Used <=Total &&
166"number of used records cannot exceed the total number of records");
167returnTotal > 0 ? Used * 100 /Total : 100;
168}
169
170/// Create a global variable to flag FSDiscriminators are used.
171voidcreateFSDiscriminatorVariable(Module *M) {
172constchar *FSDiscriminatorVar ="__llvm_fs_discriminator__";
173if (M->getGlobalVariable(FSDiscriminatorVar))
174return;
175
176auto &Context = M->getContext();
177// Place this variable to llvm.used so it won't be GC'ed.
178appendToUsed(*M, {newGlobalVariable(*M,Type::getInt1Ty(Context),true,
179GlobalValue::WeakODRLinkage,
180ConstantInt::getTrue(Context),
181 FSDiscriminatorVar)});
182}
183
184}// end of namespace sampleprofutil
185}// end of namespace llvm
Constants.h
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h
Module.h This file contains the declarations for the Module class.
I
#define I(x, y, z)
Definition:MD5.cpp:58
ModuleUtils.h
ProfileSummaryInfo.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SampleProfileLoaderBaseUtil.h
This file provides the utility functions for the sampled PGO loader base implementation.
llvm::ConstantInt::getTrue
static ConstantInt * getTrue(LLVMContext &Context)
Definition:Constants.cpp:866
llvm::DenseMapBase::find
iterator find(const_arg_type_t< KeyT > Val)
Definition:DenseMap.h:156
llvm::DenseMapBase::end
iterator end()
Definition:DenseMap.h:84
llvm::GlobalValue::WeakODRLinkage
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition:GlobalValue.h:57
llvm::GlobalVariable
Definition:GlobalVariable.h:39
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition:Module.h:65
llvm::ProfileSummaryInfo
Analysis providing profile information.
Definition:ProfileSummaryInfo.h:41
llvm::ProfileSummaryInfo::isColdCount
bool isColdCount(uint64_t C) const
Returns true if count C is considered cold.
Definition:ProfileSummaryInfo.cpp:168
llvm::ProfileSummaryInfo::isHotCount
bool isHotCount(uint64_t C) const
Returns true if count C is considered hot.
Definition:ProfileSummaryInfo.cpp:164
llvm::Type::getInt1Ty
static IntegerType * getInt1Ty(LLVMContext &C)
llvm::cl::opt
Definition:CommandLine.h:1423
llvm::sampleprof::FunctionSamples
Representation of the samples collected for a function.
Definition:SampleProf.h:745
llvm::sampleprof::FunctionSamples::getTotalSamples
uint64_t getTotalSamples() const
Return the total number of samples collected inside the function.
Definition:SampleProf.h:934
llvm::sampleprofutil::SampleCoverageTracker::countBodyRecords
unsigned countBodyRecords(const FunctionSamples *FS, ProfileSummaryInfo *PSI) const
Return the number of sample records in the body of this profile.
Definition:SampleProfileLoaderBaseUtil.cpp:122
llvm::sampleprofutil::SampleCoverageTracker::countUsedRecords
unsigned countUsedRecords(const FunctionSamples *FS, ProfileSummaryInfo *PSI) const
Return the number of sample records that were applied from this profile.
Definition:SampleProfileLoaderBaseUtil.cpp:97
llvm::sampleprofutil::SampleCoverageTracker::computeCoverage
unsigned computeCoverage(unsigned Used, unsigned Total) const
Return the fraction of sample records used in this profile.
Definition:SampleProfileLoaderBaseUtil.cpp:163
llvm::sampleprofutil::SampleCoverageTracker::countBodySamples
uint64_t countBodySamples(const FunctionSamples *FS, ProfileSummaryInfo *PSI) const
Return the number of samples collected in the body of this profile.
Definition:SampleProfileLoaderBaseUtil.cpp:141
llvm::sampleprofutil::SampleCoverageTracker::markSamplesUsed
bool markSamplesUsed(const FunctionSamples *FS, uint32_t LineOffset, uint32_t Discriminator, uint64_t Samples)
Mark as used the sample record for the given function samples at (LineOffset, Discriminator).
Definition:SampleProfileLoaderBaseUtil.cpp:81
uint32_t
uint64_t
llvm::cl::Hidden
@ Hidden
Definition:CommandLine.h:137
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition:CommandLine.h:443
llvm::sampleprofutil::createFSDiscriminatorVariable
void createFSDiscriminatorVariable(Module *M)
Create a global variable to flag FSDiscriminators are used.
Definition:SampleProfileLoaderBaseUtil.cpp:171
llvm::sampleprofutil::callsiteIsHot
bool callsiteIsHot(const FunctionSamples *CallsiteFS, ProfileSummaryInfo *PSI, bool ProfAccForSymsInList)
Return true if the given callsite is hot wrt to hot cutoff threshold.
Definition:SampleProfileLoaderBaseUtil.cpp:64
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::SampleProfileSampleCoverage
cl::opt< unsigned > SampleProfileSampleCoverage
llvm::SampleProfileRecordCoverage
cl::opt< unsigned > SampleProfileRecordCoverage
llvm::SampleProfileMaxPropagateIterations
cl::opt< unsigned > SampleProfileMaxPropagateIterations
llvm::SampleProfileUseProfi
cl::opt< bool > SampleProfileUseProfi
llvm::NoWarnSampleUnused
cl::opt< bool > NoWarnSampleUnused
llvm::appendToUsed
void appendToUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.used list.
Definition:ModuleUtils.cpp:157
llvm::TensorType::Total
@ Total
llvm::cl::desc
Definition:CommandLine.h:409
llvm::cl::value_desc
Definition:CommandLine.h:418
llvm::sampleprof::LineLocation
Represents the relative location of an instruction.
Definition:SampleProf.h:280

Generated on Fri Jul 18 2025 16:40:18 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp