aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/CodeGen/MachineFunctionSplitter.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-07-26 19:03:47 +0000
committerDimitry Andric <dim@FreeBSD.org>2023-07-26 19:04:23 +0000
commit7fa27ce4a07f19b07799a767fc29416f3b625afb (patch)
tree27825c83636c4de341eb09a74f49f5d38a15d165 /llvm/lib/CodeGen/MachineFunctionSplitter.cpp
parente3b557809604d036af6e00c60f012c2025b59a5e (diff)
Diffstat (limited to 'llvm/lib/CodeGen/MachineFunctionSplitter.cpp')
-rw-r--r--llvm/lib/CodeGen/MachineFunctionSplitter.cpp126
1 files changed, 47 insertions, 79 deletions
diff --git a/llvm/lib/CodeGen/MachineFunctionSplitter.cpp b/llvm/lib/CodeGen/MachineFunctionSplitter.cpp
index 613c52900331..fbc071536d22 100644
--- a/llvm/lib/CodeGen/MachineFunctionSplitter.cpp
+++ b/llvm/lib/CodeGen/MachineFunctionSplitter.cpp
@@ -24,6 +24,9 @@
//===----------------------------------------------------------------------===//
#include "llvm/ADT/SmallVector.h"
+#include "llvm/Analysis/BlockFrequencyInfo.h"
+#include "llvm/Analysis/BranchProbabilityInfo.h"
+#include "llvm/Analysis/EHUtils.h"
#include "llvm/Analysis/ProfileSummaryInfo.h"
#include "llvm/CodeGen/BasicBlockSectionUtils.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
@@ -83,88 +86,44 @@ public:
} // end anonymous namespace
/// setDescendantEHBlocksCold - This splits all EH pads and blocks reachable
-/// only by EH pad as cold. This will help mark EH pads statically cold instead
-/// of relying on profile data.
-static void
-setDescendantEHBlocksCold(SmallVectorImpl<MachineBasicBlock *> &EHBlocks,
- MachineFunction &MF) {
- MachineBasicBlock *StartBlock = &MF.front();
- // A block can be unknown if its not reachable from anywhere
- // EH if its only reachable from start blocks via some path through EH pads
- // NonEH if it's reachable from Non EH blocks as well.
- enum Status { Unknown = 0, EH = 1, NonEH = 2 };
- DenseSet<MachineBasicBlock *> WorkList;
- DenseMap<MachineBasicBlock *, Status> Statuses;
-
- auto getStatus = [&](MachineBasicBlock *MBB) {
- if (Statuses.find(MBB) != Statuses.end())
- return Statuses[MBB];
- else
- return Unknown;
- };
-
- auto checkPredecessors = [&](MachineBasicBlock *MBB, Status Stat) {
- for (auto *PredMBB : MBB->predecessors()) {
- Status PredStatus = getStatus(PredMBB);
- // If status of predecessor block has gone above current block
- // we update current blocks status.
- if (PredStatus > Stat)
- Stat = PredStatus;
- }
- return Stat;
- };
-
- auto addSuccesors = [&](MachineBasicBlock *MBB) {
- for (auto *SuccMBB : MBB->successors()) {
- if (!SuccMBB->isEHPad())
- WorkList.insert(SuccMBB);
- }
- };
-
- // Insert the successors of start block
- // and landing pads successor.
- Statuses[StartBlock] = NonEH;
- addSuccesors(StartBlock);
- for (auto *LP : EHBlocks) {
- addSuccesors(LP);
- Statuses[LP] = EH;
- }
-
- // Worklist iterative algorithm.
- while (!WorkList.empty()) {
- auto *MBB = *WorkList.begin();
- WorkList.erase(MBB);
-
- Status OldStatus = getStatus(MBB);
-
- // Check on predecessors and check for
- // Status update.
- Status NewStatus = checkPredecessors(MBB, OldStatus);
-
- // Did the block status change?
- bool changed = OldStatus != NewStatus;
- if (changed) {
- addSuccesors(MBB);
- Statuses[MBB] = NewStatus;
- }
+/// 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);
}
+}
- for (auto Entry : Statuses) {
- if (Entry.second == EH)
- Entry.first->setSectionID(MBBSectionID::ColdSectionID);
- }
+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);
}
static bool isColdBlock(const MachineBasicBlock &MBB,
const MachineBlockFrequencyInfo *MBFI,
ProfileSummaryInfo *PSI) {
std::optional<uint64_t> Count = MBFI->getBlockProfileCount(&MBB);
- if (!Count)
- return true;
-
- if (PercentileCutoff > 0) {
- return PSI->isColdCountNthPercentile(PercentileCutoff, *Count);
+ // 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;
}
+
return (*Count < ColdCountThreshold);
}
@@ -204,6 +163,17 @@ bool MachineFunctionSplitter::runOnMachineFunction(MachineFunction &MF) {
if (UseProfileData) {
MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
+ // 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)) {
+ // Split all EH code and it's descendant statically by default.
+ if (SplitAllEHCode)
+ setDescendantEHBlocksCold(MF);
+ finishAdjustingBasicBlocksAndLandingPads(MF);
+ return true;
+ }
}
SmallVector<MachineBasicBlock *, 2> LandingPads;
@@ -219,9 +189,10 @@ bool MachineFunctionSplitter::runOnMachineFunction(MachineFunction &MF) {
// Split all EH code and it's descendant statically by default.
if (SplitAllEHCode)
- setDescendantEHBlocksCold(LandingPads, MF);
+ setDescendantEHBlocksCold(MF);
// We only split out eh pads if all of them are cold.
else {
+ // Here we have UseProfileData == true.
bool HasHotLandingPads = false;
for (const MachineBasicBlock *LP : LandingPads) {
if (!isColdBlock(*LP, MBFI, PSI))
@@ -232,11 +203,8 @@ bool MachineFunctionSplitter::runOnMachineFunction(MachineFunction &MF) {
LP->setSectionID(MBBSectionID::ColdSectionID);
}
}
- auto Comparator = [](const MachineBasicBlock &X, const MachineBasicBlock &Y) {
- return X.getSectionID().Type < Y.getSectionID().Type;
- };
- llvm::sortBasicBlocksAndUpdateBranches(MF, Comparator);
- llvm::avoidZeroOffsetLandingPad(MF);
+
+ finishAdjustingBasicBlocksAndLandingPads(MF);
return true;
}