Skip to content
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

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions llvm/include/llvm/ADT/SparseSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ class SparseSet {
using DenseT = SmallVector<ValueT, 8>;
using size_type = unsigned;
DenseT Dense;
SparseT *Sparse = nullptr;

struct Deleter {
void operator()(SparseT *S) { free(S); }
};
std::unique_ptr<SparseT[], Deleter> Sparse;

unsigned Universe = 0;
KeyFunctorT KeyIndexOf;
SparseSetValFunctor<KeyT, ValueT, KeyFunctorT> ValIndexOf;
Expand All @@ -144,7 +149,7 @@ class SparseSet {
SparseSet() = default;
SparseSet(const SparseSet &) = delete;
SparseSet &operator=(const SparseSet &) = delete;
~SparseSet() { free(Sparse); }
SparseSet(SparseSet &&) = default;

/// setUniverse - Set the universe size which determines the largest key the
/// set can hold. The universe must be sized before any elements can be
Expand All @@ -159,11 +164,10 @@ class SparseSet {
// Hysteresis prevents needless reallocations.
if (U >= Universe/4 && U <= Universe)
return;
free(Sparse);
// The Sparse array doesn't actually need to be initialized, so malloc
// would be enough here, but that will cause tools like valgrind to
// complain about branching on uninitialized data.
Sparse = static_cast<SparseT*>(safe_calloc(U, sizeof(SparseT)));
Sparse.reset(static_cast<SparseT *>(safe_calloc(U, sizeof(SparseT))));
Universe = U;
}

Expand Down
39 changes: 34 additions & 5 deletions llvm/include/llvm/CodeGen/EdgeBundles.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/IntEqClasses.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/IR/PassManager.h"

namespace llvm {
class EdgeBundlesWrapperLegacy;
class EdgeBundlesAnalysis;

class EdgeBundles {
friend class EdgeBundlesWrapperLegacy;
friend class EdgeBundlesAnalysis;

class EdgeBundles : public MachineFunctionPass {
const MachineFunction *MF = nullptr;

/// EC - Each edge bundle is an equivalence class. The keys are:
Expand All @@ -32,10 +38,10 @@ class EdgeBundles : public MachineFunctionPass {
/// Blocks - Map each bundle to a list of basic block numbers.
SmallVector<SmallVector<unsigned, 8>, 4> Blocks;

public:
static char ID;
EdgeBundles() : MachineFunctionPass(ID) {}
void init();
EdgeBundles(MachineFunction &MF);

public:
/// getBundle - Return the ingoing (Out = false) or outgoing (Out = true)
/// bundle number for basic block #N
unsigned getBundle(unsigned N, bool Out) const { return EC[2 * N + Out]; }
Expand All @@ -52,11 +58,34 @@ class EdgeBundles : public MachineFunctionPass {
/// view - Visualize the annotated bipartite CFG with Graphviz.
void view() const;

// Handle invalidation for the new pass manager
bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
MachineFunctionAnalysisManager::Invalidator &Inv);
};

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

EdgeBundles &getEdgeBundles() { return *Impl; }
const EdgeBundles &getEdgeBundles() const { return *Impl; }

private:
bool runOnMachineFunction(MachineFunction&) override;
std::unique_ptr<EdgeBundles> Impl;
bool runOnMachineFunction(MachineFunction &MF) override;
void getAnalysisUsage(AnalysisUsage&) const override;
};

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

public:
using Result = EdgeBundles;
EdgeBundles run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
};

} // end namespace llvm

#endif
2 changes: 1 addition & 1 deletion llvm/include/llvm/CodeGen/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace llvm {
extern char &MachineRegionInfoPassID;

/// EdgeBundles analysis - Bundle machine CFG edges.
extern char &EdgeBundlesID;
extern char &EdgeBundlesWrapperLegacyID;

/// LiveVariables pass - This pass computes the set of blocks in which each
/// variable is life and sets machine operand kill flags.
Expand Down
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,51 @@ class SpillPlacement : public MachineFunctionPass {
return BlockFrequencies[Number];
}

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

// Out of line definitions so unique_ptr<Node[]> doesn't need to be complete.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't really need to comment this

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
4 changes: 2 additions & 2 deletions llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void initializeEarlyIfConverterLegacyPass(PassRegistry &);
void initializeEarlyIfPredicatorPass(PassRegistry &);
void initializeEarlyMachineLICMPass(PassRegistry &);
void initializeEarlyTailDuplicateLegacyPass(PassRegistry &);
void initializeEdgeBundlesPass(PassRegistry &);
void initializeEdgeBundlesWrapperLegacyPass(PassRegistry &);
void initializeEHContGuardCatchretPass(PassRegistry &);
void initializeExpandLargeFpConvertLegacyPassPass(PassRegistry &);
void initializeExpandLargeDivRemLegacyPassPass(PassRegistry &);
Expand Down 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
3 changes: 2 additions & 1 deletion llvm/include/llvm/Passes/MachinePassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ LOOP_PASS("loop-term-fold", LoopTermFoldPass())
// LiveVariables can be removed completely, and LiveIntervals can be directly
// computed. (We still either need to regenerate kill flags after regalloc, or
// preferably fix the scavenger to not depend on them).
MACHINE_FUNCTION_ANALYSIS("edge-bundles", EdgeBundlesAnalysis())
MACHINE_FUNCTION_ANALYSIS("live-intervals", LiveIntervalsAnalysis())
MACHINE_FUNCTION_ANALYSIS("live-reg-matrix", LiveRegMatrixAnalysis())
MACHINE_FUNCTION_ANALYSIS("live-vars", LiveVariablesAnalysis())
Expand All @@ -112,9 +113,9 @@ 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("edge-bundles", EdgeBundlesAnalysis())
// MACHINE_FUNCTION_ANALYSIS("lazy-machine-bfi",
// LazyMachineBlockFrequencyInfoAnalysis())
// MACHINE_FUNCTION_ANALYSIS("machine-loops", MachineLoopInfoAnalysis())
Expand Down
39 changes: 30 additions & 9 deletions llvm/lib/CodeGen/EdgeBundles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,35 @@ static cl::opt<bool>
ViewEdgeBundles("view-edge-bundles", cl::Hidden,
cl::desc("Pop up a window to show edge bundle graphs"));

char EdgeBundles::ID = 0;
char EdgeBundlesWrapperLegacy::ID = 0;

INITIALIZE_PASS(EdgeBundles, "edge-bundles", "Bundle Machine CFG Edges",
/* cfg = */true, /* is_analysis = */ true)
INITIALIZE_PASS(EdgeBundlesWrapperLegacy, "edge-bundles",
"Bundle Machine CFG Edges",
/* cfg = */ true, /* is_analysis = */ true)

char &llvm::EdgeBundlesID = EdgeBundles::ID;
char &llvm::EdgeBundlesWrapperLegacyID = EdgeBundlesWrapperLegacy::ID;

void EdgeBundles::getAnalysisUsage(AnalysisUsage &AU) const {
void EdgeBundlesWrapperLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
MachineFunctionPass::getAnalysisUsage(AU);
}

bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
MF = &mf;
AnalysisKey EdgeBundlesAnalysis::Key;

EdgeBundles EdgeBundlesAnalysis::run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
EdgeBundles Impl(MF);
return Impl;
}

bool EdgeBundlesWrapperLegacy::runOnMachineFunction(MachineFunction &MF) {
Impl.reset(new EdgeBundles(MF));
return false;
}

EdgeBundles::EdgeBundles(MachineFunction &MF) : MF(&MF) { init(); }

void EdgeBundles::init() {
EC.clear();
EC.grow(2 * MF->getNumBlockIDs());

Expand All @@ -64,8 +79,6 @@ bool EdgeBundles::runOnMachineFunction(MachineFunction &mf) {
if (b1 != b0)
Blocks[b1].push_back(i);
}

return false;
}

namespace llvm {
Expand Down Expand Up @@ -100,3 +113,11 @@ raw_ostream &WriteGraph<>(raw_ostream &O, const EdgeBundles &G,
void EdgeBundles::view() const {
ViewGraph(*this, "EdgeBundles");
}

bool EdgeBundles::invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
MachineFunctionAnalysisManager::Invalidator &Inv) {
// Invalidated when CFG is not preserved
auto PAC = PA.getChecker<EdgeBundlesAnalysis>();
return !PAC.preserved() && !PAC.preservedSet<CFGAnalyses>() &&
!PAC.preservedSet<AllAnalysesOn<MachineFunction>>();
}
14 changes: 7 additions & 7 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 @@ -161,8 +161,8 @@ INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy)
INITIALIZE_PASS_DEPENDENCY(EdgeBundles)
INITIALIZE_PASS_DEPENDENCY(SpillPlacement)
INITIALIZE_PASS_DEPENDENCY(EdgeBundlesWrapperLegacy)
INITIALIZE_PASS_DEPENDENCY(SpillPlacementWrapperLegacy)
INITIALIZE_PASS_DEPENDENCY(MachineOptimizationRemarkEmitterPass)
INITIALIZE_PASS_DEPENDENCY(RegAllocEvictionAdvisorAnalysis)
INITIALIZE_PASS_DEPENDENCY(RegAllocPriorityAdvisorAnalysis)
Expand Down Expand Up @@ -216,8 +216,8 @@ void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addPreserved<VirtRegMapWrapperLegacy>();
AU.addRequired<LiveRegMatrixWrapperLegacy>();
AU.addPreserved<LiveRegMatrixWrapperLegacy>();
AU.addRequired<EdgeBundles>();
AU.addRequired<SpillPlacement>();
AU.addRequired<EdgeBundlesWrapperLegacy>();
AU.addRequired<SpillPlacementWrapperLegacy>();
AU.addRequired<MachineOptimizationRemarkEmitterPass>();
AU.addRequired<RegAllocEvictionAdvisorAnalysis>();
AU.addRequired<RegAllocPriorityAdvisorAnalysis>();
Expand Down Expand Up @@ -2730,8 +2730,8 @@ bool RAGreedy::runOnMachineFunction(MachineFunction &mf) {
DomTree = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
ORE = &getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
Bundles = &getAnalysis<EdgeBundles>();
SpillPlacer = &getAnalysis<SpillPlacement>();
Bundles = &getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
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
Loading
Loading