Skip to content

[CodeGen][NewPM] Port SpillPlacement analysis to NPM #116618

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/SparseSet.h"
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/Support/BlockFrequency.h"

Expand All @@ -38,13 +39,20 @@ class BitVector;
class EdgeBundles;
class MachineBlockFrequencyInfo;
class MachineFunction;
class SpillPlacementWrapperLegacy;
class SpillPlacementAnalysis;

class SpillPlacement {
friend class SpillPlacementWrapperLegacy;
friend class SpillPlacementAnalysis;

class SpillPlacement : public MachineFunctionPass {
struct Node;

const MachineFunction *MF = nullptr;
const EdgeBundles *bundles = nullptr;
const MachineBlockFrequencyInfo *MBFI = nullptr;
Node *nodes = nullptr;

std::unique_ptr<Node[]> nodes;

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

public:
static char ID; // Pass identification, replacement for typeid.

SpillPlacement() : MachineFunctionPass(ID) {}
~SpillPlacement() override { releaseMemory(); }

/// BorderConstraint - A basic block has separate constraints for entry and
/// exit.
enum BorderConstraint {
Expand Down Expand Up @@ -154,17 +157,50 @@ class SpillPlacement : public MachineFunctionPass {
return BlockFrequencies[Number];
}

bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
MachineFunctionAnalysisManager::Invalidator &Inv);

SpillPlacement(SpillPlacement &&);
~SpillPlacement();

private:
bool runOnMachineFunction(MachineFunction &mf) override;
void getAnalysisUsage(AnalysisUsage &AU) const override;
void releaseMemory() override;
SpillPlacement();

void releaseMemory();

void run(MachineFunction &MF, EdgeBundles *Bundles,
MachineBlockFrequencyInfo *MBFI);
void activate(unsigned n);
void setThreshold(BlockFrequency Entry);

bool update(unsigned n);
};

class SpillPlacementWrapperLegacy : public MachineFunctionPass {
public:
static char ID;
SpillPlacementWrapperLegacy() : MachineFunctionPass(ID) {}

SpillPlacement &getResult() { return Impl; }
const SpillPlacement &getResult() const { return Impl; }

private:
SpillPlacement Impl;
bool runOnMachineFunction(MachineFunction &MF) override;
void getAnalysisUsage(AnalysisUsage &AU) const override;
void releaseMemory() override { Impl.releaseMemory(); }
};

class SpillPlacementAnalysis
: public AnalysisInfoMixin<SpillPlacementAnalysis> {
friend AnalysisInfoMixin<SpillPlacementAnalysis>;
static AnalysisKey Key;

public:
using Result = SpillPlacement;
SpillPlacement run(MachineFunction &, MachineFunctionAnalysisManager &);
};

} // end namespace llvm

