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