Skip to content

Commit dd74827

Browse files
rely to upstream
1 parent 34b6b9b commit dd74827

File tree

6 files changed

+223
-140
lines changed

6 files changed

+223
-140
lines changed

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ template <> struct ilist_callback_traits<MachineBasicBlock> {
9191
// The hotness of static data tracked by a MachineFunction and not represented
9292
// as a global object in the module IR / MIR. Typical examples are
9393
// MachineJumpTableInfo and MachineConstantPool.
94-
enum class DataHotness {
94+
enum class MachineFunctionDataHotness {
9595
Unknown,
9696
Cold,
9797
Hot,

llvm/include/llvm/CodeGen/MachineJumpTableInfo.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ namespace llvm {
2828
class MachineBasicBlock;
2929
class DataLayout;
3030
class raw_ostream;
31-
enum class DataHotness;
31+
enum class MachineFunctionDataHotness;
3232

3333
/// MachineJumpTableEntry - One jump table in the jump table info.
3434
///
3535
struct MachineJumpTableEntry {
3636
/// MBBs - The vector of basic blocks from which to create the jump table.
3737
std::vector<MachineBasicBlock*> MBBs;
3838

39-
DataHotness Hotness;
39+
MachineFunctionDataHotness Hotness;
4040

4141
explicit MachineJumpTableEntry(const std::vector<MachineBasicBlock *> &M);
4242
};
@@ -109,7 +109,10 @@ class MachineJumpTableInfo {
109109
return JumpTables;
110110
}
111111

112-
void updateJumpTableHotness(size_t JTI, DataHotness Hotness);
112+
// Update machine jump table entry's hotness. Return true if the hotness is
113+
// updated.
114+
bool updateJumpTableEntryHotness(size_t JTI,
115+
MachineFunctionDataHotness Hotness);
113116

114117
/// RemoveJumpTable - Mark the specific index as being dead. This will
115118
/// prevent it from being emitted.

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ namespace llvm {
7171
/// using profile information.
7272
MachineFunctionPass *createMachineFunctionSplitterPass();
7373

74-
/// createStaticDataSplitterPass - This pass partions static data sections
74+
/// createStaticDataSplitterPass - This pass partitions a static data section
7575
/// into a hot and cold section using profile information.
7676
MachineFunctionPass *createStaticDataSplitterPass();
7777

llvm/lib/CodeGen/MachineFunction.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ const unsigned MachineFunction::DebugOperandMemNumber = 1000000;
12931293

12941294
MachineJumpTableEntry::MachineJumpTableEntry(
12951295
const std::vector<MachineBasicBlock *> &MBBs)
1296-
: MBBs(MBBs), Hotness(DataHotness::Unknown) {}
1296+
: MBBs(MBBs), Hotness(MachineFunctionDataHotness::Unknown) {}
12971297

12981298
/// Return the size of each entry in the jump table.
12991299
unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
@@ -1344,13 +1344,17 @@ unsigned MachineJumpTableInfo::createJumpTableIndex(
13441344
return JumpTables.size()-1;
13451345
}
13461346

1347-
void MachineJumpTableInfo::updateJumpTableHotness(size_t JTI,
1348-
DataHotness Hotness) {
1347+
bool MachineJumpTableInfo::updateJumpTableEntryHotness(
1348+
size_t JTI, MachineFunctionDataHotness Hotness) {
13491349
assert(JTI < JumpTables.size() && "Invalid JTI!");
13501350
// Note record the largest hotness is important for mergable data (constant
13511351
// pools). Even if jump table instances are not merged, record the largest
13521352
// value seen fwiw.
1353-
JumpTables[JTI].Hotness = std::max(JumpTables[JTI].Hotness, Hotness);
1353+
if (Hotness <= JumpTables[JTI].Hotness)
1354+
return false;
1355+
1356+
JumpTables[JTI].Hotness = Hotness;
1357+
return true;
13541358
}
13551359

13561360
/// If Old is the target of any jump tables, update the jump tables to branch

llvm/lib/CodeGen/StaticDataSplitter.cpp

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This pass uses profile information to partition static data sections into
10-
// hot and cold ones. It begins to split jump tables based on profile, and
11-
// subsequent patches will handle constant pools and other module internal data.
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+
// - Constant pools (TODO)
13+
// - Other module-internal data (TODO)
1214
//
1315
// For the original RFC of this pass please see
14-
// https://discourse.llvm.org/t/rfc-profile-guided-static-data-partitioning/83744.
16+
// https://discourse.llvm.org/t/rfc-profile-guided-static-data-partitioning/83744
1517

18+
#include "llvm/ADT/ScopeExit.h"
1619
#include "llvm/ADT/Statistic.h"
1720
#include "llvm/Analysis/ProfileSummaryInfo.h"
1821
#include "llvm/CodeGen/MBFIWrapper.h"
@@ -35,8 +38,15 @@ using namespace llvm;
3538
STATISTIC(NumHotJumpTables, "Number of hot jump tables seen");
3639
STATISTIC(NumColdJumpTables, "Number of cold jump tables seen");
3740
STATISTIC(NumUnknownJumpTables,
38-
"Number of jump tables with unknown hotness. Such jump tables will "
39-
"be placed in the hot-suffixed section by default.");
41+
"Number of jump tables with unknown hotness. Option "
42+
"-static-data-default-hotness specifies the hotness.");
43+
44+
static cl::opt<MachineFunctionDataHotness> StaticDataDefaultHotness(
45+
"static-data-default-hotness", cl::Hidden,
46+
cl::desc("The hotness for static data with unknown hotness"),
47+
cl::init(MachineFunctionDataHotness::Hot),
48+
cl::values(clEnumValN(MachineFunctionDataHotness::Hot, "hot", "Hot"),
49+
clEnumValN(MachineFunctionDataHotness::Cold, "cold", "Cold")));
4050

4151
class StaticDataSplitter : public MachineFunctionPass {
4252
const MachineBranchProbabilityInfo *MBPI = nullptr;
@@ -82,12 +92,7 @@ bool StaticDataSplitter::runOnMachineFunction(MachineFunction &MF) {
8292
bool StaticDataSplitter::splitJumpTablesWithProfiles(
8393
MachineFunction &MF, MachineJumpTableInfo &MJTI) {
8494
int NumChangedJumpTables = 0;
85-
// Regard a jump table as hot by default. If the source and all of destination
86-
// blocks are cold, regard the jump table as cold. While a destination block
87-
// does not read a jump table (unless it's also a source block), a hot
88-
// destination heuristically makes its jump table hot to accommodate for
89-
// potential profile data skews (from sampled profiles, for example).
90-
DataHotness Hotness = DataHotness::Hot;
95+
9196
for (const auto &MBB : MF) {
9297
// IMPORTANT, `getJumpTableIndex` is a thin wrapper around per-target
9398
// interface `TargetInstrInfo::getjumpTableIndex`, and only X86 implements
@@ -97,24 +102,14 @@ bool StaticDataSplitter::splitJumpTablesWithProfiles(
97102
if (JTI == -1)
98103
continue;
99104

100-
bool AllBlocksCold = true;
101-
102-
if (!PSI->isColdBlock(&MBB, MBFI))
103-
AllBlocksCold = false;
105+
auto Hotness = MachineFunctionDataHotness::Hot;
104106

105-
for (const MachineBasicBlock *MBB : MJTI.getJumpTables()[JTI].MBBs)
106-
if (!PSI->isColdBlock(MBB, MBFI))
107-
AllBlocksCold = false;
108-
109-
if (AllBlocksCold) {
110-
Hotness = DataHotness::Cold;
111-
++NumColdJumpTables;
112-
} else {
113-
++NumHotJumpTables;
114-
}
107+
// Hotness is based on source basic block hotness.
108+
if (PSI->isColdBlock(&MBB, MBFI))
109+
Hotness = MachineFunctionDataHotness::Cold;
115110

116-
MF.getJumpTableInfo()->updateJumpTableHotness(JTI, Hotness);
117-
++NumChangedJumpTables;
111+
if (MF.getJumpTableInfo()->updateJumpTableEntryHotness(JTI, Hotness))
112+
++NumChangedJumpTables;
118113
}
119114
return NumChangedJumpTables > 0;
120115
}
@@ -124,18 +119,40 @@ bool StaticDataSplitter::splitJumpTables(MachineFunction &MF) {
124119
if (!MJTI || MJTI->getJumpTables().empty())
125120
return false;
126121

122+
const bool ProfileAvailable = PSI && PSI->hasProfileSummary() && MBFI &&
123+
MF.getFunction().hasProfileData();
124+
auto statOnExit = llvm::make_scope_exit([&] {
125+
if (!AreStatisticsEnabled())
126+
return;
127+
128+
if (!ProfileAvailable) {
129+
NumUnknownJumpTables += MJTI->getJumpTables().size();
130+
return;
131+
}
132+
133+
for (size_t JTI = 0; JTI < MJTI->getJumpTables().size(); JTI++) {
134+
auto Hotness = MJTI->getJumpTables()[JTI].Hotness;
135+
if (Hotness == MachineFunctionDataHotness::Hot)
136+
NumHotJumpTables++;
137+
else {
138+
assert(Hotness == MachineFunctionDataHotness::Cold &&
139+
"A jump table is hot or cold when profile information is "
140+
"available.");
141+
NumColdJumpTables++;
142+
}
143+
}
144+
});
145+
127146
// Place jump tables according to block hotness if function has profile data.
128-
if (PSI && PSI->hasProfileSummary() && MBFI &&
129-
MF.getFunction().hasProfileData())
147+
if (ProfileAvailable)
130148
return splitJumpTablesWithProfiles(MF, *MJTI);
131149

132-
// Conservatively place all jump tables in the hot-suffixed section if profile
133-
// information for the function is not available, or the target doesn't
134-
// implement `TargetInstrInfo::getJumpTableIndex` yet.
150+
// If function profile is unavailable, -static-data-default-hotness specifies
151+
// the hotness.
135152
for (size_t JTI = 0; JTI < MJTI->getJumpTables().size(); JTI++)
136-
MF.getJumpTableInfo()->updateJumpTableHotness(JTI, DataHotness::Hot);
153+
MF.getJumpTableInfo()->updateJumpTableEntryHotness(
154+
JTI, StaticDataDefaultHotness);
137155

138-
NumUnknownJumpTables += MJTI->getJumpTables().size();
139156
return true;
140157
}
141158

0 commit comments

Comments
 (0)