Skip to content

Commit

Permalink
codegen: cfunction for inferred union return (JuliaLang#35118)
Browse files Browse the repository at this point in the history
Regression caused by slight changing to union-return calling convention
to handle pointer gc-tagging.

Fixes JuliaLang#26078
  • Loading branch information
vtjnash committed Mar 17, 2020
1 parent 60ffa4a commit 5da67bd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4320,6 +4320,7 @@ static Function* gen_cfun_wrapper(
FunctionType *cft = returninfo.decl->getFunctionType();
jlfunc_sret = (returninfo.cc == jl_returninfo_t::SRet);

// TODO: Can use use emit_call_specfun_other here?
std::vector<Value*> args;
Value *result;
if (jlfunc_sret || returninfo.cc == jl_returninfo_t::Union) {
Expand Down Expand Up @@ -4391,14 +4392,22 @@ static Function* gen_cfun_wrapper(
case jl_returninfo_t::SRet:
retval = mark_julia_slot(result, astrt, NULL, tbaa_stack);
break;
case jl_returninfo_t::Union:
retval = mark_julia_slot(ctx.builder.CreateExtractValue(call, 0),
case jl_returninfo_t::Union: {
Value *box = ctx.builder.CreateExtractValue(call, 0);
Value *tindex = ctx.builder.CreateExtractValue(call, 1);
Value *derived = ctx.builder.CreateSelect(
ctx.builder.CreateICmpEQ(
ctx.builder.CreateAnd(tindex, ConstantInt::get(T_int8, 0x80)),
ConstantInt::get(T_int8, 0)),
decay_derived(ctx.builder.CreateBitCast(result, T_pjlvalue)),
decay_derived(box));
retval = mark_julia_slot(derived,
astrt,
ctx.builder.CreateExtractValue(call, 1),
tindex,
tbaa_stack);
// note that the value may not be rooted here (on the return path)
// XXX: should have a root if we need to emit a typeassert abort
retval.Vboxed = box;
break;
}
case jl_returninfo_t::Ghosts:
retval = mark_julia_slot(NULL, astrt, call, tbaa_stack);
break;
Expand Down
6 changes: 6 additions & 0 deletions test/ccall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,12 @@ foo13031(x,y,z) = z
foo13031p = @cfunction(foo13031, Cint, (Ref{Tuple{}}, Ref{Tuple{}}, Cint))
ccall(foo13031p, Cint, (Ref{Tuple{}},Ref{Tuple{}},Cint), (), (), 8)

# issue 26078

unstable26078(x) = x > 0 ? x : "foo"
handle26078 = @cfunction(unstable26078, Int32, (Int32,))
@test ccall(handle26078, Int32, (Int32,), 1) == 1

# issue 17219
function ccall_reassigned_ptr(ptr::Ptr{Cvoid})
ptr = Libdl.dlsym(Libdl.dlopen(libccalltest), "test_echo_p")
Expand Down

0 comments on commit 5da67bd

Please sign in to comment.