1 //===------ IRCompileLayer.h -- Eagerly compile IR for JIT ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Contains the definition for a basic, eagerly compiling layer of the JIT.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H
17 #include "JITSymbol.h"
18 #include "llvm/ExecutionEngine/ObjectCache.h"
19 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
20 #include "llvm/Object/ObjectFile.h"
25 /// @brief Eager IR compiling layer.
27 /// This layer accepts sets of LLVM IR Modules (via addModuleSet). It
28 /// immediately compiles each IR module to an object file (each IR Module is
29 /// compiled separately). The resulting set of object files is then added to
30 /// the layer below, which must implement the object layer concept.
31 template <typename BaseLayerT> class IRCompileLayer {
33 typedef std::function<object::OwningBinary<object::ObjectFile>(Module &)>
37 typedef typename BaseLayerT::ObjSetHandleT ObjSetHandleT;
39 typedef std::vector<std::unique_ptr<object::ObjectFile>> OwningObjectVec;
40 typedef std::vector<std::unique_ptr<MemoryBuffer>> OwningBufferVec;
43 /// @brief Handle to a set of compiled modules.
44 typedef ObjSetHandleT ModuleSetHandleT;
46 /// @brief Construct an IRCompileLayer with the given BaseLayer, which must
47 /// implement the ObjectLayer concept.
48 IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile)
49 : BaseLayer(BaseLayer), Compile(std::move(Compile)), ObjCache(nullptr) {}
51 /// @brief Set an ObjectCache to query before compiling.
52 void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
54 /// @brief Compile each module in the given module set, then then add the
55 /// resulting set of objects to the base layer, along with the memory
58 /// @return A handle for the added modules.
59 template <typename ModuleSetT>
60 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
61 std::unique_ptr<RTDyldMemoryManager> MM) {
62 OwningObjectVec Objects;
63 OwningBufferVec Buffers;
65 for (const auto &M : Ms) {
66 std::unique_ptr<object::ObjectFile> Object;
67 std::unique_ptr<MemoryBuffer> Buffer;
70 std::tie(Object, Buffer) = tryToLoadFromObjectCache(*M).takeBinary();
73 std::tie(Object, Buffer) = Compile(*M).takeBinary();
75 ObjCache->notifyObjectCompiled(&*M, Buffer->getMemBufferRef());
78 Objects.push_back(std::move(Object));
79 Buffers.push_back(std::move(Buffer));
83 BaseLayer.addObjectSet(Objects, std::move(MM));
85 BaseLayer.takeOwnershipOfBuffers(H, std::move(Buffers));
90 /// @brief Remove the module set associated with the handle H.
91 void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeObjectSet(H); }
93 /// @brief Search for the given named symbol.
94 /// @param Name The name of the symbol to search for.
95 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
96 /// @return A handle for the given named symbol, if it exists.
97 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
98 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
101 /// @brief Get the address of the given symbol in the context of the set of
102 /// compiled modules represented by the handle H. This call is
103 /// forwarded to the base layer's implementation.
104 /// @param H The handle for the module set to search in.
105 /// @param Name The name of the symbol to search for.
106 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
107 /// @return A handle for the given named symbol, if it is found in the
108 /// given module set.
109 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
110 bool ExportedSymbolsOnly) {
111 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
115 object::OwningBinary<object::ObjectFile>
116 tryToLoadFromObjectCache(const Module &M) {
117 std::unique_ptr<MemoryBuffer> ObjBuffer = ObjCache->getObject(&M);
119 return object::OwningBinary<object::ObjectFile>();
121 ErrorOr<std::unique_ptr<object::ObjectFile>> Obj =
122 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
124 return object::OwningBinary<object::ObjectFile>();
126 return object::OwningBinary<object::ObjectFile>(std::move(*Obj),
127 std::move(ObjBuffer));
130 BaseLayerT &BaseLayer;
132 ObjectCache *ObjCache;
136 #endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILINGLAYER_H