Skip to content

Commit

Permalink
Add alignment to constant globals (#50738)
Browse files Browse the repository at this point in the history
  • Loading branch information
pchintalapudi authored Aug 2, 2023
1 parent f00957b commit 3708229
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ static Value *stringConstPtr(
ctxt.resize(28);
ctxt[25] = ctxt[26] = ctxt[27] = '.';
}
GlobalVariable *gv = get_pointer_to_constant(emission_context, Data, "_j_str_" + StringRef(ctxt.data(), ctxt.size()), *M);
// Doesn't need to be aligned, we shouldn't operate on these like julia objects
GlobalVariable *gv = get_pointer_to_constant(emission_context, Data, Align(1), "_j_str_" + StringRef(ctxt.data(), ctxt.size()), *M);
Value *zero = ConstantInt::get(Type::getInt32Ty(irbuilder.getContext()), 0);
Value *Args[] = { zero, zero };
auto gep = irbuilder.CreateInBoundsGEP(gv->getValueType(),
Expand Down Expand Up @@ -918,7 +919,7 @@ static Value *data_pointer(jl_codectx_t &ctx, const jl_cgval_t &x)
if (x.constant) {
Constant *val = julia_const_to_llvm(ctx, x.constant);
if (val)
data = get_pointer_to_constant(ctx.emission_context, val, "_j_const", *jl_Module);
data = get_pointer_to_constant(ctx.emission_context, val, Align(julia_alignment(jl_typeof(x.constant))), "_j_const", *jl_Module);
else
data = literal_pointer_val(ctx, x.constant);
}
Expand Down Expand Up @@ -1106,7 +1107,7 @@ static Value *emit_typeof(jl_codectx_t &ctx, const jl_cgval_t &p, bool maybenull
if (justtag && jt->smalltag) {
ptr = ConstantInt::get(expr_type, jt->smalltag << 4);
if (ctx.emission_context.imaging)
ptr = get_pointer_to_constant(ctx.emission_context, ptr, StringRef("_j_smalltag_") + jl_symbol_name(jt->name->name), *jl_Module);
ptr = get_pointer_to_constant(ctx.emission_context, ptr, Align(sizeof(jl_value_t*)), StringRef("_j_smalltag_") + jl_symbol_name(jt->name->name), *jl_Module);
}
else if (ctx.emission_context.imaging)
ptr = ConstantExpr::getBitCast(literal_pointer_val_slot(ctx, (jl_value_t*)jt), datatype_or_p->getType());
Expand Down
14 changes: 8 additions & 6 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,7 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
static jl_cgval_t emit_invoke(jl_codectx_t &ctx, const jl_cgval_t &lival, const jl_cgval_t *argv, size_t nargs, jl_value_t *rt);

static Value *literal_pointer_val(jl_codectx_t &ctx, jl_value_t *p);
static GlobalVariable *prepare_global_in(Module *M, GlobalVariable *G);
static unsigned julia_alignment(jl_value_t *jt);

static GlobalVariable *prepare_global_in(Module *M, JuliaVariable *G)
{
Expand Down Expand Up @@ -1813,7 +1813,7 @@ static inline GlobalVariable *prepare_global_in(Module *M, GlobalVariable *G)

// --- convenience functions for tagging llvm values with julia types ---

static GlobalVariable *get_pointer_to_constant(jl_codegen_params_t &emission_context, Constant *val, const Twine &name, Module &M)
static GlobalVariable *get_pointer_to_constant(jl_codegen_params_t &emission_context, Constant *val, Align align, const Twine &name, Module &M)
{
GlobalVariable *&gv = emission_context.mergedConstants[val];
auto get_gv = [&](const Twine &name) {
Expand All @@ -1825,6 +1825,7 @@ static GlobalVariable *get_pointer_to_constant(jl_codegen_params_t &emission_con
val,
name);
gv->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
gv->setAlignment(align);
return gv;
};
if (gv == nullptr) {
Expand Down Expand Up @@ -1945,7 +1946,8 @@ static inline jl_cgval_t value_to_pointer(jl_codectx_t &ctx, Value *v, jl_value_
{
Value *loc;
if (valid_as_globalinit(v)) { // llvm can't handle all the things that could be inside a ConstantExpr
loc = get_pointer_to_constant(ctx.emission_context, cast<Constant>(v), "_j_const", *jl_Module);
assert(jl_is_concrete_type(typ)); // not legal to have an unboxed abstract type
loc = get_pointer_to_constant(ctx.emission_context, cast<Constant>(v), Align(julia_alignment(typ)), "_j_const", *jl_Module);
}
else {
loc = emit_static_alloca(ctx, v->getType());
Expand Down Expand Up @@ -2319,7 +2321,7 @@ static jl_cgval_t convert_julia_type(jl_codectx_t &ctx, const jl_cgval_t &v, jl_
new_tindex = ConstantInt::get(getInt8Ty(ctx.builder.getContext()), new_idx);
if (v.V && !v.ispointer()) {
// TODO: remove this branch once all consumers of v.TIndex understand how to handle a non-ispointer value
return value_to_pointer(ctx, v.V, typ, new_tindex);
return jl_cgval_t(value_to_pointer(ctx, v), typ, new_tindex);
}
}
else if (jl_subtype(v.typ, typ)) {
Expand Down Expand Up @@ -7672,7 +7674,7 @@ static jl_llvm_functions_t
Type *vtype = julia_type_to_llvm(ctx, jt, &isboxed);
assert(!isboxed);
assert(!type_is_ghost(vtype) && "constants should already be handled");
Value *lv = new AllocaInst(vtype, M->getDataLayout().getAllocaAddrSpace(), NULL, Align(jl_datatype_align(jt)), jl_symbol_name(s), /*InsertBefore*/ctx.topalloca);
Value *lv = new AllocaInst(vtype, M->getDataLayout().getAllocaAddrSpace(), nullptr, Align(jl_datatype_align(jt)), jl_symbol_name(s), /*InsertBefore*/ctx.topalloca);
if (CountTrackedPointers(vtype).count) {
StoreInst *SI = new StoreInst(Constant::getNullValue(vtype), lv, false, Align(sizeof(void*)));
SI->insertAfter(ctx.topalloca);
Expand All @@ -7692,7 +7694,7 @@ static jl_llvm_functions_t
(va && (int)i == ctx.vaSlot) || // or it's the va arg tuple
i == 0) { // or it is the first argument (which isn't in `argArray`)
AllocaInst *av = new AllocaInst(ctx.types().T_prjlvalue, M->getDataLayout().getAllocaAddrSpace(),
jl_symbol_name(s), /*InsertBefore*/ctx.topalloca);
nullptr, Align(sizeof(jl_value_t*)), jl_symbol_name(s), /*InsertBefore*/ctx.topalloca);
StoreInst *SI = new StoreInst(Constant::getNullValue(ctx.types().T_prjlvalue), av, false, Align(sizeof(void*)));
SI->insertAfter(ctx.topalloca);
varinfo.boxroot = av;
Expand Down
5 changes: 2 additions & 3 deletions src/llvm-late-gc-lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2370,8 +2370,7 @@ bool LateLowerGCFrame::CleanupIR(Function &F, State *S, bool *CFGModified) {
if (isLoadFromConstGV(LI, task_local) && getLoadValueAlign(LI) < 16) {
Type *T_int64 = Type::getInt64Ty(LI->getContext());
auto op = ConstantAsMetadata::get(ConstantInt::get(T_int64, 16));
LI->setMetadata(LLVMContext::MD_align,
MDNode::get(LI->getContext(), { op }));
LI->setMetadata(LLVMContext::MD_align, MDNode::get(LI->getContext(), { op }));
}
}
// As a last resort, if we didn't manage to strip down the tag
Expand Down Expand Up @@ -2480,7 +2479,7 @@ bool LateLowerGCFrame::CleanupIR(Function &F, State *S, bool *CFGModified) {
}
ReplacementArgs.push_back(nframeargs == 0 ?
(llvm::Value*)ConstantPointerNull::get(T_pprjlvalue) :
(allocaAddressSpace ? Builder.CreateAddrSpaceCast(Frame, T_prjlvalue->getPointerTo(0)) : Frame));
Builder.CreateAddrSpaceCast(Frame, T_prjlvalue->getPointerTo(0)));
ReplacementArgs.push_back(ConstantInt::get(T_int32, nframeargs));
if (callee == call2_func) {
// move trailing arg to the end now
Expand Down

0 comments on commit 3708229

Please sign in to comment.