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 16 #include "llvm/ExecutionEngine/JITSymbol.h" 17 #include "llvm/ExecutionEngine/Orc/Layer.h" 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>( 31 ThreadSafeModule, MaterializationResponsibility &R)>; 32 33 IRTransformLayer(ExecutionSession &ES, IRLayer &BaseLayer, 34 TransformFunction Transform = identityTransform); 35 36 void setTransform(TransformFunction Transform) { 37 this->Transform = std::move(Transform); 38 } 39 40 void emit(MaterializationResponsibility R, ThreadSafeModule TSM) override; 41 42 static ThreadSafeModule identityTransform(ThreadSafeModule TSM, 43 MaterializationResponsibility &R) { 44 return TSM; 45 } 46 47 private: 48 IRLayer &BaseLayer; 49 TransformFunction Transform; 50 }; 51 52 /// IR mutating layer. 53 /// 54 /// This layer applies a user supplied transform to each module that is added, 55 /// then adds the transformed module to the layer below. 56 template <typename BaseLayerT, typename TransformFtor> 57 class LegacyIRTransformLayer { 58 public: 59 60 /// Construct an LegacyIRTransformLayer with the given BaseLayer 61 LLVM_ATTRIBUTE_DEPRECATED( 62 LegacyIRTransformLayer(BaseLayerT &BaseLayer, 63 TransformFtor Transform = TransformFtor()), 64 "ORCv1 layers (layers with the 'Legacy' prefix) are deprecated. Please " 65 "use " 66 "the ORCv2 IRTransformLayer instead"); 67 68 /// Legacy layer constructor with deprecation acknowledgement. 69 LegacyIRTransformLayer(ORCv1DeprecationAcknowledgement, BaseLayerT &BaseLayer, 70 TransformFtor Transform = TransformFtor()) 71 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {} 72 73 /// Apply the transform functor to the module, then add the module to 74 /// the layer below, along with the memory manager and symbol resolver. 75 /// 76 /// @return A handle for the added modules. 77 Error addModule(VModuleKey K, std::unique_ptr<Module> M) { 78 return BaseLayer.addModule(std::move(K), Transform(std::move(M))); 79 } 80 81 /// Remove the module associated with the VModuleKey K. 82 Error removeModule(VModuleKey K) { return BaseLayer.removeModule(K); } 83 84 /// Search for the given named symbol. 85 /// @param Name The name of the symbol to search for. 86 /// @param ExportedSymbolsOnly If true, search only for exported symbols. 87 /// @return A handle for the given named symbol, if it exists. 88 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) { 89 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly); 90 } 91 92 /// Get the address of the given symbol in the context of the module 93 /// represented by the VModuleKey K. This call is forwarded to the base 94 /// layer's implementation. 95 /// @param K The VModuleKey for the module to search in. 96 /// @param Name The name of the symbol to search for. 97 /// @param ExportedSymbolsOnly If true, search only for exported symbols. 98 /// @return A handle for the given named symbol, if it is found in the 99 /// given module. 100 JITSymbol findSymbolIn(VModuleKey K, const std::string &Name, 101 bool ExportedSymbolsOnly) { 102 return BaseLayer.findSymbolIn(K, Name, ExportedSymbolsOnly); 103 } 104 105 /// Immediately emit and finalize the module represented by the given 106 /// VModuleKey. 107 /// @param K The VModuleKey for the module to emit/finalize. 108 Error emitAndFinalize(VModuleKey K) { return BaseLayer.emitAndFinalize(K); } 109 110 /// Access the transform functor directly. 111 TransformFtor& getTransform() { return Transform; } 112 113 /// Access the mumate functor directly. 114 const TransformFtor& getTransform() const { return Transform; } 115 116 private: 117 BaseLayerT &BaseLayer; 118 TransformFtor Transform; 119 }; 120 121 template <typename BaseLayerT, typename TransformFtor> 122 LegacyIRTransformLayer<BaseLayerT, TransformFtor>::LegacyIRTransformLayer( 123 BaseLayerT &BaseLayer, TransformFtor Transform) 124 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {} 125 126 } // end namespace orc 127 } // end namespace llvm 128 129 #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H 130