-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[CodeGen] Introduce Static Data Splitter pass #122183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
5d207e9
34b6b9b
1bacc51
8a85d1a
e54dacb
366bbbf
c8c122c
e5e0cee
89c80ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,15 +28,17 @@ namespace llvm { | |
| class MachineBasicBlock; | ||
| class DataLayout; | ||
| class raw_ostream; | ||
| enum class DataHotness; | ||
|
|
||
| /// MachineJumpTableEntry - One jump table in the jump table info. | ||
| /// | ||
| struct MachineJumpTableEntry { | ||
| /// MBBs - The vector of basic blocks from which to create the jump table. | ||
| std::vector<MachineBasicBlock*> MBBs; | ||
|
|
||
| explicit MachineJumpTableEntry(const std::vector<MachineBasicBlock*> &M) | ||
| : MBBs(M) {} | ||
| DataHotness Hotness; | ||
|
||
|
|
||
| explicit MachineJumpTableEntry(const std::vector<MachineBasicBlock *> &M); | ||
| }; | ||
|
|
||
| class MachineJumpTableInfo { | ||
|
|
@@ -107,6 +109,8 @@ class MachineJumpTableInfo { | |
| return JumpTables; | ||
| } | ||
|
|
||
| void updateJumpTableHotness(size_t JTI, DataHotness Hotness); | ||
|
||
|
|
||
| /// RemoveJumpTable - Mark the specific index as being dead. This will | ||
| /// prevent it from being emitted. | ||
| void RemoveJumpTable(unsigned Idx) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,6 +71,10 @@ namespace llvm { | |
| /// using profile information. | ||
| MachineFunctionPass *createMachineFunctionSplitterPass(); | ||
|
|
||
| /// createStaticDataSplitterPass - This pass partions static data sections | ||
|
||
| /// into a hot and cold section using profile information. | ||
| MachineFunctionPass *createStaticDataSplitterPass(); | ||
|
|
||
| /// MachineFunctionPrinter pass - This pass prints out the machine function to | ||
| /// the given stream as a debugging tool. | ||
| MachineFunctionPass * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1291,6 +1291,10 @@ const unsigned MachineFunction::DebugOperandMemNumber = 1000000; | |
| // MachineJumpTableInfo implementation | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| MachineJumpTableEntry::MachineJumpTableEntry( | ||
| const std::vector<MachineBasicBlock *> &MBBs) | ||
| : MBBs(MBBs), Hotness(DataHotness::Unknown) {} | ||
|
||
|
|
||
| /// Return the size of each entry in the jump table. | ||
| unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const { | ||
| // The size of a jump table entry is 4 bytes unless the entry is just the | ||
|
|
@@ -1340,6 +1344,15 @@ unsigned MachineJumpTableInfo::createJumpTableIndex( | |
| return JumpTables.size()-1; | ||
| } | ||
|
|
||
| void MachineJumpTableInfo::updateJumpTableHotness(size_t JTI, | ||
| DataHotness Hotness) { | ||
ellishg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert(JTI < JumpTables.size() && "Invalid JTI!"); | ||
| // Note record the largest hotness is important for mergable data (constant | ||
mingmingl-llvm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // pools). Even if jump table instances are not merged, record the largest | ||
| // value seen fwiw. | ||
|
||
| JumpTables[JTI].Hotness = std::max(JumpTables[JTI].Hotness, Hotness); | ||
| } | ||
|
|
||
| /// If Old is the target of any jump tables, update the jump tables to branch | ||
| /// to New instead. | ||
| bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,154 @@ | ||||||||||||||||||||||||||
| //===- StaticDataSplitter.cpp ---------------------------------------------===// | ||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||||||||||||||||||||||
| // See https://llvm.org/LICENSE.txt for license information. | ||||||||||||||||||||||||||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||
| //===----------------------------------------------------------------------===// | ||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||
| // This pass uses profile information to partition static data sections into | ||||||||||||||||||||||||||
| // hot and cold ones. It begins to split jump tables based on profile, and | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| // subsequent patches will handle constant pools and other module internal data. | ||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||
| // For the original RFC of this pass please see | ||||||||||||||||||||||||||
| // https://discourse.llvm.org/t/rfc-profile-guided-static-data-partitioning/83744. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| #include "llvm/ADT/Statistic.h" | ||||||||||||||||||||||||||
| #include "llvm/Analysis/ProfileSummaryInfo.h" | ||||||||||||||||||||||||||
| #include "llvm/CodeGen/MBFIWrapper.h" | ||||||||||||||||||||||||||
| #include "llvm/CodeGen/MachineBasicBlock.h" | ||||||||||||||||||||||||||
| #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" | ||||||||||||||||||||||||||
| #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" | ||||||||||||||||||||||||||
| #include "llvm/CodeGen/MachineConstantPool.h" | ||||||||||||||||||||||||||
| #include "llvm/CodeGen/MachineFunction.h" | ||||||||||||||||||||||||||
| #include "llvm/CodeGen/MachineFunctionPass.h" | ||||||||||||||||||||||||||
| #include "llvm/CodeGen/MachineJumpTableInfo.h" | ||||||||||||||||||||||||||
| #include "llvm/CodeGen/Passes.h" | ||||||||||||||||||||||||||
| #include "llvm/InitializePasses.h" | ||||||||||||||||||||||||||
| #include "llvm/Pass.h" | ||||||||||||||||||||||||||
| #include "llvm/Support/CommandLine.h" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| using namespace llvm; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| #define DEBUG_TYPE "static-data-splitter" | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| STATISTIC(NumHotJumpTables, "Number of hot jump tables seen"); | ||||||||||||||||||||||||||
| STATISTIC(NumColdJumpTables, "Number of cold jump tables seen"); | ||||||||||||||||||||||||||
| STATISTIC(NumUnknownJumpTables, | ||||||||||||||||||||||||||
| "Number of jump tables with unknown hotness. Such jump tables will " | ||||||||||||||||||||||||||
| "be placed in the hot-suffixed section by default."); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| class StaticDataSplitter : public MachineFunctionPass { | ||||||||||||||||||||||||||
| const MachineBranchProbabilityInfo *MBPI = nullptr; | ||||||||||||||||||||||||||
| const MachineBlockFrequencyInfo *MBFI = nullptr; | ||||||||||||||||||||||||||
| const ProfileSummaryInfo *PSI = nullptr; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Returns true iff any jump table is hot-cold categorized. | ||||||||||||||||||||||||||
| bool splitJumpTables(MachineFunction &MF); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Same as above but works on functions with profile information. | ||||||||||||||||||||||||||
| bool splitJumpTablesWithProfiles(MachineFunction &MF, | ||||||||||||||||||||||||||
| MachineJumpTableInfo &MJTI); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| public: | ||||||||||||||||||||||||||
| static char ID; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| StaticDataSplitter() : MachineFunctionPass(ID) { | ||||||||||||||||||||||||||
| initializeStaticDataSplitterPass(*PassRegistry::getPassRegistry()); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| StringRef getPassName() const override { return "Static Data Splitter"; } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void getAnalysisUsage(AnalysisUsage &AU) const override { | ||||||||||||||||||||||||||
| MachineFunctionPass::getAnalysisUsage(AU); | ||||||||||||||||||||||||||
| AU.addRequired<MachineBranchProbabilityInfoWrapperPass>(); | ||||||||||||||||||||||||||
| AU.addRequired<MachineBlockFrequencyInfoWrapperPass>(); | ||||||||||||||||||||||||||
| AU.addRequired<ProfileSummaryInfoWrapperPass>(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| bool runOnMachineFunction(MachineFunction &MF) override; | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| bool StaticDataSplitter::runOnMachineFunction(MachineFunction &MF) { | ||||||||||||||||||||||||||
| MBPI = &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI(); | ||||||||||||||||||||||||||
| MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI(); | ||||||||||||||||||||||||||
| PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Split jump tables based on profile information. Subsequent patches will | ||||||||||||||||||||||||||
| // handle other data types like constant pools, module-internal data, etc. | ||||||||||||||||||||||||||
| return splitJumpTables(MF); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| bool StaticDataSplitter::splitJumpTablesWithProfiles( | ||||||||||||||||||||||||||
| MachineFunction &MF, MachineJumpTableInfo &MJTI) { | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| int NumChangedJumpTables = 0; | ||||||||||||||||||||||||||
| // Regard a jump table as hot by default. If the source and all of destination | ||||||||||||||||||||||||||
| // blocks are cold, regard the jump table as cold. While a destination block | ||||||||||||||||||||||||||
| // does not read a jump table (unless it's also a source block), a hot | ||||||||||||||||||||||||||
| // destination heuristically makes its jump table hot to accommodate for | ||||||||||||||||||||||||||
| // potential profile data skews (from sampled profiles, for example). | ||||||||||||||||||||||||||
mingmingl-llvm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
| DataHotness Hotness = DataHotness::Hot; | ||||||||||||||||||||||||||
| for (const auto &MBB : MF) { | ||||||||||||||||||||||||||
ellishg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
| // IMPORTANT, `getJumpTableIndex` is a thin wrapper around per-target | ||||||||||||||||||||||||||
| // interface `TargetInstrInfo::getjumpTableIndex`, and only X86 implements | ||||||||||||||||||||||||||
| // it so far. | ||||||||||||||||||||||||||
| const int JTI = MBB.getJumpTableIndex(); | ||||||||||||||||||||||||||
| // This is not a source block of jump table. | ||||||||||||||||||||||||||
| if (JTI == -1) | ||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| bool AllBlocksCold = true; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (!PSI->isColdBlock(&MBB, MBFI)) | ||||||||||||||||||||||||||
| AllBlocksCold = false; | ||||||||||||||||||||||||||
mingmingl-llvm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| for (const MachineBasicBlock *MBB : MJTI.getJumpTables()[JTI].MBBs) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| Binary | hot jump table size (in bytes) before | hot jump table size (in bytes) after | cold jump table size (in bytes) before | cold jump table size (in bytes) after |
|---|---|---|---|---|
| binary1 | 298736 | 326496 | 1510720 | 1482960 |
| binary2 | 73912 | 81448 | 1298560 | 1291024 |
| binary | cold / hot jump table size ratio before | cold / hot jump table size ratio after |
|---|---|---|
| binary 1 | 5.05704033 | 4.542046457 |
| binary 2 | 17.56900097 | 15.85089873 |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to exit quickly upon finding the hot block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would make sense to exit quickly for the original code.
In the updated patch, only source block hotness is used.
Uh oh!
There was an error while loading. Please reload this page.