Skip to content

Commit 3708229

Browse files
Add alignment to constant globals (#50738)
1 parent f00957b commit 3708229

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

src/cgutils.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ static Value *stringConstPtr(
125125
ctxt.resize(28);
126126
ctxt[25] = ctxt[26] = ctxt[27] = '.';
127127
}
128-
GlobalVariable *gv = get_pointer_to_constant(emission_context, Data, "_j_str_" + StringRef(ctxt.data(), ctxt.size()), *M);
128+
// Doesn't need to be aligned, we shouldn't operate on these like julia objects
129+
GlobalVariable *gv = get_pointer_to_constant(emission_context, Data, Align(1), "_j_str_" + StringRef(ctxt.data(), ctxt.size()), *M);
129130
Value *zero = ConstantInt::get(Type::getInt32Ty(irbuilder.getContext()), 0);
130131
Value *Args[] = { zero, zero };
131132
auto gep = irbuilder.CreateInBoundsGEP(gv->getValueType(),
@@ -918,7 +919,7 @@ static Value *data_pointer(jl_codectx_t &ctx, const jl_cgval_t &x)
918919
if (x.constant) {
919920
Constant *val = julia_const_to_llvm(ctx, x.constant);
920921
if (val)
921-
data = get_pointer_to_constant(ctx.emission_context, val, "_j_const", *jl_Module);
922+
data = get_pointer_to_constant(ctx.emission_context, val, Align(julia_alignment(jl_typeof(x.constant))), "_j_const", *jl_Module);
922923
else
923924
data = literal_pointer_val(ctx, x.constant);
924925
}
@@ -1106,7 +1107,7 @@ static Value *emit_typeof(jl_codectx_t &ctx, const jl_cgval_t &p, bool maybenull
11061107
if (justtag && jt->smalltag) {
11071108
ptr = ConstantInt::get(expr_type, jt->smalltag << 4);
11081109
if (ctx.emission_context.imaging)
1109-
ptr = get_pointer_to_constant(ctx.emission_context, ptr, StringRef("_j_smalltag_") + jl_symbol_name(jt->name->name), *jl_Module);
1110+
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);
11101111
}
11111112
else if (ctx.emission_context.imaging)
11121113
ptr = ConstantExpr::getBitCast(literal_pointer_val_slot(ctx, (jl_value_t*)jt), datatype_or_p->getType());

src/codegen.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,7 +1775,7 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
17751775
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);
17761776

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

17801780
static GlobalVariable *prepare_global_in(Module *M, JuliaVariable *G)
17811781
{
@@ -1813,7 +1813,7 @@ static inline GlobalVariable *prepare_global_in(Module *M, GlobalVariable *G)
18131813

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

1816-
static GlobalVariable *get_pointer_to_constant(jl_codegen_params_t &emission_context, Constant *val, const Twine &name, Module &M)
1816+
static GlobalVariable *get_pointer_to_constant(jl_codegen_params_t &emission_context, Constant *val, Align align, const Twine &name, Module &M)
18171817
{
18181818
GlobalVariable *&gv = emission_context.mergedConstants[val];
18191819
auto get_gv = [&](const Twine &name) {
@@ -1825,6 +1825,7 @@ static GlobalVariable *get_pointer_to_constant(jl_codegen_params_t &emission_con
18251825
val,
18261826
name);
18271827
gv->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
1828+
gv->setAlignment(align);
18281829
return gv;
18291830
};
18301831
if (gv == nullptr) {
@@ -1945,7 +1946,8 @@ static inline jl_cgval_t value_to_pointer(jl_codectx_t &ctx, Value *v, jl_value_
19451946
{
19461947
Value *loc;
19471948
if (valid_as_globalinit(v)) { // llvm can't handle all the things that could be inside a ConstantExpr
1948-
loc = get_pointer_to_constant(ctx.emission_context, cast<Constant>(v), "_j_const", *jl_Module);
1949+
assert(jl_is_concrete_type(typ)); // not legal to have an unboxed abstract type
1950+
loc = get_pointer_to_constant(ctx.emission_context, cast<Constant>(v), Align(julia_alignment(typ)), "_j_const", *jl_Module);
19491951
}
19501952
else {
19511953
loc = emit_static_alloca(ctx, v->getType());
@@ -2319,7 +2321,7 @@ static jl_cgval_t convert_julia_type(jl_codectx_t &ctx, const jl_cgval_t &v, jl_
23192321
new_tindex = ConstantInt::get(getInt8Ty(ctx.builder.getContext()), new_idx);
23202322
if (v.V && !v.ispointer()) {
23212323
// TODO: remove this branch once all consumers of v.TIndex understand how to handle a non-ispointer value
2322-
return value_to_pointer(ctx, v.V, typ, new_tindex);
2324+
return jl_cgval_t(value_to_pointer(ctx, v), typ, new_tindex);
23232325
}
23242326
}
23252327
else if (jl_subtype(v.typ, typ)) {
@@ -7672,7 +7674,7 @@ static jl_llvm_functions_t
76727674
Type *vtype = julia_type_to_llvm(ctx, jt, &isboxed);
76737675
assert(!isboxed);
76747676
assert(!type_is_ghost(vtype) && "constants should already be handled");
7675-
Value *lv = new AllocaInst(vtype, M->getDataLayout().getAllocaAddrSpace(), NULL, Align(jl_datatype_align(jt)), jl_symbol_name(s), /*InsertBefore*/ctx.topalloca);
7677+
Value *lv = new AllocaInst(vtype, M->getDataLayout().getAllocaAddrSpace(), nullptr, Align(jl_datatype_align(jt)), jl_symbol_name(s), /*InsertBefore*/ctx.topalloca);
76767678
if (CountTrackedPointers(vtype).count) {
76777679
StoreInst *SI = new StoreInst(Constant::getNullValue(vtype), lv, false, Align(sizeof(void*)));
76787680
SI->insertAfter(ctx.topalloca);
@@ -7692,7 +7694,7 @@ static jl_llvm_functions_t
76927694
(va && (int)i == ctx.vaSlot) || // or it's the va arg tuple
76937695
i == 0) { // or it is the first argument (which isn't in `argArray`)
76947696
AllocaInst *av = new AllocaInst(ctx.types().T_prjlvalue, M->getDataLayout().getAllocaAddrSpace(),
7695-
jl_symbol_name(s), /*InsertBefore*/ctx.topalloca);
7697+
nullptr, Align(sizeof(jl_value_t*)), jl_symbol_name(s), /*InsertBefore*/ctx.topalloca);
76967698
StoreInst *SI = new StoreInst(Constant::getNullValue(ctx.types().T_prjlvalue), av, false, Align(sizeof(void*)));
76977699
SI->insertAfter(ctx.topalloca);
76987700
varinfo.boxroot = av;

src/llvm-late-gc-lowering.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,8 +2370,7 @@ bool LateLowerGCFrame::CleanupIR(Function &F, State *S, bool *CFGModified) {
23702370
if (isLoadFromConstGV(LI, task_local) && getLoadValueAlign(LI) < 16) {
23712371
Type *T_int64 = Type::getInt64Ty(LI->getContext());
23722372
auto op = ConstantAsMetadata::get(ConstantInt::get(T_int64, 16));
2373-
LI->setMetadata(LLVMContext::MD_align,
2374-
MDNode::get(LI->getContext(), { op }));
2373+
LI->setMetadata(LLVMContext::MD_align, MDNode::get(LI->getContext(), { op }));
23752374
}
23762375
}
23772376
// As a last resort, if we didn't manage to strip down the tag
@@ -2480,7 +2479,7 @@ bool LateLowerGCFrame::CleanupIR(Function &F, State *S, bool *CFGModified) {
24802479
}
24812480
ReplacementArgs.push_back(nframeargs == 0 ?
24822481
(llvm::Value*)ConstantPointerNull::get(T_pprjlvalue) :
2483-
(allocaAddressSpace ? Builder.CreateAddrSpaceCast(Frame, T_prjlvalue->getPointerTo(0)) : Frame));
2482+
Builder.CreateAddrSpaceCast(Frame, T_prjlvalue->getPointerTo(0)));
24842483
ReplacementArgs.push_back(ConstantInt::get(T_int32, nframeargs));
24852484
if (callee == call2_func) {
24862485
// move trailing arg to the end now

0 commit comments

Comments
 (0)