Skip to content

Commit e449fb1

Browse files
committed
fix alignment computation for nested objects
The alignment of a nested object (in C layouts) is not affected by the alignment of its parent container when computing a field offset. This can be strongly counter-intuitive (as it implies adding padding where it does not seem to provide value), but is required to match the C standard. It also permits users to write custom allocators which happen to provide alignment in excess of that which codegen may assume is guaranteed, and get the behavioral characteristics they intended to specify (without resorting to computing explicit padding). Addresses #57713 (comment)
1 parent a97137e commit e449fb1

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/cgutils.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3103,11 +3103,7 @@ static jl_cgval_t emit_getfield_knownidx(jl_codectx_t &ctx, const jl_cgval_t &st
31033103
else if (strct.ispointer()) {
31043104
auto tbaa = best_field_tbaa(ctx, strct, jt, idx, byte_offset);
31053105
Value *staddr = data_pointer(ctx, strct);
3106-
Value *addr;
3107-
if (jl_is_vecelement_type((jl_value_t*)jt) || byte_offset == 0)
3108-
addr = staddr; // VecElement types are unwrapped in LLVM.
3109-
else
3110-
addr = emit_ptrgep(ctx, staddr, byte_offset);
3106+
Value *addr = (byte_offset == 0 ? staddr : emit_ptrgep(ctx, staddr, byte_offset));
31113107
if (addr != staddr)
31123108
setNameWithField(ctx.emission_context, addr, get_objname, jt, idx, Twine("_ptr"));
31133109
if (jl_field_isptr(jt, idx)) {
@@ -3571,7 +3567,7 @@ static void union_alloca_type(jl_uniontype_t *ut,
35713567
[&](unsigned idx, jl_datatype_t *jt) {
35723568
if (!jl_is_datatype_singleton(jt)) {
35733569
size_t nb1 = jl_datatype_size(jt);
3574-
size_t align1 = jl_datatype_align(jt);
3570+
size_t align1 = julia_alignment((jl_value_t*)jt);
35753571
if (nb1 > nbytes)
35763572
nbytes = nb1;
35773573
if (align1 > align)
@@ -4133,10 +4129,11 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
41334129

41344130
// choose whether we should perform the initialization with the struct as a IR value
41354131
// or instead initialize the stack buffer with stores (the later is nearly always better)
4132+
// although we do the former if it is a vector or could be a vector element
41364133
auto tracked = split_value_size(sty);
41374134
assert(CountTrackedPointers(lt).count == tracked.second);
41384135
bool init_as_value = false;
4139-
if (lt->isVectorTy() || jl_is_vecelement_type(ty)) { // maybe also check the size ?
4136+
if (lt->isVectorTy() || jl_special_vector_alignment(1, ty) != 0) {
41404137
init_as_value = true;
41414138
}
41424139

@@ -4343,7 +4340,7 @@ static jl_cgval_t emit_new_struct(jl_codectx_t &ctx, jl_value_t *ty, size_t narg
43434340
if (strct) {
43444341
jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_stack);
43454342
promotion_point = ai.decorateInst(ctx.builder.CreateMemSet(strct, ConstantInt::get(getInt8Ty(ctx.builder.getContext()), 0),
4346-
jl_datatype_size(ty), MaybeAlign(jl_datatype_align(ty))));
4343+
jl_datatype_size(ty), Align(julia_alignment(ty))));
43474344
}
43484345
ctx.builder.restoreIP(savedIP);
43494346
}

src/datatype.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ static jl_datatype_layout_t *jl_get_layout(uint32_t sz,
300300
}
301301

302302
// Determine if homogeneous tuple with fields of type t will have
303-
// a special alignment beyond normal Julia rules.
303+
// a special alignment beyond normal rules.
304304
// Return special alignment if one exists, 0 if normal alignment rules hold.
305305
// A non-zero result *must* match the LLVM rules for a vector type <nfields x t>.
306306
// For sake of Ahead-Of-Time (AOT) compilation, this routine has to work
@@ -318,7 +318,11 @@ unsigned jl_special_vector_alignment(size_t nfields, jl_value_t *t)
318318
return 0;
319319
size_t elsz = jl_datatype_size(ty);
320320
if (elsz != 1 && elsz != 2 && elsz != 4 && elsz != 8)
321-
// Only handle power-of-two-sized elements (for now)
321+
// Only handle power-of-two-sized elements (for now), since other
322+
// lengths may be packed into very complicated arrangements (llvm pads
323+
// extra bits on most platforms when computing alignment but not when
324+
// computing type size, but adds no extra bytes for each element, so
325+
// their effect on offsets are never what you may naturally expect).
322326
return 0;
323327
size_t size = nfields * elsz;
324328
// Use natural alignment for this vector: this matches LLVM and clang.
@@ -723,9 +727,9 @@ void jl_compute_field_offsets(jl_datatype_t *st)
723727
}
724728
else {
725729
fsz = sizeof(void*);
726-
if (fsz > MAX_ALIGN)
727-
fsz = MAX_ALIGN;
728730
al = fsz;
731+
if (al > MAX_ALIGN)
732+
al = MAX_ALIGN;
729733
desc[i].isptr = 1;
730734
zeroinit = 1;
731735
npointers++;
@@ -769,8 +773,6 @@ void jl_compute_field_offsets(jl_datatype_t *st)
769773
if (al > alignm)
770774
alignm = al;
771775
}
772-
if (alignm > MAX_ALIGN)
773-
alignm = MAX_ALIGN; // We cannot guarantee alignments over 16 bytes because that's what our heap is aligned as
774776
if (LLT_ALIGN(sz, alignm) > sz) {
775777
haspadding = 1;
776778
sz = LLT_ALIGN(sz, alignm);
@@ -939,6 +941,14 @@ JL_DLLEXPORT jl_datatype_t *jl_new_primitivetype(jl_value_t *name, jl_module_t *
939941
uint32_t nbytes = (nbits + 7) / 8;
940942
uint32_t alignm = next_power_of_two(nbytes);
941943
# if defined(_CPU_X86_) && !defined(_OS_WINDOWS_)
944+
// datalayout strings are often weird: on 64-bit they usually follow fairly simple rules,
945+
// but on x86 32 bit platforms, sometimes 5 to 8 byte types are
946+
// 32-bit aligned even though the MAX_ALIGN (for types 9+ bytes) is 16
947+
// (except for f80 which is align 4 on Mingw, Linux, and BSDs--but align 16 on MSVC and Darwin)
948+
// https://llvm.org/doxygen/ARMTargetMachine_8cpp.html#adb29b487708f0dc2a940345b68649270
949+
// https://llvm.org/doxygen/AArch64TargetMachine_8cpp.html#a003a58caf135efbf7273c5ed84e700d7
950+
// https://llvm.org/doxygen/X86TargetMachine_8cpp.html#aefdbcd6131ef195da070cef7fdaf0532
951+
// 32-bit alignment is weird
942952
if (alignm == 8)
943953
alignm = 4;
944954
# endif

0 commit comments

Comments
 (0)