1 //===- ObjectTransformLayer.h - Run all objects through 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 objects passed in through a user supplied functor.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
16 
17 #include "llvm/ExecutionEngine/JITSymbol.h"
18 #include "llvm/ExecutionEngine/Orc/Layer.h"
19 #include <algorithm>
20 #include <memory>
21 #include <string>
22 
23 namespace llvm {
24 namespace orc {
25 
26 class ObjectTransformLayer : public ObjectLayer {
27 public:
28   using TransformFunction =
29       std::function<Expected<std::unique_ptr<MemoryBuffer>>(
30           std::unique_ptr<MemoryBuffer>)>;
31 
32   ObjectTransformLayer(ExecutionSession &ES, ObjectLayer &BaseLayer,
33                        TransformFunction Transform);
34 
35   void emit(MaterializationResponsibility R,
36             std::unique_ptr<MemoryBuffer> O) override;
37 
38 private:
39   ObjectLayer &BaseLayer;
40   TransformFunction Transform;
41 };
42 
43 /// Object mutating layer.
44 ///
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 LegacyObjectTransformLayer {
50 public:
51   /// Construct an ObjectTransformLayer with the given BaseLayer
52   LegacyObjectTransformLayer(BaseLayerT &BaseLayer,
53                              TransformFtor Transform = TransformFtor())
BaseLayer(BaseLayer)54       : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
55 
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.
59   ///
60   /// @return A handle for the added objects.
addObject(VModuleKey K,ObjectPtr Obj)61   template <typename ObjectPtr> Error addObject(VModuleKey K, ObjectPtr Obj) {
62     return BaseLayer.addObject(std::move(K), Transform(std::move(Obj)));
63   }
64 
65   /// Remove the object set associated with the VModuleKey K.
removeObject(VModuleKey K)66   Error removeObject(VModuleKey K) { return BaseLayer.removeObject(K); }
67 
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.
findSymbol(const std::string & Name,bool ExportedSymbolsOnly)72   JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
73     return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
74   }
75 
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
83   ///         given object set.
findSymbolIn(VModuleKey K,const std::string & Name,bool ExportedSymbolsOnly)84   JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
85                          bool ExportedSymbolsOnly) {
86     return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
87   }
88 
89   /// Immediately emit and finalize the object set represented by the
90   ///        given VModuleKey K.
emitAndFinalize(VModuleKey K)91   Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
92 
93   /// Map section addresses for the objects associated with the
94   /// VModuleKey K.
mapSectionAddress(VModuleKey K,const void * LocalAddress,JITTargetAddress TargetAddr)95   void mapSectionAddress(VModuleKey K, const void *LocalAddress,
96                          JITTargetAddress TargetAddr) {
97     BaseLayer.mapSectionAddress(K, LocalAddress, TargetAddr);
98   }
99 
100   /// Access the transform functor directly.
getTransform()101   TransformFtor &getTransform() { return Transform; }
102 
103   /// Access the mumate functor directly.
getTransform()104   const TransformFtor &getTransform() const { return Transform; }
105 
106 private:
107   BaseLayerT &BaseLayer;
108   TransformFtor Transform;
109 };
110 
111 } // end namespace orc
112 } // end namespace llvm
113 
114 #endif // LLVM_EXECUTIONENGINE_ORC_OBJECTTRANSFORMLAYER_H
115