Skip to content

Commit

Permalink
[LLVM][PM] Make FinalLowerGC NewPM compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Feb 18, 2022
1 parent 7600bfd commit ea3d788
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,10 @@ static void registerCallbacks(PassBuilder &PB) {
PM.addPass(LowerSIMDLoop());
return true;
}
if (Name == "FinalLowerGC") {
PM.addPass(FinalLowerGCPass());
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
8 changes: 8 additions & 0 deletions src/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using namespace llvm;
// Function Passes
struct DemoteFloat16 : PassInfoMixin<DemoteFloat16> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
static bool isRequired() { return true; }
};

struct CombineMulAdd : PassInfoMixin<CombineMulAdd> {
Expand All @@ -19,11 +20,13 @@ struct CombineMulAdd : PassInfoMixin<CombineMulAdd> {

struct LateLowerGC : PassInfoMixin<LateLowerGC> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
static bool isRequired() { return true; }
};

// Module Passes
struct CPUFeatures : PassInfoMixin<CPUFeatures> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
static bool isRequired() { return true; }
};

struct RemoveNI : PassInfoMixin<RemoveNI> {
Expand All @@ -34,6 +37,11 @@ struct LowerSIMDLoop : PassInfoMixin<LowerSIMDLoop> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};

struct FinalLowerGCPass : PassInfoMixin<LateLowerGC> {
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
static bool isRequired() { return true; }
};

// Loop Passes
struct JuliaLICMPass : PassInfoMixin<JuliaLICMPass> {
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
Expand Down
2 changes: 2 additions & 0 deletions test/llvmpasses/final-lower-gc.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
; RUN: opt -enable-new-pm=0 -load libjulia-codegen%shlibext -FinalLowerGC -S %s | FileCheck %s
; RUN: opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='FinalLowerGC' -S %s | FileCheck %s


@tag = external addrspace(10) global {}

Expand Down
1 change: 1 addition & 0 deletions test/llvmpasses/gcroots.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -enable-new-pm=0 -load libjulia-codegen%shlibext -LateLowerGCFrame -FinalLowerGC -S %s | FileCheck %s
; RUN: opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='function(LateLowerGCFrame),FinalLowerGC' -S %s | FileCheck %s


declare void @boxed_simple({} addrspace(10)*, {} addrspace(10)*)
Expand Down
1 change: 1 addition & 0 deletions test/llvmpasses/refinements.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -enable-new-pm=0 -load libjulia-codegen%shlibext -LateLowerGCFrame -FinalLowerGC -S %s | FileCheck %s
; RUN: opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='function(LateLowerGCFrame),FinalLowerGC' -S %s | FileCheck %s


declare {}*** @julia.ptls_states()
Expand Down
1 change: 1 addition & 0 deletions test/llvmpasses/returnstwicegc.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -enable-new-pm=0 -load libjulia-codegen%shlibext -LateLowerGCFrame -FinalLowerGC -S %s | FileCheck %s
; RUN: opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='function(LateLowerGCFrame),FinalLowerGC' -S %s | FileCheck %s


declare void @boxed_simple({} addrspace(10)*, {} addrspace(10)*)
Expand Down
2 changes: 2 additions & 0 deletions test/llvmpasses/safepoint_stress.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

# RUN: julia --startup-file=no %s | opt -enable-new-pm=0 -load libjulia-codegen%shlibext -LateLowerGCFrame -FinalLowerGC -S - | FileCheck %s
# RUN: julia --startup-file=no %s | opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='function(LateLowerGCFrame),FinalLowerGC' -S - | FileCheck %s


println("""
declare {} addrspace(10)* @alloc()
Expand Down

0 comments on commit ea3d788

Please sign in to comment.