1 //===- ObjectTransformLayer.h - Run all objects through functor -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Run all objects passed in through a user supplied functor.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
17 #include "llvm/ExecutionEngine/JITSymbol.h"
18 #include "llvm/ExecutionEngine/Orc/Layer.h"
26 class ObjectTransformLayer2 : public ObjectLayer {
28 using TransformFunction =
29 std::function<Expected<std::unique_ptr<MemoryBuffer>>(
30 std::unique_ptr<MemoryBuffer>)>;
32 ObjectTransformLayer2(ExecutionSession &ES, ObjectLayer &BaseLayer,
33 TransformFunction Transform);
35 void emit(MaterializationResponsibility R, VModuleKey K,
36 std::unique_ptr<MemoryBuffer> O) override;
39 ObjectLayer &BaseLayer;
40 TransformFunction Transform;
43 /// Object mutating layer.
45 /// This layer accepts sets of ObjectFiles (via addObject). It
46 /// immediately applies the user supplied functor to each object, then adds
47 /// the set of transformed objects to the layer below.
48 template <typename BaseLayerT, typename TransformFtor>
49 class ObjectTransformLayer {
51 /// Construct an ObjectTransformLayer with the given BaseLayer
52 ObjectTransformLayer(BaseLayerT &BaseLayer,
53 TransformFtor Transform = TransformFtor())
54 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
56 /// Apply the transform functor to each object in the object set, then
57 /// add the resulting set of objects to the base layer, along with the
58 /// memory manager and symbol resolver.
60 /// @return A handle for the added objects.
61 template <typename ObjectPtr> Error addObject(VModuleKey K, ObjectPtr Obj) {
62 return BaseLayer.addObject(std::move(K), Transform(std::move(Obj)));
65 /// Remove the object set associated with the VModuleKey K.
66 Error removeObject(VModuleKey K) { return BaseLayer.removeObject(K); }
68 /// Search for the given named symbol.
69 /// @param Name The name of the symbol to search for.
70 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
71 /// @return A handle for the given named symbol, if it exists.
72 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
73 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
76 /// Get the address of the given symbol in the context of the set of
77 /// objects represented by the VModuleKey K. This call is forwarded to
78 /// the base layer's implementation.
79 /// @param K The VModuleKey associated with the object set to search in.
80 /// @param Name The name of the symbol to search for.
81 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
82 /// @return A handle for the given named symbol, if it is found in the
84 JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
85 bool ExportedSymbolsOnly) {
86 return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
89 /// Immediately emit and finalize the object set represented by the
90 /// given VModuleKey K.
91 Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
93 /// Map section addresses for the objects associated with the
95 void mapSectionAddress(VModuleKey K, const void *LocalAddress,
96 JITTargetAddress TargetAddr) {
97 BaseLayer.mapSectionAddress(K, LocalAddress, TargetAddr);
100 /// Access the transform functor directly.
101 TransformFtor &getTransform() { return Transform; }
103 /// Access the mumate functor directly.
104 const TransformFtor &getTransform() const { return Transform; }
107 BaseLayerT &BaseLayer;
108 TransformFtor Transform;
111 } // end namespace orc
112 } // end namespace llvm
114 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H