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: Add an option to bypass jlplt mechanism #51108

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ add_tfunc(Core.Intrinsics.llvmcall, 3, INT_INF, llvmcall_tfunc, 10)
isa(t, Const) && return isa(t.val, Type) ? Ptr{t.val} : Ptr
return isType(t) ? Ptr{t.parameters[1]} : Ptr
end
add_tfunc(Core.Intrinsics.cglobal, 1, 2, cglobal_tfunc, 5)
@nospecs function cglobal_tfunc(𝕃::AbstractLattice, fptr, t, useplt)
return cglobal_tfunc(𝕃, fptr, t)
end
add_tfunc(Core.Intrinsics.cglobal, 1, 3, cglobal_tfunc, 5)

add_tfunc(Core.Intrinsics.have_fma, 1, 1, @nospecs((𝕃::AbstractLattice, x)->Bool), 1)
add_tfunc(Core.Intrinsics.arraylen, 1, 1, @nospecs((𝕃::AbstractLattice, x)->Int), 4)
Expand Down
8 changes: 6 additions & 2 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1873,8 +1873,12 @@ JL_CALLABLE(jl_f_intrinsic_call)
{
JL_TYPECHK(intrinsic_call, intrinsic, F);
enum intrinsic f = (enum intrinsic)*(uint32_t*)jl_data_ptr(F);
if (f == cglobal && nargs == 1)
f = cglobal_auto;
if (f == cglobal) {
if (nargs == 1)
f = cglobal_auto;
else if (nargs == 3)
f = cglobal_pltarg;
}
unsigned fargs = intrinsic_nargs[f];
if (!fargs)
jl_errorf("`%s` must be compiled to be called", jl_intrinsic_name(f));
Expand Down
31 changes: 27 additions & 4 deletions src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,13 +671,15 @@ static jl_cgval_t emit_runtime_call(jl_codectx_t &ctx, JL_I::intrinsic f, const
static jl_cgval_t emit_cglobal(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
{
++EmittedCGlobals;
JL_NARGS(cglobal, 1, 2);
JL_NARGS(cglobal, 1, 3);
jl_value_t *rt = NULL;
Value *res;
native_sym_arg_t sym = {};
JL_GC_PUSH2(&rt, &sym.gcroot);
bool use_jlplt = true;

if (nargs == 2) {
rt = (jl_value_t*)jl_voidpointer_type;
if (nargs >= 2) {
rt = static_eval(ctx, args[2]);
if (rt == NULL) {
JL_GC_POP();
Expand All @@ -690,9 +692,19 @@ static jl_cgval_t emit_cglobal(jl_codectx_t &ctx, jl_value_t **args, size_t narg
JL_TYPECHK(cglobal, type, rt);
rt = (jl_value_t*)jl_apply_type1((jl_value_t*)jl_pointer_type, rt);
}
else {
rt = (jl_value_t*)jl_voidpointer_type;
if (nargs == 3) {
jl_value_t *j_use_jlplt = static_eval(ctx, args[3]);
if (j_use_jlplt == NULL || !jl_is_bool(j_use_jlplt)) {
JL_GC_POP();
jl_cgval_t argv[3] = {jl_cgval_t(), jl_cgval_t(), jl_cgval_t()};
argv[0] = emit_expr(ctx, args[1]);
argv[1] = emit_expr(ctx, args[2]);
argv[2] = emit_expr(ctx, args[3]);
return emit_runtime_call(ctx, JL_I::cglobal, argv, nargs);
}
use_jlplt = jl_unbox_bool(j_use_jlplt);
}

Type *lrt = ctx.types().T_size;
assert(lrt == julia_type_to_llvm(ctx, rt));

Expand All @@ -714,6 +726,17 @@ 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 if (!use_jlplt) {
if ((sym.f_lib && !((sym.f_lib == JL_EXE_LIBNAME) ||
(sym.f_lib == JL_LIBJULIA_INTERNAL_DL_LIBNAME) ||
(sym.f_lib == JL_LIBJULIA_DL_LIBNAME))) || sym.lib_expr) {
jl_printf(JL_STDERR,"WARNING: Attempted to use library expression for symbol %s while disabling jlplt. Library expression was ignored.\n", sym.f_name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codegen shouldn't be printing, except when there is a bug in codegen

}
GlobalVariable *GV = new GlobalVariable(*jl_Module, lrt, true,
GlobalVariable::ExternalLinkage,
nullptr, sym.f_name);
res = ctx.builder.CreatePtrToInt(GV, lrt);
}
else {
if (sym.lib_expr) {
res = runtime_sym_lookup(ctx, cast<PointerType>(getInt8PtrTy(ctx.builder.getContext())), NULL, sym.lib_expr, sym.f_name, ctx.f);
Expand Down
11 changes: 8 additions & 3 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,16 +1181,21 @@ static jl_cgval_t emit_intrinsic(jl_codectx_t &ctx, intrinsic f, jl_value_t **ar
{
auto &DL = ctx.emission_context.DL;
assert(f < num_intrinsics);
if (f == cglobal && nargs == 1)
f = cglobal_auto;
if (f == cglobal) {
if (nargs == 1)
f = cglobal_auto;
else if (nargs == 3)
f = cglobal_pltarg;
}

unsigned expected_nargs = jl_intrinsic_nargs((int)f);
if (expected_nargs && expected_nargs != nargs) {
jl_errorf("intrinsic #%d %s: wrong number of arguments", f, jl_intrinsic_name((int)f));
}

if (f == llvmcall)
return emit_llvmcall(ctx, args, nargs);
if (f == cglobal_auto || f == cglobal)
if (f == cglobal_auto || f == cglobal || f == cglobal_pltarg)
return emit_cglobal(ctx, args, nargs);

SmallVector<jl_cgval_t> argv(nargs);
Expand Down
3 changes: 2 additions & 1 deletion src/intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@
/* cpu feature tests */ \
ADD_I(have_fma, 1) \
/* hidden intrinsics */ \
ADD_HIDDEN(cglobal_auto, 1)
ADD_HIDDEN(cglobal_auto, 1) \
ADD_HIDDEN(cglobal_pltarg, 3)

enum intrinsic {
#define ADD_I(func, nargs) func,
Expand Down
8 changes: 6 additions & 2 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2053,14 +2053,18 @@ void jl_merge_module(orc::ThreadSafeModule &destTSM, orc::ThreadSafeModule srcTS
assert(dest.getTargetTriple() == src.getTargetTriple() && "Cannot merge modules with different target triples!");

for (auto &SG : make_early_inc_range(src.globals())) {
GlobalVariable *dG = cast_or_null<GlobalVariable>(dest.getNamedValue(SG.getName()));
GlobalValue *dG = cast_or_null<GlobalValue>(dest.getNamedValue(SG.getName()));
if (SG.hasLocalLinkage()) {
dG = nullptr;
}
// Replace a declaration with the definition:
if (dG && !dG->hasLocalLinkage()) {
if (SG.isDeclaration()) {
SG.replaceAllUsesWith(dG);
Constant *repl = dG;
if (SG.getType() != repl->getType()) {
repl = ConstantExpr::getBitCast(repl, SG.getType());
}
SG.replaceAllUsesWith(repl);
SG.eraseFromParent();
continue;
}
Expand Down
1 change: 1 addition & 0 deletions src/jl_exported_funcs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
XX(jl_ceil_llvm_withtype) \
XX(jl_cglobal) \
XX(jl_cglobal_auto) \
XX(jl_cglobal_pltarg) \
XX(jl_checked_assignment) \
XX(jl_clear_implicit_imports) \
XX(jl_close_uv) \
Expand Down
1 change: 1 addition & 0 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,7 @@ JL_DLLEXPORT jl_value_t *jl_atomic_pointermodify(jl_value_t *p, jl_value_t *f, j
JL_DLLEXPORT jl_value_t *jl_atomic_pointerreplace(jl_value_t *p, jl_value_t *x, jl_value_t *expected, jl_value_t *success_order, jl_value_t *failure_order);
JL_DLLEXPORT jl_value_t *jl_cglobal(jl_value_t *v, jl_value_t *ty);
JL_DLLEXPORT jl_value_t *jl_cglobal_auto(jl_value_t *v);
JL_DLLEXPORT jl_value_t *jl_cglobal_pltarg(jl_value_t *v, jl_value_t *ty, jl_value_t *usejlplt);

JL_DLLEXPORT jl_value_t *jl_neg_int(jl_value_t *a);
JL_DLLEXPORT jl_value_t *jl_add_int(jl_value_t *a, jl_value_t *b);
Expand Down
4 changes: 4 additions & 0 deletions src/runtime_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ JL_DLLEXPORT jl_value_t *jl_cglobal_auto(jl_value_t *v) {
return jl_cglobal(v, (jl_value_t*)jl_nothing_type);
}

JL_DLLEXPORT jl_value_t *jl_cglobal_pltarg(jl_value_t *v, jl_value_t *ty, jl_value_t *useplt) {
return jl_cglobal(v, ty);
}

static inline char signbitbyte(void *a, unsigned bytes) JL_NOTSAFEPOINT
{
// sign bit of an signed number of n bytes, as a byte
Expand Down