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
This commit is contained in:
Chris Lattner 2008-07-14 07:23:24 +00:00
parent ae1132d2b8
commit 6157e383c4
5 changed files with 108 additions and 171 deletions

View File

@ -37,14 +37,6 @@ static inline bool Error(std::string *E, const std::string &Message) {
return true;
}
// ToStr - Simple wrapper function to convert a type to a string.
static std::string ToStr(const Type *Ty, const Module *M) {
std::ostringstream OS;
WriteTypeSymbolic(OS, Ty, M);
return OS.str();
}
//
// Function: ResolveTypes()
//
// Description:
@ -566,7 +558,7 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
if (GetLinkageResult(DGV, SGV, NewLinkage, LinkFromSrc, Err))
return true;
if (!DGV) {
if (DGV == 0) {
// No linking to be performed, simply create an identical version of the
// symbol over in the dest module... the initializer will be filled in
// later by LinkGlobalInits.
@ -581,16 +573,24 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
// If the LLVM runtime renamed the global, but it is an externally visible
// symbol, DGV must be an existing global with internal linkage. Rename
// it.
if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage())
if (!NewDGV->hasInternalLinkage() && NewDGV->getName() != SGV->getName())
ForceRenaming(NewDGV, SGV->getName());
// Make sure to remember this mapping...
// Make sure to remember this mapping.
ValueMap[SGV] = NewDGV;
// Keep track that this is an appending variable.
if (SGV->hasAppendingLinkage())
AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
} else if (DGV->hasAppendingLinkage()) {
continue;
}
// If the visibilities of the symbols disagree and the destination is a
// prototype, take the visibility of its input.
if (DGV->isDeclaration())
DGV->setVisibility(SGV->getVisibility());
if (DGV->hasAppendingLinkage()) {
// No linking is performed yet. Just insert a new copy of the global, and
// keep track of the fact that it is an appending variable in the
// AppendingVars map. The name is cleared out so that no linkage is
@ -611,25 +611,21 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
// Keep track that this is an appending variable...
AppendingVars.insert(std::make_pair(SGV->getName(), NewDGV));
} else if (GlobalAlias *DGA = dyn_cast<GlobalAlias>(DGV)) {
// SGV is global, but DGV is alias. The only valid mapping is when SGV is
// external declaration, which is effectively a no-op. Also make sure
// linkage calculation was correct.
if (!SGV->isDeclaration() || LinkFromSrc)
continue;
}
if (LinkFromSrc) {
if (isa<GlobalAlias>(DGV))
return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
"': symbol multiple defined");
// Make sure to remember this mapping.
ValueMap[SGV] = DGA;
} else if (LinkFromSrc) {
// If the types don't match, and if we are to link from the source, nuke
// DGV and create a new one of the appropriate type. Note that the thing
// we are replacing may be a function (if a prototype, weak, etc) or a
// global variable.
GlobalVariable *NewDGV =
new GlobalVariable(SGV->getType()->getElementType(),
SGV->isConstant(), SGV->getLinkage(),
/*init*/0, DGV->getName(), Dest, false,
new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(),
NewLinkage, /*init*/0, DGV->getName(), Dest, false,
SGV->getType()->getAddressSpace());
// Propagate alignment, section, and visibility info.
@ -647,33 +643,37 @@ static bool LinkGlobals(Module *Dest, const Module *Src,
// If the symbol table renamed the global, but it is an externally visible
// symbol, DGV must be an existing global with internal linkage. Rename.
if (NewDGV->getValueName() != SGV->getValueName() &&
!NewDGV->hasInternalLinkage())
if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage())
ForceRenaming(NewDGV, SGV->getName());
// Inherit const as appropriate
// Inherit const as appropriate.
NewDGV->setConstant(SGV->isConstant());
// Set calculated linkage
NewDGV->setLinkage(NewLinkage);
// Make sure to remember this mapping...
ValueMap[SGV] = ConstantExpr::getBitCast(NewDGV, SGV->getType());
} else {
// Not "link from source", keep the one in the DestModule and remap the
// input onto it.
// Special case for const propagation.
if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV))
if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant())
DGVar->setConstant(true);
// Set calculated linkage
DGV->setLinkage(NewLinkage);
// Make sure to remember this mapping...
ValueMap[SGV] = ConstantExpr::getBitCast(DGV, SGV->getType());
// Make sure to remember this mapping.
ValueMap[SGV] = NewDGV;
continue;
}
// Not "link from source", keep the one in the DestModule and remap the
// input onto it.
// Special case for const propagation.
if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV))
if (DGVar->isDeclaration() && SGV->isConstant() && !DGVar->isConstant())
DGVar->setConstant(true);
// SGV is global, but DGV is alias. The only valid mapping is when SGV is
// external declaration, which is effectively a no-op. Also make sure
// linkage calculation was correct.
if (isa<GlobalAlias>(DGV) && !SGV->isDeclaration())
return Error(Err, "Global-Alias Collision on '" + SGV->getName() +
"': symbol multiple defined");
// Set calculated linkage
DGV->setLinkage(NewLinkage);
// Make sure to remember this mapping...
ValueMap[SGV] = ConstantExpr::getBitCast(DGV, SGV->getType());
}
return false;
}
@ -895,7 +895,6 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
for (Module::const_iterator I = Src->begin(), E = Src->end(); I != E; ++I) {
const Function *SF = I; // SrcFunction
GlobalValue *DGV = 0;
Value *MappedDF;
// Check to see if may have to link the function with the global, alias or
// function.
@ -912,6 +911,11 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
if (DGV && DGV->getType() != SF->getType())
RecursiveResolveTypes(SF->getType(), DGV->getType());
GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
bool LinkFromSrc = false;
if (GetLinkageResult(DGV, SF, NewLinkage, LinkFromSrc, Err))
return true;
// If there is no linkage to be performed, just bring over SF without
// modifying it.
if (DGV == 0) {
@ -931,134 +935,65 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src,
// ... and remember this mapping...
ValueMap[SF] = NewDF;
continue;
} else if (GlobalAlias *DGA = dyn_cast<GlobalAlias>(DGV)) {
// SF is function, but DF is alias.
}
// If the visibilities of the symbols disagree and the destination is a
// prototype, take the visibility of its input.
if (DGV->isDeclaration())
DGV->setVisibility(SF->getVisibility());
if (LinkFromSrc) {
if (isa<GlobalAlias>(DGV))
return Error(Err, "Function-Alias Collision on '" + SF->getName() +
"': symbol multiple defined");
// We have a definition of the same name but different type in the
// source module. Copy the prototype to the destination and replace
// uses of the destination's prototype with the new prototype.
Function *NewDF = Function::Create(SF->getFunctionType(), NewLinkage,
SF->getName(), Dest);
CopyGVAttributes(NewDF, SF);
// Any uses of DF need to change to NewDF, with cast
DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType()));
// DF will conflict with NewDF because they both had the same. We must
// erase this now so ForceRenaming doesn't assert because DF might
// not have internal linkage.
if (GlobalVariable *Var = dyn_cast<GlobalVariable>(DGV))
Var->eraseFromParent();
else
cast<Function>(DGV)->eraseFromParent();
// If the symbol table renamed the function, but it is an externally
// visible symbol, DF must be an existing function with internal
// linkage. Rename it.
if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage())
ForceRenaming(NewDF, SF->getName());
// Remember this mapping so uses in the source module get remapped
// later by RemapOperand.
ValueMap[SF] = NewDF;
continue;
}
// Not "link from source", keep the one in the DestModule and remap the
// input onto it.
if (isa<GlobalAlias>(DGV)) {
// The only valid mappings are:
// - SF is external declaration, which is effectively a no-op.
// - SF is weak, when we just need to throw SF out.
if (!SF->isDeclaration() && !SF->isWeakForLinker())
if (!SF->isDeclaration())
return Error(Err, "Function-Alias Collision on '" + SF->getName() +
"': symbol multiple defined");
// Make sure to remember this mapping...
ValueMap[SF] = ConstantExpr::getBitCast(DGA, SF->getType());
continue;
}
Function* DF = cast<Function>(DGV);
// If types don't agree because of opaque, try to resolve them.
if (SF->getType() != DF->getType())
RecursiveResolveTypes(SF->getType(), DF->getType());
// Check visibility, merging if a definition overrides a prototype.
if (SF->getVisibility() != DF->getVisibility()) {
// If one is a prototype, ignore its visibility. Prototypes are always
// overridden by the definition.
if (!SF->isDeclaration() && !DF->isDeclaration())
return Error(Err, "Linking functions named '" + SF->getName() +
"': symbols have different visibilities!");
// Otherwise, replace the visibility of DF if DF is a prototype.
if (DF->isDeclaration())
DF->setVisibility(SF->getVisibility());
}
if (DF->getType() != SF->getType()) {
if (DF->isDeclaration() && !SF->isDeclaration()) {
// We have a definition of the same name but different type in the
// source module. Copy the prototype to the destination and replace
// uses of the destination's prototype with the new prototype.
Function *NewDF = Function::Create(SF->getFunctionType(),
SF->getLinkage(),
SF->getName(), Dest);
CopyGVAttributes(NewDF, SF);
// Set calculated linkage
DGV->setLinkage(NewLinkage);
// Any uses of DF need to change to NewDF, with cast
DF->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DF->getType()));
// DF will conflict with NewDF because they both had the same. We must
// erase this now so ForceRenaming doesn't assert because DF might
// not have internal linkage.
DF->eraseFromParent();
// If the symbol table renamed the function, but it is an externally
// visible symbol, DF must be an existing function with internal
// linkage. Rename it.
if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage())
ForceRenaming(NewDF, SF->getName());
// Remember this mapping so uses in the source module get remapped
// later by RemapOperand.
ValueMap[SF] = NewDF;
continue;
} else {
// We have two functions of the same name but different type. Any use
// of the source must be mapped to the destination, with a cast.
MappedDF = ConstantExpr::getBitCast(DF, SF->getType());
}
} else {
MappedDF = DF;
}
if (SF->isDeclaration()) {
// If SF is a declaration or if both SF & DF are declarations, just link
// the declarations, we aren't adding anything.
if (SF->hasDLLImportLinkage()) {
if (DF->isDeclaration()) {
ValueMap[SF] = MappedDF;
DF->setLinkage(SF->getLinkage());
}
} else {
ValueMap[SF] = MappedDF;
}
continue;
}
// If DF is external but SF is not, link the external functions, update
// linkage qualifiers.
if (DF->isDeclaration() && !DF->hasDLLImportLinkage()) {
ValueMap.insert(std::make_pair(SF, MappedDF));
DF->setLinkage(SF->getLinkage());
continue;
}
// At this point we know that DF has LinkOnce, Weak, or External* linkage.
if (SF->isWeakForLinker()) {
ValueMap[SF] = MappedDF;
// Linkonce+Weak = Weak
// *+External Weak = *
if ((DF->hasLinkOnceLinkage() &&
(SF->hasWeakLinkage() || SF->hasCommonLinkage())) ||
DF->hasExternalWeakLinkage())
DF->setLinkage(SF->getLinkage());
continue;
}
if (DF->isWeakForLinker()) {
// At this point we know that SF has LinkOnce or External* linkage.
ValueMap[SF] = MappedDF;
// If the source function has stronger linkage than the destination,
// its body and linkage should override ours.
if (!SF->hasLinkOnceLinkage() && !SF->hasExternalWeakLinkage()) {
// Don't inherit linkonce & external weak linkage.
DF->setLinkage(SF->getLinkage());
DF->deleteBody();
}
continue;
}
if (SF->getLinkage() != DF->getLinkage())
return Error(Err, "Functions named '" + SF->getName() +
"' have different linkage specifiers!");
// The function is defined identically in both modules!
if (SF->hasExternalLinkage())
return Error(Err, "Function '" +
ToStr(SF->getFunctionType(), Src) + "':\"" +
SF->getName() + "\" - Function is already defined!");
assert(0 && "Unknown linkage configuration found!");
// Make sure to remember this mapping.
ValueMap[SF] = ConstantExpr::getBitCast(DGV, SF->getType());
}
return false;
}

