Skip to content

Commit

Permalink
Merge pull request #42463 from JuliaLang/vc/newpm2
Browse files Browse the repository at this point in the history
[LLVM] Convert more passes to NewPM
  • Loading branch information
vchuravy authored Feb 18, 2022
2 parents 6c51d9e + ea3d788 commit 2fe57fd
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 69 deletions.
62 changes: 40 additions & 22 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,33 +879,51 @@ static void registerCallbacks(PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, FunctionPassManager &PM,
ArrayRef<PassBuilder::PipelineElement> InnerPipeline) {
if (Name == "DemoteFloat16") {
PM.addPass(DemoteFloat16());
return true;
}
if (Name == "CombineMulAdd") {
PM.addPass(CombineMulAdd());
return true;
}
return false;
if (Name == "DemoteFloat16") {
PM.addPass(DemoteFloat16());
return true;
}
if (Name == "CombineMulAdd") {
PM.addPass(CombineMulAdd());
return true;
}
if (Name == "LateLowerGCFrame") {
PM.addPass(LateLowerGC());
return true;
}
return false;
});

PB.registerPipelineParsingCallback(
[](StringRef Name, ModulePassManager &PM,
ArrayRef<PassBuilder::PipelineElement> InnerPipeline) {
if (Name == "CPUFeatures") {
PM.addPass(CPUFeatures());
return true;
}
if (Name == "RemoveNI") {
PM.addPass(RemoveNI());
return true;
}
if (Name == "LowerSIMDLoop") {
PM.addPass(LowerSIMDLoop());
return true;
}
return false;
if (Name == "CPUFeatures") {
PM.addPass(CPUFeatures());
return true;
}
if (Name == "RemoveNI") {
PM.addPass(RemoveNI());
return true;
}
if (Name == "LowerSIMDLoop") {
PM.addPass(LowerSIMDLoop());
return true;
}
if (Name == "FinalLowerGC") {
PM.addPass(FinalLowerGCPass());
return true;
}
return false;
});

PB.registerPipelineParsingCallback(
[](StringRef Name, LoopPassManager &PM,
ArrayRef<PassBuilder::PipelineElement> InnerPipeline) {
if (Name == "JuliaLICM") {
PM.addPass(JuliaLICMPass());
return true;
}
return false;
});
}

Expand Down
63 changes: 52 additions & 11 deletions src/llvm-final-gc-lowering.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file is a part of Julia. License is MIT: https://julialang.org/license

#include "llvm-version.h"
#include "passes.h"

