Skip to content

Commit 9fae47c

Browse files
resolve comments
1 parent 072c44f commit 9fae47c

File tree

4 files changed

+73
-78
lines changed

4 files changed

+73
-78
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ class AsmPrinter : public MachineFunctionPass {
338338
DwarfUsesRelocationsAcrossSections = Enable;
339339
}
340340

341+
// Returns a section suffix (hot or unlikely) for the constant if profiles
342+
// are available. Returns empty string otherwise.
343+
StringRef getConstantSectionSuffix(const Constant *C) const;
344+
341345
//===------------------------------------------------------------------===//
342346
// XRay instrumentation implementation.
343347
//===------------------------------------------------------------------===//

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,6 +2769,23 @@ namespace {
27692769

27702770
} // end anonymous namespace
27712771

2772+
StringRef AsmPrinter::getConstantSectionSuffix(const Constant *C) const {
2773+
SmallString<8> SectionNameSuffix;
2774+
if (TM.Options.EnableStaticDataPartitioning) {
2775+
if (C && SDPI && PSI) {
2776+
auto Count = SDPI->getConstantProfileCount(C);
2777+
if (Count) {
2778+
if (PSI->isHotCount(*Count)) {
2779+
SectionNameSuffix.append("hot");
2780+
} else if (PSI->isColdCount(*Count) && !SDPI->hasUnknownCount(C)) {
2781+
SectionNameSuffix.append("unlikely");
2782+
}
2783+
}
2784+
}
2785+
}
2786+
return SectionNameSuffix.str();
2787+
}
2788+
27722789
/// EmitConstantPool - Print to the current output stream assembly
27732790
/// representations of the constants in the constant pool MCP. This is
27742791
/// used to print out constants which have been "spilled to memory" by
@@ -2791,26 +2808,8 @@ void AsmPrinter::emitConstantPool() {
27912808
if (!CPE.isMachineConstantPoolEntry())
27922809
C = CPE.Val.ConstVal;
27932810

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-
}
2811+
MCSection *S = getObjFileLowering().getSectionForConstant(
2812+
getDataLayout(), Kind, C, Alignment, getConstantSectionSuffix(C));
28142813

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

