Skip to content

Commit

Permalink
Use NewPM for optimization unless ASAN is in effect
Browse files Browse the repository at this point in the history
  • Loading branch information
pchintalapudi committed Jul 28, 2022
1 parent 87e4b09 commit 690e882
Show file tree
Hide file tree
Showing 3 changed files with 36 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 @@ -534,17 +534,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 @@ -571,7 +574,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 @@ -1048,10 +1051,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 @@ -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,6 +900,7 @@ namespace {
}
};

#ifndef JL_USE_NEW_PM
struct PMCreator {
std::unique_ptr<TargetMachine> TM;
int optlevel;
Expand All @@ -911,14 +916,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
4 changes: 4 additions & 0 deletions src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ static inline bool imaging_default() {
return jl_options.image_codegen || (jl_generating_output() && !jl_options.incremental);
}

#ifndef _COMPILER_ASAN_ENABLED_
#define JL_USE_NEW_PM
#endif

struct OptimizationOptions {
bool lower_intrinsics;
bool dump_native;
Expand Down

0 comments on commit 690e882

Please sign in to comment.