CompileOnDemandLayer.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CompileOnDemandLayer.h - Compile each function on demand -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // JIT layer for breaking up modules and inserting callbacks to allow
  15. // individual functions to be compiled on demand.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
  19. #define LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
  20. #include "llvm/ADT/APInt.h"
  21. #include "llvm/ADT/Optional.h"
  22. #include "llvm/ADT/STLExtras.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/ExecutionEngine/JITSymbol.h"
  25. #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
  26. #include "llvm/ExecutionEngine/Orc/Layer.h"
  27. #include "llvm/ExecutionEngine/Orc/LazyReexports.h"
  28. #include "llvm/ExecutionEngine/Orc/Speculation.h"
  29. #include "llvm/ExecutionEngine/Orc/Shared/OrcError.h"
  30. #include "llvm/ExecutionEngine/RuntimeDyld.h"
  31. #include "llvm/IR/Attributes.h"
  32. #include "llvm/IR/Constant.h"
  33. #include "llvm/IR/Constants.h"
  34. #include "llvm/IR/DataLayout.h"
  35. #include "llvm/IR/Function.h"
  36. #include "llvm/IR/GlobalAlias.h"
  37. #include "llvm/IR/GlobalValue.h"
  38. #include "llvm/IR/GlobalVariable.h"
  39. #include "llvm/IR/Instruction.h"
  40. #include "llvm/IR/Mangler.h"
  41. #include "llvm/IR/Module.h"
  42. #include "llvm/IR/Type.h"
  43. #include "llvm/Support/Casting.h"
  44. #include "llvm/Support/raw_ostream.h"
  45. #include "llvm/Transforms/Utils/ValueMapper.h"
  46. #include <algorithm>
  47. #include <cassert>
  48. #include <functional>
  49. #include <iterator>
  50. #include <list>
  51. #include <memory>
  52. #include <set>
  53. #include <utility>
  54. #include <vector>
  55. namespace llvm {
  56. namespace orc {
  57. class CompileOnDemandLayer : public IRLayer {
  58. friend class PartitioningIRMaterializationUnit;
  59. public:
  60. /// Builder for IndirectStubsManagers.
  61. using IndirectStubsManagerBuilder =
  62. std::function<std::unique_ptr<IndirectStubsManager>()>;
  63. using GlobalValueSet = std::set<const GlobalValue *>;
  64. /// Partitioning function.
  65. using PartitionFunction =
  66. std::function<Optional<GlobalValueSet>(GlobalValueSet Requested)>;
  67. /// Off-the-shelf partitioning which compiles all requested symbols (usually
  68. /// a single function at a time).
  69. static Optional<GlobalValueSet> compileRequested(GlobalValueSet Requested);
  70. /// Off-the-shelf partitioning which compiles whole modules whenever any
  71. /// symbol in them is requested.
  72. static Optional<GlobalValueSet> compileWholeModule(GlobalValueSet Requested);
  73. /// Construct a CompileOnDemandLayer.
  74. CompileOnDemandLayer(ExecutionSession &ES, IRLayer &BaseLayer,
  75. LazyCallThroughManager &LCTMgr,
  76. IndirectStubsManagerBuilder BuildIndirectStubsManager);
  77. /// Sets the partition function.
  78. void setPartitionFunction(PartitionFunction Partition);
  79. /// Sets the ImplSymbolMap
  80. void setImplMap(ImplSymbolMap *Imp);
  81. /// Emits the given module. This should not be called by clients: it will be
  82. /// called by the JIT when a definition added via the add method is requested.
  83. void emit(std::unique_ptr<MaterializationResponsibility> R,
  84. ThreadSafeModule TSM) override;
  85. private:
  86. struct PerDylibResources {
  87. public:
  88. PerDylibResources(JITDylib &ImplD,
  89. std::unique_ptr<IndirectStubsManager> ISMgr)
  90. : ImplD(ImplD), ISMgr(std::move(ISMgr)) {}
  91. JITDylib &getImplDylib() { return ImplD; }
  92. IndirectStubsManager &getISManager() { return *ISMgr; }
  93. private:
  94. JITDylib &ImplD;
  95. std::unique_ptr<IndirectStubsManager> ISMgr;
  96. };
  97. using PerDylibResourcesMap = std::map<const JITDylib *, PerDylibResources>;
  98. PerDylibResources &getPerDylibResources(JITDylib &TargetD);
  99. void cleanUpModule(Module &M);
  100. void expandPartition(GlobalValueSet &Partition);
  101. void emitPartition(std::unique_ptr<MaterializationResponsibility> R,
  102. ThreadSafeModule TSM,
  103. IRMaterializationUnit::SymbolNameToDefinitionMap Defs);
  104. mutable std::mutex CODLayerMutex;
  105. IRLayer &BaseLayer;
  106. LazyCallThroughManager &LCTMgr;
  107. IndirectStubsManagerBuilder BuildIndirectStubsManager;
  108. PerDylibResourcesMap DylibResources;
  109. PartitionFunction Partition = compileRequested;
  110. SymbolLinkagePromoter PromoteSymbols;
  111. ImplSymbolMap *AliaseeImpls = nullptr;
  112. };
  113. } // end namespace orc
  114. } // end namespace llvm
  115. #endif // LLVM_EXECUTIONENGINE_ORC_COMPILEONDEMANDLAYER_H
  116. #ifdef __GNUC__
  117. #pragma GCC diagnostic pop
  118. #endif