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

cglobal: Fall back to runtime intrinsic #54914

Merged
merged 1 commit into from
Jun 28, 2024
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
cglobal: Fall back to runtime intrinsic
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 committed Jun 24, 2024
commit b814c784c2f4dc79476639c9967a588533b73d79
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)