LLVM  10.0.0svn
IRCompileLayer.h
Go to the documentation of this file.
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"
19 #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 class IRCompileLayer : public IRLayer {
31 public:
32  using CompileFunction =
33  std::function<Expected<std::unique_ptr<MemoryBuffer>>(Module &)>;
34 
36  std::function<void(VModuleKey K, ThreadSafeModule TSM)>;
37 
39  CompileFunction Compile);
40 
41  void setNotifyCompiled(NotifyCompiledFunction NotifyCompiled);
42 
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>
59 public:
60  /// Callback type for notifications when modules are compiled.
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.
68  BaseLayerT &BaseLayer, CompileFtor Compile,
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.
76  ORCv1DeprecationAcknowledgement, BaseLayerT &BaseLayer,
77  CompileFtor Compile,
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.
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>
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
void setNotifyCompiled(NotifyCompiledFunction NotifyCompiled)
IRCompileLayer(ExecutionSession &ES, ObjectLayer &BaseLayer, CompileFunction Compile)
Represents a symbol in the JIT.
Definition: JITSymbol.h:219
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override
Emit should materialize the given IR.
std::function< void(VModuleKey K, std::unique_ptr< Module >)> NotifyCompiledCallback
Callback type for notifications when modules are compiled.
std::function< Expected< std::unique_ptr< MemoryBuffer > >(Module &)> CompileFunction
Error removeModule(VModuleKey K)
Remove the module associated with the VModuleKey K.
Definition: BitVector.h:937
Error emitAndFinalize(VModuleKey K)
Immediately emit and finalize the module represented by the given handle.
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:171
JITSymbol findSymbolIn(VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly)
Get the address of the given symbol in compiled module represented by the handle H.
ORCv1DeprecationAcknowledgement
LegacyIRCompileLayer(ORCv1DeprecationAcknowledgement, BaseLayerT &BaseLayer, CompileFtor Compile, NotifyCompiledCallback NotifyCompiled=NotifyCompiledCallback())
Legacy layer constructor with deprecation acknowledgement.
An LLVM Module together with a shared ThreadSafeContext.
Eager IR compiling layer.
Interface for layers that accept LLVM IR.
Definition: Layer.h:25
#define LLVM_ATTRIBUTE_DEPRECATED(decl, message)
Definition: Compiler.h:304
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
CompileFtor & getCompiler()
Get a reference to the compiler functor.
void setNotifyCompiled(NotifyCompiledCallback NotifyCompiled)
(Re)set the NotifyCompiled callback.
An ExecutionSession represents a running JIT program.
Definition: Core.h:761
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:42
Error addModule(VModuleKey K, std::unique_ptr< Module > M)
Compile the module, and add the resulting object to the base layer along with the given memory manage...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Interface for Layers that accept object files.
Definition: Layer.h:113
std::function< void(VModuleKey K, ThreadSafeModule TSM)> NotifyCompiledFunction
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.