Skip to content

Commit

Permalink
Use NewPM in most places
Browse files Browse the repository at this point in the history
  • Loading branch information
pchintalapudi committed May 25, 2022
1 parent 3812144 commit f3b7063
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 51 deletions.
67 changes: 17 additions & 50 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ void jl_dump_native_impl(void *native_code,
CodeGenOpt::Aggressive // -O3 TODO: respect command -O0 flag?
));

// 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));


// set up optimization passes
SmallVector<char, 0> bc_Buffer;
Expand All @@ -555,7 +560,6 @@ void jl_dump_native_impl(void *native_code,

if (bc_fname)
postopt.addPass(BitcodeWriterPass(bc_OS));
//Is this necessary for TM?
addTargetPasses(&emitter, TM->getTargetTriple(), TM->getTargetIRAnalysis());
if (obj_fname)
if (TM->addPassesToEmitFile(emitter, obj_OS, nullptr, CGFT_ObjectFile, false))
Expand All @@ -564,17 +568,16 @@ 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");

#ifdef JL_USE_NEW_PM
NewPM optimizer(std::move(TM), jl_options.opt_level, {true, true, false});
#else
legacy::PassManager optimizer;
if (bc_fname || obj_fname || asm_fname) {
addTargetPasses(&optimizer, TM->getTargetTriple(), TM->getTargetIRAnalysis());
addOptimizationPasses(&optimizer, jl_options.opt_level, {true, true, false});
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));
#endif
Type *T_size;
if (sizeof(size_t) == 8)
T_size = Type::getInt64Ty(Context);
Expand All @@ -601,11 +604,13 @@ 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, none);
if (unopt_bc_fname)
emit_result(unopt_bc_Archive, unopt_bc_Buffer, unopt_bc_Name, outputs);
if (!bc_fname && !obj_fname && !asm_fname)
return;
optimizer.run(M);
postopt.run(M, none);
emitter.run(M);
if (unopt_bc_fname)
emit_result(unopt_bc_Archive, unopt_bc_Buffer, unopt_bc_Name, outputs);
if (bc_fname)
emit_result(bc_Archive, bc_Buffer, bc_Name, outputs);
if (obj_fname)
Expand Down Expand Up @@ -1417,48 +1422,6 @@ PreservedAnalyses NewPM::run(Module &M) {
// or adapting PassBuilder (or subclassing it) to suite our needs. This is in particular important for
// BPF, NVPTX, and AMDGPU.

void optimizeModule(Module &M, TargetMachine *TM, int opt_level, OptimizationOptions options)
{
// llvm::PassBuilder pb(targetMachine->LLVM, llvm::PipelineTuningOptions(), llvm::None, &passInstrumentationCallbacks);
PassBuilder PB;
// Create the analysis managers.
LoopAnalysisManager LAM;
PB.registerLoopAnalyses(LAM);

AAManager AA;
// TODO: Why are we only doing this for -O3?
if (opt_level >= 3) {
AA.registerFunctionAnalysis<BasicAA>();
}
if (opt_level >= 2) {
AA.registerFunctionAnalysis<ScopedNoAliasAA>();
AA.registerFunctionAnalysis<TypeBasedAA>();
}
// TM->registerDefaultAliasAnalyses(AA);

FunctionAnalysisManager FAM;
// Register the AA manager first so that our version is the one used.
FAM.registerPass([&] { return std::move(AA); });
// Register our TargetLibraryInfoImpl.
FAM.registerPass([&] { return llvm::TargetIRAnalysis(TM->getTargetIRAnalysis()); });
FAM.registerPass([&] { return llvm::TargetLibraryAnalysis(llvm::TargetLibraryInfoImpl(TM->getTargetTriple())); });

PB.registerFunctionAnalyses(FAM);

CGSCCAnalysisManager CGAM;
PB.registerCGSCCAnalyses(CGAM);

ModuleAnalysisManager MAM;
PB.registerModuleAnalyses(MAM);

PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);

ModulePassManager MPM;
addPipeline(MPM, opt_level, options);

MPM.run(M, MAM);
}

// new pass manager plugin

// NOTE: Instead of exporting all the constructors in passes.h we could
Expand Down Expand Up @@ -1577,10 +1540,14 @@ void *jl_get_llvmf_defn_impl(jl_method_instance_t *mi, size_t world, char getwra
for (auto &global : output.globals)
global.second->setLinkage(GlobalValue::ExternalLinkage);
if (optimize) {
#ifdef JL_USE_NEW_PM
NewPM PM(jl_ExecutionEngine->cloneTargetMachine(), jl_options.opt_level);
#else
legacy::PassManager PM;
addTargetPasses(&PM, jl_ExecutionEngine->getTargetTriple(), jl_ExecutionEngine->getTargetIRAnalysis());
addOptimizationPasses(&PM, jl_options.opt_level);
addMachinePasses(&PM, jl_options.opt_level);
#endif
//Safe b/c context lock is held by output
PM.run(*m.getModuleUnlocked());
}
Expand Down
24 changes: 23 additions & 1 deletion src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,11 @@ namespace {

namespace {

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

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

struct PMCreator {
#ifdef JL_USE_NEW_PM
orc::JITTargetMachineBuilder JTMB;
#else
std::unique_ptr<TargetMachine> TM;
#endif
int optlevel;
PMCreator(TargetMachine &TM, int optlevel) : TM(cantFail(createJTMBFromTM(TM, optlevel).createTargetMachine())), optlevel(optlevel) {}
PMCreator(TargetMachine &TM, int optlevel) :
#ifdef JL_USE_NEW_PM
JTMB(createJTMBFromTM(TM, optlevel)),
#else
TM(cantFail(createJTMBFromTM(TM, optlevel).createTargetMachine())),
#endif
optlevel(optlevel) {}

#ifndef JL_USE_NEW_PM
PMCreator(const PMCreator &other) : PMCreator(*other.TM, other.optlevel) {}
PMCreator(PMCreator &&other) : TM(std::move(other.TM)), optlevel(other.optlevel) {}
friend void swap(PMCreator &self, PMCreator &other) {
Expand All @@ -902,12 +918,18 @@ namespace {
swap(*this, other);
return *this;
}
#endif

std::unique_ptr<PassManager> operator()() {
#ifdef JL_USE_NEW_PM
return std::make_unique<NewPM>(cantFail(JTMB.createTargetMachine()), optlevel);
#else
auto PM = std::make_unique<legacy::PassManager>();
addTargetPasses(PM.get(), TM->getTargetTriple(), TM->getTargetIRAnalysis());
addOptimizationPasses(PM.get(), optlevel);
addMachinePasses(PM.get(), optlevel);
return PM;
#endif
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ Pass *createLowerSimdLoopPass();
// NewPM
#include "passes.h"

#define JL_USE_NEW_PM

// Whether the Function is an llvm or julia intrinsic.
static inline bool isIntrinsicFunction(Function *F)
{
Expand Down

0 comments on commit f3b7063

Please sign in to comment.