Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy global variable linkages when moving functions #31272

Merged
merged 2 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ class FunctionMover final : public ValueMaterializer
GlobalVariable *newGV = new GlobalVariable(*destModule,
GV->getType()->getElementType(),
GV->isConstant(),
GlobalVariable::ExternalLinkage,
GV->getLinkage(),
NULL,
GV->getName(),
NULL,
Expand Down
24 changes: 24 additions & 0 deletions src/llvmcalltest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,28 @@ JL_DLLEXPORT llvm::Function *MakeIdentityFunction(llvm::PointerType *AnyTy) {
return F;
}

JL_DLLEXPORT llvm::Function *MakeLoadGlobalFunction(llvm::PointerType *AnyTy) {
auto M = new Module("shadow", AnyTy->getContext());
auto intType = Type::getInt32Ty(AnyTy->getContext());
auto G = new GlobalVariable(
*M,
intType,
true,
GlobalValue::InternalLinkage,
Constant::getNullValue(intType),
"test_global_var");

auto resultType = Type::getInt64Ty(AnyTy->getContext());
auto F = Function::Create(
FunctionType::get(resultType, {}, false),
GlobalValue::ExternalLinkage,
"load_global_var",
M);

IRBuilder<> Builder(BasicBlock::Create(AnyTy->getContext(), "top", F));
Builder.CreateRet(Builder.CreatePtrToInt(G, resultType));

return F;
}

}
12 changes: 12 additions & 0 deletions test/llvmcall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ module LLVMCallFunctionTest
let x = boxed_struct()
@test really_complicated_identity(x) === x
end

# Define two functions that each compute the address of a dedicated internal global variable.
# The names of these globals are the same, so if their linkages are overwritten, then the
# linker will merge the globals. Consequently, we can test that linkage is preserved by testing
# that the addresses of the globals differ. The next few lines of code do just that.
const the_other_f1 = ccall((:MakeLoadGlobalFunction, libllvmcalltest), Ptr{Cvoid}, (Ptr{Cvoid},), AnyTy)
const the_other_f2 = ccall((:MakeLoadGlobalFunction, libllvmcalltest), Ptr{Cvoid}, (Ptr{Cvoid},), AnyTy)

@eval global_value_address1() = llvmcall($(the_other_f1), Int64, Tuple{})
@eval global_value_address2() = llvmcall($(the_other_f2), Int64, Tuple{})

@test global_value_address1() != global_value_address2()
end

# support for calling external functions
Expand Down