2017-05-26 02:00:14 +02:00
|
|
|
//===- KaleidoscopeJIT.h - A simple JIT for Kaleidoscope --------*- C++ -*-===//
|
2015-08-26 05:07:41 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-08-26 05:07:41 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Contains a simple JIT definition for use in the kaleidoscope tutorials.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
|
|
|
|
#define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
|
|
|
|
|
2020-09-23 07:37:25 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2016-08-01 22:49:11 +02:00
|
|
|
#include "llvm/ExecutionEngine/JITSymbol.h"
|
2015-08-26 05:07:41 +02:00
|
|
|
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
|
2020-09-23 07:37:25 +02:00
|
|
|
#include "llvm/ExecutionEngine/Orc/Core.h"
|
|
|
|
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
|
2021-07-01 03:53:18 +02:00
|
|
|
#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
|
2015-08-26 05:07:41 +02:00
|
|
|
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
|
2020-09-23 07:37:25 +02:00
|
|
|
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
|
2017-02-20 10:56:24 +01:00
|
|
|
#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
|
2018-02-06 22:25:11 +01:00
|
|
|
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
|
2016-05-25 03:18:36 +02:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2020-09-23 07:37:25 +02:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2016-05-25 03:18:36 +02:00
|
|
|
#include <memory>
|
2015-08-26 05:07:41 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace orc {
|
|
|
|
|
|
|
|
class KaleidoscopeJIT {
|
2020-09-23 07:37:25 +02:00
|
|
|
private:
|
|
|
|
std::unique_ptr<ExecutionSession> ES;
|
2015-08-26 05:07:41 +02:00
|
|
|
|
2020-09-23 07:37:25 +02:00
|
|
|
DataLayout DL;
|
|
|
|
MangleAndInterner Mangle;
|
2015-08-26 05:07:41 +02:00
|
|
|
|
2020-09-23 07:37:25 +02:00
|
|
|
RTDyldObjectLinkingLayer ObjectLayer;
|
|
|
|
IRCompileLayer CompileLayer;
|
2015-08-26 05:07:41 +02:00
|
|
|
|
2020-09-23 07:37:25 +02:00
|
|
|
JITDylib &MainJD;
|
|
|
|
|
|
|
|
public:
|
2021-07-27 05:50:19 +02:00
|
|
|
KaleidoscopeJIT(std::unique_ptr<ExecutionSession> ES,
|
2020-09-23 07:37:25 +02:00
|
|
|
JITTargetMachineBuilder JTMB, DataLayout DL)
|
2021-07-27 05:50:19 +02:00
|
|
|
: ES(std::move(ES)), DL(std::move(DL)), Mangle(*this->ES, this->DL),
|
2020-09-23 07:37:25 +02:00
|
|
|
ObjectLayer(*this->ES,
|
|
|
|
[]() { return std::make_unique<SectionMemoryManager>(); }),
|
|
|
|
CompileLayer(*this->ES, ObjectLayer,
|
|
|
|
std::make_unique<ConcurrentIRCompiler>(std::move(JTMB))),
|
|
|
|
MainJD(this->ES->createBareJITDylib("<main>")) {
|
|
|
|
MainJD.addGenerator(
|
|
|
|
cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(
|
|
|
|
DL.getGlobalPrefix())));
|
2015-08-26 05:07:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 07:37:25 +02:00
|
|
|
~KaleidoscopeJIT() {
|
|
|
|
if (auto Err = ES->endSession())
|
|
|
|
ES->reportError(std::move(Err));
|
2015-08-26 05:07:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 07:37:25 +02:00
|
|
|
static Expected<std::unique_ptr<KaleidoscopeJIT>> Create() {
|
2021-07-27 05:50:19 +02:00
|
|
|
auto EPC = SelfExecutorProcessControl::Create();
|
2021-07-01 03:53:18 +02:00
|
|
|
if (!EPC)
|
|
|
|
return EPC.takeError();
|
2020-09-23 07:37:25 +02:00
|
|
|
|
2021-07-27 05:50:19 +02:00
|
|
|
auto ES = std::make_unique<ExecutionSession>(std::move(*EPC));
|
2020-09-23 07:37:25 +02:00
|
|
|
|
2021-07-27 05:50:19 +02:00
|
|
|
JITTargetMachineBuilder JTMB(
|
|
|
|
ES->getExecutorProcessControl().getTargetTriple());
|
2020-09-23 07:37:25 +02:00
|
|
|
|
|
|
|
auto DL = JTMB.getDefaultDataLayoutForTarget();
|
|
|
|
if (!DL)
|
|
|
|
return DL.takeError();
|
|
|
|
|
2021-07-27 05:50:19 +02:00
|
|
|
return std::make_unique<KaleidoscopeJIT>(std::move(ES), std::move(JTMB),
|
|
|
|
std::move(*DL));
|
2015-08-26 05:07:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 07:37:25 +02:00
|
|
|
const DataLayout &getDataLayout() const { return DL; }
|
|
|
|
|
|
|
|
JITDylib &getMainJITDylib() { return MainJD; }
|
|
|
|
|
|
|
|
Error addModule(ThreadSafeModule TSM, ResourceTrackerSP RT = nullptr) {
|
|
|
|
if (!RT)
|
|
|
|
RT = MainJD.getDefaultResourceTracker();
|
|
|
|
return CompileLayer.add(RT, std::move(TSM));
|
2015-08-26 05:07:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 07:37:25 +02:00
|
|
|
Expected<JITEvaluatedSymbol> lookup(StringRef Name) {
|
|
|
|
return ES->lookup({&MainJD}, Mangle(Name.str()));
|
|
|
|
}
|
2015-08-26 05:07:41 +02:00
|
|
|
};
|
|
|
|
|
2016-05-25 03:18:36 +02:00
|
|
|
} // end namespace orc
|
|
|
|
} // end namespace llvm
|
2015-08-26 05:07:41 +02:00
|
|
|
|
|
|
|
#endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
|