LLVM  5.0.0
IRTransformLayer.h
Go to the documentation of this file.
1 //===- IRTransformLayer.h - Run all IR through a functor --------*- 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 // Run all IR passed in through a user supplied functor.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
16 
18 #include <memory>
19 #include <string>
20 
21 namespace llvm {
22 class Module;
23 namespace orc {
24 
25 /// @brief IR mutating layer.
26 ///
27 /// This layer applies a user supplied transform to each module that is added,
28 /// then adds the transformed module to the layer below.
29 template <typename BaseLayerT, typename TransformFtor>
31 public:
32 
33  /// @brief Handle to a set of added modules.
35 
36  /// @brief Construct an IRTransformLayer with the given BaseLayer
37  IRTransformLayer(BaseLayerT &BaseLayer,
38  TransformFtor Transform = TransformFtor())
39  : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
40 
41  /// @brief Apply the transform functor to the module, then add the module to
42  /// the layer below, along with the memory manager and symbol resolver.
43  ///
44  /// @return A handle for the added modules.
46  addModule(std::shared_ptr<Module> M,
47  std::shared_ptr<JITSymbolResolver> Resolver) {
48  return BaseLayer.addModule(Transform(std::move(M)), std::move(Resolver));
49  }
50 
51  /// @brief Remove the module associated with the handle H.
52  Error removeModule(ModuleHandleT H) { return BaseLayer.removeModule(H); }
53 
54  /// @brief Search for the given named symbol.
55  /// @param Name The name of the symbol to search for.
56  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
57  /// @return A handle for the given named symbol, if it exists.
58  JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
59  return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
60  }
61 
62  /// @brief Get the address of the given symbol in the context of the module
63  /// represented by the handle H. This call is forwarded to the base
64  /// layer's implementation.
65  /// @param H The handle for the module to search in.
66  /// @param Name The name of the symbol to search for.
67  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
68  /// @return A handle for the given named symbol, if it is found in the
69  /// given module.
70  JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
71  bool ExportedSymbolsOnly) {
72  return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
73  }
74 
75  /// @brief Immediately emit and finalize the module represented by the given
76  /// handle.
77  /// @param H Handle for module to emit/finalize.
79  return BaseLayer.emitAndFinalize(H);
80  }
81 
82  /// @brief Access the transform functor directly.
83  TransformFtor& getTransform() { return Transform; }
84 
85  /// @brief Access the mumate functor directly.
86  const TransformFtor& getTransform() const { return Transform; }
87 
88 private:
89  BaseLayerT &BaseLayer;
90  TransformFtor Transform;
91 };
92 
93 } // end namespace orc
94 } // end namespace llvm
95 
96 #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
Represents a symbol in the JIT.
Definition: JITSymbol.h:122
Error removeModule(ModuleHandleT H)
Remove the module associated with the handle H.
const TransformFtor & getTransform() const
Access the mumate functor directly.
JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name, bool ExportedSymbolsOnly)
Get the address of the given symbol in the context of the module represented by the handle H...
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
const char * Name
typename BaseLayerT::ModuleHandleT ModuleHandleT
Handle to a set of added modules.
#define H(x, y, z)
Definition: MD5.cpp:57
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
unsigned ModuleHandleT
IRTransformLayer(BaseLayerT &BaseLayer, TransformFtor Transform=TransformFtor())
Construct an IRTransformLayer with the given BaseLayer.
Expected< ModuleHandleT > addModule(std::shared_ptr< Module > M, std::shared_ptr< JITSymbolResolver > Resolver)
Apply the transform functor to the module, then add the module to the layer below, along with the memory manager and symbol resolver.
Error emitAndFinalize(ModuleHandleT H)
Immediately emit and finalize the module represented by the given handle.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
TransformFtor & getTransform()
Access the transform functor directly.