|
| 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 | +// - Constant pools (TODO) |
| 13 | +// - Other module-internal data (TODO) |
| 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/ScopeExit.h" |
| 19 | +#include "llvm/ADT/Statistic.h" |
| 20 | +#include "llvm/Analysis/ProfileSummaryInfo.h" |
| 21 | +#include "llvm/CodeGen/MBFIWrapper.h" |
| 22 | +#include "llvm/CodeGen/MachineBasicBlock.h" |
| 23 | +#include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
| 24 | +#include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
| 25 | +#include "llvm/CodeGen/MachineConstantPool.h" |
| 26 | +#include "llvm/CodeGen/MachineFunction.h" |
| 27 | +#include "llvm/CodeGen/MachineFunctionPass.h" |
| 28 | +#include "llvm/CodeGen/MachineJumpTableInfo.h" |
| 29 | +#include "llvm/CodeGen/Passes.h" |
| 30 | +#include "llvm/InitializePasses.h" |
| 31 | +#include "llvm/Pass.h" |
| 32 | +#include "llvm/Support/CommandLine.h" |
| 33 | + |
| 34 | +using namespace llvm; |
| 35 | + |
| 36 | +#define DEBUG_TYPE "static-data-splitter" |
| 37 | + |
| 38 | +STATISTIC(NumHotJumpTables, "Number of hot jump tables seen"); |
| 39 | +STATISTIC(NumColdJumpTables, "Number of cold jump tables seen"); |
| 40 | +STATISTIC(NumUnknownJumpTables, |
| 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("This option specifies the hotness of static data when profile " |
| 47 | + "information is unavailable"), |
| 48 | + cl::init(MachineFunctionDataHotness::Hot), |
| 49 | + cl::values(clEnumValN(MachineFunctionDataHotness::Hot, "hot", "Hot"), |
| 50 | + clEnumValN(MachineFunctionDataHotness::Cold, "cold", "Cold"))); |
| 51 | + |
| 52 | +class StaticDataSplitter : public MachineFunctionPass { |
| 53 | + const MachineBranchProbabilityInfo *MBPI = nullptr; |
| 54 | + const MachineBlockFrequencyInfo *MBFI = nullptr; |
| 55 | + const ProfileSummaryInfo *PSI = nullptr; |
| 56 | + |
| 57 | + // Returns true iff any jump table is hot-cold categorized. |
| 58 | + bool splitJumpTables(MachineFunction &MF); |
| 59 | + |
| 60 | + // Same as above but works on functions with profile information. |
| 61 | + bool splitJumpTablesWithProfiles(const MachineFunction &MF, |
| 62 | + MachineJumpTableInfo &MJTI); |
| 63 | + |
| 64 | +public: |
| 65 | + static char ID; |
| 66 | + |
| 67 | + StaticDataSplitter() : MachineFunctionPass(ID) { |
| 68 | + initializeStaticDataSplitterPass(*PassRegistry::getPassRegistry()); |
| 69 | + } |
| 70 | + |
| 71 | + StringRef getPassName() const override { return "Static Data Splitter"; } |
| 72 | + |
| 73 | + void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 74 | + MachineFunctionPass::getAnalysisUsage(AU); |
| 75 | + AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); |
| 76 | + AU.addRequired<MachineBlockFrequencyInfoWrapperPass>(); |
| 77 | + AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
| 78 | + } |
| 79 | + |
| 80 | + bool runOnMachineFunction(MachineFunction &MF) override; |
| 81 | +}; |
| 82 | + |
| 83 | +bool StaticDataSplitter::runOnMachineFunction(MachineFunction &MF) { |
| 84 | + MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); |
| 85 | + MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI(); |
| 86 | + PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
| 87 | + |
| 88 | + return splitJumpTables(MF); |
| 89 | +} |
| 90 | + |
| 91 | +bool StaticDataSplitter::splitJumpTablesWithProfiles( |
| 92 | + const MachineFunction &MF, MachineJumpTableInfo &MJTI) { |
| 93 | + int NumChangedJumpTables = 0; |
| 94 | + |
| 95 | + // Jump table could be used by either terminating instructions or |
| 96 | + // non-terminating ones, so we walk all instructions and use |
| 97 | + // `MachineOperand::isJTI()` to identify jump table operands. |
| 98 | + // Similarly, `MachineOperand::isCPI()` can identify constant pool usages |
| 99 | + // in the same loop. |
| 100 | + for (const auto &MBB : MF) { |
| 101 | + for (const MachineInstr &I : MBB) { |
| 102 | + for (const MachineOperand &Op : I.operands()) { |
| 103 | + if (!Op.isJTI()) |
| 104 | + continue; |
| 105 | + const int JTI = Op.getIndex(); |
| 106 | + // This is not a source block of jump table. |
| 107 | + if (JTI == -1) |
| 108 | + continue; |
| 109 | + |
| 110 | + auto Hotness = MachineFunctionDataHotness::Hot; |
| 111 | + |
| 112 | + // Hotness is based on source basic block hotness. |
| 113 | + // TODO: PSI APIs are about instruction hotness. Introduce API for data |
| 114 | + // access hotness. |
| 115 | + if (PSI->isColdBlock(&MBB, MBFI)) |
| 116 | + Hotness = MachineFunctionDataHotness::Cold; |
| 117 | + |
| 118 | + if (MJTI.updateJumpTableEntryHotness(JTI, Hotness)) |
| 119 | + ++NumChangedJumpTables; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + return NumChangedJumpTables > 0; |
| 124 | +} |
| 125 | + |
| 126 | +bool StaticDataSplitter::splitJumpTables(MachineFunction &MF) { |
| 127 | + MachineJumpTableInfo *MJTI = MF.getJumpTableInfo(); |
| 128 | + if (!MJTI || MJTI->getJumpTables().empty()) |
| 129 | + return false; |
| 130 | + |
| 131 | + const bool ProfileAvailable = PSI && PSI->hasProfileSummary() && MBFI && |
| 132 | + MF.getFunction().hasProfileData(); |
| 133 | + auto statOnExit = llvm::make_scope_exit([&] { |
| 134 | + if (!AreStatisticsEnabled()) |
| 135 | + return; |
| 136 | + |
| 137 | + if (!ProfileAvailable) { |
| 138 | + NumUnknownJumpTables += MJTI->getJumpTables().size(); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + for (size_t JTI = 0; JTI < MJTI->getJumpTables().size(); JTI++) { |
| 143 | + auto Hotness = MJTI->getJumpTables()[JTI].Hotness; |
| 144 | + if (Hotness == MachineFunctionDataHotness::Hot) { |
| 145 | + ++NumHotJumpTables; |
| 146 | + } else { |
| 147 | + assert(Hotness == MachineFunctionDataHotness::Cold && |
| 148 | + "A jump table is either hot or cold when profile information is " |
| 149 | + "available."); |
| 150 | + ++NumColdJumpTables; |
| 151 | + } |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + // Place jump tables according to block hotness if function has profile data. |
| 156 | + if (ProfileAvailable) |
| 157 | + return splitJumpTablesWithProfiles(MF, *MJTI); |
| 158 | + |
| 159 | + // If function profile is unavailable (e.g., module not instrumented, or new |
| 160 | + // code paths lacking samples), -static-data-default-hotness specifies the |
| 161 | + // hotness. |
| 162 | + for (size_t JTI = 0; JTI < MJTI->getJumpTables().size(); JTI++) |
| 163 | + MF.getJumpTableInfo()->updateJumpTableEntryHotness( |
| 164 | + JTI, StaticDataDefaultHotness); |
| 165 | + |
| 166 | + return true; |
| 167 | +} |
| 168 | + |
| 169 | +char StaticDataSplitter::ID = 0; |
| 170 | + |
| 171 | +INITIALIZE_PASS_BEGIN(StaticDataSplitter, DEBUG_TYPE, "Split static data", |
| 172 | + false, false) |
| 173 | +INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass) |
| 174 | +INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfoWrapperPass) |
| 175 | +INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) |
| 176 | +INITIALIZE_PASS_END(StaticDataSplitter, DEBUG_TYPE, "Split static data", false, |
| 177 | + false) |
| 178 | + |
| 179 | +MachineFunctionPass *llvm::createStaticDataSplitterPass() { |
| 180 | + return new StaticDataSplitter(); |
| 181 | +} |
0 commit comments