blob: ecff09b98f87389ef8454dcb6dfeeccc45ed432e [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- IRCompileLayer.h -- Eagerly compile IR for JIT -----------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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 Scull5e1ddfa2018-08-14 10:06:54 +01006//
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 Scullcdfcccc2018-10-05 20:58:37 +010018#include "llvm/ExecutionEngine/Orc/Layer.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010019#include "llvm/Support/Error.h"
Andrew Scullcdfcccc2018-10-05 20:58:37 +010020#include "llvm/Support/MemoryBuffer.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010021#include <memory>
22#include <string>
23
24namespace llvm {
25
26class Module;
27
28namespace orc {
29
Andrew Walbran16937d02019-10-22 13:54:20 +010030class IRCompileLayer : public IRLayer {
Andrew Scullcdfcccc2018-10-05 20:58:37 +010031public:
32 using CompileFunction =
33 std::function<Expected<std::unique_ptr<MemoryBuffer>>(Module &)>;
34
35 using NotifyCompiledFunction =
Andrew Scull0372a572018-11-16 15:47:06 +000036 std::function<void(VModuleKey K, ThreadSafeModule TSM)>;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010037
Andrew Walbran16937d02019-10-22 13:54:20 +010038 IRCompileLayer(ExecutionSession &ES, ObjectLayer &BaseLayer,
39 CompileFunction Compile);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010040
41 void setNotifyCompiled(NotifyCompiledFunction NotifyCompiled);
42
Andrew Walbran16937d02019-10-22 13:54:20 +010043 void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010044
45private:
46 mutable std::mutex IRLayerMutex;
47 ObjectLayer &BaseLayer;
48 CompileFunction Compile;
49 NotifyCompiledFunction NotifyCompiled = NotifyCompiledFunction();
50};
51
52/// Eager IR compiling layer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010053///
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.
57template <typename BaseLayerT, typename CompileFtor>
Andrew Walbran16937d02019-10-22 13:54:20 +010058class LegacyIRCompileLayer {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010059public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010060 /// Callback type for notifications when modules are compiled.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010061 using NotifyCompiledCallback =
62 std::function<void(VModuleKey K, std::unique_ptr<Module>)>;
63
Andrew Walbran16937d02019-10-22 13:54:20 +010064 /// Construct an LegacyIRCompileLayer with the given BaseLayer, which must
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010065 /// implement the ObjectLayer concept.
Andrew Walbran16937d02019-10-22 13:54:20 +010066 LegacyIRCompileLayer(
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010067 BaseLayerT &BaseLayer, CompileFtor Compile,
68 NotifyCompiledCallback NotifyCompiled = NotifyCompiledCallback())
69 : BaseLayer(BaseLayer), Compile(std::move(Compile)),
70 NotifyCompiled(std::move(NotifyCompiled)) {}
71
Andrew Scullcdfcccc2018-10-05 20:58:37 +010072 /// Get a reference to the compiler functor.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073 CompileFtor& getCompiler() { return Compile; }
74
Andrew Scullcdfcccc2018-10-05 20:58:37 +010075 /// (Re)set the NotifyCompiled callback.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010076 void setNotifyCompiled(NotifyCompiledCallback NotifyCompiled) {
77 this->NotifyCompiled = std::move(NotifyCompiled);
78 }
79
Andrew Scullcdfcccc2018-10-05 20:58:37 +010080 /// Compile the module, and add the resulting object to the base layer
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010081 /// 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 Scullcdfcccc2018-10-05 20:58:37 +010090 /// Remove the module associated with the VModuleKey K.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010091 Error removeModule(VModuleKey K) { return BaseLayer.removeObject(K); }
92
Andrew Scullcdfcccc2018-10-05 20:58:37 +010093 /// Search for the given named symbol.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010094 /// @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 Scullcdfcccc2018-10-05 20:58:37 +0100101 /// Get the address of the given symbol in compiled module represented
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100102 /// 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 Scullcdfcccc2018-10-05 20:58:37 +0100114 /// Immediately emit and finalize the module represented by the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100115 /// handle.
116 /// @param K The VModuleKey for the module to emit/finalize.
117 Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
118
119private:
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