Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- IRCompileLayer.h -- Eagerly compile IR for JIT -----------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Contains the definition for a basic, eagerly compiling layer of the JIT. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H |
| 14 | #define LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H |
| 15 | |
| 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/ExecutionEngine/JITSymbol.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 18 | #include "llvm/ExecutionEngine/Orc/Layer.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | #include "llvm/Support/Error.h" |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 20 | #include "llvm/Support/MemoryBuffer.h" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <string> |
| 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | class Module; |
| 27 | |
| 28 | namespace orc { |
| 29 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 30 | class IRCompileLayer : public IRLayer { |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 31 | public: |
| 32 | using CompileFunction = |
| 33 | std::function<Expected<std::unique_ptr<MemoryBuffer>>(Module &)>; |
| 34 | |
| 35 | using NotifyCompiledFunction = |
Andrew Scull | 0372a57 | 2018-11-16 15:47:06 +0000 | [diff] [blame] | 36 | std::function<void(VModuleKey K, ThreadSafeModule TSM)>; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 37 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 38 | IRCompileLayer(ExecutionSession &ES, ObjectLayer &BaseLayer, |
| 39 | CompileFunction Compile); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 40 | |
| 41 | void setNotifyCompiled(NotifyCompiledFunction NotifyCompiled); |
| 42 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 43 | void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 44 | |
| 45 | private: |
| 46 | mutable std::mutex IRLayerMutex; |
| 47 | ObjectLayer &BaseLayer; |
| 48 | CompileFunction Compile; |
| 49 | NotifyCompiledFunction NotifyCompiled = NotifyCompiledFunction(); |
| 50 | }; |
| 51 | |
| 52 | /// Eager IR compiling layer. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 53 | /// |
| 54 | /// This layer immediately compiles each IR module added via addModule to an |
| 55 | /// object file and adds this module file to the layer below, which must |
| 56 | /// implement the object layer concept. |
| 57 | template <typename BaseLayerT, typename CompileFtor> |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 58 | class LegacyIRCompileLayer { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 59 | public: |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 60 | /// Callback type for notifications when modules are compiled. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 61 | using NotifyCompiledCallback = |
| 62 | std::function<void(VModuleKey K, std::unique_ptr<Module>)>; |
| 63 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 64 | /// Construct an LegacyIRCompileLayer with the given BaseLayer, which must |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 65 | /// implement the ObjectLayer concept. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 66 | LegacyIRCompileLayer( |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 67 | BaseLayerT &BaseLayer, CompileFtor Compile, |
| 68 | NotifyCompiledCallback NotifyCompiled = NotifyCompiledCallback()) |
| 69 | : BaseLayer(BaseLayer), Compile(std::move(Compile)), |
| 70 | NotifyCompiled(std::move(NotifyCompiled)) {} |
| 71 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 72 | /// Get a reference to the compiler functor. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | CompileFtor& getCompiler() { return Compile; } |
| 74 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 75 | /// (Re)set the NotifyCompiled callback. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 76 | void setNotifyCompiled(NotifyCompiledCallback NotifyCompiled) { |
| 77 | this->NotifyCompiled = std::move(NotifyCompiled); |
| 78 | } |
| 79 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 80 | /// Compile the module, and add the resulting object to the base layer |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 81 | /// along with the given memory manager and symbol resolver. |
| 82 | Error addModule(VModuleKey K, std::unique_ptr<Module> M) { |
| 83 | if (auto Err = BaseLayer.addObject(std::move(K), Compile(*M))) |
| 84 | return Err; |
| 85 | if (NotifyCompiled) |
| 86 | NotifyCompiled(std::move(K), std::move(M)); |
| 87 | return Error::success(); |
| 88 | } |
| 89 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 90 | /// Remove the module associated with the VModuleKey K. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 91 | Error removeModule(VModuleKey K) { return BaseLayer.removeObject(K); } |
| 92 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 93 | /// Search for the given named symbol. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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); |
| 99 | } |
| 100 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 101 | /// Get the address of the given symbol in compiled module represented |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 102 | /// by the handle H. This call is forwarded to the base layer's |
| 103 | /// implementation. |
| 104 | /// @param K The VModuleKey for the module 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. |
| 109 | JITSymbol findSymbolIn(VModuleKey K, const std::string &Name, |
| 110 | bool ExportedSymbolsOnly) { |
| 111 | return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly); |
| 112 | } |
| 113 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 114 | /// Immediately emit and finalize the module represented by the given |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 115 | /// handle. |
| 116 | /// @param K The VModuleKey for the module to emit/finalize. |
| 117 | Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); } |
| 118 | |
| 119 | private: |
| 120 | BaseLayerT &BaseLayer; |
| 121 | CompileFtor Compile; |
| 122 | NotifyCompiledCallback NotifyCompiled; |
| 123 | }; |
| 124 | |
| 125 | } // end namespace orc |
| 126 | |
| 127 | } // end namespace llvm |
| 128 | |
| 129 | #endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILINGLAYER_H |