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 "JITSymbol.h"
22 /// @brief IR mutating layer.
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 {
30 /// @brief Handle to a set of added modules.
31 typedef typename BaseLayerT::ModuleSetHandleT ModuleSetHandleT;
33 /// @brief Construct an IRTransformLayer with the given BaseLayer
34 IRTransformLayer(BaseLayerT &BaseLayer,
35 TransformFtor Transform = TransformFtor())
36 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
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.
42 /// @return A handle for the added modules.
43 template <typename ModuleSetT, typename MemoryManagerPtrT,
44 typename SymbolResolverPtrT>
45 ModuleSetHandleT addModuleSet(ModuleSetT Ms,
46 MemoryManagerPtrT MemMgr,
47 SymbolResolverPtrT Resolver) {
49 for (auto I = Ms.begin(), E = Ms.end(); I != E; ++I)
50 *I = Transform(std::move(*I));
52 return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr),
56 /// @brief Remove the module set associated with the handle H.
57 void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeModuleSet(H); }
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.
63 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
64 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
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
75 JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
76 bool ExportedSymbolsOnly) {
77 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
80 /// @brief Immediately emit and finalize the module set represented by the
82 /// @param H Handle for module set to emit/finalize.
83 void emitAndFinalize(ModuleSetHandleT H) {
84 BaseLayer.emitAndFinalize(H);
87 /// @brief Access the transform functor directly.
88 TransformFtor& getTransform() { return Transform; }
90 /// @brief Access the mumate functor directly.
91 const TransformFtor& getTransform() const { return Transform; }
94 BaseLayerT &BaseLayer;
95 TransformFtor Transform;
98 } // End namespace orc.
99 } // End namespace llvm.
101 #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H