Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
ImportedFunctionsInliningStatistics.cpp
Go to the documentation of this file.
1//===-- ImportedFunctionsInliningStats.cpp ----------------------*- C++ -*-===//
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// Generating inliner statistics for imported functions, mostly useful for
9// ThinLTO.
10//===----------------------------------------------------------------------===//
11
12#include "llvm/Analysis/Utils/ImportedFunctionsInliningStatistics.h"
13#include "llvm/ADT/STLExtras.h"
14#include "llvm/IR/Function.h"
15#include "llvm/IR/Module.h"
16#include "llvm/Support/CommandLine.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
19#include <iomanip>
20#include <sstream>
21#include <string>
22
23using namespacellvm;
24
25namespacellvm {
26cl::opt<InlinerFunctionImportStatsOpts>InlinerFunctionImportStats(
27"inliner-function-import-stats",
28cl::init(InlinerFunctionImportStatsOpts::No),
29cl::values(clEnumValN(InlinerFunctionImportStatsOpts::Basic,"basic",
30"basic statistics"),
31clEnumValN(InlinerFunctionImportStatsOpts::Verbose,"verbose",
32"printing of statistics for each inlined function")),
33cl::Hidden,cl::desc("Enable inliner stats for imported functions"));
34}// namespace llvm
35
36ImportedFunctionsInliningStatistics::InlineGraphNode &
37ImportedFunctionsInliningStatistics::createInlineGraphNode(constFunction &F) {
38
39auto &ValueLookup = NodesMap[F.getName()];
40if (!ValueLookup) {
41 ValueLookup = std::make_unique<InlineGraphNode>();
42 ValueLookup->Imported =F.hasMetadata("thinlto_src_module");
43 }
44return *ValueLookup;
45}
46
47voidImportedFunctionsInliningStatistics::recordInline(constFunction &Caller,
48constFunction &Callee) {
49
50 InlineGraphNode &CallerNode = createInlineGraphNode(Caller);
51 InlineGraphNode &CalleeNode = createInlineGraphNode(Callee);
52 CalleeNode.NumberOfInlines++;
53
54if (!CallerNode.Imported && !CalleeNode.Imported) {
55// Direct inline from not imported callee to not imported caller, so we
56// don't have to add this to graph. It might be very helpful if you wanna
57// get the inliner statistics in compile step where there are no imported
58// functions. In this case the graph would be empty.
59 CalleeNode.NumberOfRealInlines++;
60return;
61 }
62
63 CallerNode.InlinedCallees.push_back(&CalleeNode);
64if (!CallerNode.Imported) {
65// We could avoid second lookup, but it would make the code ultra ugly.
66auto It = NodesMap.find(Caller.getName());
67assert(It != NodesMap.end() &&"The node should be already there.");
68// Save Caller as a starting node for traversal. The string has to be one
69// from map because Caller can disappear (and function name with it).
70 NonImportedCallers.push_back(It->first());
71 }
72}
73
74voidImportedFunctionsInliningStatistics::setModuleInfo(constModule &M) {
75ModuleName = M.getName();
76for (constauto &F : M.functions()) {
77if (F.isDeclaration())
78continue;
79 AllFunctions++;
80 ImportedFunctions += int(F.hasMetadata("thinlto_src_module"));
81 }
82}
83static std::stringgetStatString(constchar *Msg, int32_t Fraction, int32_tAll,
84constchar *PercentageOfMsg,
85bool LineEnd =true) {
86double Result = 0;
87if (All != 0)
88 Result = 100 *static_cast<double>(Fraction) /All;
89
90 std::stringstream Str;
91 Str << std::setprecision(4) << Msg <<": " << Fraction <<" [" << Result
92 <<"% of " << PercentageOfMsg <<"]";
93if (LineEnd)
94 Str <<"\n";
95return Str.str();
96}
97
98voidImportedFunctionsInliningStatistics::dump(constboolVerbose) {
99 calculateRealInlines();
100 NonImportedCallers.clear();
101
102 int32_t InlinedImportedFunctionsCount = 0;
103 int32_t InlinedNotImportedFunctionsCount = 0;
104
105 int32_t InlinedImportedFunctionsToImportingModuleCount = 0;
106 int32_t InlinedNotImportedFunctionsToImportingModuleCount = 0;
107
108constauto SortedNodes = getSortedNodes();
109 std::string Out;
110 Out.reserve(5000);
111raw_string_ostream Ostream(Out);
112
113 Ostream <<"------- Dumping inliner stats for [" <<ModuleName
114 <<"] -------\n";
115
116if (Verbose)
117 Ostream <<"-- List of inlined functions:\n";
118
119for (constauto &Node : SortedNodes) {
120assert(Node->second->NumberOfInlines >= Node->second->NumberOfRealInlines);
121if (Node->second->NumberOfInlines == 0)
122continue;
123
124if (Node->second->Imported) {
125 InlinedImportedFunctionsCount++;
126 InlinedImportedFunctionsToImportingModuleCount +=
127 int(Node->second->NumberOfRealInlines > 0);
128 }else {
129 InlinedNotImportedFunctionsCount++;
130 InlinedNotImportedFunctionsToImportingModuleCount +=
131 int(Node->second->NumberOfRealInlines > 0);
132 }
133
134if (Verbose)
135 Ostream <<"Inlined "
136 << (Node->second->Imported ?"imported " :"not imported ")
137 <<"function [" << Node->first() <<"]"
138 <<": #inlines = " << Node->second->NumberOfInlines
139 <<", #inlines_to_importing_module = "
140 << Node->second->NumberOfRealInlines <<"\n";
141 }
142
143auto InlinedFunctionsCount =
144 InlinedImportedFunctionsCount + InlinedNotImportedFunctionsCount;
145auto NotImportedFuncCount = AllFunctions - ImportedFunctions;
146auto ImportedNotInlinedIntoModule =
147 ImportedFunctions - InlinedImportedFunctionsToImportingModuleCount;
148
149 Ostream <<"-- Summary:\n"
150 <<"All functions: " << AllFunctions
151 <<", imported functions: " << ImportedFunctions <<"\n"
152 <<getStatString("inlined functions", InlinedFunctionsCount,
153 AllFunctions,"all functions")
154 <<getStatString("imported functions inlined anywhere",
155 InlinedImportedFunctionsCount, ImportedFunctions,
156"imported functions")
157 <<getStatString("imported functions inlined into importing module",
158 InlinedImportedFunctionsToImportingModuleCount,
159 ImportedFunctions,"imported functions",
160/*LineEnd=*/false)
161 <<getStatString(", remaining", ImportedNotInlinedIntoModule,
162 ImportedFunctions,"imported functions")
163 <<getStatString("non-imported functions inlined anywhere",
164 InlinedNotImportedFunctionsCount,
165 NotImportedFuncCount,"non-imported functions")
166 <<getStatString(
167"non-imported functions inlined into importing module",
168 InlinedNotImportedFunctionsToImportingModuleCount,
169 NotImportedFuncCount,"non-imported functions");
170 Ostream.flush();
171dbgs() << Out;
172}
173
174void ImportedFunctionsInliningStatistics::calculateRealInlines() {
175// Removing duplicated Callers.
176llvm::sort(NonImportedCallers);
177 NonImportedCallers.erase(llvm::unique(NonImportedCallers),
178 NonImportedCallers.end());
179
180for (constauto &Name : NonImportedCallers) {
181auto &Node = *NodesMap[Name];
182if (!Node.Visited)
183 dfs(Node);
184 }
185}
186
187void ImportedFunctionsInliningStatistics::dfs(InlineGraphNode &GraphNode) {
188assert(!GraphNode.Visited);
189 GraphNode.Visited =true;
190for (auto *const InlinedFunctionNode : GraphNode.InlinedCallees) {
191 InlinedFunctionNode->NumberOfRealInlines++;
192if (!InlinedFunctionNode->Visited)
193 dfs(*InlinedFunctionNode);
194 }
195}
196
197ImportedFunctionsInliningStatistics::SortedNodesTy
198ImportedFunctionsInliningStatistics::getSortedNodes() {
199 SortedNodesTy SortedNodes;
200 SortedNodes.reserve(NodesMap.size());
201for (constNodesMapTy::value_type &Node : NodesMap)
202 SortedNodes.push_back(&Node);
203
204llvm::sort(SortedNodes, [&](const SortedNodesTy::value_type &Lhs,
205const SortedNodesTy::value_type &Rhs) {
206if (Lhs->second->NumberOfInlines != Rhs->second->NumberOfInlines)
207return Lhs->second->NumberOfInlines > Rhs->second->NumberOfInlines;
208if (Lhs->second->NumberOfRealInlines != Rhs->second->NumberOfRealInlines)
209return Lhs->second->NumberOfRealInlines >
210 Rhs->second->NumberOfRealInlines;
211return Lhs->first() < Rhs->first();
212 });
213return SortedNodes;
214}
CommandLine.h
clEnumValN
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition:CommandLine.h:686
Debug.h
Name
std::string Name
Definition:ELFObjHandler.cpp:77
Function.h
Module.h
Module.h This file contains the declarations for the Module class.
getStatString
static std::string getStatString(const char *Msg, int32_t Fraction, int32_t All, const char *PercentageOfMsg, bool LineEnd=true)
Definition:ImportedFunctionsInliningStatistics.cpp:83
ImportedFunctionsInliningStatistics.h
F
#define F(x, y, z)
Definition:MD5.cpp:55
if
if(PassOpts->AAPipeline)
Definition:PassBuilderBindings.cpp:64
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
STLExtras.h
This file contains some templates that are useful if you are working with the STL at all.
Node
Definition:ItaniumDemangle.h:163
llvm::Function
Definition:Function.h:63
llvm::ImportedFunctionsInliningStatistics::setModuleInfo
void setModuleInfo(const Module &M)
Set information like AllFunctions, ImportedFunctions, ModuleName.
Definition:ImportedFunctionsInliningStatistics.cpp:74
llvm::ImportedFunctionsInliningStatistics::dump
void dump(bool Verbose)
Dump stats computed with InlinerStatistics class.
Definition:ImportedFunctionsInliningStatistics.cpp:98
llvm::ImportedFunctionsInliningStatistics::recordInline
void recordInline(const Function &Caller, const Function &Callee)
Record inline of.
Definition:ImportedFunctionsInliningStatistics.cpp:47
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition:Module.h:65
llvm::StringMapImpl::size
unsigned size() const
Definition:StringMap.h:104
llvm::StringMap::end
iterator end()
Definition:StringMap.h:220
llvm::StringMap::find
iterator find(StringRef Key)
Definition:StringMap.h:233
llvm::StringMap< std::unique_ptr< InlineGraphNode > >::value_type
StringMapEntry< std::unique_ptr< InlineGraphNode > > value_type
Definition:StringMap.h:213
llvm::cl::opt
Definition:CommandLine.h:1423
llvm::raw_ostream::flush
void flush()
Definition:raw_ostream.h:198
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition:raw_ostream.h:661
llvm::cl::Hidden
@ Hidden
Definition:CommandLine.h:137
llvm::cl::values
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition:CommandLine.h:711
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition:CommandLine.h:443
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::AllocationType::All
@ All
llvm::unique
auto unique(Range &&R, Predicate P)
Definition:STLExtras.h:2055
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition:STLExtras.h:1664
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition:Debug.cpp:163
llvm::InlinerFunctionImportStats
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"))
Definition:InlineAdvisor.cpp:67
llvm::InlinerFunctionImportStatsOpts::Basic
@ Basic
llvm::InlinerFunctionImportStatsOpts::No
@ No
llvm::InlinerFunctionImportStatsOpts::Verbose
@ Verbose
raw_ostream.h
ModuleName
Definition:ItaniumDemangle.h:1097
llvm::cl::desc
Definition:CommandLine.h:409

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

©2009-2025 Movatter.jp