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