LLVM  3.7.0
IRCompileLayer.h
Go to the documentation of this file.
1 //===------ IRCompileLayer.h -- Eagerly compile IR for JIT ------*- 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 // Contains the definition for a basic, eagerly compiling layer of the JIT.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H
16 
17 #include "JITSymbol.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include <memory>
22 
23 namespace llvm {
24 namespace orc {
25 
26 /// @brief Eager IR compiling layer.
27 ///
28 /// This layer accepts sets of LLVM IR Modules (via addModuleSet). It
29 /// immediately compiles each IR module to an object file (each IR Module is
30 /// compiled separately). The resulting set of object files is then added to
31 /// the layer below, which must implement the object layer concept.
32 template <typename BaseLayerT> class IRCompileLayer {
33 public:
34  typedef std::function<object::OwningBinary<object::ObjectFile>(Module &)>
36 
37 private:
38  typedef typename BaseLayerT::ObjSetHandleT ObjSetHandleT;
39 
40  typedef std::vector<std::unique_ptr<object::ObjectFile>> OwningObjectVec;
41  typedef std::vector<std::unique_ptr<MemoryBuffer>> OwningBufferVec;
42 
43 public:
44  /// @brief Handle to a set of compiled modules.
45  typedef ObjSetHandleT ModuleSetHandleT;
46 
47  /// @brief Construct an IRCompileLayer with the given BaseLayer, which must
48  /// implement the ObjectLayer concept.
49  IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile)
50  : BaseLayer(BaseLayer), Compile(std::move(Compile)), ObjCache(nullptr) {}
51 
52  /// @brief Set an ObjectCache to query before compiling.
53  void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
54 
55  /// @brief Compile each module in the given module set, then add the resulting
56  /// set of objects to the base layer along with the memory manager and
57  /// symbol resolver.
58  ///
59  /// @return A handle for the added modules.
60  template <typename ModuleSetT, typename MemoryManagerPtrT,
61  typename SymbolResolverPtrT>
63  MemoryManagerPtrT MemMgr,
64  SymbolResolverPtrT Resolver) {
65  OwningObjectVec Objects;
66  OwningBufferVec Buffers;
67 
68  for (const auto &M : Ms) {
69  std::unique_ptr<object::ObjectFile> Object;
70  std::unique_ptr<MemoryBuffer> Buffer;
71 
72  if (ObjCache)
73  std::tie(Object, Buffer) = tryToLoadFromObjectCache(*M).takeBinary();
74 
75  if (!Object) {
76  std::tie(Object, Buffer) = Compile(*M).takeBinary();
77  if (ObjCache)
78  ObjCache->notifyObjectCompiled(&*M, Buffer->getMemBufferRef());
79  }
80 
81  Objects.push_back(std::move(Object));
82  Buffers.push_back(std::move(Buffer));
83  }
84 
86  BaseLayer.addObjectSet(Objects, std::move(MemMgr), std::move(Resolver));
87 
88  BaseLayer.takeOwnershipOfBuffers(H, std::move(Buffers));
89 
90  return H;
91  }
92 
93  /// @brief Remove the module set associated with the handle H.
94  void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeObjectSet(H); }
95 
96  /// @brief Search for the given named symbol.
97  /// @param Name The name of the symbol to search for.
98  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
99  /// @return A handle for the given named symbol, if it exists.
100  JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
101  return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
102  }
103 
104  /// @brief Get the address of the given symbol in the context of the set of
105  /// compiled modules represented by the handle H. This call is
106  /// forwarded to the base layer's implementation.
107  /// @param H The handle for the module set to search in.
108  /// @param Name The name of the symbol to search for.
109  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
110  /// @return A handle for the given named symbol, if it is found in the
111  /// given module set.
113  bool ExportedSymbolsOnly) {
114  return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
115  }
116 
117  /// @brief Immediately emit and finalize the moduleOB set represented by the
118  /// given handle.
119  /// @param H Handle for module set to emit/finalize.
121  BaseLayer.emitAndFinalize(H);
122  }
123 
124 private:
126  tryToLoadFromObjectCache(const Module &M) {
127  std::unique_ptr<MemoryBuffer> ObjBuffer = ObjCache->getObject(&M);
128  if (!ObjBuffer)
130 
132  object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
133  if (!Obj)
135 
136  return object::OwningBinary<object::ObjectFile>(std::move(*Obj),
137  std::move(ObjBuffer));
138  }
139 
140  BaseLayerT &BaseLayer;
141  CompileFtor Compile;
142  ObjectCache *ObjCache;
143 };
144 
145 } // End namespace orc.
146 } // End namespace llvm.
147 
148 #endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILINGLAYER_H
Represents either an error or a value T.
Definition: ErrorOr.h:82
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
virtual std::unique_ptr< MemoryBuffer > getObject(const Module *M)=0
Returns a pointer to a newly allocated MemoryBuffer that contains the object which corresponds with M...
ObjSetHandleT ModuleSetHandleT
Handle to a set of compiled modules.
IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile)
Construct an IRCompileLayer with the given BaseLayer, which must implement the ObjectLayer concept...
void setObjectCache(ObjectCache *NewCache)
Set an ObjectCache to query before compiling.
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
Eager IR compiling layer.
#define H(x, y, z)
Definition: MD5.cpp:53
Represents a symbol in the JIT.
Definition: JITSymbol.h:29
std::function< object::OwningBinary< object::ObjectFile >Module &)> CompileFtor
ModuleSetHandleT addModuleSet(ModuleSetT Ms, MemoryManagerPtrT MemMgr, SymbolResolverPtrT Resolver)
Compile each module in the given module set, then add the resulting set of objects to the base layer ...
virtual void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj)=0
notifyObjectCompiled - Provides a pointer to compiled code for Module M.
void emitAndFinalize(ModuleSetHandleT H)
Immediately emit and finalize the moduleOB set represented by the given handle.
void removeModuleSet(ModuleSetHandleT H)
Remove the module set associated with the handle H.
JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name, bool ExportedSymbolsOnly)
Get the address of the given symbol in the context of the set of compiled modules represented by the ...
This is the base ObjectCache type which can be provided to an ExecutionEngine for the purpose of avoi...
Definition: ObjectCache.h:22
static ErrorOr< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Create ObjectFile from path.
Definition: ObjectFile.cpp:102