1 //===--------- JITLinkGeneric.cpp - Generic JIT linker utilities ----------===// 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 // 9 // Generic JITLinker utility class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "JITLinkGeneric.h" 14 15 #include "llvm/Support/BinaryStreamReader.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 18 #define DEBUG_TYPE "jitlink" 19 20 namespace llvm { 21 namespace jitlink { 22 23 JITLinkerBase::~JITLinkerBase() = default; 24 25 void JITLinkerBase::linkPhase1(std::unique_ptr<JITLinkerBase> Self) { 26 27 LLVM_DEBUG({ 28 dbgs() << "Starting link phase 1 for graph " << G->getName() << "\n"; 29 }); 30 31 // Prune and optimize the graph. 32 if (auto Err = runPasses(Passes.PrePrunePasses)) 33 return Ctx->notifyFailed(std::move(Err)); 34 35 LLVM_DEBUG({ 36 dbgs() << "Link graph \"" << G->getName() << "\" pre-pruning:\n"; 37 G->dump(dbgs()); 38 }); 39 40 prune(*G); 41 42 LLVM_DEBUG({ 43 dbgs() << "Link graph \"" << G->getName() << "\" post-pruning:\n"; 44 G->dump(dbgs()); 45 }); 46 47 // Run post-pruning passes. 48 if (auto Err = runPasses(Passes.PostPrunePasses)) 49 return Ctx->notifyFailed(std::move(Err)); 50 51 Ctx->getMemoryManager().allocate( 52 Ctx->getJITLinkDylib(), *G, 53 [S = std::move(Self)](AllocResult AR) mutable { 54 // FIXME: Once MSVC implements c++17 order of evaluation rules for calls 55 // this can be simplified to 56 // S->linkPhase2(std::move(S), std::move(AR)); 57 auto *TmpSelf = S.get(); 58 TmpSelf->linkPhase2(std::move(S), std::move(AR)); 59 }); 60 } 61 62 void JITLinkerBase::linkPhase2(std::unique_ptr<JITLinkerBase> Self, 63 AllocResult AR) { 64 65 if (AR) 66 Alloc = std::move(*AR); 67 else 68 return Ctx->notifyFailed(AR.takeError()); 69 70 LLVM_DEBUG({ 71 dbgs() << "Link graph \"" << G->getName() 72 << "\" before post-allocation passes:\n"; 73 G->dump(dbgs()); 74 }); 75 76 // Run post-allocation passes. 77 if (auto Err = runPasses(Passes.PostAllocationPasses)) 78 return Ctx->notifyFailed(std::move(Err)); 79 80 // Notify client that the defined symbols have been assigned addresses. 81 LLVM_DEBUG(dbgs() << "Resolving symbols defined in " << G->getName() << "\n"); 82 83 if (auto Err = Ctx->notifyResolved(*G)) 84 return Ctx->notifyFailed(std::move(Err)); 85 86 auto ExternalSymbols = getExternalSymbolNames(); 87 88 // If there are no external symbols then proceed immediately with phase 3. 89 if (ExternalSymbols.empty()) { 90 LLVM_DEBUG({ 91 dbgs() << "No external symbols for " << G->getName() 92 << ". Proceeding immediately with link phase 3.\n"; 93 }); 94 // FIXME: Once MSVC implements c++17 order of evaluation rules for calls 95 // this can be simplified. See below. 96 auto &TmpSelf = *Self; 97 TmpSelf.linkPhase3(std::move(Self), AsyncLookupResult()); 98 return; 99 } 100 101 // Otherwise look up the externals. 102 LLVM_DEBUG({ 103 dbgs() << "Issuing lookup for external symbols for " << G->getName() 104 << " (may trigger materialization/linking of other graphs)...\n"; 105 }); 106 107 // We're about to hand off ownership of ourself to the continuation. Grab a 108 // pointer to the context so that we can call it to initiate the lookup. 109 // 110 // FIXME: Once MSVC implements c++17 order of evaluation rules for calls this 111 // can be simplified to: 112 // 113 // Ctx->lookup(std::move(UnresolvedExternals), 114 // [Self=std::move(Self)](Expected<AsyncLookupResult> Result) { 115 // Self->linkPhase3(std::move(Self), std::move(Result)); 116 // }); 117 Ctx->lookup(std::move(ExternalSymbols), 118 createLookupContinuation( 119 [S = std::move(Self)]( 120 Expected<AsyncLookupResult> LookupResult) mutable { 121 auto &TmpSelf = *S; 122 TmpSelf.linkPhase3(std::move(S), std::move(LookupResult)); 123 })); 124 } 125 126 void JITLinkerBase::linkPhase3(std::unique_ptr<JITLinkerBase> Self, 127 Expected<AsyncLookupResult> LR) { 128 129 LLVM_DEBUG({ 130 dbgs() << "Starting link phase 3 for graph " << G->getName() << "\n"; 131 }); 132 133 // If the lookup failed, bail out. 134 if (!LR) 135 return abandonAllocAndBailOut(std::move(Self), LR.takeError()); 136 137 // Assign addresses to external addressables. 138 applyLookupResult(*LR); 139 140 LLVM_DEBUG({ 141 dbgs() << "Link graph \"" << G->getName() 142 << "\" before pre-fixup passes:\n"; 143 G->dump(dbgs()); 144 }); 145 146 if (auto Err = runPasses(Passes.PreFixupPasses)) 147 return abandonAllocAndBailOut(std::move(Self), std::move(Err)); 148 149 LLVM_DEBUG({ 150 dbgs() << "Link graph \"" << G->getName() << "\" before copy-and-fixup:\n"; 151 G->dump(dbgs()); 152 }); 153 154 // Fix up block content. 155 if (auto Err = fixUpBlocks(*G)) 156 return abandonAllocAndBailOut(std::move(Self), std::move(Err)); 157 158 LLVM_DEBUG({ 159 dbgs() << "Link graph \"" << G->getName() << "\" after copy-and-fixup:\n"; 160 G->dump(dbgs()); 161 }); 162 163 if (auto Err = runPasses(Passes.PostFixupPasses)) 164 return abandonAllocAndBailOut(std::move(Self), std::move(Err)); 165 166 Alloc->finalize([S = std::move(Self)](FinalizeResult FR) mutable { 167 // FIXME: Once MSVC implements c++17 order of evaluation rules for calls 168 // this can be simplified to 169 // S->linkPhase2(std::move(S), std::move(AR)); 170 auto *TmpSelf = S.get(); 171 TmpSelf->linkPhase4(std::move(S), std::move(FR)); 172 }); 173 } 174 175 void JITLinkerBase::linkPhase4(std::unique_ptr<JITLinkerBase> Self, 176 FinalizeResult FR) { 177 178 LLVM_DEBUG({ 179 dbgs() << "Starting link phase 4 for graph " << G->getName() << "\n"; 180 }); 181 182 if (!FR) 183 return Ctx->notifyFailed(FR.takeError()); 184 185 Ctx->notifyFinalized(std::move(*FR)); 186 187 LLVM_DEBUG({ dbgs() << "Link of graph " << G->getName() << " complete\n"; }); 188 } 189 190 Error JITLinkerBase::runPasses(LinkGraphPassList &Passes) { 191 for (auto &P : Passes) 192 if (auto Err = P(*G)) 193 return Err; 194 return Error::success(); 195 } 196 197 JITLinkContext::LookupMap JITLinkerBase::getExternalSymbolNames() const { 198 // Identify unresolved external symbols. 199 JITLinkContext::LookupMap UnresolvedExternals; 200 for (auto *Sym : G->external_symbols()) { 201 assert(!Sym->getAddress() && 202 "External has already been assigned an address"); 203 assert(Sym->getName() != StringRef() && Sym->getName() != "" && 204 "Externals must be named"); 205 SymbolLookupFlags LookupFlags = 206 Sym->isWeaklyReferenced() ? SymbolLookupFlags::WeaklyReferencedSymbol 207 : SymbolLookupFlags::RequiredSymbol; 208 UnresolvedExternals[Sym->getName()] = LookupFlags; 209 } 210 return UnresolvedExternals; 211 } 212 213 void JITLinkerBase::applyLookupResult(AsyncLookupResult Result) { 214 for (auto *Sym : G->external_symbols()) { 215 assert(Sym->getOffset() == 0 && 216 "External symbol is not at the start of its addressable block"); 217 assert(!Sym->getAddress() && "Symbol already resolved"); 218 assert(!Sym->isDefined() && "Symbol being resolved is already defined"); 219 auto ResultI = Result.find(Sym->getName()); 220 if (ResultI != Result.end()) 221 Sym->getAddressable().setAddress( 222 orc::ExecutorAddr(ResultI->second.getAddress())); 223 else 224 assert(Sym->isWeaklyReferenced() && 225 "Failed to resolve non-weak reference"); 226 } 227 228 LLVM_DEBUG({ 229 dbgs() << "Externals after applying lookup result:\n"; 230 for (auto *Sym : G->external_symbols()) 231 dbgs() << " " << Sym->getName() << ": " 232 << formatv("{0:x16}", Sym->getAddress().getValue()) << "\n"; 233 }); 234 } 235 236 void JITLinkerBase::abandonAllocAndBailOut(std::unique_ptr<JITLinkerBase> Self, 237 Error Err) { 238 assert(Err && "Should not be bailing out on success value"); 239 assert(Alloc && "can not call abandonAllocAndBailOut before allocation"); 240 Alloc->abandon([S = std::move(Self), E1 = std::move(Err)](Error E2) mutable { 241 S->Ctx->notifyFailed(joinErrors(std::move(E1), std::move(E2))); 242 }); 243 } 244 245 void prune(LinkGraph &G) { 246 std::vector<Symbol *> Worklist; 247 DenseSet<Block *> VisitedBlocks; 248 249 // Build the initial worklist from all symbols initially live. 250 for (auto *Sym : G.defined_symbols()) 251 if (Sym->isLive()) 252 Worklist.push_back(Sym); 253 254 // Propagate live flags to all symbols reachable from the initial live set. 255 while (!Worklist.empty()) { 256 auto *Sym = Worklist.back(); 257 Worklist.pop_back(); 258 259 auto &B = Sym->getBlock(); 260 261 // Skip addressables that we've visited before. 262 if (VisitedBlocks.count(&B)) 263 continue; 264 265 VisitedBlocks.insert(&B); 266 267 for (auto &E : Sym->getBlock().edges()) { 268 // If the edge target is a defined symbol that is being newly marked live 269 // then add it to the worklist. 270 if (E.getTarget().isDefined() && !E.getTarget().isLive()) 271 Worklist.push_back(&E.getTarget()); 272 273 // Mark the target live. 274 E.getTarget().setLive(true); 275 } 276 } 277 278 // Collect all defined symbols to remove, then remove them. 279 { 280 LLVM_DEBUG(dbgs() << "Dead-stripping defined symbols:\n"); 281 std::vector<Symbol *> SymbolsToRemove; 282 for (auto *Sym : G.defined_symbols()) 283 if (!Sym->isLive()) 284 SymbolsToRemove.push_back(Sym); 285 for (auto *Sym : SymbolsToRemove) { 286 LLVM_DEBUG(dbgs() << " " << *Sym << "...\n"); 287 G.removeDefinedSymbol(*Sym); 288 } 289 } 290 291 // Delete any unused blocks. 292 { 293 LLVM_DEBUG(dbgs() << "Dead-stripping blocks:\n"); 294 std::vector<Block *> BlocksToRemove; 295 for (auto *B : G.blocks()) 296 if (!VisitedBlocks.count(B)) 297 BlocksToRemove.push_back(B); 298 for (auto *B : BlocksToRemove) { 299 LLVM_DEBUG(dbgs() << " " << *B << "...\n"); 300 G.removeBlock(*B); 301 } 302 } 303 304 // Collect all external symbols to remove, then remove them. 305 { 306 LLVM_DEBUG(dbgs() << "Removing unused external symbols:\n"); 307 std::vector<Symbol *> SymbolsToRemove; 308 for (auto *Sym : G.external_symbols()) 309 if (!Sym->isLive()) 310 SymbolsToRemove.push_back(Sym); 311 for (auto *Sym : SymbolsToRemove) { 312 LLVM_DEBUG(dbgs() << " " << *Sym << "...\n"); 313 G.removeExternalSymbol(*Sym); 314 } 315 } 316 } 317 318 } // end namespace jitlink 319 } // end namespace llvm 320