28 "inliner-function-import-stats",
33 "printing of statistics for each inlined function")),
37ImportedFunctionsInliningStatistics::InlineGraphNode &
38ImportedFunctionsInliningStatistics::createInlineGraphNode(
const Function &
F) {
40 auto &ValueLookup = NodesMap[
F.getName()];
42 ValueLookup = std::make_unique<InlineGraphNode>();
43 ValueLookup->Imported =
F.hasMetadata(
"thinlto_src_module");
51 InlineGraphNode &CallerNode = createInlineGraphNode(Caller);
52 InlineGraphNode &CalleeNode = createInlineGraphNode(Callee);
53 CalleeNode.NumberOfInlines++;
55 if (!CallerNode.Imported && !CalleeNode.Imported) {
60 CalleeNode.NumberOfRealInlines++;
64 CallerNode.InlinedCallees.push_back(&CalleeNode);
65 if (!CallerNode.Imported) {
67 auto It = NodesMap.
find(Caller.getName());
68 assert(It != NodesMap.
end() &&
"The node should be already there.");
71 NonImportedCallers.push_back(It->first());
77 for (
const auto &
F : M.functions()) {
78 if (
F.isDeclaration())
81 ImportedFunctions += int(
F.hasMetadata(
"thinlto_src_module"));
85 const char *PercentageOfMsg,
86 bool LineEnd =
true) {
89 Result = 100 *
static_cast<double>(Fraction) /
All;
91 std::stringstream Str;
92 Str << std::setprecision(4) << Msg <<
": " << Fraction <<
" [" << Result
93 <<
"% of " << PercentageOfMsg <<
"]";
100 calculateRealInlines();
101 NonImportedCallers.clear();
103 int32_t InlinedImportedFunctionsCount = 0;
104 int32_t InlinedNotImportedFunctionsCount = 0;
106 int32_t InlinedImportedFunctionsToImportingModuleCount = 0;
107 int32_t InlinedNotImportedFunctionsToImportingModuleCount = 0;
109 const auto SortedNodes = getSortedNodes();
114 Ostream <<
"------- Dumping inliner stats for [" <<
ModuleName
118 Ostream <<
"-- List of inlined functions:\n";
120 for (
const auto &Node : SortedNodes) {
121 assert(Node->second->NumberOfInlines >= Node->second->NumberOfRealInlines);
122 if (Node->second->NumberOfInlines == 0)
125 if (Node->second->Imported) {
126 InlinedImportedFunctionsCount++;
127 InlinedImportedFunctionsToImportingModuleCount +=
128 int(Node->second->NumberOfRealInlines > 0);
130 InlinedNotImportedFunctionsCount++;
131 InlinedNotImportedFunctionsToImportingModuleCount +=
132 int(Node->second->NumberOfRealInlines > 0);
136 Ostream <<
"Inlined "
137 << (Node->second->Imported ?
"imported " :
"not imported ")
138 <<
"function [" << Node->first() <<
"]"
139 <<
": #inlines = " << Node->second->NumberOfInlines
140 <<
", #inlines_to_importing_module = "
141 << Node->second->NumberOfRealInlines <<
"\n";
144 auto InlinedFunctionsCount =
145 InlinedImportedFunctionsCount + InlinedNotImportedFunctionsCount;
146 auto NotImportedFuncCount = AllFunctions - ImportedFunctions;
147 auto ImportedNotInlinedIntoModule =
148 ImportedFunctions - InlinedImportedFunctionsToImportingModuleCount;
150 Ostream <<
"-- Summary:\n"
151 <<
"All functions: " << AllFunctions
152 <<
", imported functions: " << ImportedFunctions <<
"\n"
154 AllFunctions,
"all functions")
156 InlinedImportedFunctionsCount, ImportedFunctions,
157 "imported functions")
158 <<
getStatString(
"imported functions inlined into importing module",
159 InlinedImportedFunctionsToImportingModuleCount,
160 ImportedFunctions,
"imported functions",
162 <<
getStatString(
", remaining", ImportedNotInlinedIntoModule,
163 ImportedFunctions,
"imported functions")
165 InlinedNotImportedFunctionsCount,
166 NotImportedFuncCount,
"non-imported functions")
168 "non-imported functions inlined into importing module",
169 InlinedNotImportedFunctionsToImportingModuleCount,
170 NotImportedFuncCount,
"non-imported functions");
175void ImportedFunctionsInliningStatistics::calculateRealInlines() {
178 NonImportedCallers.erase(
llvm::unique(NonImportedCallers),
179 NonImportedCallers.end());
181 for (
const auto &
Name : NonImportedCallers) {
182 auto &Node = *NodesMap[
Name];
188void ImportedFunctionsInliningStatistics::dfs(InlineGraphNode &GraphNode) {
189 assert(!GraphNode.Visited);
190 GraphNode.Visited =
true;
191 for (
auto *
const InlinedFunctionNode : GraphNode.InlinedCallees) {
192 InlinedFunctionNode->NumberOfRealInlines++;
193 if (!InlinedFunctionNode->Visited)
194 dfs(*InlinedFunctionNode);
198ImportedFunctionsInliningStatistics::SortedNodesTy
199ImportedFunctionsInliningStatistics::getSortedNodes() {
200 SortedNodesTy SortedNodes;
201 SortedNodes.reserve(NodesMap.
size());
203 SortedNodes.push_back(&
Node);
205 llvm::sort(SortedNodes, [&](
const SortedNodesTy::value_type &Lhs,
206 const SortedNodesTy::value_type &Rhs) {
207 if (Lhs->second->NumberOfInlines != Rhs->second->NumberOfInlines)
208 return Lhs->second->NumberOfInlines > Rhs->second->NumberOfInlines;
209 if (Lhs->second->NumberOfRealInlines != Rhs->second->NumberOfRealInlines)
210 return Lhs->second->NumberOfRealInlines >
211 Rhs->second->NumberOfRealInlines;
212 return Lhs->first() < Rhs->first();
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
static std::string getStatString(const char *Msg, int32_t Fraction, int32_t All, const char *PercentageOfMsg, bool LineEnd=true)
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void setModuleInfo(const Module &M)
Set information like AllFunctions, ImportedFunctions, ModuleName.
void dump(bool Verbose)
Dump stats computed with InlinerStatistics class.
void recordInline(const Function &Caller, const Function &Callee)
Record inline of.
A Module instance is used to store all the information related to an LLVM module.
iterator find(StringRef Key)
StringMapEntry< std::unique_ptr< InlineGraphNode > > value_type
A raw_ostream that writes to an std::string.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
auto unique(Range &&R, Predicate P)
void sort(IteratorTy Start, IteratorTy End)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
cl::opt< InlinerFunctionImportStatsOpts > InlinerFunctionImportStats("inliner-function-import-stats", cl::init(InlinerFunctionImportStatsOpts::No), cl::values(clEnumValN(InlinerFunctionImportStatsOpts::Basic, "basic", "basic statistics"), clEnumValN(InlinerFunctionImportStatsOpts::Verbose, "verbose", "printing of statistics for each inlined function")), cl::Hidden, cl::desc("Enable inliner stats for imported functions"))