Skip to content

Commit dd37d6e

Browse files
committed
make arrays follow alignment as well.
1 parent 6170f82 commit dd37d6e

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/array.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ size_t jl_arr_xtralloc_limit = 0;
4848
#define MAXINTVAL (((size_t)-1)>>1)
4949

5050
static jl_array_t *_new_array_(jl_value_t *atype, uint32_t ndims, size_t *dims,
51-
int isunboxed, int elsz)
51+
int isunboxed, int elsz, int elalign)
5252
{
5353
jl_ptls_t ptls = jl_get_ptls_states();
5454
size_t i, tot, nel=1;
@@ -87,11 +87,11 @@ static jl_array_t *_new_array_(jl_value_t *atype, uint32_t ndims, size_t *dims,
8787
int tsz = JL_ARRAY_ALIGN(sizeof(jl_array_t) + ndimwords*sizeof(size_t), JL_CACHE_BYTE_ALIGNMENT);
8888
if (tot <= ARRAY_INLINE_NBYTES) {
8989
if (isunboxed && elsz >= 4)
90-
tsz = JL_ARRAY_ALIGN(tsz, JL_SMALL_BYTE_ALIGNMENT); // align data area
90+
tsz = JL_ARRAY_ALIGN(tsz, elalign); // align data area
9191
size_t doffs = tsz;
9292
tsz += tot;
9393
tsz = JL_ARRAY_ALIGN(tsz, JL_SMALL_BYTE_ALIGNMENT); // align whole object
94-
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, JL_SMALL_BYTE_ALIGNMENT, atype);
94+
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, elalign, atype);
9595
// No allocation or safepoint allowed after this
9696
a->flags.how = 0;
9797
data = (char*)a + doffs;
@@ -147,14 +147,13 @@ static inline jl_array_t *_new_array(jl_value_t *atype, uint32_t ndims, size_t *
147147
elsz = sizeof(void*);
148148
al = elsz;
149149
}
150-
151-
return _new_array_(atype, ndims, dims, isunboxed, elsz);
150+
return _new_array_(atype, ndims, dims, isunboxed, elsz, al);
152151
}
153152

154153
jl_array_t *jl_new_array_for_deserialization(jl_value_t *atype, uint32_t ndims, size_t *dims,
155-
int isunboxed, int elsz)
154+
int isunboxed, int elsz, int elalign)
156155
{
157-
return _new_array_(atype, ndims, dims, isunboxed, elsz);
156+
return _new_array_(atype, ndims, dims, isunboxed, elsz, elalign);
158157
}
159158

