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/STLExtras.h"
17 #include "llvm/ExecutionEngine/ExecutionEngine.h"
18 #include "llvm/ExecutionEngine/JITSymbol.h"
19 #include "llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h"
20 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
21 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
22 #include "llvm/ExecutionEngine/Orc/IRTransformLayer.h"
23 #include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
24 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
25 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
26 #include "llvm/ExecutionEngine/RuntimeDyld.h"
27 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/LegacyPassManager.h"
30 #include "llvm/IR/Mangler.h"
31 #include "llvm/Support/DynamicLibrary.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/Transforms/InstCombine/InstCombine.h"
35 #include "llvm/Transforms/Scalar.h"
36 #include "llvm/Transforms/Scalar/GVN.h"
47 class KaleidoscopeJIT
{
50 std::map
<VModuleKey
, std::shared_ptr
<SymbolResolver
>> Resolvers
;
51 std::unique_ptr
<TargetMachine
> TM
;
53 LegacyRTDyldObjectLinkingLayer ObjectLayer
;
54 LegacyIRCompileLayer
<decltype(ObjectLayer
), SimpleCompiler
> CompileLayer
;
56 using OptimizeFunction
=
57 std::function
<std::unique_ptr
<Module
>(std::unique_ptr
<Module
>)>;
59 LegacyIRTransformLayer
<decltype(CompileLayer
), OptimizeFunction
> OptimizeLayer
;
61 std::unique_ptr
<JITCompileCallbackManager
> CompileCallbackManager
;
62 LegacyCompileOnDemandLayer
<decltype(OptimizeLayer
)> CODLayer
;
66 : TM(EngineBuilder().selectTarget()), DL(TM
->createDataLayout()),
67 ObjectLayer(AcknowledgeORCv1Deprecation
, ES
,
68 [this](VModuleKey K
) {
69 return LegacyRTDyldObjectLinkingLayer::Resources
{
70 std::make_shared
<SectionMemoryManager
>(),
73 CompileLayer(AcknowledgeORCv1Deprecation
, ObjectLayer
,
75 OptimizeLayer(AcknowledgeORCv1Deprecation
, CompileLayer
,
76 [this](std::unique_ptr
<Module
> M
) {
77 return optimizeModule(std::move(M
));
79 CompileCallbackManager(cantFail(orc::createLocalCompileCallbackManager(
80 TM
->getTargetTriple(), ES
, 0))),
82 AcknowledgeORCv1Deprecation
, ES
, OptimizeLayer
,
83 [&](orc::VModuleKey K
) { return Resolvers
[K
]; },
84 [&](orc::VModuleKey K
, std::shared_ptr
<SymbolResolver
> R
) {
85 Resolvers
[K
] = std::move(R
);
87 [](Function
&F
) { return std::set
<Function
*>({&F
}); },
88 *CompileCallbackManager
,
89 orc::createLocalIndirectStubsManagerBuilder(
90 TM
->getTargetTriple())) {
91 llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);
94 TargetMachine
&getTargetMachine() { return *TM
; }
96 VModuleKey
addModule(std::unique_ptr
<Module
> M
) {
97 // Create a new VModuleKey.
98 VModuleKey K
= ES
.allocateVModule();
100 // Build a resolver and associate it with the new key.
101 Resolvers
[K
] = createLegacyLookupResolver(
103 [this](const std::string
&Name
) -> JITSymbol
{
104 if (auto Sym
= CompileLayer
.findSymbol(Name
, false))
106 else if (auto Err
= Sym
.takeError())
107 return std::move(Err
);
109 RTDyldMemoryManager::getSymbolAddressInProcess(Name
))
110 return JITSymbol(SymAddr
, JITSymbolFlags::Exported
);
113 [](Error Err
) { cantFail(std::move(Err
), "lookupFlags failed"); });
115 // Add the module to the JIT with the new key.
116 cantFail(CODLayer
.addModule(K
, std::move(M
)));
120 JITSymbol
findSymbol(const std::string Name
) {
121 std::string MangledName
;
122 raw_string_ostream
MangledNameStream(MangledName
);
123 Mangler::getNameWithPrefix(MangledNameStream
, Name
, DL
);
124 return CODLayer
.findSymbol(MangledNameStream
.str(), true);
127 void removeModule(VModuleKey K
) {
128 cantFail(CODLayer
.removeModule(K
));
132 std::unique_ptr
<Module
> optimizeModule(std::unique_ptr
<Module
> M
) {
133 // Create a function pass manager.
134 auto FPM
= std::make_unique
<legacy::FunctionPassManager
>(M
.get());
136 // Add some optimizations.
137 FPM
->add(createInstructionCombiningPass());
138 FPM
->add(createReassociatePass());
139 FPM
->add(createGVNPass());
140 FPM
->add(createCFGSimplificationPass());
141 FPM
->doInitialization();
143 // Run the optimizations over all functions in the module being added to
152 } // end namespace orc
153 } // end namespace llvm
155 #endif // LLVM_EXECUTIONENGINE_ORC_KALEIDOSCOPEJIT_H