Skip to content

Commit 13b73ed

Browse files
authored
Replace SimpleCompiler to avoid a copy and createObjectFile (#59486)
Replace the call to orc::SimpleCompiler::operator() with equivalent code that doesn't unnecessarily parse the object file again (if it would have failed, we're going to find out later anyway). This also lets us make the MemoryBuffer name unique and avoid copying it in JuliaOJIT::addObjectFile. REF: https://github.com/JuliaLang/llvm-project/blob/85122d41d6783e828412f9a1d0f7c771e2a38acc/llvm/lib/ExecutionEngine/Orc/CompileUtils.cpp#L38-L67
1 parent d1282cb commit 13b73ed

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/jitlayers.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,10 +1609,33 @@ namespace {
16091609
PoolIdx = jl_options.opt_level;
16101610
}
16111611
assert(PoolIdx < N && "Invalid optimization level for compiler!");
1612-
return orc::SimpleCompiler(****TMs[PoolIdx])(M);
1612+
1613+
auto TM = **TMs[PoolIdx];
1614+
if (M.getDataLayout().isDefault())
1615+
M.setDataLayout((*TM)->createDataLayout());
1616+
1617+
SmallVector<char, 0> ObjBufferSV;
1618+
{
1619+
raw_svector_ostream ObjStream(ObjBufferSV);
1620+
legacy::PassManager PM;
1621+
MCContext *Ctx;
1622+
if ((*TM)->addPassesToEmitMC(PM, Ctx, ObjStream))
1623+
return make_error<StringError>("Target does not support MC emission",
1624+
inconvertibleErrorCode());
1625+
PM.run(M);
1626+
}
1627+
1628+
// OrcJIT requires that all modules / files have unique names:
1629+
// https://llvm.org/doxygen/namespacellvm_1_1orc.html#a1f5a1bc60c220cdccbab0f26b2a425e1
1630+
auto name = (M.getModuleIdentifier() + "-jitted-" +
1631+
Twine(jl_atomic_fetch_add_relaxed(&bufcounter, 1)))
1632+
.str();
1633+
return std::make_unique<SmallVectorMemoryBuffer>(std::move(ObjBufferSV), name,
1634+
false);
16131635
}
16141636

16151637
std::array<std::unique_ptr<JuliaOJIT::ResourcePool<std::unique_ptr<TargetMachine>>>, N> TMs;
1638+
_Atomic(size_t) bufcounter{0};
16161639
};
16171640
}
16181641

@@ -2133,11 +2156,6 @@ Error JuliaOJIT::addExternalModule(orc::JITDylib &JD, orc::ThreadSafeModule TSM,
21332156

21342157
Error JuliaOJIT::addObjectFile(orc::JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
21352158
assert(Obj && "Can not add null object");
2136-
// OrcJIT requires that all modules / files have unique names:
2137-
// https://llvm.org/doxygen/namespacellvm_1_1orc.html#a1f5a1bc60c220cdccbab0f26b2a425e1
2138-
// so we have to force a copy here
2139-
std::string Name = ("jitted-" + Twine(jl_atomic_fetch_add_relaxed(&jitcounter, 1))).str();
2140-
Obj = Obj->getMemBufferCopy(Obj->getBuffer(), Name);
21412159
return ObjectLayer.add(JD.getDefaultResourceTracker(), std::move(Obj));
21422160
}
21432161

0 commit comments

Comments
 (0)