xref: /llvm-project/llvm/lib/ExecutionEngine/JITLink/JITLinkGeneric.cpp (revision 41c41fcbc036d75e6b6ea0756857099befbb3313)
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->getLinkage() == Linkage::Weak
207             ? SymbolLookupFlags::WeaklyReferencedSymbol
208             : SymbolLookupFlags::RequiredSymbol;
209     UnresolvedExternals[Sym->getName()] = LookupFlags;
210   }
211   return UnresolvedExternals;
212 }
213 
214 void JITLinkerBase::applyLookupResult(AsyncLookupResult Result) {
215   for (auto *Sym : G->external_symbols()) {
216     assert(Sym->getOffset() == 0 &&
217            "External symbol is not at the start of its addressable block");
218     assert(!Sym->getAddress() && "Symbol already resolved");
219     assert(!Sym->isDefined() && "Symbol being resolved is already defined");
220     auto ResultI = Result.find(Sym->getName());
221     if (ResultI != Result.end())
222       Sym->getAddressable().setAddress(
223           orc::ExecutorAddr(ResultI->second.getAddress()));
224     else
225       assert(Sym->getLinkage() == Linkage::Weak &&
226              "Failed to resolve non-weak reference");
227   }
228 
229   LLVM_DEBUG({
230     dbgs() << "Externals after applying lookup result:\n";
231     for (auto *Sym : G->external_symbols())
232       dbgs() << "  " << Sym->getName() << ": "
233              << formatv("{0:x16}", Sym->getAddress().getValue()) << "\n";
234   });
235 }
236 
237 void JITLinkerBase::abandonAllocAndBailOut(std::unique_ptr<JITLinkerBase> Self,
238                                            Error Err) {
239   assert(Err && "Should not be bailing out on success value");
240   assert(Alloc && "can not call abandonAllocAndBailOut before allocation");
241   Alloc->abandon([S = std::move(Self), E1 = std::move(Err)](Error E2) mutable {
242     S->Ctx->notifyFailed(joinErrors(std::move(E1), std::move(E2)));
243   });
244 }
245 
246 void prune(LinkGraph &G) {
247   std::vector<Symbol *> Worklist;
248   DenseSet<Block *> VisitedBlocks;
249 
250   // Build the initial worklist from all symbols initially live.
251   for (auto *Sym : G.defined_symbols())
252     if (Sym->isLive())
253       Worklist.push_back(Sym);
254 
255   // Propagate live flags to all symbols reachable from the initial live set.
256   while (!Worklist.empty()) {
257     auto *Sym = Worklist.back();
258     Worklist.pop_back();
259 
260     auto &B = Sym->getBlock();
261 
262     // Skip addressables that we've visited before.
263     if (VisitedBlocks.count(&B))
264       continue;
265 
266     VisitedBlocks.insert(&B);
267 
268     for (auto &E : Sym->getBlock().edges()) {
269       // If the edge target is a defined symbol that is being newly marked live
270       // then add it to the worklist.
271       if (E.getTarget().isDefined() && !E.getTarget().isLive())
272         Worklist.push_back(&E.getTarget());
273 
274       // Mark the target live.
275       E.getTarget().setLive(true);
276     }
277   }
278 
279   // Collect all defined symbols to remove, then remove them.
280   {
281     LLVM_DEBUG(dbgs() << "Dead-stripping defined symbols:\n");
282     std::vector<Symbol *> SymbolsToRemove;
283     for (auto *Sym : G.defined_symbols())
284       if (!Sym->isLive())
285         SymbolsToRemove.push_back(Sym);
286     for (auto *Sym : SymbolsToRemove) {
287       LLVM_DEBUG(dbgs() << "  " << *Sym << "...\n");
288       G.removeDefinedSymbol(*Sym);
289     }
290   }
291 
292   // Delete any unused blocks.
293   {
294     LLVM_DEBUG(dbgs() << "Dead-stripping blocks:\n");
295     std::vector<Block *> BlocksToRemove;
296     for (auto *B : G.blocks())
297       if (!VisitedBlocks.count(B))
298         BlocksToRemove.push_back(B);
299     for (auto *B : BlocksToRemove) {
300       LLVM_DEBUG(dbgs() << "  " << *B << "...\n");
301       G.removeBlock(*B);
302     }
303   }
304 
305   // Collect all external symbols to remove, then remove them.
306   {
307     LLVM_DEBUG(dbgs() << "Removing unused external symbols:\n");
308     std::vector<Symbol *> SymbolsToRemove;
309     for (auto *Sym : G.external_symbols())
310       if (!Sym->isLive())
311         SymbolsToRemove.push_back(Sym);
312     for (auto *Sym : SymbolsToRemove) {
313       LLVM_DEBUG(dbgs() << "  " << *Sym << "...\n");
314       G.removeExternalSymbol(*Sym);
315     }
316   }
317 }
318 
319 } // end namespace jitlink
320 } // end namespace llvm
321