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