Skip to content

Commit 072c44f

Browse files
[CodeGen][StaticDataSplitter]Support constant pool partitioning
1 parent 967dc03 commit 072c44f

File tree

11 files changed

+422
-14
lines changed

11 files changed

+422
-14
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "llvm/ADT/DenseMap.h"
1919
#include "llvm/ADT/MapVector.h"
2020
#include "llvm/ADT/SmallVector.h"
21+
#include "llvm/Analysis/ProfileSummaryInfo.h"
22+
#include "llvm/Analysis/StaticDataProfileInfo.h"
2123
#include "llvm/BinaryFormat/Dwarf.h"
2224
#include "llvm/CodeGen/DwarfStringPoolEntry.h"
2325
#include "llvm/CodeGen/MachineFunctionPass.h"
@@ -132,6 +134,12 @@ class AsmPrinter : public MachineFunctionPass {
132134
/// default, this is equal to CurrentFnSym.
133135
MCSymbol *CurrentFnSymForSize = nullptr;
134136

137+
/// Provides the profile information for constants.
138+
const StaticDataProfileInfo *SDPI = nullptr;
139+
140+
/// The profile summary information.
141+
const ProfileSummaryInfo *PSI = nullptr;
142+
135143
/// Map a basic block section ID to the begin and end symbols of that section
136144
/// which determine the section's range.
137145
struct MBBSectionRange {

llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
6868
const Constant *C,
6969
Align &Alignment) const override;
7070

71+
/// Similar to the function above, but append \p SectionSuffix to the section
72+
/// name.
73+
MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
74+
const Constant *C, Align &Alignment,
75+
StringRef SectionSuffix) const override;
76+
7177
MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
7278
const TargetMachine &TM) const override;
7379

llvm/include/llvm/Target/TargetLoweringObjectFile.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
104104
SectionKind Kind, const Constant *C,
105105
Align &Alignment) const;
106106

