Skip to content

Commit

Permalink
Revert "Remove references to non opaque pointers in codegen and LLVM …
Browse files Browse the repository at this point in the history
…passes (JuliaLang#54853)"

This reverts commit 5e1bcdf.
  • Loading branch information
Zentrik committed Jul 10, 2024
1 parent 3ab8fef commit 773a9aa
Show file tree
Hide file tree
Showing 14 changed files with 365 additions and 201 deletions.
64 changes: 35 additions & 29 deletions src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ static bool runtime_sym_gvs(jl_codectx_t &ctx, const char *f_lib, const char *f_
runtime_lib = true;
auto &libgv = ctx.emission_context.libMapGV[f_lib];
if (libgv.first == NULL) {
libptrgv = new GlobalVariable(*M, getPointerTy(M->getContext()), false,
libptrgv = new GlobalVariable(*M, getInt8PtrTy(M->getContext()), false,
GlobalVariable::ExternalLinkage,
Constant::getNullValue(getPointerTy(M->getContext())), name);
Constant::getNullValue(getInt8PtrTy(M->getContext())), name);
libgv.first = libptrgv;
}
else {
Expand Down Expand Up @@ -169,7 +169,7 @@ static Value *runtime_sym_lookup(
}
else {
// f_lib is actually one of the special sentinel values
libname = ConstantExpr::getIntToPtr(ConstantInt::get(emission_context.DL.getIntPtrType(irbuilder.getContext()), (uintptr_t)f_lib), getPointerTy(irbuilder.getContext()));
libname = ConstantExpr::getIntToPtr(ConstantInt::get(emission_context.DL.getIntPtrType(irbuilder.getContext()), (uintptr_t)f_lib), getInt8PtrTy(irbuilder.getContext()));
}
auto lookup = irbuilder.CreateCall(prepare_call_in(jl_builderModule(irbuilder), jldlsym_func),
{ libname, nameval, libptrgv });
Expand All @@ -186,7 +186,7 @@ static Value *runtime_sym_lookup(
p->addIncoming(llvmf_orig, enter_bb);
p->addIncoming(llvmf, llvmf->getParent());
setName(emission_context, p, f_name);
return p;
return irbuilder.CreateBitCast(p, funcptype);
}

static Value *runtime_sym_lookup(
Expand Down Expand Up @@ -244,7 +244,7 @@ static GlobalVariable *emit_plt_thunk(
auto T_pvoidfunc = JuliaType::get_pvoidfunc_ty(M->getContext());
GlobalVariable *got = new GlobalVariable(*M, T_pvoidfunc, false,
GlobalVariable::ExternalLinkage,
plt,
ConstantExpr::getBitCast(plt, T_pvoidfunc),
fname + "_got");
if (runtime_lib) {
got->addAttribute("julia.libname", f_lib);
Expand All @@ -256,7 +256,7 @@ static GlobalVariable *emit_plt_thunk(
IRBuilder<> irbuilder(b0);
Value *ptr = runtime_sym_lookup(ctx.emission_context, irbuilder, NULL, funcptype, f_lib, NULL, f_name, plt, libptrgv,
llvmgv, runtime_lib);
StoreInst *store = irbuilder.CreateAlignedStore(ptr, got, Align(sizeof(void*)));
StoreInst *store = irbuilder.CreateAlignedStore(irbuilder.CreateBitCast(ptr, T_pvoidfunc), got, Align(sizeof(void*)));
store->setAtomic(AtomicOrdering::Release);
SmallVector<Value*, 16> args;
for (auto &arg : plt->args())
Expand Down Expand Up @@ -311,6 +311,7 @@ static Value *emit_plt(
GlobalVariable *libptrgv;
GlobalVariable *llvmgv;
bool runtime_lib = runtime_sym_gvs(ctx, f_lib, f_name, libptrgv, llvmgv);
PointerType *funcptype = PointerType::get(functype, 0);

auto &pltMap = ctx.emission_context.allPltMap[attrs];
auto key = std::make_tuple(llvmgv, functype, cc);
Expand All @@ -327,7 +328,7 @@ static Value *emit_plt(
// since the only thing we do to this loaded pointer is to call it
// immediately.
got_val->setAtomic(AtomicOrdering::Unordered);
return got_val;
return ctx.builder.CreateBitCast(got_val, funcptype);
}

// --- ABI Implementations ---
Expand Down Expand Up @@ -410,7 +411,7 @@ static Value *llvm_type_rewrite(

assert(from_type->isPointerTy() == target_type->isPointerTy()); // expect that all ABIs consider all pointers to be equivalent
if (target_type->isPointerTy())
return v;
return emit_bitcast(ctx, v, target_type);

// simple integer and float widening & conversion cases
if (from_type->getPrimitiveSizeInBits() > 0 &&
Expand Down Expand Up @@ -446,13 +447,13 @@ static Value *llvm_type_rewrite(
to = emit_static_alloca(ctx, target_type);
setName(ctx.emission_context, to, "type_rewrite_buffer");
cast<AllocaInst>(to)->setAlignment(align);
from = to;
from = emit_bitcast(ctx, to, from_type->getPointerTo());
}
else {
from = emit_static_alloca(ctx, from_type);
setName(ctx.emission_context, from, "type_rewrite_buffer");
cast<AllocaInst>(from)->setAlignment(align);
to = from;
to = emit_bitcast(ctx, from, target_type->getPointerTo());
}
ctx.builder.CreateAlignedStore(v, from, align);
auto pun = ctx.builder.CreateAlignedLoad(target_type, to, align);
Expand Down Expand Up @@ -706,7 +707,7 @@ 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.jl_ptr != NULL) {
res = sym.jl_ptr;
res = ctx.builder.CreateBitCast(sym.jl_ptr, lrt);
}
else if (sym.fptr != NULL) {
res = ConstantInt::get(lrt, (uint64_t)sym.fptr);
Expand All @@ -715,10 +716,10 @@ static jl_cgval_t emit_cglobal(jl_codectx_t &ctx, jl_value_t **args, size_t narg
}
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);
res = runtime_sym_lookup(ctx, cast<PointerType>(getInt8PtrTy(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);
res = runtime_sym_lookup(ctx, cast<PointerType>(getInt8PtrTy(ctx.builder.getContext())), sym.f_lib, NULL, sym.f_name, ctx.f);
}
} else {
// Fall back to runtime intrinsic
Expand Down Expand Up @@ -1231,7 +1232,7 @@ std::string generate_func_sig(const char *fname)
bool isboxed;
if (jl_is_abstract_ref_type(tti)) {
tti = (jl_value_t*)jl_voidpointer_type;
t = getPointerTy(LLVMCtx);
t = getInt8PtrTy(LLVMCtx);
isboxed = false;
}
else if (llvmcall && jl_is_llvmpointer_type(tti)) {
Expand Down Expand Up @@ -1593,7 +1594,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
Value *retval;
if (isboxed) {
retval = boxed(ctx, argv[0]);
retval = emit_pointer_from_objref(ctx, retval /*T_prjlvalue*/);
retval = emit_pointer_from_objref(ctx, emit_bitcast(ctx, retval, ctx.types().T_prjlvalue));
}
else {
retval = emit_unbox(ctx, largty, argv[0], tti);
Expand Down Expand Up @@ -1682,9 +1683,9 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
assert(lrt == getInt16Ty(ctx.builder.getContext()));
assert(!isVa && !llvmcall && nccallargs == 0);
JL_GC_POP();
Value *ptask = get_current_task(ctx);
Value *ptask_i16 = emit_bitcast(ctx, get_current_task(ctx), getInt16PtrTy(ctx.builder.getContext()));
const int tid_offset = offsetof(jl_task_t, tid);
Value *ptid = ctx.builder.CreateInBoundsGEP(getInt8Ty(ctx.builder.getContext()), ptask, ConstantInt::get(ctx.types().T_size, tid_offset / sizeof(int8_t)));
Value *ptid = ctx.builder.CreateInBoundsGEP(getInt16Ty(ctx.builder.getContext()), ptask_i16, ConstantInt::get(ctx.types().T_size, tid_offset / sizeof(int16_t)));
setName(ctx.emission_context, ptid, "thread_id_ptr");
LoadInst *tid = ctx.builder.CreateAlignedLoad(getInt16Ty(ctx.builder.getContext()), ptid, Align(sizeof(int16_t)));
setName(ctx.emission_context, tid, "thread_id");
Expand Down Expand Up @@ -1712,9 +1713,9 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
#endif
) {
JL_GC_POP();
Value *ptls_p = get_current_ptls(ctx);
Value *ptls_i32 = emit_bitcast(ctx, get_current_ptls(ctx), getInt32PtrTy(ctx.builder.getContext()));
const int finh_offset = offsetof(jl_tls_states_t, finalizers_inhibited);
Value *pfinh = ctx.builder.CreateInBoundsGEP(getInt8Ty(ctx.builder.getContext()), ptls_p, ConstantInt::get(ctx.types().T_size, finh_offset / sizeof(int8_t)));
Value *pfinh = ctx.builder.CreateInBoundsGEP(getInt32Ty(ctx.builder.getContext()), ptls_i32, ConstantInt::get(ctx.types().T_size, finh_offset / 4));
setName(ctx.emission_context, pfinh, "finalizers_inhibited_ptr");
LoadInst *finh = ctx.builder.CreateAlignedLoad(getInt32Ty(ctx.builder.getContext()), pfinh, Align(sizeof(int32_t)));
setName(ctx.emission_context, finh, "finalizers_inhibited");
Expand Down Expand Up @@ -1745,7 +1746,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
assert(lrt == getVoidTy(ctx.builder.getContext()));
assert(!isVa && !llvmcall && nccallargs == 1);
JL_GC_POP();
Value *ptls_pv = get_current_ptls(ctx);
Value *ptls_pv = emit_bitcast(ctx, get_current_ptls(ctx), ctx.types().T_ppjlvalue);
const int nt_offset = offsetof(jl_tls_states_t, next_task);
Value *pnt = ctx.builder.CreateInBoundsGEP(ctx.types().T_pjlvalue, ptls_pv, ConstantInt::get(ctx.types().T_size, nt_offset / sizeof(void*)));
setName(ctx.emission_context, pnt, "next_task_ptr");
Expand Down Expand Up @@ -1811,7 +1812,8 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
++CCALL_STAT(jl_string_ptr);
assert(lrt == ctx.types().T_ptr);
assert(!isVa && !llvmcall && nccallargs == 1);
auto obj = emit_pointer_from_objref(ctx, boxed(ctx, argv[0])); // T_pprjlvalue
auto obj = emit_bitcast(ctx, emit_pointer_from_objref(ctx, boxed(ctx, argv[0])),
ctx.types().T_pprjlvalue);
// The inbounds gep makes it more clear to LLVM that the resulting value is not
// a null pointer.
auto strp = ctx.builder.CreateConstInBoundsGEP1_32(ctx.types().T_prjlvalue, obj, 1);
Expand All @@ -1823,7 +1825,8 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
++CCALL_STAT(jl_symbol_name);
assert(lrt == ctx.types().T_ptr);
assert(!isVa && !llvmcall && nccallargs == 1);
auto obj = emit_pointer_from_objref(ctx, boxed(ctx, argv[0])); // T_pprjlvalue
auto obj = emit_bitcast(ctx, emit_pointer_from_objref(ctx, boxed(ctx, argv[0])),
ctx.types().T_pprjlvalue);
// The inbounds gep makes it more clear to LLVM that the resulting value is not
// a null pointer.
auto strp = ctx.builder.CreateConstInBoundsGEP1_32(
Expand Down Expand Up @@ -1928,7 +1931,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
if (val.typ == (jl_value_t*)jl_symbol_type) {
JL_GC_POP();
const int hash_offset = offsetof(jl_sym_t, hash);
Value *ph1 = decay_derived(ctx, boxed(ctx, val));
Value *ph1 = emit_bitcast(ctx, decay_derived(ctx, boxed(ctx, val)), ctx.types().T_size->getPointerTo());
Value *ph2 = ctx.builder.CreateInBoundsGEP(ctx.types().T_size, ph1, ConstantInt::get(ctx.types().T_size, hash_offset / ctx.types().sizeof_ptr));
setName(ctx.emission_context, ph2, "object_id_ptr");
LoadInst *hashval = ctx.builder.CreateAlignedLoad(ctx.types().T_size, ph2, ctx.types().alignof_ptr);
Expand All @@ -1940,13 +1943,15 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
else if (!val.isboxed) {
// If the value is not boxed, try to compute the object id without
// reboxing it.
auto T_p_derived = PointerType::get(ctx.builder.getContext(), AddressSpace::Derived);
auto T_pint8_derived = PointerType::get(getInt8Ty(ctx.builder.getContext()), AddressSpace::Derived);
if (!val.isghost && !val.ispointer())
val = value_to_pointer(ctx, val);
Value *args[] = {
emit_typeof(ctx, val, false, true),
val.isghost ? ConstantPointerNull::get(T_p_derived) :
decay_derived(ctx, data_pointer(ctx, val))
val.isghost ? ConstantPointerNull::get(T_pint8_derived) :
ctx.builder.CreateBitCast(
decay_derived(ctx, data_pointer(ctx, val)),
T_pint8_derived)
};
Value *ret = ctx.builder.CreateCall(prepare_call(jl_object_id__func), ArrayRef<Value*>(args));
setName(ctx.emission_context, ret, "object_id");
Expand Down Expand Up @@ -2040,7 +2045,7 @@ jl_cgval_t function_sig_t::emit_a_ccall(
result = emit_static_alloca(ctx, lrt);
setName(ctx.emission_context, result, "ccall_sret");
sretty = lrt;
argvals[0] = result;
argvals[0] = ctx.builder.CreateBitCast(result, fargt_sig[0]);
}
else {
// XXX: result needs to be zero'd and given a GC root here
Expand All @@ -2051,7 +2056,7 @@ jl_cgval_t function_sig_t::emit_a_ccall(
sretty = ctx.types().T_jlvalue;
sretboxed = true;
gc_uses.push_back(result);
argvals[0] = emit_pointer_from_objref(ctx, result);
argvals[0] = ctx.builder.CreateBitCast(emit_pointer_from_objref(ctx, result), fargt_sig[0]);
}
}

Expand Down Expand Up @@ -2114,7 +2119,8 @@ jl_cgval_t function_sig_t::emit_a_ccall(
else if (symarg.jl_ptr != NULL) {
++LiteralCCalls;
null_pointer_check(ctx, symarg.jl_ptr, nullptr);
llvmf = symarg.jl_ptr;
Type *funcptype = PointerType::get(functype, 0);
llvmf = ctx.builder.CreateBitCast(symarg.jl_ptr, funcptype);
}
else if (symarg.fptr != NULL) {
++LiteralCCalls;
Expand Down
Loading

0 comments on commit 773a9aa

Please sign in to comment.