Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame^] | 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 "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ExecutionEngine/JITSymbol.h" |
| 19 | #include "llvm/ExecutionEngine/Orc/Core.h" |
| 20 | #include "llvm/Support/Error.h" |
| 21 | #include <memory> |
| 22 | #include <string> |
| 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | class Module; |
| 27 | |
| 28 | namespace orc { |
| 29 | |
| 30 | /// @brief Eager IR compiling layer. |
| 31 | /// |
| 32 | /// This layer immediately compiles each IR module added via addModule to an |
| 33 | /// object file and adds this module file to the layer below, which must |
| 34 | /// implement the object layer concept. |
| 35 | template <typename BaseLayerT, typename CompileFtor> |
| 36 | class IRCompileLayer { |
| 37 | public: |
| 38 | /// @brief Callback type for notifications when modules are compiled. |
| 39 | using NotifyCompiledCallback = |
| 40 | std::function<void(VModuleKey K, std::unique_ptr<Module>)>; |
| 41 | |
| 42 | /// @brief Construct an IRCompileLayer with the given BaseLayer, which must |
| 43 | /// implement the ObjectLayer concept. |
| 44 | IRCompileLayer( |
| 45 | BaseLayerT &BaseLayer, CompileFtor Compile, |
| 46 | NotifyCompiledCallback NotifyCompiled = NotifyCompiledCallback()) |
| 47 | : BaseLayer(BaseLayer), Compile(std::move(Compile)), |
| 48 | NotifyCompiled(std::move(NotifyCompiled)) {} |
| 49 | |
| 50 | /// @brief Get a reference to the compiler functor. |
| 51 | CompileFtor& getCompiler() { return Compile; } |
| 52 | |
| 53 | /// @brief (Re)set the NotifyCompiled callback. |
| 54 | void setNotifyCompiled(NotifyCompiledCallback NotifyCompiled) { |
| 55 | this->NotifyCompiled = std::move(NotifyCompiled); |
| 56 | } |
| 57 | |
| 58 | /// @brief Compile the module, and add the resulting object to the base layer |
| 59 | /// along with the given memory manager and symbol resolver. |
| 60 | Error addModule(VModuleKey K, std::unique_ptr<Module> M) { |
| 61 | if (auto Err = BaseLayer.addObject(std::move(K), Compile(*M))) |
| 62 | return Err; |
| 63 | if (NotifyCompiled) |
| 64 | NotifyCompiled(std::move(K), std::move(M)); |
| 65 | return Error::success(); |
| 66 | } |
| 67 | |
| 68 | /// @brief Remove the module associated with the VModuleKey K. |
| 69 | Error removeModule(VModuleKey K) { return BaseLayer.removeObject(K); } |
| 70 | |
| 71 | /// @brief Search for the given named symbol. |
| 72 | /// @param Name The name of the symbol to search for. |
| 73 | /// @param ExportedSymbolsOnly If true, search only for exported symbols. |
| 74 | /// @return A handle for the given named symbol, if it exists. |
| 75 | JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) { |
| 76 | return BaseLayer.findSymbol(Name, ExportedSymbolsOnly); |
| 77 | } |
| 78 | |
| 79 | /// @brief Get the address of the given symbol in compiled module represented |
| 80 | /// by the handle H. This call is forwarded to the base layer's |
| 81 | /// implementation. |
| 82 | /// @param K The VModuleKey for the module to search in. |
| 83 | /// @param Name The name of the symbol to search for. |
| 84 | /// @param ExportedSymbolsOnly If true, search only for exported symbols. |
| 85 | /// @return A handle for the given named symbol, if it is found in the |
| 86 | /// given module. |
| 87 | JITSymbol findSymbolIn(VModuleKey K, const std::string &Name, |
| 88 | bool ExportedSymbolsOnly) { |
| 89 | return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly); |
| 90 | } |
| 91 | |
| 92 | /// @brief Immediately emit and finalize the module represented by the given |
| 93 | /// handle. |
| 94 | /// @param K The VModuleKey for the module to emit/finalize. |
| 95 | Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); } |
| 96 | |
| 97 | private: |
| 98 | BaseLayerT &BaseLayer; |
| 99 | CompileFtor Compile; |
| 100 | NotifyCompiledCallback NotifyCompiled; |
| 101 | }; |
| 102 | |
| 103 | } // end namespace orc |
| 104 | |
| 105 | } // end namespace llvm |
| 106 | |
| 107 | #endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILINGLAYER_H |