Skip to content

Commit

Permalink
Change TM handling slighly and force no fastisel
Browse files Browse the repository at this point in the history
in some targets
  • Loading branch information
gbaraldi committed Oct 18, 2023
1 parent 213c89b commit 8f117e0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/aotcompile.cpp
Expand Up @@ -1001,7 +1001,7 @@ static AOTOutputs add_output_impl(Module &M, TargetMachine &SourceTM, ShardTimer
SourceTM.getRelocationModel(),
SourceTM.getCodeModel(),
SourceTM.getOptLevel()));

fixupTM(*TM);
if (unopt) {
timers.unopt.startTimer();
raw_svector_ostream OS(out.unopt);
Expand Down Expand Up @@ -1029,6 +1029,7 @@ static AOTOutputs add_output_impl(Module &M, TargetMachine &SourceTM, ShardTimer
SourceTM.getRelocationModel(),
SourceTM.getCodeModel(),
SourceTM.getOptLevel()));
fixupTM(*PMTM);
NewPM optimizer{std::move(PMTM), getOptLevel(jl_options.opt_level), OptimizationOptions::defaults(true, true)};
optimizer.run(M);
assert(!verifyLLVMIR(M));
Expand Down Expand Up @@ -1514,6 +1515,7 @@ void jl_dump_native_impl(void *native_code,
CMModel,
CodeGenOpt::Aggressive // -O3 TODO: respect command -O0 flag?
));
fixupTM(*SourceTM);
auto DL = jl_create_datalayout(*SourceTM);
std::string StackProtectorGuard;
unsigned OverrideStackAlignment;
Expand Down
2 changes: 1 addition & 1 deletion src/codegen.cpp
Expand Up @@ -4802,7 +4802,7 @@ static Value *global_binding_pointer(jl_codectx_t &ctx, jl_module_t *m, jl_sym_t
// var not found. switch to delayed lookup.
Constant *initnul = Constant::getNullValue(ctx.types().T_pjlvalue);
GlobalVariable *bindinggv = new GlobalVariable(*ctx.f->getParent(), ctx.types().T_pjlvalue,
false, GlobalVariable::PrivateLinkage, initnul);
false, GlobalVariable::PrivateLinkage, initnul, "jl_binding_ptr"); // LLVM has bugs with nameless globals
LoadInst *cachedval = ctx.builder.CreateAlignedLoad(ctx.types().T_pjlvalue, bindinggv, Align(sizeof(void*)));
setName(ctx.emission_context, cachedval, jl_symbol_name(m->name) + StringRef(".") + jl_symbol_name(s) + ".cached");
cachedval->setOrdering(AtomicOrdering::Unordered);
Expand Down
34 changes: 26 additions & 8 deletions src/jitlayers.cpp
Expand Up @@ -1198,6 +1198,12 @@ namespace {
#else
None;
#endif
if (TheTriple.isAArch64())
codemodel = CodeModel::Small;
if (TheTriple.isPPC()) {
// On PPC the small model is limited to 16bit offsets
codemodel = CodeModel::Medium;
}
auto optlevel = CodeGenOptLevelFor(jl_options.opt_level);
auto TM = TheTarget->createTargetMachine(
TheTriple.getTriple(), TheCPU, FeaturesStr,
Expand All @@ -1209,11 +1215,7 @@ namespace {
);
assert(TM && "Failed to select target machine -"
" Is the LLVM backend for this CPU enabled?");
if (!TheTriple.isARM() && !TheTriple.isPPC64()) {
// FastISel seems to be buggy for ARM. Ref #13321
if (jl_options.opt_level < 2)
TM->setFastISel(true);
}
fixupTM(*TM);
return std::unique_ptr<TargetMachine>(TM);
}
} // namespace
Expand All @@ -1239,7 +1241,9 @@ namespace {
: JTMB(createJTMBFromTM(TM, optlevel)) {}

std::unique_ptr<TargetMachine> operator()() JL_NOTSAFEPOINT {
return cantFail(JTMB.createTargetMachine());
auto TM = cantFail(JTMB.createTargetMachine());
fixupTM(*TM);
return TM;
}
};

Expand All @@ -1251,7 +1255,9 @@ namespace {
: JTMB(createJTMBFromTM(TM, optlevel)), O(getOptLevel(optlevel)), printers(printers) {}

auto operator()() JL_NOTSAFEPOINT {
auto NPM = std::make_unique<NewPM>(cantFail(JTMB.createTargetMachine()), O);
auto TM = cantFail(JTMB.createTargetMachine());
fixupTM(*TM);
auto NPM = std::make_unique<NewPM>(std::move(TM), O);
// TODO this needs to be locked, as different resource pools may add to the printer vector at the same time
printers.push_back([NPM = NPM.get()]() JL_NOTSAFEPOINT {
NPM->printTimers();
Expand Down Expand Up @@ -1637,6 +1643,16 @@ void optimizeDLSyms(Module &M) {
JuliaOJIT::DLSymOptimizer(true)(M);
}

void fixupTM(TargetMachine &TM){
auto TheTriple = TM.getTargetTriple();
if (jl_options.opt_level < 2) {
if (!TheTriple.isARM() && !TheTriple.isPPC64() && !TheTriple.isAArch64())
TM.setFastISel(true);
else // FastISel seems to be buggy Ref #13321
TM.setFastISel(false);
}
}

llvm::DataLayout jl_create_datalayout(TargetMachine &TM) {
// Mark our address spaces as non-integral
auto jl_data_layout = TM.createDataLayout();
Expand Down Expand Up @@ -2153,7 +2169,7 @@ void jl_merge_module(orc::ThreadSafeModule &destTSM, orc::ThreadSafeModule srcTS

std::unique_ptr<TargetMachine> JuliaOJIT::cloneTargetMachine() const
{
return std::unique_ptr<TargetMachine>(getTarget()
auto NewTM = std::unique_ptr<TargetMachine>(getTarget()
.createTargetMachine(
getTargetTriple().str(),
getTargetCPU(),
Expand All @@ -2162,6 +2178,8 @@ std::unique_ptr<TargetMachine> JuliaOJIT::cloneTargetMachine() const
TM->getRelocationModel(),
TM->getCodeModel(),
TM->getOptLevel()));
fixupTM(*NewTM);
return NewTM;
}

const Triple& JuliaOJIT::getTargetTriple() const {
Expand Down
1 change: 1 addition & 0 deletions src/jitlayers.h
Expand Up @@ -569,6 +569,7 @@ Module &jl_codegen_params_t::shared_module() JL_NOTSAFEPOINT {
}
return *_shared_module;
}
void fixupTM(TargetMachine &TM) JL_NOTSAFEPOINT;

void optimizeDLSyms(Module &M);

Expand Down
4 changes: 2 additions & 2 deletions src/pipeline.cpp
Expand Up @@ -744,9 +744,9 @@ PIC.addClassToPassName(decltype(CREATE_PASS)::name(), NAME);
}

NewPM::NewPM(std::unique_ptr<TargetMachine> TM, OptimizationLevel O, OptimizationOptions options) :
TM(std::move(TM)),
TM(std::move(TM)),
#if JL_LLVM_VERSION < 160000
SI(false),
SI(false),
PIC(createPIC(SI)),
#else
PIC(createPIC()),
Expand Down

0 comments on commit 8f117e0

Please sign in to comment.