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 "llvm/ExecutionEngine/ObjectCache.h"
18 #include "llvm/Object/ObjectFile.h"
23 /// @brief Eager IR compiling layer.
25 /// This layer accepts sets of LLVM IR Modules (via addModuleSet). It
26 /// immediately compiles each IR module to an object file (each IR Module is
27 /// compiled separately). The resulting set of object files is then added to
28 /// the layer below, which must implement the object layer concept.
29 template <typename BaseLayerT> class IRCompileLayer {
31 typedef std::function<object::OwningBinary<object::ObjectFile>(Module &)>
35 typedef typename BaseLayerT::ObjSetHandleT ObjSetHandleT;
37 typedef std::vector<std::unique_ptr<object::ObjectFile>> OwningObjectVec;
38 typedef std::vector<std::unique_ptr<MemoryBuffer>> OwningBufferVec;
41 /// @brief Handle to a set of compiled modules.
42 typedef ObjSetHandleT ModuleSetHandleT;
44 /// @brief Construct an IRCompileLayer with the given BaseLayer, which must
45 /// implement the ObjectLayer concept.
46 IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile)
47 : BaseLayer(BaseLayer), Compile(std::move(Compile)), ObjCache(nullptr) {}
49 /// @brief Set an ObjectCache to query before compiling.
50 void setObjectCache(ObjectCache *NewCache) { ObjCache = NewCache; }
52 /// @brief Compile each module in the given module set, then then add the
53 /// resulting set of objects to the base layer, along with the memory
56 /// @return A handle for the added modules.
57 template <typename ModuleSetT>
58 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
59 std::unique_ptr<RTDyldMemoryManager> MM) {
60 OwningObjectVec Objects;
61 OwningBufferVec Buffers;
63 for (const auto &M : Ms) {
64 std::unique_ptr<object::ObjectFile> Object;
65 std::unique_ptr<MemoryBuffer> Buffer;
68 std::tie(Object, Buffer) = tryToLoadFromObjectCache(*M).takeBinary();
71 std::tie(Object, Buffer) = Compile(*M).takeBinary();
73 ObjCache->notifyObjectCompiled(&*M, Buffer->getMemBufferRef());
76 Objects.push_back(std::move(Object));
77 Buffers.push_back(std::move(Buffer));
81 BaseLayer.addObjectSet(Objects, std::move(MM));
83 BaseLayer.takeOwnershipOfBuffers(H, std::move(Buffers));
88 /// @brief Remove the module set associated with the handle H.
89 void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeObjectSet(H); }
91 /// @brief Get the address of a loaded symbol. This call is forwarded to the
92 /// base layer's getSymbolAddress implementation.
93 uint64_t getSymbolAddress(const std::string &Name, bool ExportedSymbolsOnly) {
94 return BaseLayer.getSymbolAddress(Name, ExportedSymbolsOnly);
97 /// @brief Get the address of the given symbol in the context of the set of
98 /// compiled modules represented by the handle H. This call is
99 /// forwarded to the base layer's implementation.
100 uint64_t lookupSymbolAddressIn(ModuleSetHandleT H, const std::string &Name,
101 bool ExportedSymbolsOnly) {
102 return BaseLayer.lookupSymbolAddressIn(H, Name, ExportedSymbolsOnly);
106 object::OwningBinary<object::ObjectFile>
107 tryToLoadFromObjectCache(const Module &M) {
108 std::unique_ptr<MemoryBuffer> ObjBuffer = ObjCache->getObject(&M);
110 return object::OwningBinary<object::ObjectFile>();
112 ErrorOr<std::unique_ptr<object::ObjectFile>> Obj =
113 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef());
115 return object::OwningBinary<object::ObjectFile>();
117 return object::OwningBinary<object::ObjectFile>(std::move(*Obj),
118 std::move(ObjBuffer));
121 BaseLayerT &BaseLayer;
123 ObjectCache *ObjCache;
127 #endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILINGLAYER_H