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/IRCompileLayer.h"
22 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
23 #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
24 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
25 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/LegacyPassManager.h"
29 #include "llvm/Transforms/InstCombine/InstCombine.h"
30 #include "llvm/Transforms/Scalar.h"
31 #include "llvm/Transforms/Scalar/GVN.h"
37 class KaleidoscopeJIT
{
40 RTDyldObjectLinkingLayer ObjectLayer
;
41 IRCompileLayer CompileLayer
;
42 IRTransformLayer OptimizeLayer
;
45 MangleAndInterner Mangle
;
46 ThreadSafeContext Ctx
;
49 KaleidoscopeJIT(JITTargetMachineBuilder JTMB
, DataLayout DL
)
51 []() { return std::make_unique
<SectionMemoryManager
>(); }),
52 CompileLayer(ES
, ObjectLayer
, ConcurrentIRCompiler(std::move(JTMB
))),
53 OptimizeLayer(ES
, CompileLayer
, optimizeModule
),
54 DL(std::move(DL
)), Mangle(ES
, this->DL
),
55 Ctx(std::make_unique
<LLVMContext
>()) {
56 ES
.getMainJITDylib().addGenerator(
57 cantFail(DynamicLibrarySearchGenerator::GetForCurrentProcess(
58 DL
.getGlobalPrefix())));
61 const DataLayout
&getDataLayout() const { return DL
; }
63 LLVMContext
&getContext() { return *Ctx
.getContext(); }
65 static Expected
<std::unique_ptr
<KaleidoscopeJIT
>> Create() {
66 auto JTMB
= JITTargetMachineBuilder::detectHost();
69 return JTMB
.takeError();
71 auto DL
= JTMB
->getDefaultDataLayoutForTarget();
73 return DL
.takeError();
75 return std::make_unique
<KaleidoscopeJIT
>(std::move(*JTMB
), std::move(*DL
));
78 Error
addModule(std::unique_ptr
<Module
> M
) {
79 return OptimizeLayer
.add(ES
.getMainJITDylib(),
80 ThreadSafeModule(std::move(M
), Ctx
));
83 Expected
<JITEvaluatedSymbol
> lookup(StringRef Name
) {
84 return ES
.lookup({&ES
.getMainJITDylib()}, Mangle(Name
.str()));
88 static Expected
<ThreadSafeModule
>
89 optimizeModule(ThreadSafeModule TSM
, const MaterializationResponsibility
&R
) {
90 TSM
.withModuleDo([](Module
&M
) {
91 // Create a function pass manager.
92 auto FPM
= std::make_unique
<legacy::FunctionPassManager
>(&M
);
94 // Add some optimizations.
95 FPM
->add(createInstructionCombiningPass());
96 FPM
->add(createReassociatePass());
97 FPM
->add(createGVNPass());
98 FPM
->add(createCFGSimplificationPass());
99 FPM
->doInitialization();
101 // Run the optimizations over all functions in the module being added to
111 } // end namespace orc
112 } // end namespace llvm
114 #endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H