1 | //===- lib/Linker/IRMover.cpp ---------------------------------------------===// |
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 | #include "llvm/Linker/IRMover.h" |
10 | #include "LinkDiagnosticInfo.h" |
11 | #include "llvm/ADT/SetVector.h" |
12 | #include "llvm/ADT/SmallString.h" |
13 | #include "llvm/IR/AutoUpgrade.h" |
14 | #include "llvm/IR/Constants.h" |
15 | #include "llvm/IR/DebugInfoMetadata.h" |
16 | #include "llvm/IR/DiagnosticPrinter.h" |
17 | #include "llvm/IR/Function.h" |
18 | #include "llvm/IR/GVMaterializer.h" |
19 | #include "llvm/IR/GlobalValue.h" |
20 | #include "llvm/IR/Instruction.h" |
21 | #include "llvm/IR/Instructions.h" |
22 | #include "llvm/IR/Intrinsics.h" |
23 | #include "llvm/IR/Module.h" |
24 | #include "llvm/IR/PseudoProbe.h" |
25 | #include "llvm/IR/TypeFinder.h" |
26 | #include "llvm/Object/ModuleSymbolTable.h" |
27 | #include "llvm/Support/Error.h" |
28 | #include "llvm/TargetParser/Triple.h" |
29 | #include "llvm/Transforms/Utils/ValueMapper.h" |
30 | #include <optional> |
31 | #include <utility> |
32 | using namespace llvm; |
33 | |
34 | /// Most of the errors produced by this module are inconvertible StringErrors. |
35 | /// This convenience function lets us return one of those more easily. |
36 | static Error stringErr(const Twine &T) { |
37 | return make_error<StringError>(Args: T, Args: inconvertibleErrorCode()); |
38 | } |
39 | |
40 | //===----------------------------------------------------------------------===// |
41 | // TypeMap implementation. |
42 | //===----------------------------------------------------------------------===// |
43 | |
44 | namespace { |
45 | class TypeMapTy : public ValueMapTypeRemapper { |
46 | /// This is a mapping from a source type to a destination type to use. |
47 | DenseMap<Type *, Type *> MappedTypes; |
48 | |
49 | public: |
50 | TypeMapTy(IRMover::IdentifiedStructTypeSet &DstStructTypesSet) |
51 | : DstStructTypesSet(DstStructTypesSet) {} |
52 | |
53 | IRMover::IdentifiedStructTypeSet &DstStructTypesSet; |
54 | /// Indicate that the specified type in the destination module is conceptually |
55 | /// equivalent to the specified type in the source module. |
56 | void addTypeMapping(Type *DstTy, Type *SrcTy); |
57 | |
58 | /// Return the mapped type to use for the specified input type from the |
59 | /// source module. |
60 | Type *get(Type *SrcTy); |
61 | |
62 | FunctionType *get(FunctionType *T) { |
63 | return cast<FunctionType>(Val: get(SrcTy: (Type *)T)); |
64 | } |
65 | |
66 | private: |
67 | Type *remapType(Type *SrcTy) override { return get(SrcTy); } |
68 | |
69 | bool recursivelyAddMappingIfTypesAreIsomorphic(Type *DstTy, Type *SrcTy); |
70 | }; |
71 | } |
72 | |
73 | void TypeMapTy::addTypeMapping(Type *DstTy, Type *SrcTy) { |
74 | recursivelyAddMappingIfTypesAreIsomorphic(DstTy, SrcTy); |
75 | } |
76 | |
77 | /// Recursively walk this pair of types, returning true if they are isomorphic, |
78 | /// false if they are not. Types that were determined to be isomorphic are |
79 | /// added to MappedTypes. |
80 | bool TypeMapTy::recursivelyAddMappingIfTypesAreIsomorphic(Type *DstTy, |
81 | Type *SrcTy) { |
82 | // Two types with differing kinds are clearly not isomorphic. |
83 | if (DstTy->getTypeID() != SrcTy->getTypeID()) |
84 | return false; |
85 | |
86 | // If we have an entry in the MappedTypes table, then we have our answer. |
87 | Type *&Entry = MappedTypes[SrcTy]; |
88 | if (Entry) |
89 | return Entry == DstTy; |
90 | |
91 | // Two identical types are clearly isomorphic. Remember this |
92 | // non-speculatively. |
93 | if (DstTy == SrcTy) { |
94 | Entry = DstTy; |
95 | return true; |
96 | } |
97 | |
98 | // Okay, we have two types with identical kinds that we haven't seen before. |
99 | |
100 | // Always consider opaque struct types non-isomorphic. |
101 | if (StructType *SSTy = dyn_cast<StructType>(Val: SrcTy)) { |
102 | if (SSTy->isOpaque() || cast<StructType>(Val: DstTy)->isOpaque()) |
103 | return false; |
104 | } |
105 | |
106 | // If the number of subtypes disagree between the two types, then we fail. |
107 | if (SrcTy->getNumContainedTypes() != DstTy->getNumContainedTypes()) |
108 | return false; |
109 | |
110 | // Fail if any of the extra properties (e.g. array size) of the type disagree. |
111 | if (isa<IntegerType>(Val: DstTy)) |
112 | return false; // bitwidth disagrees. |
113 | if (PointerType *PT = dyn_cast<PointerType>(Val: DstTy)) { |
114 | if (PT->getAddressSpace() != cast<PointerType>(Val: SrcTy)->getAddressSpace()) |
115 | return false; |
116 | } else if (FunctionType *FT = dyn_cast<FunctionType>(Val: DstTy)) { |
117 | if (FT->isVarArg() != cast<FunctionType>(Val: SrcTy)->isVarArg()) |
118 | return false; |
119 | } else if (StructType *DSTy = dyn_cast<StructType>(Val: DstTy)) { |
120 | StructType *SSTy = cast<StructType>(Val: SrcTy); |
121 | if (DSTy->isLiteral() != SSTy->isLiteral() || |
122 | DSTy->isPacked() != SSTy->isPacked()) |
123 | return false; |
124 | } else if (auto *DArrTy = dyn_cast<ArrayType>(Val: DstTy)) { |
125 | if (DArrTy->getNumElements() != cast<ArrayType>(Val: SrcTy)->getNumElements()) |
126 | return false; |
127 | } else if (auto *DVecTy = dyn_cast<VectorType>(Val: DstTy)) { |
128 | if (DVecTy->getElementCount() != cast<VectorType>(Val: SrcTy)->getElementCount()) |
129 | return false; |
130 | } |
131 | |
132 | // Recursively check the subelements. |
133 | for (unsigned I = 0, E = SrcTy->getNumContainedTypes(); I != E; ++I) |
134 | if (!recursivelyAddMappingIfTypesAreIsomorphic(DstTy: DstTy->getContainedType(i: I), |
135 | SrcTy: SrcTy->getContainedType(i: I))) |
136 | return false; |
137 | |
138 | // If everything seems to have lined up, then everything is great. |
139 | [[maybe_unused]] auto Res = MappedTypes.insert(KV: {SrcTy, DstTy}); |
140 | assert(!Res.second && "Recursive type?" ); |
141 | |
142 | if (auto *STy = dyn_cast<StructType>(Val: SrcTy)) { |
143 | // We clear name of SrcTy to lower amount of renaming in LLVM context. |
144 | // Renaming occurs because we load all source modules to the same context |
145 | // and declaration with existing name gets renamed (i.e Foo -> Foo.42). |
146 | // As a result we may get several different types in the destination |
147 | // module, which are in fact the same. |
148 | if (STy->hasName()) |
149 | STy->setName("" ); |
150 | } |
151 | |
152 | return true; |
153 | } |
154 | |
155 | Type *TypeMapTy::get(Type *Ty) { |
156 | // If we already have an entry for this type, return it. |
157 | Type **Entry = &MappedTypes[Ty]; |
158 | if (*Entry) |
159 | return *Entry; |
160 | |
161 | // These are types that LLVM itself will unique. |
162 | bool IsUniqued = !isa<StructType>(Val: Ty) || cast<StructType>(Val: Ty)->isLiteral(); |
163 | |
164 | if (!IsUniqued) { |
165 | #ifndef NDEBUG |
166 | for (auto &Pair : MappedTypes) { |
167 | assert(!(Pair.first != Ty && Pair.second == Ty) && |
168 | "mapping to a source type" ); |
169 | } |
170 | #endif |
171 | } |
172 | |
173 | // If this is not a recursive type, then just map all of the elements and |
174 | // then rebuild the type from inside out. |
175 | SmallVector<Type *, 4> ElementTypes; |
176 | |
177 | // If there are no element types to map, then the type is itself. This is |
178 | // true for the anonymous {} struct, things like 'float', integers, etc. |
179 | if (Ty->getNumContainedTypes() == 0 && IsUniqued) |
180 | return *Entry = Ty; |
181 | |
182 | // Remap all of the elements, keeping track of whether any of them change. |
183 | bool AnyChange = false; |
184 | ElementTypes.resize(N: Ty->getNumContainedTypes()); |
185 | for (unsigned I = 0, E = Ty->getNumContainedTypes(); I != E; ++I) { |
186 | ElementTypes[I] = get(Ty: Ty->getContainedType(i: I)); |
187 | AnyChange |= ElementTypes[I] != Ty->getContainedType(i: I); |
188 | } |
189 | |
190 | // Refresh Entry after recursively processing stuff. |
191 | Entry = &MappedTypes[Ty]; |
192 | assert(!*Entry && "Recursive type!" ); |
193 | |
194 | // If all of the element types mapped directly over and the type is not |
195 | // a named struct, then the type is usable as-is. |
196 | if (!AnyChange && IsUniqued) |
197 | return *Entry = Ty; |
198 | |
199 | // Otherwise, rebuild a modified type. |
200 | switch (Ty->getTypeID()) { |
201 | default: |
202 | llvm_unreachable("unknown derived type to remap" ); |
203 | case Type::ArrayTyID: |
204 | return *Entry = ArrayType::get(ElementType: ElementTypes[0], |
205 | NumElements: cast<ArrayType>(Val: Ty)->getNumElements()); |
206 | case Type::ScalableVectorTyID: |
207 | case Type::FixedVectorTyID: |
208 | return *Entry = VectorType::get(ElementType: ElementTypes[0], |
209 | EC: cast<VectorType>(Val: Ty)->getElementCount()); |
210 | case Type::FunctionTyID: |
211 | return *Entry = FunctionType::get(Result: ElementTypes[0], |
212 | Params: ArrayRef(ElementTypes).slice(N: 1), |
213 | isVarArg: cast<FunctionType>(Val: Ty)->isVarArg()); |
214 | case Type::StructTyID: { |
215 | auto *STy = cast<StructType>(Val: Ty); |
216 | bool IsPacked = STy->isPacked(); |
217 | if (IsUniqued) |
218 | return *Entry = StructType::get(Context&: Ty->getContext(), Elements: ElementTypes, isPacked: IsPacked); |
219 | |
220 | // If the type is opaque, we can just use it directly. |
221 | if (STy->isOpaque()) { |
222 | DstStructTypesSet.addOpaque(Ty: STy); |
223 | return *Entry = Ty; |
224 | } |
225 | |
226 | if (StructType *OldT = |
227 | DstStructTypesSet.findNonOpaque(ETypes: ElementTypes, IsPacked)) { |
228 | STy->setName("" ); |
229 | return *Entry = OldT; |
230 | } |
231 | |
232 | if (!AnyChange) { |
233 | DstStructTypesSet.addNonOpaque(Ty: STy); |
234 | return *Entry = Ty; |
235 | } |
236 | |
237 | StructType *DTy = |
238 | StructType::create(Context&: Ty->getContext(), Elements: ElementTypes, Name: "" , isPacked: STy->isPacked()); |
239 | |
240 | // Steal STy's name. |
241 | if (STy->hasName()) { |
242 | SmallString<16> TmpName = STy->getName(); |
243 | STy->setName("" ); |
244 | DTy->setName(TmpName); |
245 | } |
246 | |
247 | DstStructTypesSet.addNonOpaque(Ty: DTy); |
248 | return *Entry = DTy; |
249 | } |
250 | } |
251 | } |
252 | |
253 | LinkDiagnosticInfo::LinkDiagnosticInfo(DiagnosticSeverity Severity, |
254 | const Twine &Msg) |
255 | : DiagnosticInfo(DK_Linker, Severity), Msg(Msg) {} |
256 | void LinkDiagnosticInfo::print(DiagnosticPrinter &DP) const { DP << Msg; } |
257 | |
258 | //===----------------------------------------------------------------------===// |
259 | // IRLinker implementation. |
260 | //===----------------------------------------------------------------------===// |
261 | |
262 | namespace { |
263 | class IRLinker; |
264 | |
265 | /// Creates prototypes for functions that are lazily linked on the fly. This |
266 | /// speeds up linking for modules with many/ lazily linked functions of which |
267 | /// few get used. |
268 | class GlobalValueMaterializer final : public ValueMaterializer { |
269 | IRLinker &TheIRLinker; |
270 | |
271 | public: |
272 | GlobalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {} |
273 | Value *materialize(Value *V) override; |
274 | }; |
275 | |
276 | class LocalValueMaterializer final : public ValueMaterializer { |
277 | IRLinker &TheIRLinker; |
278 | |
279 | public: |
280 | LocalValueMaterializer(IRLinker &TheIRLinker) : TheIRLinker(TheIRLinker) {} |
281 | Value *materialize(Value *V) override; |
282 | }; |
283 | |
284 | /// Type of the Metadata map in \a ValueToValueMapTy. |
285 | typedef DenseMap<const Metadata *, TrackingMDRef> MDMapT; |
286 | |
287 | /// This is responsible for keeping track of the state used for moving data |
288 | /// from SrcM to DstM. |
289 | class IRLinker { |
290 | Module &DstM; |
291 | std::unique_ptr<Module> SrcM; |
292 | |
293 | /// See IRMover::move(). |
294 | IRMover::LazyCallback AddLazyFor; |
295 | |
296 | TypeMapTy TypeMap; |
297 | GlobalValueMaterializer GValMaterializer; |
298 | LocalValueMaterializer LValMaterializer; |
299 | |
300 | /// A metadata map that's shared between IRLinker instances. |
301 | MDMapT &SharedMDs; |
302 | |
303 | /// Mapping of values from what they used to be in Src, to what they are now |
304 | /// in DstM. ValueToValueMapTy is a ValueMap, which involves some overhead |
305 | /// due to the use of Value handles which the Linker doesn't actually need, |
306 | /// but this allows us to reuse the ValueMapper code. |
307 | ValueToValueMapTy ValueMap; |
308 | ValueToValueMapTy IndirectSymbolValueMap; |
309 | |
310 | DenseSet<GlobalValue *> ValuesToLink; |
311 | std::vector<GlobalValue *> Worklist; |
312 | std::vector<std::pair<GlobalValue *, Value*>> RAUWWorklist; |
313 | |
314 | /// Set of globals with eagerly copied metadata that may require remapping. |
315 | /// This remapping is performed after metadata linking. |
316 | DenseSet<GlobalObject *> UnmappedMetadata; |
317 | |
318 | void maybeAdd(GlobalValue *GV) { |
319 | if (ValuesToLink.insert(V: GV).second) |
320 | Worklist.push_back(x: GV); |
321 | } |
322 | |
323 | /// Whether we are importing globals for ThinLTO, as opposed to linking the |
324 | /// source module. If this flag is set, it means that we can rely on some |
325 | /// other object file to define any non-GlobalValue entities defined by the |
326 | /// source module. This currently causes us to not link retained types in |
327 | /// debug info metadata and module inline asm. |
328 | bool IsPerformingImport; |
329 | |
330 | /// Set to true when all global value body linking is complete (including |
331 | /// lazy linking). Used to prevent metadata linking from creating new |
332 | /// references. |
333 | bool DoneLinkingBodies = false; |
334 | |
335 | /// The Error encountered during materialization. We use an Optional here to |
336 | /// avoid needing to manage an unconsumed success value. |
337 | std::optional<Error> FoundError; |
338 | void setError(Error E) { |
339 | if (E) |
340 | FoundError = std::move(E); |
341 | } |
342 | |
343 | /// Entry point for mapping values and alternate context for mapping aliases. |
344 | ValueMapper Mapper; |
345 | unsigned IndirectSymbolMCID; |
346 | |
347 | /// Handles cloning of a global values from the source module into |
348 | /// the destination module, including setting the attributes and visibility. |
349 | GlobalValue *copyGlobalValueProto(const GlobalValue *SGV, bool ForDefinition); |
350 | |
351 | void emitWarning(const Twine &Message) { |
352 | SrcM->getContext().diagnose(DI: LinkDiagnosticInfo(DS_Warning, Message)); |
353 | } |
354 | |
355 | /// Given a global in the source module, return the global in the |
356 | /// destination module that is being linked to, if any. |
357 | GlobalValue *getLinkedToGlobal(const GlobalValue *SrcGV) { |
358 | // If the source has no name it can't link. If it has local linkage, |
359 | // there is no name match-up going on. |
360 | if (!SrcGV->hasName() || SrcGV->hasLocalLinkage()) |
361 | return nullptr; |
362 | |
363 | // Otherwise see if we have a match in the destination module's symtab. |
364 | GlobalValue *DGV = DstM.getNamedValue(Name: SrcGV->getName()); |
365 | if (!DGV) |
366 | return nullptr; |
367 | |
368 | // If we found a global with the same name in the dest module, but it has |
369 | // internal linkage, we are really not doing any linkage here. |
370 | if (DGV->hasLocalLinkage()) |
371 | return nullptr; |
372 | |
373 | // If we found an intrinsic declaration with mismatching prototypes, we |
374 | // probably had a nameclash. Don't use that version. |
375 | if (auto *FDGV = dyn_cast<Function>(Val: DGV)) |
376 | if (FDGV->isIntrinsic()) |
377 | if (const auto *FSrcGV = dyn_cast<Function>(Val: SrcGV)) |
378 | if (FDGV->getFunctionType() != TypeMap.get(T: FSrcGV->getFunctionType())) |
379 | return nullptr; |
380 | |
381 | // Otherwise, we do in fact link to the destination global. |
382 | return DGV; |
383 | } |
384 | |
385 | void computeTypeMapping(); |
386 | |
387 | Expected<Constant *> linkAppendingVarProto(GlobalVariable *DstGV, |
388 | const GlobalVariable *SrcGV); |
389 | |
390 | /// Given the GlobaValue \p SGV in the source module, and the matching |
391 | /// GlobalValue \p DGV (if any), return true if the linker will pull \p SGV |
392 | /// into the destination module. |
393 | /// |
394 | /// Note this code may call the client-provided \p AddLazyFor. |
395 | bool shouldLink(GlobalValue *DGV, GlobalValue &SGV); |
396 | Expected<Constant *> linkGlobalValueProto(GlobalValue *GV, |
397 | bool ForIndirectSymbol); |
398 | |
399 | Error linkModuleFlagsMetadata(); |
400 | |
401 | void linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src); |
402 | Error linkFunctionBody(Function &Dst, Function &Src); |
403 | void linkAliasAliasee(GlobalAlias &Dst, GlobalAlias &Src); |
404 | void linkIFuncResolver(GlobalIFunc &Dst, GlobalIFunc &Src); |
405 | Error linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src); |
406 | |
407 | /// Replace all types in the source AttributeList with the |
408 | /// corresponding destination type. |
409 | AttributeList mapAttributeTypes(LLVMContext &C, AttributeList Attrs); |
410 | |
411 | /// Functions that take care of cloning a specific global value type |
412 | /// into the destination module. |
413 | GlobalVariable *copyGlobalVariableProto(const GlobalVariable *SGVar); |
414 | Function *copyFunctionProto(const Function *SF); |
415 | GlobalValue *copyIndirectSymbolProto(const GlobalValue *SGV); |
416 | |
417 | /// Perform "replace all uses with" operations. These work items need to be |
418 | /// performed as part of materialization, but we postpone them to happen after |
419 | /// materialization is done. The materializer called by ValueMapper is not |
420 | /// expected to delete constants, as ValueMapper is holding pointers to some |
421 | /// of them, but constant destruction may be indirectly triggered by RAUW. |
422 | /// Hence, the need to move this out of the materialization call chain. |
423 | void flushRAUWWorklist(); |
424 | |
425 | /// When importing for ThinLTO, prevent importing of types listed on |
426 | /// the DICompileUnit that we don't need a copy of in the importing |
427 | /// module. |
428 | void prepareCompileUnitsForImport(); |
429 | void linkNamedMDNodes(); |
430 | |
431 | /// Update attributes while linking. |
432 | void updateAttributes(GlobalValue &GV); |
433 | |
434 | public: |
435 | IRLinker(Module &DstM, MDMapT &SharedMDs, |
436 | IRMover::IdentifiedStructTypeSet &Set, std::unique_ptr<Module> SrcM, |
437 | ArrayRef<GlobalValue *> ValuesToLink, |
438 | IRMover::LazyCallback AddLazyFor, bool IsPerformingImport) |
439 | : DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(std::move(AddLazyFor)), |
440 | TypeMap(Set), GValMaterializer(*this), LValMaterializer(*this), |
441 | SharedMDs(SharedMDs), IsPerformingImport(IsPerformingImport), |
442 | Mapper(ValueMap, RF_ReuseAndMutateDistinctMDs | RF_IgnoreMissingLocals, |
443 | &TypeMap, &GValMaterializer), |
444 | IndirectSymbolMCID(Mapper.registerAlternateMappingContext( |
445 | VM&: IndirectSymbolValueMap, Materializer: &LValMaterializer)) { |
446 | ValueMap.getMDMap() = std::move(SharedMDs); |
447 | for (GlobalValue *GV : ValuesToLink) |
448 | maybeAdd(GV); |
449 | if (IsPerformingImport) |
450 | prepareCompileUnitsForImport(); |
451 | } |
452 | ~IRLinker() { SharedMDs = std::move(*ValueMap.getMDMap()); } |
453 | |
454 | Error run(); |
455 | Value *materialize(Value *V, bool ForIndirectSymbol); |
456 | }; |
457 | } |
458 | |
459 | /// The LLVM SymbolTable class autorenames globals that conflict in the symbol |
460 | /// table. This is good for all clients except for us. Go through the trouble |
461 | /// to force this back. |
462 | static void forceRenaming(GlobalValue *GV, StringRef Name) { |
463 | // If the global doesn't force its name or if it already has the right name, |
464 | // there is nothing for us to do. |
465 | if (GV->hasLocalLinkage() || GV->getName() == Name) |
466 | return; |
467 | |
468 | Module *M = GV->getParent(); |
469 | |
470 | // If there is a conflict, rename the conflict. |
471 | if (GlobalValue *ConflictGV = M->getNamedValue(Name)) { |
472 | GV->takeName(V: ConflictGV); |
473 | ConflictGV->setName(Name); // This will cause ConflictGV to get renamed |
474 | assert(ConflictGV->getName() != Name && "forceRenaming didn't work" ); |
475 | } else { |
476 | GV->setName(Name); // Force the name back |
477 | } |
478 | } |
479 | |
480 | Value *GlobalValueMaterializer::materialize(Value *SGV) { |
481 | return TheIRLinker.materialize(V: SGV, ForIndirectSymbol: false); |
482 | } |
483 | |
484 | Value *LocalValueMaterializer::materialize(Value *SGV) { |
485 | return TheIRLinker.materialize(V: SGV, ForIndirectSymbol: true); |
486 | } |
487 | |
488 | Value *IRLinker::materialize(Value *V, bool ForIndirectSymbol) { |
489 | auto *SGV = dyn_cast<GlobalValue>(Val: V); |
490 | if (!SGV) |
491 | return nullptr; |
492 | |
493 | // If SGV is from dest, it was already materialized when dest was loaded. |
494 | if (SGV->getParent() == &DstM) |
495 | return nullptr; |
496 | |
497 | // When linking a global from other modules than source & dest, skip |
498 | // materializing it because it would be mapped later when its containing |
499 | // module is linked. Linking it now would potentially pull in many types that |
500 | // may not be mapped properly. |
501 | if (SGV->getParent() != SrcM.get()) |
502 | return nullptr; |
503 | |
504 | Expected<Constant *> NewProto = linkGlobalValueProto(GV: SGV, ForIndirectSymbol); |
505 | if (!NewProto) { |
506 | setError(NewProto.takeError()); |
507 | return nullptr; |
508 | } |
509 | if (!*NewProto) |
510 | return nullptr; |
511 | |
512 | GlobalValue *New = dyn_cast<GlobalValue>(Val: *NewProto); |
513 | if (!New) |
514 | return *NewProto; |
515 | |
516 | // If we already created the body, just return. |
517 | if (auto *F = dyn_cast<Function>(Val: New)) { |
518 | if (!F->isDeclaration()) |
519 | return New; |
520 | } else if (auto *V = dyn_cast<GlobalVariable>(Val: New)) { |
521 | if (V->hasInitializer() || V->hasAppendingLinkage()) |
522 | return New; |
523 | } else if (auto *GA = dyn_cast<GlobalAlias>(Val: New)) { |
524 | if (GA->getAliasee()) |
525 | return New; |
526 | } else if (auto *GI = dyn_cast<GlobalIFunc>(Val: New)) { |
527 | if (GI->getResolver()) |
528 | return New; |
529 | } else { |
530 | llvm_unreachable("Invalid GlobalValue type" ); |
531 | } |
532 | |
533 | // If the global is being linked for an indirect symbol, it may have already |
534 | // been scheduled to satisfy a regular symbol. Similarly, a global being linked |
535 | // for a regular symbol may have already been scheduled for an indirect |
536 | // symbol. Check for these cases by looking in the other value map and |
537 | // confirming the same value has been scheduled. If there is an entry in the |
538 | // ValueMap but the value is different, it means that the value already had a |
539 | // definition in the destination module (linkonce for instance), but we need a |
540 | // new definition for the indirect symbol ("New" will be different). |
541 | if ((ForIndirectSymbol && ValueMap.lookup(Val: SGV) == New) || |
542 | (!ForIndirectSymbol && IndirectSymbolValueMap.lookup(Val: SGV) == New)) |
543 | return New; |
544 | |
545 | if (ForIndirectSymbol || shouldLink(DGV: New, SGV&: *SGV)) |
546 | setError(linkGlobalValueBody(Dst&: *New, Src&: *SGV)); |
547 | |
548 | updateAttributes(GV&: *New); |
549 | return New; |
550 | } |
551 | |
552 | /// Loop through the global variables in the src module and merge them into the |
553 | /// dest module. |
554 | GlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) { |
555 | // No linking to be performed or linking from the source: simply create an |
556 | // identical version of the symbol over in the dest module... the |
557 | // initializer will be filled in later by LinkGlobalInits. |
558 | GlobalVariable *NewDGV = |
559 | new GlobalVariable(DstM, TypeMap.get(Ty: SGVar->getValueType()), |
560 | SGVar->isConstant(), GlobalValue::ExternalLinkage, |
561 | /*init*/ nullptr, SGVar->getName(), |
562 | /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(), |
563 | SGVar->getAddressSpace()); |
564 | NewDGV->setAlignment(SGVar->getAlign()); |
565 | NewDGV->copyAttributesFrom(Src: SGVar); |
566 | return NewDGV; |
567 | } |
568 | |
569 | AttributeList IRLinker::mapAttributeTypes(LLVMContext &C, AttributeList Attrs) { |
570 | for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) { |
571 | for (int AttrIdx = Attribute::FirstTypeAttr; |
572 | AttrIdx <= Attribute::LastTypeAttr; AttrIdx++) { |
573 | Attribute::AttrKind TypedAttr = (Attribute::AttrKind)AttrIdx; |
574 | if (Attrs.hasAttributeAtIndex(Index: i, Kind: TypedAttr)) { |
575 | if (Type *Ty = |
576 | Attrs.getAttributeAtIndex(Index: i, Kind: TypedAttr).getValueAsType()) { |
577 | Attrs = Attrs.replaceAttributeTypeAtIndex(C, ArgNo: i, Kind: TypedAttr, |
578 | ReplacementTy: TypeMap.get(Ty)); |
579 | break; |
580 | } |
581 | } |
582 | } |
583 | } |
584 | return Attrs; |
585 | } |
586 | |
587 | /// Link the function in the source module into the destination module if |
588 | /// needed, setting up mapping information. |
589 | Function *IRLinker::copyFunctionProto(const Function *SF) { |
590 | // If there is no linkage to be performed or we are linking from the source, |
591 | // bring SF over. |
592 | auto *F = Function::Create(Ty: TypeMap.get(T: SF->getFunctionType()), |
593 | Linkage: GlobalValue::ExternalLinkage, |
594 | AddrSpace: SF->getAddressSpace(), N: SF->getName(), M: &DstM); |
595 | F->copyAttributesFrom(Src: SF); |
596 | F->setAttributes(mapAttributeTypes(C&: F->getContext(), Attrs: F->getAttributes())); |
597 | return F; |
598 | } |
599 | |
600 | /// Set up prototypes for any indirect symbols that come over from the source |
601 | /// module. |
602 | GlobalValue *IRLinker::copyIndirectSymbolProto(const GlobalValue *SGV) { |
603 | // If there is no linkage to be performed or we're linking from the source, |
604 | // bring over SGA. |
605 | auto *Ty = TypeMap.get(Ty: SGV->getValueType()); |
606 | |
607 | if (auto *GA = dyn_cast<GlobalAlias>(Val: SGV)) { |
608 | auto *DGA = GlobalAlias::create(Ty, AddressSpace: SGV->getAddressSpace(), |
609 | Linkage: GlobalValue::ExternalLinkage, |
610 | Name: SGV->getName(), Parent: &DstM); |
611 | DGA->copyAttributesFrom(Src: GA); |
612 | return DGA; |
613 | } |
614 | |
615 | if (auto *GI = dyn_cast<GlobalIFunc>(Val: SGV)) { |
616 | auto *DGI = GlobalIFunc::create(Ty, AddressSpace: SGV->getAddressSpace(), |
617 | Linkage: GlobalValue::ExternalLinkage, |
618 | Name: SGV->getName(), Resolver: nullptr, Parent: &DstM); |
619 | DGI->copyAttributesFrom(Src: GI); |
620 | return DGI; |
621 | } |
622 | |
623 | llvm_unreachable("Invalid source global value type" ); |
624 | } |
625 | |
626 | GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV, |
627 | bool ForDefinition) { |
628 | GlobalValue *NewGV; |
629 | if (auto *SGVar = dyn_cast<GlobalVariable>(Val: SGV)) { |
630 | NewGV = copyGlobalVariableProto(SGVar); |
631 | } else if (auto *SF = dyn_cast<Function>(Val: SGV)) { |
632 | NewGV = copyFunctionProto(SF); |
633 | } else { |
634 | if (ForDefinition) |
635 | NewGV = copyIndirectSymbolProto(SGV); |
636 | else if (SGV->getValueType()->isFunctionTy()) |
637 | NewGV = |
638 | Function::Create(Ty: cast<FunctionType>(Val: TypeMap.get(Ty: SGV->getValueType())), |
639 | Linkage: GlobalValue::ExternalLinkage, AddrSpace: SGV->getAddressSpace(), |
640 | N: SGV->getName(), M: &DstM); |
641 | else |
642 | NewGV = |
643 | new GlobalVariable(DstM, TypeMap.get(Ty: SGV->getValueType()), |
644 | /*isConstant*/ false, GlobalValue::ExternalLinkage, |
645 | /*init*/ nullptr, SGV->getName(), |
646 | /*insertbefore*/ nullptr, |
647 | SGV->getThreadLocalMode(), SGV->getAddressSpace()); |
648 | } |
649 | |
650 | if (ForDefinition) |
651 | NewGV->setLinkage(SGV->getLinkage()); |
652 | else if (SGV->hasExternalWeakLinkage()) |
653 | NewGV->setLinkage(GlobalValue::ExternalWeakLinkage); |
654 | |
655 | if (auto *NewGO = dyn_cast<GlobalObject>(Val: NewGV)) { |
656 | // Metadata for global variables and function declarations is copied eagerly. |
657 | if (isa<GlobalVariable>(Val: SGV) || SGV->isDeclaration()) { |
658 | NewGO->copyMetadata(Src: cast<GlobalObject>(Val: SGV), Offset: 0); |
659 | if (SGV->isDeclaration() && NewGO->hasMetadata()) |
660 | UnmappedMetadata.insert(V: NewGO); |
661 | } |
662 | } |
663 | |
664 | // Remove these copied constants in case this stays a declaration, since |
665 | // they point to the source module. If the def is linked the values will |
666 | // be mapped in during linkFunctionBody. |
667 | if (auto *NewF = dyn_cast<Function>(Val: NewGV)) { |
668 | NewF->setPersonalityFn(nullptr); |
669 | NewF->setPrefixData(nullptr); |
670 | NewF->setPrologueData(nullptr); |
671 | } |
672 | |
673 | return NewGV; |
674 | } |
675 | |
676 | static StringRef getTypeNamePrefix(StringRef Name) { |
677 | size_t DotPos = Name.rfind(C: '.'); |
678 | return (DotPos == 0 || DotPos == StringRef::npos || Name.back() == '.' || |
679 | !isdigit(static_cast<unsigned char>(Name[DotPos + 1]))) |
680 | ? Name |
681 | : Name.substr(Start: 0, N: DotPos); |
682 | } |
683 | |
684 | /// Loop over all of the linked values to compute type mappings. For example, |
685 | /// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct |
686 | /// types 'Foo' but one got renamed when the module was loaded into the same |
687 | /// LLVMContext. |
688 | void IRLinker::computeTypeMapping() { |
689 | for (GlobalValue &SGV : SrcM->globals()) { |
690 | GlobalValue *DGV = getLinkedToGlobal(SrcGV: &SGV); |
691 | if (!DGV) |
692 | continue; |
693 | |
694 | if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) { |
695 | TypeMap.addTypeMapping(DstTy: DGV->getType(), SrcTy: SGV.getType()); |
696 | continue; |
697 | } |
698 | |
699 | // Unify the element type of appending arrays. |
700 | ArrayType *DAT = cast<ArrayType>(Val: DGV->getValueType()); |
701 | ArrayType *SAT = cast<ArrayType>(Val: SGV.getValueType()); |
702 | TypeMap.addTypeMapping(DstTy: DAT->getElementType(), SrcTy: SAT->getElementType()); |
703 | } |
704 | |
705 | for (GlobalValue &SGV : *SrcM) |
706 | if (GlobalValue *DGV = getLinkedToGlobal(SrcGV: &SGV)) { |
707 | if (DGV->getType() == SGV.getType()) { |
708 | // If the types of DGV and SGV are the same, it means that DGV is from |
709 | // the source module and got added to DstM from a shared metadata. We |
710 | // shouldn't map this type to itself in case the type's components get |
711 | // remapped to a new type from DstM (for instance, during the loop over |
712 | // SrcM->getIdentifiedStructTypes() below). |
713 | continue; |
714 | } |
715 | |
716 | TypeMap.addTypeMapping(DstTy: DGV->getType(), SrcTy: SGV.getType()); |
717 | } |
718 | |
719 | for (GlobalValue &SGV : SrcM->aliases()) |
720 | if (GlobalValue *DGV = getLinkedToGlobal(SrcGV: &SGV)) |
721 | TypeMap.addTypeMapping(DstTy: DGV->getType(), SrcTy: SGV.getType()); |
722 | |
723 | // Incorporate types by name, scanning all the types in the source module. |
724 | // At this point, the destination module may have a type "%foo = { i32 }" for |
725 | // example. When the source module got loaded into the same LLVMContext, if |
726 | // it had the same type, it would have been renamed to "%foo.42 = { i32 }". |
727 | std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes(); |
728 | for (StructType *ST : Types) { |
729 | if (!ST->hasName()) |
730 | continue; |
731 | |
732 | if (TypeMap.DstStructTypesSet.hasType(Ty: ST)) { |
733 | // This is actually a type from the destination module. |
734 | // getIdentifiedStructTypes() can have found it by walking debug info |
735 | // metadata nodes, some of which get linked by name when ODR Type Uniquing |
736 | // is enabled on the Context, from the source to the destination module. |
737 | continue; |
738 | } |
739 | |
740 | auto STTypePrefix = getTypeNamePrefix(Name: ST->getName()); |
741 | if (STTypePrefix.size() == ST->getName().size()) |
742 | continue; |
743 | |
744 | // Check to see if the destination module has a struct with the prefix name. |
745 | StructType *DST = StructType::getTypeByName(C&: ST->getContext(), Name: STTypePrefix); |
746 | if (!DST) |
747 | continue; |
748 | |
749 | // Don't use it if this actually came from the source module. They're in |
750 | // the same LLVMContext after all. Also don't use it unless the type is |
751 | // actually used in the destination module. This can happen in situations |
752 | // like this: |
753 | // |
754 | // Module A Module B |
755 | // -------- -------- |
756 | // %Z = type { %A } %B = type { %C.1 } |
757 | // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* } |
758 | // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] } |
759 | // %C = type { i8* } %B.3 = type { %C.1 } |
760 | // |
761 | // When we link Module B with Module A, the '%B' in Module B is |
762 | // used. However, that would then use '%C.1'. But when we process '%C.1', |
763 | // we prefer to take the '%C' version. So we are then left with both |
764 | // '%C.1' and '%C' being used for the same types. This leads to some |
765 | // variables using one type and some using the other. |
766 | if (TypeMap.DstStructTypesSet.hasType(Ty: DST)) |
767 | TypeMap.addTypeMapping(DstTy: DST, SrcTy: ST); |
768 | } |
769 | } |
770 | |
771 | static void getArrayElements(const Constant *C, |
772 | SmallVectorImpl<Constant *> &Dest) { |
773 | unsigned NumElements = cast<ArrayType>(Val: C->getType())->getNumElements(); |
774 | |
775 | for (unsigned i = 0; i != NumElements; ++i) |
776 | Dest.push_back(Elt: C->getAggregateElement(Elt: i)); |
777 | } |
778 | |
779 | /// If there were any appending global variables, link them together now. |
780 | Expected<Constant *> |
781 | IRLinker::linkAppendingVarProto(GlobalVariable *DstGV, |
782 | const GlobalVariable *SrcGV) { |
783 | // Check that both variables have compatible properties. |
784 | if (DstGV && !DstGV->isDeclaration() && !SrcGV->isDeclaration()) { |
785 | if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage()) |
786 | return stringErr( |
787 | T: "Linking globals named '" + SrcGV->getName() + |
788 | "': can only link appending global with another appending " |
789 | "global!" ); |
790 | |
791 | if (DstGV->isConstant() != SrcGV->isConstant()) |
792 | return stringErr(T: "Appending variables linked with different const'ness!" ); |
793 | |
794 | if (DstGV->getAlign() != SrcGV->getAlign()) |
795 | return stringErr( |
796 | T: "Appending variables with different alignment need to be linked!" ); |
797 | |
798 | if (DstGV->getVisibility() != SrcGV->getVisibility()) |
799 | return stringErr( |
800 | T: "Appending variables with different visibility need to be linked!" ); |
801 | |
802 | if (DstGV->hasGlobalUnnamedAddr() != SrcGV->hasGlobalUnnamedAddr()) |
803 | return stringErr( |
804 | T: "Appending variables with different unnamed_addr need to be linked!" ); |
805 | |
806 | if (DstGV->getSection() != SrcGV->getSection()) |
807 | return stringErr( |
808 | T: "Appending variables with different section name need to be linked!" ); |
809 | |
810 | if (DstGV->getAddressSpace() != SrcGV->getAddressSpace()) |
811 | return stringErr(T: "Appending variables with different address spaces need " |
812 | "to be linked!" ); |
813 | } |
814 | |
815 | // Do not need to do anything if source is a declaration. |
816 | if (SrcGV->isDeclaration()) |
817 | return DstGV; |
818 | |
819 | Type *EltTy = cast<ArrayType>(Val: TypeMap.get(Ty: SrcGV->getValueType())) |
820 | ->getElementType(); |
821 | |
822 | // FIXME: This upgrade is done during linking to support the C API. Once the |
823 | // old form is deprecated, we should move this upgrade to |
824 | // llvm::UpgradeGlobalVariable() and simplify the logic here and in |
825 | // Mapper::mapAppendingVariable() in ValueMapper.cpp. |
826 | StringRef Name = SrcGV->getName(); |
827 | bool IsNewStructor = false; |
828 | bool IsOldStructor = false; |
829 | if (Name == "llvm.global_ctors" || Name == "llvm.global_dtors" ) { |
830 | if (cast<StructType>(Val: EltTy)->getNumElements() == 3) |
831 | IsNewStructor = true; |
832 | else |
833 | IsOldStructor = true; |
834 | } |
835 | |
836 | PointerType *VoidPtrTy = PointerType::get(C&: SrcGV->getContext(), AddressSpace: 0); |
837 | if (IsOldStructor) { |
838 | auto &ST = *cast<StructType>(Val: EltTy); |
839 | Type *Tys[3] = {ST.getElementType(N: 0), ST.getElementType(N: 1), VoidPtrTy}; |
840 | EltTy = StructType::get(Context&: SrcGV->getContext(), Elements: Tys, isPacked: false); |
841 | } |
842 | |
843 | uint64_t DstNumElements = 0; |
844 | if (DstGV && !DstGV->isDeclaration()) { |
845 | ArrayType *DstTy = cast<ArrayType>(Val: DstGV->getValueType()); |
846 | DstNumElements = DstTy->getNumElements(); |
847 | |
848 | // Check to see that they two arrays agree on type. |
849 | if (EltTy != DstTy->getElementType()) |
850 | return stringErr(T: "Appending variables with different element types!" ); |
851 | } |
852 | |
853 | SmallVector<Constant *, 16> SrcElements; |
854 | getArrayElements(C: SrcGV->getInitializer(), Dest&: SrcElements); |
855 | |
856 | if (IsNewStructor) { |
857 | erase_if(C&: SrcElements, P: [this](Constant *E) { |
858 | auto *Key = |
859 | dyn_cast<GlobalValue>(Val: E->getAggregateElement(Elt: 2)->stripPointerCasts()); |
860 | if (!Key) |
861 | return false; |
862 | GlobalValue *DGV = getLinkedToGlobal(SrcGV: Key); |
863 | return !shouldLink(DGV, SGV&: *Key); |
864 | }); |
865 | } |
866 | uint64_t NewSize = DstNumElements + SrcElements.size(); |
867 | ArrayType *NewType = ArrayType::get(ElementType: EltTy, NumElements: NewSize); |
868 | |
869 | // Create the new global variable. |
870 | GlobalVariable *NG = new GlobalVariable( |
871 | DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(), |
872 | /*init*/ nullptr, /*name*/ "" , DstGV, SrcGV->getThreadLocalMode(), |
873 | SrcGV->getAddressSpace()); |
874 | |
875 | NG->copyAttributesFrom(Src: SrcGV); |
876 | forceRenaming(GV: NG, Name: SrcGV->getName()); |
877 | |
878 | Mapper.scheduleMapAppendingVariable( |
879 | GV&: *NG, |
880 | InitPrefix: (DstGV && !DstGV->isDeclaration()) ? DstGV->getInitializer() : nullptr, |
881 | IsOldCtorDtor: IsOldStructor, NewMembers: SrcElements); |
882 | |
883 | // Replace any uses of the two global variables with uses of the new |
884 | // global. |
885 | if (DstGV) { |
886 | RAUWWorklist.push_back(x: std::make_pair(x&: DstGV, y&: NG)); |
887 | } |
888 | |
889 | return NG; |
890 | } |
891 | |
892 | bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) { |
893 | if (ValuesToLink.count(V: &SGV) || SGV.hasLocalLinkage()) |
894 | return true; |
895 | |
896 | if (DGV && !DGV->isDeclarationForLinker()) |
897 | return false; |
898 | |
899 | if (SGV.isDeclaration() || DoneLinkingBodies) |
900 | return false; |
901 | |
902 | // Callback to the client to give a chance to lazily add the Global to the |
903 | // list of value to link. |
904 | bool LazilyAdded = false; |
905 | if (AddLazyFor) |
906 | AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) { |
907 | maybeAdd(GV: &GV); |
908 | LazilyAdded = true; |
909 | }); |
910 | return LazilyAdded; |
911 | } |
912 | |
913 | Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV, |
914 | bool ForIndirectSymbol) { |
915 | GlobalValue *DGV = getLinkedToGlobal(SrcGV: SGV); |
916 | |
917 | bool ShouldLink = shouldLink(DGV, SGV&: *SGV); |
918 | |
919 | // just missing from map |
920 | if (ShouldLink) { |
921 | auto I = ValueMap.find(Val: SGV); |
922 | if (I != ValueMap.end()) |
923 | return cast<Constant>(Val&: I->second); |
924 | |
925 | I = IndirectSymbolValueMap.find(Val: SGV); |
926 | if (I != IndirectSymbolValueMap.end()) |
927 | return cast<Constant>(Val&: I->second); |
928 | } |
929 | |
930 | if (!ShouldLink && ForIndirectSymbol) |
931 | DGV = nullptr; |
932 | |
933 | // Handle the ultra special appending linkage case first. |
934 | if (SGV->hasAppendingLinkage() || (DGV && DGV->hasAppendingLinkage())) |
935 | return linkAppendingVarProto(DstGV: cast_or_null<GlobalVariable>(Val: DGV), |
936 | SrcGV: cast<GlobalVariable>(Val: SGV)); |
937 | |
938 | bool NeedsRenaming = false; |
939 | GlobalValue *NewGV; |
940 | if (DGV && !ShouldLink) { |
941 | NewGV = DGV; |
942 | } else { |
943 | // If we are done linking global value bodies (i.e. we are performing |
944 | // metadata linking), don't link in the global value due to this |
945 | // reference, simply map it to null. |
946 | if (DoneLinkingBodies) |
947 | return nullptr; |
948 | |
949 | NewGV = copyGlobalValueProto(SGV, ForDefinition: ShouldLink || ForIndirectSymbol); |
950 | if (ShouldLink || !ForIndirectSymbol) |
951 | NeedsRenaming = true; |
952 | } |
953 | |
954 | // Overloaded intrinsics have overloaded types names as part of their |
955 | // names. If we renamed overloaded types we should rename the intrinsic |
956 | // as well. |
957 | if (Function *F = dyn_cast<Function>(Val: NewGV)) |
958 | if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) { |
959 | // Note: remangleIntrinsicFunction does not copy metadata and as such |
960 | // F should not occur in the set of objects with unmapped metadata. |
961 | // If this assertion fails then remangleIntrinsicFunction needs updating. |
962 | assert(!UnmappedMetadata.count(F) && "intrinsic has unmapped metadata" ); |
963 | NewGV->eraseFromParent(); |
964 | NewGV = *Remangled; |
965 | NeedsRenaming = false; |
966 | } |
967 | |
968 | if (NeedsRenaming) |
969 | forceRenaming(GV: NewGV, Name: SGV->getName()); |
970 | |
971 | if (ShouldLink || ForIndirectSymbol) { |
972 | if (const Comdat *SC = SGV->getComdat()) { |
973 | if (auto *GO = dyn_cast<GlobalObject>(Val: NewGV)) { |
974 | Comdat *DC = DstM.getOrInsertComdat(Name: SC->getName()); |
975 | DC->setSelectionKind(SC->getSelectionKind()); |
976 | GO->setComdat(DC); |
977 | } |
978 | } |
979 | } |
980 | |
981 | if (!ShouldLink && ForIndirectSymbol) |
982 | NewGV->setLinkage(GlobalValue::InternalLinkage); |
983 | |
984 | Constant *C = NewGV; |
985 | // Only create a bitcast if necessary. In particular, with |
986 | // DebugTypeODRUniquing we may reach metadata in the destination module |
987 | // containing a GV from the source module, in which case SGV will be |
988 | // the same as DGV and NewGV, and TypeMap.get() will assert since it |
989 | // assumes it is being invoked on a type in the source module. |
990 | if (DGV && NewGV != SGV) { |
991 | C = ConstantExpr::getPointerBitCastOrAddrSpaceCast( |
992 | C: NewGV, Ty: TypeMap.get(Ty: SGV->getType())); |
993 | } |
994 | |
995 | if (DGV && NewGV != DGV) { |
996 | // Schedule "replace all uses with" to happen after materializing is |
997 | // done. It is not safe to do it now, since ValueMapper may be holding |
998 | // pointers to constants that will get deleted if RAUW runs. |
999 | RAUWWorklist.push_back(x: std::make_pair( |
1000 | x&: DGV, |
1001 | y: ConstantExpr::getPointerBitCastOrAddrSpaceCast(C: NewGV, Ty: DGV->getType()))); |
1002 | } |
1003 | |
1004 | return C; |
1005 | } |
1006 | |
1007 | /// Update the initializers in the Dest module now that all globals that may be |
1008 | /// referenced are in Dest. |
1009 | void IRLinker::linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src) { |
1010 | // Figure out what the initializer looks like in the dest module. |
1011 | Mapper.scheduleMapGlobalInitializer(GV&: Dst, Init&: *Src.getInitializer()); |
1012 | } |
1013 | |
1014 | /// Copy the source function over into the dest function and fix up references |
1015 | /// to values. At this point we know that Dest is an external function, and |
1016 | /// that Src is not. |
1017 | Error IRLinker::linkFunctionBody(Function &Dst, Function &Src) { |
1018 | assert(Dst.isDeclaration() && !Src.isDeclaration()); |
1019 | |
1020 | // Materialize if needed. |
1021 | if (Error Err = Src.materialize()) |
1022 | return Err; |
1023 | |
1024 | // Link in the operands without remapping. |
1025 | if (Src.hasPrefixData()) |
1026 | Dst.setPrefixData(Src.getPrefixData()); |
1027 | if (Src.hasPrologueData()) |
1028 | Dst.setPrologueData(Src.getPrologueData()); |
1029 | if (Src.hasPersonalityFn()) |
1030 | Dst.setPersonalityFn(Src.getPersonalityFn()); |
1031 | |
1032 | // Copy over the metadata attachments without remapping. |
1033 | Dst.copyMetadata(Src: &Src, Offset: 0); |
1034 | |
1035 | // Steal arguments and splice the body of Src into Dst. |
1036 | Dst.stealArgumentListFrom(Src); |
1037 | Dst.splice(ToIt: Dst.end(), FromF: &Src); |
1038 | |
1039 | // Everything has been moved over. Remap it. |
1040 | Mapper.scheduleRemapFunction(F&: Dst); |
1041 | return Error::success(); |
1042 | } |
1043 | |
1044 | void IRLinker::linkAliasAliasee(GlobalAlias &Dst, GlobalAlias &Src) { |
1045 | Mapper.scheduleMapGlobalAlias(GA&: Dst, Aliasee&: *Src.getAliasee(), MappingContextID: IndirectSymbolMCID); |
1046 | } |
1047 | |
1048 | void IRLinker::linkIFuncResolver(GlobalIFunc &Dst, GlobalIFunc &Src) { |
1049 | Mapper.scheduleMapGlobalIFunc(GI&: Dst, Resolver&: *Src.getResolver(), MappingContextID: IndirectSymbolMCID); |
1050 | } |
1051 | |
1052 | Error IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) { |
1053 | if (auto *F = dyn_cast<Function>(Val: &Src)) |
1054 | return linkFunctionBody(Dst&: cast<Function>(Val&: Dst), Src&: *F); |
1055 | if (auto *GVar = dyn_cast<GlobalVariable>(Val: &Src)) { |
1056 | linkGlobalVariable(Dst&: cast<GlobalVariable>(Val&: Dst), Src&: *GVar); |
1057 | return Error::success(); |
1058 | } |
1059 | if (auto *GA = dyn_cast<GlobalAlias>(Val: &Src)) { |
1060 | linkAliasAliasee(Dst&: cast<GlobalAlias>(Val&: Dst), Src&: *GA); |
1061 | return Error::success(); |
1062 | } |
1063 | linkIFuncResolver(Dst&: cast<GlobalIFunc>(Val&: Dst), Src&: cast<GlobalIFunc>(Val&: Src)); |
1064 | return Error::success(); |
1065 | } |
1066 | |
1067 | void IRLinker::flushRAUWWorklist() { |
1068 | for (const auto &Elem : RAUWWorklist) { |
1069 | GlobalValue *Old; |
1070 | Value *New; |
1071 | std::tie(args&: Old, args&: New) = Elem; |
1072 | |
1073 | Old->replaceAllUsesWith(V: New); |
1074 | Old->eraseFromParent(); |
1075 | } |
1076 | RAUWWorklist.clear(); |
1077 | } |
1078 | |
1079 | void IRLinker::prepareCompileUnitsForImport() { |
1080 | NamedMDNode *SrcCompileUnits = SrcM->getNamedMetadata(Name: "llvm.dbg.cu" ); |
1081 | if (!SrcCompileUnits) |
1082 | return; |
1083 | // When importing for ThinLTO, prevent importing of types listed on |
1084 | // the DICompileUnit that we don't need a copy of in the importing |
1085 | // module. They will be emitted by the originating module. |
1086 | for (MDNode *N : SrcCompileUnits->operands()) { |
1087 | auto *CU = cast<DICompileUnit>(Val: N); |
1088 | assert(CU && "Expected valid compile unit" ); |
1089 | // Enums, macros, and retained types don't need to be listed on the |
1090 | // imported DICompileUnit. This means they will only be imported |
1091 | // if reached from the mapped IR. |
1092 | CU->replaceEnumTypes(N: nullptr); |
1093 | CU->replaceMacros(N: nullptr); |
1094 | CU->replaceRetainedTypes(N: nullptr); |
1095 | |
1096 | // The original definition (or at least its debug info - if the variable is |
1097 | // internalized and optimized away) will remain in the source module, so |
1098 | // there's no need to import them. |
1099 | // If LLVM ever does more advanced optimizations on global variables |
1100 | // (removing/localizing write operations, for instance) that can track |
1101 | // through debug info, this decision may need to be revisited - but do so |
1102 | // with care when it comes to debug info size. Emitting small CUs containing |
1103 | // only a few imported entities into every destination module may be very |
1104 | // size inefficient. |
1105 | CU->replaceGlobalVariables(N: nullptr); |
1106 | |
1107 | CU->replaceImportedEntities(N: nullptr); |
1108 | } |
1109 | } |
1110 | |
1111 | /// Insert all of the named MDNodes in Src into the Dest module. |
1112 | void IRLinker::linkNamedMDNodes() { |
1113 | const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata(); |
1114 | for (const NamedMDNode &NMD : SrcM->named_metadata()) { |
1115 | // Don't link module flags here. Do them separately. |
1116 | if (&NMD == SrcModFlags) |
1117 | continue; |
1118 | // Don't import pseudo probe descriptors here for thinLTO. They will be |
1119 | // emitted by the originating module. |
1120 | if (IsPerformingImport && NMD.getName() == PseudoProbeDescMetadataName) { |
1121 | if (!DstM.getNamedMetadata(Name: NMD.getName())) |
1122 | emitWarning(Message: "Pseudo-probe ignored: source module '" + |
1123 | SrcM->getModuleIdentifier() + |
1124 | "' is compiled with -fpseudo-probe-for-profiling while " |
1125 | "destination module '" + |
1126 | DstM.getModuleIdentifier() + "' is not\n" ); |
1127 | continue; |
1128 | } |
1129 | // The stats are computed per module and will all be merged in the binary. |
1130 | // Importing the metadata will cause duplication of the stats. |
1131 | if (IsPerformingImport && NMD.getName() == "llvm.stats" ) |
1132 | continue; |
1133 | |
1134 | NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(Name: NMD.getName()); |
1135 | // Add Src elements into Dest node. |
1136 | for (const MDNode *Op : NMD.operands()) |
1137 | DestNMD->addOperand(M: Mapper.mapMDNode(N: *Op)); |
1138 | } |
1139 | } |
1140 | |
1141 | /// Merge the linker flags in Src into the Dest module. |
1142 | Error IRLinker::linkModuleFlagsMetadata() { |
1143 | // If the source module has no module flags, we are done. |
1144 | const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata(); |
1145 | if (!SrcModFlags) |
1146 | return Error::success(); |
1147 | |
1148 | // Check for module flag for updates before do anything. |
1149 | UpgradeModuleFlags(M&: *SrcM); |
1150 | UpgradeNVVMAnnotations(M&: *SrcM); |
1151 | |
1152 | // If the destination module doesn't have module flags yet, then just copy |
1153 | // over the source module's flags. |
1154 | NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata(); |
1155 | if (DstModFlags->getNumOperands() == 0) { |
1156 | for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) |
1157 | DstModFlags->addOperand(M: SrcModFlags->getOperand(i: I)); |
1158 | |
1159 | return Error::success(); |
1160 | } |
1161 | |
1162 | // First build a map of the existing module flags and requirements. |
1163 | DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags; |
1164 | SmallSetVector<MDNode *, 16> Requirements; |
1165 | SmallVector<unsigned, 0> Mins; |
1166 | DenseSet<MDString *> SeenMin; |
1167 | for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) { |
1168 | MDNode *Op = DstModFlags->getOperand(i: I); |
1169 | uint64_t Behavior = |
1170 | mdconst::extract<ConstantInt>(MD: Op->getOperand(I: 0))->getZExtValue(); |
1171 | MDString *ID = cast<MDString>(Val: Op->getOperand(I: 1)); |
1172 | |
1173 | if (Behavior == Module::Require) { |
1174 | Requirements.insert(X: cast<MDNode>(Val: Op->getOperand(I: 2))); |
1175 | } else { |
1176 | if (Behavior == Module::Min) |
1177 | Mins.push_back(Elt: I); |
1178 | Flags[ID] = std::make_pair(x&: Op, y&: I); |
1179 | } |
1180 | } |
1181 | |
1182 | // Merge in the flags from the source module, and also collect its set of |
1183 | // requirements. |
1184 | for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) { |
1185 | MDNode *SrcOp = SrcModFlags->getOperand(i: I); |
1186 | ConstantInt *SrcBehavior = |
1187 | mdconst::extract<ConstantInt>(MD: SrcOp->getOperand(I: 0)); |
1188 | MDString *ID = cast<MDString>(Val: SrcOp->getOperand(I: 1)); |
1189 | MDNode *DstOp; |
1190 | unsigned DstIndex; |
1191 | std::tie(args&: DstOp, args&: DstIndex) = Flags.lookup(Val: ID); |
1192 | unsigned SrcBehaviorValue = SrcBehavior->getZExtValue(); |
1193 | SeenMin.insert(V: ID); |
1194 | |
1195 | // If this is a requirement, add it and continue. |
1196 | if (SrcBehaviorValue == Module::Require) { |
1197 | // If the destination module does not already have this requirement, add |
1198 | // it. |
1199 | if (Requirements.insert(X: cast<MDNode>(Val: SrcOp->getOperand(I: 2)))) { |
1200 | DstModFlags->addOperand(M: SrcOp); |
1201 | } |
1202 | continue; |
1203 | } |
1204 | |
1205 | // If there is no existing flag with this ID, just add it. |
1206 | if (!DstOp) { |
1207 | if (SrcBehaviorValue == Module::Min) { |
1208 | Mins.push_back(Elt: DstModFlags->getNumOperands()); |
1209 | SeenMin.erase(V: ID); |
1210 | } |
1211 | Flags[ID] = std::make_pair(x&: SrcOp, y: DstModFlags->getNumOperands()); |
1212 | DstModFlags->addOperand(M: SrcOp); |
1213 | continue; |
1214 | } |
1215 | |
1216 | // Otherwise, perform a merge. |
1217 | ConstantInt *DstBehavior = |
1218 | mdconst::extract<ConstantInt>(MD: DstOp->getOperand(I: 0)); |
1219 | unsigned DstBehaviorValue = DstBehavior->getZExtValue(); |
1220 | |
1221 | auto overrideDstValue = [&]() { |
1222 | DstModFlags->setOperand(I: DstIndex, New: SrcOp); |
1223 | Flags[ID].first = SrcOp; |
1224 | }; |
1225 | |
1226 | // If either flag has override behavior, handle it first. |
1227 | if (DstBehaviorValue == Module::Override) { |
1228 | // Diagnose inconsistent flags which both have override behavior. |
1229 | if (SrcBehaviorValue == Module::Override && |
1230 | SrcOp->getOperand(I: 2) != DstOp->getOperand(I: 2)) |
1231 | return stringErr(T: "linking module flags '" + ID->getString() + |
1232 | "': IDs have conflicting override values in '" + |
1233 | SrcM->getModuleIdentifier() + "' and '" + |
1234 | DstM.getModuleIdentifier() + "'" ); |
1235 | continue; |
1236 | } else if (SrcBehaviorValue == Module::Override) { |
1237 | // Update the destination flag to that of the source. |
1238 | overrideDstValue(); |
1239 | continue; |
1240 | } |
1241 | |
1242 | // Diagnose inconsistent merge behavior types. |
1243 | if (SrcBehaviorValue != DstBehaviorValue) { |
1244 | bool MinAndWarn = (SrcBehaviorValue == Module::Min && |
1245 | DstBehaviorValue == Module::Warning) || |
1246 | (DstBehaviorValue == Module::Min && |
1247 | SrcBehaviorValue == Module::Warning); |
1248 | bool MaxAndWarn = (SrcBehaviorValue == Module::Max && |
1249 | DstBehaviorValue == Module::Warning) || |
1250 | (DstBehaviorValue == Module::Max && |
1251 | SrcBehaviorValue == Module::Warning); |
1252 | if (!(MaxAndWarn || MinAndWarn)) |
1253 | return stringErr(T: "linking module flags '" + ID->getString() + |
1254 | "': IDs have conflicting behaviors in '" + |
1255 | SrcM->getModuleIdentifier() + "' and '" + |
1256 | DstM.getModuleIdentifier() + "'" ); |
1257 | } |
1258 | |
1259 | auto ensureDistinctOp = [&](MDNode *DstValue) { |
1260 | assert(isa<MDTuple>(DstValue) && |
1261 | "Expected MDTuple when appending module flags" ); |
1262 | if (DstValue->isDistinct()) |
1263 | return dyn_cast<MDTuple>(Val: DstValue); |
1264 | ArrayRef<MDOperand> DstOperands = DstValue->operands(); |
1265 | MDTuple *New = MDTuple::getDistinct( |
1266 | Context&: DstM.getContext(), MDs: SmallVector<Metadata *, 4>(DstOperands)); |
1267 | Metadata *FlagOps[] = {DstOp->getOperand(I: 0), ID, New}; |
1268 | MDNode *Flag = MDTuple::getDistinct(Context&: DstM.getContext(), MDs: FlagOps); |
1269 | DstModFlags->setOperand(I: DstIndex, New: Flag); |
1270 | Flags[ID].first = Flag; |
1271 | return New; |
1272 | }; |
1273 | |
1274 | // Emit a warning if the values differ and either source or destination |
1275 | // request Warning behavior. |
1276 | if ((DstBehaviorValue == Module::Warning || |
1277 | SrcBehaviorValue == Module::Warning) && |
1278 | SrcOp->getOperand(I: 2) != DstOp->getOperand(I: 2)) { |
1279 | std::string Str; |
1280 | raw_string_ostream(Str) |
1281 | << "linking module flags '" << ID->getString() |
1282 | << "': IDs have conflicting values ('" << *SrcOp->getOperand(I: 2) |
1283 | << "' from " << SrcM->getModuleIdentifier() << " with '" |
1284 | << *DstOp->getOperand(I: 2) << "' from " << DstM.getModuleIdentifier() |
1285 | << ')'; |
1286 | emitWarning(Message: Str); |
1287 | } |
1288 | |
1289 | // Choose the minimum if either source or destination request Min behavior. |
1290 | if (DstBehaviorValue == Module::Min || SrcBehaviorValue == Module::Min) { |
1291 | ConstantInt *DstValue = |
1292 | mdconst::extract<ConstantInt>(MD: DstOp->getOperand(I: 2)); |
1293 | ConstantInt *SrcValue = |
1294 | mdconst::extract<ConstantInt>(MD: SrcOp->getOperand(I: 2)); |
1295 | |
1296 | // The resulting flag should have a Min behavior, and contain the minimum |
1297 | // value from between the source and destination values. |
1298 | Metadata *FlagOps[] = { |
1299 | (DstBehaviorValue != Module::Min ? SrcOp : DstOp)->getOperand(I: 0), ID, |
1300 | (SrcValue->getZExtValue() < DstValue->getZExtValue() ? SrcOp : DstOp) |
1301 | ->getOperand(I: 2)}; |
1302 | MDNode *Flag = MDNode::get(Context&: DstM.getContext(), MDs: FlagOps); |
1303 | DstModFlags->setOperand(I: DstIndex, New: Flag); |
1304 | Flags[ID].first = Flag; |
1305 | continue; |
1306 | } |
1307 | |
1308 | // Choose the maximum if either source or destination request Max behavior. |
1309 | if (DstBehaviorValue == Module::Max || SrcBehaviorValue == Module::Max) { |
1310 | ConstantInt *DstValue = |
1311 | mdconst::extract<ConstantInt>(MD: DstOp->getOperand(I: 2)); |
1312 | ConstantInt *SrcValue = |
1313 | mdconst::extract<ConstantInt>(MD: SrcOp->getOperand(I: 2)); |
1314 | |
1315 | // The resulting flag should have a Max behavior, and contain the maximum |
1316 | // value from between the source and destination values. |
1317 | Metadata *FlagOps[] = { |
1318 | (DstBehaviorValue != Module::Max ? SrcOp : DstOp)->getOperand(I: 0), ID, |
1319 | (SrcValue->getZExtValue() > DstValue->getZExtValue() ? SrcOp : DstOp) |
1320 | ->getOperand(I: 2)}; |
1321 | MDNode *Flag = MDNode::get(Context&: DstM.getContext(), MDs: FlagOps); |
1322 | DstModFlags->setOperand(I: DstIndex, New: Flag); |
1323 | Flags[ID].first = Flag; |
1324 | continue; |
1325 | } |
1326 | |
1327 | // Perform the merge for standard behavior types. |
1328 | switch (SrcBehaviorValue) { |
1329 | case Module::Require: |
1330 | case Module::Override: |
1331 | llvm_unreachable("not possible" ); |
1332 | case Module::Error: { |
1333 | // Emit an error if the values differ. |
1334 | if (SrcOp->getOperand(I: 2) != DstOp->getOperand(I: 2)) { |
1335 | std::string Str; |
1336 | raw_string_ostream(Str) |
1337 | << "linking module flags '" << ID->getString() |
1338 | << "': IDs have conflicting values: '" << *SrcOp->getOperand(I: 2) |
1339 | << "' from " << SrcM->getModuleIdentifier() << ", and '" |
1340 | << *DstOp->getOperand(I: 2) << "' from " + DstM.getModuleIdentifier(); |
1341 | return stringErr(T: Str); |
1342 | } |
1343 | continue; |
1344 | } |
1345 | case Module::Warning: { |
1346 | break; |
1347 | } |
1348 | case Module::Max: { |
1349 | break; |
1350 | } |
1351 | case Module::Append: { |
1352 | MDTuple *DstValue = ensureDistinctOp(cast<MDNode>(Val: DstOp->getOperand(I: 2))); |
1353 | MDNode *SrcValue = cast<MDNode>(Val: SrcOp->getOperand(I: 2)); |
1354 | for (const auto &O : SrcValue->operands()) |
1355 | DstValue->push_back(MD: O); |
1356 | break; |
1357 | } |
1358 | case Module::AppendUnique: { |
1359 | SmallSetVector<Metadata *, 16> Elts; |
1360 | MDTuple *DstValue = ensureDistinctOp(cast<MDNode>(Val: DstOp->getOperand(I: 2))); |
1361 | MDNode *SrcValue = cast<MDNode>(Val: SrcOp->getOperand(I: 2)); |
1362 | Elts.insert(Start: DstValue->op_begin(), End: DstValue->op_end()); |
1363 | Elts.insert(Start: SrcValue->op_begin(), End: SrcValue->op_end()); |
1364 | for (auto I = DstValue->getNumOperands(); I < Elts.size(); I++) |
1365 | DstValue->push_back(MD: Elts[I]); |
1366 | break; |
1367 | } |
1368 | } |
1369 | |
1370 | } |
1371 | |
1372 | // For the Min behavior, set the value to 0 if either module does not have the |
1373 | // flag. |
1374 | for (auto Idx : Mins) { |
1375 | MDNode *Op = DstModFlags->getOperand(i: Idx); |
1376 | MDString *ID = cast<MDString>(Val: Op->getOperand(I: 1)); |
1377 | if (!SeenMin.count(V: ID)) { |
1378 | ConstantInt *V = mdconst::extract<ConstantInt>(MD: Op->getOperand(I: 2)); |
1379 | Metadata *FlagOps[] = { |
1380 | Op->getOperand(I: 0), ID, |
1381 | ConstantAsMetadata::get(C: ConstantInt::get(Ty: V->getType(), V: 0))}; |
1382 | DstModFlags->setOperand(I: Idx, New: MDNode::get(Context&: DstM.getContext(), MDs: FlagOps)); |
1383 | } |
1384 | } |
1385 | |
1386 | // Check all of the requirements. |
1387 | for (MDNode *Requirement : Requirements) { |
1388 | MDString *Flag = cast<MDString>(Val: Requirement->getOperand(I: 0)); |
1389 | Metadata *ReqValue = Requirement->getOperand(I: 1); |
1390 | |
1391 | MDNode *Op = Flags[Flag].first; |
1392 | if (!Op || Op->getOperand(I: 2) != ReqValue) |
1393 | return stringErr(T: "linking module flags '" + Flag->getString() + |
1394 | "': does not have the required value" ); |
1395 | } |
1396 | return Error::success(); |
1397 | } |
1398 | |
1399 | /// Return InlineAsm adjusted with target-specific directives if required. |
1400 | /// For ARM and Thumb, we have to add directives to select the appropriate ISA |
1401 | /// to support mixing module-level inline assembly from ARM and Thumb modules. |
1402 | static std::string adjustInlineAsm(const std::string &InlineAsm, |
1403 | const Triple &Triple) { |
1404 | if (Triple.getArch() == Triple::thumb || Triple.getArch() == Triple::thumbeb) |
1405 | return ".text\n.balign 2\n.thumb\n" + InlineAsm; |
1406 | if (Triple.getArch() == Triple::arm || Triple.getArch() == Triple::armeb) |
1407 | return ".text\n.balign 4\n.arm\n" + InlineAsm; |
1408 | return InlineAsm; |
1409 | } |
1410 | |
1411 | void IRLinker::updateAttributes(GlobalValue &GV) { |
1412 | /// Remove nocallback attribute while linking, because nocallback attribute |
1413 | /// indicates that the function is only allowed to jump back into caller's |
1414 | /// module only by a return or an exception. When modules are linked, this |
1415 | /// property cannot be guaranteed anymore. For example, the nocallback |
1416 | /// function may contain a call to another module. But if we merge its caller |
1417 | /// and callee module here, and not the module containing the nocallback |
1418 | /// function definition itself, the nocallback property will be violated |
1419 | /// (since the nocallback function will call back into the newly merged module |
1420 | /// containing both its caller and callee). This could happen if the module |
1421 | /// containing the nocallback function definition is native code, so it does |
1422 | /// not participate in the LTO link. Note if the nocallback function does |
1423 | /// participate in the LTO link, and thus ends up in the merged module |
1424 | /// containing its caller and callee, removing the attribute doesn't hurt as |
1425 | /// it has no effect on definitions in the same module. |
1426 | if (auto *F = dyn_cast<Function>(Val: &GV)) { |
1427 | if (!F->isIntrinsic()) |
1428 | F->removeFnAttr(Kind: llvm::Attribute::NoCallback); |
1429 | |
1430 | // Remove nocallback attribute when it is on a call-site. |
1431 | for (BasicBlock &BB : *F) |
1432 | for (Instruction &I : BB) |
1433 | if (CallBase *CI = dyn_cast<CallBase>(Val: &I)) |
1434 | CI->removeFnAttr(Kind: Attribute::NoCallback); |
1435 | } |
1436 | } |
1437 | |
1438 | Error IRLinker::run() { |
1439 | // Ensure metadata materialized before value mapping. |
1440 | if (SrcM->getMaterializer()) |
1441 | if (Error Err = SrcM->getMaterializer()->materializeMetadata()) |
1442 | return Err; |
1443 | |
1444 | // Inherit the target data from the source module if the destination |
1445 | // module doesn't have one already. |
1446 | if (DstM.getDataLayout().isDefault()) |
1447 | DstM.setDataLayout(SrcM->getDataLayout()); |
1448 | |
1449 | // Copy the target triple from the source to dest if the dest's is empty. |
1450 | if (DstM.getTargetTriple().empty() && !SrcM->getTargetTriple().empty()) |
1451 | DstM.setTargetTriple(SrcM->getTargetTriple()); |
1452 | |
1453 | Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM.getTargetTriple()); |
1454 | |
1455 | // During CUDA compilation we have to link with the bitcode supplied with |
1456 | // CUDA. libdevice bitcode either has no data layout set (pre-CUDA-11), or has |
1457 | // the layout that is different from the one used by LLVM/clang (it does not |
1458 | // include i128). Issuing a warning is not very helpful as there's not much |
1459 | // the user can do about it. |
1460 | bool EnableDLWarning = true; |
1461 | bool EnableTripleWarning = true; |
1462 | if (SrcTriple.isNVPTX() && DstTriple.isNVPTX()) { |
1463 | bool SrcHasLibDeviceDL = |
1464 | (SrcM->getDataLayoutStr().empty() || |
1465 | SrcM->getDataLayoutStr() == "e-i64:64-v16:16-v32:32-n16:32:64" ); |
1466 | // libdevice bitcode uses nvptx64-nvidia-gpulibs or just |
1467 | // 'nvptx-unknown-unknown' triple (before CUDA-10.x) and is compatible with |
1468 | // all NVPTX variants. |
1469 | bool SrcHasLibDeviceTriple = (SrcTriple.getVendor() == Triple::NVIDIA && |
1470 | SrcTriple.getOSName() == "gpulibs" ) || |
1471 | (SrcTriple.getVendorName() == "unknown" && |
1472 | SrcTriple.getOSName() == "unknown" ); |
1473 | EnableTripleWarning = !SrcHasLibDeviceTriple; |
1474 | EnableDLWarning = !(SrcHasLibDeviceTriple && SrcHasLibDeviceDL); |
1475 | } |
1476 | |
1477 | if (EnableDLWarning && (SrcM->getDataLayout() != DstM.getDataLayout())) { |
1478 | emitWarning(Message: "Linking two modules of different data layouts: '" + |
1479 | SrcM->getModuleIdentifier() + "' is '" + |
1480 | SrcM->getDataLayoutStr() + "' whereas '" + |
1481 | DstM.getModuleIdentifier() + "' is '" + |
1482 | DstM.getDataLayoutStr() + "'\n" ); |
1483 | } |
1484 | |
1485 | if (EnableTripleWarning && !SrcM->getTargetTriple().empty() && |
1486 | !SrcTriple.isCompatibleWith(Other: DstTriple)) |
1487 | emitWarning(Message: "Linking two modules of different target triples: '" + |
1488 | SrcM->getModuleIdentifier() + "' is '" + |
1489 | SrcM->getTargetTriple().str() + "' whereas '" + |
1490 | DstM.getModuleIdentifier() + "' is '" + |
1491 | DstM.getTargetTriple().str() + "'\n" ); |
1492 | |
1493 | DstM.setTargetTriple(Triple(SrcTriple.merge(Other: DstTriple))); |
1494 | |
1495 | // Loop over all of the linked values to compute type mappings. |
1496 | computeTypeMapping(); |
1497 | |
1498 | std::reverse(first: Worklist.begin(), last: Worklist.end()); |
1499 | while (!Worklist.empty()) { |
1500 | GlobalValue *GV = Worklist.back(); |
1501 | Worklist.pop_back(); |
1502 | |
1503 | // Already mapped. |
1504 | if (ValueMap.find(Val: GV) != ValueMap.end() || |
1505 | IndirectSymbolValueMap.find(Val: GV) != IndirectSymbolValueMap.end()) |
1506 | continue; |
1507 | |
1508 | assert(!GV->isDeclaration()); |
1509 | Mapper.mapValue(V: *GV); |
1510 | if (FoundError) |
1511 | return std::move(*FoundError); |
1512 | flushRAUWWorklist(); |
1513 | } |
1514 | |
1515 | // Note that we are done linking global value bodies. This prevents |
1516 | // metadata linking from creating new references. |
1517 | DoneLinkingBodies = true; |
1518 | Mapper.addFlags(Flags: RF_NullMapMissingGlobalValues); |
1519 | |
1520 | // Remap all of the named MDNodes in Src into the DstM module. We do this |
1521 | // after linking GlobalValues so that MDNodes that reference GlobalValues |
1522 | // are properly remapped. |
1523 | linkNamedMDNodes(); |
1524 | |
1525 | // Clean up any global objects with potentially unmapped metadata. |
1526 | // Specifically declarations which did not become definitions. |
1527 | for (GlobalObject *NGO : UnmappedMetadata) { |
1528 | if (NGO->isDeclaration()) |
1529 | Mapper.remapGlobalObjectMetadata(GO&: *NGO); |
1530 | } |
1531 | |
1532 | if (!IsPerformingImport && !SrcM->getModuleInlineAsm().empty()) { |
1533 | // Append the module inline asm string. |
1534 | DstM.appendModuleInlineAsm(Asm: adjustInlineAsm(InlineAsm: SrcM->getModuleInlineAsm(), |
1535 | Triple: SrcTriple)); |
1536 | } else if (IsPerformingImport) { |
1537 | // Import any symver directives for symbols in DstM. |
1538 | ModuleSymbolTable::CollectAsmSymvers(M: *SrcM, |
1539 | AsmSymver: [&](StringRef Name, StringRef Alias) { |
1540 | if (DstM.getNamedValue(Name)) { |
1541 | SmallString<256> S(".symver " ); |
1542 | S += Name; |
1543 | S += ", " ; |
1544 | S += Alias; |
1545 | DstM.appendModuleInlineAsm(Asm: S); |
1546 | } |
1547 | }); |
1548 | } |
1549 | |
1550 | // Reorder the globals just added to the destination module to match their |
1551 | // original order in the source module. |
1552 | for (GlobalVariable &GV : SrcM->globals()) { |
1553 | if (GV.hasAppendingLinkage()) |
1554 | continue; |
1555 | Value *NewValue = Mapper.mapValue(V: GV); |
1556 | if (FoundError) |
1557 | return std::move(*FoundError); |
1558 | if (NewValue) { |
1559 | auto *NewGV = dyn_cast<GlobalVariable>(Val: NewValue->stripPointerCasts()); |
1560 | if (NewGV) { |
1561 | NewGV->removeFromParent(); |
1562 | DstM.insertGlobalVariable(GV: NewGV); |
1563 | } |
1564 | } |
1565 | } |
1566 | |
1567 | // Merge the module flags into the DstM module. |
1568 | return linkModuleFlagsMetadata(); |
1569 | } |
1570 | |
1571 | IRMover::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P) |
1572 | : ETypes(E), IsPacked(P) {} |
1573 | |
1574 | IRMover::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST) |
1575 | : ETypes(ST->elements()), IsPacked(ST->isPacked()) {} |
1576 | |
1577 | bool IRMover::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const { |
1578 | return IsPacked == That.IsPacked && ETypes == That.ETypes; |
1579 | } |
1580 | |
1581 | bool IRMover::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const { |
1582 | return !this->operator==(That); |
1583 | } |
1584 | |
1585 | StructType *IRMover::StructTypeKeyInfo::getEmptyKey() { |
1586 | return DenseMapInfo<StructType *>::getEmptyKey(); |
1587 | } |
1588 | |
1589 | StructType *IRMover::StructTypeKeyInfo::getTombstoneKey() { |
1590 | return DenseMapInfo<StructType *>::getTombstoneKey(); |
1591 | } |
1592 | |
1593 | unsigned IRMover::StructTypeKeyInfo::getHashValue(const KeyTy &Key) { |
1594 | return hash_combine(args: hash_combine_range(R: Key.ETypes), args: Key.IsPacked); |
1595 | } |
1596 | |
1597 | unsigned IRMover::StructTypeKeyInfo::getHashValue(const StructType *ST) { |
1598 | return getHashValue(Key: KeyTy(ST)); |
1599 | } |
1600 | |
1601 | bool IRMover::StructTypeKeyInfo::isEqual(const KeyTy &LHS, |
1602 | const StructType *RHS) { |
1603 | if (RHS == getEmptyKey() || RHS == getTombstoneKey()) |
1604 | return false; |
1605 | return LHS == KeyTy(RHS); |
1606 | } |
1607 | |
1608 | bool IRMover::StructTypeKeyInfo::isEqual(const StructType *LHS, |
1609 | const StructType *RHS) { |
1610 | if (RHS == getEmptyKey() || RHS == getTombstoneKey()) |
1611 | return LHS == RHS; |
1612 | return KeyTy(LHS) == KeyTy(RHS); |
1613 | } |
1614 | |
1615 | void IRMover::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) { |
1616 | assert(!Ty->isOpaque()); |
1617 | NonOpaqueStructTypes.insert(V: Ty); |
1618 | } |
1619 | |
1620 | void IRMover::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) { |
1621 | assert(!Ty->isOpaque()); |
1622 | NonOpaqueStructTypes.insert(V: Ty); |
1623 | bool Removed = OpaqueStructTypes.erase(V: Ty); |
1624 | (void)Removed; |
1625 | assert(Removed); |
1626 | } |
1627 | |
1628 | void IRMover::IdentifiedStructTypeSet::addOpaque(StructType *Ty) { |
1629 | assert(Ty->isOpaque()); |
1630 | OpaqueStructTypes.insert(V: Ty); |
1631 | } |
1632 | |
1633 | StructType * |
1634 | IRMover::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes, |
1635 | bool IsPacked) { |
1636 | IRMover::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked); |
1637 | auto I = NonOpaqueStructTypes.find_as(Val: Key); |
1638 | return I == NonOpaqueStructTypes.end() ? nullptr : *I; |
1639 | } |
1640 | |
1641 | bool IRMover::IdentifiedStructTypeSet::hasType(StructType *Ty) { |
1642 | if (Ty->isOpaque()) |
1643 | return OpaqueStructTypes.count(V: Ty); |
1644 | auto I = NonOpaqueStructTypes.find(V: Ty); |
1645 | return I == NonOpaqueStructTypes.end() ? false : *I == Ty; |
1646 | } |
1647 | |
1648 | IRMover::IRMover(Module &M) : Composite(M) { |
1649 | TypeFinder StructTypes; |
1650 | StructTypes.run(M, /* OnlyNamed */ onlyNamed: false); |
1651 | for (StructType *Ty : StructTypes) { |
1652 | if (Ty->isOpaque()) |
1653 | IdentifiedStructTypes.addOpaque(Ty); |
1654 | else |
1655 | IdentifiedStructTypes.addNonOpaque(Ty); |
1656 | } |
1657 | // Self-map metadatas in the destination module. This is needed when |
1658 | // DebugTypeODRUniquing is enabled on the LLVMContext, since metadata in the |
1659 | // destination module may be reached from the source module. |
1660 | for (const auto *MD : StructTypes.getVisitedMetadata()) { |
1661 | SharedMDs[MD].reset(MD: const_cast<MDNode *>(MD)); |
1662 | } |
1663 | } |
1664 | |
1665 | Error IRMover::move(std::unique_ptr<Module> Src, |
1666 | ArrayRef<GlobalValue *> ValuesToLink, |
1667 | LazyCallback AddLazyFor, bool IsPerformingImport) { |
1668 | IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes, |
1669 | std::move(Src), ValuesToLink, std::move(AddLazyFor), |
1670 | IsPerformingImport); |
1671 | return TheIRLinker.run(); |
1672 | } |
1673 | |