Skip to content

Commit

Permalink
Create the new pass manager pipelines (#46175)
Browse files Browse the repository at this point in the history
* Create basic NewPM structures

* Replace incidental uses of the legacy pass manager with the new pass manager

* Run the MC emitter
  • Loading branch information
pchintalapudi authored Jul 30, 2022
1 parent 6b51780 commit 2e0b75c
Show file tree
Hide file tree
Showing 6 changed files with 700 additions and 74 deletions.
3 changes: 2 additions & 1 deletion 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 @@ -317,6 +317,7 @@ $(BUILDDIR)/signal-handling.o $(BUILDDIR)/signal-handling.dbg.obj: $(addprefix $
$(BUILDDIR)/staticdata.o $(BUILDDIR)/staticdata.dbg.obj: $(SRCDIR)/processor.h $(SRCDIR)/builtin_proto.h
$(BUILDDIR)/toplevel.o $(BUILDDIR)/toplevel.dbg.obj: $(SRCDIR)/builtin_proto.h
$(BUILDDIR)/ircode.o $(BUILDDIR)/ircode.dbg.obj: $(SRCDIR)/serialize.h
$(BUILDDIR)/pipeline.o $(BUILDDIR)/pipeline.dbg.obj: $(SRCDIR)/passes.h $(SRCDIR)/jitlayers.h

$(addprefix $(BUILDDIR)/,threading.o threading.dbg.obj gc.o gc.dbg.obj init.c init.dbg.obj task.o task.dbg.obj): $(addprefix $(SRCDIR)/,threading.h)
$(addprefix $(BUILDDIR)/,APInt-C.o APInt-C.dbg.obj runtime_intrinsics.o runtime_intrinsics.dbg.obj): $(SRCDIR)/APInt-C.h
Expand Down
92 changes: 22 additions & 70 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,20 +515,23 @@ void jl_dump_native_impl(void *native_code,
std::vector<NewArchiveMember> unopt_bc_Archive;
std::vector<std::string> outputs;

legacy::PassManager preopt, postopt;
PassBuilder emptyPB;
AnalysisManagers empty(emptyPB);
ModulePassManager preopt, postopt;
legacy::PassManager emitter; // MC emission is only supported on legacy PM

if (unopt_bc_fname)
preopt.add(createBitcodeWriterPass(unopt_bc_OS));
preopt.addPass(BitcodeWriterPass(unopt_bc_OS));

//Is this necessary for TM?
// addTargetPasses(&postopt, TM->getTargetTriple(), TM->getTargetIRAnalysis());
if (bc_fname)
postopt.add(createBitcodeWriterPass(bc_OS));
postopt.addPass(BitcodeWriterPass(bc_OS));
//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;
Expand Down Expand Up @@ -567,7 +570,7 @@ 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, empty.MAM);
optimizer.run(M);

// We would like to emit an alias or an weakref alias to redirect these symbols
Expand All @@ -585,7 +588,8 @@ 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, empty.MAM);
emitter.run(M);

if (unopt_bc_fname)
emit_result(unopt_bc_Archive, unopt_bc_Buffer, unopt_bc_Name, outputs);
Expand Down Expand Up @@ -946,79 +950,27 @@ 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
8 changes: 5 additions & 3 deletions src/disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,11 @@ void jl_strip_llvm_debug(Module *m)

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

// print an llvm IR acquired from jl_get_llvmf
Expand Down
40 changes: 40 additions & 0 deletions src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
#include <llvm/ExecutionEngine/Orc/IRTransformLayer.h>
#include <llvm/ExecutionEngine/JITEventListener.h>

#include <llvm/Passes/PassBuilder.h>
#include <llvm/Passes/PassPlugin.h>
#include <llvm/Passes/StandardInstrumentations.h>

#include <llvm/Target/TargetMachine.h>
#include "julia_assert.h"
#include "debug-registry.h"
#include "platform.h"

#include <stack>
#include <queue>
Expand Down Expand Up @@ -69,6 +74,41 @@ static inline bool imaging_default() {
return jl_options.image_codegen || (jl_generating_output() && !jl_options.incremental);
}

struct OptimizationOptions {
bool lower_intrinsics;
bool dump_native;
bool external_use;

static constexpr OptimizationOptions defaults() {
return {true, false, false};
}
};

struct NewPM {
std::unique_ptr<TargetMachine> TM;
StandardInstrumentations SI;
std::unique_ptr<PassInstrumentationCallbacks> PIC;
PassBuilder PB;
ModulePassManager MPM;
OptimizationLevel O;

NewPM(std::unique_ptr<TargetMachine> TM, OptimizationLevel O, OptimizationOptions options = OptimizationOptions::defaults());

void run(Module &M);
};

struct AnalysisManagers {
LoopAnalysisManager LAM;
FunctionAnalysisManager FAM;
CGSCCAnalysisManager CGAM;
ModuleAnalysisManager MAM;

AnalysisManagers(PassBuilder &PB);
AnalysisManagers(TargetMachine &TM, PassBuilder &PB, OptimizationLevel O);
};

OptimizationLevel getOptLevel(int optlevel);

struct jl_locked_stream {
JL_STREAM *stream = nullptr;
std::mutex mutex;
Expand Down
27 changes: 27 additions & 0 deletions src/llvm-julia-passes.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//Module passes
#ifdef MODULE_PASS
MODULE_PASS("CPUFeatures", CPUFeatures())
MODULE_PASS("RemoveNI", RemoveNI())
MODULE_PASS("LowerSIMDLoop", LowerSIMDLoop())
MODULE_PASS("FinalLowerGC", FinalLowerGCPass())
MODULE_PASS("JuliaMultiVersioning", MultiVersioning())
MODULE_PASS("RemoveJuliaAddrspaces", RemoveJuliaAddrspacesPass())
MODULE_PASS("RemoveAddrspaces", RemoveAddrspacesPass())
MODULE_PASS("LowerPTLSPass", LowerPTLSPass())
#endif

//Function passes
#ifdef FUNCTION_PASS
FUNCTION_PASS("DemoteFloat16", DemoteFloat16())
FUNCTION_PASS("CombineMulAdd", CombineMulAdd())
FUNCTION_PASS("LateLowerGCFrame", LateLowerGC())
FUNCTION_PASS("AllocOpt", AllocOptPass())
FUNCTION_PASS("PropagateJuliaAddrspaces", PropagateJuliaAddrspacesPass())
FUNCTION_PASS("LowerExcHandlers", LowerExcHandlers())
FUNCTION_PASS("GCInvariantVerifier", GCInvariantVerifierPass())
#endif

//Loop passes
#ifdef LOOP_PASS
LOOP_PASS("JuliaLICM", JuliaLICMPass())
#endif
Loading

0 comments on commit 2e0b75c

Please sign in to comment.