Skip to content

Commit

Permalink
codegen: Avoid some uses of PointerType::getElementType() (#43721)
Browse files Browse the repository at this point in the history
getElementType() will eventually go away with the LLVM switch
to opaque pointers.

The assertion in gen_cfun_wrapper is still checked within
CreateAlignedLoad.
  • Loading branch information
dnadlinger committed Jan 11, 2022
1 parent 631f195 commit 3b5f985
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ static Value *emit_plt(
functype, attrs, cc, f_lib, f_name, libptrgv, llvmgv, runtime_lib);
}
GlobalVariable *got = prepare_global_in(jl_Module, sharedgot);
LoadInst *got_val = ctx.builder.CreateAlignedLoad(got->getType()->getElementType(), got, Align(sizeof(void*)));
LoadInst *got_val = ctx.builder.CreateAlignedLoad(got->getValueType(), got, Align(sizeof(void*)));
// See comment in `runtime_sym_lookup` above. This in principle needs a
// consume ordering too. This is even less likely to cause issues though
// since the only thing we do to this loaded pointer is to call it
Expand Down
35 changes: 17 additions & 18 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2482,30 +2482,29 @@ static Value *emit_arraylen(jl_codectx_t &ctx, const jl_cgval_t &tinfo)

static Value *emit_arrayptr_internal(jl_codectx_t &ctx, const jl_cgval_t &tinfo, Value *t, unsigned AS, bool isboxed)
{
Value *addr =
ctx.builder.CreateStructGEP(jl_array_llvmt,
emit_bitcast(ctx, t, jl_parray_llvmt),
0); // index (not offset) of data field in jl_parray_llvmt
Value *addr = ctx.builder.CreateStructGEP(jl_array_llvmt,
emit_bitcast(ctx, t, jl_parray_llvmt), 0);
// Normally allocated array of 0 dimension always have a inline pointer.
// However, we can't rely on that here since arrays can also be constructed from C pointers.
MDNode *tbaa = arraytype_constshape(tinfo.typ) ? tbaa_const : tbaa_arrayptr;
PointerType *PT = cast<PointerType>(addr->getType());
PointerType *PPT = cast<PointerType>(PT->getElementType());
PointerType *PPT = cast<PointerType>(jl_array_llvmt->getElementType(0));
PointerType *LoadT = PPT;

if (isboxed) {
addr = ctx.builder.CreateBitCast(addr,
PointerType::get(PointerType::get(T_prjlvalue, AS),
PT->getAddressSpace()));
} else if (AS != PPT->getAddressSpace()) {
addr = ctx.builder.CreateBitCast(addr,
PointerType::get(
PointerType::get(PPT->getElementType(), AS),
PT->getAddressSpace()));
}
LoadInst *LI = ctx.builder.CreateAlignedLoad(
cast<PointerType>(addr->getType())->getElementType(), addr, Align(sizeof(char*)));
LoadT = PointerType::get(T_prjlvalue, AS);
}
else if (AS != PPT->getAddressSpace()) {
LoadT = PointerType::getWithSamePointeeType(PPT, AS);
}
if (LoadT != PPT) {
const auto Ty = PointerType::get(LoadT, PT->getAddressSpace());
addr = ctx.builder.CreateBitCast(addr, Ty);
}

LoadInst *LI = ctx.builder.CreateAlignedLoad(LoadT, addr, Align(sizeof(char *)));
LI->setOrdering(AtomicOrdering::NotAtomic);
LI->setMetadata(LLVMContext::MD_nonnull, MDNode::get(jl_LLVMContext, None));
tbaa_decorate(tbaa, LI);
tbaa_decorate(arraytype_constshape(tinfo.typ) ? tbaa_const : tbaa_arrayptr, LI);
return LI;
}

Expand Down
7 changes: 3 additions & 4 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ static Type *T_pjlvalue;
static Type *T_prjlvalue;
static Type *T_ppjlvalue;
static Type *T_pprjlvalue;
static Type *jl_array_llvmt;
static StructType *jl_array_llvmt;
static Type *jl_parray_llvmt;
static FunctionType *jl_func_sig;
static FunctionType *jl_func_sig_sparams;
Expand Down Expand Up @@ -1143,7 +1143,7 @@ static inline GlobalVariable *prepare_global_in(Module *M, GlobalVariable *G)
GlobalValue *local = M->getNamedValue(G->getName());
if (!local) {
// Copy the GlobalVariable, but without the initializer, so it becomes a declaration
GlobalVariable *proto = new GlobalVariable(*M, G->getType()->getElementType(),
GlobalVariable *proto = new GlobalVariable(*M, G->getValueType(),
G->isConstant(), GlobalVariable::ExternalLinkage,
nullptr, G->getName(), nullptr, G->getThreadLocalMode());
proto->copyAttributesFrom(G);
Expand Down Expand Up @@ -5242,7 +5242,6 @@ static Function* gen_cfun_wrapper(
// undo whatever we might have done to this poor argument
assert(jl_is_datatype(jargty));
if (sig.byRefList.at(i)) {
assert(cast<PointerType>(val->getType())->getElementType() == sig.fargt[i]);
val = ctx.builder.CreateAlignedLoad(sig.fargt[i], val, Align(1)); // unknown alignment from C
}
else {
Expand Down Expand Up @@ -5476,7 +5475,7 @@ static Function* gen_cfun_wrapper(
ctx.builder.ClearInsertionPoint();

if (aliasname) {
GlobalAlias::create(cw->getType()->getElementType(), cw->getType()->getAddressSpace(),
GlobalAlias::create(cw->getValueType(), cw->getType()->getAddressSpace(),
GlobalValue::ExternalLinkage, aliasname, cw, M);
}

Expand Down
2 changes: 1 addition & 1 deletion src/llvm-ptls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ bool LowerPTLS::runOnModule(Module &_M)
#endif
T_pgcstack_getter = FT_pgcstack_getter->getPointerTo();
T_pppjlvalue = cast<PointerType>(FT_pgcstack_getter->getReturnType());
T_ppjlvalue = cast<PointerType>(T_pppjlvalue->getElementType());
T_ppjlvalue = JuliaType::get_ppjlvalue_ty(*ctx);
if (imaging_mode) {
pgcstack_func_slot = create_aliased_global(T_pgcstack_getter, "jl_pgcstack_func_slot");
pgcstack_key_slot = create_aliased_global(T_size, "jl_pgcstack_key_slot"); // >= sizeof(jl_pgcstack_key_t)
Expand Down

0 comments on commit 3b5f985

Please sign in to comment.