2020-08-05 15:34:31 -07:00
|
|
|
//===-- MachineFunctionSplitter.cpp - Split machine functions //-----------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// \file
|
|
|
|
// Uses profile information to split out cold blocks.
|
|
|
|
//
|
|
|
|
// This pass splits out cold machine basic blocks from the parent function. This
|
|
|
|
// implementation leverages the basic block section framework. Blocks marked
|
|
|
|
// cold by this pass are grouped together in a separate section prefixed with
|
|
|
|
// ".text.unlikely.*". The linker can then group these together as a cold
|
|
|
|
// section. The split part of the function is a contiguous region identified by
|
|
|
|
// the symbol "foo.cold". Grouping all cold blocks across functions together
|
|
|
|
// decreases fragmentation and improves icache and itlb utilization. Note that
|
|
|
|
// the overall changes to the binary size are negligible; only a small number of
|
|
|
|
// additional jump instructions may be introduced.
|
|
|
|
//
|
|
|
|
// For the original RFC of this pass please see
|
|
|
|
// https://groups.google.com/d/msg/llvm-dev/RUegaMg-iqc/wFAVxa6fCgAJ
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-02-09 14:13:47 -08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
[CodeGen] Fine tune MachineFunctionSplitPass (MFS) for FSAFDO.
The original MFS work D85368 shows good performance improvement with
Instrumented FDO. However, AutoFDO or Flow-Sensitive AutoFDO (FSAFDO)
does not show performance gain. This is mainly caused by a less
accurate profile compared to the iFDO profile.
For the past few months, we have been working to improve FSAFDO
quality, like in D145171. Taking advantage of this improvement, MFS
now shows performance improvements over FSAFDO profiles.
That being said, 2 minor changes need to be made, 1) An FS-AutoFDO
profile generation pass needs to be added right before MFS pass and an
FSAFDO profile load pass is needed when FS-AutoFDO is enabled and the
MFS flag is present. 2) MFS only applies to hot functions, because we
believe (and experiment also shows) FS-AutoFDO is more accurate about
functions that have plenty of samples than those with no or very few
samples.
With this improvement, we see a 1.2% performance improvement in clang
benchmark, 0.9% QPS improvement in our internal search benchmark, and
3%-5% improvement in internal storage benchmark.
This is #1 of the two patches that enables the improvement.
Reviewed By: wenlei, snehasish, xur
Differential Revision: https://reviews.llvm.org/D152399
2023-07-10 15:40:01 -07:00
|
|
|
#include "llvm/Analysis/BranchProbabilityInfo.h"
|
2023-01-25 22:26:47 -08:00
|
|
|
#include "llvm/Analysis/EHUtils.h"
|
2020-08-05 15:34:31 -07:00
|
|
|
#include "llvm/Analysis/ProfileSummaryInfo.h"
|
|
|
|
#include "llvm/CodeGen/BasicBlockSectionUtils.h"
|
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2023-08-04 18:58:55 +00:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2020-08-05 15:34:31 -07:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/InitializePasses.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2022-12-05 15:19:30 -08:00
|
|
|
#include <optional>
|
2020-08-05 15:34:31 -07:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2020-10-08 17:36:13 -07:00
|
|
|
// FIXME: This cutoff value is CPU dependent and should be moved to
|
|
|
|
// TargetTransformInfo once we consider enabling this on other platforms.
|
|
|
|
// The value is expressed as a ProfileSummaryInfo integer percentile cutoff.
|
|
|
|
// Defaults to 999950, i.e. all blocks colder than 99.995 percentile are split.
|
|
|
|
// The default was empirically determined to be optimal when considering cutoff
|
|
|
|
// values between 99%-ile to 100%-ile with respect to iTLB and icache metrics on
|
|
|
|
// Intel CPUs.
|
2020-08-05 15:34:31 -07:00
|
|
|
static cl::opt<unsigned>
|
|
|
|
PercentileCutoff("mfs-psi-cutoff",
|
|
|
|
cl::desc("Percentile profile summary cutoff used to "
|
|
|
|
"determine cold blocks. Unused if set to zero."),
|
2020-10-08 17:36:13 -07:00
|
|
|
cl::init(999950), cl::Hidden);
|
2020-08-05 15:34:31 -07:00
|
|
|
|
|
|
|
static cl::opt<unsigned> ColdCountThreshold(
|
|
|
|
"mfs-count-threshold",
|
|
|
|
cl::desc(
|
|
|
|
"Minimum number of times a block must be executed to be retained."),
|
|
|
|
cl::init(1), cl::Hidden);
|
|
|
|
|
2022-08-17 12:13:27 -07:00
|
|
|
static cl::opt<bool> SplitAllEHCode(
|
|
|
|
"mfs-split-ehcode",
|
|
|
|
cl::desc("Splits all EH code and it's descendants by default."),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
2020-08-05 15:34:31 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MachineFunctionSplitter : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
MachineFunctionSplitter() : MachineFunctionPass(ID) {
|
|
|
|
initializeMachineFunctionSplitterPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef getPassName() const override {
|
|
|
|
return "Machine Function Splitter Transformation";
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &F) override;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2022-08-17 12:13:27 -07:00
|
|
|
/// setDescendantEHBlocksCold - This splits all EH pads and blocks reachable
|
2023-01-25 22:26:47 -08:00
|
|
|
/// only by EH pad as cold. This will help mark EH pads statically cold
|
|
|
|
/// instead of relying on profile data.
|
|
|
|
static void setDescendantEHBlocksCold(MachineFunction &MF) {
|
|
|
|
DenseSet<MachineBasicBlock *> EHBlocks;
|
|
|
|
computeEHOnlyBlocks(MF, EHBlocks);
|
|
|
|
for (auto Block : EHBlocks) {
|
|
|
|
Block->setSectionID(MBBSectionID::ColdSectionID);
|
2022-08-17 12:13:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[CodeGen] Fine tune MachineFunctionSplitPass (MFS) for FSAFDO.
The original MFS work D85368 shows good performance improvement with
Instrumented FDO. However, AutoFDO or Flow-Sensitive AutoFDO (FSAFDO)
does not show performance gain. This is mainly caused by a less
accurate profile compared to the iFDO profile.
For the past few months, we have been working to improve FSAFDO
quality, like in D145171. Taking advantage of this improvement, MFS
now shows performance improvements over FSAFDO profiles.
That being said, 2 minor changes need to be made, 1) An FS-AutoFDO
profile generation pass needs to be added right before MFS pass and an
FSAFDO profile load pass is needed when FS-AutoFDO is enabled and the
MFS flag is present. 2) MFS only applies to hot functions, because we
believe (and experiment also shows) FS-AutoFDO is more accurate about
functions that have plenty of samples than those with no or very few
samples.
With this improvement, we see a 1.2% performance improvement in clang
benchmark, 0.9% QPS improvement in our internal search benchmark, and
3%-5% improvement in internal storage benchmark.
This is #1 of the two patches that enables the improvement.
Reviewed By: wenlei, snehasish, xur
Differential Revision: https://reviews.llvm.org/D152399
2023-07-10 15:40:01 -07:00
|
|
|
static void finishAdjustingBasicBlocksAndLandingPads(MachineFunction &MF) {
|
|
|
|
auto Comparator = [](const MachineBasicBlock &X, const MachineBasicBlock &Y) {
|
|
|
|
return X.getSectionID().Type < Y.getSectionID().Type;
|
|
|
|
};
|
|
|
|
llvm::sortBasicBlocksAndUpdateBranches(MF, Comparator);
|
|
|
|
llvm::avoidZeroOffsetLandingPad(MF);
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:13:47 -08:00
|
|
|
static bool isColdBlock(const MachineBasicBlock &MBB,
|
2020-08-05 15:34:31 -07:00
|
|
|
const MachineBlockFrequencyInfo *MBFI,
|
2023-07-18 10:30:06 -07:00
|
|
|
ProfileSummaryInfo *PSI) {
|
2022-12-05 15:19:30 -08:00
|
|
|
std::optional<uint64_t> Count = MBFI->getBlockProfileCount(&MBB);
|
2023-07-18 10:30:06 -07:00
|
|
|
// For instrumentation profiles and sample profiles, we use different ways
|
|
|
|
// to judge whether a block is cold and should be split.
|
|
|
|
if (PSI->hasInstrumentationProfile() || PSI->hasCSInstrumentationProfile()) {
|
|
|
|
// If using instrument profile, which is deemed "accurate", no count means
|
|
|
|
// cold.
|
|
|
|
if (!Count)
|
|
|
|
return true;
|
|
|
|
if (PercentileCutoff > 0)
|
|
|
|
return PSI->isColdCountNthPercentile(PercentileCutoff, *Count);
|
|
|
|
// Fallthrough to end of function.
|
|
|
|
} else if (PSI->hasSampleProfile()) {
|
|
|
|
// For sample profile, no count means "do not judege coldness".
|
|
|
|
if (!Count)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-05 15:34:31 -07:00
|
|
|
return (*Count < ColdCountThreshold);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MachineFunctionSplitter::runOnMachineFunction(MachineFunction &MF) {
|
2022-08-17 12:13:27 -07:00
|
|
|
// We target functions with profile data. Static information in the form
|
|
|
|
// of exception handling code may be split to cold if user passes the
|
|
|
|
// mfs-split-ehcode flag.
|
|
|
|
bool UseProfileData = MF.getFunction().hasProfileData();
|
|
|
|
if (!UseProfileData && !SplitAllEHCode)
|
2020-08-05 15:34:31 -07:00
|
|
|
return false;
|
|
|
|
|
2023-08-04 18:58:55 +00:00
|
|
|
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
|
|
|
|
if (!TII.isFunctionSafeToSplit(MF))
|
2020-08-05 15:34:31 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Renumbering blocks here preserves the order of the blocks as
|
|
|
|
// sortBasicBlocksAndUpdateBranches uses the numeric identifier to sort
|
|
|
|
// blocks. Preserving the order of blocks is essential to retaining decisions
|
|
|
|
// made by prior passes such as MachineBlockPlacement.
|
|
|
|
MF.RenumberBlocks();
|
|
|
|
MF.setBBSectionsType(BasicBlockSection::Preset);
|
2022-08-17 12:13:27 -07:00
|
|
|
|
|
|
|
MachineBlockFrequencyInfo *MBFI = nullptr;
|
|
|
|
ProfileSummaryInfo *PSI = nullptr;
|
|
|
|
if (UseProfileData) {
|
2024-07-12 15:45:01 +08:00
|
|
|
MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
|
2022-08-17 12:13:27 -07:00
|
|
|
PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
|
2023-07-18 10:30:06 -07:00
|
|
|
// If we don't have a good profile (sample profile is not deemed
|
|
|
|
// as a "good profile") and the function is not hot, then early
|
|
|
|
// return. (Because we can only trust hot functions when profile
|
|
|
|
// quality is not good.)
|
|
|
|
if (PSI->hasSampleProfile() && !PSI->isFunctionHotInCallGraph(&MF, *MBFI)) {
|
[CodeGen] Fine tune MachineFunctionSplitPass (MFS) for FSAFDO.
The original MFS work D85368 shows good performance improvement with
Instrumented FDO. However, AutoFDO or Flow-Sensitive AutoFDO (FSAFDO)
does not show performance gain. This is mainly caused by a less
accurate profile compared to the iFDO profile.
For the past few months, we have been working to improve FSAFDO
quality, like in D145171. Taking advantage of this improvement, MFS
now shows performance improvements over FSAFDO profiles.
That being said, 2 minor changes need to be made, 1) An FS-AutoFDO
profile generation pass needs to be added right before MFS pass and an
FSAFDO profile load pass is needed when FS-AutoFDO is enabled and the
MFS flag is present. 2) MFS only applies to hot functions, because we
believe (and experiment also shows) FS-AutoFDO is more accurate about
functions that have plenty of samples than those with no or very few
samples.
With this improvement, we see a 1.2% performance improvement in clang
benchmark, 0.9% QPS improvement in our internal search benchmark, and
3%-5% improvement in internal storage benchmark.
This is #1 of the two patches that enables the improvement.
Reviewed By: wenlei, snehasish, xur
Differential Revision: https://reviews.llvm.org/D152399
2023-07-10 15:40:01 -07:00
|
|
|
// Split all EH code and it's descendant statically by default.
|
|
|
|
if (SplitAllEHCode)
|
|
|
|
setDescendantEHBlocksCold(MF);
|
|
|
|
finishAdjustingBasicBlocksAndLandingPads(MF);
|
|
|
|
return true;
|
|
|
|
}
|
2022-08-17 12:13:27 -07:00
|
|
|
}
|
2020-08-05 15:34:31 -07:00
|
|
|
|
2021-02-09 14:13:47 -08:00
|
|
|
SmallVector<MachineBasicBlock *, 2> LandingPads;
|
2020-08-05 15:34:31 -07:00
|
|
|
for (auto &MBB : MF) {
|
2021-02-09 14:13:47 -08:00
|
|
|
if (MBB.isEntryBlock())
|
2020-08-05 15:34:31 -07:00
|
|
|
continue;
|
2021-02-09 14:13:47 -08:00
|
|
|
|
|
|
|
if (MBB.isEHPad())
|
|
|
|
LandingPads.push_back(&MBB);
|
2024-02-22 03:30:28 +00:00
|
|
|
else if (UseProfileData && isColdBlock(MBB, MBFI, PSI) &&
|
|
|
|
TII.isMBBSafeToSplitToCold(MBB) && !SplitAllEHCode)
|
2020-08-05 15:34:31 -07:00
|
|
|
MBB.setSectionID(MBBSectionID::ColdSectionID);
|
|
|
|
}
|
|
|
|
|
2022-08-17 12:13:27 -07:00
|
|
|
// Split all EH code and it's descendant statically by default.
|
|
|
|
if (SplitAllEHCode)
|
2023-01-25 22:26:47 -08:00
|
|
|
setDescendantEHBlocksCold(MF);
|
2021-02-09 14:13:47 -08:00
|
|
|
// We only split out eh pads if all of them are cold.
|
2022-08-17 12:13:27 -07:00
|
|
|
else {
|
[CodeGen] Fine tune MachineFunctionSplitPass (MFS) for FSAFDO.
The original MFS work D85368 shows good performance improvement with
Instrumented FDO. However, AutoFDO or Flow-Sensitive AutoFDO (FSAFDO)
does not show performance gain. This is mainly caused by a less
accurate profile compared to the iFDO profile.
For the past few months, we have been working to improve FSAFDO
quality, like in D145171. Taking advantage of this improvement, MFS
now shows performance improvements over FSAFDO profiles.
That being said, 2 minor changes need to be made, 1) An FS-AutoFDO
profile generation pass needs to be added right before MFS pass and an
FSAFDO profile load pass is needed when FS-AutoFDO is enabled and the
MFS flag is present. 2) MFS only applies to hot functions, because we
believe (and experiment also shows) FS-AutoFDO is more accurate about
functions that have plenty of samples than those with no or very few
samples.
With this improvement, we see a 1.2% performance improvement in clang
benchmark, 0.9% QPS improvement in our internal search benchmark, and
3%-5% improvement in internal storage benchmark.
This is #1 of the two patches that enables the improvement.
Reviewed By: wenlei, snehasish, xur
Differential Revision: https://reviews.llvm.org/D152399
2023-07-10 15:40:01 -07:00
|
|
|
// Here we have UseProfileData == true.
|
2022-08-17 12:13:27 -07:00
|
|
|
bool HasHotLandingPads = false;
|
|
|
|
for (const MachineBasicBlock *LP : LandingPads) {
|
2024-02-22 03:30:28 +00:00
|
|
|
if (!isColdBlock(*LP, MBFI, PSI) || !TII.isMBBSafeToSplitToCold(*LP))
|
2022-08-17 12:13:27 -07:00
|
|
|
HasHotLandingPads = true;
|
|
|
|
}
|
|
|
|
if (!HasHotLandingPads) {
|
|
|
|
for (MachineBasicBlock *LP : LandingPads)
|
|
|
|
LP->setSectionID(MBBSectionID::ColdSectionID);
|
|
|
|
}
|
2021-02-09 14:13:47 -08:00
|
|
|
}
|
[CodeGen] Fine tune MachineFunctionSplitPass (MFS) for FSAFDO.
The original MFS work D85368 shows good performance improvement with
Instrumented FDO. However, AutoFDO or Flow-Sensitive AutoFDO (FSAFDO)
does not show performance gain. This is mainly caused by a less
accurate profile compared to the iFDO profile.
For the past few months, we have been working to improve FSAFDO
quality, like in D145171. Taking advantage of this improvement, MFS
now shows performance improvements over FSAFDO profiles.
That being said, 2 minor changes need to be made, 1) An FS-AutoFDO
profile generation pass needs to be added right before MFS pass and an
FSAFDO profile load pass is needed when FS-AutoFDO is enabled and the
MFS flag is present. 2) MFS only applies to hot functions, because we
believe (and experiment also shows) FS-AutoFDO is more accurate about
functions that have plenty of samples than those with no or very few
samples.
With this improvement, we see a 1.2% performance improvement in clang
benchmark, 0.9% QPS improvement in our internal search benchmark, and
3%-5% improvement in internal storage benchmark.
This is #1 of the two patches that enables the improvement.
Reviewed By: wenlei, snehasish, xur
Differential Revision: https://reviews.llvm.org/D152399
2023-07-10 15:40:01 -07:00
|
|
|
|
|
|
|
finishAdjustingBasicBlocksAndLandingPads(MF);
|
2020-08-05 15:34:31 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineFunctionSplitter::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<MachineModuleInfoWrapperPass>();
|
2024-07-12 15:45:01 +08:00
|
|
|
AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
|
2020-08-05 15:34:31 -07:00
|
|
|
AU.addRequired<ProfileSummaryInfoWrapperPass>();
|
|
|
|
}
|
|
|
|
|
|
|
|
char MachineFunctionSplitter::ID = 0;
|
|
|
|
INITIALIZE_PASS(MachineFunctionSplitter, "machine-function-splitter",
|
|
|
|
"Split machine functions using profile information", false,
|
|
|
|
false)
|
|
|
|
|
|
|
|
MachineFunctionPass *llvm::createMachineFunctionSplitterPass() {
|
|
|
|
return new MachineFunctionSplitter();
|
|
|
|
}
|