LLVM  8.0.1
ObjectTransformLayer.h
Go to the documentation of this file.
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 
19 #include <algorithm>
20 #include <memory>
21 #include <string>
22 
23 namespace llvm {
24 namespace orc {
25 
27 public:
28  using TransformFunction =
29  std::function<Expected<std::unique_ptr<MemoryBuffer>>(
30  std::unique_ptr<MemoryBuffer>)>;
31 
33  TransformFunction Transform);
34 
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>
50 public:
51  /// Construct an ObjectTransformLayer with the given BaseLayer
52  LegacyObjectTransformLayer(BaseLayerT &BaseLayer,
53  TransformFtor Transform = TransformFtor())
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.
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.
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.
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.
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.
91  Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
92 
93  /// Map section addresses for the objects associated with the
94  /// VModuleKey K.
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.
101  TransformFtor &getTransform() { return Transform; }
102 
103  /// Access the mumate functor directly.
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
void mapSectionAddress(VModuleKey K, const void *LocalAddress, JITTargetAddress TargetAddr)
Map section addresses for the objects associated with the VModuleKey K.
Represents a symbol in the JIT.
Definition: JITSymbol.h:238
JITSymbol findSymbolIn(VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly)
Get the address of the given symbol in the context of the set of objects represented by the VModuleKe...
This class represents lattice values for constants.
Definition: AllocatorList.h:24
const TransformFtor & getTransform() const
Access the mumate functor directly.
Error removeObject(VModuleKey K)
Remove the object set associated with the VModuleKey K.
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
Error emitAndFinalize(VModuleKey K)
Immediately emit and finalize the object set represented by the given VModuleKey K.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Definition: BitVector.h:938
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:155
uint64_t JITTargetAddress
Represents an address in the target process&#39;s address space.
Definition: JITSymbol.h:41
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
TransformFtor & getTransform()
Access the transform functor directly.
Error addObject(VModuleKey K, ObjectPtr Obj)
Apply the transform functor to each object in the object set, then add the resulting set of objects t...
void emit(MaterializationResponsibility R, std::unique_ptr< MemoryBuffer > O) override
Emit should materialize the given IR.
An ExecutionSession represents a running JIT program.
Definition: Core.h:697
std::function< Expected< std::unique_ptr< MemoryBuffer > >(std::unique_ptr< MemoryBuffer >)> TransformFunction
LegacyObjectTransformLayer(BaseLayerT &BaseLayer, TransformFtor Transform=TransformFtor())
Construct an ObjectTransformLayer with the given BaseLayer.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
Interface for Layers that accept object files.
Definition: Layer.h:114
ObjectTransformLayer(ExecutionSession &ES, ObjectLayer &BaseLayer, TransformFunction Transform)