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