Skip to content

Commit 317561d

Browse files
committed
make arrays follow alignment as well.
1 parent 939941d commit 317561d

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

src/array.c

Lines changed: 16 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;
@@ -122,6 +122,7 @@ static jl_array_t *_new_array_(jl_value_t *atype, uint32_t ndims, size_t *dims,
122122
a->flags.ndims = ndims;
123123
a->flags.ptrarray = !isunboxed;
124124
a->elsize = elsz;
125+
a->elalign = elalign;
125126
a->flags.isshared = 0;
126127
a->flags.isaligned = 1;
127128
a->offset = 0;
@@ -147,14 +148,13 @@ static inline jl_array_t *_new_array(jl_value_t *atype, uint32_t ndims, size_t *
147148
elsz = sizeof(void*);
148149
al = elsz;
149150
}
150-
151-
return _new_array_(atype, ndims, dims, isunboxed, elsz);
151+
return _new_array_(atype, ndims, dims, isunboxed, elsz, al);
152152
}
153153

154154
jl_array_t *jl_new_array_for_deserialization(jl_value_t *atype, uint32_t ndims, size_t *dims,
155-
int isunboxed, int elsz)
155+
int isunboxed, int elsz, int elalign)
156156
{
157-
return _new_array_(atype, ndims, dims, isunboxed, elsz);
157+
return _new_array_(atype, ndims, dims, isunboxed, elsz, elalign);
158158
}
159159

