Skip to content

Commit 03f1722

Browse files
committed
make arrays follow alignment as well.
1 parent 06ed45f commit 03f1722

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

src/array.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ size_t jl_arr_xtralloc_limit = 0;
5454
#define MAXINTVAL (((size_t)-1)>>1)
5555

5656
static jl_array_t *_new_array_(jl_value_t *atype, uint32_t ndims, size_t *dims,
57-
int isunboxed, int elsz)
57+
int isunboxed, int elsz, int elalign)
5858
{
5959
jl_ptls_t ptls = jl_get_ptls_states();
6060
size_t i, tot, nel=1;
@@ -89,11 +89,11 @@ static jl_array_t *_new_array_(jl_value_t *atype, uint32_t ndims, size_t *dims,
8989
int tsz = JL_ARRAY_ALIGN(sizeof(jl_array_t) + ndimwords*sizeof(size_t), JL_CACHE_BYTE_ALIGNMENT);
9090
if (tot <= ARRAY_INLINE_NBYTES) {
9191
if (isunboxed && elsz >= 4)
92-
tsz = JL_ARRAY_ALIGN(tsz, JL_SMALL_BYTE_ALIGNMENT); // align data area
92+
tsz = JL_ARRAY_ALIGN(tsz, elalign); // align data area
9393
size_t doffs = tsz;
9494
tsz += tot;
9595
tsz = JL_ARRAY_ALIGN(tsz, JL_SMALL_BYTE_ALIGNMENT); // align whole object
96-
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, JL_SMALL_BYTE_ALIGNMENT, atype);
96+
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, elalign, atype);
9797
// No allocation or safepoint allowed after this
9898
a->flags.how = 0;
9999
data = (char*)a + doffs;
@@ -123,6 +123,7 @@ static jl_array_t *_new_array_(jl_value_t *atype, uint32_t ndims, size_t *dims,
123123
a->flags.ndims = ndims;
124124
a->flags.ptrarray = !isunboxed;
125125
a->elsize = elsz;
126+
a->elalign = elalign;
126127
a->flags.isshared = 0;
127128
a->flags.isaligned = 1;
128129
a->offset = 0;
@@ -141,18 +142,20 @@ static jl_array_t *_new_array_(jl_value_t *atype, uint32_t ndims, size_t *dims,
141142

142143
static inline jl_array_t *_new_array(jl_value_t *atype, uint32_t ndims, size_t *dims)
143144
{
144-
int isunboxed=0, elsz=sizeof(void*);
145+
int isunboxed=0, elsz=sizeof(void*), elalign=JL_SMALL_BYTE_ALIGNMENT;
145146
jl_value_t *el_type = jl_tparam0(atype);
146147
isunboxed = store_unboxed(el_type);
147-
if (isunboxed)
148+
if (isunboxed) {
148149
elsz = jl_datatype_size(el_type);
149-
return _new_array_(atype, ndims, dims, isunboxed, elsz);
150+
elalign =jl_datatype_size(el_type);
151+
}
152+
return _new_array_(atype, ndims, dims, isunboxed, elsz, elalign);
150153
}
151154

152155
jl_array_t *jl_new_array_for_deserialization(jl_value_t *atype, uint32_t ndims, size_t *dims,
153-
int isunboxed, int elsz)
156+
int isunboxed, int elsz, int elalign)
154157
{
155-
return _new_array_(atype, ndims, dims, isunboxed, elsz);
158+
return _new_array_(atype, ndims, dims, isunboxed, elsz, elalign);
156159
}
157160

158161
#ifndef NDEBUG
@@ -194,6 +197,7 @@ JL_DLLEXPORT jl_array_t *jl_reshape_array(jl_value_t *atype, jl_array_t *data,
194197
if (!data->flags.ptrarray) {
195198
a->elsize = jl_datatype_size(el_type);
196199
unsigned align = jl_datatype_align(el_type);
200+
a->elalign = align;
197201
jl_value_t *ownerty = jl_typeof(owner);
198202
unsigned oldalign = (ownerty == (jl_value_t*)jl_string_type ? 1 :
199203
jl_datatype_align(jl_tparam0(ownerty)));
@@ -205,6 +209,7 @@ JL_DLLEXPORT jl_array_t *jl_reshape_array(jl_value_t *atype, jl_array_t *data,
205209
}
206210
else {
207211
a->elsize = sizeof(void*);
212+
a->elalign = sizeof(void*);
208213
a->flags.ptrarray = 1;
209214
}
210215

@@ -258,6 +263,7 @@ JL_DLLEXPORT jl_array_t *jl_string_to_array(jl_value_t *str)
258263
a->data = jl_string_data(str);
259264
a->flags.isaligned = 0;
260265
a->elsize = 1;
266+
a->elalign = 0;
261267
a->flags.ptrarray = 0;
262268
jl_array_data_owner(a) = str;
263269
a->flags.how = 3;
@@ -294,14 +300,15 @@ JL_DLLEXPORT jl_array_t *jl_ptr_to_array_1d(jl_value_t *atype, void *data,
294300

295301
int ndimwords = jl_array_ndimwords(1);
296302
int tsz = JL_ARRAY_ALIGN(sizeof(jl_array_t) + ndimwords*sizeof(size_t), JL_CACHE_BYTE_ALIGNMENT);
297-
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, JL_CACHE_BYTE_ALIGNMENT, atype);
303+
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, align, atype);
298304
// No allocation or safepoint allowed after this
299305
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
300306
a->data = data;
301307
#ifdef STORE_ARRAY_LEN
302308
a->length = nel;
303309
#endif
304310
a->elsize = elsz;
311+
a->elalign = align;
305312
a->flags.ptrarray = !isunboxed;
306313
a->flags.ndims = 1;
307314
a->flags.isshared = 1;
@@ -357,14 +364,15 @@ JL_DLLEXPORT jl_array_t *jl_ptr_to_array(jl_value_t *atype, void *data,
357364

358365
int ndimwords = jl_array_ndimwords(ndims);
359366
int tsz = JL_ARRAY_ALIGN(sizeof(jl_array_t) + ndimwords*sizeof(size_t), JL_CACHE_BYTE_ALIGNMENT);
360-
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, JL_CACHE_BYTE_ALIGNMENT, atype);
367+
a = (jl_array_t*)jl_gc_alloc(ptls, tsz, align, atype);
361368
// No allocation or safepoint allowed after this
362369
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
363370
a->data = data;
364371
#ifdef STORE_ARRAY_LEN
365372
a->length = nel;
366373
#endif
367374
a->elsize = elsz;
375+
a->elalign = align;
368376
a->flags.ptrarray = !isunboxed;
369377
a->flags.ndims = ndims;
370378
a->offset = 0;
@@ -938,8 +946,9 @@ JL_DLLEXPORT void jl_array_sizehint(jl_array_t *a, size_t sz)
938946
JL_DLLEXPORT jl_array_t *jl_array_copy(jl_array_t *ary)
939947
{
940948
size_t elsz = ary->elsize;
949+
int elalign = ary->elalign;
941950
jl_array_t *new_ary = _new_array_(jl_typeof(ary), jl_array_ndims(ary),
942-
&ary->nrows, !ary->flags.ptrarray, elsz);
951+
&ary->nrows, !ary->flags.ptrarray, elsz, elalign);
943952
memcpy(new_ary->data, ary->data, jl_array_len(ary) * elsz);
944953
return new_ary;
945954
}

src/dump.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,11 +860,13 @@ static void jl_serialize_value_(jl_serializer_state *s, jl_value_t *v, int as_li
860860
if (ar->flags.ndims == 1 && ar->elsize < 128) {
861861
writetag(s->s, (jl_value_t*)Array1d_tag);
862862
write_uint8(s->s, (ar->flags.ptrarray<<7) | (ar->elsize & 0x7f));
863+
write_uint8(s->s, ar->elalign & 0x7f);
863864
}
864865
else {
865866
writetag(s->s, (jl_value_t*)jl_array_type);
866867
write_uint16(s->s, ar->flags.ndims);
867868
write_uint16(s->s, (ar->flags.ptrarray<<15) | (ar->elsize & 0x7fff));
869+
write_uint16(s->s, ar->elalign);
868870
}
869871
for (i=0; i < ar->flags.ndims; i++)
870872
jl_serialize_value(s, jl_box_long(jl_array_dim(ar,i)));
@@ -1635,18 +1637,22 @@ static jl_value_t *jl_deserialize_value_array(jl_serializer_state *s, jl_value_t
16351637
{
16361638
int usetable = (s->mode != MODE_AST);
16371639
int16_t i, ndims;
1638-
int isunboxed, elsize;
1640+
int isunboxed, elsize, elalign;
16391641
if (vtag == (jl_value_t*)Array1d_tag) {
16401642
ndims = 1;
16411643
elsize = read_uint8(s->s);
1644+
elalign = read_uint8(s->s);
16421645
isunboxed = !(elsize >> 7);
16431646
elsize = elsize & 0x7f;
1647+
elalign = elalign & 0x7f;
16441648
}
16451649
else {
16461650
ndims = read_uint16(s->s);
16471651
elsize = read_uint16(s->s);
1652+
elalign = read_uint16(s->s);
16481653
isunboxed = !(elsize >> 15);
16491654
elsize = elsize & 0x7fff;
1655+
elalign = elalign & 0x7fff;
16501656
}
16511657
uintptr_t pos = backref_list.len;
16521658
if (usetable)
@@ -1655,7 +1661,7 @@ static jl_value_t *jl_deserialize_value_array(jl_serializer_state *s, jl_value_t
16551661
for (i = 0; i < ndims; i++) {
16561662
dims[i] = jl_unbox_long(jl_deserialize_value(s, NULL));
16571663
}
1658-
jl_array_t *a = jl_new_array_for_deserialization((jl_value_t*)NULL, ndims, dims, isunboxed, elsize);
1664+
jl_array_t *a = jl_new_array_for_deserialization((jl_value_t*)NULL, ndims, dims, isunboxed, elsize, elalign);
16591665
if (usetable)
16601666
backref_list.items[pos] = a;
16611667
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
@@ -148,6 +148,7 @@ JL_EXTENSION typedef struct {
148148
#endif
149149
jl_array_flags_t flags;
150150
uint16_t elsize;
151+
uint16_t elalign;
151152
uint32_t offset; // for 1-d only. does not need to get big.
152153
size_t nrows;
153154
union {

src/julia_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ jl_datatype_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)