ObjectTransformLayer.h revision 360784
1//===- ObjectTransformLayer.h - Run all objects through functor -*- 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// Run all objects passed in through a user supplied functor.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
14#define LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
15
16#include "llvm/ExecutionEngine/JITSymbol.h"
17#include "llvm/ExecutionEngine/Orc/Layer.h"
18#include <algorithm>
19#include <memory>
20#include <string>
21
22namespace llvm {
23namespace orc {
24
25class ObjectTransformLayer : public ObjectLayer {
26public:
27  using TransformFunction =
28      std::function<Expected<std::unique_ptr<MemoryBuffer>>(
29          std::unique_ptr<MemoryBuffer>)>;
30
31  ObjectTransformLayer(ExecutionSession &ES, ObjectLayer &BaseLayer,
32                       TransformFunction Transform = TransformFunction());
33
34  void emit(MaterializationResponsibility R,
35            std::unique_ptr<MemoryBuffer> O) override;
36
37  void setTransform(TransformFunction Transform) {
38    this->Transform = std::move(Transform);
39  }
40
41private:
42  ObjectLayer &BaseLayer;
43  TransformFunction Transform;
44};
45
46/// Object mutating layer.
47///
48///   This layer accepts sets of ObjectFiles (via addObject). It
49/// immediately applies the user supplied functor to each object, then adds
50/// the set of transformed objects to the layer below.
51template <typename BaseLayerT, typename TransformFtor>
52class LegacyObjectTransformLayer {
53public:
54  /// Construct an ObjectTransformLayer with the given BaseLayer
55  LLVM_ATTRIBUTE_DEPRECATED(
56      LegacyObjectTransformLayer(BaseLayerT &BaseLayer,
57                                 TransformFtor Transform = TransformFtor()),
58      "ORCv1 layers (layers with the 'Legacy' prefix) are deprecated. Please "
59      "use "
60      "the ORCv2 ObjectTransformLayer instead");
61
62  /// Legacy layer constructor with deprecation acknowledgement.
63  LegacyObjectTransformLayer(ORCv1DeprecationAcknowledgement,
64                             BaseLayerT &BaseLayer,
65                             TransformFtor Transform = TransformFtor())
66      : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
67
68  /// Apply the transform functor to each object in the object set, then
69  ///        add the resulting set of objects to the base layer, along with the
70  ///        memory manager and symbol resolver.
71  ///
72  /// @return A handle for the added objects.
73  template <typename ObjectPtr> Error addObject(VModuleKey K, ObjectPtr Obj) {
74    return BaseLayer.addObject(std::move(K), Transform(std::move(Obj)));
75  }
76
77  /// Remove the object set associated with the VModuleKey K.
78  Error removeObject(VModuleKey K) { return BaseLayer.removeObject(K); }
79
80  /// Search for the given named symbol.
81  /// @param Name The name of the symbol to search for.
82  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
83  /// @return A handle for the given named symbol, if it exists.
84  JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
85    return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
86  }
87
88  /// Get the address of the given symbol in the context of the set of
89  ///        objects represented by the VModuleKey K. This call is forwarded to
90  ///        the base layer's implementation.
91  /// @param K The VModuleKey associated with the object set to search in.
92  /// @param Name The name of the symbol to search for.
93  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
94  /// @return A handle for the given named symbol, if it is found in the
95  ///         given object set.
96  JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
97                         bool ExportedSymbolsOnly) {
98    return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
99  }
100
101  /// Immediately emit and finalize the object set represented by the
102  ///        given VModuleKey K.
103  Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
104
105  /// Map section addresses for the objects associated with the
106  /// VModuleKey K.
107  void mapSectionAddress(VModuleKey K, const void *LocalAddress,
108                         JITTargetAddress TargetAddr) {
109    BaseLayer.mapSectionAddress(K, LocalAddress, TargetAddr);
110  }
111
112  /// Access the transform functor directly.
113  TransformFtor &getTransform() { return Transform; }
114
115  /// Access the mumate functor directly.
116  const TransformFtor &getTransform() const { return Transform; }
117
118private:
119  BaseLayerT &BaseLayer;
120  TransformFtor Transform;
121};
122
123template <typename BaseLayerT, typename TransformFtor>
124LegacyObjectTransformLayer<BaseLayerT, TransformFtor>::
125    LegacyObjectTransformLayer(BaseLayerT &BaseLayer, TransformFtor Transform)
126    : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
127
128} // end namespace orc
129} // end namespace llvm
130
131#endif // LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
132