LLVM  5.0.0
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"
19 #include "llvm/Support/Error.h"
20 #include <memory>
21 #include <string>
22 
23 namespace llvm {
24 
25 class Module;
26 
27 namespace orc {
28 
29 /// @brief Eager IR compiling layer.
30 ///
31 /// This layer immediately compiles each IR module added via addModule to an
32 /// object file and adds this module file to the layer below, which must
33 /// implement the object layer concept.
34 template <typename BaseLayerT, typename CompileFtor>
36 public:
37 
38  /// @brief Handle to a compiled module.
39  using ModuleHandleT = typename BaseLayerT::ObjHandleT;
40 
41  /// @brief Construct an IRCompileLayer with the given BaseLayer, which must
42  /// implement the ObjectLayer concept.
43  IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile)
44  : BaseLayer(BaseLayer), Compile(std::move(Compile)) {}
45 
46  /// @brief Get a reference to the compiler functor.
47  CompileFtor& getCompiler() { return Compile; }
48 
49  /// @brief Compile the module, and add the resulting object to the base layer
50  /// along with the given memory manager and symbol resolver.
51  ///
52  /// @return A handle for the added module.
54  addModule(std::shared_ptr<Module> M,
55  std::shared_ptr<JITSymbolResolver> Resolver) {
56  using CompileResult = decltype(Compile(*M));
57  auto Obj = std::make_shared<CompileResult>(Compile(*M));
58  return BaseLayer.addObject(std::move(Obj), std::move(Resolver));
59  }
60 
61  /// @brief Remove the module associated with the handle H.
63  return BaseLayer.removeObject(H);
64  }
65 
66  /// @brief Search for the given named symbol.
67  /// @param Name The name of the symbol to search for.
68  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
69  /// @return A handle for the given named symbol, if it exists.
70  JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
71  return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
72  }
73 
74  /// @brief Get the address of the given symbol in compiled module represented
75  /// by the handle H. This call is forwarded to the base layer's
76  /// implementation.
77  /// @param H The handle for the module to search in.
78  /// @param Name The name of the symbol to search for.
79  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
80  /// @return A handle for the given named symbol, if it is found in the
81  /// given module.
82  JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
83  bool ExportedSymbolsOnly) {
84  return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
85  }
86 
87  /// @brief Immediately emit and finalize the module represented by the given
88  /// handle.
89  /// @param H Handle for module to emit/finalize.
91  return BaseLayer.emitAndFinalize(H);
92  }
93 
94 private:
95  BaseLayerT &BaseLayer;
96  CompileFtor Compile;
97 };
98 
99 } // end namespace orc
100 
101 } // end namespace llvm
102 
103 #endif // LLVM_EXECUTIONENGINE_ORC_IRCOMPILINGLAYER_H
Represents a symbol in the JIT.
Definition: JITSymbol.h:122
Expected< ModuleHandleT > addModule(std::shared_ptr< Module > M, std::shared_ptr< JITSymbolResolver > Resolver)
Compile the module, and add the resulting object to the base layer along with the given memory manage...
Error removeModule(ModuleHandleT H)
Remove the module associated with the handle H.
CompileFtor & getCompiler()
Get a reference to the compiler functor.
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
const char * Name
Eager IR compiling layer.
#define H(x, y, z)
Definition: MD5.cpp:57
IRCompileLayer(BaseLayerT &BaseLayer, CompileFtor Compile)
Construct an IRCompileLayer with the given BaseLayer, which must implement the ObjectLayer concept...
Error emitAndFinalize(ModuleHandleT H)
Immediately emit and finalize the module represented by the given handle.
typename ObjectLayerT::ObjHandleT ModuleHandleT
Handle to a compiled module.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, bool ExportedSymbolsOnly)
Get the address of the given symbol in compiled module represented by the handle H.