1 //===- IRTransformLayer.h - Run all IR through a functor --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Run all IR passed in through a user supplied functor.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
15 #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
17 #include "llvm/ExecutionEngine/JITSymbol.h"
18 #include "llvm/ExecutionEngine/Orc/Layer.h"
26 class IRTransformLayer : public IRLayer {
28 using TransformFunction = std::function<Expected<ThreadSafeModule>(
29 ThreadSafeModule, const MaterializationResponsibility &R)>;
31 IRTransformLayer(ExecutionSession &ES, IRLayer &BaseLayer,
32 TransformFunction Transform = identityTransform);
34 void setTransform(TransformFunction Transform) {
35 this->Transform = std::move(Transform);
38 void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override;
40 static ThreadSafeModule
41 identityTransform(ThreadSafeModule TSM,
42 const MaterializationResponsibility &R) {
48 TransformFunction Transform;
51 /// IR mutating layer.
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>
56 class LegacyIRTransformLayer {
59 /// Construct an LegacyIRTransformLayer with the given BaseLayer
60 LegacyIRTransformLayer(BaseLayerT &BaseLayer,
61 TransformFtor Transform = TransformFtor())
62 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
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.
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)));
72 /// Remove the module associated with the VModuleKey K.
73 Error removeModule(VModuleKey K) { return BaseLayer.removeModule(K); }
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);
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
91 JITSymbol findSymbolIn(VModuleKey K, const std::string &Name,
92 bool ExportedSymbolsOnly) {
93 return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly);
96 /// Immediately emit and finalize the module represented by the given
98 /// @param K The VModuleKey for the module to emit/finalize.
99 Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); }
101 /// Access the transform functor directly.
102 TransformFtor& getTransform() { return Transform; }
104 /// Access the mumate functor directly.
105 const TransformFtor& getTransform() const { return Transform; }
108 BaseLayerT &BaseLayer;
109 TransformFtor Transform;
112 } // end namespace orc
113 } // end namespace llvm
115 #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H