From 3887e01cc430944b70f180e71b7422c5ec9d66c3 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Wed, 16 Feb 2022 16:21:48 -0500 Subject: [PATCH] [NewPM] add pass plugin --- src/Makefile | 2 +- src/aotcompile.cpp | 52 +++++++++++++++++++++++++++++---- src/jitlayers.h | 6 ++++ src/julia.expmap | 2 +- src/llvm-cpufeatures.cpp | 5 +--- src/llvm-demote-float16.cpp | 5 +--- src/llvm-muladd.cpp | 5 +--- src/llvm-remove-ni.cpp | 5 +--- src/llvm-simdloop.cpp | 8 ++--- src/passes.h | 32 ++++++++++++++++++++ test/llvmpasses/cpu-features.ll | 1 + test/llvmpasses/loopinfo.jl | 1 + test/llvmpasses/muladd.ll | 2 ++ test/llvmpasses/simdloop.ll | 1 + 14 files changed, 98 insertions(+), 29 deletions(-) create mode 100644 src/passes.h diff --git a/src/Makefile b/src/Makefile index b7235597fd08c..b673dda90116c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -108,7 +108,7 @@ PUBLIC_HEADERS := $(BUILDDIR)/julia_version.h $(wildcard $(SRCDIR)/support/*.h) ifeq ($(OS),WINNT) PUBLIC_HEADERS += $(addprefix $(SRCDIR)/,win32_ucontext.h) endif -HEADERS := $(PUBLIC_HEADERS) $(addprefix $(SRCDIR)/,julia_internal.h options.h timing.h) $(addprefix $(BUILDDIR)/,$(DTRACE_HEADERS) jl_internal_funcs.inc) +HEADERS := $(PUBLIC_HEADERS) $(addprefix $(SRCDIR)/,julia_internal.h options.h timing.h passes.h) $(addprefix $(BUILDDIR)/,$(DTRACE_HEADERS) jl_internal_funcs.inc) PUBLIC_HEADERS += $(addprefix $(SRCDIR)/,julia_gcext.h) PUBLIC_HEADER_TARGETS := $(addprefix $(build_includedir)/julia/,$(notdir $(PUBLIC_HEADERS)) $(UV_HEADERS)) diff --git a/src/aotcompile.cpp b/src/aotcompile.cpp index 1c5ccbebcb0a7..8eb718836da6d 100644 --- a/src/aotcompile.cpp +++ b/src/aotcompile.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,8 @@ #include #include #include +#include +#include #if defined(USE_POLLY) #include #include @@ -52,11 +55,6 @@ using namespace llvm; -// our passes -namespace llvm { - extern Pass *createLowerSimdLoopPass(); -} - #include "julia.h" #include "julia_internal.h" #include "jitlayers.h" @@ -872,6 +870,50 @@ void jl_add_optimization_passes_impl(LLVMPassManagerRef PM, int opt_level, int l addOptimizationPasses(unwrap(PM), opt_level, lower_intrinsics); } +// new pass manager plugin + +// NOTE: Instead of exporting all the constructors in passes.h we could +// forward the callbacks to the respective passes. LLVM seems to prefer this, +// and when we add the full pass builder having them directly will be helpful. +static void registerCallbacks(PassBuilder &PB) { + PB.registerPipelineParsingCallback( + [](StringRef Name, FunctionPassManager &PM, + ArrayRef InnerPipeline) { + if (Name == "DemoteFloat16") { + PM.addPass(DemoteFloat16()); + return true; + } + if (Name == "CombineMulAdd") { + PM.addPass(CombineMulAdd()); + return true; + } + return false; + }); + + PB.registerPipelineParsingCallback( + [](StringRef Name, ModulePassManager &PM, + ArrayRef 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; + }); +} + +extern "C" JL_DLLEXPORT ::llvm::PassPluginLibraryInfo +llvmGetPassPluginInfo() { + return {LLVM_PLUGIN_API_VERSION, "Julia", "1", registerCallbacks}; +} + // --- native code info, and dump function to IR and ASM --- // Get pointer to llvm::Function instance, compiling if necessary // for use in reflection from Julia. diff --git a/src/jitlayers.h b/src/jitlayers.h index ba3f81fa66997..fbc42944b7235 100644 --- a/src/jitlayers.h +++ b/src/jitlayers.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "llvm/IR/LegacyPassManager.h" #include @@ -260,6 +261,11 @@ Pass *createMultiVersioningPass(); Pass *createAllocOptPass(); Pass *createDemoteFloat16Pass(); Pass *createCPUFeaturesPass(); +Pass *createLowerSimdLoopPass(); + +// NewPM +#include "passes.h" + // Whether the Function is an llvm or julia intrinsic. static inline bool isIntrinsicFunction(Function *F) { diff --git a/src/julia.expmap b/src/julia.expmap index 558dfec6bd260..27c1b604244df 100644 --- a/src/julia.expmap +++ b/src/julia.expmap @@ -29,11 +29,11 @@ jlbacktrace; jlbacktracet; _IO_stdin_used; - __ZN4llvm23createLowerSimdLoopPassEv; _Z24jl_coverage_data_pointerN4llvm9StringRefEi; _Z22jl_coverage_alloc_lineN4llvm9StringRefEi; _Z22jl_malloc_data_pointerN4llvm9StringRefEi; LLVMExtra*; + llvmGetPassPluginInfo; /* freebsd */ environ; diff --git a/src/llvm-cpufeatures.cpp b/src/llvm-cpufeatures.cpp index 8accd399371ae..c87932f9e3ef7 100644 --- a/src/llvm-cpufeatures.cpp +++ b/src/llvm-cpufeatures.cpp @@ -14,6 +14,7 @@ // instead of using the global target machine? #include "llvm-version.h" +#include "passes.h" #include #include @@ -108,10 +109,6 @@ bool lowerCPUFeatures(Module &M) } } -struct CPUFeatures : PassInfoMixin { - PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); -}; - PreservedAnalyses CPUFeatures::run(Module &M, ModuleAnalysisManager &AM) { lowerCPUFeatures(M); diff --git a/src/llvm-demote-float16.cpp b/src/llvm-demote-float16.cpp index 3e328424e26d2..25c93252558bb 100644 --- a/src/llvm-demote-float16.cpp +++ b/src/llvm-demote-float16.cpp @@ -17,6 +17,7 @@ #define DEBUG_TYPE "demote_float16" #include "support/dtypes.h" +#include "passes.h" #include #include @@ -127,10 +128,6 @@ static bool demoteFloat16(Function &F) } // end anonymous namespace -struct DemoteFloat16 : PassInfoMixin { - PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); -}; - PreservedAnalyses DemoteFloat16::run(Function &F, FunctionAnalysisManager &AM) { demoteFloat16(F); diff --git a/src/llvm-muladd.cpp b/src/llvm-muladd.cpp index 7166698db356f..f9de29c3ef39c 100644 --- a/src/llvm-muladd.cpp +++ b/src/llvm-muladd.cpp @@ -3,6 +3,7 @@ #define DEBUG_TYPE "combine_muladd" #undef DEBUG #include "llvm-version.h" +#include "passes.h" #include #include @@ -82,10 +83,6 @@ static bool combineMulAdd(Function &F) return true; } -struct CombineMulAdd : PassInfoMixin { - PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); -}; - PreservedAnalyses CombineMulAdd::run(Function &F, FunctionAnalysisManager &AM) { combineMulAdd(F); diff --git a/src/llvm-remove-ni.cpp b/src/llvm-remove-ni.cpp index 40b0ecd735b13..50a6041c017e0 100644 --- a/src/llvm-remove-ni.cpp +++ b/src/llvm-remove-ni.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 @@ -34,10 +35,6 @@ static bool removeNI(Module &M) } } -struct RemoveNI : PassInfoMixin { - PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); -}; - PreservedAnalyses RemoveNI::run(Module &M, ModuleAnalysisManager &AM) { removeNI(M); diff --git a/src/llvm-simdloop.cpp b/src/llvm-simdloop.cpp index 8d80a535b2319..5d7b86f06701d 100644 --- a/src/llvm-simdloop.cpp +++ b/src/llvm-simdloop.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" #define DEBUG_TYPE "lower_simd_loop" @@ -28,7 +29,7 @@ #include "julia_assert.h" -namespace llvm { +using namespace llvm; namespace { @@ -213,9 +214,6 @@ static bool markLoopInfo(Module &M, Function *marker, function_ref { - PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); -}; PreservedAnalyses LowerSIMDLoop::run(Module &M, ModuleAnalysisManager &AM) @@ -288,5 +286,3 @@ extern "C" JL_DLLEXPORT void LLVMExtraAddLowerSimdLoopPass_impl(LLVMPassManagerR { unwrap(PM)->add(createLowerSimdLoopPass()); } - -} // namespace llvm diff --git a/src/passes.h b/src/passes.h new file mode 100644 index 0000000000000..6a18eedf1ea7e --- /dev/null +++ b/src/passes.h @@ -0,0 +1,32 @@ +// This file is a part of Julia. License is MIT: https://julialang.org/license + +#ifndef JL_PASSES_H +#define JL_PASSES_H + +#include + +using namespace llvm; + +// Function Passes +struct DemoteFloat16 : PassInfoMixin { + PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); +}; + +struct CombineMulAdd : PassInfoMixin { + PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); +}; + +// Module Passes +struct CPUFeatures : PassInfoMixin { + PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); +}; + +struct RemoveNI : PassInfoMixin { + PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); +}; + +struct LowerSIMDLoop : PassInfoMixin { + PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); +}; + +#endif diff --git a/test/llvmpasses/cpu-features.ll b/test/llvmpasses/cpu-features.ll index 42a615a838e6b..ccb8cc69f0f66 100644 --- a/test/llvmpasses/cpu-features.ll +++ b/test/llvmpasses/cpu-features.ll @@ -1,4 +1,5 @@ ; RUN: opt -enable-new-pm=0 -load libjulia-codegen%shlibext -CPUFeatures -simplifycfg -S %s | FileCheck %s +; RUN: opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='CPUFeatures,simplifycfg' -S %s | FileCheck %s declare i1 @julia.cpu.have_fma.f64() declare double @with_fma(double %0, double %1, double %2) diff --git a/test/llvmpasses/loopinfo.jl b/test/llvmpasses/loopinfo.jl index 508127066ee33..412bee7015c3e 100644 --- a/test/llvmpasses/loopinfo.jl +++ b/test/llvmpasses/loopinfo.jl @@ -3,6 +3,7 @@ # RUN: julia --startup-file=no %s %t && llvm-link -S %t/* -o %t/module.ll # RUN: cat %t/module.ll | FileCheck %s # RUN: cat %t/module.ll | opt -enable-new-pm=0 -load libjulia-codegen%shlibext -LowerSIMDLoop -S - | FileCheck %s -check-prefix=LOWER +# RUN: cat %t/module.ll | opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='LowerSIMDLoop' -S - | FileCheck %s -check-prefix=LOWER # RUN: julia --startup-file=no %s %t -O && llvm-link -S %t/* -o %t/module.ll # RUN: cat %t/module.ll | FileCheck %s -check-prefix=FINAL diff --git a/test/llvmpasses/muladd.ll b/test/llvmpasses/muladd.ll index 42a6bcff96b96..2eddb62cef3ec 100644 --- a/test/llvmpasses/muladd.ll +++ b/test/llvmpasses/muladd.ll @@ -1,4 +1,6 @@ ; RUN: opt -enable-new-pm=0 -load libjulia-codegen%shlibext -CombineMulAdd -S %s | FileCheck %s +; RUN: opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='CombineMulAdd' -S %s | FileCheck %s + define double @fast_muladd1(double %a, double %b, double %c) { top: diff --git a/test/llvmpasses/simdloop.ll b/test/llvmpasses/simdloop.ll index ac3c92092b3ce..894d3a1428a5c 100644 --- a/test/llvmpasses/simdloop.ll +++ b/test/llvmpasses/simdloop.ll @@ -1,4 +1,5 @@ ; RUN: opt -enable-new-pm=0 -load libjulia-codegen%shlibext -LowerSIMDLoop -S %s | FileCheck %s +; RUN: opt -enable-new-pm=1 --load-pass-plugin=libjulia-codegen%shlibext -passes='LowerSIMDLoop' -S %s | FileCheck %s declare void @julia.loopinfo_marker()