1 //===--------- LLJIT.cpp - An ORC-based JIT for compiling LLVM IR ---------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
10 #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
11 #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
12 #include "llvm/ExecutionEngine/Orc/OrcError.h"
13 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
14 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
15 #include "llvm/IR/Mangler.h"
16
17 namespace llvm {
18 namespace orc {
19
prepareForConstruction()20 Error LLJITBuilderState::prepareForConstruction() {
21
22 if (!JTMB) {
23 if (auto JTMBOrErr = JITTargetMachineBuilder::detectHost())
24 JTMB = std::move(*JTMBOrErr);
25 else
26 return JTMBOrErr.takeError();
27 }
28
29 // If the client didn't configure any linker options then auto-configure the
30 // JIT linker.
31 if (!CreateObjectLinkingLayer && JTMB->getCodeModel() == None &&
32 JTMB->getRelocationModel() == None) {
33
34 auto &TT = JTMB->getTargetTriple();
35 if (TT.isOSBinFormatMachO() &&
36 (TT.getArch() == Triple::aarch64 || TT.getArch() == Triple::x86_64)) {
37
38 JTMB->setRelocationModel(Reloc::PIC_);
39 JTMB->setCodeModel(CodeModel::Small);
40 CreateObjectLinkingLayer =
41 [](ExecutionSession &ES,
42 const Triple &) -> std::unique_ptr<ObjectLayer> {
43 return std::make_unique<ObjectLinkingLayer>(
44 ES, std::make_unique<jitlink::InProcessMemoryManager>());
45 };
46 }
47 }
48
49 return Error::success();
50 }
51
~LLJIT()52 LLJIT::~LLJIT() {
53 if (CompileThreads)
54 CompileThreads->wait();
55 }
56
defineAbsolute(StringRef Name,JITEvaluatedSymbol Sym)57 Error LLJIT::defineAbsolute(StringRef Name, JITEvaluatedSymbol Sym) {
58 auto InternedName = ES->intern(Name);
59 SymbolMap Symbols({{InternedName, Sym}});
60 return Main.define(absoluteSymbols(std::move(Symbols)));
61 }
62
addIRModule(JITDylib & JD,ThreadSafeModule TSM)63 Error LLJIT::addIRModule(JITDylib &JD, ThreadSafeModule TSM) {
64 assert(TSM && "Can not add null module");
65
66 if (auto Err =
67 TSM.withModuleDo([&](Module &M) { return applyDataLayout(M); }))
68 return Err;
69
70 return CompileLayer->add(JD, std::move(TSM), ES->allocateVModule());
71 }
72
addObjectFile(JITDylib & JD,std::unique_ptr<MemoryBuffer> Obj)73 Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
74 assert(Obj && "Can not add null object");
75
76 return ObjTransformLayer.add(JD, std::move(Obj), ES->allocateVModule());
77 }
78
lookupLinkerMangled(JITDylib & JD,StringRef Name)79 Expected<JITEvaluatedSymbol> LLJIT::lookupLinkerMangled(JITDylib &JD,
80 StringRef Name) {
81 return ES->lookup(
82 makeJITDylibSearchOrder(&JD, JITDylibLookupFlags::MatchAllSymbols),
83 ES->intern(Name));
84 }
85
86 std::unique_ptr<ObjectLayer>
createObjectLinkingLayer(LLJITBuilderState & S,ExecutionSession & ES)87 LLJIT::createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES) {
88
89 // If the config state provided an ObjectLinkingLayer factory then use it.
90 if (S.CreateObjectLinkingLayer)
91 return S.CreateObjectLinkingLayer(ES, S.JTMB->getTargetTriple());
92
93 // Otherwise default to creating an RTDyldObjectLinkingLayer that constructs
94 // a new SectionMemoryManager for each object.
95 auto GetMemMgr = []() { return std::make_unique<SectionMemoryManager>(); };
96 auto ObjLinkingLayer =
97 std::make_unique<RTDyldObjectLinkingLayer>(ES, std::move(GetMemMgr));
98
99 if (S.JTMB->getTargetTriple().isOSBinFormatCOFF()) {
100 ObjLinkingLayer->setOverrideObjectFlagsWithResponsibilityFlags(true);
101 ObjLinkingLayer->setAutoClaimResponsibilityForObjectSymbols(true);
102 }
103
104 // FIXME: Explicit conversion to std::unique_ptr<ObjectLayer> added to silence
105 // errors from some GCC / libstdc++ bots. Remove this conversion (i.e.
106 // just return ObjLinkingLayer) once those bots are upgraded.
107 return std::unique_ptr<ObjectLayer>(std::move(ObjLinkingLayer));
108 }
109
110 Expected<std::unique_ptr<IRCompileLayer::IRCompiler>>
createCompileFunction(LLJITBuilderState & S,JITTargetMachineBuilder JTMB)111 LLJIT::createCompileFunction(LLJITBuilderState &S,
112 JITTargetMachineBuilder JTMB) {
113
114 /// If there is a custom compile function creator set then use it.
115 if (S.CreateCompileFunction)
116 return S.CreateCompileFunction(std::move(JTMB));
117
118 // Otherwise default to creating a SimpleCompiler, or ConcurrentIRCompiler,
119 // depending on the number of threads requested.
120 if (S.NumCompileThreads > 0)
121 return std::make_unique<ConcurrentIRCompiler>(std::move(JTMB));
122
123 auto TM = JTMB.createTargetMachine();
124 if (!TM)
125 return TM.takeError();
126
127 return std::make_unique<TMOwningSimpleCompiler>(std::move(*TM));
128 }
129
LLJIT(LLJITBuilderState & S,Error & Err)130 LLJIT::LLJIT(LLJITBuilderState &S, Error &Err)
131 : ES(S.ES ? std::move(S.ES) : std::make_unique<ExecutionSession>()),
132 Main(this->ES->createJITDylib("<main>")), DL(""),
133 ObjLinkingLayer(createObjectLinkingLayer(S, *ES)),
134 ObjTransformLayer(*this->ES, *ObjLinkingLayer), CtorRunner(Main),
135 DtorRunner(Main) {
136
137 ErrorAsOutParameter _(&Err);
138
139 if (auto DLOrErr = S.JTMB->getDefaultDataLayoutForTarget())
140 DL = std::move(*DLOrErr);
141 else {
142 Err = DLOrErr.takeError();
143 return;
144 }
145
146 {
147 auto CompileFunction = createCompileFunction(S, std::move(*S.JTMB));
148 if (!CompileFunction) {
149 Err = CompileFunction.takeError();
150 return;
151 }
152 CompileLayer = std::make_unique<IRCompileLayer>(
153 *ES, ObjTransformLayer, std::move(*CompileFunction));
154 }
155
156 if (S.NumCompileThreads > 0) {
157 CompileLayer->setCloneToNewContextOnEmit(true);
158 CompileThreads = std::make_unique<ThreadPool>(S.NumCompileThreads);
159 ES->setDispatchMaterialization(
160 [this](JITDylib &JD, std::unique_ptr<MaterializationUnit> MU) {
161 // FIXME: Switch to move capture once we have c++14.
162 auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU));
163 auto Work = [SharedMU, &JD]() { SharedMU->doMaterialize(JD); };
164 CompileThreads->async(std::move(Work));
165 });
166 }
167 }
168
mangle(StringRef UnmangledName)169 std::string LLJIT::mangle(StringRef UnmangledName) {
170 std::string MangledName;
171 {
172 raw_string_ostream MangledNameStream(MangledName);
173 Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL);
174 }
175 return MangledName;
176 }
177
applyDataLayout(Module & M)178 Error LLJIT::applyDataLayout(Module &M) {
179 if (M.getDataLayout().isDefault())
180 M.setDataLayout(DL);
181
182 if (M.getDataLayout() != DL)
183 return make_error<StringError>(
184 "Added modules have incompatible data layouts",
185 inconvertibleErrorCode());
186
187 return Error::success();
188 }
189
recordCtorDtors(Module & M)190 void LLJIT::recordCtorDtors(Module &M) {
191 CtorRunner.add(getConstructors(M));
192 DtorRunner.add(getDestructors(M));
193 }
194
prepareForConstruction()195 Error LLLazyJITBuilderState::prepareForConstruction() {
196 if (auto Err = LLJITBuilderState::prepareForConstruction())
197 return Err;
198 TT = JTMB->getTargetTriple();
199 return Error::success();
200 }
201
addLazyIRModule(JITDylib & JD,ThreadSafeModule TSM)202 Error LLLazyJIT::addLazyIRModule(JITDylib &JD, ThreadSafeModule TSM) {
203 assert(TSM && "Can not add null module");
204
205 if (auto Err = TSM.withModuleDo([&](Module &M) -> Error {
206 if (auto Err = applyDataLayout(M))
207 return Err;
208
209 recordCtorDtors(M);
210 return Error::success();
211 }))
212 return Err;
213
214 return CODLayer->add(JD, std::move(TSM), ES->allocateVModule());
215 }
216
LLLazyJIT(LLLazyJITBuilderState & S,Error & Err)217 LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) {
218
219 // If LLJIT construction failed then bail out.
220 if (Err)
221 return;
222
223 ErrorAsOutParameter _(&Err);
224
225 /// Take/Create the lazy-compile callthrough manager.
226 if (S.LCTMgr)
227 LCTMgr = std::move(S.LCTMgr);
228 else {
229 if (auto LCTMgrOrErr = createLocalLazyCallThroughManager(
230 S.TT, *ES, S.LazyCompileFailureAddr))
231 LCTMgr = std::move(*LCTMgrOrErr);
232 else {
233 Err = LCTMgrOrErr.takeError();
234 return;
235 }
236 }
237
238 // Take/Create the indirect stubs manager builder.
239 auto ISMBuilder = std::move(S.ISMBuilder);
240
241 // If none was provided, try to build one.
242 if (!ISMBuilder)
243 ISMBuilder = createLocalIndirectStubsManagerBuilder(S.TT);
244
245 // No luck. Bail out.
246 if (!ISMBuilder) {
247 Err = make_error<StringError>("Could not construct "
248 "IndirectStubsManagerBuilder for target " +
249 S.TT.str(),
250 inconvertibleErrorCode());
251 return;
252 }
253
254 // Create the transform layer.
255 TransformLayer = std::make_unique<IRTransformLayer>(*ES, *CompileLayer);
256
257 // Create the COD layer.
258 CODLayer = std::make_unique<CompileOnDemandLayer>(
259 *ES, *TransformLayer, *LCTMgr, std::move(ISMBuilder));
260
261 if (S.NumCompileThreads > 0)
262 CODLayer->setCloneToNewContextOnEmit(true);
263 }
264
265 } // End namespace orc.
266 } // End namespace llvm.
267