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