llvm.org GIT mirror llvm / 6157e38
Reimplement LinkFunctionProtos in terms of GetLinkageResult. This fixes the second half of link-global-to-func.ll and causes some minor changes in messages. There are two TODOs here. First, this causes a regression in 2008-07-06-AliasWeakDest.ll, which is now failing (so I xfailed it). Anton, I would really appreciate it if you could take a look at this. It should be a matter of adding proper alias support to GetLinkageResult, and was probably already a latent bug that would manifest with globals. The second todo is to reimplement LinkAlias in the same pattern as function and global linking. This should be pretty straight-forward for someone who knows aliases, but isn't a requirement for correctness. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53548 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
5 changed file(s) with 112 addition(s) and 175 deletion(s). Raw diff Collapse all Expand all
3636 return true;
3737 }
3838
39 // ToStr - Simple wrapper function to convert a type to a string.
40 static std::string ToStr(const Type *Ty, const Module *M) {
41 std::ostringstream OS;
42 WriteTypeSymbolic(OS, Ty, M);
43 return OS.str();
44 }
45
46 //
4739 // Function: ResolveTypes()
4840 //
4941 // Description:
565557 if (GetLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc, Err))
566558 return true;
567559
568 if (!DGV) {
560 if (DGV == 0) {
569561 // No linking to be performed, simply create an identical version of the
570562 // symbol over in the dest module... the initializer will be filled in
571563 // later by LinkGlobalInits.
580572 // If the LLVM runtime renamed the global, but it is an externally visible
581573 // symbol, DGV must be an existing global with internal linkage. Rename
582574 // it.
583 if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage())
575 if (!NewDGV->hasInternalLinkage() && NewDGV->getName() != SGV->getName())
584576 ForceRenaming(NewDGV, SGV->getName());
585577
586 // Make sure to remember this mapping...
578 // Make sure to remember this mapping.
587579 ValueMap[SGV] = NewDGV;
588580
589581 // Keep track that this is an appending variable.
590582 if (SGV->hasAppendingLinkage())
591583 AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
592 } else if (DGV->hasAppendingLinkage()) {
584 continue;
585 }
586
587 // If the visibilities of the symbols disagree and the destination is a
588 // prototype, take the visibility of its input.
589 if (DGV->isDeclaration())
590 DGV->setVisibility(SGV->getVisibility());
591
592 if (DGV->hasAppendingLinkage()) {
593593 // No linking is performed yet. Just insert a new copy of the global, and
594594 // keep track of the fact that it is an appending variable in the
595595 // AppendingVars map. The name is cleared out so that no linkage is
610610
611611 // Keep track that this is an appending variable...
612612 AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
613 } else if (GlobalAlias *DGA = dyn_cast(DGV)) {
614 // SGV is global, but DGV is alias. The only valid mapping is when SGV is
615 // external declaration, which is effectively a no-op. Also make sure
616 // linkage calculation was correct.
617 if (!SGV->isDeclaration() || LinkFromSrc)
613 continue;
614 }
615
616 if (LinkFromSrc) {
617 if (isa(DGV))
618618 return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
619619 "': symbol multiple defined");
620
621 // Make sure to remember this mapping.
622 ValueMap[SGV] = DGA;
623 } else if (LinkFromSrc) {
620
624621 // If the types don't match, and if we are to link from the source, nuke
625622 // DGV and create a new one of the appropriate type. Note that the thing
626623 // we are replacing may be a function (if a prototype, weak, etc) or a
627624 // global variable.
628625 GlobalVariable *NewDGV =
629 new GlobalVariable(SGV->getType()->getElementType(),
630 SGV->isConstant(), SGV->getLinkage(),
631 /*init*/0, DGV->getName(), Dest, false,
626 new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
627 NewLinkage, /*init*/0, DGV->getName(), Dest, false,
632628 SGV->getType()->getAddressSpace());
633629
634630 // Propagate alignment, section, and visibility info.
646642
647643 // If the symbol table renamed the global, but it is an externally visible
648644 // symbol, DGV must be an existing global with internal linkage. Rename.
649 if (NewDGV->getValueName() != SGV->getValueName() &&
650 !NewDGV->hasInternalLinkage())
645 if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage())
651646 ForceRenaming(NewDGV, SGV->getName());
652647
653 // Inherit const as appropriate
648 // Inherit const as appropriate.
654649 NewDGV->setConstant(SGV->isConstant());
655650
656 // Set calculated linkage
657 NewDGV->setLinkage(NewLinkage);
658
659 // Make sure to remember this mapping...
660 ValueMap[SGV] = ConstantExpr::getBitCast(NewDGV, SGV->getType());
661 } else {
662 // Not "link from source", keep the one in the DestModule and remap the
663 // input onto it.
664
665 // Special case for const propagation.
666 if (GlobalVariable *DGVar = dyn_cast(DGV))
667 if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant())
668 DGVar->setConstant(true);
669
670 // Set calculated linkage
671 DGV->setLinkage(NewLinkage);
672
673 // Make sure to remember this mapping...
674 ValueMap[SGV] = ConstantExpr::getBitCast(DGV, SGV->getType());
675 }
651 // Make sure to remember this mapping.
652 ValueMap[SGV] = NewDGV;
653 continue;
654 }
655
656 // Not "link from source", keep the one in the DestModule and remap the
657 // input onto it.
658
659 // Special case for const propagation.
660 if (GlobalVariable *DGVar = dyn_cast(DGV))
661 if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant())
662 DGVar->setConstant(true);
663
664 // SGV is global, but DGV is alias. The only valid mapping is when SGV is
665 // external declaration, which is effectively a no-op. Also make sure
666 // linkage calculation was correct.
667 if (isa(DGV) && !SGV->isDeclaration())
668 return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
669 "': symbol multiple defined");
670
671 // Set calculated linkage
672 DGV->setLinkage(NewLinkage);
673
674 // Make sure to remember this mapping...
675 ValueMap[SGV] = ConstantExpr::getBitCast(DGV, SGV->getType());
676676 }
677677 return false;
678678 }
894894 for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
895895 const Function *SF = I; // SrcFunction
896896 GlobalValue *DGV = 0;
897 Value *MappedDF;
898897
899898 // Check to see if may have to link the function with the global, alias or
900899 // function.
911910 if (DGV && DGV->getType() != SF->getType())
912911 RecursiveResolveTypes(SF->getType(), DGV->getType());
913912
913 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
914 bool LinkFromSrc = false;
915 if (GetLinkageResult(DGV, SF, NewLinkage, LinkFromSrc, Err))
916 return true;
917
914918 // If there is no linkage to be performed, just bring over SF without
915919 // modifying it.
916920 if (DGV == 0) {
930934 // ... and remember this mapping...
931935 ValueMap[SF] = NewDF;
932936 continue;
933 } else if (GlobalAlias *DGA = dyn_cast(DGV)) {
934 // SF is function, but DF is alias.
937 }
938
939 // If the visibilities of the symbols disagree and the destination is a
940 // prototype, take the visibility of its input.
941 if (DGV->isDeclaration())
942 DGV->setVisibility(SF->getVisibility());
943
944 if (LinkFromSrc) {
945 if (isa(DGV))
946 return Error(Err, "Function-Alias Collision on '" + SF->getName() +
947 "': symbol multiple defined");
948
949 // We have a definition of the same name but different type in the
950 // source module. Copy the prototype to the destination and replace
951 // uses of the destination's prototype with the new prototype.
952 Function *NewDF = Function::Create(SF->getFunctionType(), NewLinkage,
953 SF->getName(), Dest);
954 CopyGVAttributes(NewDF, SF);
955
956 // Any uses of DF need to change to NewDF, with cast
957 DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType()));
958
959 // DF will conflict with NewDF because they both had the same. We must
960 // erase this now so ForceRenaming doesn't assert because DF might
961 // not have internal linkage.
962 if (GlobalVariable *Var = dyn_cast(DGV))
963 Var->eraseFromParent();
964 else
965 cast(DGV)->eraseFromParent();
966
967 // If the symbol table renamed the function, but it is an externally
968 // visible symbol, DF must be an existing function with internal
969 // linkage. Rename it.
970 if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage())
971 ForceRenaming(NewDF, SF->getName());
972
973 // Remember this mapping so uses in the source module get remapped
974 // later by RemapOperand.
975 ValueMap[SF] = NewDF;
976 continue;
977 }
978
979 // Not "link from source", keep the one in the DestModule and remap the
980 // input onto it.
981
982 if (isa(DGV)) {
935983 // The only valid mappings are:
936984 // - SF is external declaration, which is effectively a no-op.
937985 // - SF is weak, when we just need to throw SF out.
938 if (!SF->isDeclaration() && !SF->isWeakForLinker())
986 if (!SF->isDeclaration())
939987 return Error(Err, "Function-Alias Collision on '" + SF->getName() +
940988 "': symbol multiple defined");
941
942 // Make sure to remember this mapping...
943 ValueMap[SF] = ConstantExpr::getBitCast(DGA, SF->getType());
944 continue;
945 }
946
947 Function* DF = cast(DGV);
948 // If types don't agree because of opaque, try to resolve them.
949 if (SF->getType() != DF->getType())
950 RecursiveResolveTypes(SF->getType(), DF->getType());
951
952 // Check visibility, merging if a definition overrides a prototype.
953 if (SF->getVisibility() != DF->getVisibility()) {
954 // If one is a prototype, ignore its visibility. Prototypes are always
955 // overridden by the definition.
956 if (!SF->isDeclaration() && !DF->isDeclaration())
957 return Error(Err, "Linking functions named '" + SF->getName() +
958 "': symbols have different visibilities!");
959
960 // Otherwise, replace the visibility of DF if DF is a prototype.
961 if (DF->isDeclaration())
962 DF->setVisibility(SF->getVisibility());
963 }
964
965 if (DF->getType() != SF->getType()) {
966 if (DF->isDeclaration() && !SF->isDeclaration()) {
967 // We have a definition of the same name but different type in the
968 // source module. Copy the prototype to the destination and replace
969 // uses of the destination's prototype with the new prototype.
970 Function *NewDF = Function::Create(SF->getFunctionType(),
971 SF->getLinkage(),
972 SF->getName(), Dest);
973 CopyGVAttributes(NewDF, SF);
974
975 // Any uses of DF need to change to NewDF, with cast
976 DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DF->getType()));
977
978 // DF will conflict with NewDF because they both had the same. We must
979 // erase this now so ForceRenaming doesn't assert because DF might
980 // not have internal linkage.
981 DF->eraseFromParent();
982
983 // If the symbol table renamed the function, but it is an externally
984 // visible symbol, DF must be an existing function with internal
985 // linkage. Rename it.
986 if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage())
987 ForceRenaming(NewDF, SF->getName());
988
989 // Remember this mapping so uses in the source module get remapped
990 // later by RemapOperand.
991 ValueMap[SF] = NewDF;
992 continue;
993 } else {
994 // We have two functions of the same name but different type. Any use
995 // of the source must be mapped to the destination, with a cast.
996 MappedDF = ConstantExpr::getBitCast(DF, SF->getType());
997 }
998 } else {
999 MappedDF = DF;
1000 }
1001
1002 if (SF->isDeclaration()) {
1003 // If SF is a declaration or if both SF & DF are declarations, just link
1004 // the declarations, we aren't adding anything.
1005 if (SF->hasDLLImportLinkage()) {
1006 if (DF->isDeclaration()) {
1007 ValueMap[SF] = MappedDF;
1008 DF->setLinkage(SF->getLinkage());
1009 }
1010 } else {
1011 ValueMap[SF] = MappedDF;
1012 }
1013 continue;
1014 }
1015
1016 // If DF is external but SF is not, link the external functions, update
1017 // linkage qualifiers.
1018 if (DF->isDeclaration() && !DF->hasDLLImportLinkage()) {
1019 ValueMap.insert(std::make_pair(SF, MappedDF));
1020 DF->setLinkage(SF->getLinkage());
1021 continue;
1022 }
1023
1024 // At this point we know that DF has LinkOnce, Weak, or External* linkage.
1025 if (SF->isWeakForLinker()) {
1026 ValueMap[SF] = MappedDF;
1027
1028 // Linkonce+Weak = Weak
1029 // *+External Weak = *
1030 if ((DF->hasLinkOnceLinkage() &&
1031 (SF->hasWeakLinkage() || SF->hasCommonLinkage())) ||
1032 DF->hasExternalWeakLinkage())
1033 DF->setLinkage(SF->getLinkage());
1034 continue;
1035 }
1036
1037 if (DF->isWeakForLinker()) {
1038 // At this point we know that SF has LinkOnce or External* linkage.
1039 ValueMap[SF] = MappedDF;
1040
1041 // If the source function has stronger linkage than the destination,
1042 // its body and linkage should override ours.
1043 if (!SF->hasLinkOnceLinkage() && !SF->hasExternalWeakLinkage()) {
1044 // Don't inherit linkonce & external weak linkage.
1045 DF->setLinkage(SF->getLinkage());
1046 DF->deleteBody();
1047 }
1048 continue;
1049 }
1050
1051 if (SF->getLinkage() != DF->getLinkage())
1052 return Error(Err, "Functions named '" + SF->getName() +
1053 "' have different linkage specifiers!");
1054
1055 // The function is defined identically in both modules!
1056 if (SF->hasExternalLinkage())
1057 return Error(Err, "Function '" +
1058 ToStr(SF->getFunctionType(), Src) + "':\"" +
1059 SF->getName() + "\" - Function is already defined!");
1060 assert(0 && "Unknown linkage configuration found!");
989 }
990
991 // Set calculated linkage
992 DGV->setLinkage(NewLinkage);
993
994 // Make sure to remember this mapping.
995 ValueMap[SF] = ConstantExpr::getBitCast(DGV, SF->getType());
1061996 }
1062997 return false;
1063998 }
22 ; RUN: llvm-as %p/2008-07-06-AliasWeakDest2.ll -o %t2.bc -f
33 ; RUN: llvm-link %t1.bc %t2.bc -f -o %t3.bc
44 ; RUN: llvm-link %t2.bc %t1.bc -f -o %t4.bc
5
6 ; XFAIL: *
57
68 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"
79 target triple = "i386-pc-linux-gnu"
0 ; RUN: llvm-as %s -o %t1.bc -f
11 ; RUN: echo {declare void @__eprintf(i8*, i8*, i32, i8*) noreturn define void @foo() { tail call void @__eprintf( i8* undef, i8* undef, i32 4, i8* null ) noreturn nounwind unreachable }} | llvm-as -o %t2.bc -f
22 ; RUN: llvm-link %t2.bc %t1.bc -o - | llvm-dis | grep __eprintf
3 ; RN: llvm-link %t1.bc %t2.bc -o - | llvm-dis | grep __eprintf
3 ; RUN: llvm-link %t1.bc %t2.bc -o - | llvm-dis | grep __eprintf
44
55 ; rdar://6072702
66
33 ; RUN: llvm-as %s -o %t.two.bc -f
44 ; RUN: not llvm-ld -disable-opt -link-as-library %t.one.bc %t.two.bc \
55 ; RUN: -o %t.bc 2>%t.err
6 ; RUN: grep "Function is already defined" %t.err
6 ; RUN: grep "symbol multiply defined" %t.err
77
88 define i32 @bar() {
99 ret i32 0
33 ; RUN: llvm-as %s -o %t.foo2.bc -f
44 ; RUN: echo {define void @foo(i32 %x) { ret void }} | llvm-as -o %t.foo3.bc -f
55 ; RUN: not llvm-link %t.foo1.bc %t.foo2.bc -o %t.bc |& \
6 ; RUN: grep {Function is already defined}
6 ; RUN: grep {symbol multiply defined}
77 ; RUN: not llvm-link %t.foo1.bc %t.foo3.bc -o %t.bc |& \
8 ; RUN: grep {Function is already defined}
8 ; RUN: grep {symbol multiply defined}
99 define void @foo() { ret void }