Skip to content

Commit

Permalink
In function mover, look up the global/function by name also
Browse files Browse the repository at this point in the history
If there is both a declaration and a definition it can happen that the
function and the reference to it are copied seperately, hence we need
to check whether the name already exists. Otherwise LLVM will
deduplicate the name resulting in linking errors. We don't emit this
code pattern but it can happen when used with llvmcall.
  • Loading branch information
Keno committed Aug 12, 2014
1 parent daa3f82 commit 631b098
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class FunctionMover : public ValueMaterializer
ValueMaterializer(), VMap(), destModule(dest), srcModule(src)
{

}
}
ValueToValueMapTy VMap;
llvm::Module *destModule;
llvm::Module *srcModule;
Expand All @@ -118,12 +118,15 @@ class FunctionMover : public ValueMaterializer
// Check whether we already emitted it once
uint64_t addr = jl_mcjmm->getSymbolAddress(F->getName());
if (addr == 0) {
Function *oldF = destModule->getFunction(F->getName());
if (oldF)
return oldF;
return clone_llvm_function(shadow,this);
}
}
else {
return destModule->getOrInsertFunction(F->getName(),F->getFunctionType());
}
}
}
else if (!F->isDeclaration()) {
return clone_llvm_function(F,this);
}
Expand All @@ -133,10 +136,13 @@ class FunctionMover : public ValueMaterializer
// Create forward declaration in current module
return destModule->getOrInsertFunction(F->getName(),F->getFunctionType());
}
}
}
else if (isa<GlobalVariable>(V)) {
GlobalVariable *GV = cast<GlobalVariable>(V);
assert(GV != NULL);
GlobalVariable *oldGV = destModule->getGlobalVariable(GV->getName());
if (oldGV != NULL)
return oldGV;
GlobalVariable *newGV = new GlobalVariable(*destModule,
GV->getType()->getElementType(),
GV->isConstant(),
Expand All @@ -156,7 +162,7 @@ class FunctionMover : public ValueMaterializer
if (it != llvm_to_jl_value.end()) {
newGV->setInitializer(Constant::getIntegerValue(GV->getType()->getElementType(),APInt(sizeof(void*)*8,(ptrint_t)it->second)));
newGV->setConstant(true);
}
}
else if (GV->hasInitializer()) {
Value *C = MapValue(GV->getInitializer(),VMap,RF_None,NULL,this);
newGV->setInitializer(cast<Constant>(C));
Expand Down

0 comments on commit 631b098

Please sign in to comment.