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