Skip to content

Commit b68340c

Browse files
authored
[CodeGen][NewPM] Port SpillPlacement analysis to NPM (#116618)
1 parent 0d1d1b3 commit b68340c

File tree

7 files changed

+107
-37
lines changed

7 files changed

+107
-37
lines changed

llvm/lib/CodeGen/SpillPlacement.h renamed to llvm/include/llvm/CodeGen/SpillPlacement.h

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/ADT/ArrayRef.h"
3030
#include "llvm/ADT/SmallVector.h"
3131
#include "llvm/ADT/SparseSet.h"
32+
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
3233
#include "llvm/CodeGen/MachineFunctionPass.h"
3334
#include "llvm/Support/BlockFrequency.h"
3435

@@ -38,13 +39,20 @@ class BitVector;
3839
class EdgeBundles;
3940
class MachineBlockFrequencyInfo;
4041
class MachineFunction;
42+
class SpillPlacementWrapperLegacy;
43+
class SpillPlacementAnalysis;
44+
45+
class SpillPlacement {
46+
friend class SpillPlacementWrapperLegacy;
47+
friend class SpillPlacementAnalysis;
4148

42-
class SpillPlacement : public MachineFunctionPass {
4349
struct Node;
50+
4451
const MachineFunction *MF = nullptr;
4552
const EdgeBundles *bundles = nullptr;
4653
const MachineBlockFrequencyInfo *MBFI = nullptr;
47-
Node *nodes = nullptr;
54+
55+
std::unique_ptr<Node[]> nodes;
4856

4957
// Nodes that are active in the current computation. Owned by the prepare()
5058
// caller.
@@ -68,11 +76,6 @@ class SpillPlacement : public MachineFunctionPass {
6876
SparseSet<unsigned> TodoList;
6977

7078
public:
71-
static char ID; // Pass identification, replacement for typeid.
72-
73-
SpillPlacement() : MachineFunctionPass(ID) {}
74-
~SpillPlacement() override { releaseMemory(); }
75-
7679
/// BorderConstraint - A basic block has separate constraints for entry and
7780
/// exit.
7881
enum BorderConstraint {
@@ -154,17 +157,50 @@ class SpillPlacement : public MachineFunctionPass {
154157
return BlockFrequencies[Number];
155158
}
156159

160+
bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
161+
MachineFunctionAnalysisManager::Invalidator &Inv);
162+
163+
SpillPlacement(SpillPlacement &&);
164+
~SpillPlacement();
165+
157166
private:
158-
bool runOnMachineFunction(MachineFunction &mf) override;
159-
void getAnalysisUsage(AnalysisUsage &AU) const override;
160-
void releaseMemory() override;
167+
SpillPlacement();
161168

169+
void releaseMemory();
170+
171+
void run(MachineFunction &MF, EdgeBundles *Bundles,
172+
MachineBlockFrequencyInfo *MBFI);
162173
void activate(unsigned n);
163174
void setThreshold(BlockFrequency Entry);
164175

165176
bool update(unsigned n);
166177
};
167178

179+
class SpillPlacementWrapperLegacy : public MachineFunctionPass {
180+
public:
181+
static char ID;
182+
SpillPlacementWrapperLegacy() : MachineFunctionPass(ID) {}
183+
184+
SpillPlacement &getResult() { return Impl; }
185+
const SpillPlacement &getResult() const { return Impl; }
186+
187+
private:
188+
SpillPlacement Impl;
189+
bool runOnMachineFunction(MachineFunction &MF) override;
190+
void getAnalysisUsage(AnalysisUsage &AU) const override;
191+
void releaseMemory() override { Impl.releaseMemory(); }
192+
};
193+
194+
class SpillPlacementAnalysis
195+
: public AnalysisInfoMixin<SpillPlacementAnalysis> {
196+
friend AnalysisInfoMixin<SpillPlacementAnalysis>;
197+
static AnalysisKey Key;
198+
199+
public:
200+
using Result = SpillPlacement;
201+
SpillPlacement run(MachineFunction &, MachineFunctionAnalysisManager &);
202+
};
203+
168204
} // end namespace llvm
169205

170206
#endif // LLVM_LIB_CODEGEN_SPILLPLACEMENT_H

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ void initializeSinkingLegacyPassPass(PassRegistry &);
289289
void initializeSjLjEHPreparePass(PassRegistry &);
290290
void initializeSlotIndexesWrapperPassPass(PassRegistry &);
291291
void initializeSpeculativeExecutionLegacyPassPass(PassRegistry &);
292-
void initializeSpillPlacementPass(PassRegistry &);
292+
void initializeSpillPlacementWrapperLegacyPass(PassRegistry &);
293293
void initializeStackColoringLegacyPass(PassRegistry &);
294294
void initializeStackFrameLayoutAnalysisPassPass(PassRegistry &);
295295
void initializeStackMapLivenessPass(PassRegistry &);

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ MACHINE_FUNCTION_ANALYSIS("machine-post-dom-tree",
113113
MACHINE_FUNCTION_ANALYSIS("machine-trace-metrics", MachineTraceMetricsAnalysis())
114114
MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
115115
MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
116+
MACHINE_FUNCTION_ANALYSIS("spill-code-placement", SpillPlacementAnalysis())
116117
MACHINE_FUNCTION_ANALYSIS("virtregmap", VirtRegMapAnalysis())
117118
// MACHINE_FUNCTION_ANALYSIS("live-stacks", LiveStacksPass())
118119
// MACHINE_FUNCTION_ANALYSIS("lazy-machine-bfi",

llvm/lib/CodeGen/RegAllocGreedy.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "RegAllocBase.h"
1818
#include "RegAllocEvictionAdvisor.h"
1919
#include "RegAllocPriorityAdvisor.h"
20-
#include "SpillPlacement.h"
2120
#include "SplitKit.h"
2221
#include "llvm/ADT/ArrayRef.h"
2322
#include "llvm/ADT/BitVector.h"
@@ -50,6 +49,7 @@
5049
#include "llvm/CodeGen/RegAllocRegistry.h"
5150
#include "llvm/CodeGen/RegisterClassInfo.h"
5251
#include "llvm/CodeGen/SlotIndexes.h"
52+
#include "llvm/CodeGen/SpillPlacement.h"
5353
#include "llvm/CodeGen/Spiller.h"
5454
#include "llvm/CodeGen/TargetInstrInfo.h"
5555
#include "llvm/CodeGen/TargetRegisterInfo.h"
@@ -162,7 +162,7 @@ INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
162162
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
163163
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy)
164164
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
165-
INITIALIZE_PASS_DEPENDENCY(SpillPlacement)
165+
INITIALIZE_PASS_DEPENDENCY(SpillPlacementWrapperLegacy)
166166
INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
167167
INITIALIZE_PASS_DEPENDENCY(RegAllocEvictionAdvisorAnalysis)
168168
INITIALIZE_PASS_DEPENDENCY(RegAllocPriorityAdvisorAnalysis)
@@ -217,7 +217,7 @@ void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
217217
AU.addRequired<LiveRegMatrixWrapperLegacy>();
218218
AU.addPreserved<LiveRegMatrixWrapperLegacy>();
219219
AU.addRequired<EdgeBundlesWrapperLegacy>();
220-
AU.addRequired<SpillPlacement>();
220+
AU.addRequired<SpillPlacementWrapperLegacy>();
221221
AU.addRequired<MachineOptimizationRemarkEmitterPass>();
222222
AU.addRequired<RegAllocEvictionAdvisorAnalysis>();
223223
AU.addRequired<RegAllocPriorityAdvisorAnalysis>();
@@ -2731,7 +2731,7 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
27312731
ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
27322732
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
27332733
Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
2734-
SpillPlacer = &getAnalysis<SpillPlacement>();
2734+
SpillPlacer = &getAnalysis<SpillPlacementWrapperLegacy>().getResult();
27352735
DebugVars = &getAnalysis<LiveDebugVariables>();
27362736

27372737
initializeCSRCost();

llvm/lib/CodeGen/RegAllocGreedy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "RegAllocBase.h"
1717
#include "RegAllocEvictionAdvisor.h"
1818
#include "RegAllocPriorityAdvisor.h"
19-
#include "SpillPlacement.h"
2019
#include "SplitKit.h"
2120
#include "llvm/ADT/ArrayRef.h"
2221
#include "llvm/ADT/BitVector.h"
@@ -30,6 +29,7 @@
3029
#include "llvm/CodeGen/MachineFunction.h"
3130
#include "llvm/CodeGen/MachineFunctionPass.h"
3231
#include "llvm/CodeGen/RegisterClassInfo.h"
32+
#include "llvm/CodeGen/SpillPlacement.h"
3333
#include "llvm/CodeGen/Spiller.h"
3434
#include "llvm/CodeGen/TargetRegisterInfo.h"
3535
#include <algorithm>

llvm/lib/CodeGen/SpillPlacement.cpp

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//
2727
//===----------------------------------------------------------------------===//
2828

29-
#include "SpillPlacement.h"
29+
#include "llvm/CodeGen/SpillPlacement.h"
3030
#include "llvm/ADT/BitVector.h"
3131
#include "llvm/CodeGen/EdgeBundles.h"
3232
#include "llvm/CodeGen/MachineBasicBlock.h"
@@ -44,17 +44,17 @@ using namespace llvm;
4444

4545
#define DEBUG_TYPE "spill-code-placement"
4646

47-
char SpillPlacement::ID = 0;
47+
char SpillPlacementWrapperLegacy::ID = 0;
4848

49-
char &llvm::SpillPlacementID = SpillPlacement::ID;
49+
char &llvm::SpillPlacementID = SpillPlacementWrapperLegacy::ID;
5050

51-
INITIALIZE_PASS_BEGIN(SpillPlacement, DEBUG_TYPE,
51+
INITIALIZE_PASS_BEGIN(SpillPlacementWrapperLegacy, DEBUG_TYPE,
5252
"Spill Code Placement Analysis", true, true)
5353
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
54-
INITIALIZE_PASS_END(SpillPlacement, DEBUG_TYPE,
54+
INITIALIZE_PASS_END(SpillPlacementWrapperLegacy, DEBUG_TYPE,
5555
"Spill Code Placement Analysis", true, true)
5656

57-
void SpillPlacement::getAnalysisUsage(AnalysisUsage &AU) const {
57+
void SpillPlacementWrapperLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
5858
AU.setPreservesAll();
5959
AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
6060
AU.addRequiredTransitive<EdgeBundlesWrapperLegacy>();
@@ -189,32 +189,64 @@ struct SpillPlacement::Node {
189189
}
190190
};
191191

192-
bool SpillPlacement::runOnMachineFunction(MachineFunction &mf) {
192+
bool SpillPlacementWrapperLegacy::runOnMachineFunction(MachineFunction &MF) {
193+
auto *Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
194+
auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
195+
196+
Impl.run(MF, Bundles, MBFI);
197+
return false;
198+
}
199+
200+
AnalysisKey SpillPlacementAnalysis::Key;
201+
202+
SpillPlacement
203+
SpillPlacementAnalysis::run(MachineFunction &MF,
204+
MachineFunctionAnalysisManager &MFAM) {
205+
auto *Bundles = &MFAM.getResult<EdgeBundlesAnalysis>(MF);
206+
auto *MBFI = &MFAM.getResult<MachineBlockFrequencyAnalysis>(MF);
207+
SpillPlacement Impl;
208+
Impl.run(MF, Bundles, MBFI);
209+
return Impl;
210+
}
211+
212+
bool SpillPlacementAnalysis::Result::invalidate(
213+
MachineFunction &MF, const PreservedAnalyses &PA,
214+
MachineFunctionAnalysisManager::Invalidator &Inv) {
215+
auto PAC = PA.getChecker<SpillPlacementAnalysis>();
216+
if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<MachineFunction>>())
217+
return true;
218+
// Check dependencies.
219+
return Inv.invalidate<EdgeBundlesAnalysis>(MF, PA) ||
220+
Inv.invalidate<MachineBlockFrequencyAnalysis>(MF, PA);
221+
}
222+
223+
SpillPlacement::SpillPlacement() = default;
224+
SpillPlacement::~SpillPlacement() = default;
225+
SpillPlacement::SpillPlacement(SpillPlacement &&) = default;
226+
227+
void SpillPlacement::releaseMemory() {
228+
nodes.reset();
229+
TodoList.clear();
230+
}
231+
232+
void SpillPlacement::run(MachineFunction &mf, EdgeBundles *Bundles,
233+
MachineBlockFrequencyInfo *MBFI) {
193234
MF = &mf;
194-
bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
235+
this->bundles = Bundles;
236+
this->MBFI = MBFI;
195237

196238
assert(!nodes && "Leaking node array");
197-
nodes = new Node[bundles->getNumBundles()];
239+
nodes.reset(new Node[bundles->getNumBundles()]);
198240
TodoList.clear();
199241
TodoList.setUniverse(bundles->getNumBundles());
200242

201243
// Compute total ingoing and outgoing block frequencies for all bundles.
202244
BlockFrequencies.resize(mf.getNumBlockIDs());
203-
MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
204245
setThreshold(MBFI->getEntryFreq());
205246
for (auto &I : mf) {
206247
unsigned Num = I.getNumber();
207248
BlockFrequencies[Num] = MBFI->getBlockFreq(&I);
208249
}
209-
210-
// We never change the function.
211-
return false;
212-
}
213-
214-
void SpillPlacement::releaseMemory() {
215-
delete[] nodes;
216-
nodes = nullptr;
217-
TodoList.clear();
218250
}
219251

220252
/// activate - mark node n as active if it wasn't already.
@@ -323,9 +355,9 @@ bool SpillPlacement::scanActiveBundles() {
323355
}
324356

325357
bool SpillPlacement::update(unsigned n) {
326-
if (!nodes[n].update(nodes, Threshold))
358+
if (!nodes[n].update(nodes.get(), Threshold))
327359
return false;
328-
nodes[n].getDissentingNeighbors(TodoList, nodes);
360+
nodes[n].getDissentingNeighbors(TodoList, nodes.get());
329361
return true;
330362
}
331363

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
#include "llvm/CodeGen/ShadowStackGCLowering.h"
131131
#include "llvm/CodeGen/SjLjEHPrepare.h"
132132
#include "llvm/CodeGen/SlotIndexes.h"
133+
#include "llvm/CodeGen/SpillPlacement.h"
133134
#include "llvm/CodeGen/StackColoring.h"
134135
#include "llvm/CodeGen/StackProtector.h"
135136
#include "llvm/CodeGen/TailDuplication.h"

0 commit comments

Comments
 (0)