LLVM  9.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.
67  BaseLayerT &BaseLayer, CompileFtor Compile,
69  : BaseLayer(BaseLayer), Compile(std::move(Compile)),
70  NotifyCompiled(std::move(NotifyCompiled)) {}
71 
72  /// Get a reference to the compiler functor.
73  CompileFtor& getCompiler() { return Compile; }
74 
75  /// (Re)set the NotifyCompiled callback.
77  this->NotifyCompiled = std::move(NotifyCompiled);
78  }
79 
80  /// Compile the module, and add the resulting object to the base layer
81  /// 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 
90  /// Remove the module associated with the VModuleKey K.
91  Error removeModule(VModuleKey K) { return BaseLayer.removeObject(K); }
92 
93  /// Search for the given named symbol.
94  /// @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 
101  /// Get the address of the given symbol in compiled module represented
102  /// 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 
114  /// Immediately emit and finalize the module represented by the given
115  /// handle.
116  /// @param K The VModuleKey for the module to emit/finalize.
117  Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
118 
119 private:
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
void setNotifyCompiled(NotifyCompiledFunction NotifyCompiled)
IRCompileLayer(ExecutionSession &ES, ObjectLayer &BaseLayer, CompileFunction Compile)
Represents a symbol in the JIT.
Definition: JITSymbol.h:237
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:65
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.
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:39
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:154
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:25
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:696
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.