CompileOnDemandLayer.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //===----- CompileOnDemandLayer.cpp - Lazily emit IR on first call --------===//
  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/CompileOnDemandLayer.h"
  9. #include "llvm/ADT/Hashing.h"
  10. #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
  11. #include "llvm/IR/Mangler.h"
  12. #include "llvm/IR/Module.h"
  13. #include "llvm/Support/FormatVariadic.h"
  14. #include <string>
  15. using namespace llvm;
  16. using namespace llvm::orc;
  17. static ThreadSafeModule extractSubModule(ThreadSafeModule &TSM,
  18. StringRef Suffix,
  19. GVPredicate ShouldExtract) {
  20. auto DeleteExtractedDefs = [](GlobalValue &GV) {
  21. // Bump the linkage: this global will be provided by the external module.
  22. GV.setLinkage(GlobalValue::ExternalLinkage);
  23. // Delete the definition in the source module.
  24. if (isa<Function>(GV)) {
  25. auto &F = cast<Function>(GV);
  26. F.deleteBody();
  27. F.setPersonalityFn(nullptr);
  28. } else if (isa<GlobalVariable>(GV)) {
  29. cast<GlobalVariable>(GV).setInitializer(nullptr);
  30. } else if (isa<GlobalAlias>(GV)) {
  31. // We need to turn deleted aliases into function or variable decls based
  32. // on the type of their aliasee.
  33. auto &A = cast<GlobalAlias>(GV);
  34. Constant *Aliasee = A.getAliasee();
  35. assert(A.hasName() && "Anonymous alias?");
  36. assert(Aliasee->hasName() && "Anonymous aliasee");
  37. std::string AliasName = std::string(A.getName());
  38. if (isa<Function>(Aliasee)) {
  39. auto *F = cloneFunctionDecl(*A.getParent(), *cast<Function>(Aliasee));
  40. A.replaceAllUsesWith(F);
  41. A.eraseFromParent();
  42. F->setName(AliasName);
  43. } else if (isa<GlobalVariable>(Aliasee)) {
  44. auto *G = cloneGlobalVariableDecl(*A.getParent(),
  45. *cast<GlobalVariable>(Aliasee));
  46. A.replaceAllUsesWith(G);
  47. A.eraseFromParent();
  48. G->setName(AliasName);
  49. } else
  50. llvm_unreachable("Alias to unsupported type");
  51. } else
  52. llvm_unreachable("Unsupported global type");
  53. };
  54. auto NewTSM = cloneToNewContext(TSM, ShouldExtract, DeleteExtractedDefs);
  55. NewTSM.withModuleDo([&](Module &M) {
  56. M.setModuleIdentifier((M.getModuleIdentifier() + Suffix).str());
  57. });
  58. return NewTSM;
  59. }
  60. namespace llvm {
  61. namespace orc {
  62. class PartitioningIRMaterializationUnit : public IRMaterializationUnit {
  63. public:
  64. PartitioningIRMaterializationUnit(ExecutionSession &ES,
  65. const IRSymbolMapper::ManglingOptions &MO,
  66. ThreadSafeModule TSM,
  67. CompileOnDemandLayer &Parent)
  68. : IRMaterializationUnit(ES, MO, std::move(TSM)), Parent(Parent) {}
  69. PartitioningIRMaterializationUnit(
  70. ThreadSafeModule TSM, Interface I,
  71. SymbolNameToDefinitionMap SymbolToDefinition,
  72. CompileOnDemandLayer &Parent)
  73. : IRMaterializationUnit(std::move(TSM), std::move(I),
  74. std::move(SymbolToDefinition)),
  75. Parent(Parent) {}
  76. private:
  77. void materialize(std::unique_ptr<MaterializationResponsibility> R) override {
  78. Parent.emitPartition(std::move(R), std::move(TSM),
  79. std::move(SymbolToDefinition));
  80. }
  81. void discard(const JITDylib &V, const SymbolStringPtr &Name) override {
  82. // All original symbols were materialized by the CODLayer and should be
  83. // final. The function bodies provided by M should never be overridden.
  84. llvm_unreachable("Discard should never be called on an "
  85. "ExtractingIRMaterializationUnit");
  86. }
  87. mutable std::mutex SourceModuleMutex;
  88. CompileOnDemandLayer &Parent;
  89. };
  90. std::optional<CompileOnDemandLayer::GlobalValueSet>
  91. CompileOnDemandLayer::compileRequested(GlobalValueSet Requested) {
  92. return std::move(Requested);
  93. }
  94. std::optional<CompileOnDemandLayer::GlobalValueSet>
  95. CompileOnDemandLayer::compileWholeModule(GlobalValueSet Requested) {
  96. return std::nullopt;
  97. }
  98. CompileOnDemandLayer::CompileOnDemandLayer(
  99. ExecutionSession &ES, IRLayer &BaseLayer, LazyCallThroughManager &LCTMgr,
  100. IndirectStubsManagerBuilder BuildIndirectStubsManager)
  101. : IRLayer(ES, BaseLayer.getManglingOptions()), BaseLayer(BaseLayer),
  102. LCTMgr(LCTMgr),
  103. BuildIndirectStubsManager(std::move(BuildIndirectStubsManager)) {}
  104. void CompileOnDemandLayer::setPartitionFunction(PartitionFunction Partition) {
  105. this->Partition = std::move(Partition);
  106. }
  107. void CompileOnDemandLayer::setImplMap(ImplSymbolMap *Imp) {
  108. this->AliaseeImpls = Imp;
  109. }
  110. void CompileOnDemandLayer::emit(
  111. std::unique_ptr<MaterializationResponsibility> R, ThreadSafeModule TSM) {
  112. assert(TSM && "Null module");
  113. auto &ES = getExecutionSession();
  114. // Sort the callables and non-callables, build re-exports and lodge the
  115. // actual module with the implementation dylib.
  116. auto &PDR = getPerDylibResources(R->getTargetJITDylib());
  117. SymbolAliasMap NonCallables;
  118. SymbolAliasMap Callables;
  119. TSM.withModuleDo([&](Module &M) {
  120. // First, do some cleanup on the module:
  121. cleanUpModule(M);
  122. });
  123. for (auto &KV : R->getSymbols()) {
  124. auto &Name = KV.first;
  125. auto &Flags = KV.second;
  126. if (Flags.isCallable())
  127. Callables[Name] = SymbolAliasMapEntry(Name, Flags);
  128. else
  129. NonCallables[Name] = SymbolAliasMapEntry(Name, Flags);
  130. }
  131. // Create a partitioning materialization unit and lodge it with the
  132. // implementation dylib.
  133. if (auto Err = PDR.getImplDylib().define(
  134. std::make_unique<PartitioningIRMaterializationUnit>(
  135. ES, *getManglingOptions(), std::move(TSM), *this))) {
  136. ES.reportError(std::move(Err));
  137. R->failMaterialization();
  138. return;
  139. }
  140. if (!NonCallables.empty())
  141. if (auto Err =
  142. R->replace(reexports(PDR.getImplDylib(), std::move(NonCallables),
  143. JITDylibLookupFlags::MatchAllSymbols))) {
  144. getExecutionSession().reportError(std::move(Err));
  145. R->failMaterialization();
  146. return;
  147. }
  148. if (!Callables.empty()) {
  149. if (auto Err = R->replace(
  150. lazyReexports(LCTMgr, PDR.getISManager(), PDR.getImplDylib(),
  151. std::move(Callables), AliaseeImpls))) {
  152. getExecutionSession().reportError(std::move(Err));
  153. R->failMaterialization();
  154. return;
  155. }
  156. }
  157. }
  158. CompileOnDemandLayer::PerDylibResources &
  159. CompileOnDemandLayer::getPerDylibResources(JITDylib &TargetD) {
  160. std::lock_guard<std::mutex> Lock(CODLayerMutex);
  161. auto I = DylibResources.find(&TargetD);
  162. if (I == DylibResources.end()) {
  163. auto &ImplD =
  164. getExecutionSession().createBareJITDylib(TargetD.getName() + ".impl");
  165. JITDylibSearchOrder NewLinkOrder;
  166. TargetD.withLinkOrderDo([&](const JITDylibSearchOrder &TargetLinkOrder) {
  167. NewLinkOrder = TargetLinkOrder;
  168. });
  169. assert(!NewLinkOrder.empty() && NewLinkOrder.front().first == &TargetD &&
  170. NewLinkOrder.front().second ==
  171. JITDylibLookupFlags::MatchAllSymbols &&
  172. "TargetD must be at the front of its own search order and match "
  173. "non-exported symbol");
  174. NewLinkOrder.insert(std::next(NewLinkOrder.begin()),
  175. {&ImplD, JITDylibLookupFlags::MatchAllSymbols});
  176. ImplD.setLinkOrder(NewLinkOrder, false);
  177. TargetD.setLinkOrder(std::move(NewLinkOrder), false);
  178. PerDylibResources PDR(ImplD, BuildIndirectStubsManager());
  179. I = DylibResources.insert(std::make_pair(&TargetD, std::move(PDR))).first;
  180. }
  181. return I->second;
  182. }
  183. void CompileOnDemandLayer::cleanUpModule(Module &M) {
  184. for (auto &F : M.functions()) {
  185. if (F.isDeclaration())
  186. continue;
  187. if (F.hasAvailableExternallyLinkage()) {
  188. F.deleteBody();
  189. F.setPersonalityFn(nullptr);
  190. continue;
  191. }
  192. }
  193. }
  194. void CompileOnDemandLayer::expandPartition(GlobalValueSet &Partition) {
  195. // Expands the partition to ensure the following rules hold:
  196. // (1) If any alias is in the partition, its aliasee is also in the partition.
  197. // (2) If any aliasee is in the partition, its aliases are also in the
  198. // partiton.
  199. // (3) If any global variable is in the partition then all global variables
  200. // are in the partition.
  201. assert(!Partition.empty() && "Unexpected empty partition");
  202. const Module &M = *(*Partition.begin())->getParent();
  203. bool ContainsGlobalVariables = false;
  204. std::vector<const GlobalValue *> GVsToAdd;
  205. for (const auto *GV : Partition)
  206. if (isa<GlobalAlias>(GV))
  207. GVsToAdd.push_back(
  208. cast<GlobalValue>(cast<GlobalAlias>(GV)->getAliasee()));
  209. else if (isa<GlobalVariable>(GV))
  210. ContainsGlobalVariables = true;
  211. for (auto &A : M.aliases())
  212. if (Partition.count(cast<GlobalValue>(A.getAliasee())))
  213. GVsToAdd.push_back(&A);
  214. if (ContainsGlobalVariables)
  215. for (auto &G : M.globals())
  216. GVsToAdd.push_back(&G);
  217. for (const auto *GV : GVsToAdd)
  218. Partition.insert(GV);
  219. }
  220. void CompileOnDemandLayer::emitPartition(
  221. std::unique_ptr<MaterializationResponsibility> R, ThreadSafeModule TSM,
  222. IRMaterializationUnit::SymbolNameToDefinitionMap Defs) {
  223. // FIXME: Need a 'notify lazy-extracting/emitting' callback to tie the
  224. // extracted module key, extracted module, and source module key
  225. // together. This could be used, for example, to provide a specific
  226. // memory manager instance to the linking layer.
  227. auto &ES = getExecutionSession();
  228. GlobalValueSet RequestedGVs;
  229. for (auto &Name : R->getRequestedSymbols()) {
  230. if (Name == R->getInitializerSymbol())
  231. TSM.withModuleDo([&](Module &M) {
  232. for (auto &GV : getStaticInitGVs(M))
  233. RequestedGVs.insert(&GV);
  234. });
  235. else {
  236. assert(Defs.count(Name) && "No definition for symbol");
  237. RequestedGVs.insert(Defs[Name]);
  238. }
  239. }
  240. /// Perform partitioning with the context lock held, since the partition
  241. /// function is allowed to access the globals to compute the partition.
  242. auto GVsToExtract =
  243. TSM.withModuleDo([&](Module &M) { return Partition(RequestedGVs); });
  244. // Take a 'None' partition to mean the whole module (as opposed to an empty
  245. // partition, which means "materialize nothing"). Emit the whole module
  246. // unmodified to the base layer.
  247. if (GVsToExtract == std::nullopt) {
  248. Defs.clear();
  249. BaseLayer.emit(std::move(R), std::move(TSM));
  250. return;
  251. }
  252. // If the partition is empty, return the whole module to the symbol table.
  253. if (GVsToExtract->empty()) {
  254. if (auto Err =
  255. R->replace(std::make_unique<PartitioningIRMaterializationUnit>(
  256. std::move(TSM),
  257. MaterializationUnit::Interface(R->getSymbols(),
  258. R->getInitializerSymbol()),
  259. std::move(Defs), *this))) {
  260. getExecutionSession().reportError(std::move(Err));
  261. R->failMaterialization();
  262. return;
  263. }
  264. return;
  265. }
  266. // Ok -- we actually need to partition the symbols. Promote the symbol
  267. // linkages/names, expand the partition to include any required symbols
  268. // (i.e. symbols that can't be separated from our partition), and
  269. // then extract the partition.
  270. //
  271. // FIXME: We apply this promotion once per partitioning. It's safe, but
  272. // overkill.
  273. auto ExtractedTSM =
  274. TSM.withModuleDo([&](Module &M) -> Expected<ThreadSafeModule> {
  275. auto PromotedGlobals = PromoteSymbols(M);
  276. if (!PromotedGlobals.empty()) {
  277. MangleAndInterner Mangle(ES, M.getDataLayout());
  278. SymbolFlagsMap SymbolFlags;
  279. IRSymbolMapper::add(ES, *getManglingOptions(),
  280. PromotedGlobals, SymbolFlags);
  281. if (auto Err = R->defineMaterializing(SymbolFlags))
  282. return std::move(Err);
  283. }
  284. expandPartition(*GVsToExtract);
  285. // Submodule name is given by hashing the names of the globals.
  286. std::string SubModuleName;
  287. {
  288. std::vector<const GlobalValue*> HashGVs;
  289. HashGVs.reserve(GVsToExtract->size());
  290. for (const auto *GV : *GVsToExtract)
  291. HashGVs.push_back(GV);
  292. llvm::sort(HashGVs, [](const GlobalValue *LHS, const GlobalValue *RHS) {
  293. return LHS->getName() < RHS->getName();
  294. });
  295. hash_code HC(0);
  296. for (const auto *GV : HashGVs) {
  297. assert(GV->hasName() && "All GVs to extract should be named by now");
  298. auto GVName = GV->getName();
  299. HC = hash_combine(HC, hash_combine_range(GVName.begin(), GVName.end()));
  300. }
  301. raw_string_ostream(SubModuleName)
  302. << ".submodule."
  303. << formatv(sizeof(size_t) == 8 ? "{0:x16}" : "{0:x8}",
  304. static_cast<size_t>(HC))
  305. << ".ll";
  306. }
  307. // Extract the requested partiton (plus any necessary aliases) and
  308. // put the rest back into the impl dylib.
  309. auto ShouldExtract = [&](const GlobalValue &GV) -> bool {
  310. return GVsToExtract->count(&GV);
  311. };
  312. return extractSubModule(TSM, SubModuleName , ShouldExtract);
  313. });
  314. if (!ExtractedTSM) {
  315. ES.reportError(ExtractedTSM.takeError());
  316. R->failMaterialization();
  317. return;
  318. }
  319. if (auto Err = R->replace(std::make_unique<PartitioningIRMaterializationUnit>(
  320. ES, *getManglingOptions(), std::move(TSM), *this))) {
  321. ES.reportError(std::move(Err));
  322. R->failMaterialization();
  323. return;
  324. }
  325. BaseLayer.emit(std::move(R), std::move(*ExtractedTSM));
  326. }
  327. } // end namespace orc
  328. } // end namespace llvm