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