]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/ExecutionEngine/Orc/IRCompileLayer.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / ExecutionEngine / Orc / IRCompileLayer.h
1 //===- IRCompileLayer.h -- Eagerly compile IR for JIT -----------*- C++ -*-===//
2 //
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
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"
18 #include "llvm/ExecutionEngine/Orc/Layer.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include <memory>
22 #include <string>
23
24 namespace llvm {
25
26 class Module;
27
28 namespace orc {
29
30 class IRCompileLayer : public IRLayer {
31 public:
32   using CompileFunction =
33       std::function<Expected<std::unique_ptr<MemoryBuffer>>(Module &)>;
34
35   using NotifyCompiledFunction =
36       std::function<void(VModuleKey K, ThreadSafeModule TSM)>;
37
38   IRCompileLayer(ExecutionSession &ES, ObjectLayer &BaseLayer,
39                  CompileFunction Compile);
40
41   void setNotifyCompiled(NotifyCompiledFunction NotifyCompiled);
42
43   void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override;
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.
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>
58 class LegacyIRCompileLayer {
59 public:
60   /// Callback type for notifications when modules are compiled.
61   using NotifyCompiledCallback =
62       std::function<void(VModuleKey K, std::unique_ptr<Module>)>;
63
64   /// Construct an LegacyIRCompileLayer with the given BaseLayer, which must
65   ///        implement the ObjectLayer concept.
66   LLVM_ATTRIBUTE_DEPRECATED(
67       LegacyIRCompileLayer(
68           BaseLayerT &BaseLayer, CompileFtor Compile,
69           NotifyCompiledCallback NotifyCompiled = NotifyCompiledCallback()),
70       "ORCv1 layers (layers with the 'Legacy' prefix) are deprecated. Please "
71       "use "
72       "the ORCv2 IRCompileLayer instead");
73
74   /// Legacy layer constructor with deprecation acknowledgement.
75   LegacyIRCompileLayer(
76       ORCv1DeprecationAcknowledgement, BaseLayerT &BaseLayer,
77       CompileFtor Compile,
78       NotifyCompiledCallback NotifyCompiled = NotifyCompiledCallback())
79       : BaseLayer(BaseLayer), Compile(std::move(Compile)),
80         NotifyCompiled(std::move(NotifyCompiled)) {}
81
82   /// Get a reference to the compiler functor.
83   CompileFtor& getCompiler() { return Compile; }
84
85   /// (Re)set the NotifyCompiled callback.
86   void setNotifyCompiled(NotifyCompiledCallback NotifyCompiled) {
87     this->NotifyCompiled = std::move(NotifyCompiled);
88   }
89
90   /// Compile the module, and add the resulting object to the base layer
91   ///        along with the given memory manager and symbol resolver.
92   Error addModule(VModuleKey K, std::unique_ptr<Module> M) {
93     if (auto Err = BaseLayer.addObject(std::move(K), Compile(*M)))
94       return Err;
95     if (NotifyCompiled)
96       NotifyCompiled(std::move(K), std::move(M));
97     return Error::success();
98   }
99
100   /// Remove the module associated with the VModuleKey K.
101   Error removeModule(VModuleKey K) { return BaseLayer.removeObject(K); }
102
103   /// Search for the given named symbol.
104   /// @param Name The name of the symbol to search for.
105   /// @param ExportedSymbolsOnly If true, search only for exported symbols.
106   /// @return A handle for the given named symbol, if it exists.
107   JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
108     return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
109   }
110
111   /// Get the address of the given symbol in compiled module represented
112   ///        by the handle H. This call is forwarded to the base layer's
113   ///        implementation.
114   /// @param K The VModuleKey for the module to search in.
115   /// @param Name The name of the symbol to search for.
116   /// @param ExportedSymbolsOnly If true, search only for exported symbols.
117   /// @return A handle for the given named symbol, if it is found in the
118   ///         given module.
119   JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
120                          bool ExportedSymbolsOnly) {
121     return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
122   }
123
124   /// Immediately emit and finalize the module represented by the given
125   ///        handle.
126   /// @param K The VModuleKey for the module to emit/finalize.
127   Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
128
129 private:
130   BaseLayerT &BaseLayer;
131   CompileFtor Compile;
132   NotifyCompiledCallback NotifyCompiled;
133 };
134
135 template <typename BaseLayerT, typename CompileFtor>
136 LegacyIRCompileLayer<BaseLayerT, CompileFtor>::LegacyIRCompileLayer(
137     BaseLayerT &BaseLayer, CompileFtor Compile,
138     NotifyCompiledCallback NotifyCompiled)
139     : BaseLayer(BaseLayer), Compile(std::move(Compile)),
140       NotifyCompiled(std::move(NotifyCompiled)) {}
141
142 } // end namespace orc
143 } // end namespace llvm
144
145 #endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILINGLAYER_H