#endif // LLVM_LIB_CODEGEN_SPILLPLACEMENT_H
2 changes: 1 addition & 1 deletion llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void initializeSinkingLegacyPassPass(PassRegistry &);
void initializeSjLjEHPreparePass(PassRegistry &);
void initializeSlotIndexesWrapperPassPass(PassRegistry &);
void initializeSpeculativeExecutionLegacyPassPass(PassRegistry &);
void initializeSpillPlacementPass(PassRegistry &);
void initializeSpillPlacementWrapperLegacyPass(PassRegistry &);
void initializeStackColoringLegacyPass(PassRegistry &);
void initializeStackFrameLayoutAnalysisPassPass(PassRegistry &);
void initializeStackMapLivenessPass(PassRegistry &);
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/Passes/MachinePassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ MACHINE_FUNCTION_ANALYSIS("machine-post-dom-tree",
MACHINE_FUNCTION_ANALYSIS("machine-trace-metrics", MachineTraceMetricsAnalysis())
MACHINE_FUNCTION_ANALYSIS("pass-instrumentation", PassInstrumentationAnalysis(PIC))
MACHINE_FUNCTION_ANALYSIS("slot-indexes", SlotIndexesAnalysis())
MACHINE_FUNCTION_ANALYSIS("spill-code-placement", SpillPlacementAnalysis())
MACHINE_FUNCTION_ANALYSIS("virtregmap", VirtRegMapAnalysis())
// MACHINE_FUNCTION_ANALYSIS("live-stacks", LiveStacksPass())
// MACHINE_FUNCTION_ANALYSIS("lazy-machine-bfi",
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/CodeGen/RegAllocGreedy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "RegAllocBase.h"
#include "RegAllocEvictionAdvisor.h"
#include "RegAllocPriorityAdvisor.h"
#include "SpillPlacement.h"
#include "SplitKit.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitVector.h"
Expand Down Expand Up @@ -50,6 +49,7 @@
#include "llvm/CodeGen/RegAllocRegistry.h"
#include "llvm/CodeGen/RegisterClassInfo.h"
#include "llvm/CodeGen/SlotIndexes.h"
#include "llvm/CodeGen/SpillPlacement.h"
#include "llvm/CodeGen/Spiller.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
Expand Down Expand Up @@ -162,7 +162,7 @@ INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy)
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
INITIALIZE_PASS_DEPENDENCY(SpillPlacement)
INITIALIZE_PASS_DEPENDENCY(SpillPlacementWrapperLegacy)
INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
INITIALIZE_PASS_DEPENDENCY(RegAllocEvictionAdvisorAnalysis)
INITIALIZE_PASS_DEPENDENCY(RegAllocPriorityAdvisorAnalysis)
Expand Down Expand Up @@ -217,7 +217,7 @@ void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<LiveRegMatrixWrapperLegacy>();
AU.addPreserved<LiveRegMatrixWrapperLegacy>();
AU.addRequired<EdgeBundlesWrapperLegacy>();
AU.addRequired<SpillPlacement>();
AU.addRequired<SpillPlacementWrapperLegacy>();
AU.addRequired<MachineOptimizationRemarkEmitterPass>();
AU.addRequired<RegAllocEvictionAdvisorAnalysis>();
AU.addRequired<RegAllocPriorityAdvisorAnalysis>();
Expand Down Expand Up @@ -2731,7 +2731,7 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
SpillPlacer = &getAnalysis<SpillPlacement>();
SpillPlacer = &getAnalysis<SpillPlacementWrapperLegacy>().getResult();
DebugVars = &getAnalysis<LiveDebugVariables>();

initializeCSRCost();
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/RegAllocGreedy.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "RegAllocBase.h"
#include "RegAllocEvictionAdvisor.h"
#include "RegAllocPriorityAdvisor.h"
#include "SpillPlacement.h"
#include "SplitKit.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitVector.h"
Expand All @@ -30,6 +29,7 @@
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/RegisterClassInfo.h"
#include "llvm/CodeGen/SpillPlacement.h"
#include "llvm/CodeGen/Spiller.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include <algorithm>
Expand Down
74 changes: 53 additions & 21 deletions llvm/lib/CodeGen/SpillPlacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//
//===----------------------------------------------------------------------===//

#include "SpillPlacement.h"
#include "llvm/CodeGen/SpillPlacement.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/CodeGen/EdgeBundles.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
Expand All @@ -44,17 +44,17 @@ using namespace llvm;

#define DEBUG_TYPE "spill-code-placement"

char SpillPlacement::ID = 0;
char SpillPlacementWrapperLegacy::ID = 0;

char &llvm::SpillPlacementID = SpillPlacement::ID;
char &llvm::SpillPlacementID = SpillPlacementWrapperLegacy::ID;