107+
/// Similar to the function above, but append \p SectionSuffix to the section
108+
/// name.
109+
virtual MCSection *getSectionForConstant(const DataLayout &DL,
110+
SectionKind Kind, const Constant *C,
111+
Align &Alignment,
112+
StringRef SectionSuffix) const;
113+
107114
virtual MCSection *
108115
getSectionForMachineBasicBlock(const Function &F,
109116
const MachineBasicBlock &MBB,

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,8 +2791,26 @@ void AsmPrinter::emitConstantPool() {
27912791
if (!CPE.isMachineConstantPoolEntry())
27922792
C = CPE.Val.ConstVal;
27932793

2794-
MCSection *S = getObjFileLowering().getSectionForConstant(
2795-
getDataLayout(), Kind, C, Alignment);
2794+
MCSection *S = nullptr;
2795+
if (TM.Options.EnableStaticDataPartitioning) {
2796+
SmallString<8> SectionNameSuffix;
2797+
if (C && SDPI && PSI) {
2798+
auto Count = SDPI->getConstantProfileCount(C);
2799+
if (Count) {
2800+
if (PSI->isHotCount(*Count)) {
2801+
SectionNameSuffix.append("hot");
2802+
} else if (PSI->isColdCount(*Count) && !SDPI->hasUnknownCount(C)) {
2803+
SectionNameSuffix.append("unlikely");
2804+
}
2805+
}
2806+
}
2807+
2808+
S = getObjFileLowering().getSectionForConstant(
2809+
getDataLayout(), Kind, C, Alignment, SectionNameSuffix);
2810+
} else {
2811+
S = getObjFileLowering().getSectionForConstant(getDataLayout(), Kind, C,
2812+
Alignment);
2813+
}
27962814

27972815
// The number of sections are small, just do a linear search from the
27982816
// last section to the first.

llvm/lib/CodeGen/StaticDataSplitter.cpp

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// for the following types of static data:
1111
// - Jump tables
1212
// - Module-internal global variables
13-
// - Constant pools (TODO)
13+
// - Constant pools
1414
//
1515
// For the original RFC of this pass please see
1616
// https://discourse.llvm.org/t/rfc-profile-guided-static-data-partitioning/83744
@@ -117,16 +117,17 @@ bool StaticDataSplitter::partitionStaticDataWithProfiles(MachineFunction &MF) {
117117

118118
const TargetMachine &TM = MF.getTarget();
119119
MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
120+
const MachineConstantPool *MCP = MF.getConstantPool();
120121

121122
// Jump table could be used by either terminating instructions or
122123
// non-terminating ones, so we walk all instructions and use
123124
// `MachineOperand::isJTI()` to identify jump table operands.
124-
// Similarly, `MachineOperand::isCPI()` can identify constant pool usages
125-
// in the same loop.
125+
// Similarly, `MachineOperand::isCPI()` is used to identify constant pool
126+
// usages in the same loop.
126127
for (const auto &MBB : MF) {
127128
for (const MachineInstr &I : MBB) {
128129
for (const MachineOperand &Op : I.operands()) {
129-
if (!Op.isJTI() && !Op.isGlobal())
130+
if (!Op.isJTI() && !Op.isGlobal() && !Op.isCPI())
130131
continue;
131132

132133
std::optional<uint64_t> Count = MBFI->getBlockProfileCount(&MBB);
@@ -148,7 +149,7 @@ bool StaticDataSplitter::partitionStaticDataWithProfiles(MachineFunction &MF) {
148149

149150
if (MJTI->updateJumpTableEntryHotness(JTI, Hotness))
150151
++NumChangedJumpTables;
151-
} else {
152+
} else if (Op.isGlobal()) {
152153
// Find global variables with local linkage.
153154
const GlobalVariable *GV =
154155
getLocalLinkageGlobalVariable(Op.getGlobal());
@@ -159,6 +160,20 @@ bool StaticDataSplitter::partitionStaticDataWithProfiles(MachineFunction &MF) {
159160
!inStaticDataSection(GV, TM))
160161
continue;
161162
SDPI->addConstantProfileCount(GV, Count);
163+
} else {
164+
assert(Op.isCPI() && "Op must be constant pool index in this branch");
165+
int CPI = Op.getIndex();
166+
if (CPI == -1)
167+
continue;
168+
169+
assert(MCP != nullptr && "Constant pool info is not available.");
170+
const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
171+
172+
if (CPE.isMachineConstantPoolEntry())
173+
continue;
174+
175+
const Constant *C = CPE.Val.ConstVal;
176+
SDPI->addConstantProfileCount(C, Count);
162177
}
163178
}
164179
}
@@ -203,17 +218,34 @@ void StaticDataSplitter::updateStatsWithProfiles(const MachineFunction &MF) {
203218

204219
void StaticDataSplitter::annotateStaticDataWithoutProfiles(
205220
const MachineFunction &MF) {
221+
const MachineConstantPool *MCP = MF.getConstantPool();
206222
for (const auto &MBB : MF) {
207223
for (const MachineInstr &I : MBB) {
208224
for (const MachineOperand &Op : I.operands()) {
209-
if (!Op.isGlobal())
210-
continue;
211-
const GlobalVariable *GV =
212-
getLocalLinkageGlobalVariable(Op.getGlobal());
213-
if (!GV || GV->getName().starts_with("llvm.") ||
214-
!inStaticDataSection(GV, MF.getTarget()))
225+
if (!Op.isGlobal() && !Op.isCPI())
215226
continue;
216-
SDPI->addConstantProfileCount(GV, std::nullopt);
227+
if (Op.isGlobal()) {
228+
const GlobalVariable *GV =
229+
getLocalLinkageGlobalVariable(Op.getGlobal());
230+
if (!GV || GV->getName().starts_with("llvm.") ||
231+
!inStaticDataSection(GV, MF.getTarget()))
232+
continue;
233+
SDPI->addConstantProfileCount(GV, std::nullopt);
234+
} else {
235+
assert(Op.isCPI() && "Op must be constant pool index in this branch");
236+
int CPI = Op.getIndex();
237+
if (CPI == -1)
238+
continue;
239+
240+
assert(MCP != nullptr && "Constant pool info is not available.");
241+
const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
242+
243+
if (CPE.isMachineConstantPoolEntry())
244+
continue;
245+
246+
const Constant *C = CPE.Val.ConstVal;
247+
SDPI->addConstantProfileCount(C, std::nullopt);
248+
}
217249
}
218250
}
219251
}

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,41 @@ MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
10721072
return DataRelROSection;
10731073
}
10741074

1075+
MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
1076+
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
1077+
StringRef SectionPrefix) const {
1078+
// TODO: Share code between this function and
1079+
// MCObjectInfo::initELFMCObjectFileInfo.
1080+
if (SectionPrefix.empty())
1081+
return getSectionForConstant(DL, Kind, C, Alignment);
1082+
1083+
auto &Context = getContext();
1084+
if (Kind.isMergeableConst4() && MergeableConst4Section)
1085+
return Context.getELFSection(".rodata.cst4." + SectionPrefix,
1086+
ELF::SHT_PROGBITS,
1087+
ELF::SHF_ALLOC | ELF::SHF_MERGE, 4);
1088+
if (Kind.isMergeableConst8() && MergeableConst8Section)
1089+
return Context.getELFSection(".rodata.cst8." + SectionPrefix,
1090+
ELF::SHT_PROGBITS,
1091+
ELF::SHF_ALLOC | ELF::SHF_MERGE, 8);
1092+
if (Kind.isMergeableConst16() && MergeableConst16Section)
1093+
return Context.getELFSection(".rodata.cst16." + SectionPrefix,
1094+
ELF::SHT_PROGBITS,
1095+
ELF::SHF_ALLOC | ELF::SHF_MERGE, 16);
1096+
if (Kind.isMergeableConst32() && MergeableConst32Section)
1097+
return Context.getELFSection(".rodata.cst32." + SectionPrefix,
1098+
ELF::SHT_PROGBITS,
1099+
ELF::SHF_ALLOC | ELF::SHF_MERGE, 32);
1100+
if (Kind.isReadOnly())
1101+
return Context.getELFSection(".rodata" + SectionPrefix, ELF::SHT_PROGBITS,
1102+
ELF::SHF_ALLOC);
1103+
1104+
assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
1105+
return Context.getELFSection(".data.rel.ro" + SectionPrefix,
1106+
ELF::SHT_PROGBITS,
1107+
ELF::SHF_ALLOC | ELF::SHF_WRITE);
1108+
}
1109+
10751110
/// Returns a unique section for the given machine basic block.
10761111
MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
10771112
const Function &F, const MachineBasicBlock &MBB,

llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,16 @@ class AArch64AsmPrinter : public AsmPrinter {
226226
}
227227

228228
bool runOnMachineFunction(MachineFunction &MF) override {
229+
auto *PSIW = getAnalysisIfAvailable<ProfileSummaryInfoWrapperPass>();
230+
if (PSIW) {
231+
PSI = &PSIW->getPSI();
232+
}
233+
234+
auto *SDPIW = getAnalysisIfAvailable<StaticDataProfileInfoWrapperPass>();
235+
if (SDPIW) {
236+
SDPI = &SDPIW->getStaticDataProfileInfo();
237+
}
238+
229239
AArch64FI = MF.getInfo<AArch64FunctionInfo>();
230240
STI = &MF.getSubtarget<AArch64Subtarget>();
231241

llvm/lib/Target/TargetLoweringObjectFile.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,16 @@ MCSection *TargetLoweringObjectFile::getSectionForConstant(
386386
return DataSection;
387387
}
388388

389+
MCSection *TargetLoweringObjectFile::getSectionForConstant(
390+
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
391+
StringRef SectionPrefix) const {
392+
// Fallback to `getSectionForConstant` without `SectionPrefix` parameter if it
393+
// is empty.
394+
if (SectionPrefix.empty())
395+
return getSectionForConstant(DL, Kind, C, Alignment);
396+
report_fatal_error("Unimplemented");
397+
}
398+
389399
MCSection *TargetLoweringObjectFile::getSectionForMachineBasicBlock(
390400
const Function &F, const MachineBasicBlock &MBB,
391401
const TargetMachine &TM) const {

llvm/lib/Target/X86/X86AsmPrinter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "X86InstrInfo.h"
2121
#include "X86MachineFunctionInfo.h"
2222
#include "X86Subtarget.h"
23+
#include "llvm/Analysis/StaticDataProfileInfo.h"
2324
#include "llvm/BinaryFormat/COFF.h"
2425
#include "llvm/BinaryFormat/ELF.h"
2526
#include "llvm/CodeGen/MachineConstantPool.h"
@@ -61,6 +62,15 @@ X86AsmPrinter::X86AsmPrinter(TargetMachine &TM,
6162
/// runOnMachineFunction - Emit the function body.
6263
///
6364
bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
65+
auto *PSIW = getAnalysisIfAvailable<ProfileSummaryInfoWrapperPass>();
66+
if (PSIW) {
67+
PSI = &PSIW->getPSI();
68+
}
69+
70+
auto *SDPIW = getAnalysisIfAvailable<StaticDataProfileInfoWrapperPass>();
71+
if (SDPIW) {
72+
SDPI = &SDPIW->getStaticDataProfileInfo();
73+
}
6474
Subtarget = &MF.getSubtarget<X86Subtarget>();
6575

6676
SMShadowTracker.startFunction(MF);

0 commit comments

Comments
 (0)