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

Transition pipeline to NewPM #44365

Closed
wants to merge 7 commits into from
Closed
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
5 changes: 3 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ CODEGEN_SRCS := codegen jitlayers aotcompile debuginfo disasm llvm-simdloop llvm
llvm-final-gc-lowering llvm-pass-helpers llvm-late-gc-lowering llvm-ptls \
llvm-lower-handlers llvm-gc-invariant-verifier llvm-propagate-addrspaces \
llvm-multiversioning llvm-alloc-opt llvm-alloc-helpers cgmemmgr llvm-remove-addrspaces \
llvm-remove-ni llvm-julia-licm llvm-demote-float16 llvm-cpufeatures
llvm-remove-ni llvm-julia-licm llvm-demote-float16 llvm-cpufeatures pipeline
FLAGS += -I$(shell $(LLVM_CONFIG_HOST) --includedir)
CG_LLVM_LIBS := all
ifeq ($(USE_POLLY),1)
Expand Down Expand Up @@ -289,7 +289,7 @@ $(BUILDDIR)/builtins.o $(BUILDDIR)/builtins.dbg.obj: $(SRCDIR)/iddict.c $(SRCDIR
$(BUILDDIR)/codegen.o $(BUILDDIR)/codegen.dbg.obj: $(addprefix $(SRCDIR)/,\
intrinsics.cpp jitlayers.h debug-registry.h intrinsics.h codegen_shared.h cgutils.cpp ccall.cpp abi_*.cpp processor.h builtin_proto.h)
$(BUILDDIR)/debuginfo.o $(BUILDDIR)/debuginfo.dbg.obj: $(addprefix $(SRCDIR)/,debuginfo.h processor.h jitlayers.h debug-registry.h)
$(BUILDDIR)/disasm.o $(BUILDDIR)/disasm.dbg.obj: $(SRCDIR)/debuginfo.h $(SRCDIR)/processor.h
$(BUILDDIR)/disasm.o $(BUILDDIR)/disasm.dbg.obj: $(SRCDIR)/debuginfo.h $(SRCDIR)/processor.h $(SRCDIR)/jitlayers.h
$(BUILDDIR)/dump.o $(BUILDDIR)/dump.dbg.obj: $(addprefix $(SRCDIR)/,common_symbols1.inc common_symbols2.inc builtin_proto.h serialize.h)
$(BUILDDIR)/gc-debug.o $(BUILDDIR)/gc-debug.dbg.obj: $(SRCDIR)/gc.h
$(BUILDDIR)/gc-pages.o $(BUILDDIR)/gc-pages.dbg.obj: $(SRCDIR)/gc.h
Expand All @@ -312,6 +312,7 @@ $(BUILDDIR)/llvm-pass-helpers.o $(BUILDDIR)/llvm-pass-helpers.dbg.obj: $(SRCDIR)
$(BUILDDIR)/llvm-propagate-addrspaces.o $(BUILDDIR)/llvm-propagate-addrspaces.dbg.obj: $(SRCDIR)/codegen_shared.h
$(BUILDDIR)/llvm-remove-addrspaces.o $(BUILDDIR)/llvm-remove-addrspaces.dbg.obj: $(SRCDIR)/codegen_shared.h
$(BUILDDIR)/llvm-ptls.o $(BUILDDIR)/llvm-ptls.dbg.obj: $(SRCDIR)/codegen_shared.h
$(BUILDDIR)/pipeline.o $(BUILDDIR)/pipeline.dbg.obj: $(SRCDIR)/jitlayers.h
$(BUILDDIR)/processor.o $(BUILDDIR)/processor.dbg.obj: $(addprefix $(SRCDIR)/,processor_*.cpp processor.h features_*.h)
$(BUILDDIR)/signal-handling.o $(BUILDDIR)/signal-handling.dbg.obj: $(addprefix $(SRCDIR)/,signals-*.c)
$(BUILDDIR)/staticdata.o $(BUILDDIR)/staticdata.dbg.obj: $(SRCDIR)/processor.h $(SRCDIR)/builtin_proto.h
Expand Down
177 changes: 104 additions & 73 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,44 @@
#endif
#endif

// NewPM needs to manually include all the pass headers
#include "llvm/Transforms/IPO/AlwaysInliner.h"
#include <llvm/Transforms/IPO/ConstantMerge.h>
#include "llvm/Transforms/InstCombine/InstCombine.h"
#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
#include "llvm/Transforms/Scalar/ADCE.h"
#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
#include "llvm/Transforms/Scalar/DCE.h"
#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
#include "llvm/Transforms/Scalar/DivRemPairs.h"
#include "llvm/Transforms/Scalar/EarlyCSE.h"
#include "llvm/Transforms/Scalar/GVN.h"
#include "llvm/Transforms/Scalar/IndVarSimplify.h"
#include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
#include "llvm/Transforms/Scalar/JumpThreading.h"
#include "llvm/Transforms/Scalar/LICM.h"
#include "llvm/Transforms/Scalar/LoopDeletion.h"
#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
#include "llvm/Transforms/Scalar/LoopRotation.h"
#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
#include "llvm/Transforms/Scalar/Reassociate.h"
#include "llvm/Transforms/Scalar/SCCP.h"
#include "llvm/Transforms/Scalar/SROA.h"
#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
#include "llvm/Transforms/Scalar/SimplifyCFG.h"
#include "llvm/Transforms/Vectorize/LoopVectorize.h"
#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
#include "llvm/Transforms/Vectorize/VectorCombine.h"



// for outputting code
#include <llvm/Bitcode/BitcodeWriter.h>
#include <llvm/Bitcode/BitcodeWriterPass.h>
Expand Down Expand Up @@ -515,29 +553,39 @@ void jl_dump_native_impl(void *native_code,
std::vector<NewArchiveMember> unopt_bc_Archive;
std::vector<std::string> outputs;

#ifndef JL_USE_NEW_PM
legacy::PassManager preopt, postopt;
#else
PassBuilder PB;
AnalysisManagers AM{*TM, PB, getOptLevel(jl_options.opt_level)};
ModulePassManager preopt, postopt;
#endif
legacy::PassManager emitter;

if (unopt_bc_fname)
if (unopt_bc_fname) {
#ifndef JL_USE_NEW_PM
preopt.add(createBitcodeWriterPass(unopt_bc_OS));
#else
preopt.addPass(BitcodeWriterPass(unopt_bc_OS));
#endif
}

//Is this necessary for TM?
// addTargetPasses(&postopt, TM->getTargetTriple(), TM->getTargetIRAnalysis());
if (bc_fname)
if (bc_fname) {
#ifndef JL_USE_NEW_PM
postopt.add(createBitcodeWriterPass(bc_OS));
#else
postopt.addPass(BitcodeWriterPass(bc_OS));
#endif
}
//Is this necessary for TM?
addTargetPasses(&emitter, TM->getTargetTriple(), TM->getTargetIRAnalysis());
if (obj_fname)
if (TM->addPassesToEmitFile(postopt, obj_OS, nullptr, CGFT_ObjectFile, false))
if (TM->addPassesToEmitFile(emitter, obj_OS, nullptr, CGFT_ObjectFile, false))
jl_safe_printf("ERROR: target does not support generation of object files\n");
if (asm_fname)
if (TM->addPassesToEmitFile(postopt, asm_OS, nullptr, CGFT_AssemblyFile, false))
if (TM->addPassesToEmitFile(emitter, asm_OS, nullptr, CGFT_AssemblyFile, false))
jl_safe_printf("ERROR: target does not support generation of object files\n");

legacy::PassManager optimizer;
if (bc_fname || obj_fname || asm_fname) {
addTargetPasses(&optimizer, TM->getTargetTriple(), TM->getTargetIRAnalysis());
addOptimizationPasses(&optimizer, jl_options.opt_level, true, true);
addMachinePasses(&optimizer, jl_options.opt_level);
}

// Reset the target triple to make sure it matches the new target machine
auto dataM = data->M.getModuleUnlocked();
dataM->setTargetTriple(TM->getTargetTriple().str());
Expand All @@ -549,6 +597,17 @@ void jl_dump_native_impl(void *native_code,
T_size = Type::getInt32Ty(Context);
Type *T_psize = T_size->getPointerTo();

#ifndef JL_USE_NEW_PM
legacy::PassManager optimizer;
if (bc_fname || obj_fname || asm_fname) {
addTargetPasses(&optimizer, TM->getTargetTriple(), TM->getTargetIRAnalysis());
addOptimizationPasses(&optimizer, jl_options.opt_level, true, true);
addMachinePasses(&optimizer, jl_options.opt_level);
}
#else
NewPM optimizer{std::move(TM), getOptLevel(jl_options.opt_level), {true, true}};
#endif

// add metadata information
if (imaging_default()) {
emit_offset_table(*dataM, data->jl_sysimg_gvars, "jl_sysimg_gvars", T_psize);
Expand All @@ -567,7 +626,11 @@ void jl_dump_native_impl(void *native_code,

// do the actual work
auto add_output = [&] (Module &M, StringRef unopt_bc_Name, StringRef bc_Name, StringRef obj_Name, StringRef asm_Name) {
preopt.run(M);
preopt.run(M
#ifdef JL_USE_NEW_PM
, AM.MAM
#endif
);
optimizer.run(M);

// We would like to emit an alias or an weakref alias to redirect these symbols
Expand All @@ -585,7 +648,12 @@ void jl_dump_native_impl(void *native_code,
injectCRTAlias(M, "__truncdfhf2", "julia__truncdfhf2",
FunctionType::get(Type::getHalfTy(Context), { Type::getDoubleTy(Context) }, false));

postopt.run(M);
postopt.run(M
#ifdef JL_USE_NEW_PM
, AM.MAM
#endif
);
emitter.run(M);

if (unopt_bc_fname)
emit_result(unopt_bc_Archive, unopt_bc_Buffer, unopt_bc_Name, outputs);
Expand Down Expand Up @@ -937,6 +1005,9 @@ void jl_add_optimization_passes_impl(LLVMPassManagerRef PM, int opt_level, int l
addOptimizationPasses(unwrap(PM), opt_level, lower_intrinsics);
}

void buildBasicPipeline(ModulePassManager &MPM, PassBuilder &PB, OptimizationLevel O, OptimizationOptions options);
void buildFullPipeline(ModulePassManager &MPM, PassBuilder &PB, OptimizationLevel O, OptimizationOptions options);

// new pass manager plugin

// NOTE: Instead of exporting all the constructors in passes.h we could
Expand All @@ -946,79 +1017,39 @@ 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;
}
if (Name == "LateLowerGCFrame") {
PM.addPass(LateLowerGC());
return true;
}
if (Name == "AllocOpt") {
PM.addPass(AllocOptPass());
return true;
}
if (Name == "PropagateJuliaAddrspaces") {
PM.addPass(PropagateJuliaAddrspacesPass());
return true;
}
if (Name == "LowerExcHandlers") {
PM.addPass(LowerExcHandlers());
return true;
}
if (Name == "GCInvariantVerifier") {
// TODO: Parse option and allow users to set `Strong`
PM.addPass(GCInvariantVerifierPass());
return true;
#define FUNCTION_PASS(NAME, CREATE_PASS) \
if (Name == NAME) { \
PM.addPass(CREATE_PASS); \
return true; \
}
#include "llvm-julia-passes.inc"
#undef FUNCTION_PASS
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;
}
if (Name == "FinalLowerGC") {
PM.addPass(FinalLowerGCPass());
return true;
}
if (Name == "RemoveJuliaAddrspaces") {
PM.addPass(RemoveJuliaAddrspacesPass());
return true;
}
if (Name == "MultiVersioning") {
PM.addPass(MultiVersioning());
return true;
}
if (Name == "LowerPTLS") {
PM.addPass(LowerPTLSPass());
return true;
#define MODULE_PASS(NAME, CREATE_PASS) \
if (Name == NAME) { \
PM.addPass(CREATE_PASS); \
return true; \
}
#include "llvm-julia-passes.inc"
#undef MODULE_PASS
return false;
});

PB.registerPipelineParsingCallback(
[](StringRef Name, LoopPassManager &PM,
ArrayRef<PassBuilder::PipelineElement> InnerPipeline) {
if (Name == "JuliaLICM") {
PM.addPass(JuliaLICMPass());
return true;
#define LOOP_PASS(NAME, CREATE_PASS) \
if (Name == NAME) { \
PM.addPass(CREATE_PASS); \
return true; \
}
#include "llvm-julia-passes.inc"
#undef LOOP_PASS
return false;
});
}
Expand Down
4 changes: 4 additions & 0 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8597,6 +8597,9 @@ extern "C" void jl_init_llvm(void)
if (jl_using_gdb_jitevents)
jl_ExecutionEngine->enableJITDebuggingSupport();

#if defined(_COMPILER_ASAN_ENABLED_) && defined(JL_USE_NEW_PM)
#warning "JIT profiling support (JL_USE_*_JITEVENTS) not yet available for ASAN with NewPM (requires JITLink)"
#else
#if defined(JL_USE_INTEL_JITEVENTS) || \
defined(JL_USE_OPROFILE_JITEVENTS) || \
defined(JL_USE_PERF_JITEVENTS)
Expand Down Expand Up @@ -8638,6 +8641,7 @@ extern "C" void jl_init_llvm(void)
jl_ExecutionEngine->RegisterJITEventListener(JITEventListener::createPerfJITEventListener());
#endif
#endif
#endif
#endif

cl::PrintOptionValues();
Expand Down
8 changes: 8 additions & 0 deletions src/disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,17 @@ void jl_strip_llvm_debug(Module *m)

void jl_strip_llvm_addrspaces(Module *m)
{
#ifndef JL_USE_NEW_PM
legacy::PassManager PM;
PM.add(createRemoveJuliaAddrspacesPass());
PM.run(*m);
#else
PassBuilder PB;
AnalysisManagers AM{PB};
ModulePassManager PM;
PM.addPass(RemoveJuliaAddrspacesPass());
PM.run(*m, AM.MAM);
#endif
}

// print an llvm IR acquired from jl_get_llvmf
Expand Down
29 changes: 24 additions & 5 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,11 @@ namespace {

namespace {

#ifndef JL_USE_NEW_PM
typedef legacy::PassManager PassManager;
#else
typedef NewPM PassManager;
#endif

orc::JITTargetMachineBuilder createJTMBFromTM(TargetMachine &TM, int optlevel) {
return orc::JITTargetMachineBuilder(TM.getTargetTriple())
Expand All @@ -896,21 +900,24 @@ namespace {
}
};

#ifndef JL_USE_NEW_PM
struct PMCreator {
std::unique_ptr<TargetMachine> TM;
int optlevel;
PMCreator(TargetMachine &TM, int optlevel) : TM(cantFail(createJTMBFromTM(TM, optlevel).createTargetMachine())), optlevel(optlevel) {}
PMCreator(const PMCreator &other) : PMCreator(*other.TM, other.optlevel) {}
PMCreator(PMCreator &&other) : TM(std::move(other.TM)), optlevel(other.optlevel) {}
PMCreator &operator=(const PMCreator &other) {
TM = cantFail(createJTMBFromTM(*other.TM, other.optlevel).createTargetMachine());
optlevel = other.optlevel;
return *this;
}
PMCreator(PMCreator &&other) = default;
PMCreator &operator=(PMCreator &&other) = default;
friend void swap(PMCreator &self, PMCreator &other) {
using std::swap;
swap(self.TM, other.TM);
swap(self.optlevel, other.optlevel);
}
PMCreator &operator=(PMCreator other) {
swap(*this, other);
return *this;
}
std::unique_ptr<PassManager> operator()() {
auto PM = std::make_unique<legacy::PassManager>();
addTargetPasses(PM.get(), TM->getTargetTriple(), TM->getTargetIRAnalysis());
Expand All @@ -920,6 +927,18 @@ namespace {
}
};

#else

struct PMCreator {
orc::JITTargetMachineBuilder JTMB;
OptimizationLevel O;
PMCreator(TargetMachine &TM, int optlevel) : JTMB(createJTMBFromTM(TM, optlevel)), O(getOptLevel(optlevel)) {}
std::unique_ptr<PassManager> operator()() {
return std::make_unique<NewPM>(cantFail(JTMB.createTargetMachine()), O);
}
};
#endif

struct OptimizerT {
OptimizerT(TargetMachine &TM, int optlevel) : optlevel(optlevel), PMs(PMCreator(TM, optlevel)) {}

Expand Down
Loading