LLVM  10.0.0svn
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 
13 #include "llvm/IR/Mangler.h"
14 
15 namespace llvm {
16 namespace orc {
17 
19 
20  if (!JTMB) {
21  if (auto JTMBOrErr = JITTargetMachineBuilder::detectHost())
22  JTMB = std::move(*JTMBOrErr);
23  else
24  return JTMBOrErr.takeError();
25  }
26 
27  return Error::success();
28 }
29 
31  if (CompileThreads)
32  CompileThreads->wait();
33 }
34 
36  auto InternedName = ES->intern(Name);
37  SymbolMap Symbols({{InternedName, Sym}});
38  return Main.define(absoluteSymbols(std::move(Symbols)));
39 }
40 
42  assert(TSM && "Can not add null module");
43 
44  if (auto Err = applyDataLayout(*TSM.getModule()))
45  return Err;
46 
47  return CompileLayer->add(JD, std::move(TSM), ES->allocateVModule());
48 }
49 
50 Error LLJIT::addObjectFile(JITDylib &JD, std::unique_ptr<MemoryBuffer> Obj) {
51  assert(Obj && "Can not add null object");
52 
53  return ObjLinkingLayer->add(JD, std::move(Obj), ES->allocateVModule());
54 }
55 
57  StringRef Name) {
58  return ES->lookup(JITDylibSearchList({{&JD, true}}), ES->intern(Name));
59 }
60 
61 std::unique_ptr<ObjectLayer>
63 
64  // If the config state provided an ObjectLinkingLayer factory then use it.
66  return S.CreateObjectLinkingLayer(ES);
67 
68  // Otherwise default to creating an RTDyldObjectLinkingLayer that constructs
69  // a new SectionMemoryManager for each object.
70  auto GetMemMgr = []() { return llvm::make_unique<SectionMemoryManager>(); };
71  return llvm::make_unique<RTDyldObjectLinkingLayer>(ES, std::move(GetMemMgr));
72 }
73 
77 
78  /// If there is a custom compile function creator set then use it.
80  return S.CreateCompileFunction(std::move(JTMB));
81 
82  // Otherwise default to creating a SimpleCompiler, or ConcurrentIRCompiler,
83  // depending on the number of threads requested.
84  if (S.NumCompileThreads > 0)
85  return ConcurrentIRCompiler(std::move(JTMB));
86 
87  auto TM = JTMB.createTargetMachine();
88  if (!TM)
89  return TM.takeError();
90 
91  return TMOwningSimpleCompiler(std::move(*TM));
92 }
93 
95  : ES(S.ES ? std::move(S.ES) : llvm::make_unique<ExecutionSession>()),
96  Main(this->ES->getMainJITDylib()), DL(""), CtorRunner(Main),
97  DtorRunner(Main) {
98 
99  ErrorAsOutParameter _(&Err);
100 
102 
103  if (auto DLOrErr = S.JTMB->getDefaultDataLayoutForTarget())
104  DL = std::move(*DLOrErr);
105  else {
106  Err = DLOrErr.takeError();
107  return;
108  }
109 
110  {
111  auto CompileFunction = createCompileFunction(S, std::move(*S.JTMB));
112  if (!CompileFunction) {
113  Err = CompileFunction.takeError();
114  return;
115  }
116  CompileLayer = llvm::make_unique<IRCompileLayer>(
117  *ES, *ObjLinkingLayer, std::move(*CompileFunction));
118  }
119 
120  if (S.NumCompileThreads > 0) {
121  CompileLayer->setCloneToNewContextOnEmit(true);
122  CompileThreads = llvm::make_unique<ThreadPool>(S.NumCompileThreads);
123  ES->setDispatchMaterialization(
124  [this](JITDylib &JD, std::unique_ptr<MaterializationUnit> MU) {
125  // FIXME: Switch to move capture once we have c++14.
126  auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU));
127  auto Work = [SharedMU, &JD]() { SharedMU->doMaterialize(JD); };
128  CompileThreads->async(std::move(Work));
129  });
130  }
131 }
132 
133 std::string LLJIT::mangle(StringRef UnmangledName) {
134  std::string MangledName;
135  {
136  raw_string_ostream MangledNameStream(MangledName);
137  Mangler::getNameWithPrefix(MangledNameStream, UnmangledName, DL);
138  }
139  return MangledName;
140 }
141 
143  if (M.getDataLayout().isDefault())
144  M.setDataLayout(DL);
145 
146  if (M.getDataLayout() != DL)
147  return make_error<StringError>(
148  "Added modules have incompatible data layouts",
150 
151  return Error::success();
152 }
153 
157 }
158 
161  return Err;
162  TT = JTMB->getTargetTriple();
163  return Error::success();
164 }
165 
167  assert(TSM && "Can not add null module");
168 
169  if (auto Err = applyDataLayout(*TSM.getModule()))
170  return Err;
171 
172  recordCtorDtors(*TSM.getModule());
173 
174  return CODLayer->add(JD, std::move(TSM), ES->allocateVModule());
175 }
176 
177 LLLazyJIT::LLLazyJIT(LLLazyJITBuilderState &S, Error &Err) : LLJIT(S, Err) {
178 
179  // If LLJIT construction failed then bail out.
180  if (Err)
181  return;
182 
183  ErrorAsOutParameter _(&Err);
184 
185  /// Take/Create the lazy-compile callthrough manager.
186  if (S.LCTMgr)
187  LCTMgr = std::move(S.LCTMgr);
188  else {
189  if (auto LCTMgrOrErr = createLocalLazyCallThroughManager(
190  S.TT, *ES, S.LazyCompileFailureAddr))
191  LCTMgr = std::move(*LCTMgrOrErr);
192  else {
193  Err = LCTMgrOrErr.takeError();
194  return;
195  }
196  }
197 
198  // Take/Create the indirect stubs manager builder.
199  auto ISMBuilder = std::move(S.ISMBuilder);
200 
201  // If none was provided, try to build one.
202  if (!ISMBuilder)
204 
205  // No luck. Bail out.
206  if (!ISMBuilder) {
207  Err = make_error<StringError>("Could not construct "
208  "IndirectStubsManagerBuilder for target " +
209  S.TT.str(),
211  return;
212  }
213 
214  // Create the transform layer.
215  TransformLayer = llvm::make_unique<IRTransformLayer>(*ES, *CompileLayer);
216 
217  // Create the COD layer.
218  CODLayer = llvm::make_unique<CompileOnDemandLayer>(
219  *ES, *TransformLayer, *LCTMgr, std::move(ISMBuilder));
220 
221  if (S.NumCompileThreads > 0)
222  CODLayer->setCloneToNewContextOnEmit(true);
223 }
224 
225 } // End namespace orc.
226 } // End namespace llvm.
A SimpleCompiler that owns its TargetMachine.
Definition: CompileUtils.h:60
static Expected< IRCompileLayer::CompileFunction > createCompileFunction(LLJITBuilderState &S, JITTargetMachineBuilder JTMB)
Definition: LLJIT.cpp:75
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:35
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
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:94
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:194
Error addIRModule(JITDylib &JD, ThreadSafeModule TSM)
Adds an IR module to the given JITDylib.
Definition: LLJIT.cpp:41
void setDataLayout(StringRef Desc)
Set the data layout.
Definition: Module.cpp:363
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&... args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
Definition: STLExtras.h:1405
Error addLazyIRModule(JITDylib &JD, ThreadSafeModule M)
Add a module to be lazily compiled to JITDylib JD.
Definition: LLJIT.cpp:166
std::unique_ptr< AbsoluteSymbolsMaterializationUnit > absoluteSymbols(SymbolMap Symbols, VModuleKey K=VModuleKey())
Create an AbsoluteSymbolsMaterializationUnit with the given symbols.
Definition: Core.h:348
Definition: BitVector.h:937
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
A thread-safe version of SimpleCompiler.
Definition: CompileUtils.h:76
std::vector< std::pair< JITDylib *, bool > > JITDylibSearchList
A list of (JITDylib*, bool) pairs.
Definition: Core.h:59
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
void add(iterator_range< CtorDtorIterator > CtorDtors)
CtorDtorRunner DtorRunner
Definition: LLJIT.h:145
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:365
static Expected< JITTargetMachineBuilder > detectHost()
Create a JITTargetMachineBuilder for the host system.
Module * getModule()
Get the module wrapped by this ThreadSafeModule.
std::string mangle(StringRef UnmangledName)
Definition: LLJIT.cpp:133
std::unique_ptr< IRCompileLayer > CompileLayer
Definition: LLJIT.h:143
CompileFunctionCreator CreateCompileFunction
Definition: LLJIT.h:197
An LLVM Module together with a shared ThreadSafeContext.
CtorDtorRunner CtorRunner
Definition: LLJIT.h:145
ObjectLinkingLayerCreator CreateObjectLinkingLayer
Definition: LLJIT.h:196
std::unique_ptr< ThreadPool > CompileThreads
Definition: LLJIT.h:140
std::unique_ptr< ExecutionSession > ES
Definition: LLJIT.h:136
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
void recordCtorDtors(Module &M)
Definition: LLJIT.cpp:154
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:753
std::unique_ptr< ObjectLayer > ObjLinkingLayer
Definition: LLJIT.h:142
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:189
Helper for Errors used as out-parameters.
Definition: Error.h:1021
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:56
Error addObjectFile(JITDylib &JD, std::unique_ptr< MemoryBuffer > Obj)
Adds an object file to the given JITDylib.
Definition: LLJIT.cpp:50
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:142
~LLJIT()
Destruct this instance.
Definition: LLJIT.cpp:30
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::unique_ptr< LazyCallThroughManager > LCTMgr
Definition: LLJIT.h:287
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:482
Error prepareForConstruction()
Called prior to JIT class construcion to fix up defaults.
Definition: LLJIT.cpp:18
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
JITTargetAddress LazyCompileFailureAddr
Definition: LLJIT.h:286
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:139
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
static std::unique_ptr< ObjectLayer > createObjectLinkingLayer(LLJITBuilderState &S, ExecutionSession &ES)
Definition: LLJIT.cpp:62
Optional< JITTargetMachineBuilder > JTMB
Definition: LLJIT.h:195
IndirectStubsManagerBuilderFunction ISMBuilder
Definition: LLJIT.h:288
#define _
A symbol table that supports asynchoronous symbol queries.
Definition: Core.h:500
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.