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/ADT/STLExtras.h"
18 #include "llvm/ExecutionEngine/JITSymbol.h"
19 #include "llvm/ExecutionEngine/Orc/Layer.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/MemoryBuffer.h"
31 class IRCompileLayer2 : public IRLayer {
33 using CompileFunction =
34 std::function<Expected<std::unique_ptr<MemoryBuffer>>(Module &)>;
36 using NotifyCompiledFunction =
37 std::function<void(VModuleKey K, std::unique_ptr<Module>)>;
39 IRCompileLayer2(ExecutionSession &ES, ObjectLayer &BaseLayer,
40 CompileFunction Compile);
42 void setNotifyCompiled(NotifyCompiledFunction NotifyCompiled);
44 void emit(MaterializationResponsibility R, VModuleKey K,
45 std::unique_ptr<Module> M) override;
48 mutable std::mutex IRLayerMutex;
49 ObjectLayer &BaseLayer;
50 CompileFunction Compile;
51 NotifyCompiledFunction NotifyCompiled = NotifyCompiledFunction();
54 /// Eager IR compiling layer.
56 /// This layer immediately compiles each IR module added via addModule to an
57 /// object file and adds this module file to the layer below, which must
58 /// implement the object layer concept.
59 template <typename BaseLayerT, typename CompileFtor>
60 class IRCompileLayer {
62 /// Callback type for notifications when modules are compiled.
63 using NotifyCompiledCallback =
64 std::function<void(VModuleKey K, std::unique_ptr<Module>)>;
66 /// Construct an IRCompileLayer with the given BaseLayer, which must
67 /// implement the ObjectLayer concept.
69 BaseLayerT &BaseLayer, CompileFtor Compile,
70 NotifyCompiledCallback NotifyCompiled = NotifyCompiledCallback())
71 : BaseLayer(BaseLayer), Compile(std::move(Compile)),
72 NotifyCompiled(std::move(NotifyCompiled)) {}
74 /// Get a reference to the compiler functor.
75 CompileFtor& getCompiler() { return Compile; }
77 /// (Re)set the NotifyCompiled callback.
78 void setNotifyCompiled(NotifyCompiledCallback NotifyCompiled) {
79 this->NotifyCompiled = std::move(NotifyCompiled);
82 /// Compile the module, and add the resulting object to the base layer
83 /// along with the given memory manager and symbol resolver.
84 Error addModule(VModuleKey K, std::unique_ptr<Module> M) {
85 if (auto Err = BaseLayer.addObject(std::move(K), Compile(*M)))
88 NotifyCompiled(std::move(K), std::move(M));
89 return Error::success();
92 /// Remove the module associated with the VModuleKey K.
93 Error removeModule(VModuleKey K) { return BaseLayer.removeObject(K); }
95 /// Search for the given named symbol.
96 /// @param Name The name of the symbol to search for.
97 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
98 /// @return A handle for the given named symbol, if it exists.
99 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
100 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
103 /// Get the address of the given symbol in compiled module represented
104 /// by the handle H. This call is forwarded to the base layer's
106 /// @param K The VModuleKey for the module to search in.
107 /// @param Name The name of the symbol to search for.
108 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
109 /// @return A handle for the given named symbol, if it is found in the
111 JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
112 bool ExportedSymbolsOnly) {
113 return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
116 /// Immediately emit and finalize the module represented by the given
118 /// @param K The VModuleKey for the module to emit/finalize.
119 Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
122 BaseLayerT &BaseLayer;
124 NotifyCompiledCallback NotifyCompiled;
127 } // end namespace orc
129 } // end namespace llvm
131 #endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILINGLAYER_H