Skip to content

Commit

Permalink
Use NewPM almost everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
pchintalapudi committed Jul 21, 2022
1 parent 4119135 commit 9ebd5ce
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
60 changes: 45 additions & 15 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,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 @@ -587,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 @@ -605,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 @@ -623,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
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
14 changes: 14 additions & 0 deletions src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ struct NewPM {
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);

#define JL_USE_NEW_PM

typedef struct _jl_llvm_functions_t {
std::string functionObject; // jlcall llvm Function name
std::string specFunctionObject; // specialized llvm Function name
Expand Down
2 changes: 1 addition & 1 deletion src/llvm-julia-passes.inc
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ FUNCTION_PASS("GCInvariantVerifier", GCInvariantVerifierPass())
//Loop passes
#ifdef LOOP_PASS
LOOP_PASS("JuliaLICM", JuliaLICMPass())
#endif
#endif
42 changes: 32 additions & 10 deletions src/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,18 +567,40 @@ NewPM::NewPM(std::unique_ptr<TargetMachine> TM, OptimizationLevel O, Optimizatio
PB(this->TM.get(), PipelineTuningOptions(), None, PIC.get()),
MPM(createMPM(PB, O, options)), O(O) {}

void NewPM::run(Module &M) {
//We must recreate the analysis managers every time
//so that analyses from previous runs of the pass manager
//do not hang around for the next run
LoopAnalysisManager LAM;
FunctionAnalysisManager FAM(createFAM(O, TM->getTargetIRAnalysis(), TM->getTargetTriple()));
CGSCCAnalysisManager CGAM;
ModuleAnalysisManager MAM;
AnalysisManagers::AnalysisManagers(TargetMachine &TM, PassBuilder &PB, OptimizationLevel O) : LAM(), FAM(createFAM(O, TM.getTargetIRAnalysis(), TM.getTargetTriple())), CGAM(), MAM() {
PB.registerLoopAnalyses(LAM);
PB.registerFunctionAnalyses(FAM);
PB.registerCGSCCAnalyses(CGAM);
PB.registerModuleAnalyses(MAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
}

AnalysisManagers::AnalysisManagers(PassBuilder &PB) : LAM(), FAM(), CGAM(), MAM() {
PB.registerLoopAnalyses(LAM);
PB.registerFunctionAnalyses(FAM);
PB.registerCGSCCAnalyses(CGAM);
PB.registerModuleAnalyses(MAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
MPM.run(M, MAM);
}
}

void NewPM::run(Module &M) {
//We must recreate the analysis managers every time
//so that analyses from previous runs of the pass manager
//do not hang around for the next run
AnalysisManagers AM{*TM, PB, O};
MPM.run(M, AM.MAM);
}

OptimizationLevel getOptLevel(int optlevel) {
switch (std::min(std::max(optlevel, 0), 3)) {
case 0:
return OptimizationLevel::O0;
case 1:
return OptimizationLevel::O1;
case 2:
return OptimizationLevel::O2;
case 3:
return OptimizationLevel::O3;
}
llvm_unreachable("cannot get here!");
}

0 comments on commit 9ebd5ce

Please sign in to comment.