From ea3d788823e21bd8d25f22da79cedbbb86eb0b2e Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Thu, 17 Feb 2022 23:09:10 -0500 Subject: [PATCH] [LLVM][PM] Make FinalLowerGC NewPM compatible --- src/aotcompile.cpp | 4 ++ src/llvm-final-gc-lowering.cpp | 63 ++++++++++++++++++++++++----- src/passes.h | 8 ++++ test/llvmpasses/final-lower-gc.ll | 2 + test/llvmpasses/gcroots.ll | 1 + test/llvmpasses/refinements.ll | 1 + test/llvmpasses/returnstwicegc.ll | 1 + test/llvmpasses/safepoint_stress.jl | 2 + 8 files changed, 71 insertions(+), 11 deletions(-) diff --git a/src/aotcompile.cpp b/src/aotcompile.cpp index b9b3da10d432d..344e1ad8942d0 100644 --- a/src/aotcompile.cpp +++ b/src/aotcompile.cpp @@ -909,6 +909,10 @@ static void registerCallbacks(PassBuilder &PB) { PM.addPass(LowerSIMDLoop()); return true; } + if (Name == "FinalLowerGC") { + PM.addPass(FinalLowerGCPass()); + return true; + } return false; }); diff --git a/src/llvm-final-gc-lowering.cpp b/src/llvm-final-gc-lowering.cpp index 2f1ae2be32080..8a7d82b9381a7 100644 --- a/src/llvm-final-gc-lowering.cpp +++ b/src/llvm-final-gc-lowering.cpp @@ -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 #include @@ -28,10 +29,11 @@ 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; @@ -39,10 +41,6 @@ struct FinalLowerGC: public FunctionPass, private JuliaPassContext { 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); @@ -325,12 +323,55 @@ bool FinalLowerGC::runOnFunction(Function &F) return true; } -char FinalLowerGC::ID = 0; -static RegisterPass 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 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) diff --git a/src/passes.h b/src/passes.h index 311ac6de5a09a..9327c6e4065f2 100644 --- a/src/passes.h +++ b/src/passes.h @@ -11,6 +11,7 @@ using namespace llvm; // Function Passes struct DemoteFloat16 : PassInfoMixin { PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); + static bool isRequired() { return true; } }; struct CombineMulAdd : PassInfoMixin { @@ -19,11 +20,13 @@ struct CombineMulAdd : PassInfoMixin { struct LateLowerGC : PassInfoMixin { PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); + static bool isRequired() { return true; } }; // Module Passes struct CPUFeatures : PassInfoMixin { PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); + static bool isRequired() { return true; } }; struct RemoveNI : PassInfoMixin { @@ -34,6 +37,11 @@ struct LowerSIMDLoop : PassInfoMixin { PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); }; +struct FinalLowerGCPass : PassInfoMixin { + PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); + static bool isRequired() { return true; } +}; + // Loop Passes struct JuliaLICMPass : PassInfoMixin { PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, diff --git a/test/llvmpasses/final-lower-gc.ll b/test/llvmpasses/final-lower-gc.ll index 5e4a23770c4e0..176b695ba918b 100644 --- a/test/llvmpasses/final-lower-gc.ll +++ b/test/llvmpasses/final-lower-gc.ll @@ -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 {} diff --git a/test/llvmpasses/gcroots.ll b/test/llvmpasses/gcroots.ll index 29cb8683244d1..84f120712734b 100644 --- a/test/llvmpasses/gcroots.ll +++ b/test/llvmpasses/gcroots.ll @@ -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)*) diff --git a/test/llvmpasses/refinements.ll b/test/llvmpasses/refinements.ll index 0da965e538db8..cb2dea816c56b 100644 --- a/test/llvmpasses/refinements.ll +++ b/test/llvmpasses/refinements.ll @@ -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() diff --git a/test/llvmpasses/returnstwicegc.ll b/test/llvmpasses/returnstwicegc.ll index ebc6127813876..17791d630d61a 100644 --- a/test/llvmpasses/returnstwicegc.ll +++ b/test/llvmpasses/returnstwicegc.ll @@ -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)*) diff --git a/test/llvmpasses/safepoint_stress.jl b/test/llvmpasses/safepoint_stress.jl index 7ecf01a194851..dc6752e76d595 100644 --- a/test/llvmpasses/safepoint_stress.jl +++ b/test/llvmpasses/safepoint_stress.jl @@ -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()