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,
450 RF_ReuseAndMutateDistinctMDs | RF_IgnoreMissingLocals |
451 (IsPerformingImport ? RF_Importing : RF_None),
452 &TypeMap, &GValMaterializer),
453 IndirectSymbolMCID(Mapper.registerAlternateMappingContext(
454 VM&: IndirectSymbolValueMap, Materializer: &LValMaterializer)) {
455 ValueMap.getMDMap() = std::move(SharedMDs);
456 for (GlobalValue *GV : ValuesToLink)
457 maybeAdd(GV);
458 if (IsPerformingImport)
459 prepareCompileUnitsForImport();
460 }
461 ~IRLinker() { SharedMDs = std::move(*ValueMap.getMDMap()); }
462
463 Error run();
464 Value *materialize(Value *V, bool ForIndirectSymbol);
465};
466}
467
468/// The LLVM SymbolTable class autorenames globals that conflict in the symbol
469/// table. This is good for all clients except for us. Go through the trouble
470/// to force this back.
471static void forceRenaming(GlobalValue *GV, StringRef Name) {
472 // If the global doesn't force its name or if it already has the right name,
473 // there is nothing for us to do.
474 if (GV->hasLocalLinkage() || GV->getName() == Name)
475 return;
476
477 Module *M = GV->getParent();
478
479 // If there is a conflict, rename the conflict.
480 if (GlobalValue *ConflictGV = M->getNamedValue(Name)) {
481 GV->takeName(V: ConflictGV);
482 ConflictGV->setName(Name); // This will cause ConflictGV to get renamed
483 assert(ConflictGV->getName() != Name && "forceRenaming didn't work");
484 } else {
485 GV->setName(Name); // Force the name back
486 }
487}
488
489Value *GlobalValueMaterializer::materialize(Value *SGV) {
490 return TheIRLinker.materialize(V: SGV, ForIndirectSymbol: false);
491}
492
493Value *LocalValueMaterializer::materialize(Value *SGV) {
494 return TheIRLinker.materialize(V: SGV, ForIndirectSymbol: true);
495}
496
497Value *IRLinker::materialize(Value *V, bool ForIndirectSymbol) {
498 auto *SGV = dyn_cast<GlobalValue>(Val: V);
499 if (!SGV)
500 return nullptr;
501
502 // If SGV is from dest, it was already materialized when dest was loaded.
503 if (SGV->getParent() == &DstM)
504 return nullptr;
505
506 // When linking a global from other modules than source & dest, skip
507 // materializing it because it would be mapped later when its containing
508 // module is linked. Linking it now would potentially pull in many types that
509 // may not be mapped properly.
510 if (SGV->getParent() != SrcM.get())
511 return nullptr;
512
513 Expected<Constant *> NewProto = linkGlobalValueProto(GV: SGV, ForIndirectSymbol);
514 if (!NewProto) {
515 setError(NewProto.takeError());
516 return nullptr;
517 }
518 if (!*NewProto)
519 return nullptr;
520
521 GlobalValue *New = dyn_cast<GlobalValue>(Val: *NewProto);
522 if (!New)
523 return *NewProto;
524
525 // If we already created the body, just return.
526 if (auto *F = dyn_cast<Function>(Val: New)) {
527 if (!F->isDeclaration())
528 return New;
529 } else if (auto *V = dyn_cast<GlobalVariable>(Val: New)) {
530 if (V->hasInitializer() || V->hasAppendingLinkage())
531 return New;
532 } else if (auto *GA = dyn_cast<GlobalAlias>(Val: New)) {
533 if (GA->getAliasee())
534 return New;
535 } else if (auto *GI = dyn_cast<GlobalIFunc>(Val: New)) {
536 if (GI->getResolver())
537 return New;
538 } else {
539 llvm_unreachable("Invalid GlobalValue type");
540 }
541
542 // If the global is being linked for an indirect symbol, it may have already
543 // been scheduled to satisfy a regular symbol. Similarly, a global being linked
544 // for a regular symbol may have already been scheduled for an indirect
545 // symbol. Check for these cases by looking in the other value map and
546 // confirming the same value has been scheduled. If there is an entry in the
547 // ValueMap but the value is different, it means that the value already had a
548 // definition in the destination module (linkonce for instance), but we need a
549 // new definition for the indirect symbol ("New" will be different).
550 if ((ForIndirectSymbol && ValueMap.lookup(Val: SGV) == New) ||
551 (!ForIndirectSymbol && IndirectSymbolValueMap.lookup(Val: SGV) == New))
552 return New;
553
554 if (ForIndirectSymbol || shouldLink(DGV: New, SGV&: *SGV))
555 setError(linkGlobalValueBody(Dst&: *New, Src&: *SGV));
556
557 updateAttributes(GV&: *New);
558 return New;
559}
560
561/// Loop through the global variables in the src module and merge them into the
562/// dest module.
563GlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {
564 // No linking to be performed or linking from the source: simply create an
565 // identical version of the symbol over in the dest module... the
566 // initializer will be filled in later by LinkGlobalInits.
567 GlobalVariable *NewDGV =
568 new GlobalVariable(DstM, TypeMap.get(Ty: SGVar->getValueType()),
569 SGVar->isConstant(), GlobalValue::ExternalLinkage,
570 /*init*/ nullptr, SGVar->getName(),
571 /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
572 SGVar->getAddressSpace());
573 NewDGV->setAlignment(SGVar->getAlign());
574 NewDGV->copyAttributesFrom(Src: SGVar);
575 return NewDGV;
576}
577
578AttributeList IRLinker::mapAttributeTypes(LLVMContext &C, AttributeList Attrs) {
579 for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {
580 for (int AttrIdx = Attribute::FirstTypeAttr;
581 AttrIdx <= Attribute::LastTypeAttr; AttrIdx++) {
582 Attribute::AttrKind TypedAttr = (Attribute::AttrKind)AttrIdx;
583 if (Attrs.hasAttributeAtIndex(Index: i, Kind: TypedAttr)) {
584 if (Type *Ty =
585 Attrs.getAttributeAtIndex(Index: i, Kind: TypedAttr).getValueAsType()) {
586 Attrs = Attrs.replaceAttributeTypeAtIndex(C, ArgNo: i, Kind: TypedAttr,
587 ReplacementTy: TypeMap.get(Ty));
588 break;
589 }
590 }
591 }
592 }
593 return Attrs;
594}
595
596/// Link the function in the source module into the destination module if
597/// needed, setting up mapping information.
598Function *IRLinker::copyFunctionProto(const Function *SF) {
599 // If there is no linkage to be performed or we are linking from the source,
600 // bring SF over.
601 auto *F = Function::Create(Ty: TypeMap.get(T: SF->getFunctionType()),
602 Linkage: GlobalValue::ExternalLinkage,
603 AddrSpace: SF->getAddressSpace(), N: SF->getName(), M: &DstM);
604 F->copyAttributesFrom(Src: SF);
605 F->setAttributes(mapAttributeTypes(C&: F->getContext(), Attrs: F->getAttributes()));
606 return F;
607}
608
609/// Set up prototypes for any indirect symbols that come over from the source
610/// module.
611GlobalValue *IRLinker::copyIndirectSymbolProto(const GlobalValue *SGV) {
612 // If there is no linkage to be performed or we're linking from the source,
613 // bring over SGA.
614 auto *Ty = TypeMap.get(Ty: SGV->getValueType());
615
616 if (auto *GA = dyn_cast<GlobalAlias>(Val: SGV)) {
617 auto *DGA = GlobalAlias::create(Ty, AddressSpace: SGV->getAddressSpace(),
618 Linkage: GlobalValue::ExternalLinkage,
619 Name: SGV->getName(), Parent: &DstM);
620 DGA->copyAttributesFrom(Src: GA);
621 return DGA;
622 }
623
624 if (auto *GI = dyn_cast<GlobalIFunc>(Val: SGV)) {
625 auto *DGI = GlobalIFunc::create(Ty, AddressSpace: SGV->getAddressSpace(),
626 Linkage: GlobalValue::ExternalLinkage,
627 Name: SGV->getName(), Resolver: nullptr, Parent: &DstM);
628 DGI->copyAttributesFrom(Src: GI);
629 return DGI;
630 }
631
632 llvm_unreachable("Invalid source global value type");
633}
634
635GlobalValue *IRLinker::copyGlobalValueProto(const GlobalValue *SGV,
636 bool ForDefinition) {
637 GlobalValue *NewGV;
638 if (auto *SGVar = dyn_cast<GlobalVariable>(Val: SGV)) {
639 NewGV = copyGlobalVariableProto(SGVar);
640 } else if (auto *SF = dyn_cast<Function>(Val: SGV)) {
641 NewGV = copyFunctionProto(SF);
642 } else {
643 if (ForDefinition)
644 NewGV = copyIndirectSymbolProto(SGV);
645 else if (SGV->getValueType()->isFunctionTy())
646 NewGV =
647 Function::Create(Ty: cast<FunctionType>(Val: TypeMap.get(Ty: SGV->getValueType())),
648 Linkage: GlobalValue::ExternalLinkage, AddrSpace: SGV->getAddressSpace(),
649 N: SGV->getName(), M: &DstM);
650 else
651 NewGV =
652 new GlobalVariable(DstM, TypeMap.get(Ty: SGV->getValueType()),
653 /*isConstant*/ false, GlobalValue::ExternalLinkage,
654 /*init*/ nullptr, SGV->getName(),
655 /*insertbefore*/ nullptr,
656 SGV->getThreadLocalMode(), SGV->getAddressSpace());
657 }
658
659 if (ForDefinition)
660 NewGV->setLinkage(SGV->getLinkage());
661 else if (SGV->hasExternalWeakLinkage())
662 NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
663
664 if (auto *NewGO = dyn_cast<GlobalObject>(Val: NewGV)) {
665 // Metadata for global variables and function declarations is copied eagerly.
666 if (isa<GlobalVariable>(Val: SGV) || SGV->isDeclaration()) {
667 NewGO->copyMetadata(Src: cast<GlobalObject>(Val: SGV), Offset: 0);
668 if (SGV->isDeclaration() && NewGO->hasMetadata())
669 UnmappedMetadata.insert(V: NewGO);
670 }
671 }
672
673 // Remove these copied constants in case this stays a declaration, since
674 // they point to the source module. If the def is linked the values will
675 // be mapped in during linkFunctionBody.
676 if (auto *NewF = dyn_cast<Function>(Val: NewGV)) {
677 NewF->setPersonalityFn(nullptr);
678 NewF->setPrefixData(nullptr);
679 NewF->setPrologueData(nullptr);
680 }
681
682 return NewGV;
683}
684
685static StringRef getTypeNamePrefix(StringRef Name) {
686 size_t DotPos = Name.rfind(C: '.');
687 return (DotPos == 0 || DotPos == StringRef::npos || Name.back() == '.' ||
688 !isdigit(static_cast<unsigned char>(Name[DotPos + 1])))
689 ? Name
690 : Name.substr(Start: 0, N: DotPos);
691}
692
693/// Loop over all of the linked values to compute type mappings. For example,
694/// if we link "extern Foo *x" and "Foo *x = NULL", then we have two struct
695/// types 'Foo' but one got renamed when the module was loaded into the same
696/// LLVMContext.
697void IRLinker::computeTypeMapping() {
698 for (GlobalValue &SGV : SrcM->globals()) {
699 GlobalValue *DGV = getLinkedToGlobal(SrcGV: &SGV);
700 if (!DGV)
701 continue;
702
703 if (!DGV->hasAppendingLinkage() || !SGV.hasAppendingLinkage()) {
704 TypeMap.addTypeMapping(DstTy: DGV->getType(), SrcTy: SGV.getType());
705 continue;
706 }
707
708 // Unify the element type of appending arrays.
709 ArrayType *DAT = cast<ArrayType>(Val: DGV->getValueType());
710 ArrayType *SAT = cast<ArrayType>(Val: SGV.getValueType());
711 TypeMap.addTypeMapping(DstTy: DAT->getElementType(), SrcTy: SAT->getElementType());
712 }
713
714 for (GlobalValue &SGV : *SrcM)
715 if (GlobalValue *DGV = getLinkedToGlobal(SrcGV: &SGV)) {
716 if (DGV->getType() == SGV.getType()) {
717 // If the types of DGV and SGV are the same, it means that DGV is from
718 // the source module and got added to DstM from a shared metadata. We
719 // shouldn't map this type to itself in case the type's components get
720 // remapped to a new type from DstM (for instance, during the loop over
721 // SrcM->getIdentifiedStructTypes() below).
722 continue;
723 }
724
725 TypeMap.addTypeMapping(DstTy: DGV->getType(), SrcTy: SGV.getType());
726 }
727
728 for (GlobalValue &SGV : SrcM->aliases())
729 if (GlobalValue *DGV = getLinkedToGlobal(SrcGV: &SGV))
730 TypeMap.addTypeMapping(DstTy: DGV->getType(), SrcTy: SGV.getType());
731
732 // Incorporate types by name, scanning all the types in the source module.
733 // At this point, the destination module may have a type "%foo = { i32 }" for
734 // example. When the source module got loaded into the same LLVMContext, if
735 // it had the same type, it would have been renamed to "%foo.42 = { i32 }".
736 std::vector<StructType *> Types = SrcM->getIdentifiedStructTypes();
737 for (StructType *ST : Types) {
738 if (!ST->hasName())
739 continue;
740
741 if (TypeMap.DstStructTypesSet.hasType(Ty: ST)) {
742 // This is actually a type from the destination module.
743 // getIdentifiedStructTypes() can have found it by walking debug info
744 // metadata nodes, some of which get linked by name when ODR Type Uniquing
745 // is enabled on the Context, from the source to the destination module.
746 continue;
747 }
748
749 auto STTypePrefix = getTypeNamePrefix(Name: ST->getName());
750 if (STTypePrefix.size() == ST->getName().size())
751 continue;
752
753 // Check to see if the destination module has a struct with the prefix name.
754 StructType *DST = StructType::getTypeByName(C&: ST->getContext(), Name: STTypePrefix);
755 if (!DST)
756 continue;
757
758 // Don't use it if this actually came from the source module. They're in
759 // the same LLVMContext after all. Also don't use it unless the type is
760 // actually used in the destination module. This can happen in situations
761 // like this:
762 //
763 // Module A Module B
764 // -------- --------
765 // %Z = type { %A } %B = type { %C.1 }
766 // %A = type { %B.1, [7 x i8] } %C.1 = type { i8* }
767 // %B.1 = type { %C } %A.2 = type { %B.3, [5 x i8] }
768 // %C = type { i8* } %B.3 = type { %C.1 }
769 //
770 // When we link Module B with Module A, the '%B' in Module B is
771 // used. However, that would then use '%C.1'. But when we process '%C.1',
772 // we prefer to take the '%C' version. So we are then left with both
773 // '%C.1' and '%C' being used for the same types. This leads to some
774 // variables using one type and some using the other.
775 if (TypeMap.DstStructTypesSet.hasType(Ty: DST))
776 TypeMap.addTypeMapping(DstTy: DST, SrcTy: ST);
777 }
778}
779
780static void getArrayElements(const Constant *C,
781 SmallVectorImpl<Constant *> &Dest) {
782 unsigned NumElements = cast<ArrayType>(Val: C->getType())->getNumElements();
783
784 for (unsigned i = 0; i != NumElements; ++i)
785 Dest.push_back(Elt: C->getAggregateElement(Elt: i));
786}
787
788/// If there were any appending global variables, link them together now.
789Expected<Constant *>
790IRLinker::linkAppendingVarProto(GlobalVariable *DstGV,
791 const GlobalVariable *SrcGV) {
792 // Check that both variables have compatible properties.
793 if (DstGV && !DstGV->isDeclaration() && !SrcGV->isDeclaration()) {
794 if (!SrcGV->hasAppendingLinkage() || !DstGV->hasAppendingLinkage())
795 return stringErr(
796 T: "Linking globals named '" + SrcGV->getName() +
797 "': can only link appending global with another appending "
798 "global!");
799
800 if (DstGV->isConstant() != SrcGV->isConstant())
801 return stringErr(T: "Appending variables linked with different const'ness!");
802
803 if (DstGV->getAlign() != SrcGV->getAlign())
804 return stringErr(
805 T: "Appending variables with different alignment need to be linked!");
806
807 if (DstGV->getVisibility() != SrcGV->getVisibility())
808 return stringErr(
809 T: "Appending variables with different visibility need to be linked!");
810
811 if (DstGV->hasGlobalUnnamedAddr() != SrcGV->hasGlobalUnnamedAddr())
812 return stringErr(
813 T: "Appending variables with different unnamed_addr need to be linked!");
814
815 if (DstGV->getSection() != SrcGV->getSection())
816 return stringErr(
817 T: "Appending variables with different section name need to be linked!");
818
819 if (DstGV->getAddressSpace() != SrcGV->getAddressSpace())
820 return stringErr(T: "Appending variables with different address spaces need "
821 "to be linked!");
822 }
823
824 // Do not need to do anything if source is a declaration.
825 if (SrcGV->isDeclaration())
826 return DstGV;
827
828 Type *EltTy = cast<ArrayType>(Val: TypeMap.get(Ty: SrcGV->getValueType()))
829 ->getElementType();
830
831 // FIXME: This upgrade is done during linking to support the C API. Once the
832 // old form is deprecated, we should move this upgrade to
833 // llvm::UpgradeGlobalVariable() and simplify the logic here and in
834 // Mapper::mapAppendingVariable() in ValueMapper.cpp.
835 StringRef Name = SrcGV->getName();
836 bool IsNewStructor = false;
837 bool IsOldStructor = false;
838 if (Name == "llvm.global_ctors" || Name == "llvm.global_dtors") {
839 if (cast<StructType>(Val: EltTy)->getNumElements() == 3)
840 IsNewStructor = true;
841 else
842 IsOldStructor = true;
843 }
844
845 PointerType *VoidPtrTy = PointerType::get(C&: SrcGV->getContext(), AddressSpace: 0);
846 if (IsOldStructor) {
847 auto &ST = *cast<StructType>(Val: EltTy);
848 Type *Tys[3] = {ST.getElementType(N: 0), ST.getElementType(N: 1), VoidPtrTy};
849 EltTy = StructType::get(Context&: SrcGV->getContext(), Elements: Tys, isPacked: false);
850 }
851
852 uint64_t DstNumElements = 0;
853 if (DstGV && !DstGV->isDeclaration()) {
854 ArrayType *DstTy = cast<ArrayType>(Val: DstGV->getValueType());
855 DstNumElements = DstTy->getNumElements();
856
857 // Check to see that they two arrays agree on type.
858 if (EltTy != DstTy->getElementType())
859 return stringErr(T: "Appending variables with different element types!");
860 }
861
862 SmallVector<Constant *, 16> SrcElements;
863 getArrayElements(C: SrcGV->getInitializer(), Dest&: SrcElements);
864
865 if (IsNewStructor) {
866 erase_if(C&: SrcElements, P: [this](Constant *E) {
867 auto *Key =
868 dyn_cast<GlobalValue>(Val: E->getAggregateElement(Elt: 2)->stripPointerCasts());
869 if (!Key)
870 return false;
871 GlobalValue *DGV = getLinkedToGlobal(SrcGV: Key);
872 return !shouldLink(DGV, SGV&: *Key);
873 });
874 }
875 uint64_t NewSize = DstNumElements + SrcElements.size();
876 ArrayType *NewType = ArrayType::get(ElementType: EltTy, NumElements: NewSize);
877
878 // Create the new global variable.
879 GlobalVariable *NG = new GlobalVariable(
880 DstM, NewType, SrcGV->isConstant(), SrcGV->getLinkage(),
881 /*init*/ nullptr, /*name*/ "", DstGV, SrcGV->getThreadLocalMode(),
882 SrcGV->getAddressSpace());
883
884 NG->copyAttributesFrom(Src: SrcGV);
885 forceRenaming(GV: NG, Name: SrcGV->getName());
886
887 Mapper.scheduleMapAppendingVariable(GV&: *NG, OldGV: DstGV, IsOldCtorDtor: IsOldStructor, NewMembers: SrcElements);
888
889 // Replace any uses of the two global variables with uses of the new
890 // global.
891 if (DstGV) {
892 RAUWWorklist.push_back(x: std::make_pair(x&: DstGV, y&: NG));
893 }
894
895 return NG;
896}
897
898bool IRLinker::shouldLink(GlobalValue *DGV, GlobalValue &SGV) {
899 if (ValuesToLink.count(V: &SGV) || SGV.hasLocalLinkage())
900 return true;
901
902 if (DGV && !DGV->isDeclarationForLinker())
903 return false;
904
905 if (SGV.isDeclaration() || DoneLinkingBodies)
906 return false;
907
908 // Callback to the client to give a chance to lazily add the Global to the
909 // list of value to link.
910 bool LazilyAdded = false;
911 if (AddLazyFor)
912 AddLazyFor(SGV, [this, &LazilyAdded](GlobalValue &GV) {
913 maybeAdd(GV: &GV);
914 LazilyAdded = true;
915 });
916 return LazilyAdded;
917}
918
919Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV,
920 bool ForIndirectSymbol) {
921 GlobalValue *DGV = getLinkedToGlobal(SrcGV: SGV);
922
923 bool ShouldLink = shouldLink(DGV, SGV&: *SGV);
924
925 // just missing from map
926 if (ShouldLink) {
927 auto I = ValueMap.find(Val: SGV);
928 if (I != ValueMap.end())
929 return cast<Constant>(Val&: I->second);
930
931 I = IndirectSymbolValueMap.find(Val: SGV);
932 if (I != IndirectSymbolValueMap.end())
933 return cast<Constant>(Val&: I->second);
934 }
935
936 if (!ShouldLink && ForIndirectSymbol)
937 DGV = nullptr;
938
939 // Handle the ultra special appending linkage case first.
940 if (SGV->hasAppendingLinkage() || (DGV && DGV->hasAppendingLinkage()))
941 return linkAppendingVarProto(DstGV: cast_or_null<GlobalVariable>(Val: DGV),
942 SrcGV: cast<GlobalVariable>(Val: SGV));
943
944 bool NeedsRenaming = false;
945 GlobalValue *NewGV;
946 if (DGV && !ShouldLink) {
947 NewGV = DGV;
948
949 // If the source is an exact definition, optimizations may have modified
950 // its attributes, e.g. by dropping noundef attributes when replacing
951 // arguments with poison. In this case, it is important for correctness
952 // that we use the signature from the exact definition.
953 if (isa<Function>(Val: SGV) && DGV->isDeclaration() &&
954 SGV->hasExactDefinition() && !DoneLinkingBodies) {
955 NewGV = copyGlobalValueProto(SGV, /*ForDefinition=*/false);
956 NeedsRenaming = true;
957 }
958 } else {
959 // If we are done linking global value bodies (i.e. we are performing
960 // metadata linking), don't link in the global value due to this
961 // reference, simply map it to null.
962 if (DoneLinkingBodies)
963 return nullptr;
964
965 NewGV = copyGlobalValueProto(SGV, ForDefinition: ShouldLink || ForIndirectSymbol);
966 if (ShouldLink || !ForIndirectSymbol)
967 NeedsRenaming = true;
968 }
969
970 // Overloaded intrinsics have overloaded types names as part of their
971 // names. If we renamed overloaded types we should rename the intrinsic
972 // as well.
973 if (Function *F = dyn_cast<Function>(Val: NewGV))
974 if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) {
975 // Note: remangleIntrinsicFunction does not copy metadata and as such
976 // F should not occur in the set of objects with unmapped metadata.
977 // If this assertion fails then remangleIntrinsicFunction needs updating.
978 assert(!UnmappedMetadata.count(F) && "intrinsic has unmapped metadata");
979 NewGV->eraseFromParent();
980 NewGV = *Remangled;
981 NeedsRenaming = false;
982 }
983
984 if (NeedsRenaming)
985 forceRenaming(GV: NewGV, Name: SGV->getName());
986
987 if (ShouldLink || ForIndirectSymbol) {
988 if (const Comdat *SC = SGV->getComdat()) {
989 if (auto *GO = dyn_cast<GlobalObject>(Val: NewGV)) {
990 Comdat *DC = DstM.getOrInsertComdat(Name: SC->getName());
991 DC->setSelectionKind(SC->getSelectionKind());
992 GO->setComdat(DC);
993 }
994 }
995 }
996
997 if (!ShouldLink && ForIndirectSymbol)
998 NewGV->setLinkage(GlobalValue::InternalLinkage);
999
1000 Constant *C = NewGV;
1001 // Only create a bitcast if necessary. In particular, with
1002 // DebugTypeODRUniquing we may reach metadata in the destination module
1003 // containing a GV from the source module, in which case SGV will be
1004 // the same as DGV and NewGV, and TypeMap.get() will assert since it
1005 // assumes it is being invoked on a type in the source module.
1006 if (DGV && NewGV != SGV) {
1007 C = ConstantExpr::getPointerBitCastOrAddrSpaceCast(
1008 C: NewGV, Ty: TypeMap.get(Ty: SGV->getType()));
1009 }
1010
1011 if (DGV && NewGV != DGV) {
1012 // Schedule "replace all uses with" to happen after materializing is
1013 // done. It is not safe to do it now, since ValueMapper may be holding
1014 // pointers to constants that will get deleted if RAUW runs.
1015 RAUWWorklist.push_back(x: std::make_pair(
1016 x&: DGV,
1017 y: ConstantExpr::getPointerBitCastOrAddrSpaceCast(C: NewGV, Ty: DGV->getType())));
1018 }
1019
1020 return C;
1021}
1022
1023/// Update the initializers in the Dest module now that all globals that may be
1024/// referenced are in Dest.
1025void IRLinker::linkGlobalVariable(GlobalVariable &Dst, GlobalVariable &Src) {
1026 // Figure out what the initializer looks like in the dest module.
1027 Mapper.scheduleMapGlobalInitializer(GV&: Dst, Init&: *Src.getInitializer());
1028}
1029
1030/// Copy the source function over into the dest function and fix up references
1031/// to values. At this point we know that Dest is an external function, and
1032/// that Src is not.
1033Error IRLinker::linkFunctionBody(Function &Dst, Function &Src) {
1034 assert(Dst.isDeclaration() && !Src.isDeclaration());
1035
1036 // Materialize if needed.
1037 if (Error Err = Src.materialize())
1038 return Err;
1039
1040 // Link in the operands without remapping.
1041 if (Src.hasPrefixData())
1042 Dst.setPrefixData(Src.getPrefixData());
1043 if (Src.hasPrologueData())
1044 Dst.setPrologueData(Src.getPrologueData());
1045 if (Src.hasPersonalityFn())
1046 Dst.setPersonalityFn(Src.getPersonalityFn());
1047
1048 // Copy over the metadata attachments without remapping.
1049 Dst.copyMetadata(Src: &Src, Offset: 0);
1050
1051 // Steal arguments and splice the body of Src into Dst.
1052 Dst.stealArgumentListFrom(Src);
1053 Dst.splice(ToIt: Dst.end(), FromF: &Src);
1054
1055 // Everything has been moved over. Remap it.
1056 Mapper.scheduleRemapFunction(F&: Dst);
1057 return Error::success();
1058}
1059
1060void IRLinker::linkAliasAliasee(GlobalAlias &Dst, GlobalAlias &Src) {
1061 Mapper.scheduleMapGlobalAlias(GA&: Dst, Aliasee&: *Src.getAliasee(), MappingContextID: IndirectSymbolMCID);
1062}
1063
1064void IRLinker::linkIFuncResolver(GlobalIFunc &Dst, GlobalIFunc &Src) {
1065 Mapper.scheduleMapGlobalIFunc(GI&: Dst, Resolver&: *Src.getResolver(), MappingContextID: IndirectSymbolMCID);
1066}
1067
1068Error IRLinker::linkGlobalValueBody(GlobalValue &Dst, GlobalValue &Src) {
1069 if (auto *F = dyn_cast<Function>(Val: &Src))
1070 return linkFunctionBody(Dst&: cast<Function>(Val&: Dst), Src&: *F);
1071 if (auto *GVar = dyn_cast<GlobalVariable>(Val: &Src)) {
1072 linkGlobalVariable(Dst&: cast<GlobalVariable>(Val&: Dst), Src&: *GVar);
1073 return Error::success();
1074 }
1075 if (auto *GA = dyn_cast<GlobalAlias>(Val: &Src)) {
1076 linkAliasAliasee(Dst&: cast<GlobalAlias>(Val&: Dst), Src&: *GA);
1077 return Error::success();
1078 }
1079 linkIFuncResolver(Dst&: cast<GlobalIFunc>(Val&: Dst), Src&: cast<GlobalIFunc>(Val&: Src));
1080 return Error::success();
1081}
1082
1083void IRLinker::flushRAUWWorklist() {
1084 for (const auto &Elem : RAUWWorklist) {
1085 GlobalValue *Old;
1086 Value *New;
1087 std::tie(args&: Old, args&: New) = Elem;
1088
1089 Old->replaceAllUsesWith(V: New);
1090 Old->eraseFromParent();
1091 }
1092 RAUWWorklist.clear();
1093}
1094
1095void IRLinker::prepareCompileUnitsForImport() {
1096 NamedMDNode *SrcCompileUnits = SrcM->getNamedMetadata(Name: "llvm.dbg.cu");
1097 if (!SrcCompileUnits)
1098 return;
1099 // When importing for ThinLTO, prevent importing of types listed on
1100 // the DICompileUnit that we don't need a copy of in the importing
1101 // module. They will be emitted by the originating module.
1102 for (MDNode *N : SrcCompileUnits->operands()) {
1103 auto *CU = cast<DICompileUnit>(Val: N);
1104 assert(CU && "Expected valid compile unit");
1105 // Enums, macros, and retained types don't need to be listed on the
1106 // imported DICompileUnit. This means they will only be imported
1107 // if reached from the mapped IR.
1108 CU->replaceEnumTypes(N: nullptr);
1109 CU->replaceMacros(N: nullptr);
1110 CU->replaceRetainedTypes(N: nullptr);
1111
1112 // The original definition (or at least its debug info - if the variable is
1113 // internalized and optimized away) will remain in the source module, so
1114 // there's no need to import them.
1115 // If LLVM ever does more advanced optimizations on global variables
1116 // (removing/localizing write operations, for instance) that can track
1117 // through debug info, this decision may need to be revisited - but do so
1118 // with care when it comes to debug info size. Emitting small CUs containing
1119 // only a few imported entities into every destination module may be very
1120 // size inefficient.
1121 CU->replaceGlobalVariables(N: nullptr);
1122
1123 CU->replaceImportedEntities(N: nullptr);
1124 }
1125}
1126
1127/// Insert all of the named MDNodes in Src into the Dest module.
1128void IRLinker::linkNamedMDNodes() {
1129 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1130 for (const NamedMDNode &NMD : SrcM->named_metadata()) {
1131 // Don't link module flags here. Do them separately.
1132 if (&NMD == SrcModFlags)
1133 continue;
1134 // Don't import pseudo probe descriptors here for thinLTO. They will be
1135 // emitted by the originating module.
1136 if (IsPerformingImport && NMD.getName() == PseudoProbeDescMetadataName) {
1137 if (!DstM.getNamedMetadata(Name: NMD.getName()))
1138 emitWarning(Message: "Pseudo-probe ignored: source module '" +
1139 SrcM->getModuleIdentifier() +
1140 "' is compiled with -fpseudo-probe-for-profiling while "
1141 "destination module '" +
1142 DstM.getModuleIdentifier() + "' is not\n");
1143 continue;
1144 }
1145 // The stats are computed per module and will all be merged in the binary.
1146 // Importing the metadata will cause duplication of the stats.
1147 if (IsPerformingImport && NMD.getName() == "llvm.stats")
1148 continue;
1149
1150 NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(Name: NMD.getName());
1151
1152 auto &Inserted = NamedMDNodes[DestNMD];
1153 if (Inserted.empty()) {
1154 // Must be the first module, copy everything from DestNMD.
1155 Inserted.insert(I: DestNMD->operands().begin(), E: DestNMD->operands().end());
1156 }
1157
1158 // Add Src elements into Dest node.
1159 for (const MDNode *Op : NMD.operands()) {
1160 MDNode *MD = Mapper.mapMDNode(N: *Op);
1161 if (Inserted.insert(Ptr: MD).second)
1162 DestNMD->addOperand(M: MD);
1163 }
1164 }
1165}
1166
1167/// Merge the linker flags in Src into the Dest module.
1168Error IRLinker::linkModuleFlagsMetadata() {
1169 // If the source module has no module flags, we are done.
1170 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1171 if (!SrcModFlags)
1172 return Error::success();
1173
1174 // Check for module flag for updates before do anything.
1175 UpgradeModuleFlags(M&: *SrcM);
1176 UpgradeNVVMAnnotations(M&: *SrcM);
1177
1178 // If the destination module doesn't have module flags yet, then just copy
1179 // over the source module's flags.
1180 NamedMDNode *DstModFlags = DstM.getOrInsertModuleFlagsMetadata();
1181 if (DstModFlags->getNumOperands() == 0) {
1182 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1183 DstModFlags->addOperand(M: SrcModFlags->getOperand(i: I));
1184
1185 return Error::success();
1186 }
1187
1188 // First build a map of the existing module flags and requirements.
1189 DenseMap<MDString *, std::pair<MDNode *, unsigned>> Flags;
1190 SmallSetVector<MDNode *, 16> Requirements;
1191 SmallVector<unsigned, 0> Mins;
1192 DenseSet<MDString *> SeenMin;
1193 for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1194 MDNode *Op = DstModFlags->getOperand(i: I);
1195 uint64_t Behavior =
1196 mdconst::extract<ConstantInt>(MD: Op->getOperand(I: 0))->getZExtValue();
1197 MDString *ID = cast<MDString>(Val: Op->getOperand(I: 1));
1198
1199 if (Behavior == Module::Require) {
1200 Requirements.insert(X: cast<MDNode>(Val: Op->getOperand(I: 2)));
1201 } else {
1202 if (Behavior == Module::Min)
1203 Mins.push_back(Elt: I);
1204 Flags[ID] = std::make_pair(x&: Op, y&: I);
1205 }
1206 }
1207
1208 // Merge in the flags from the source module, and also collect its set of
1209 // requirements.
1210 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1211 MDNode *SrcOp = SrcModFlags->getOperand(i: I);
1212 ConstantInt *SrcBehavior =
1213 mdconst::extract<ConstantInt>(MD: SrcOp->getOperand(I: 0));
1214 MDString *ID = cast<MDString>(Val: SrcOp->getOperand(I: 1));
1215 MDNode *DstOp;
1216 unsigned DstIndex;
1217 std::tie(args&: DstOp, args&: DstIndex) = Flags.lookup(Val: ID);
1218 unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1219 SeenMin.insert(V: ID);
1220
1221 // If this is a requirement, add it and continue.
1222 if (SrcBehaviorValue == Module::Require) {
1223 // If the destination module does not already have this requirement, add
1224 // it.
1225 if (Requirements.insert(X: cast<MDNode>(Val: SrcOp->getOperand(I: 2)))) {
1226 DstModFlags->addOperand(M: SrcOp);
1227 }
1228 continue;
1229 }
1230
1231 // If there is no existing flag with this ID, just add it.
1232 if (!DstOp) {
1233 if (SrcBehaviorValue == Module::Min) {
1234 Mins.push_back(Elt: DstModFlags->getNumOperands());
1235 SeenMin.erase(V: ID);
1236 }
1237 Flags[ID] = std::make_pair(x&: SrcOp, y: DstModFlags->getNumOperands());
1238 DstModFlags->addOperand(M: SrcOp);
1239 continue;
1240 }
1241
1242 // Otherwise, perform a merge.
1243 ConstantInt *DstBehavior =
1244 mdconst::extract<ConstantInt>(MD: DstOp->getOperand(I: 0));
1245 unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1246
1247 auto overrideDstValue = [&]() {
1248 DstModFlags->setOperand(I: DstIndex, New: SrcOp);
1249 Flags[ID].first = SrcOp;
1250 };
1251
1252 // If either flag has override behavior, handle it first.
1253 if (DstBehaviorValue == Module::Override) {
1254 // Diagnose inconsistent flags which both have override behavior.
1255 if (SrcBehaviorValue == Module::Override &&
1256 SrcOp->getOperand(I: 2) != DstOp->getOperand(I: 2))
1257 return stringErr(T: "linking module flags '" + ID->getString() +
1258 "': IDs have conflicting override values in '" +
1259 SrcM->getModuleIdentifier() + "' and '" +
1260 DstM.getModuleIdentifier() + "'");
1261 continue;
1262 } else if (SrcBehaviorValue == Module::Override) {
1263 // Update the destination flag to that of the source.
1264 overrideDstValue();
1265 continue;
1266 }
1267
1268 // Diagnose inconsistent merge behavior types.
1269 if (SrcBehaviorValue != DstBehaviorValue) {
1270 bool MinAndWarn = (SrcBehaviorValue == Module::Min &&
1271 DstBehaviorValue == Module::Warning) ||
1272 (DstBehaviorValue == Module::Min &&
1273 SrcBehaviorValue == Module::Warning);
1274 bool MaxAndWarn = (SrcBehaviorValue == Module::Max &&
1275 DstBehaviorValue == Module::Warning) ||
1276 (DstBehaviorValue == Module::Max &&
1277 SrcBehaviorValue == Module::Warning);
1278 if (!(MaxAndWarn || MinAndWarn))
1279 return stringErr(T: "linking module flags '" + ID->getString() +
1280 "': IDs have conflicting behaviors in '" +
1281 SrcM->getModuleIdentifier() + "' and '" +
1282 DstM.getModuleIdentifier() + "'");
1283 }
1284
1285 auto ensureDistinctOp = [&](MDNode *DstValue) {
1286 assert(isa<MDTuple>(DstValue) &&
1287 "Expected MDTuple when appending module flags");
1288 if (DstValue->isDistinct())
1289 return dyn_cast<MDTuple>(Val: DstValue);
1290 ArrayRef<MDOperand> DstOperands = DstValue->operands();
1291 MDTuple *New = MDTuple::getDistinct(
1292 Context&: DstM.getContext(), MDs: SmallVector<Metadata *, 4>(DstOperands));
1293 Metadata *FlagOps[] = {DstOp->getOperand(I: 0), ID, New};
1294 MDNode *Flag = MDTuple::getDistinct(Context&: DstM.getContext(), MDs: FlagOps);
1295 DstModFlags->setOperand(I: DstIndex, New: Flag);
1296 Flags[ID].first = Flag;
1297 return New;
1298 };
1299
1300 // Emit a warning if the values differ and either source or destination
1301 // request Warning behavior.
1302 if ((DstBehaviorValue == Module::Warning ||
1303 SrcBehaviorValue == Module::Warning) &&
1304 SrcOp->getOperand(I: 2) != DstOp->getOperand(I: 2)) {
1305 std::string Str;
1306 raw_string_ostream(Str)
1307 << "linking module flags '" << ID->getString()
1308 << "': IDs have conflicting values ('" << *SrcOp->getOperand(I: 2)
1309 << "' from " << SrcM->getModuleIdentifier() << " with '"
1310 << *DstOp->getOperand(I: 2) << "' from " << DstM.getModuleIdentifier()
1311 << ')';
1312 emitWarning(Message: Str);
1313 }
1314
1315 // Choose the minimum if either source or destination request Min behavior.
1316 if (DstBehaviorValue == Module::Min || SrcBehaviorValue == Module::Min) {
1317 ConstantInt *DstValue =
1318 mdconst::extract<ConstantInt>(MD: DstOp->getOperand(I: 2));
1319 ConstantInt *SrcValue =
1320 mdconst::extract<ConstantInt>(MD: SrcOp->getOperand(I: 2));
1321
1322 // The resulting flag should have a Min behavior, and contain the minimum
1323 // value from between the source and destination values.
1324 Metadata *FlagOps[] = {
1325 (DstBehaviorValue != Module::Min ? SrcOp : DstOp)->getOperand(I: 0), ID,
1326 (SrcValue->getZExtValue() < DstValue->getZExtValue() ? SrcOp : DstOp)
1327 ->getOperand(I: 2)};
1328 MDNode *Flag = MDNode::get(Context&: DstM.getContext(), MDs: FlagOps);
1329 DstModFlags->setOperand(I: DstIndex, New: Flag);
1330 Flags[ID].first = Flag;
1331 continue;
1332 }
1333
1334 // Choose the maximum if either source or destination request Max behavior.
1335 if (DstBehaviorValue == Module::Max || SrcBehaviorValue == Module::Max) {
1336 ConstantInt *DstValue =
1337 mdconst::extract<ConstantInt>(MD: DstOp->getOperand(I: 2));
1338 ConstantInt *SrcValue =
1339 mdconst::extract<ConstantInt>(MD: SrcOp->getOperand(I: 2));
1340
1341 // The resulting flag should have a Max behavior, and contain the maximum
1342 // value from between the source and destination values.
1343 Metadata *FlagOps[] = {
1344 (DstBehaviorValue != Module::Max ? SrcOp : DstOp)->getOperand(I: 0), ID,
1345 (SrcValue->getZExtValue() > DstValue->getZExtValue() ? SrcOp : DstOp)
1346 ->getOperand(I: 2)};
1347 MDNode *Flag = MDNode::get(Context&: DstM.getContext(), MDs: FlagOps);
1348 DstModFlags->setOperand(I: DstIndex, New: Flag);
1349 Flags[ID].first = Flag;
1350 continue;
1351 }
1352
1353 // Perform the merge for standard behavior types.
1354 switch (SrcBehaviorValue) {
1355 case Module::Require:
1356 case Module::Override:
1357 llvm_unreachable("not possible");
1358 case Module::Error: {
1359 // Emit an error if the values differ.
1360 if (SrcOp->getOperand(I: 2) != DstOp->getOperand(I: 2)) {
1361 std::string Str;
1362 raw_string_ostream(Str)
1363 << "linking module flags '" << ID->getString()
1364 << "': IDs have conflicting values: '" << *SrcOp->getOperand(I: 2)
1365 << "' from " << SrcM->getModuleIdentifier() << ", and '"
1366 << *DstOp->getOperand(I: 2) << "' from " + DstM.getModuleIdentifier();
1367 return stringErr(T: Str);
1368 }
1369 continue;
1370 }
1371 case Module::Warning: {
1372 break;
1373 }
1374 case Module::Max: {
1375 break;
1376 }
1377 case Module::Append: {
1378 MDTuple *DstValue = ensureDistinctOp(cast<MDNode>(Val: DstOp->getOperand(I: 2)));
1379 MDNode *SrcValue = cast<MDNode>(Val: SrcOp->getOperand(I: 2));
1380 for (const auto &O : SrcValue->operands())
1381 DstValue->push_back(MD: O);
1382 break;
1383 }
1384 case Module::AppendUnique: {
1385 SmallSetVector<Metadata *, 16> Elts;
1386 MDTuple *DstValue = ensureDistinctOp(cast<MDNode>(Val: DstOp->getOperand(I: 2)));
1387 MDNode *SrcValue = cast<MDNode>(Val: SrcOp->getOperand(I: 2));
1388 Elts.insert(Start: DstValue->op_begin(), End: DstValue->op_end());
1389 Elts.insert(Start: SrcValue->op_begin(), End: SrcValue->op_end());
1390 for (auto I = DstValue->getNumOperands(); I < Elts.size(); I++)
1391 DstValue->push_back(MD: Elts[I]);
1392 break;
1393 }
1394 }
1395
1396 }
1397
1398 // For the Min behavior, set the value to 0 if either module does not have the
1399 // flag.
1400 for (auto Idx : Mins) {
1401 MDNode *Op = DstModFlags->getOperand(i: Idx);
1402 MDString *ID = cast<MDString>(Val: Op->getOperand(I: 1));
1403 if (!SeenMin.count(V: ID)) {
1404 ConstantInt *V = mdconst::extract<ConstantInt>(MD: Op->getOperand(I: 2));
1405 Metadata *FlagOps[] = {
1406 Op->getOperand(I: 0), ID,
1407 ConstantAsMetadata::get(C: ConstantInt::get(Ty: V->getType(), V: 0))};
1408 DstModFlags->setOperand(I: Idx, New: MDNode::get(Context&: DstM.getContext(), MDs: FlagOps));
1409 }
1410 }
1411
1412 // Check all of the requirements.
1413 for (MDNode *Requirement : Requirements) {
1414 MDString *Flag = cast<MDString>(Val: Requirement->getOperand(I: 0));
1415 Metadata *ReqValue = Requirement->getOperand(I: 1);
1416
1417 MDNode *Op = Flags[Flag].first;
1418 if (!Op || Op->getOperand(I: 2) != ReqValue)
1419 return stringErr(T: "linking module flags '" + Flag->getString() +
1420 "': does not have the required value");
1421 }
1422 return Error::success();
1423}
1424
1425/// Return InlineAsm adjusted with target-specific directives if required.
1426/// For ARM and Thumb, we have to add directives to select the appropriate ISA
1427/// to support mixing module-level inline assembly from ARM and Thumb modules.
1428static std::string adjustInlineAsm(const std::string &InlineAsm,
1429 const Triple &Triple) {
1430 if (Triple.getArch() == Triple::thumb || Triple.getArch() == Triple::thumbeb)
1431 return ".text\n.balign 2\n.thumb\n" + InlineAsm;
1432 if (Triple.getArch() == Triple::arm || Triple.getArch() == Triple::armeb)
1433 return ".text\n.balign 4\n.arm\n" + InlineAsm;
1434 return InlineAsm;
1435}
1436
1437void IRLinker::updateAttributes(GlobalValue &GV) {
1438 /// Remove nocallback attribute while linking, because nocallback attribute
1439 /// indicates that the function is only allowed to jump back into caller's
1440 /// module only by a return or an exception. When modules are linked, this
1441 /// property cannot be guaranteed anymore. For example, the nocallback
1442 /// function may contain a call to another module. But if we merge its caller
1443 /// and callee module here, and not the module containing the nocallback
1444 /// function definition itself, the nocallback property will be violated
1445 /// (since the nocallback function will call back into the newly merged module
1446 /// containing both its caller and callee). This could happen if the module
1447 /// containing the nocallback function definition is native code, so it does
1448 /// not participate in the LTO link. Note if the nocallback function does
1449 /// participate in the LTO link, and thus ends up in the merged module
1450 /// containing its caller and callee, removing the attribute doesn't hurt as
1451 /// it has no effect on definitions in the same module.
1452 if (auto *F = dyn_cast<Function>(Val: &GV)) {
1453 if (!F->isIntrinsic())
1454 F->removeFnAttr(Kind: llvm::Attribute::NoCallback);
1455
1456 // Remove nocallback attribute when it is on a call-site.
1457 for (BasicBlock &BB : *F)
1458 for (Instruction &I : BB)
1459 if (CallBase *CI = dyn_cast<CallBase>(Val: &I))
1460 CI->removeFnAttr(Kind: Attribute::NoCallback);
1461 }
1462}
1463
1464Error IRLinker::run() {
1465 // Ensure metadata materialized before value mapping.
1466 if (SrcM->getMaterializer())
1467 if (Error Err = SrcM->getMaterializer()->materializeMetadata())
1468 return Err;
1469
1470 // Inherit the target data from the source module if the destination
1471 // module doesn't have one already.
1472 if (DstM.getDataLayout().isDefault())
1473 DstM.setDataLayout(SrcM->getDataLayout());
1474
1475 // Copy the target triple from the source to dest if the dest's is empty.
1476 if (DstM.getTargetTriple().empty() && !SrcM->getTargetTriple().empty())
1477 DstM.setTargetTriple(SrcM->getTargetTriple());
1478
1479 Triple SrcTriple(SrcM->getTargetTriple()), DstTriple(DstM.getTargetTriple());
1480
1481 // During CUDA compilation we have to link with the bitcode supplied with
1482 // CUDA. libdevice bitcode either has no data layout set (pre-CUDA-11), or has
1483 // the layout that is different from the one used by LLVM/clang (it does not
1484 // include i128). Issuing a warning is not very helpful as there's not much
1485 // the user can do about it.
1486 bool EnableDLWarning = true;
1487 bool EnableTripleWarning = true;
1488 if (SrcTriple.isNVPTX() && DstTriple.isNVPTX()) {
1489 bool SrcHasLibDeviceDL =
1490 (SrcM->getDataLayoutStr().empty() ||
1491 SrcM->getDataLayoutStr() == "e-i64:64-v16:16-v32:32-n16:32:64");
1492 // libdevice bitcode uses nvptx64-nvidia-gpulibs or just
1493 // 'nvptx-unknown-unknown' triple (before CUDA-10.x) and is compatible with
1494 // all NVPTX variants.
1495 bool SrcHasLibDeviceTriple = (SrcTriple.getVendor() == Triple::NVIDIA &&
1496 SrcTriple.getOSName() == "gpulibs") ||
1497 (SrcTriple.getVendorName() == "unknown" &&
1498 SrcTriple.getOSName() == "unknown");
1499 EnableTripleWarning = !SrcHasLibDeviceTriple;
1500 EnableDLWarning = !(SrcHasLibDeviceTriple && SrcHasLibDeviceDL);
1501 }
1502
1503 if (EnableDLWarning && (SrcM->getDataLayout() != DstM.getDataLayout())) {
1504 emitWarning(Message: "Linking two modules of different data layouts: '" +
1505 SrcM->getModuleIdentifier() + "' is '" +
1506 SrcM->getDataLayoutStr() + "' whereas '" +
1507 DstM.getModuleIdentifier() + "' is '" +
1508 DstM.getDataLayoutStr() + "'\n");
1509 }
1510
1511 if (EnableTripleWarning && !SrcM->getTargetTriple().empty() &&
1512 !SrcTriple.isCompatibleWith(Other: DstTriple))
1513 emitWarning(Message: "Linking two modules of different target triples: '" +
1514 SrcM->getModuleIdentifier() + "' is '" +
1515 SrcM->getTargetTriple().str() + "' whereas '" +
1516 DstM.getModuleIdentifier() + "' is '" +
1517 DstM.getTargetTriple().str() + "'\n");
1518
1519 DstM.setTargetTriple(Triple(SrcTriple.merge(Other: DstTriple)));
1520
1521 // Loop over all of the linked values to compute type mappings.
1522 computeTypeMapping();
1523
1524 // Convert module level attributes to function level attributes because
1525 // after merging modules the attributes might change and would have different
1526 // effect on the functions as the original module would have.
1527 copyModuleAttrToFunctions(M&: *SrcM);
1528
1529 std::reverse(first: Worklist.begin(), last: Worklist.end());
1530 while (!Worklist.empty()) {
1531 GlobalValue *GV = Worklist.back();
1532 Worklist.pop_back();
1533
1534 // Already mapped.
1535 if (ValueMap.find(Val: GV) != ValueMap.end() ||
1536 IndirectSymbolValueMap.find(Val: GV) != IndirectSymbolValueMap.end())
1537 continue;
1538
1539 assert(!GV->isDeclaration());
1540 Mapper.mapValue(V: *GV);
1541 if (FoundError)
1542 return std::move(*FoundError);
1543 flushRAUWWorklist();
1544 }
1545
1546 // Note that we are done linking global value bodies. This prevents
1547 // metadata linking from creating new references.
1548 DoneLinkingBodies = true;
1549 Mapper.addFlags(Flags: RF_NullMapMissingGlobalValues);
1550
1551 // Remap all of the named MDNodes in Src into the DstM module. We do this
1552 // after linking GlobalValues so that MDNodes that reference GlobalValues
1553 // are properly remapped.
1554 linkNamedMDNodes();
1555
1556 // Clean up any global objects with potentially unmapped metadata.
1557 // Specifically declarations which did not become definitions.
1558 for (GlobalObject *NGO : UnmappedMetadata) {
1559 if (NGO->isDeclaration())
1560 Mapper.remapGlobalObjectMetadata(GO&: *NGO);
1561 }
1562
1563 if (!IsPerformingImport && !SrcM->getModuleInlineAsm().empty()) {
1564 // Append the module inline asm string.
1565 DstM.appendModuleInlineAsm(Asm: adjustInlineAsm(InlineAsm: SrcM->getModuleInlineAsm(),
1566 Triple: SrcTriple));
1567 } else if (IsPerformingImport) {
1568 // Import any symver directives for symbols in DstM.
1569 ModuleSymbolTable::CollectAsmSymvers(M: *SrcM,
1570 AsmSymver: [&](StringRef Name, StringRef Alias) {
1571 if (DstM.getNamedValue(Name)) {
1572 SmallString<256> S(".symver ");
1573 S += Name;
1574 S += ", ";
1575 S += Alias;
1576 DstM.appendModuleInlineAsm(Asm: S);
1577 }
1578 });
1579 }
1580
1581 // Reorder the globals just added to the destination module to match their
1582 // original order in the source module.
1583 for (GlobalVariable &GV : SrcM->globals()) {
1584 if (GV.hasAppendingLinkage())
1585 continue;
1586 Value *NewValue = Mapper.mapValue(V: GV);
1587 if (FoundError)
1588 return std::move(*FoundError);
1589 if (NewValue) {
1590 auto *NewGV = dyn_cast<GlobalVariable>(Val: NewValue->stripPointerCasts());
1591 if (NewGV) {
1592 NewGV->removeFromParent();
1593 DstM.insertGlobalVariable(GV: NewGV);
1594 }
1595 }
1596 }
1597
1598 // Merge the module flags into the DstM module.
1599 return linkModuleFlagsMetadata();
1600}
1601
1602IRMover::StructTypeKeyInfo::KeyTy::KeyTy(ArrayRef<Type *> E, bool P)
1603 : ETypes(E), IsPacked(P) {}
1604
1605IRMover::StructTypeKeyInfo::KeyTy::KeyTy(const StructType *ST)
1606 : ETypes(ST->elements()), IsPacked(ST->isPacked()) {}
1607
1608bool IRMover::StructTypeKeyInfo::KeyTy::operator==(const KeyTy &That) const {
1609 return IsPacked == That.IsPacked && ETypes == That.ETypes;
1610}
1611
1612bool IRMover::StructTypeKeyInfo::KeyTy::operator!=(const KeyTy &That) const {
1613 return !this->operator==(That);
1614}
1615
1616unsigned IRMover::StructTypeKeyInfo::getHashValue(const KeyTy &Key) {
1617 return hash_combine(args: hash_combine_range(R: Key.ETypes), args: Key.IsPacked);
1618}
1619
1620unsigned IRMover::StructTypeKeyInfo::getHashValue(const StructType *ST) {
1621 return getHashValue(Key: KeyTy(ST));
1622}
1623
1624bool IRMover::StructTypeKeyInfo::isEqual(const KeyTy &LHS,
1625 const StructType *RHS) {
1626 return LHS == KeyTy(RHS);
1627}
1628
1629bool IRMover::StructTypeKeyInfo::isEqual(const StructType *LHS,
1630 const StructType *RHS) {
1631 return KeyTy(LHS) == KeyTy(RHS);
1632}
1633
1634void IRMover::IdentifiedStructTypeSet::addNonOpaque(StructType *Ty) {
1635 assert(!Ty->isOpaque());
1636 NonOpaqueStructTypes.insert(V: Ty);
1637}
1638
1639void IRMover::IdentifiedStructTypeSet::switchToNonOpaque(StructType *Ty) {
1640 assert(!Ty->isOpaque());
1641 NonOpaqueStructTypes.insert(V: Ty);
1642 bool Removed = OpaqueStructTypes.erase(V: Ty);
1643 (void)Removed;
1644 assert(Removed);
1645}
1646
1647void IRMover::IdentifiedStructTypeSet::addOpaque(StructType *Ty) {
1648 assert(Ty->isOpaque());
1649 OpaqueStructTypes.insert(V: Ty);
1650}
1651
1652StructType *
1653IRMover::IdentifiedStructTypeSet::findNonOpaque(ArrayRef<Type *> ETypes,
1654 bool IsPacked) {
1655 IRMover::StructTypeKeyInfo::KeyTy Key(ETypes, IsPacked);
1656 auto I = NonOpaqueStructTypes.find_as(Val: Key);
1657 return I == NonOpaqueStructTypes.end() ? nullptr : *I;
1658}
1659
1660bool IRMover::IdentifiedStructTypeSet::hasType(StructType *Ty) {
1661 if (Ty->isOpaque())
1662 return OpaqueStructTypes.count(V: Ty);
1663 auto I = NonOpaqueStructTypes.find(V: Ty);
1664 return I == NonOpaqueStructTypes.end() ? false : *I == Ty;
1665}
1666
1667IRMover::IRMover(Module &M) : Composite(M) {
1668 TypeFinder StructTypes;
1669 StructTypes.run(M, /* OnlyNamed */ onlyNamed: false);
1670 for (StructType *Ty : StructTypes) {
1671 if (Ty->isOpaque())
1672 IdentifiedStructTypes.addOpaque(Ty);
1673 else
1674 IdentifiedStructTypes.addNonOpaque(Ty);
1675 }
1676 // Self-map metadatas in the destination module. This is needed when
1677 // DebugTypeODRUniquing is enabled on the LLVMContext, since metadata in the
1678 // destination module may be reached from the source module.
1679 for (const auto *MD : StructTypes.getVisitedMetadata()) {
1680 SharedMDs[MD].reset(MD: const_cast<MDNode *>(MD));
1681 }
1682
1683 // Convert module level attributes to function level attributes because
1684 // after merging modules the attributes might change and would have different
1685 // effect on the functions as the original module would have.
1686 copyModuleAttrToFunctions(M);
1687}
1688
1689Error IRMover::move(std::unique_ptr<Module> Src,
1690 ArrayRef<GlobalValue *> ValuesToLink,
1691 LazyCallback AddLazyFor, bool IsPerformingImport) {
1692 IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes,
1693 std::move(Src), ValuesToLink, std::move(AddLazyFor),
1694 IsPerformingImport, NamedMDNodes);
1695 return TheIRLinker.run();
1696}
1697