LLVM  8.0.1
IRTransformLayer.h
Go to the documentation of this file.
1 //===- IRTransformLayer.h - Run all IR through a 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 IR passed in through a user supplied functor.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
16 
19 #include <memory>
20 #include <string>
21 
22 namespace llvm {
23 class Module;
24 namespace orc {
25 
26 class IRTransformLayer : public IRLayer {
27 public:
28  using TransformFunction = std::function<Expected<ThreadSafeModule>(
30 
33 
34  void setTransform(TransformFunction Transform) {
35  this->Transform = std::move(Transform);
36  }
37 
39 
40  static ThreadSafeModule
43  return TSM;
44  }
45 
46 private:
47  IRLayer &BaseLayer;
48  TransformFunction Transform;
49 };
50 
51 /// IR mutating layer.
52 ///
53 /// This layer applies a user supplied transform to each module that is added,
54 /// then adds the transformed module to the layer below.
55 template <typename BaseLayerT, typename TransformFtor>
57 public:
58 
59  /// Construct an LegacyIRTransformLayer with the given BaseLayer
60  LegacyIRTransformLayer(BaseLayerT &BaseLayer,
61  TransformFtor Transform = TransformFtor())
62  : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
63 
64  /// Apply the transform functor to the module, then add the module to
65  /// the layer below, along with the memory manager and symbol resolver.
66  ///
67  /// @return A handle for the added modules.
68  Error addModule(VModuleKey K, std::unique_ptr<Module> M) {
69  return BaseLayer.addModule(std::move(K), Transform(std::move(M)));
70  }
71 
72  /// Remove the module associated with the VModuleKey K.
73  Error removeModule(VModuleKey K) { return BaseLayer.removeModule(K); }
74 
75  /// Search for the given named symbol.
76  /// @param Name The name of the symbol to search for.
77  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
78  /// @return A handle for the given named symbol, if it exists.
79  JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
80  return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
81  }
82 
83  /// Get the address of the given symbol in the context of the module
84  /// represented by the VModuleKey K. This call is forwarded to the base
85  /// layer's implementation.
86  /// @param K The VModuleKey for the module to search in.
87  /// @param Name The name of the symbol to search for.
88  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
89  /// @return A handle for the given named symbol, if it is found in the
90  /// given module.
91  JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
92  bool ExportedSymbolsOnly) {
93  return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
94  }
95 
96  /// Immediately emit and finalize the module represented by the given
97  /// VModuleKey.
98  /// @param K The VModuleKey for the module to emit/finalize.
99  Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
100 
101  /// Access the transform functor directly.
102  TransformFtor& getTransform() { return Transform; }
103 
104  /// Access the mumate functor directly.
105  const TransformFtor& getTransform() const { return Transform; }
106 
107 private:
108  BaseLayerT &BaseLayer;
109  TransformFtor Transform;
110 };
111 
112 } // end namespace orc
113 } // end namespace llvm
114 
115 #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
Represents a symbol in the JIT.
Definition: JITSymbol.h:238
This class represents lattice values for constants.
Definition: AllocatorList.h:24
IRTransformLayer(ExecutionSession &ES, IRLayer &BaseLayer, TransformFunction Transform=identityTransform)
static ThreadSafeModule identityTransform(ThreadSafeModule TSM, const MaterializationResponsibility &R)
TransformFtor & getTransform()
Access the transform functor directly.
uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:40
JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly)
Search for the given named symbol.
Error emitAndFinalize(VModuleKey K)
Immediately emit and finalize the module represented by the given VModuleKey.
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
const TransformFtor & getTransform() const
Access the mumate functor directly.
Error removeModule(VModuleKey K)
Remove the module associated with the VModuleKey K.
JITSymbol findSymbolIn(VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly)
Get the address of the given symbol in the context of the module represented by the VModuleKey K...
An LLVM Module together with a shared ThreadSafeContext.
Interface for layers that accept LLVM IR.
Definition: Layer.h:26
std::function< Expected< ThreadSafeModule >(ThreadSafeModule, const MaterializationResponsibility &R)> TransformFunction
Error addModule(VModuleKey K, std::unique_ptr< Module > M)
Apply the transform functor to the module, then add the module to the layer below, along with the memory manager and symbol resolver.
An ExecutionSession represents a running JIT program.
Definition: Core.h:697
LegacyIRTransformLayer(BaseLayerT &BaseLayer, TransformFtor Transform=TransformFtor())
Construct an LegacyIRTransformLayer with the given BaseLayer.
void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override
Emit should materialize the given IR.
void setTransform(TransformFunction Transform)
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158