llvm/lib/CodeGen/StaticDataSplitter.cpp

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ class StaticDataSplitter : public MachineFunctionPass {
6969

7070
void annotateStaticDataWithoutProfiles(const MachineFunction &MF);
7171

72+
// Returns the constant if the operand refers to a global variable or constant
73+
// that gets lowered to static data sections. Otherwise, return nullptr.
74+
const Constant *getConstant(const MachineOperand &Op, const TargetMachine &TM,
75+
const MachineConstantPool *MCP);
76+
7277
public:
7378
static char ID;
7479

@@ -112,12 +117,42 @@ bool StaticDataSplitter::runOnMachineFunction(MachineFunction &MF) {
112117
return Changed;
113118
}
114119

120+
const Constant *
121+
StaticDataSplitter::getConstant(const MachineOperand &Op,
122+
const TargetMachine &TM,
123+
const MachineConstantPool *MCP) {
124+
if (!Op.isGlobal() && !Op.isCPI())
125+
return nullptr;
126+
127+
if (Op.isGlobal()) {
128+
// Find global variables with local linkage.
129+
const GlobalVariable *GV = getLocalLinkageGlobalVariable(Op.getGlobal());
130+
// Skip 'special' global variables conservatively because they are
131+
// often handled specially, and skip those not in static data
132+
// sections.
133+
if (!GV || GV->getName().starts_with("llvm.") ||
134+
!inStaticDataSection(GV, TM))
135+
return nullptr;
136+
return GV;
137+
}
138+
assert(Op.isCPI() && "Op must be constant pool index in this branch");
139+
int CPI = Op.getIndex();
140+
if (CPI == -1)
141+
return nullptr;
142+
143+
assert(MCP != nullptr && "Constant pool info is not available.");
144+
const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
145+
146+
if (CPE.isMachineConstantPoolEntry())
147+
return nullptr;
148+
149+
return CPE.Val.ConstVal;
150+
}
151+
115152
bool StaticDataSplitter::partitionStaticDataWithProfiles(MachineFunction &MF) {
116153
int NumChangedJumpTables = 0;
117154

118-
const TargetMachine &TM = MF.getTarget();
119155
MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
120-
const MachineConstantPool *MCP = MF.getConstantPool();
121156

122157
// Jump table could be used by either terminating instructions or
123158
// non-terminating ones, so we walk all instructions and use
@@ -149,30 +184,8 @@ bool StaticDataSplitter::partitionStaticDataWithProfiles(MachineFunction &MF) {
149184

150185
if (MJTI->updateJumpTableEntryHotness(JTI, Hotness))
151186
++NumChangedJumpTables;
152-
} else if (Op.isGlobal()) {
153-
// Find global variables with local linkage.
154-
const GlobalVariable *GV =
155-
getLocalLinkageGlobalVariable(Op.getGlobal());
156-
// Skip 'special' global variables conservatively because they are
157-
// often handled specially, and skip those not in static data
158-
// sections.
159-
if (!GV || GV->getName().starts_with("llvm.") ||
160-
!inStaticDataSection(GV, TM))
161-
continue;
162-
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;
187+
} else if (const Constant *C =
188+
getConstant(Op, MF.getTarget(), MF.getConstantPool())) {
176189
SDPI->addConstantProfileCount(C, Count);
177190
}
178191
}
@@ -218,34 +231,13 @@ void StaticDataSplitter::updateStatsWithProfiles(const MachineFunction &MF) {
218231

219232
void StaticDataSplitter::annotateStaticDataWithoutProfiles(
220233
const MachineFunction &MF) {
221-
const MachineConstantPool *MCP = MF.getConstantPool();
222234
for (const auto &MBB : MF) {
223235
for (const MachineInstr &I : MBB) {
224236
for (const MachineOperand &Op : I.operands()) {
225-
if (!Op.isGlobal() && !Op.isCPI())
226-
continue;
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;
237+
const Constant *C =
238+
getConstant(Op, MF.getTarget(), MF.getConstantPool());
239+
if (C)
247240
SDPI->addConstantProfileCount(C, std::nullopt);
248-
}
249241
}
250242
}
251243
}

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,35 +1074,35 @@ MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
10741074

10751075
MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
10761076
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
1077-
StringRef SectionPrefix) const {
1077+
StringRef SectionSuffix) const {
10781078
// TODO: Share code between this function and
10791079
// MCObjectInfo::initELFMCObjectFileInfo.
1080-
if (SectionPrefix.empty())
1080+
if (SectionSuffix.empty())
10811081
return getSectionForConstant(DL, Kind, C, Alignment);
10821082

10831083
auto &Context = getContext();
10841084
if (Kind.isMergeableConst4() && MergeableConst4Section)
1085-
return Context.getELFSection(".rodata.cst4." + SectionPrefix,
1085+
return Context.getELFSection(".rodata.cst4." + SectionSuffix,
10861086
ELF::SHT_PROGBITS,
10871087
ELF::SHF_ALLOC | ELF::SHF_MERGE, 4);
10881088
if (Kind.isMergeableConst8() && MergeableConst8Section)
1089-
return Context.getELFSection(".rodata.cst8." + SectionPrefix,
1089+
return Context.getELFSection(".rodata.cst8." + SectionSuffix,
10901090
ELF::SHT_PROGBITS,
10911091
ELF::SHF_ALLOC | ELF::SHF_MERGE, 8);
10921092
if (Kind.isMergeableConst16() && MergeableConst16Section)
1093-
return Context.getELFSection(".rodata.cst16." + SectionPrefix,
1093+
return Context.getELFSection(".rodata.cst16." + SectionSuffix,
10941094
ELF::SHT_PROGBITS,
10951095
ELF::SHF_ALLOC | ELF::SHF_MERGE, 16);
10961096
if (Kind.isMergeableConst32() && MergeableConst32Section)
1097-
return Context.getELFSection(".rodata.cst32." + SectionPrefix,
1097+
return Context.getELFSection(".rodata.cst32." + SectionSuffix,
10981098
ELF::SHT_PROGBITS,
10991099
ELF::SHF_ALLOC | ELF::SHF_MERGE, 32);
11001100
if (Kind.isReadOnly())
1101-
return Context.getELFSection(".rodata" + SectionPrefix, ELF::SHT_PROGBITS,
1101+
return Context.getELFSection(".rodata." + SectionSuffix, ELF::SHT_PROGBITS,
11021102
ELF::SHF_ALLOC);
11031103

11041104
assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
1105-
return Context.getELFSection(".data.rel.ro" + SectionPrefix,
1105+
return Context.getELFSection(".data.rel.ro." + SectionSuffix,
11061106
ELF::SHT_PROGBITS,
11071107
ELF::SHF_ALLOC | ELF::SHF_WRITE);
11081108
}

0 commit comments

Comments
 (0)