View File

@ -4,6 +4,8 @@
; RUN: llvm-link %t1.bc %t2.bc -f -o %t3.bc
; RUN: llvm-link %t2.bc %t1.bc -f -o %t4.bc
; XFAIL: *
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"
target triple = "i386-pc-linux-gnu"

View File

@ -1,7 +1,7 @@
; RUN: llvm-as %s -o %t1.bc -f
; 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
; RUN: llvm-link %t2.bc %t1.bc -o - | llvm-dis | grep __eprintf
; RN: llvm-link %t1.bc %t2.bc -o - | llvm-dis | grep __eprintf
; RUN: llvm-link %t1.bc %t2.bc -o - | llvm-dis | grep __eprintf
; rdar://6072702

View File

@ -4,7 +4,7 @@
; RUN: llvm-as %s -o %t.two.bc -f
; RUN: not llvm-ld -disable-opt -link-as-library %t.one.bc %t.two.bc \
; RUN: -o %t.bc 2>%t.err
; RUN: grep "Function is already defined" %t.err
; RUN: grep "symbol multiply defined" %t.err
define i32 @bar() {
ret i32 0

View File

@ -4,7 +4,7 @@
; RUN: llvm-as %s -o %t.foo2.bc -f
; RUN: echo {define void @foo(i32 %x) { ret void }} | llvm-as -o %t.foo3.bc -f
; RUN: not llvm-link %t.foo1.bc %t.foo2.bc -o %t.bc |& \
; RUN: grep {Function is already defined}
; RUN: grep {symbol multiply defined}
; RUN: not llvm-link %t.foo1.bc %t.foo3.bc -o %t.bc |& \
; RUN: grep {Function is already defined}
; RUN: grep {symbol multiply defined}
define void @foo() { ret void }