LLVM 23.0.0git
StaticDataSplitter.cpp
Go to the documentation of this file.
1//===- StaticDataSplitter.cpp ---------------------------------------------===//
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// The pass uses branch profile data to assign hotness based section qualifiers
10// for the following types of static data:
11// - Jump tables
12// - Module-internal global variables
13// - Constant pools
14//
15// For the original RFC of this pass please see
16// https://discourse.llvm.org/t/rfc-profile-guided-static-data-partitioning/83744
17
18#include "llvm/ADT/Statistic.h"
29#include "llvm/CodeGen/Passes.h"
32#include "llvm/Pass.h"
34
35using namespace llvm;
36
37#define DEBUG_TYPE "static-data-splitter"
38
39STATISTIC(NumHotJumpTables, "Number of hot jump tables seen.");
40STATISTIC(NumColdJumpTables, "Number of cold jump tables seen.");
41STATISTIC(NumUnknownJumpTables,
42 "Number of jump tables with unknown hotness. They are from functions "
43 "without profile information.");
44
46 const MachineBranchProbabilityInfo *MBPI = nullptr;
47 const MachineBlockFrequencyInfo *MBFI = nullptr;
48 const ProfileSummaryInfo *PSI = nullptr;
49 StaticDataProfileInfo *SDPI = nullptr;
50
51 // If the global value is a local linkage global variable, return it.
52 // Otherwise, return nullptr.
53 const GlobalVariable *getLocalLinkageGlobalVariable(const GlobalValue *GV);
54
55 // Returns true if the global variable is in one of {.rodata, .bss, .data,
56 // .data.rel.ro} sections.
57 bool inStaticDataSection(const GlobalVariable &GV, const TargetMachine &TM);
58
59 // Returns the constant if the operand refers to a global variable or constant
60 // that gets lowered to static data sections. Otherwise, return nullptr.
61 const Constant *getConstant(const MachineOperand &Op, const TargetMachine &TM,
62 const MachineConstantPool *MCP);
63
64 // Use profiles to partition static data.
65 bool partitionStaticDataWithProfiles(MachineFunction &MF);
66
67 // Update LLVM statistics for a machine function with profiles.
68 void updateStatsWithProfiles(const MachineFunction &MF);
69
70 // Update LLVM statistics for a machine function without profiles.
71 void updateStatsWithoutProfiles(const MachineFunction &MF);
72
73 void annotateStaticDataWithoutProfiles(const MachineFunction &MF);
74
75public:
76 static char ID;
77
79
80 StringRef getPassName() const override { return "Static Data Splitter"; }
81
82 void getAnalysisUsage(AnalysisUsage &AU) const override {
88 // This pass does not modify any required analysis results except
89 // StaticDataProfileInfoWrapperPass, but StaticDataProfileInfoWrapperPass
90 // is made an immutable pass that it won't be re-scheduled by pass manager
91 // anyway. So mark setPreservesAll() here for faster compile time.
92 AU.setPreservesAll();
93 }
94
95 bool runOnMachineFunction(MachineFunction &MF) override;
96};
97
102
104 .getStaticDataProfileInfo();
105
106 const bool ProfileAvailable = PSI && PSI->hasProfileSummary() && MBFI &&
108
109 if (!ProfileAvailable) {
110 annotateStaticDataWithoutProfiles(MF);
111 updateStatsWithoutProfiles(MF);
112 return false;
113 }
114
115 bool Changed = partitionStaticDataWithProfiles(MF);
116
117 updateStatsWithProfiles(MF);
118 return Changed;
119}
120
121const Constant *
122StaticDataSplitter::getConstant(const MachineOperand &Op,
123 const TargetMachine &TM,
124 const MachineConstantPool *MCP) {
125 if (!Op.isGlobal() && !Op.isCPI())
126 return nullptr;
127
128 if (Op.isGlobal()) {
129 // Find global variables with local linkage.
130 const GlobalVariable *GV = getLocalLinkageGlobalVariable(Op.getGlobal());
131 // Skip those not eligible for annotation or not in static data sections.
132 if (!GV || !llvm::memprof::IsAnnotationOK(*GV) ||
133 !inStaticDataSection(*GV, TM))
134 return nullptr;
135 return GV;
136 }
137 assert(Op.isCPI() && "Op must be constant pool index in this branch");
138 int CPI = Op.getIndex();
139 if (CPI == -1)
140 return nullptr;
141
142 assert(MCP != nullptr && "Constant pool info is not available.");
143 const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
144
146 return nullptr;
147
148 return CPE.Val.ConstVal;
149}
150
151bool StaticDataSplitter::partitionStaticDataWithProfiles(MachineFunction &MF) {
152 // If any of the static data (jump tables, global variables, constant pools)
153 // are captured by the analysis, set `Changed` to true. Note this pass won't
154 // invalidate any analysis pass (see `getAnalysisUsage` above), so the main
155 // purpose of tracking and conveying the change (to pass manager) is
156 // informative as opposed to invalidating any analysis results. As an example
157 // of where this information is useful, `PMDataManager::dumpPassInfo` will
158 // only dump pass info if a local change happens, otherwise a pass appears as
159 // "skipped".
160 bool Changed = false;
161
162 MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
163
164 // Jump table could be used by either terminating instructions or
165 // non-terminating ones, so we walk all instructions and use
166 // `MachineOperand::isJTI()` to identify jump table operands.
167 // Similarly, `MachineOperand::isCPI()` is used to identify constant pool
168 // usages in the same loop.
169 for (const auto &MBB : MF) {
170 std::optional<uint64_t> Count = MBFI->getBlockProfileCount(&MBB);
171 for (const MachineInstr &I : MBB) {
172 for (const MachineOperand &Op : I.operands()) {
173 if (!Op.isJTI() && !Op.isGlobal() && !Op.isCPI())
174 continue;
175
176 if (Op.isJTI()) {
177 assert(MJTI != nullptr && "Jump table info is not available.");
178 const int JTI = Op.getIndex();
179 // This is not a source block of jump table.
180 if (JTI == -1)
181 continue;
182
183 auto Hotness = MachineFunctionDataHotness::Hot;
184
185 // Hotness is based on source basic block hotness.
186 // TODO: PSI APIs are about instruction hotness. Introduce API for
187 // data access hotness.
188 if (Count && PSI->isColdCount(*Count))
189 Hotness = MachineFunctionDataHotness::Cold;
190
191 Changed |= MJTI->updateJumpTableEntryHotness(JTI, Hotness);
192 } else if (const Constant *C =
193 getConstant(Op, MF.getTarget(), MF.getConstantPool())) {
194 SDPI->addConstantProfileCount(C, Count);
195 Changed = true;
196 }
197 }
198 }
199 }
200 return Changed;
201}
202
203const GlobalVariable *
204StaticDataSplitter::getLocalLinkageGlobalVariable(const GlobalValue *GV) {
205 // LLVM IR Verifier requires that a declaration must have valid declaration
206 // linkage, and local linkages are not among the valid ones. So there is no
207 // need to check GV is not a declaration here.
208 return (GV && GV->hasLocalLinkage()) ? dyn_cast<GlobalVariable>(GV) : nullptr;
209}
210
211bool StaticDataSplitter::inStaticDataSection(const GlobalVariable &GV,
212 const TargetMachine &TM) {
213
214 SectionKind Kind = TargetLoweringObjectFile::getKindForGlobal(&GV, TM);
215 return Kind.isData() || Kind.isReadOnly() || Kind.isReadOnlyWithRel() ||
216 Kind.isBSS();
217}
218
219void StaticDataSplitter::updateStatsWithProfiles(const MachineFunction &MF) {
221 return;
222
223 if (const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo()) {
224 for (const auto &JumpTable : MJTI->getJumpTables()) {
225 if (JumpTable.Hotness == MachineFunctionDataHotness::Hot) {
226 ++NumHotJumpTables;
227 } else {
228 assert(JumpTable.Hotness == MachineFunctionDataHotness::Cold &&
229 "A jump table is either hot or cold when profile information is "
230 "available.");
231 ++NumColdJumpTables;
232 }
233 }
234 }
235}
236
237void StaticDataSplitter::annotateStaticDataWithoutProfiles(
238 const MachineFunction &MF) {
239 for (const auto &MBB : MF)
240 for (const MachineInstr &I : MBB)
241 for (const MachineOperand &Op : I.operands())
242 if (const Constant *C =
243 getConstant(Op, MF.getTarget(), MF.getConstantPool()))
244 SDPI->addConstantProfileCount(C, std::nullopt);
245}
246
247void StaticDataSplitter::updateStatsWithoutProfiles(const MachineFunction &MF) {
249 return;
250
251 if (const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo()) {
252 NumUnknownJumpTables += MJTI->getJumpTables().size();
253 }
254}
255
257
259 false, false)
265 false)
266
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define DEBUG_TYPE
#define I(x, y, z)
Definition MD5.cpp:57
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
This is an important base class in LLVM.
Definition Constant.h:43
bool hasProfileData(bool IncludeSynthetic=false) const
Return true if the function is annotated with profile data.
Definition Function.h:334
bool hasLocalLinkage() const
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
union llvm::MachineConstantPoolEntry::@004270020304201266316354007027341142157160323045 Val
The constant itself.
bool isMachineConstantPoolEntry() const
isMachineConstantPoolEntry - Return true if the MachineConstantPoolEntry is indeed a target specific ...
The MachineConstantPool class keeps track of constants referenced by a function which must be spilled...
const std::vector< MachineConstantPoolEntry > & getConstants() const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
LLVM_ABI bool updateJumpTableEntryHotness(size_t JTI, MachineFunctionDataHotness Hotness)
const std::vector< MachineJumpTableEntry > & getJumpTables() const
MachineOperand class - Representation of each machine instruction operand.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
Analysis providing profile information.
This wraps the StaticDataProfileInfo object as an immutable pass, for a backend pass to operate on.
A class that holds the constants that represent static data and their profile information and provide...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
Primary interface to the complete machine description for the target machine.
Changed
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
bool IsAnnotationOK(const GlobalVariable &GV)
Returns true if the annotation kind of the global variable GV is AnnotationOK.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI MachineFunctionPass * createStaticDataSplitterPass()
createStaticDataSplitterPass - This is a machine-function pass that categorizes static data hotness u...
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI bool AreStatisticsEnabled()
Check if statistics are enabled.
DWARFExpression::Operation Op