IRTransformLayer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #ifndef LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
  14. #define LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H
  15. #include "JITSymbol.h"
  16. namespace llvm {
  17. namespace orc {
  18. /// @brief IR mutating layer.
  19. ///
  20. /// This layer accepts sets of LLVM IR Modules (via addModuleSet). It
  21. /// immediately applies the user supplied functor to each module, then adds
  22. /// the set of transformed modules to the layer below.
  23. template <typename BaseLayerT, typename TransformFtor>
  24. class IRTransformLayer {
  25. public:
  26. /// @brief Handle to a set of added modules.
  27. typedef typename BaseLayerT::ModuleSetHandleT ModuleSetHandleT;
  28. /// @brief Construct an IRTransformLayer with the given BaseLayer
  29. IRTransformLayer(BaseLayerT &BaseLayer,
  30. TransformFtor Transform = TransformFtor())
  31. : BaseLayer(BaseLayer), Transform(std::move(Transform)) {}
  32. /// @brief Apply the transform functor to each module in the module set, then
  33. /// add the resulting set of modules to the base layer, along with the
  34. /// memory manager and symbol resolver.
  35. ///
  36. /// @return A handle for the added modules.
  37. template <typename ModuleSetT, typename MemoryManagerPtrT,
  38. typename SymbolResolverPtrT>
  39. ModuleSetHandleT addModuleSet(ModuleSetT Ms,
  40. MemoryManagerPtrT MemMgr,
  41. SymbolResolverPtrT Resolver) {
  42. for (auto I = Ms.begin(), E = Ms.end(); I != E; ++I)
  43. *I = Transform(std::move(*I));
  44. return BaseLayer.addModuleSet(std::move(Ms), std::move(MemMgr),
  45. std::move(Resolver));
  46. }
  47. /// @brief Remove the module set associated with the handle H.
  48. void removeModuleSet(ModuleSetHandleT H) { BaseLayer.removeModuleSet(H); }
  49. /// @brief Search for the given named symbol.
  50. /// @param Name The name of the symbol to search for.
  51. /// @param ExportedSymbolsOnly If true, search only for exported symbols.
  52. /// @return A handle for the given named symbol, if it exists.
  53. JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
  54. return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
  55. }
  56. /// @brief Get the address of the given symbol in the context of the set of
  57. /// modules represented by the handle H. This call is forwarded to the
  58. /// base layer's implementation.
  59. /// @param H The handle for the module set to search in.
  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 is found in the
  63. /// given module set.
  64. JITSymbol findSymbolIn(ModuleSetHandleT H, const std::string &Name,
  65. bool ExportedSymbolsOnly) {
  66. return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
  67. }
  68. /// @brief Immediately emit and finalize the module set represented by the
  69. /// given handle.
  70. /// @param H Handle for module set to emit/finalize.
  71. void emitAndFinalize(ModuleSetHandleT H) {
  72. BaseLayer.emitAndFinalize(H);
  73. }
  74. /// @brief Access the transform functor directly.
  75. TransformFtor& getTransform() { return Transform; }
  76. /// @brief Access the mumate functor directly.
  77. const TransformFtor& getTransform() const { return Transform; }
  78. private:
  79. BaseLayerT &BaseLayer;
  80. TransformFtor Transform;
  81. };
  82. } // End namespace orc.
  83. } // End namespace llvm.
  84. #endif // LLVM_EXECUTIONENGINE_ORC_IRTRANSFORMLAYER_H