Skip to content

Commit 8ccc996

Browse files
author
git apple-llvm automerger
committed
Merge commit '4e0cbdac50b0' from apple/master into swift/master-next
2 parents 2f24758 + 4e0cbda commit 8ccc996

File tree

15 files changed

+661
-20
lines changed

15 files changed

+661
-20
lines changed

llvm/include/llvm/CodeGen/MachineBlockFrequencyInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class MachineBlockFrequencyInfo : public MachineFunctionPass {
3838
static char ID;
3939

4040
MachineBlockFrequencyInfo();
41+
explicit MachineBlockFrequencyInfo(MachineFunction &F,
42+
MachineBranchProbabilityInfo &MBPI,
43+
MachineLoopInfo &MLI);
4144
~MachineBlockFrequencyInfo() override;
4245

4346
void getAnalysisUsage(AnalysisUsage &AU) const override;

llvm/include/llvm/CodeGen/MachineDominators.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class MachineDominatorTree : public MachineFunctionPass {
8181
static char ID; // Pass ID, replacement for typeid
8282

8383
MachineDominatorTree();
84+
explicit MachineDominatorTree(MachineFunction &MF) : MachineFunctionPass(ID) {
85+
calculate(MF);
86+
}
8487

8588
DomTreeT &getBase() {
8689
if (!DT) DT.reset(new DomTreeT());
@@ -111,6 +114,8 @@ class MachineDominatorTree : public MachineFunctionPass {
111114

112115
bool runOnMachineFunction(MachineFunction &F) override;
113116

117+
void calculate(MachineFunction &F);
118+
114119
bool dominates(const MachineDomTreeNode *A,
115120
const MachineDomTreeNode *B) const {
116121
applySplitCriticalEdges();

llvm/include/llvm/CodeGen/MachineLoopInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
namespace llvm {
3939

40+
class MachineDominatorTree;
4041
// Implementation in LoopInfoImpl.h
4142
class MachineLoop;
4243
extern template class LoopBase<MachineBasicBlock, MachineLoop>;
@@ -91,6 +92,10 @@ class MachineLoopInfo : public MachineFunctionPass {
9192
MachineLoopInfo() : MachineFunctionPass(ID) {
9293
initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry());
9394
}
95+
explicit MachineLoopInfo(MachineDominatorTree &MDT)
96+
: MachineFunctionPass(ID) {
97+
calculate(MDT);
98+
}
9499
MachineLoopInfo(const MachineLoopInfo &) = delete;
95100
MachineLoopInfo &operator=(const MachineLoopInfo &) = delete;
96101

@@ -133,6 +138,7 @@ class MachineLoopInfo : public MachineFunctionPass {
133138

134139
/// Calculate the natural loop information.
135140
bool runOnMachineFunction(MachineFunction &F) override;
141+
void calculate(MachineDominatorTree &MDT);
136142

137143
void releaseMemory() override { LI.releaseMemory(); }
138144

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===- MachineSizeOpts.h - machine size optimization ------------*- C++ -*-===//
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+
// This file contains some shared machine IR code size optimization related
10+
// code.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
#ifndef LLVM_CODEGEN_MACHINE_SIZEOPTS_H
14+
#define LLVM_CODEGEN_MACHINE_SIZEOPTS_H
15+
16+
#include "llvm/Transforms/Utils/SizeOpts.h"
17+
18+
namespace llvm {
19+
20+
class ProfileSummaryInfo;
21+
class MachineBasicBlock;
22+
class MachineBlockFrequencyInfo;
23+
class MachineFunction;
24+
25+
/// Returns true if machine function \p MF is suggested to be size-optimized
26+
/// base on the profile.
27+
bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI,
28+
const MachineBlockFrequencyInfo *BFI);
29+
/// Returns true if machine basic block \p MBB is suggested to be size-optimized
30+
/// base on the profile.
31+
bool shouldOptimizeForSize(const MachineBasicBlock *MBB,
32+
ProfileSummaryInfo *PSI,
33+
const MachineBlockFrequencyInfo *MBFI);
34+
35+
} // end namespace llvm
36+
37+
#endif // LLVM_CODEGEN_MACHINE_SIZEOPTS_H

llvm/include/llvm/Transforms/Utils/SizeOpts.h

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,71 @@
1313
#ifndef LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
1414
#define LLVM_TRANSFORMS_UTILS_SIZEOPTS_H
1515

16+
#include "llvm/Analysis/BlockFrequencyInfo.h"
17+
#include "llvm/Analysis/ProfileSummaryInfo.h"
18+
#include "llvm/Support/CommandLine.h"
19+
20+
using namespace llvm;
21+
22+
extern cl::opt<bool> EnablePGSO;
23+
extern cl::opt<bool> PGSOLargeWorkingSetSizeOnly;
24+
extern cl::opt<bool> ForcePGSO;
25+
extern cl::opt<int> PgsoCutoffInstrProf;
26+
extern cl::opt<int> PgsoCutoffSampleProf;
27+
1628
namespace llvm {
1729

1830
class BasicBlock;
1931
class BlockFrequencyInfo;
2032
class Function;
2133
class ProfileSummaryInfo;
2234

35+
template<typename AdapterT, typename FuncT, typename BFIT>
36+
bool shouldFuncOptimizeForSizeImpl(const FuncT *F, ProfileSummaryInfo *PSI,
37+
BFIT *BFI) {
38+
assert(F);
39+
if (!PSI || !BFI || !PSI->hasProfileSummary())
40+
return false;
41+
if (ForcePGSO)
42+
return true;
43+
if (!EnablePGSO)
44+
return false;
45+
if (PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize()) {
46+
// Even if the working set size isn't large, size-optimize cold code.
47+
return AdapterT::isFunctionColdInCallGraph(F, PSI, *BFI);
48+
}
49+
return !AdapterT::isFunctionHotInCallGraphNthPercentile(
50+
PSI->hasSampleProfile() ? PgsoCutoffSampleProf : PgsoCutoffInstrProf,
51+
F, PSI, *BFI);
52+
}
53+
54+
template<typename AdapterT, typename BlockT, typename BFIT>
55+
bool shouldOptimizeForSizeImpl(const BlockT *BB, ProfileSummaryInfo *PSI,
56+
BFIT *BFI) {
57+
assert(BB);
58+
if (!PSI || !BFI || !PSI->hasProfileSummary())
59+
return false;
60+
if (ForcePGSO)
61+
return true;
62+
if (!EnablePGSO)
63+
return false;
64+
if (PGSOLargeWorkingSetSizeOnly && !PSI->hasLargeWorkingSetSize()) {
65+
// Even if the working set size isn't large, size-optimize cold code.
66+
return AdapterT::isColdBlock(BB, PSI, BFI);
67+
}
68+
return !AdapterT::isHotBlockNthPercentile(
69+
PSI->hasSampleProfile() ? PgsoCutoffSampleProf : PgsoCutoffInstrProf,
70+
BB, PSI, BFI);
71+
}
72+
2373
/// Returns true if function \p F is suggested to be size-optimized base on the
2474
/// profile.
25-
bool shouldOptimizeForSize(Function *F, ProfileSummaryInfo *PSI,
75+
bool shouldOptimizeForSize(const Function *F, ProfileSummaryInfo *PSI,
2676
BlockFrequencyInfo *BFI);
77+
2778
/// Returns true if basic block \p BB is suggested to be size-optimized base
2879
/// on the profile.
29-
bool shouldOptimizeForSize(BasicBlock *BB, ProfileSummaryInfo *PSI,
80+
bool shouldOptimizeForSize(const BasicBlock *BB, ProfileSummaryInfo *PSI,
3081
BlockFrequencyInfo *BFI);
3182

3283
} // end namespace llvm

llvm/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ add_llvm_library(LLVMCodeGen
9292
MachineRegisterInfo.cpp
9393
MachineScheduler.cpp
9494
MachineSink.cpp
95+
MachineSizeOpts.cpp
9596
MachineSSAUpdater.cpp
9697
MachineTraceMetrics.cpp
9798
MachineVerifier.cpp

llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ MachineBlockFrequencyInfo::MachineBlockFrequencyInfo()
172172
initializeMachineBlockFrequencyInfoPass(*PassRegistry::getPassRegistry());
173173
}
174174

175+
MachineBlockFrequencyInfo::MachineBlockFrequencyInfo(
176+
MachineFunction &F,
177+
MachineBranchProbabilityInfo &MBPI,
178+
MachineLoopInfo &MLI) : MachineFunctionPass(ID) {
179+
calculate(F, MBPI, MLI);
180+
}
181+
175182
MachineBlockFrequencyInfo::~MachineBlockFrequencyInfo() = default;
176183

177184
void MachineBlockFrequencyInfo::getAnalysisUsage(AnalysisUsage &AU) const {

llvm/lib/CodeGen/MachineDominators.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@ void MachineDominatorTree::getAnalysisUsage(AnalysisUsage &AU) const {
4949
}
5050

5151
bool MachineDominatorTree::runOnMachineFunction(MachineFunction &F) {
52+
calculate(F);
53+
return false;
54+
}
55+
56+
void MachineDominatorTree::calculate(MachineFunction &F) {
5257
CriticalEdgesToSplit.clear();
5358
NewBBs.clear();
5459
DT.reset(new DomTreeBase<MachineBasicBlock>());
5560
DT->recalculate(F);
56-
return false;
5761
}
5862

5963
MachineDominatorTree::MachineDominatorTree()

llvm/lib/CodeGen/MachineLoopInfo.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ INITIALIZE_PASS_END(MachineLoopInfo, "machine-loops",
3636
char &llvm::MachineLoopInfoID = MachineLoopInfo::ID;
3737

3838
bool MachineLoopInfo::runOnMachineFunction(MachineFunction &) {
39-
releaseMemory();
40-
LI.analyze(getAnalysis<MachineDominatorTree>().getBase());
39+
calculate(getAnalysis<MachineDominatorTree>());
4140
return false;
4241
}
4342

43+
void MachineLoopInfo::calculate(MachineDominatorTree &MDT) {
44+
releaseMemory();
45+
LI.analyze(MDT.getBase());
46+
}
47+
4448
void MachineLoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
4549
AU.setPreservesAll();
4650
AU.addRequired<MachineDominatorTree>();

llvm/lib/CodeGen/MachineSizeOpts.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//===- MachineSizeOpts.cpp - code size optimization related code ----------===//
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+
// This file contains some shared machine IR code size optimization related
10+
// code.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "llvm/CodeGen/MachineSizeOpts.h"
15+
#include "llvm/Analysis/ProfileSummaryInfo.h"
16+
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
17+
18+
using namespace llvm;
19+
20+
extern cl::opt<bool> EnablePGSO;
21+
extern cl::opt<bool> PGSOLargeWorkingSetSizeOnly;
22+
extern cl::opt<bool> ForcePGSO;
23+
extern cl::opt<int> PgsoCutoffInstrProf;
24+
extern cl::opt<int> PgsoCutoffSampleProf;
25+
26+
namespace machine_size_opts_detail {
27+
28+
/// Like ProfileSummaryInfo::isColdBlock but for MachineBasicBlock.
29+
bool isColdBlock(const MachineBasicBlock *MBB,
30+
ProfileSummaryInfo *PSI,
31+
const MachineBlockFrequencyInfo *MBFI) {
32+
auto Count = MBFI->getBlockProfileCount(MBB);
33+
return Count && PSI->isColdCount(*Count);
34+
}
35+
36+
/// Like ProfileSummaryInfo::isHotBlockNthPercentile but for MachineBasicBlock.
37+
static bool isHotBlockNthPercentile(int PercentileCutoff,
38+
const MachineBasicBlock *MBB,
39+
ProfileSummaryInfo *PSI,
40+
const MachineBlockFrequencyInfo *MBFI) {
41+
auto Count = MBFI->getBlockProfileCount(MBB);
42+
return Count && PSI->isHotCountNthPercentile(PercentileCutoff, *Count);
43+
}
44+
45+
/// Like ProfileSummaryInfo::isFunctionColdInCallGraph but for
46+
/// MachineFunction.
47+
bool isFunctionColdInCallGraph(
48+
const MachineFunction *MF,
49+
ProfileSummaryInfo *PSI,
50+
const MachineBlockFrequencyInfo &MBFI) {
51+
if (auto FunctionCount = MF->getFunction().getEntryCount())
52+
if (!PSI->isColdCount(FunctionCount.getCount()))
53+
return false;
54+
for (const auto &MBB : *MF)
55+
if (!isColdBlock(&MBB, PSI, &MBFI))
56+
return false;
57+
return true;
58+
}
59+
60+
/// Like ProfileSummaryInfo::isFunctionHotInCallGraphNthPercentile but for
61+
/// MachineFunction.
62+
bool isFunctionHotInCallGraphNthPercentile(
63+
int PercentileCutoff,
64+
const MachineFunction *MF,
65+
ProfileSummaryInfo *PSI,
66+
const MachineBlockFrequencyInfo &MBFI) {
67+
if (auto FunctionCount = MF->getFunction().getEntryCount())
68+
if (PSI->isHotCountNthPercentile(PercentileCutoff,
69+
FunctionCount.getCount()))
70+
return true;
71+
for (const auto &MBB : *MF)
72+
if (isHotBlockNthPercentile(PercentileCutoff, &MBB, PSI, &MBFI))
73+
return true;
74+
return false;
75+
}
76+
} // namespace machine_size_opts_detail
77+
78+
namespace {
79+
struct MachineBasicBlockBFIAdapter {
80+
static bool isFunctionColdInCallGraph(const MachineFunction *MF,
81+
ProfileSummaryInfo *PSI,
82+
const MachineBlockFrequencyInfo &MBFI) {
83+
return machine_size_opts_detail::isFunctionColdInCallGraph(MF, PSI, MBFI);
84+
}
85+
static bool isFunctionHotInCallGraphNthPercentile(
86+
int CutOff,
87+
const MachineFunction *MF,
88+
ProfileSummaryInfo *PSI,
89+
const MachineBlockFrequencyInfo &MBFI) {
90+
return machine_size_opts_detail::isFunctionHotInCallGraphNthPercentile(
91+
CutOff, MF, PSI, MBFI);
92+
}
93+
static bool isColdBlock(const MachineBasicBlock *MBB,
94+
ProfileSummaryInfo *PSI,
95+
const MachineBlockFrequencyInfo *MBFI) {
96+
return machine_size_opts_detail::isColdBlock(MBB, PSI, MBFI);
97+
}
98+
static bool isHotBlockNthPercentile(int CutOff,
99+
const MachineBasicBlock *MBB,
100+
ProfileSummaryInfo *PSI,
101+
const MachineBlockFrequencyInfo *MBFI) {
102+
return machine_size_opts_detail::isHotBlockNthPercentile(
103+
CutOff, MBB, PSI, MBFI);
104+
}
105+
};
106+
} // end anonymous namespace
107+
108+
bool llvm::shouldOptimizeForSize(const MachineFunction *MF,
109+
ProfileSummaryInfo *PSI,
110+
const MachineBlockFrequencyInfo *MBFI) {
111+
return shouldFuncOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
112+
MF, PSI, MBFI);
113+
}
114+
115+
bool llvm::shouldOptimizeForSize(const MachineBasicBlock *MBB,
116+
ProfileSummaryInfo *PSI,
117+
const MachineBlockFrequencyInfo *MBFI) {
118+
return shouldOptimizeForSizeImpl<MachineBasicBlockBFIAdapter>(
119+
MBB, PSI, MBFI);
120+
}

0 commit comments

Comments
 (0)