Skip to content

Commit

Permalink
Optionally use NewPM for optimization (#46176)
Browse files Browse the repository at this point in the history
* Use NewPM for optimization unless ASAN is in effect

* Disable NewPM by default
  • Loading branch information
pchintalapudi authored Sep 7, 2022
1 parent 31d4c22 commit 83658d2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
23 changes: 15 additions & 8 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,17 +551,20 @@ void jl_dump_native_impl(void *native_code,
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());
dataM->setDataLayout(jl_create_datalayout(*TM));

#ifndef JL_USE_NEW_PM
legacy::PassManager optimizer;
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, false}};
#endif

Type *T_size;
if (sizeof(size_t) == 8)
T_size = Type::getInt64Ty(Context);
Expand All @@ -588,7 +591,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, empty.MAM);
optimizer.run(M);
if (bc_fname || obj_fname || asm_fname) optimizer.run(M);

// We would like to emit an alias or an weakref alias to redirect these symbols
// but LLVM doesn't let us emit a GlobalAlias to a declaration...
Expand Down Expand Up @@ -1065,10 +1068,14 @@ void jl_get_llvmf_defn_impl(jl_llvmf_dump_t* dump, jl_method_instance_t *mi, siz
for (auto &global : output.globals)
global.second->setLinkage(GlobalValue::ExternalLinkage);
if (optimize) {
#ifndef JL_USE_NEW_PM
legacy::PassManager PM;
addTargetPasses(&PM, jl_ExecutionEngine->getTargetTriple(), jl_ExecutionEngine->getTargetIRAnalysis());
addOptimizationPasses(&PM, jl_options.opt_level);
addMachinePasses(&PM, jl_options.opt_level);
#else
NewPM PM{jl_ExecutionEngine->cloneTargetMachine(), getOptLevel(jl_options.opt_level)};
#endif
//Safe b/c context lock is held by output
PM.run(*m.getModuleUnlocked());
}
Expand Down
18 changes: 17 additions & 1 deletion src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,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 @@ -995,6 +999,7 @@ namespace {
}
};

#ifndef JL_USE_NEW_PM
struct PMCreator {
std::unique_ptr<TargetMachine> TM;
int optlevel;
Expand All @@ -1010,14 +1015,25 @@ namespace {
swap(*this, other);
return *this;
}
std::unique_ptr<PassManager> operator()() {
auto operator()() {
auto PM = std::make_unique<legacy::PassManager>();
addTargetPasses(PM.get(), TM->getTargetTriple(), TM->getTargetIRAnalysis());
addOptimizationPasses(PM.get(), optlevel);
addMachinePasses(PM.get(), optlevel);
return PM;
}
};
#else
struct PMCreator {
orc::JITTargetMachineBuilder JTMB;
OptimizationLevel O;
PMCreator(TargetMachine &TM, int optlevel) : JTMB(createJTMBFromTM(TM, optlevel)), O(getOptLevel(optlevel)) {}

auto 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

0 comments on commit 83658d2

Please sign in to comment.