Skip to content

Commit

Permalink
cglobal: Fall back to runtime intrinsic (JuliaLang#54914)
Browse files Browse the repository at this point in the history
Our codegen for `cglobal` was sharing the `static_eval` code for symbols
with ccall. However, we do have full runtime emulation for this
intrinsic, so mandating that the symbol can be statically evaluated is
not required and causes semantic differences between the interpreter and
codegen, which is undesirable. Just fall back to the runtime intrinsic
instead.
  • Loading branch information
Keno authored Jun 28, 2024
1 parent 89e391b commit 8791d54
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ static void interpret_symbol_arg(jl_codectx_t &ctx, native_sym_arg_t &out, jl_va
jl_cgval_t arg1 = emit_expr(ctx, arg);
jl_value_t *ptr_ty = arg1.typ;
if (!jl_is_cpointer_type(ptr_ty)) {
if (!ccall)
return;
const char *errmsg = invalid_symbol_err_msg(ccall);
emit_cpointercheck(ctx, arg1, errmsg);
}
Expand Down Expand Up @@ -703,14 +705,6 @@ static jl_cgval_t emit_cglobal(jl_codectx_t &ctx, jl_value_t **args, size_t narg

interpret_symbol_arg(ctx, sym, args[1], /*ccall=*/false, false);

if (sym.f_name == NULL && sym.fptr == NULL && sym.jl_ptr == NULL && sym.gcroot != NULL) {
const char *errmsg = invalid_symbol_err_msg(/*ccall=*/false);
jl_cgval_t arg1 = emit_expr(ctx, args[1]);
emit_type_error(ctx, arg1, literal_pointer_val(ctx, (jl_value_t *)jl_pointer_type), errmsg);
JL_GC_POP();
return jl_cgval_t();
}

if (sym.jl_ptr != NULL) {
res = sym.jl_ptr;
}
Expand All @@ -719,13 +713,21 @@ static jl_cgval_t emit_cglobal(jl_codectx_t &ctx, jl_value_t **args, size_t narg
if (ctx.emission_context.imaging_mode)
jl_printf(JL_STDERR,"WARNING: literal address used in cglobal for %s; code cannot be statically compiled\n", sym.f_name);
}
else {
else if (sym.f_name != NULL) {
if (sym.lib_expr) {
res = runtime_sym_lookup(ctx, getPointerTy(ctx.builder.getContext()), NULL, sym.lib_expr, sym.f_name, ctx.f);
}
else /*if (ctx.emission_context.imaging) */{
res = runtime_sym_lookup(ctx, getPointerTy(ctx.builder.getContext()), sym.f_lib, NULL, sym.f_name, ctx.f);
}
} else {
// Fall back to runtime intrinsic
JL_GC_POP();
jl_cgval_t argv[2];
argv[0] = emit_expr(ctx, args[1]);
if (nargs == 2)
argv[1] = emit_expr(ctx, args[2]);
return emit_runtime_call(ctx, nargs == 1 ? JL_I::cglobal_auto : JL_I::cglobal, argv, nargs);
}

JL_GC_POP();
Expand Down
11 changes: 11 additions & 0 deletions test/ccall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1937,3 +1937,14 @@ end

# issue #52025
@test Base.unsafe_convert(Ptr{Ptr{Cchar}}, Base.cconvert(Ptr{Ptr{Cchar}}, map(pointer, ["ab"]))) isa Ptr{Ptr{Cchar}}

# Cglobal with non-static symbols doesn't error
function cglobal_non_static1()
sym = (:global_var, libccalltest)
cglobal(sym)
end
global the_sym = (:global_var, libccalltest)
cglobal_non_static2() = cglobal(the_sym)

@test isa(cglobal_non_static1(), Ptr)
@test isa(cglobal_non_static2(), Ptr)

0 comments on commit 8791d54

Please sign in to comment.