160159
#ifndef JL_NDEBUG
@@ -305,7 +304,7 @@ JL_DLLEXPORT jl_array_t *jl_ptr_to_array_1d(jl_value_t *atype, void *data,
305304

306305
int ndimwords = jl_array_ndimwords(1);
307306
int tsz = JL_ARRAY_ALIGN(sizeof(jl_array_t) + ndimwords*sizeof(size_t), JL_CACHE_BYTE_ALIGNMENT);
308-
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, JL_CACHE_BYTE_ALIGNMENT, atype);
307+
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, align, atype);
309308
// No allocation or safepoint allowed after this
310309
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
311310
a->data = data;
@@ -371,7 +370,7 @@ JL_DLLEXPORT jl_array_t *jl_ptr_to_array(jl_value_t *atype, void *data,
371370

372371
int ndimwords = jl_array_ndimwords(ndims);
373372
int tsz = JL_ARRAY_ALIGN(sizeof(jl_array_t) + ndimwords*sizeof(size_t), JL_CACHE_BYTE_ALIGNMENT);
374-
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, JL_CACHE_BYTE_ALIGNMENT, atype);
373+
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, align, atype);
375374
// No allocation or safepoint allowed after this
376375
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
377376
a->data = data;
@@ -1042,9 +1041,10 @@ JL_DLLEXPORT void jl_array_sizehint(jl_array_t *a, size_t sz)
10421041
JL_DLLEXPORT jl_array_t *jl_array_copy(jl_array_t *ary)
10431042
{
10441043
size_t elsz = ary->elsize;
1044+
size_t elalign = ary->flags.ptrarray ? sizeof(void*) : jl_datatype_align(jl_tparam0(jl_typeof(ary)));
10451045
size_t len = jl_array_len(ary);
10461046
jl_array_t *new_ary = _new_array_(jl_typeof(ary), jl_array_ndims(ary),
1047-
&ary->nrows, !ary->flags.ptrarray, elsz);
1047+
&ary->nrows, !ary->flags.ptrarray, elsz, elalign);
10481048
memcpy(new_ary->data, ary->data, len * elsz);
10491049
// ensure isbits union arrays copy their selector bytes correctly
10501050
if (!ary->flags.ptrarray && jl_is_uniontype(jl_tparam0(jl_typeof(ary)))) {

src/dump.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,17 @@ static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_li
574574
}
575575
else if (jl_is_array(v)) {
576576
jl_array_t *ar = (jl_array_t*)v;
577+
size_t elalign = ar->flags.ptrarray ? sizeof(void*) : jl_datatype_align(jl_tparam0(jl_typeof(ar)));
577578
if (ar->flags.ndims == 1 && ar->elsize < 128) {
578579
writetag(s->s, (jl_value_t*)Array1d_tag);
579580
write_uint8(s->s, (ar->flags.ptrarray<<7) | (ar->elsize & 0x7f));
581+
write_uint8(s->s, elalign);
580582
}
581583
else {
582584
writetag(s->s, (jl_value_t*)jl_array_type);
583585
write_uint16(s->s, ar->flags.ndims);
584586
write_uint16(s->s, (ar->flags.ptrarray<<15) | (ar->elsize & 0x7fff));
587+
write_uint16(s->s, elalign);
585588
}
586589
for (i=0; i < ar->flags.ndims; i++)
587590
jl_serialize_value(s, jl_box_long(jl_array_dim(ar,i)));
@@ -1329,18 +1332,22 @@ static jl_value_t *jl_deserialize_value_array(jl_serializer_state *s, jl_value_t
13291332
{
13301333
int usetable = (s->mode != MODE_AST);
13311334
int16_t i, ndims;
1332-
int isunboxed, elsize;
1335+
int isunboxed, elsize, elalign;
13331336
if (vtag == (jl_value_t*)Array1d_tag) {
13341337
ndims = 1;
13351338
elsize = read_uint8(s->s);
1339+
elalign = read_uint8(s->s);
13361340
isunboxed = !(elsize >> 7);
13371341
elsize = elsize & 0x7f;
1342+
elalign = elalign;
13381343
}
13391344
else {
13401345
ndims = read_uint16(s->s);
13411346
elsize = read_uint16(s->s);
1347+
elalign = read_uint16(s->s);
13421348
isunboxed = !(elsize >> 15);
13431349
elsize = elsize & 0x7fff;
1350+
elalign = elalign;
13441351
}
13451352
uintptr_t pos = backref_list.len;
13461353
if (usetable)
@@ -1349,7 +1356,7 @@ static jl_value_t *jl_deserialize_value_array(jl_serializer_state *s, jl_value_t
13491356
for (i = 0; i < ndims; i++) {
13501357
dims[i] = jl_unbox_long(jl_deserialize_value(s, NULL));
13511358
}
1352-
jl_array_t *a = jl_new_array_for_deserialization((jl_value_t*)NULL, ndims, dims, isunboxed, elsize);
1359+
jl_array_t *a = jl_new_array_for_deserialization((jl_value_t*)NULL, ndims, dims, isunboxed, elsize, elalign);
13531360
if (usetable)
13541361
backref_list.items[pos] = a;
13551362
jl_value_t *aty = jl_deserialize_value(s, &jl_astaggedvalue(a)->type);

src/julia_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ JL_DLLEXPORT jl_value_t *jl_argument_datatype(jl_value_t *argt);
500500
jl_value_t *jl_nth_slot_type(jl_value_t *sig, size_t i);
501501
void jl_compute_field_offsets(jl_datatype_t *st);
502502
jl_array_t *jl_new_array_for_deserialization(jl_value_t *atype, uint32_t ndims, size_t *dims,
503-
int isunboxed, int elsz);
503+
int isunboxed, int elsz, int elalign);
504504
void jl_module_run_initializer(jl_module_t *m);
505505
extern jl_array_t *jl_module_init_order;
506506
extern union jl_typemap_t jl_cfunction_list;

0 commit comments

Comments
 (0)