LLVM  9.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 class IRTransformLayer : public IRLayer {
26 public:
27  using TransformFunction = std::function<Expected<ThreadSafeModule>(
29 
32 
33  void setTransform(TransformFunction Transform) {
34  this->Transform = std::move(Transform);
35  }
36 
38 
39  static ThreadSafeModule
42  return TSM;
43  }
44 
45 private:
46  IRLayer &BaseLayer;
47  TransformFunction Transform;
48 };
49 
50 /// IR mutating layer.
51 ///
52 /// This layer applies a user supplied transform to each module that is added,
53 /// then adds the transformed module to the layer below.
54 template <typename BaseLayerT, typename TransformFtor>
56 public:
57 
58  /// Construct an LegacyIRTransformLayer with the given BaseLayer
59  LegacyIRTransformLayer(BaseLayerT &BaseLayer,
60  TransformFtor Transform = TransformFtor())
61  : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
62 
63  /// Apply the transform functor to the module, then add the module to
64  /// the layer below, along with the memory manager and symbol resolver.
65  ///
66  /// @return A handle for the added modules.
67  Error addModule(VModuleKey K, std::unique_ptr<Module> M) {
68  return BaseLayer.addModule(std::move(K), Transform(std::move(M)));
69  }
70 
71  /// Remove the module associated with the VModuleKey K.
72  Error removeModule(VModuleKey K) { return BaseLayer.removeModule(K); }
73 
74  /// Search for the given named symbol.
75  /// @param Name The name of the symbol to search for.
76  /// @param ExportedSymbolsOnly If true, search only for exported symbols.
77  /// @return A handle for the given named symbol, if it exists.
78  JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
79  return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
80  }
81 
82  /// Get the address of the given symbol in the context of the module
83  /// represented by the VModuleKey K. This call is forwarded to the base
84  /// layer's implementation.
85  /// @param K The VModuleKey for the module to search in.
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 is found in the
89  /// given module.
90  JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
91  bool ExportedSymbolsOnly) {
92  return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
93  }
94 
95  /// Immediately emit and finalize the module represented by the given
96  /// VModuleKey.
97  /// @param K The VModuleKey for the module to emit/finalize.
98  Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
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_IRTRANSFORMLAYER_H
Represents a symbol in the JIT.
Definition: JITSymbol.h:237
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)
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:39
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:154
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
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:696
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:157