INITIALIZE_PASS_BEGIN(SpillPlacement, DEBUG_TYPE,
INITIALIZE_PASS_BEGIN(SpillPlacementWrapperLegacy, DEBUG_TYPE,
"Spill Code Placement Analysis", true, true)
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
INITIALIZE_PASS_END(SpillPlacement, DEBUG_TYPE,
INITIALIZE_PASS_END(SpillPlacementWrapperLegacy, DEBUG_TYPE,
"Spill Code Placement Analysis", true, true)

void SpillPlacement::getAnalysisUsage(AnalysisUsage &AU) const {
void SpillPlacementWrapperLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired<MachineBlockFrequencyInfoWrapperPass>();
AU.addRequiredTransitive<EdgeBundlesWrapperLegacy>();
Expand Down Expand Up @@ -189,32 +189,64 @@ struct SpillPlacement::Node {
}
};

bool SpillPlacement::runOnMachineFunction(MachineFunction &mf) {
bool SpillPlacementWrapperLegacy::runOnMachineFunction(MachineFunction &MF) {
auto *Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();

Impl.run(MF, Bundles, MBFI);
return false;
}

AnalysisKey SpillPlacementAnalysis::Key;

SpillPlacement
SpillPlacementAnalysis::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
auto *Bundles = &MFAM.getResult<EdgeBundlesAnalysis>(MF);
auto *MBFI = &MFAM.getResult<MachineBlockFrequencyAnalysis>(MF);
SpillPlacement Impl;
Impl.run(MF, Bundles, MBFI);
return Impl;
}

bool SpillPlacementAnalysis::Result::invalidate(
MachineFunction &MF, const PreservedAnalyses &PA,
MachineFunctionAnalysisManager::Invalidator &Inv) {
auto PAC = PA.getChecker<SpillPlacementAnalysis>();
if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<MachineFunction>>())
return true;
// Check dependencies.
return Inv.invalidate<EdgeBundlesAnalysis>(MF, PA) ||
Inv.invalidate<MachineBlockFrequencyAnalysis>(MF, PA);
}

SpillPlacement::SpillPlacement() = default;
SpillPlacement::~SpillPlacement() = default;
SpillPlacement::SpillPlacement(SpillPlacement &&) = default;

void SpillPlacement::releaseMemory() {
nodes.reset();
TodoList.clear();
}

void SpillPlacement::run(MachineFunction &mf, EdgeBundles *Bundles,
MachineBlockFrequencyInfo *MBFI) {
MF = &mf;
bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
this->bundles = Bundles;
this->MBFI = MBFI;

assert(!nodes && "Leaking node array");
nodes = new Node[bundles->getNumBundles()];
nodes.reset(new Node[bundles->getNumBundles()]);
TodoList.clear();
TodoList.setUniverse(bundles->getNumBundles());

// Compute total ingoing and outgoing block frequencies for all bundles.
BlockFrequencies.resize(mf.getNumBlockIDs());
MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
setThreshold(MBFI->getEntryFreq());
for (auto &I : mf) {
unsigned Num = I.getNumber();
BlockFrequencies[Num] = MBFI->getBlockFreq(&I);
}

// We never change the function.
return false;
}

void SpillPlacement::releaseMemory() {
delete[] nodes;
nodes = nullptr;
TodoList.clear();
}

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

bool SpillPlacement::update(unsigned n) {
if (!nodes[n].update(nodes, Threshold))
if (!nodes[n].update(nodes.get(), Threshold))
return false;
nodes[n].getDissentingNeighbors(TodoList, nodes);
nodes[n].getDissentingNeighbors(TodoList, nodes.get());
return true;
}

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
#include "llvm/CodeGen/ShadowStackGCLowering.h"
#include "llvm/CodeGen/SjLjEHPrepare.h"
#include "llvm/CodeGen/SlotIndexes.h"
#include "llvm/CodeGen/SpillPlacement.h"
#include "llvm/CodeGen/StackColoring.h"
#include "llvm/CodeGen/StackProtector.h"
#include "llvm/CodeGen/TailDuplication.h"
Expand Down