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"
23 /// @brief IR mutating layer.
25 /// This layer applies a user supplied transform to each module that is added,
26 /// then adds the transformed module to the layer below.
27 template <typename BaseLayerT, typename TransformFtor>
28 class IRTransformLayer {
31 /// @brief Handle to a set of added modules.
32 using ModuleHandleT = typename BaseLayerT::ModuleHandleT;
34 /// @brief Construct an IRTransformLayer with the given BaseLayer
35 IRTransformLayer(BaseLayerT &BaseLayer,
36 TransformFtor Transform = TransformFtor())
37 : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
39 /// @brief Apply the transform functor to the module, then add the module to
40 /// the layer below, along with the memory manager and symbol resolver.
42 /// @return A handle for the added modules.
43 template <typename MemoryManagerPtrT, typename SymbolResolverPtrT>
44 ModuleHandleT addModule(std::shared_ptr<Module> M,
45 MemoryManagerPtrT MemMgr,
46 SymbolResolverPtrT Resolver) {
47 return BaseLayer.addModule(Transform(std::move(M)), std::move(MemMgr),
51 /// @brief Remove the module associated with the handle H.
52 void removeModule(ModuleHandleT H) { BaseLayer.removeModule(H); }
54 /// @brief Search for the given named symbol.
55 /// @param Name The name of the symbol to search for.
56 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
57 /// @return A handle for the given named symbol, if it exists.
58 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
59 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
62 /// @brief Get the address of the given symbol in the context of the module
63 /// represented by the handle H. This call is forwarded to the base
64 /// layer's implementation.
65 /// @param H The handle for the module to search in.
66 /// @param Name The name of the symbol to search for.
67 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
68 /// @return A handle for the given named symbol, if it is found in the
70 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
71 bool ExportedSymbolsOnly) {
72 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
75 /// @brief Immediately emit and finalize the module represented by the given
77 /// @param H Handle for module to emit/finalize.
78 void emitAndFinalize(ModuleHandleT H) {
79 BaseLayer.emitAndFinalize(H);
82 /// @brief Access the transform functor directly.
83 TransformFtor& getTransform() { return Transform; }
85 /// @brief Access the mumate functor directly.
86 const TransformFtor& getTransform() const { return Transform; }
89 BaseLayerT &BaseLayer;
90 TransformFtor Transform;
93 } // end namespace orc
94 } // end namespace llvm
96 #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H