Skip to content

Commit

Permalink
fix #35393, use multiple TargetMachines for different optimization le…
Browse files Browse the repository at this point in the history
…vels (#35403)
  • Loading branch information
JeffBezanson committed Apr 9, 2020
1 parent 716c98e commit 70d7e1e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#include <llvm/Support/FormattedStream.h>
#include <llvm/ADT/StringMap.h>

#include <llvm/Support/TargetRegistry.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/Analysis/TargetTransformInfo.h>
#include <llvm/Analysis/TargetLibraryInfo.h>

using namespace llvm;

#include "julia.h"
Expand Down Expand Up @@ -451,13 +456,10 @@ CodeGenOpt::Level CodeGenOptLevelFor(int optlevel)

static void addPassesForOptLevel(legacy::PassManager &PM, TargetMachine &TM, raw_svector_ostream &ObjStream, MCContext *Ctx, int optlevel)
{
auto oldlevel = TM.getOptLevel();
TM.setOptLevel(CodeGenOptLevelFor(optlevel));
addTargetPasses(&PM, &TM);
addOptimizationPasses(&PM, optlevel);
if (TM.addPassesToEmitMC(PM, Ctx, ObjStream))
llvm_unreachable("Target does not support MC emission.");
TM.setOptLevel(oldlevel);
}

CompilerResultT JuliaOJIT::CompilerT::operator()(Module &M)
Expand All @@ -481,8 +483,6 @@ CompilerResultT JuliaOJIT::CompilerT::operator()(Module &M)
}
}
}
auto oldlevel = jit.TM.getOptLevel();
jit.TM.setOptLevel(CodeGenOptLevelFor(optlevel));
if (optlevel == 0)
jit.PM0.run(M);
else if (optlevel == 1)
Expand All @@ -491,7 +491,6 @@ CompilerResultT JuliaOJIT::CompilerT::operator()(Module &M)
jit.PM2.run(M);
else if (optlevel >= 3)
jit.PM3.run(M);
jit.TM.setOptLevel(oldlevel);

std::unique_ptr<MemoryBuffer> ObjBuffer(
new SmallVectorMemoryBuffer(std::move(jit.ObjBufferSV)));
Expand Down Expand Up @@ -542,10 +541,15 @@ JuliaOJIT::JuliaOJIT(TargetMachine &TM)
CompilerT(this)
)
{
addPassesForOptLevel(PM0, TM, ObjStream, Ctx, 0);
addPassesForOptLevel(PM1, TM, ObjStream, Ctx, 1);
addPassesForOptLevel(PM2, TM, ObjStream, Ctx, 2);
addPassesForOptLevel(PM3, TM, ObjStream, Ctx, 3);
for (int i = 0; i < 4; i++) {
TMs[i] = TM.getTarget().createTargetMachine(TM.getTargetTriple().getTriple(), TM.getTargetCPU(),
TM.getTargetFeatureString(), TM.Options, Reloc::Static, TM.getCodeModel(),
CodeGenOptLevelFor(i), true);
}
addPassesForOptLevel(PM0, *TMs[0], ObjStream, Ctx, 0);
addPassesForOptLevel(PM1, *TMs[1], ObjStream, Ctx, 1);
addPassesForOptLevel(PM2, *TMs[2], ObjStream, Ctx, 2);
addPassesForOptLevel(PM3, *TMs[3], ObjStream, Ctx, 3);

// Make sure SectionMemoryManager::getSymbolAddressInProcess can resolve
// symbols in the program as well. The nullptr argument to the function
Expand Down
1 change: 1 addition & 0 deletions src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class JuliaOJIT {
legacy::PassManager PM1;
legacy::PassManager PM2;
legacy::PassManager PM3;
TargetMachine *TMs[4];
MCContext *Ctx;
std::shared_ptr<RTDyldMemoryManager> MemMgr;
DebugObjectRegistrar registrar;
Expand Down

2 comments on commit 70d7e1e

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.