blob: de4603f98f83eced39429009ca2b6b16665481d7 [file] [log] [blame]
Yi Kong7bf2a682019-04-18 17:30:49 -07001//===- 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);
33
34 void emit(MaterializationResponsibility R,
35 std::unique_ptr<MemoryBuffer> O) override;
36
37private:
38 ObjectLayer &BaseLayer;
39 TransformFunction Transform;
40};
41
42/// Object mutating layer.
43///
44/// This layer accepts sets of ObjectFiles (via addObject). It
45/// immediately applies the user supplied functor to each object, then adds
46/// the set of transformed objects to the layer below.
47template <typename BaseLayerT, typename TransformFtor>
48class LegacyObjectTransformLayer {
49public:
50 /// Construct an ObjectTransformLayer with the given BaseLayer
51 LegacyObjectTransformLayer(BaseLayerT &BaseLayer,
52 TransformFtor Transform = TransformFtor())
53 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
54
55 /// Apply the transform functor to each object in the object set, then
56 /// add the resulting set of objects to the base layer, along with the
57 /// memory manager and symbol resolver.
58 ///
59 /// @return A handle for the added objects.
60 template <typename ObjectPtr> Error addObject(VModuleKey K, ObjectPtr Obj) {
61 return BaseLayer.addObject(std::move(K), Transform(std::move(Obj)));
62 }
63
64 /// Remove the object set associated with the VModuleKey K.
65 Error removeObject(VModuleKey K) { return BaseLayer.removeObject(K); }
66
67 /// Search for the given named symbol.
68 /// @param Name The name of the symbol to search for.
69 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
70 /// @return A handle for the given named symbol, if it exists.
71 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
72 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
73 }
74
75 /// Get the address of the given symbol in the context of the set of
76 /// objects represented by the VModuleKey K. This call is forwarded to
77 /// the base layer's implementation.
78 /// @param K The VModuleKey associated with the object set to search in.
79 /// @param Name The name of the symbol to search for.
80 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
81 /// @return A handle for the given named symbol, if it is found in the
82 /// given object set.
83 JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
84 bool ExportedSymbolsOnly) {
85 return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
86 }
87
88 /// Immediately emit and finalize the object set represented by the
89 /// given VModuleKey K.
90 Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
91
92 /// Map section addresses for the objects associated with the
93 /// VModuleKey K.
94 void mapSectionAddress(VModuleKey K, const void *LocalAddress,
95 JITTargetAddress TargetAddr) {
96 BaseLayer.mapSectionAddress(K, LocalAddress, TargetAddr);
97 }
98
99 /// Access the transform functor directly.
100 TransformFtor &getTransform() { return Transform; }
101
102 /// Access the mumate functor directly.
103 const TransformFtor &getTransform() const { return Transform; }
104
105private:
106 BaseLayerT &BaseLayer;
107 TransformFtor Transform;
108};
109
110} // end namespace orc
111} // end namespace llvm
112
113#endif // LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H