LLVM  8.0.1
IRCompileLayer.h
Go to the documentation of this file.
1 //===- IRCompileLayer.h -- Eagerly compile IR for JIT -----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Contains the definition for a basic, eagerly compiling layer of the JIT.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_IRCOMPILELAYER_H
16 
17 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/Error.h"
22 #include <memory>
23 #include <string>
24 
25 namespace llvm {
26 
27 class Module;
28 
29 namespace orc {
30 
31 class IRCompileLayer : public IRLayer {
32 public:
33  using CompileFunction =
34  std::function<Expected<std::unique_ptr<MemoryBuffer>>(Module &)>;
35 
37  std::function<void(VModuleKey K, ThreadSafeModule TSM)>;
38 
40  CompileFunction Compile);
41 
42  void setNotifyCompiled(NotifyCompiledFunction NotifyCompiled);
43 
45 
46 private:
47  mutable std::mutex IRLayerMutex;
48  ObjectLayer &BaseLayer;
49  CompileFunction Compile;
50  NotifyCompiledFunction NotifyCompiled = NotifyCompiledFunction();
51 };
52 
53 /// Eager IR compiling layer.
54 ///
55 /// This layer immediately compiles each IR module added via addModule to an
56 /// object file and adds this module file to the layer below, which must
57 /// implement the object layer concept.
58 template <typename BaseLayerT, typename CompileFtor>
60 public:
61  /// Callback type for notifications when modules are compiled.
63  std::function<void(VModuleKey K, std::unique_ptr<Module>)>;
64 
65  /// Construct an LegacyIRCompileLayer with the given BaseLayer, which must
66  /// implement the ObjectLayer concept.
68  BaseLayerT &BaseLayer, CompileFtor Compile,
70  : BaseLayer(BaseLayer), Compile(std::move(Compile)),
71  NotifyCompiled(std::move(NotifyCompiled)) {}
72 
73  /// Get a reference to the compiler functor.
74  CompileFtor& getCompiler() { return Compile; }
75 
76  /// (Re)set the NotifyCompiled callback.
78  this->NotifyCompiled = std::move(NotifyCompiled);
79  }
80 
81  /// Compile the module, and add the resulting object to the base layer
82  /// along with the given memory manager and symbol resolver.
83  Error addModule(VModuleKey K, std::unique_ptr<Module> M) {
84  if (auto Err = BaseLayer.addObject(std::move(K), Compile(*M)))
85  return Err;
86  if (NotifyCompiled)
87  NotifyCompiled(std::move(K), std::move(M));
88  return Error::success();
89  }
90 
91  /// Remove the module associated with the VModuleKey K.
92  Error removeModule(VModuleKey K) { return BaseLayer.removeObject(K); }
93 
94  /// Search for the given named symbol.
95  /// @param Name The name of the symbol to search for.
96  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
97  /// @return A handle for the given named symbol, if it exists.
98  JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
99  return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
100  }
101 
102  /// Get the address of the given symbol in compiled module represented
103  /// by the handle H. This call is forwarded to the base layer's
104  /// implementation.
105  /// @param K The VModuleKey for the module to search in.
106  /// @param Name The name of the symbol to search for.
107  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
108  /// @return A handle for the given named symbol, if it is found in the
109  /// given module.
110  JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
111  bool ExportedSymbolsOnly) {
112  return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
113  }
114 
115  /// Immediately emit and finalize the module represented by the given
116  /// handle.
117  /// @param K The VModuleKey for the module to emit/finalize.
118  Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
119 
120 private:
121  BaseLayerT &BaseLayer;
122  CompileFtor Compile;
123  NotifyCompiledCallback NotifyCompiled;
124 };
125 
126 } // end namespace orc
127 
128 } // end namespace llvm
129 
130 #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:238
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override
Emit should materialize the given IR.
LegacyIRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile, NotifyCompiledCallback NotifyCompiled=NotifyCompiledCallback())
Construct an LegacyIRCompileLayer with the given BaseLayer, which must implement the ObjectLayer conc...
std::function< void(VModuleKey K, std::unique_ptr< Module >)> NotifyCompiledCallback
Callback type for notifications when modules are compiled.
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
std::function< Expected< std::unique_ptr< MemoryBuffer > >(Module &)> CompileFunction
Error removeModule(VModuleKey K)
Remove the module associated with the VModuleKey K.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Definition: BitVector.h:938
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:155
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.
An LLVM Module together with a shared ThreadSafeContext.
Eager IR compiling layer.
Interface for layers that accept LLVM IR.
Definition: Layer.h:26
static ErrorSuccess success()
Create a success value.
Definition: Error.h:327
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:697
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:158
Interface for Layers that accept object files.
Definition: Layer.h:114
std::function< void(VModuleKey K, ThreadSafeModule TSM)> NotifyCompiledFunction
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.