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
17#include "llvm/ExecutionEngine/JITSymbol.h"
18#include <memory>
19#include <string>
20
21namespace llvm {
22class Module;
23namespace 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.
29template <typename BaseLayerT, typename TransformFtor>
30class IRTransformLayer {
31public:
32
33  /// @brief Handle to a set of added modules.
34  using ModuleHandleT = typename BaseLayerT::ModuleHandleT;
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.
45  Expected<ModuleHandleT>
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.
78  Error emitAndFinalize(ModuleHandleT H) {
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
88private:
89  BaseLayerT &BaseLayer;
90  TransformFtor Transform;
91};
92
93} // end namespace orc
94} // end namespace llvm
95
96#endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
97