LLVM  11.0.0git
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  class IRCompiler {
33  public:
35  virtual ~IRCompiler();
37  return MO;
38  }
40 
41  protected:
43 
44  private:
46  };
47 
49  std::function<void(VModuleKey K, ThreadSafeModule TSM)>;
50 
52  std::unique_ptr<IRCompiler> Compile);
53 
54  IRCompiler &getCompiler() { return *Compile; }
55 
56  void setNotifyCompiled(NotifyCompiledFunction NotifyCompiled);
57 
59 
60 private:
61  mutable std::mutex IRLayerMutex;
62  ObjectLayer &BaseLayer;
63  std::unique_ptr<IRCompiler> Compile;
64  const IRSymbolMapper::ManglingOptions *ManglingOpts;
66 };
67 
68 /// Eager IR compiling layer.
69 ///
70 /// This layer immediately compiles each IR module added via addModule to an
71 /// object file and adds this module file to the layer below, which must
72 /// implement the object layer concept.
73 template <typename BaseLayerT, typename CompileFtor>
75 public:
76  /// Callback type for notifications when modules are compiled.
78  std::function<void(VModuleKey K, std::unique_ptr<Module>)>;
79 
80  /// Construct an LegacyIRCompileLayer with the given BaseLayer, which must
81  /// implement the ObjectLayer concept.
84  BaseLayerT &BaseLayer, CompileFtor Compile,
86  "ORCv1 layers (layers with the 'Legacy' prefix) are deprecated. Please "
87  "use "
88  "the ORCv2 IRCompileLayer instead");
89 
90  /// Legacy layer constructor with deprecation acknowledgement.
92  ORCv1DeprecationAcknowledgement, BaseLayerT &BaseLayer,
93  CompileFtor Compile,
95  : BaseLayer(BaseLayer), Compile(std::move(Compile)),
96  NotifyCompiled(std::move(NotifyCompiled)) {}
97 
98  /// Get a reference to the compiler functor.
99  CompileFtor& getCompiler() { return Compile; }
100 
101  /// (Re)set the NotifyCompiled callback.
103  this->NotifyCompiled = std::move(NotifyCompiled);
104  }
105 
106  /// Compile the module, and add the resulting object to the base layer
107  /// along with the given memory manager and symbol resolver.
108  Error addModule(VModuleKey K, std::unique_ptr<Module> M) {
109  auto Obj = Compile(*M);
110  if (!Obj)
111  return Obj.takeError();
112  if (auto Err = BaseLayer.addObject(std::move(K), std::move(*Obj)))
113  return Err;
114  if (NotifyCompiled)
115  NotifyCompiled(std::move(K), std::move(M));
116  return Error::success();
117  }
118 
119  /// Remove the module associated with the VModuleKey K.
120  Error removeModule(VModuleKey K) { return BaseLayer.removeObject(K); }
121 
122  /// Search for the given named symbol.
123  /// @param Name The name of the symbol to search for.
124  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
125  /// @return A handle for the given named symbol, if it exists.
126  JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
127  return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
128  }
129 
130  /// Get the address of the given symbol in compiled module represented
131  /// by the handle H. This call is forwarded to the base layer's
132  /// implementation.
133  /// @param K The VModuleKey for the module to search in.
134  /// @param Name The name of the symbol to search for.
135  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
136  /// @return A handle for the given named symbol, if it is found in the
137  /// given module.
138  JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
139  bool ExportedSymbolsOnly) {
140  return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
141  }
142 
143  /// Immediately emit and finalize the module represented by the given
144  /// handle.
145  /// @param K The VModuleKey for the module to emit/finalize.
146  Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
147 
148 private:
149  BaseLayerT &BaseLayer;
150  CompileFtor Compile;
151  NotifyCompiledCallback NotifyCompiled;
152 };
153 
154 template <typename BaseLayerT, typename CompileFtor>
156  BaseLayerT &BaseLayer, CompileFtor Compile,
157  NotifyCompiledCallback NotifyCompiled)
158  : BaseLayer(BaseLayer), Compile(std::move(Compile)),
159  NotifyCompiled(std::move(NotifyCompiled)) {}
160 
161 } // end namespace orc
162 } // end namespace llvm
163 
164 #endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILINGLAYER_H
void setNotifyCompiled(NotifyCompiledFunction NotifyCompiled)
Represents a symbol in the JIT.
Definition: JITSymbol.h:265
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:67
Machine Debugify Module
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.
virtual Expected< std::unique_ptr< MemoryBuffer > > operator()(Module &M)=0
std::function< void(VModuleKey K, std::unique_ptr< Module >)> NotifyCompiledCallback
Callback type for notifications when modules are compiled.
Error removeModule(VModuleKey K)
Remove the module associated with the VModuleKey K.
Definition: BitVector.h:959
Error emitAndFinalize(VModuleKey K)
Immediately emit and finalize the module represented by the given handle.
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:410
IRCompiler & getCompiler()
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.
IRSymbolMapper::ManglingOptions & manglingOptions()
An LLVM Module together with a shared ThreadSafeContext.
IRCompileLayer(ExecutionSession &ES, ObjectLayer &BaseLayer, std::unique_ptr< IRCompiler > Compile)
Eager IR compiling layer.
Interface for layers that accept LLVM IR.
Definition: Layer.h:66
#define LLVM_ATTRIBUTE_DEPRECATED(decl, message)
Definition: Compiler.h:327
static ErrorSuccess success()
Create a success value.
Definition: Error.h:332
CompileFtor & getCompiler()
Get a reference to the compiler functor.
IRCompiler(IRSymbolMapper::ManglingOptions MO)
void setNotifyCompiled(NotifyCompiledCallback NotifyCompiled)
(Re)set the NotifyCompiled callback.
An ExecutionSession represents a running JIT program.
Definition: Core.h:1088
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
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:128
const IRSymbolMapper::ManglingOptions & getManglingOptions() const
std::function< void(VModuleKey K, ThreadSafeModule TSM)> NotifyCompiledFunction
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.