LLVM  10.0.0
LLJIT.cpp
Go to the documentation of this file.
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 
15 #include "llvm/IR/Mangler.h"
16 
17 namespace llvm {
18 namespace orc {
19 
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);
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 
53  if (CompileThreads)
54  CompileThreads->wait();
55 }
56 
58  auto InternedName = ES->intern(Name);
59  SymbolMap Symbols({{InternedName, Sym}});
60  return Main.define(absoluteSymbols(std::move(Symbols)));
61 }
62 
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 
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 
80  StringRef Name) {
81  return ES->lookup(
83  ES->intern(Name));
84 }
85 
86 std::unique_ptr<ObjectLayer>
88 
89  // If the config state provided an ObjectLinkingLayer factory then use it.
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 
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 
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 
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 
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",
186 
187  return Error::success();
188 }
189 
193 }
194 
197  return Err;
198  TT = JTMB->getTargetTriple();
199  return Error::success();
200 }
201 
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 
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)
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(),
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.
const NoneType None
Definition: None.h:23
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::function< std::unique_ptr< IndirectStubsManager >)> createLocalIndirectStubsManagerBuilder(const Triple &T)
Create a local indriect stubs manager builder.
Error defineAbsolute(StringRef Name, JITEvaluatedSymbol Address)
Convenience method for defining an absolute symbol.
Definition: LLJIT.cpp:57
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
LLJIT(LLJITBuilderState &S, Error &Err)
Create an LLJIT instance with a single compile thread.
Definition: LLJIT.cpp:130
auto withModuleDo(Func &&F) -> decltype(F(std::declval< Module &>()))
Locks the associated ThreadSafeContext and calls the given function on the contained Module...
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:198
Error addIRModule(JITDylib &JD, ThreadSafeModule TSM)
Adds an IR module to the given JITDylib.
Definition: LLJIT.cpp:63
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:363
Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M)
Add a module to be lazily compiled to JITDylib JD.
Definition: LLJIT.cpp:202
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols, VModuleKey K=VModuleKey())
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
Definition: Core.h:630
Definition: BitVector.h:937
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
void add(iterator_range< CtorDtorIterator > CtorDtors)
CtorDtorRunner DtorRunner
Definition: LLJIT.h:149
iterator_range< CtorDtorIterator > getDestructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
const std::string & str() const
Definition: Triple.h:366
static Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
static Expected< std::unique_ptr< IRCompileLayer::IRCompiler > > createCompileFunction(LLJITBuilderState &S, JITTargetMachineBuilder JTMB)
Definition: LLJIT.cpp:111
std::string mangle(StringRef UnmangledName)
Definition: LLJIT.cpp:169
std::unique_ptr< IRCompileLayer > CompileLayer
Definition: LLJIT.h:147
CompileFunctionCreator CreateCompileFunction
Definition: LLJIT.h:201
An LLVM Module together with a shared ThreadSafeContext.
CtorDtorRunner CtorRunner
Definition: LLJIT.h:149
JITDylibSearchOrder makeJITDylibSearchOrder(ArrayRef< JITDylib *> JDs, JITDylibLookupFlags Flags=JITDylibLookupFlags::MatchExportedSymbolsOnly)
Convenience function for creating a search order from an ArrayRef of JITDylib*, all with the same fla...
Definition: Core.h:98
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
ObjectLinkingLayerCreator CreateObjectLinkingLayer
Definition: LLJIT.h:200
std::unique_ptr< ThreadPool > CompileThreads
Definition: LLJIT.h:143
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:139
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
void recordCtorDtors(Module &M)
Definition: LLJIT.cpp:190
Expected< std::unique_ptr< LazyCallThroughManager > > createLocalLazyCallThroughManager(const Triple &T, ExecutionSession &ES, JITTargetAddress ErrorHandlerAddr)
Create a LocalLazyCallThroughManager from the given triple and execution session. ...
bool isDefault() const
Test if the DataLayout was constructed from an empty string.
Definition: DataLayout.h:245
An ExecutionSession represents a running JIT program.
Definition: Core.h:1058
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:208
Helper for Errors used as out-parameters.
Definition: Error.h:1055
Expected< std::unique_ptr< TargetMachine > > createTargetMachine()
Create a TargetMachine.
Expected< JITEvaluatedSymbol > lookupLinkerMangled(JITDylib &JD, StringRef Name)
Look up a symbol in JITDylib JD by the symbol&#39;s linker-mangled name (to look up symbols based on thei...
Definition: LLJIT.cpp:79
Error addObjectFile(JITDylib &JD, std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.cpp:73
iterator_range< CtorDtorIterator > getConstructors(const Module &M)
Create an iterator range over the entries of the llvm.global_ctors array.
Error applyDataLayout(Module &M)
Definition: LLJIT.cpp:178
~LLJIT()
Destruct this instance.
Definition: LLJIT.cpp:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::unique_ptr< LazyCallThroughManager > LCTMgr
Definition: LLJIT.h:291
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:516
Error prepareForConstruction()
Called prior to JIT class construcion to fix up defaults.
Definition: LLJIT.cpp:20
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
JITTargetAddress LazyCompileFailureAddr
Definition: LLJIT.h:290
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable&#39;s name.
Definition: Mangler.cpp:111
DataLayout DL
Definition: LLJIT.h:142
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
static std::unique_ptr< ObjectLayer > createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES)
Definition: LLJIT.cpp:87
Optional< JITTargetMachineBuilder > JTMB
Definition: LLJIT.h:199
IndirectStubsManagerBuilderFunction ISMBuilder
Definition: LLJIT.h:292
ObjectTransformLayer ObjTransformLayer
Definition: LLJIT.h:146
#define _
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:774
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77
A utility class for building TargetMachines for JITs.