2021-11-01 15:09:11 +00:00
|
|
|
/* -*- C++ -*-
|
|
|
|
* Serene Programming Language
|
|
|
|
*
|
2022-01-27 11:44:44 +00:00
|
|
|
* Copyright (c) 2019-2022 Sameer Rahmani <lxsameer@gnu.org>
|
2021-11-01 15:09:11 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 2.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "serene/jit/engine.h"
|
|
|
|
|
|
|
|
#include "serene/context.h"
|
|
|
|
#include "serene/jit/layers.h"
|
|
|
|
#include "serene/utils.h"
|
|
|
|
|
2021-11-04 00:57:00 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2021-11-01 15:09:11 +00:00
|
|
|
namespace serene::jit {
|
|
|
|
|
2021-11-04 00:57:00 +00:00
|
|
|
static void handleLazyCallThroughError() {
|
|
|
|
// TODO: Report to the diag engine
|
|
|
|
llvm::errs() << "LazyCallThrough error: Could not find function body";
|
|
|
|
// TODO: terminate ?
|
|
|
|
}
|
|
|
|
|
2021-11-19 00:41:28 +00:00
|
|
|
SereneJIT::SereneJIT(Namespace &entryNS,
|
2021-11-01 15:09:11 +00:00
|
|
|
std::unique_ptr<orc::ExecutionSession> es,
|
|
|
|
std::unique_ptr<orc::EPCIndirectionUtils> epciu,
|
2021-11-06 21:23:37 +00:00
|
|
|
orc::JITTargetMachineBuilder jtmb, llvm::DataLayout &&dl)
|
2021-11-04 00:57:00 +00:00
|
|
|
|
2021-11-01 15:09:11 +00:00
|
|
|
: es(std::move(es)), epciu(std::move(epciu)), dl(dl),
|
|
|
|
mangler(*this->es, this->dl),
|
|
|
|
objectLayer(
|
|
|
|
*this->es,
|
|
|
|
[]() { return std::make_unique<llvm::SectionMemoryManager>(); }),
|
|
|
|
compileLayer(
|
|
|
|
*this->es, objectLayer,
|
|
|
|
std::make_unique<orc::ConcurrentIRCompiler>(std::move(jtmb))),
|
2021-11-06 21:23:37 +00:00
|
|
|
transformLayer(*this->es, compileLayer, optimizeModule),
|
2021-11-19 00:41:28 +00:00
|
|
|
astLayer(transformLayer, mangler),
|
|
|
|
nsLayer(entryNS.getContext(), transformLayer, mangler, dl),
|
|
|
|
mainJD(this->es->createBareJITDylib(entryNS.name)),
|
|
|
|
ctx(entryNS.getContext()), currentNS(entryNS) {
|
2021-11-01 15:09:11 +00:00
|
|
|
UNUSED(this->ctx);
|
|
|
|
mainJD.addGenerator(
|
|
|
|
cantFail(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
|
|
|
|
dl.getGlobalPrefix())));
|
2021-11-04 00:57:00 +00:00
|
|
|
|
2021-11-06 21:23:37 +00:00
|
|
|
// if (numCompileThreads > 0) {
|
|
|
|
// compileOnDemandLayer.setCloneToNewContextOnEmit(true);
|
|
|
|
// }
|
2021-11-01 15:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Error SereneJIT::addNS(llvm::StringRef nsname,
|
|
|
|
orc::ResourceTrackerSP rt) {
|
|
|
|
if (!rt) {
|
|
|
|
rt = mainJD.getDefaultResourceTracker();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsLayer.add(rt, nsname);
|
|
|
|
};
|
|
|
|
|
2021-11-19 00:41:28 +00:00
|
|
|
llvm::Error SereneJIT::addAst(exprs::Ast &ast, orc::ResourceTrackerSP rt) {
|
|
|
|
if (!rt) {
|
|
|
|
rt = mainJD.getDefaultResourceTracker();
|
|
|
|
}
|
|
|
|
|
|
|
|
return astLayer.add(rt, getCurrentNS(), ast);
|
|
|
|
};
|
|
|
|
|
|
|
|
llvm::Expected<std::unique_ptr<SereneJIT>> makeSereneJIT(Namespace &ns) {
|
2021-11-01 15:09:11 +00:00
|
|
|
auto epc = orc::SelfExecutorProcessControl::Create();
|
|
|
|
if (!epc) {
|
|
|
|
return epc.takeError();
|
|
|
|
}
|
|
|
|
auto es = std::make_unique<orc::ExecutionSession>(std::move(*epc));
|
2021-11-04 00:57:00 +00:00
|
|
|
auto epciu =
|
|
|
|
orc::EPCIndirectionUtils::Create(es->getExecutorProcessControl());
|
|
|
|
if (!epciu) {
|
|
|
|
return epciu.takeError();
|
|
|
|
}
|
|
|
|
|
|
|
|
(*epciu)->createLazyCallThroughManager(
|
|
|
|
*es, llvm::pointerToJITTargetAddress(&handleLazyCallThroughError));
|
|
|
|
|
|
|
|
if (auto err = setUpInProcessLCTMReentryViaEPCIU(**epciu)) {
|
|
|
|
return std::move(err);
|
|
|
|
}
|
2021-11-01 15:09:11 +00:00
|
|
|
|
|
|
|
orc::JITTargetMachineBuilder jtmb(
|
|
|
|
es->getExecutorProcessControl().getTargetTriple());
|
|
|
|
|
|
|
|
auto dl = jtmb.getDefaultDataLayoutForTarget();
|
|
|
|
if (!dl) {
|
|
|
|
return dl.takeError();
|
|
|
|
}
|
|
|
|
|
2021-11-19 00:41:28 +00:00
|
|
|
return std::make_unique<SereneJIT>(ns, std::move(es), std::move(*epciu),
|
2021-11-04 00:57:00 +00:00
|
|
|
std::move(jtmb), std::move(*dl));
|
2021-11-01 15:09:11 +00:00
|
|
|
};
|
|
|
|
} // namespace serene::jit
|