LLVM  10.0.0svn
IRTransformLayer.h
Go to the documentation of this file.
1 //===- IRTransformLayer.h - Run all IR through a 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 IR passed in through a user supplied functor.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
14 #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
15 
18 #include <memory>
19 #include <string>
20 
21 namespace llvm {
22 class Module;
23 namespace orc {
24 
25 /// A layer that applies a transform to emitted modules.
26 /// The transform function is responsible for locking the ThreadSafeContext
27 /// before operating on the module.
28 class IRTransformLayer : public IRLayer {
29 public:
30  using TransformFunction = std::function<Expected<ThreadSafeModule>(
32 
35 
36  void setTransform(TransformFunction Transform) {
37  this->Transform = std::move(Transform);
38  }
39 
41 
42  static ThreadSafeModule
45  return TSM;
46  }
47 
48 private:
49  IRLayer &BaseLayer;
50  TransformFunction Transform;
51 };
52 
53 /// IR mutating layer.
54 ///
55 /// This layer applies a user supplied transform to each module that is added,
56 /// then adds the transformed module to the layer below.
57 template <typename BaseLayerT, typename TransformFtor>
59 public:
60 
61  /// Construct an LegacyIRTransformLayer with the given BaseLayer
63  LegacyIRTransformLayer(BaseLayerT &BaseLayer,
64  TransformFtor Transform = TransformFtor()),
65  "ORCv1 layers (layers with the 'Legacy' prefix) are deprecated. Please "
66  "use "
67  "the ORCv2 IRTransformLayer instead");
68 
69  /// Legacy layer constructor with deprecation acknowledgement.
71  TransformFtor Transform = TransformFtor())
72  : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
73 
74  /// Apply the transform functor to the module, then add the module to
75  /// the layer below, along with the memory manager and symbol resolver.
76  ///
77  /// @return A handle for the added modules.
78  Error addModule(VModuleKey K, std::unique_ptr<Module> M) {
79  return BaseLayer.addModule(std::move(K), Transform(std::move(M)));
80  }
81 
82  /// Remove the module associated with the VModuleKey K.
83  Error removeModule(VModuleKey K) { return BaseLayer.removeModule(K); }
84 
85  /// Search for the given named symbol.
86  /// @param Name The name of the symbol to search for.
87  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
88  /// @return A handle for the given named symbol, if it exists.
89  JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
90  return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
91  }
92 
93  /// Get the address of the given symbol in the context of the module
94  /// represented by the VModuleKey K. This call is forwarded to the base
95  /// layer's implementation.
96  /// @param K The VModuleKey for the module to search in.
97  /// @param Name The name of the symbol to search for.
98  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
99  /// @return A handle for the given named symbol, if it is found in the
100  /// given module.
101  JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
102  bool ExportedSymbolsOnly) {
103  return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
104  }
105 
106  /// Immediately emit and finalize the module represented by the given
107  /// VModuleKey.
108  /// @param K The VModuleKey for the module to emit/finalize.
109  Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
110 
111  /// Access the transform functor directly.
112  TransformFtor& getTransform() { return Transform; }
113 
114  /// Access the mumate functor directly.
115  const TransformFtor& getTransform() const { return Transform; }
116 
117 private:
118  BaseLayerT &BaseLayer;
119  TransformFtor Transform;
120 };
121 
122 template <typename BaseLayerT, typename TransformFtor>
124  BaseLayerT &BaseLayer, TransformFtor Transform)
125  : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
126 
127 } // end namespace orc
128 } // end namespace llvm
129 
130 #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
Represents a symbol in the JIT.
Definition: JITSymbol.h:219
This class represents lattice values for constants.
Definition: AllocatorList.h:23
IRTransformLayer(ExecutionSession &ES, IRLayer &BaseLayer, TransformFunction Transform=identityTransform)
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static ThreadSafeModule identityTransform(ThreadSafeModule TSM, const MaterializationResponsibility &R)
A layer that applies a transform to emitted modules.
TransformFtor & getTransform()
Access the transform functor directly.
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.
Definition: BitVector.h:937
Tracks responsibility for materialization, and mediates interactions between MaterializationUnits and...
Definition: Core.h:171
ORCv1DeprecationAcknowledgement
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:25
#define LLVM_ATTRIBUTE_DEPRECATED(decl, message)
Definition: Compiler.h:296
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:761
uint8_t uint64_t VModuleKey
VModuleKey provides a unique identifier (allocated and managed by ExecutionSessions) for a module add...
Definition: Core.h:42
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:157
LegacyIRTransformLayer(ORCv1DeprecationAcknowledgement, BaseLayerT &BaseLayer, TransformFtor Transform=TransformFtor())
Legacy layer constructor with deprecation acknowledgement.