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/Orc/CompileOnDemandLayer.h"
18 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
19 #include "llvm/ExecutionEngine/Orc/Core.h"
20 #include "llvm/ExecutionEngine/Orc/EPCIndirectionUtils.h"
21 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
22 #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
23 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
24 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
25 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
26 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
27 #include "llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h"
28 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/LegacyPassManager.h"
32 #include "llvm/Transforms/InstCombine/InstCombine.h"
33 #include "llvm/Transforms/Scalar.h"
34 #include "llvm/Transforms/Scalar/GVN.h"
40 class KaleidoscopeJIT
{
42 std::unique_ptr
<ExecutionSession
> ES
;
43 std::unique_ptr
<EPCIndirectionUtils
> EPCIU
;
46 MangleAndInterner Mangle
;
48 RTDyldObjectLinkingLayer ObjectLayer
;
49 IRCompileLayer CompileLayer
;
50 IRTransformLayer OptimizeLayer
;
51 CompileOnDemandLayer CODLayer
;
55 static void handleLazyCallThroughError() {
56 errs() << "LazyCallThrough error: Could not find function body";
61 KaleidoscopeJIT(std::unique_ptr
<ExecutionSession
> ES
,
62 std::unique_ptr
<EPCIndirectionUtils
> EPCIU
,
63 JITTargetMachineBuilder JTMB
, DataLayout DL
)
64 : ES(std::move(ES
)), EPCIU(std::move(EPCIU
)), DL(std::move(DL
)),
65 Mangle(*this->ES
, this->DL
),
66 ObjectLayer(*this->ES
,
67 []() { return std::make_unique
<SectionMemoryManager
>(); }),
68 CompileLayer(*this->ES
, ObjectLayer
,
69 std::make_unique
<ConcurrentIRCompiler
>(std::move(JTMB
))),
70 OptimizeLayer(*this->ES
, CompileLayer
, optimizeModule
),
71 CODLayer(*this->ES
, OptimizeLayer
,
72 this->EPCIU
->getLazyCallThroughManager(),
73 [this] { return this->EPCIU
->createIndirectStubsManager(); }),
74 MainJD(this->ES
->createBareJITDylib("<main>")) {
76 cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(
77 DL
.getGlobalPrefix())));
81 if (auto Err
= ES
->endSession())
82 ES
->reportError(std::move(Err
));
83 if (auto Err
= EPCIU
->cleanup())
84 ES
->reportError(std::move(Err
));
87 static Expected
<std::unique_ptr
<KaleidoscopeJIT
>> Create() {
88 auto EPC
= SelfExecutorProcessControl::Create();
90 return EPC
.takeError();
92 auto ES
= std::make_unique
<ExecutionSession
>(std::move(*EPC
));
94 auto EPCIU
= EPCIndirectionUtils::Create(*ES
);
96 return EPCIU
.takeError();
98 (*EPCIU
)->createLazyCallThroughManager(
99 *ES
, ExecutorAddr::fromPtr(&handleLazyCallThroughError
));
101 if (auto Err
= setUpInProcessLCTMReentryViaEPCIU(**EPCIU
))
102 return std::move(Err
);
104 JITTargetMachineBuilder
JTMB(
105 ES
->getExecutorProcessControl().getTargetTriple());
107 auto DL
= JTMB
.getDefaultDataLayoutForTarget();
109 return DL
.takeError();
111 return std::make_unique
<KaleidoscopeJIT
>(std::move(ES
), std::move(*EPCIU
),
112 std::move(JTMB
), std::move(*DL
));
115 const DataLayout
&getDataLayout() const { return DL
; }
117 JITDylib
&getMainJITDylib() { return MainJD
; }
119 Error
addModule(ThreadSafeModule TSM
, ResourceTrackerSP RT
= nullptr) {
121 RT
= MainJD
.getDefaultResourceTracker();
123 return CODLayer
.add(RT
, std::move(TSM
));
126 Expected
<ExecutorSymbolDef
> lookup(StringRef Name
) {
127 return ES
->lookup({&MainJD
}, Mangle(Name
.str()));
131 static Expected
<ThreadSafeModule
>
132 optimizeModule(ThreadSafeModule TSM
, const MaterializationResponsibility
&R
) {
133 TSM
.withModuleDo([](Module
&M
) {
134 // Create a function pass manager.
135 auto FPM
= std::make_unique
<legacy::FunctionPassManager
>(&M
);
137 // Add some optimizations.
138 FPM
->add(createInstructionCombiningPass());
139 FPM
->add(createReassociatePass());
140 FPM
->add(createGVNPass());
141 FPM
->add(createCFGSimplificationPass());
142 FPM
->doInitialization();
144 // Run the optimizations over all functions in the module being added to
150 return std::move(TSM
);
154 } // end namespace orc
155 } // end namespace llvm
157 #endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H