LLVM  7.0.0svn
OrcMCJITReplacement.h
Go to the documentation of this file.
1 //===- OrcMCJITReplacement.h - Orc based MCJIT replacement ------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Orc based MCJIT replacement.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H
15 #define LLVM_LIB_EXECUTIONENGINE_ORC_ORCMCJITREPLACEMENT_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringRef.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/Mangler.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/Object/Archive.h"
34 #include "llvm/Object/Binary.h"
35 #include "llvm/Object/ObjectFile.h"
36 #include "llvm/Support/Error.h"
40 #include <algorithm>
41 #include <cassert>
42 #include <cstddef>
43 #include <cstdint>
44 #include <map>
45 #include <memory>
46 #include <set>
47 #include <string>
48 #include <vector>
49 
50 namespace llvm {
51 
52 class ObjectCache;
53 
54 namespace orc {
55 
57  // OrcMCJITReplacement needs to do a little extra book-keeping to ensure that
58  // Orc's automatic finalization doesn't kick in earlier than MCJIT clients are
59  // expecting - see finalizeMemory.
60  class MCJITReplacementMemMgr : public MCJITMemoryManager {
61  public:
62  MCJITReplacementMemMgr(OrcMCJITReplacement &M,
63  std::shared_ptr<MCJITMemoryManager> ClientMM)
64  : M(M), ClientMM(std::move(ClientMM)) {}
65 
66  uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
67  unsigned SectionID,
68  StringRef SectionName) override {
69  uint8_t *Addr =
70  ClientMM->allocateCodeSection(Size, Alignment, SectionID,
71  SectionName);
72  M.SectionsAllocatedSinceLastLoad.insert(Addr);
73  return Addr;
74  }
75 
76  uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
77  unsigned SectionID, StringRef SectionName,
78  bool IsReadOnly) override {
79  uint8_t *Addr = ClientMM->allocateDataSection(Size, Alignment, SectionID,
80  SectionName, IsReadOnly);
81  M.SectionsAllocatedSinceLastLoad.insert(Addr);
82  return Addr;
83  }
84 
85  void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
86  uintptr_t RODataSize, uint32_t RODataAlign,
87  uintptr_t RWDataSize,
88  uint32_t RWDataAlign) override {
89  return ClientMM->reserveAllocationSpace(CodeSize, CodeAlign,
90  RODataSize, RODataAlign,
91  RWDataSize, RWDataAlign);
92  }
93 
94  bool needsToReserveAllocationSpace() override {
95  return ClientMM->needsToReserveAllocationSpace();
96  }
97 
98  void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
99  size_t Size) override {
100  return ClientMM->registerEHFrames(Addr, LoadAddr, Size);
101  }
102 
103  void deregisterEHFrames() override {
104  return ClientMM->deregisterEHFrames();
105  }
106 
107  void notifyObjectLoaded(RuntimeDyld &RTDyld,
108  const object::ObjectFile &O) override {
109  return ClientMM->notifyObjectLoaded(RTDyld, O);
110  }
111 
112  void notifyObjectLoaded(ExecutionEngine *EE,
113  const object::ObjectFile &O) override {
114  return ClientMM->notifyObjectLoaded(EE, O);
115  }
116 
117  bool finalizeMemory(std::string *ErrMsg = nullptr) override {
118  // Each set of objects loaded will be finalized exactly once, but since
119  // symbol lookup during relocation may recursively trigger the
120  // loading/relocation of other modules, and since we're forwarding all
121  // finalizeMemory calls to a single underlying memory manager, we need to
122  // defer forwarding the call on until all necessary objects have been
123  // loaded. Otherwise, during the relocation of a leaf object, we will end
124  // up finalizing memory, causing a crash further up the stack when we
125  // attempt to apply relocations to finalized memory.
126  // To avoid finalizing too early, look at how many objects have been
127  // loaded but not yet finalized. This is a bit of a hack that relies on
128  // the fact that we're lazily emitting object files: The only way you can
129  // get more than one set of objects loaded but not yet finalized is if
130  // they were loaded during relocation of another set.
131  if (M.UnfinalizedSections.size() == 1)
132  return ClientMM->finalizeMemory(ErrMsg);
133  return false;
134  }
135 
136  private:
138  std::shared_ptr<MCJITMemoryManager> ClientMM;
139  };
140 
141  class LinkingResolver : public LegacyJITSymbolResolver {
142  public:
143  LinkingResolver(OrcMCJITReplacement &M) : M(M) {}
144 
145  JITSymbol findSymbol(const std::string &Name) override {
146  return M.ClientResolver->findSymbol(Name);
147  }
148 
149  JITSymbol findSymbolInLogicalDylib(const std::string &Name) override {
150  if (auto Sym = M.findMangledSymbol(Name))
151  return Sym;
152  return M.ClientResolver->findSymbolInLogicalDylib(Name);
153  }
154 
155  private:
157  };
158 
159 private:
160  static ExecutionEngine *
161  createOrcMCJITReplacement(std::string *ErrorMsg,
162  std::shared_ptr<MCJITMemoryManager> MemMgr,
163  std::shared_ptr<LegacyJITSymbolResolver> Resolver,
164  std::unique_ptr<TargetMachine> TM) {
165  return new OrcMCJITReplacement(std::move(MemMgr), std::move(Resolver),
166  std::move(TM));
167  }
168 
169 public:
170  OrcMCJITReplacement(std::shared_ptr<MCJITMemoryManager> MemMgr,
171  std::shared_ptr<LegacyJITSymbolResolver> ClientResolver,
172  std::unique_ptr<TargetMachine> TM)
173  : ExecutionEngine(TM->createDataLayout()), TM(std::move(TM)),
174  MemMgr(
175  std::make_shared<MCJITReplacementMemMgr>(*this, std::move(MemMgr))),
176  Resolver(std::make_shared<LinkingResolver>(*this)),
177  ClientResolver(std::move(ClientResolver)), NotifyObjectLoaded(*this),
178  NotifyFinalized(*this),
179  ObjectLayer([this]() { return this->MemMgr; }, NotifyObjectLoaded,
180  NotifyFinalized),
181  CompileLayer(ObjectLayer, SimpleCompiler(*this->TM)),
182  LazyEmitLayer(CompileLayer) {}
183 
184  static void Register() {
185  OrcMCJITReplacementCtor = createOrcMCJITReplacement;
186  }
187 
188  void addModule(std::unique_ptr<Module> M) override {
189  // If this module doesn't have a DataLayout attached then attach the
190  // default.
191  if (M->getDataLayout().isDefault()) {
192  M->setDataLayout(getDataLayout());
193  } else {
194  assert(M->getDataLayout() == getDataLayout() && "DataLayout Mismatch");
195  }
196  auto *MPtr = M.release();
197  ShouldDelete[MPtr] = true;
198  auto Deleter = [this](Module *Mod) {
199  auto I = ShouldDelete.find(Mod);
200  if (I != ShouldDelete.end() && I->second)
201  delete Mod;
202  };
203  LocalModules.push_back(std::shared_ptr<Module>(MPtr, std::move(Deleter)));
204  cantFail(LazyEmitLayer.addModule(LocalModules.back(), Resolver));
205  }
206 
207  void addObjectFile(std::unique_ptr<object::ObjectFile> O) override {
208  auto Obj =
209  std::make_shared<object::OwningBinary<object::ObjectFile>>(std::move(O),
210  nullptr);
211  cantFail(ObjectLayer.addObject(std::move(Obj), Resolver));
212  }
213 
215  auto Obj =
216  std::make_shared<object::OwningBinary<object::ObjectFile>>(std::move(O));
217  cantFail(ObjectLayer.addObject(std::move(Obj), Resolver));
218  }
219 
221  Archives.push_back(std::move(A));
222  }
223 
224  bool removeModule(Module *M) override {
225  for (auto I = LocalModules.begin(), E = LocalModules.end(); I != E; ++I) {
226  if (I->get() == M) {
227  ShouldDelete[M] = false;
228  LocalModules.erase(I);
229  return true;
230  }
231  }
232  return false;
233  }
234 
236  return cantFail(findSymbol(Name).getAddress());
237  }
238 
240  return findMangledSymbol(Mangle(Name));
241  }
242 
243  void finalizeObject() override {
244  // This is deprecated - Aim to remove in ExecutionEngine.
245  // REMOVE IF POSSIBLE - Doesn't make sense for New JIT.
246  }
247 
248  void mapSectionAddress(const void *LocalAddress,
249  uint64_t TargetAddress) override {
250  for (auto &P : UnfinalizedSections)
251  if (P.second.count(LocalAddress))
252  ObjectLayer.mapSectionAddress(P.first, LocalAddress, TargetAddress);
253  }
254 
255  uint64_t getGlobalValueAddress(const std::string &Name) override {
256  return getSymbolAddress(Name);
257  }
258 
259  uint64_t getFunctionAddress(const std::string &Name) override {
260  return getSymbolAddress(Name);
261  }
262 
263  void *getPointerToFunction(Function *F) override {
264  uint64_t FAddr = getSymbolAddress(F->getName());
265  return reinterpret_cast<void *>(static_cast<uintptr_t>(FAddr));
266  }
267 
269  bool AbortOnFailure = true) override {
270  uint64_t Addr = getSymbolAddress(Name);
271  if (!Addr && AbortOnFailure)
272  llvm_unreachable("Missing symbol!");
273  return reinterpret_cast<void *>(static_cast<uintptr_t>(Addr));
274  }
275 
277  ArrayRef<GenericValue> ArgValues) override;
278 
279  void setObjectCache(ObjectCache *NewCache) override {
280  CompileLayer.getCompiler().setObjectCache(NewCache);
281  }
282 
283  void setProcessAllSections(bool ProcessAllSections) override {
284  ObjectLayer.setProcessAllSections(ProcessAllSections);
285  }
286 
287  void runStaticConstructorsDestructors(bool isDtors) override;
288 
289 private:
290  JITSymbol findMangledSymbol(StringRef Name) {
291  if (auto Sym = LazyEmitLayer.findSymbol(Name, false))
292  return Sym;
293  if (auto Sym = ClientResolver->findSymbol(Name))
294  return Sym;
295  if (auto Sym = scanArchives(Name))
296  return Sym;
297 
298  return nullptr;
299  }
300 
301  JITSymbol scanArchives(StringRef Name) {
302  for (object::OwningBinary<object::Archive> &OB : Archives) {
303  object::Archive *A = OB.getBinary();
304  // Look for our symbols in each Archive
305  auto OptionalChildOrErr = A->findSym(Name);
306  if (!OptionalChildOrErr)
307  report_fatal_error(OptionalChildOrErr.takeError());
308  auto &OptionalChild = *OptionalChildOrErr;
309  if (OptionalChild) {
310  // FIXME: Support nested archives?
312  OptionalChild->getAsBinary();
313  if (!ChildBinOrErr) {
314  // TODO: Actually report errors helpfully.
315  consumeError(ChildBinOrErr.takeError());
316  continue;
317  }
318  std::unique_ptr<object::Binary> &ChildBin = ChildBinOrErr.get();
319  if (ChildBin->isObject()) {
320  std::unique_ptr<object::ObjectFile> ChildObj(
321  static_cast<object::ObjectFile*>(ChildBinOrErr->release()));
322  auto Obj =
323  std::make_shared<object::OwningBinary<object::ObjectFile>>(
324  std::move(ChildObj), nullptr);
325  cantFail(ObjectLayer.addObject(std::move(Obj), Resolver));
326  if (auto Sym = ObjectLayer.findSymbol(Name, true))
327  return Sym;
328  }
329  }
330  }
331  return nullptr;
332  }
333 
334  class NotifyObjectLoadedT {
335  public:
336  using LoadedObjInfoListT =
337  std::vector<std::unique_ptr<RuntimeDyld::LoadedObjectInfo>>;
338 
339  NotifyObjectLoadedT(OrcMCJITReplacement &M) : M(M) {}
340 
343  const RuntimeDyld::LoadedObjectInfo &Info) const {
344  M.UnfinalizedSections[H] = std::move(M.SectionsAllocatedSinceLastLoad);
345  M.SectionsAllocatedSinceLastLoad = SectionAddrSet();
346  M.MemMgr->notifyObjectLoaded(&M, *Obj->getBinary());
347  }
348  private:
350  };
351 
352  class NotifyFinalizedT {
353  public:
354  NotifyFinalizedT(OrcMCJITReplacement &M) : M(M) {}
355 
357  M.UnfinalizedSections.erase(H);
358  }
359 
360  private:
362  };
363 
364  std::string Mangle(StringRef Name) {
365  std::string MangledName;
366  {
367  raw_string_ostream MangledNameStream(MangledName);
368  Mang.getNameWithPrefix(MangledNameStream, Name, getDataLayout());
369  }
370  return MangledName;
371  }
372 
376 
377  std::unique_ptr<TargetMachine> TM;
378  std::shared_ptr<MCJITReplacementMemMgr> MemMgr;
379  std::shared_ptr<LinkingResolver> Resolver;
380  std::shared_ptr<LegacyJITSymbolResolver> ClientResolver;
381  Mangler Mang;
382 
383  // IMPORTANT: ShouldDelete *must* come before LocalModules: The shared_ptr
384  // delete blocks in LocalModules refer to the ShouldDelete map, so
385  // LocalModules needs to be destructed before ShouldDelete.
386  std::map<Module*, bool> ShouldDelete;
387  std::vector<std::shared_ptr<Module>> LocalModules;
388 
389  NotifyObjectLoadedT NotifyObjectLoaded;
390  NotifyFinalizedT NotifyFinalized;
391 
392  ObjectLayerT ObjectLayer;
393  CompileLayerT CompileLayer;
394  LazyEmitLayerT LazyEmitLayer;
395 
396  // We need to store ObjLayerT::ObjSetHandles for each of the object sets
397  // that have been emitted but not yet finalized so that we can forward the
398  // mapSectionAddress calls appropriately.
399  using SectionAddrSet = std::set<const void *>;
400  struct ObjHandleCompare {
401  bool operator()(ObjectLayerT::ObjHandleT H1,
402  ObjectLayerT::ObjHandleT H2) const {
403  return &*H1 < &*H2;
404  }
405  };
406  SectionAddrSet SectionsAllocatedSinceLastLoad;
407  std::map<ObjectLayerT::ObjHandleT, SectionAddrSet, ObjHandleCompare>
408  UnfinalizedSections;
409 
410  std::vector<object::OwningBinary<object::Archive>> Archives;
411 };
412 
413 } // end namespace orc
414 
415 } // end namespace llvm
416 
417 #endif // LLVM_LIB_EXECUTIONENGINE_ORC_MCJITREPLACEMENT_H
void addObjectFile(std::unique_ptr< object::ObjectFile > O) override
addObjectFile - Add an ObjectFile to the execution engine.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:688
Information about the loaded object.
Definition: RuntimeDyld.h:69
Represents a symbol in the JIT.
Definition: JITSymbol.h:180
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:115
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
void finalizeObject() override
finalizeObject - ensure the module is fully processed and is usable.
Expected< Optional< Child > > findSym(StringRef name) const
Definition: Archive.cpp:974
Bare bones object linking layer.
Legacy symbol resolution interface.
Definition: JITSymbol.h:312
This class is the base class for all object file types.
Definition: ObjectFile.h:190
std::shared_ptr< object::OwningBinary< object::ObjectFile > > ObjectPtr
F(f)
const DataLayout & getDataLayout() const
Error takeError()
Take ownership of the stored error.
Definition: Error.h:537
LinkedObjectListT::iterator ObjHandleT
Handle to a loaded object.
Definition: BitVector.h:920
uint64_t getFunctionAddress(const std::string &Name) override
getFunctionAddress - Return the address of the specified function.
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
Simple compile functor: Takes a single IR module and returns an ObjectFile.
Definition: CompileUtils.h:40
OrcMCJITReplacement(std::shared_ptr< MCJITMemoryManager > MemMgr, std::shared_ptr< LegacyJITSymbolResolver > ClientResolver, std::unique_ptr< TargetMachine > TM)
bool removeModule(Module *M) override
removeModule - Removes a Module from the list of modules, but does not free the module&#39;s memory...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
GenericValue runFunction(Function *F, ArrayRef< GenericValue > ArgValues) override
runFunction - Execute the specified function with the specified arguments, and return the result...
uint64_t getGlobalValueAddress(const std::string &Name) override
getGlobalValueAddress - Return the address of the specified global value.
#define P(N)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void addObjectFile(object::OwningBinary< object::ObjectFile > O) override
#define H(x, y, z)
Definition: MD5.cpp:57
void setObjectCache(ObjectCache *NewCache) override
Sets the pre-compiled object cache.
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:962
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void * getPointerToFunction(Function *F) override
getPointerToFunction - The different EE&#39;s represent function bodies in different ways.
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
void runStaticConstructorsDestructors(bool isDtors) override
runStaticConstructorsDestructors - This method is used to execute all of the static constructors or d...
Module.h This file contains the declarations for the Module class.
JITSymbol findSymbol(StringRef Name)
uint64_t getSymbolAddress(StringRef Name)
reference get()
Returns a reference to the stored T value.
Definition: Error.h:517
static ExecutionEngine *(* OrcMCJITReplacementCtor)(std::string *ErrorStr, std::shared_ptr< MCJITMemoryManager > MM, std::shared_ptr< LegacyJITSymbolResolver > SR, std::unique_ptr< TargetMachine > TM)
The access may modify the value stored in memory.
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:224
#define I(x, y, z)
Definition: MD5.cpp:58
void setProcessAllSections(bool ProcessAllSections) override
setProcessAllSections (MCJIT Only): By default, only sections that are "required for execution" are p...
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:23
void addModule(std::unique_ptr< Module > M) override
Add a Module to the list of modules that we can JIT from.
void * getPointerToNamedFunction(StringRef Name, bool AbortOnFailure=true) override
getPointerToNamedFunction - This method returns the address of the specified function by using the dl...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:462
void addArchive(object::OwningBinary< object::Archive > A) override
addArchive - Add an Archive to the execution engine.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) override
mapSectionAddress - map a section to its target address space value.