llvm.org GIT mirror llvm / 21fca5d
Fix linking when we copy over only a decl. We were failing to copy the fact that the GV is weak and in the case of an alias, producing invalid IR. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@254538 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
3 changed file(s) with 55 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
452452 /// Handles cloning of a global values from the source module into
453453 /// the destination module, including setting the attributes and visibility.
454454 GlobalValue *copyGlobalValueProto(TypeMapTy &TypeMap, const GlobalValue *SGV,
455 const GlobalValue *DGV = nullptr);
455 const GlobalValue *DGV, bool ForDefinition);
456456
457457 /// Check if we should promote the given local value to global scope.
458458 bool doPromoteLocalToGlobal(const GlobalValue *SGV);
593593 void ModuleLinker::copyGVAttributes(GlobalValue *NewGV,
594594 const GlobalValue *SrcGV) {
595595 auto *GA = dyn_cast(SrcGV);
596 // Check for the special case of converting an alias (definition) to a
597 // non-alias (declaration). This can happen when we are importing and
598 // encounter a weak_any alias (weak_any defs may not be imported, see
599 // comments in ModuleLinker::getLinkage) or an alias whose base object is
600 // being imported as a declaration. In that case copy the attributes from the
601 // base object.
602596 if (GA && !dyn_cast(NewGV)) {
603 assert(isPerformingImport() && !doImportAsDefinition(GA));
597 // FIXME: this is likelly bogus:
604598 NewGV->copyAttributesFrom(GA->getBaseObject());
605599 } else
606600 NewGV->copyAttributesFrom(SrcGV);
778772 // No linking to be performed or linking from the source: simply create an
779773 // identical version of the symbol over in the dest module... the
780774 // initializer will be filled in later by LinkGlobalInits.
781 GlobalVariable *NewDGV = new GlobalVariable(
782 DstM, TypeMap.get(SGVar->getType()->getElementType()),
783 SGVar->isConstant(), getLinkage(SGVar), /*init*/ nullptr, getName(SGVar),
784 /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
785 SGVar->getType()->getAddressSpace());
775 GlobalVariable *NewDGV =
776 new GlobalVariable(DstM, TypeMap.get(SGVar->getType()->getElementType()),
777 SGVar->isConstant(), GlobalValue::ExternalLinkage,
778 /*init*/ nullptr, getName(SGVar),
779 /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
780 SGVar->getType()->getAddressSpace());
786781
787782 return NewDGV;
788783 }
793788 const Function *SF) {
794789 // If there is no linkage to be performed or we are linking from the source,
795790 // bring SF over.
796 return Function::Create(TypeMap.get(SF->getFunctionType()), getLinkage(SF),
797 getName(SF), &DstM);
791 return Function::Create(TypeMap.get(SF->getFunctionType()),
792 GlobalValue::ExternalLinkage, getName(SF), &DstM);
798793 }
799794
800795 /// Set up prototypes for any aliases that come over from the source module.
828823 // bring over SGA.
829824 auto *Ty = TypeMap.get(SGA->getValueType());
830825 return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
831 getLinkage(SGA), getName(SGA), &DstM);
826 GlobalValue::ExternalLinkage, getName(SGA), &DstM);
832827 }
833828
834829 static GlobalValue::VisibilityTypes
856851
857852 GlobalValue *ModuleLinker::copyGlobalValueProto(TypeMapTy &TypeMap,
858853 const GlobalValue *SGV,
859 const GlobalValue *DGV) {
854 const GlobalValue *DGV,
855 bool ForDefinition) {
860856 GlobalValue *NewGV;
861 if (auto *SGVar = dyn_cast(SGV))
857 if (auto *SGVar = dyn_cast(SGV)) {
862858 NewGV = copyGlobalVariableProto(TypeMap, SGVar);
863 else if (auto *SF = dyn_cast(SGV))
859 } else if (auto *SF = dyn_cast(SGV)) {
864860 NewGV = copyFunctionProto(TypeMap, SF);
865 else
866 NewGV = copyGlobalAliasProto(TypeMap, cast(SGV));
861 } else {
862 if (ForDefinition)
863 NewGV = copyGlobalAliasProto(TypeMap, cast(SGV));
864 else
865 NewGV = new GlobalVariable(
866 DstM, TypeMap.get(SGV->getType()->getElementType()),
867 /*isConstant*/ false, GlobalValue::ExternalLinkage,
868 /*init*/ nullptr, getName(SGV),
869 /*insertbefore*/ nullptr, SGV->getThreadLocalMode(),
870 SGV->getType()->getAddressSpace());
871 }
872
873 if (ForDefinition)
874 NewGV->setLinkage(getLinkage(SGV));
875 else if (SGV->hasAvailableExternallyLinkage() || SGV->hasWeakLinkage() ||
876 SGV->hasLinkOnceLinkage())
877 NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
878
867879 copyGVAttributes(NewGV, SGV);
868880 setVisibility(NewGV, SGV, DGV);
869881 return NewGV;
14451457 // When linking from source we setVisibility from copyGlobalValueProto.
14461458 setVisibility(NewGV, SGV, DGV);
14471459 } else {
1448 NewGV = copyGlobalValueProto(TypeMap, SGV, DGV);
1460 NewGV = copyGlobalValueProto(TypeMap, SGV, DGV, LinkFromSrc);
14491461
14501462 if (isPerformingImport() && !doImportAsDefinition(SGV))
14511463 DoNotLinkFromSource.insert(SGV);
0 $c = comdat any
1
2 @v2 = weak global i32 0, comdat ($c)
3 define i32* @f2() {
4 ret i32* @v2
5 }
6
7 @v3 = weak alias i32, i32* @v2
8 define i32* @f3() {
9 ret i32* @v3
10 }
11
0 ; RUN: llvm-link -S %s %p/Inputs/comdat14.ll -o - | FileCheck %s
1
2 $c = comdat any
3
4 @v = global i32 0, comdat ($c)
5
6 ; CHECK: @v = global i32 0, comdat($c)
7 ; CHECK: @v2 = extern_weak global i32
8 ; CHECK: @v3 = extern_weak global i32