#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Function.h>
Expand Down Expand Up @@ -28,21 +29,18 @@ using namespace llvm;
// This pass targets typical back-ends for which the standard Julia
// runtime library is available. Atypical back-ends should supply
// their own lowering pass.
struct FinalLowerGC: public FunctionPass, private JuliaPassContext {
static char ID;
FinalLowerGC() : FunctionPass(ID)
{ }

struct FinalLowerGC: private JuliaPassContext {
bool runOnFunction(Function &F);
bool doInitialization(Module &M);
bool doFinalization(Module &M);

private:
Function *queueRootFunc;
Function *poolAllocFunc;
Function *bigAllocFunc;
Instruction *pgcstack;

bool doInitialization(Module &M) override;
bool doFinalization(Module &M) override;
bool runOnFunction(Function &F) override;

// Lowers a `julia.new_gc_frame` intrinsic.
Value *lowerNewGCFrame(CallInst *target, Function &F);

Expand Down Expand Up @@ -325,12 +323,55 @@ bool FinalLowerGC::runOnFunction(Function &F)
return true;
}

char FinalLowerGC::ID = 0;
static RegisterPass<FinalLowerGC> X("FinalLowerGC", "Final GC intrinsic lowering pass", false, false);
struct FinalLowerGCLegacy: public FunctionPass {
static char ID;
FinalLowerGCLegacy() : FunctionPass(ID), finalLowerGC(FinalLowerGC()) {}

protected:
void getAnalysisUsage(AnalysisUsage &AU) const override {
FunctionPass::getAnalysisUsage(AU);
}

private:
bool runOnFunction(Function &F) override;
bool doInitialization(Module &M) override;
bool doFinalization(Module &M) override;

FinalLowerGC finalLowerGC;
};

bool FinalLowerGCLegacy::runOnFunction(Function &F) {
return finalLowerGC.runOnFunction(F);
}

bool FinalLowerGCLegacy::doInitialization(Module &M) {
return finalLowerGC.doInitialization(M);
}

bool FinalLowerGCLegacy::doFinalization(Module &M) {
return finalLowerGC.doFinalization(M);
}


PreservedAnalyses FinalLowerGCPass::run(Module &M, ModuleAnalysisManager &AM)
{
auto finalLowerGC = FinalLowerGC();
finalLowerGC.doInitialization(M);
for (auto &F : M.functions()) {
if (F.isDeclaration())
continue;
finalLowerGC.runOnFunction(F);
}
finalLowerGC.doFinalization(M);
return PreservedAnalyses::all();
}

char FinalLowerGCLegacy::ID = 0;
static RegisterPass<FinalLowerGCLegacy> X("FinalLowerGC", "Final GC intrinsic lowering pass", false, false);

Pass *createFinalLowerGCPass()
{
return new FinalLowerGC();
return new FinalLowerGCLegacy();
}

extern "C" JL_DLLEXPORT void LLVMExtraAddFinalLowerGCPass_impl(LLVMPassManagerRef PM)
Expand Down
63 changes: 50 additions & 13 deletions src/llvm-julia-licm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file is a part of Julia. License is MIT: https://julialang.org/license

#include "llvm-version.h"
#include "passes.h"

#include <llvm/Analysis/LoopInfo.h>
#include <llvm/Analysis/LoopPass.h>
Expand Down Expand Up @@ -28,11 +29,27 @@ using namespace llvm;

namespace {

struct JuliaLICMPass : public LoopPass, public JuliaPassContext {
struct JuliaLICMPassLegacy : public LoopPass {
static char ID;
JuliaLICMPass() : LoopPass(ID) {};
JuliaLICMPassLegacy() : LoopPass(ID) {};

bool runOnLoop(Loop *L, LPPassManager &LPM) override
bool runOnLoop(Loop *L, LPPassManager &LPM) override;

protected:
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<LoopInfoWrapperPass>();
AU.setPreservesAll();
}
};

struct JuliaLICM : public JuliaPassContext {
function_ref<DominatorTree &()> GetDT;
function_ref<LoopInfo &()> GetLI;
JuliaLICM(function_ref<DominatorTree &()> GetDT,
function_ref<LoopInfo &()> GetLI) : GetDT(GetDT), GetLI(GetLI) {}

bool runOnLoop(Loop *L)
{
// Get the preheader block to move instructions into,
// required to run this pass.
Expand All @@ -48,8 +65,8 @@ struct JuliaLICMPass : public LoopPass, public JuliaPassContext {
// We also hoist write barriers here, so we don't exit if write_barrier_func exists
if (!gc_preserve_begin_func && !write_barrier_func && !alloc_obj_func)
return false;
auto LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
auto LI = &GetLI();
auto DT = &GetDT();

// Lazy initialization of exit blocks insertion points.
bool exit_pts_init = false;
Expand Down Expand Up @@ -159,22 +176,42 @@ struct JuliaLICMPass : public LoopPass, public JuliaPassContext {
}
return changed;
}

void getAnalysisUsage(AnalysisUsage &AU) const override
{
getLoopAnalysisUsage(AU);
}
};

char JuliaLICMPass::ID = 0;
static RegisterPass<JuliaLICMPass>
bool JuliaLICMPassLegacy::runOnLoop(Loop *L, LPPassManager &LPM) {
auto GetDT = [this]() -> DominatorTree & {
return getAnalysis<DominatorTreeWrapperPass>().getDomTree();
};
auto GetLI = [this]() -> LoopInfo & {
return getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
};
auto juliaLICM = JuliaLICM(GetDT, GetLI);
return juliaLICM.runOnLoop(L);
}

char JuliaLICMPassLegacy::ID = 0;
static RegisterPass<JuliaLICMPassLegacy>
Y("JuliaLICM", "LICM for julia specific intrinsics.",
false, false);
} //namespace

PreservedAnalyses JuliaLICMPass::run(Loop &L, LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR, LPMUpdater &U)
{
auto GetDT = [&AR]() -> DominatorTree & {
return AR.DT;
};
auto GetLI = [&AR]() -> LoopInfo & {
return AR.LI;
};
auto juliaLICM = JuliaLICM(GetDT, GetLI);
juliaLICM.runOnLoop(&L);
return PreservedAnalyses::all();
}

Pass *createJuliaLICMPass()
{
return new JuliaLICMPass();
return new JuliaLICMPassLegacy();
}

extern "C" JL_DLLEXPORT void LLVMExtraJuliaLICMPass_impl(LLVMPassManagerRef PM)
Expand Down
63 changes: 40 additions & 23 deletions src/llvm-late-gc-lowering.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file is a part of Julia. License is MIT: https://julialang.org/license

#include "llvm-version.h"
#include "passes.h"

#include <llvm-c/Core.h>
#include <llvm-c/Types.h>
Expand Down Expand Up @@ -301,16 +302,11 @@ struct State {
State(Function &F) : F(&F), DT(nullptr), MaxPtrNumber(-1), MaxSafepointNumber(-1) {}
};

namespace llvm {
void initializeLateLowerGCFramePass(PassRegistry &Registry);
}

struct LateLowerGCFrame: public FunctionPass, private JuliaPassContext {

struct LateLowerGCFrameLegacy: public FunctionPass {
static char ID;
LateLowerGCFrame() : FunctionPass(ID)
{
llvm::initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
}
LateLowerGCFrameLegacy() : FunctionPass(ID) {}

protected:
void getAnalysisUsage(AnalysisUsage &AU) const override {
Expand All @@ -320,6 +316,17 @@ struct LateLowerGCFrame: public FunctionPass, private JuliaPassContext {
AU.setPreservesCFG();
}

private:
bool runOnFunction(Function &F) override;
};

struct LateLowerGCFrame: private JuliaPassContext {
function_ref<DominatorTree &()> GetDT;
LateLowerGCFrame(function_ref<DominatorTree &()> GetDT) : GetDT(GetDT) {}

public:
bool runOnFunction(Function &F);

private:
CallInst *pgcstack;

Expand Down Expand Up @@ -350,8 +357,6 @@ struct LateLowerGCFrame: public FunctionPass, private JuliaPassContext {
void PlaceGCFrameStore(State &S, unsigned R, unsigned MinColorRoot, const std::vector<int> &Colors, Value *GCFrame, Instruction *InsertBefore);
void PlaceGCFrameStores(State &S, unsigned MinColorRoot, const std::vector<int> &Colors, Value *GCFrame);
void PlaceRootsAndUpdateCalls(std::vector<int> &Colors, State &S, std::map<Value *, std::pair<int, int>>);
bool doInitialization(Module &M) override;
bool runOnFunction(Function &F) override;
bool CleanupIR(Function &F, State *S=nullptr);
void NoteUseChain(State &S, BBState &BBS, User *TheUser);
SmallVector<int, 1> GetPHIRefinements(PHINode *phi, State &S);
Expand Down Expand Up @@ -1385,7 +1390,7 @@ void LateLowerGCFrame::FixUpRefinements(ArrayRef<int> PHINumbers, State &S)
j++;
if (auto inst = dyn_cast<Instruction>(S.ReversePtrNumbering[refine])) {
if (!S.DT)
S.DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
S.DT = &GetDT();
if (S.DT->dominates(inst, Phi))
continue;
// Decrement `j` so we'll overwrite/ignore it.
Expand Down Expand Up @@ -1997,7 +2002,7 @@ void LateLowerGCFrame::ComputeLiveSets(State &S) {
// add in any extra live values.
if (!S.GCPreserves.empty()) {
if (!S.DT) {
S.DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
S.DT = &GetDT();
}
for (auto it2 : S.GCPreserves) {
if (!S.DT->dominates(it2.first, Safepoint))
Expand Down Expand Up @@ -2669,16 +2674,9 @@ void LateLowerGCFrame::PlaceRootsAndUpdateCalls(std::vector<int> &Colors, State
}
}

bool LateLowerGCFrame::doInitialization(Module &M) {
// Initialize platform-agnostic references.
initAll(M);
return true;
}

bool LateLowerGCFrame::runOnFunction(Function &F) {
initAll(*F.getParent());
LLVM_DEBUG(dbgs() << "GC ROOT PLACEMENT: Processing function " << F.getName() << "\n");
// Check availability of functions again since they might have been deleted.
initFunctions(*F.getParent());
if (!pgcstack_getter)
return CleanupIR(F);

Expand All @@ -2695,11 +2693,30 @@ bool LateLowerGCFrame::runOnFunction(Function &F) {
return true;
}

char LateLowerGCFrame::ID = 0;
static RegisterPass<LateLowerGCFrame> X("LateLowerGCFrame", "Late Lower GCFrame Pass", false, false);
bool LateLowerGCFrameLegacy::runOnFunction(Function &F) {
auto GetDT = [this]() -> DominatorTree & {
return getAnalysis<DominatorTreeWrapperPass>().getDomTree();
};
auto lateLowerGCFrame = LateLowerGCFrame(GetDT);
return lateLowerGCFrame.runOnFunction(F);
}

PreservedAnalyses LateLowerGC::run(Function &F, FunctionAnalysisManager &AM)
{
auto GetDT = [&AM, &F]() -> DominatorTree & {
return AM.getResult<DominatorTreeAnalysis>(F);
};
auto lateLowerGCFrame = LateLowerGCFrame(GetDT);
lateLowerGCFrame.runOnFunction(F);
return PreservedAnalyses::all();
}


char LateLowerGCFrameLegacy::ID = 0;
static RegisterPass<LateLowerGCFrameLegacy> X("LateLowerGCFrame", "Late Lower GCFrame Pass", false, false);

Pass *createLateLowerGCFramePass() {
return new LateLowerGCFrame();
return new LateLowerGCFrameLegacy();
}

extern "C" JL_DLLEXPORT void LLVMExtraAddLateLowerGCFramePass_impl(LLVMPassManagerRef PM)
Expand Down
Loading

0 comments on commit 2fe57fd

Please sign in to comment.