160160
#ifndef JL_NDEBUG
@@ -197,6 +197,7 @@ JL_DLLEXPORT jl_array_t *jl_reshape_array(jl_value_t *atype, jl_array_t *data,
197197
assert(isboxed == data->flags.ptrarray);
198198
if (!isboxed) {
199199
a->elsize = elsz;
200+
a->elalign = align;
200201
jl_value_t *ownerty = jl_typeof(owner);
201202
size_t oldelsz = 0, oldalign = 0;
202203
if (ownerty == (jl_value_t*)jl_string_type) {
@@ -213,6 +214,7 @@ JL_DLLEXPORT jl_array_t *jl_reshape_array(jl_value_t *atype, jl_array_t *data,
213214
}
214215
else {
215216
a->elsize = sizeof(void*);
217+
a->elalign = sizeof(void*);
216218
a->flags.ptrarray = 1;
217219
}
218220

@@ -266,6 +268,7 @@ JL_DLLEXPORT jl_array_t *jl_string_to_array(jl_value_t *str)
266268
a->data = jl_string_data(str);
267269
a->flags.isaligned = 0;
268270
a->elsize = 1;
271+
a->elalign = 0;
269272
a->flags.ptrarray = 0;
270273
jl_array_data_owner(a) = str;
271274
a->flags.how = 3;
@@ -305,14 +308,15 @@ JL_DLLEXPORT jl_array_t *jl_ptr_to_array_1d(jl_value_t *atype, void *data,
305308

306309
int ndimwords = jl_array_ndimwords(1);
307310
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);
311+
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, align, atype);
309312
// No allocation or safepoint allowed after this
310313
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
311314
a->data = data;
312315
#ifdef STORE_ARRAY_LEN
313316
a->length = nel;
314317
#endif
315318
a->elsize = elsz;
319+
a->elalign = align;
316320
a->flags.ptrarray = !isunboxed;
317321
a->flags.ndims = 1;
318322
a->flags.isshared = 1;
@@ -371,14 +375,15 @@ JL_DLLEXPORT jl_array_t *jl_ptr_to_array(jl_value_t *atype, void *data,
371375

372376
int ndimwords = jl_array_ndimwords(ndims);
373377
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);
378+
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, align, atype);
375379
// No allocation or safepoint allowed after this
376380
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
377381
a->data = data;
378382
#ifdef STORE_ARRAY_LEN
379383
a->length = nel;
380384
#endif
381385
a->elsize = elsz;
386+
a->elalign = align;
382387
a->flags.ptrarray = !isunboxed;
383388
a->flags.ndims = ndims;
384389
a->offset = 0;
@@ -1042,9 +1047,10 @@ JL_DLLEXPORT void jl_array_sizehint(jl_array_t *a, size_t sz)
10421047
JL_DLLEXPORT jl_array_t *jl_array_copy(jl_array_t *ary)
10431048
{
10441049
size_t elsz = ary->elsize;
1050+
size_t elalign = ary->elalign;
10451051
size_t len = jl_array_len(ary);
10461052
jl_array_t *new_ary = _new_array_(jl_typeof(ary), jl_array_ndims(ary),
1047-
&ary->nrows, !ary->flags.ptrarray, elsz);
1053+
&ary->nrows, !ary->flags.ptrarray, elsz, elalign);
10481054
memcpy(new_ary->data, ary->data, len * elsz);
10491055
// ensure isbits union arrays copy their selector bytes correctly
10501056
if (!ary->flags.ptrarray && jl_is_uniontype(jl_tparam0(jl_typeof(ary)))) {

src/dump.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,11 +577,13 @@ static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_li
577577
if (ar->flags.ndims == 1 && ar->elsize < 128) {
578578
writetag(s->s, (jl_value_t*)Array1d_tag);
579579
write_uint8(s->s, (ar->flags.ptrarray<<7) | (ar->elsize & 0x7f));
580+
write_uint8(s->s, ar->elalign & 0x7f);
580581
}
581582
else {
582583
writetag(s->s, (jl_value_t*)jl_array_type);
583584
write_uint16(s->s, ar->flags.ndims);
584585
write_uint16(s->s, (ar->flags.ptrarray<<15) | (ar->elsize & 0x7fff));
586+
write_uint16(s->s, ar->elalign);
585587
}
586588
for (i=0; i < ar->flags.ndims; i++)
587589
jl_serialize_value(s, jl_box_long(jl_array_dim(ar,i)));
@@ -1329,18 +1331,22 @@ static jl_value_t *jl_deserialize_value_array(jl_serializer_state *s, jl_value_t
13291331
{
13301332
int usetable = (s->mode != MODE_AST);
13311333
int16_t i, ndims;
1332-
int isunboxed, elsize;
1334+
int isunboxed, elsize, elalign;
13331335
if (vtag == (jl_value_t*)Array1d_tag) {
13341336
ndims = 1;
13351337
elsize = read_uint8(s->s);
1338+
elalign = read_uint8(s->s);
13361339
isunboxed = !(elsize >> 7);
13371340
elsize = elsize & 0x7f;
1341+
elalign = elalign & 0x7f;
13381342
}
13391343
else {
13401344
ndims = read_uint16(s->s);
13411345
elsize = read_uint16(s->s);
1346+
elalign = read_uint16(s->s);
13421347
isunboxed = !(elsize >> 15);
13431348
elsize = elsize & 0x7fff;
1349+
elalign = elalign & 0x7fff;
13441350
}
13451351
uintptr_t pos = backref_list.len;
13461352
if (usetable)
@@ -1349,7 +1355,7 @@ static jl_value_t *jl_deserialize_value_array(jl_serializer_state *s, jl_value_t
13491355
for (i = 0; i < ndims; i++) {
13501356
dims[i] = jl_unbox_long(jl_deserialize_value(s, NULL));
13511357
}
1352-
jl_array_t *a = jl_new_array_for_deserialization((jl_value_t*)NULL, ndims, dims, isunboxed, elsize);
1358+
jl_array_t *a = jl_new_array_for_deserialization((jl_value_t*)NULL, ndims, dims, isunboxed, elsize, elalign);
13531359
if (usetable)
13541360
backref_list.items[pos] = a;
13551361
jl_value_t *aty = jl_deserialize_value(s, &jl_astaggedvalue(a)->type);

src/julia.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ JL_EXTENSION typedef struct {
155155
#endif
156156
jl_array_flags_t flags;
157157
uint16_t elsize;
158+
uint16_t elalign;
158159
uint32_t offset; // for 1-d only. does not need to get big.
159160
size_t nrows;
160161
union {

src/julia_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ JL_DLLEXPORT jl_value_t *jl_argument_datatype(jl_value_t *argt);
488488
jl_value_t *jl_nth_slot_type(jl_value_t *sig, size_t i);
489489
void jl_compute_field_offsets(jl_datatype_t *st);
490490
jl_array_t *jl_new_array_for_deserialization(jl_value_t *atype, uint32_t ndims, size_t *dims,
491-
int isunboxed, int elsz);
491+
int isunboxed, int elsz, int elalign);
492492
void jl_module_run_initializer(jl_module_t *m);
493493
extern jl_array_t *jl_module_init_order;
494494
extern union jl_typemap_t jl_cfunction_list;

0 commit comments

Comments
 (0)