1 //===- KaleidoscopeJIT.h - A simple JIT for Kaleidoscope --------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Contains a simple JIT definition for use in the kaleidoscope tutorials.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
14 #define LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ExecutionEngine/JITSymbol.h"
18 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
19 #include "llvm/ExecutionEngine/Orc/Core.h"
20 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
21 #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
22 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
23 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
24 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
25 #include "llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h"
26 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/LLVMContext.h"
34 class KaleidoscopeJIT
{
36 std::unique_ptr
<ExecutionSession
> ES
;
39 MangleAndInterner Mangle
;
41 RTDyldObjectLinkingLayer ObjectLayer
;
42 IRCompileLayer CompileLayer
;
47 KaleidoscopeJIT(std::unique_ptr
<ExecutionSession
> ES
,
48 JITTargetMachineBuilder JTMB
, DataLayout DL
)
49 : ES(std::move(ES
)), DL(std::move(DL
)), Mangle(*this->ES
, this->DL
),
50 ObjectLayer(*this->ES
,
51 []() { return std::make_unique
<SectionMemoryManager
>(); }),
52 CompileLayer(*this->ES
, ObjectLayer
,
53 std::make_unique
<ConcurrentIRCompiler
>(std::move(JTMB
))),
54 MainJD(this->ES
->createBareJITDylib("<main>")) {
56 cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(
57 DL
.getGlobalPrefix())));
58 if (JTMB
.getTargetTriple().isOSBinFormatCOFF()) {
59 ObjectLayer
.setOverrideObjectFlagsWithResponsibilityFlags(true);
60 ObjectLayer
.setAutoClaimResponsibilityForObjectSymbols(true);
65 if (auto Err
= ES
->endSession())
66 ES
->reportError(std::move(Err
));
69 static Expected
<std::unique_ptr
<KaleidoscopeJIT
>> Create() {
70 auto EPC
= SelfExecutorProcessControl::Create();
72 return EPC
.takeError();
74 auto ES
= std::make_unique
<ExecutionSession
>(std::move(*EPC
));
76 JITTargetMachineBuilder
JTMB(
77 ES
->getExecutorProcessControl().getTargetTriple());
79 auto DL
= JTMB
.getDefaultDataLayoutForTarget();
81 return DL
.takeError();
83 return std::make_unique
<KaleidoscopeJIT
>(std::move(ES
), std::move(JTMB
),
87 const DataLayout
&getDataLayout() const { return DL
; }
89 JITDylib
&getMainJITDylib() { return MainJD
; }
91 Error
addModule(ThreadSafeModule TSM
, ResourceTrackerSP RT
= nullptr) {
93 RT
= MainJD
.getDefaultResourceTracker();
94 return CompileLayer
.add(RT
, std::move(TSM
));
97 Expected
<ExecutorSymbolDef
> lookup(StringRef Name
) {
98 return ES
->lookup({&MainJD
}, Mangle(Name
.str()));
102 } // end namespace orc
103 } // end namespace llvm
105 #endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H