1 //===----- IRTransformLayer.h - Run all IR through a functor ----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Run all IR passed in through a user supplied functor. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H 15 #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H 16 17 #include "JITSymbol.h" 18 19 namespace llvm { 20 namespace orc { 21 22 /// @brief IR mutating layer. 23 /// 24 /// This layer accepts sets of LLVM IR Modules (via addModuleSet). It 25 /// immediately applies the user supplied functor to each module, then adds 26 /// the set of transformed modules to the layer below. 27 template <typename BaseLayerT, typename TransformFtor> 28 class IRTransformLayer { 29 public: 30 /// @brief Handle to a set of added modules. 31 typedef typename BaseLayerT::ModuleSetHandleT ModuleSetHandleT; 32 33 /// @brief Construct an IRTransformLayer with the given BaseLayer 34 IRTransformLayer(BaseLayerT &BaseLayer, 35 TransformFtor Transform = TransformFtor()) BaseLayer(BaseLayer)36 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {} 37 38 /// @brief Apply the transform functor to each module in the module set, then 39 /// add the resulting set of modules to the base layer, along with the 40 /// memory manager and symbol resolver. 41 /// 42 /// @return A handle for the added modules. 43 template <typename ModuleSetT, typename MemoryManagerPtrT, 44 typename SymbolResolverPtrT> addModuleSet(ModuleSetT Ms,MemoryManagerPtrT MemMgr,SymbolResolverPtrT Resolver)45 ModuleSetHandleT addModuleSet(ModuleSetT Ms, 46 MemoryManagerPtrT MemMgr, 47 SymbolResolverPtrT Resolver) { 48 49 for (auto I = Ms.begin(), E = Ms.end(); I != E; ++I) 50 *I = Transform(std::move(*I)); 51 52 return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr), 53 std::move(Resolver)); 54 } 55 56 /// @brief Remove the module set associated with the handle H. removeModuleSet(ModuleSetHandleT H)57 void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeModuleSet(H); } 58 59 /// @brief Search for the given named symbol. 60 /// @param Name The name of the symbol to search for. 61 /// @param ExportedSymbolsOnly If true, search only for exported symbols. 62 /// @return A handle for the given named symbol, if it exists. findSymbol(const std::string & Name,bool ExportedSymbolsOnly)63 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) { 64 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly); 65 } 66 67 /// @brief Get the address of the given symbol in the context of the set of 68 /// modules represented by the handle H. This call is forwarded to the 69 /// base layer's implementation. 70 /// @param H The handle for the module set to search in. 71 /// @param Name The name of the symbol to search for. 72 /// @param ExportedSymbolsOnly If true, search only for exported symbols. 73 /// @return A handle for the given named symbol, if it is found in the 74 /// given module set. findSymbolIn(ModuleSetHandleT H,const std::string & Name,bool ExportedSymbolsOnly)75 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name, 76 bool ExportedSymbolsOnly) { 77 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly); 78 } 79 80 /// @brief Immediately emit and finalize the module set represented by the 81 /// given handle. 82 /// @param H Handle for module set to emit/finalize. emitAndFinalize(ModuleSetHandleT H)83 void emitAndFinalize(ModuleSetHandleT H) { 84 BaseLayer.emitAndFinalize(H); 85 } 86 87 /// @brief Access the transform functor directly. getTransform()88 TransformFtor& getTransform() { return Transform; } 89 90 /// @brief Access the mumate functor directly. getTransform()91 const TransformFtor& getTransform() const { return Transform; } 92 93 private: 94 BaseLayerT &BaseLayer; 95 TransformFtor Transform; 96 }; 97 98 } // End namespace orc. 99 } // End namespace llvm. 100 101 #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H 102