Layer.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //===-------------------- Layer.cpp - Layer interfaces --------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ExecutionEngine/Orc/Layer.h"
  9. #include "llvm/ExecutionEngine/Orc/DebugUtils.h"
  10. #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
  11. #include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
  12. #include "llvm/IR/Constants.h"
  13. #include "llvm/Support/Debug.h"
  14. #define DEBUG_TYPE "orc"
  15. namespace llvm {
  16. namespace orc {
  17. IRLayer::~IRLayer() {}
  18. Error IRLayer::add(ResourceTrackerSP RT, ThreadSafeModule TSM) {
  19. assert(RT && "RT can not be null");
  20. auto &JD = RT->getJITDylib();
  21. return JD.define(std::make_unique<BasicIRLayerMaterializationUnit>(
  22. *this, *getManglingOptions(), std::move(TSM)),
  23. std::move(RT));
  24. }
  25. IRMaterializationUnit::IRMaterializationUnit(
  26. ExecutionSession &ES, const IRSymbolMapper::ManglingOptions &MO,
  27. ThreadSafeModule TSM)
  28. : MaterializationUnit(Interface()), TSM(std::move(TSM)) {
  29. assert(this->TSM && "Module must not be null");
  30. MangleAndInterner Mangle(ES, this->TSM.getModuleUnlocked()->getDataLayout());
  31. this->TSM.withModuleDo([&](Module &M) {
  32. for (auto &G : M.global_values()) {
  33. // Skip globals that don't generate symbols.
  34. if (!G.hasName() || G.isDeclaration() || G.hasLocalLinkage() ||
  35. G.hasAvailableExternallyLinkage() || G.hasAppendingLinkage())
  36. continue;
  37. // thread locals generate different symbols depending on whether or not
  38. // emulated TLS is enabled.
  39. if (G.isThreadLocal() && MO.EmulatedTLS) {
  40. auto &GV = cast<GlobalVariable>(G);
  41. auto Flags = JITSymbolFlags::fromGlobalValue(GV);
  42. auto EmuTLSV = Mangle(("__emutls_v." + GV.getName()).str());
  43. SymbolFlags[EmuTLSV] = Flags;
  44. SymbolToDefinition[EmuTLSV] = &GV;
  45. // If this GV has a non-zero initializer we'll need to emit an
  46. // __emutls.t symbol too.
  47. if (GV.hasInitializer()) {
  48. const auto *InitVal = GV.getInitializer();
  49. // Skip zero-initializers.
  50. if (isa<ConstantAggregateZero>(InitVal))
  51. continue;
  52. const auto *InitIntValue = dyn_cast<ConstantInt>(InitVal);
  53. if (InitIntValue && InitIntValue->isZero())
  54. continue;
  55. auto EmuTLST = Mangle(("__emutls_t." + GV.getName()).str());
  56. SymbolFlags[EmuTLST] = Flags;
  57. }
  58. continue;
  59. }
  60. // Otherwise we just need a normal linker mangling.
  61. auto MangledName = Mangle(G.getName());
  62. SymbolFlags[MangledName] = JITSymbolFlags::fromGlobalValue(G);
  63. SymbolToDefinition[MangledName] = &G;
  64. }
  65. // If we need an init symbol for this module then create one.
  66. if (!llvm::empty(getStaticInitGVs(M))) {
  67. size_t Counter = 0;
  68. do {
  69. std::string InitSymbolName;
  70. raw_string_ostream(InitSymbolName)
  71. << "$." << M.getModuleIdentifier() << ".__inits." << Counter++;
  72. InitSymbol = ES.intern(InitSymbolName);
  73. } while (SymbolFlags.count(InitSymbol));
  74. SymbolFlags[InitSymbol] = JITSymbolFlags::MaterializationSideEffectsOnly;
  75. }
  76. });
  77. }
  78. IRMaterializationUnit::IRMaterializationUnit(
  79. ThreadSafeModule TSM, Interface I,
  80. SymbolNameToDefinitionMap SymbolToDefinition)
  81. : MaterializationUnit(std::move(I)), TSM(std::move(TSM)),
  82. SymbolToDefinition(std::move(SymbolToDefinition)) {}
  83. StringRef IRMaterializationUnit::getName() const {
  84. if (TSM)
  85. return TSM.withModuleDo(
  86. [](const Module &M) -> StringRef { return M.getModuleIdentifier(); });
  87. return "<null module>";
  88. }
  89. void IRMaterializationUnit::discard(const JITDylib &JD,
  90. const SymbolStringPtr &Name) {
  91. LLVM_DEBUG(JD.getExecutionSession().runSessionLocked([&]() {
  92. dbgs() << "In " << JD.getName() << " discarding " << *Name << " from MU@"
  93. << this << " (" << getName() << ")\n";
  94. }););
  95. auto I = SymbolToDefinition.find(Name);
  96. assert(I != SymbolToDefinition.end() &&
  97. "Symbol not provided by this MU, or previously discarded");
  98. assert(!I->second->isDeclaration() &&
  99. "Discard should only apply to definitions");
  100. I->second->setLinkage(GlobalValue::AvailableExternallyLinkage);
  101. SymbolToDefinition.erase(I);
  102. }
  103. BasicIRLayerMaterializationUnit::BasicIRLayerMaterializationUnit(
  104. IRLayer &L, const IRSymbolMapper::ManglingOptions &MO, ThreadSafeModule TSM)
  105. : IRMaterializationUnit(L.getExecutionSession(), MO, std::move(TSM)), L(L) {
  106. }
  107. void BasicIRLayerMaterializationUnit::materialize(
  108. std::unique_ptr<MaterializationResponsibility> R) {
  109. // Throw away the SymbolToDefinition map: it's not usable after we hand
  110. // off the module.
  111. SymbolToDefinition.clear();
  112. // If cloneToNewContextOnEmit is set, clone the module now.
  113. if (L.getCloneToNewContextOnEmit())
  114. TSM = cloneToNewContext(TSM);
  115. #ifndef NDEBUG
  116. auto &ES = R->getTargetJITDylib().getExecutionSession();
  117. auto &N = R->getTargetJITDylib().getName();
  118. #endif // NDEBUG
  119. LLVM_DEBUG(ES.runSessionLocked(
  120. [&]() { dbgs() << "Emitting, for " << N << ", " << *this << "\n"; }););
  121. L.emit(std::move(R), std::move(TSM));
  122. LLVM_DEBUG(ES.runSessionLocked([&]() {
  123. dbgs() << "Finished emitting, for " << N << ", " << *this << "\n";
  124. }););
  125. }
  126. char ObjectLayer::ID;
  127. ObjectLayer::ObjectLayer(ExecutionSession &ES) : ES(ES) {}
  128. ObjectLayer::~ObjectLayer() {}
  129. Error ObjectLayer::add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O,
  130. MaterializationUnit::Interface I) {
  131. assert(RT && "RT can not be null");
  132. auto &JD = RT->getJITDylib();
  133. return JD.define(std::make_unique<BasicObjectLayerMaterializationUnit>(
  134. *this, std::move(O), std::move(I)),
  135. std::move(RT));
  136. }
  137. Error ObjectLayer::add(ResourceTrackerSP RT, std::unique_ptr<MemoryBuffer> O) {
  138. auto I = getObjectFileInterface(getExecutionSession(), O->getMemBufferRef());
  139. if (!I)
  140. return I.takeError();
  141. return add(std::move(RT), std::move(O), std::move(*I));
  142. }
  143. Error ObjectLayer::add(JITDylib &JD, std::unique_ptr<MemoryBuffer> O) {
  144. auto I = getObjectFileInterface(getExecutionSession(), O->getMemBufferRef());
  145. if (!I)
  146. return I.takeError();
  147. return add(JD, std::move(O), std::move(*I));
  148. }
  149. Expected<std::unique_ptr<BasicObjectLayerMaterializationUnit>>
  150. BasicObjectLayerMaterializationUnit::Create(ObjectLayer &L,
  151. std::unique_ptr<MemoryBuffer> O) {
  152. auto ObjInterface =
  153. getObjectFileInterface(L.getExecutionSession(), O->getMemBufferRef());
  154. if (!ObjInterface)
  155. return ObjInterface.takeError();
  156. return std::unique_ptr<BasicObjectLayerMaterializationUnit>(
  157. new BasicObjectLayerMaterializationUnit(L, std::move(O),
  158. std::move(*ObjInterface)));
  159. }
  160. BasicObjectLayerMaterializationUnit::BasicObjectLayerMaterializationUnit(
  161. ObjectLayer &L, std::unique_ptr<MemoryBuffer> O, Interface I)
  162. : MaterializationUnit(std::move(I)), L(L), O(std::move(O)) {}
  163. StringRef BasicObjectLayerMaterializationUnit::getName() const {
  164. if (O)
  165. return O->getBufferIdentifier();
  166. return "<null object>";
  167. }
  168. void BasicObjectLayerMaterializationUnit::materialize(
  169. std::unique_ptr<MaterializationResponsibility> R) {
  170. L.emit(std::move(R), std::move(O));
  171. }
  172. void BasicObjectLayerMaterializationUnit::discard(const JITDylib &JD,
  173. const SymbolStringPtr &Name) {
  174. // This is a no-op for object files: Having removed 'Name' from SymbolFlags
  175. // the symbol will be dead-stripped by the JIT linker.
  176. }
  177. } // End namespace orc.
  178. } // End namespace llvm.