Skip to content

Commit ca54c9a

Browse files
authored
flambda-backend: Determine unboxed int32 array length using custom_ops index (#2252)
* squash to fix github diff * Fix merge * add custom ops size to config * run autoconf * revert const_symbol_offset changes * fix negative offsets * static assert message
1 parent 48884b1 commit ca54c9a

File tree

12 files changed

+62
-56
lines changed

12 files changed

+62
-56
lines changed

Makefile.config.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ WITH_CPP_MANGLING=@cpp_mangling@
218218
WITH_PROFINFO=@profinfo@
219219
PROFINFO_WIDTH=@profinfo_width@
220220
HEADER_RESERVED_BITS=@reserved_header_bits@
221+
CUSTOM_OPS_STRUCT_SIZE=@custom_ops_struct_size@
221222
LIBUNWIND_AVAILABLE=@libunwind_available@
222223
LIBUNWIND_INCLUDE_FLAGS=@libunwind_include_flags@
223224
LIBUNWIND_LINK_FLAGS=@libunwind_link_flags@

configure

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ ostype="Unix"
6262
SO="so"
6363
toolchain="cc"
6464
reserved_header_bits=0
65+
custom_ops_struct_size=64
6566
instrumented_runtime=false
6667
instrumented_runtime_libs=""
6768
bootstrapping_flexdll=false
@@ -204,6 +205,7 @@ AC_SUBST([install_bytecode_programs])
204205
AC_SUBST([install_source_artifacts])
205206
AC_SUBST([install_ocamlnat])
206207
AC_SUBST([reserved_header_bits])
208+
AC_SUBST([custom_ops_struct_size])
207209
AC_SUBST([frame_pointers])
208210
AC_SUBST([cpp_mangling])
209211
AC_SUBST([flambda])
@@ -2199,6 +2201,7 @@ AS_IF([test x"$enable_naked_pointers_checker" = "xyes" ],
21992201
OCAML_MMAP_SUPPORTS_HUGE_PAGES
22002202

22012203
AC_DEFINE_UNQUOTED([HEADER_RESERVED_BITS], [$reserved_header_bits])
2204+
AC_DEFINE_UNQUOTED([CUSTOM_OPS_STRUCT_SIZE], [$custom_ops_struct_size])
22022205

22032206
AS_IF([test x"$enable_installing_bytecode_programs" = "xno"],
22042207
[install_bytecode_programs=false],

runtime/array.c

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,23 @@ static uintnat unboxed_array_deserialize(void* dst)
5555
// the int32 unboxed arrays, care needs to be taken with the last word
5656
// when the array is of odd length -- this is not currently initialized.
5757

58-
CAMLexport struct custom_operations caml_unboxed_int32_odd_array_ops = {
59-
"_unboxed_int32_odd_array",
60-
custom_finalize_default,
61-
no_polymorphic_compare,
62-
no_polymorphic_hash,
63-
unboxed_array_serialize,
64-
unboxed_array_deserialize,
65-
custom_compare_ext_default,
66-
custom_fixed_length_default
67-
};
68-
69-
CAMLexport struct custom_operations caml_unboxed_int32_even_array_ops = {
70-
"_unboxed_int32_even_array",
71-
custom_finalize_default,
72-
no_polymorphic_compare,
73-
no_polymorphic_hash,
74-
unboxed_array_serialize,
75-
unboxed_array_deserialize,
76-
custom_compare_ext_default,
77-
custom_fixed_length_default
58+
CAMLexport struct custom_operations caml_unboxed_int32_array_ops[2] = {
59+
{ "_unboxed_int32_even_array",
60+
custom_finalize_default,
61+
no_polymorphic_compare,
62+
no_polymorphic_hash,
63+
unboxed_array_serialize,
64+
unboxed_array_deserialize,
65+
custom_compare_ext_default,
66+
custom_fixed_length_default },
67+
{ "_unboxed_int32_odd_array",
68+
custom_finalize_default,
69+
no_polymorphic_compare,
70+
no_polymorphic_hash,
71+
unboxed_array_serialize,
72+
unboxed_array_deserialize,
73+
custom_compare_ext_default,
74+
custom_fixed_length_default },
7875
};
7976

8077
CAMLexport struct custom_operations caml_unboxed_int64_array_ops = {
@@ -481,14 +478,9 @@ CAMLprim value caml_make_unboxed_int32_vect(value len)
481478
mlsize_t num_elements = Long_val(len);
482479
/* [num_fields] does not include the custom operations field. */
483480
mlsize_t num_fields = (num_elements + 1) / 2;
484-
struct custom_operations* ops;
485-
486-
if (num_elements % 2 == 0)
487-
ops = &caml_unboxed_int32_even_array_ops;
488-
else
489-
ops = &caml_unboxed_int32_odd_array_ops;
490481

491-
return caml_alloc_custom(ops, num_fields * sizeof(value), 0, 0);
482+
return caml_alloc_custom(&caml_unboxed_int32_array_ops[num_elements % 2],
483+
num_fields * sizeof(value), 0, 0);
492484
}
493485

494486
CAMLprim value caml_make_unboxed_int64_vect(value len)

runtime/caml/custom.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ struct custom_operations {
3636
int (*compare_ext)(value v1, value v2);
3737
const struct custom_fixed_length* fixed_length;
3838
};
39+
_Static_assert(sizeof(struct custom_operations) == CUSTOM_OPS_STRUCT_SIZE,
40+
"Unexpected CUSTOM_OPS_STRUCT_SIZE");
3941

4042
#define custom_finalize_default NULL
4143
#define custom_compare_default NULL

runtime/caml/m.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878

7979
#undef HEADER_RESERVED_BITS
8080

81+
#undef CUSTOM_OPS_STRUCT_SIZE
82+
8183
#undef ASM_CFI_SUPPORTED
8284

8385
#undef WITH_FRAME_POINTERS

runtime4/array.c

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,23 @@ static uintnat unboxed_array_deserialize(void* dst)
5555
// the int32 unboxed arrays, care needs to be taken with the last word
5656
// when the array is of odd length -- this is not currently initialized.
5757

58-
CAMLexport struct custom_operations caml_unboxed_int32_odd_array_ops = {
59-
"_unboxed_int32_odd_array",
60-
custom_finalize_default,
61-
no_polymorphic_compare,
62-
no_polymorphic_hash,
63-
unboxed_array_serialize,
64-
unboxed_array_deserialize,
65-
custom_compare_ext_default,
66-
custom_fixed_length_default
67-
};
68-
69-
CAMLexport struct custom_operations caml_unboxed_int32_even_array_ops = {
70-
"_unboxed_int32_even_array",
71-
custom_finalize_default,
72-
no_polymorphic_compare,
73-
no_polymorphic_hash,
74-
unboxed_array_serialize,
75-
unboxed_array_deserialize,
76-
custom_compare_ext_default,
77-
custom_fixed_length_default
58+
CAMLexport struct custom_operations caml_unboxed_int32_array_ops[2] = {
59+
{ "_unboxed_int32_even_array",
60+
custom_finalize_default,
61+
no_polymorphic_compare,
62+
no_polymorphic_hash,
63+
unboxed_array_serialize,
64+
unboxed_array_deserialize,
65+
custom_compare_ext_default,
66+
custom_fixed_length_default },
67+
{ "_unboxed_int32_odd_array",
68+
custom_finalize_default,
69+
no_polymorphic_compare,
70+
no_polymorphic_hash,
71+
unboxed_array_serialize,
72+
unboxed_array_deserialize,
73+
custom_compare_ext_default,
74+
custom_fixed_length_default },
7875
};
7976

8077
CAMLexport struct custom_operations caml_unboxed_int64_array_ops = {
@@ -403,14 +400,9 @@ CAMLprim value caml_make_unboxed_int32_vect(value len)
403400
mlsize_t num_elements = Long_val(len);
404401
/* [num_fields] does not include the custom operations field. */
405402
mlsize_t num_fields = (num_elements + 1) / 2;
406-
struct custom_operations* ops;
407-
408-
if (num_elements % 2 == 0)
409-
ops = &caml_unboxed_int32_even_array_ops;
410-
else
411-
ops = &caml_unboxed_int32_odd_array_ops;
412403

413-
return caml_alloc_custom(ops, num_fields * sizeof(value), 0, 0);
404+
return caml_alloc_custom(&caml_unboxed_int32_array_ops[num_elements % 2],
405+
num_fields * sizeof(value), 0, 0);
414406
}
415407

416408
CAMLprim value caml_make_unboxed_int64_vect(value len)

runtime4/caml/custom.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ struct custom_operations {
3939
int (*compare_ext)(value v1, value v2);
4040
const struct custom_fixed_length* fixed_length;
4141
};
42+
_Static_assert(sizeof(struct custom_operations) == CUSTOM_OPS_STRUCT_SIZE,
43+
"Unexpected CUSTOM_OPS_STRUCT_SIZE");
4244

4345
#define custom_finalize_default NULL
4446
#define custom_compare_default NULL

runtime4/caml/m.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
supports word-aligned 64-bit integers. Leave undefined if
7777
64-bit integers are not supported. */
7878

79+
#undef CUSTOM_OPS_STRUCT_SIZE
80+
7981
#undef PROFINFO_WIDTH
8082

8183
#undef ASM_CFI_SUPPORTED

utils/config.fixed.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ let asm = boot_cannot_call "the assembler"
6464
let asm_cfi_supported = false
6565
let with_frame_pointers = false
6666
let reserved_header_bits = 0
67+
let custom_ops_struct_size = 64
6768
let ext_exe = ".ex_The boot compiler should not be using Config.ext_exe"
6869
let ext_obj = ".o_The boot compiler cannot process C objects"
6970
let ext_asm = ".s_The boot compiler should not be using Config.ext_asm"

utils/config.generated.ml.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ let asm_cfi_supported = @asm_cfi_supported@
100100
let with_frame_pointers = @frame_pointers@
101101
let with_cpp_mangling = @cpp_mangling@
102102
let reserved_header_bits = @reserved_header_bits@
103+
let custom_ops_struct_size = @custom_ops_struct_size@
103104

104105
let ext_exe = {@QS@|@exeext@|@QS@}
105106
let ext_obj = "." ^ {@QS@|@OBJEXT@|@QS@}

utils/config.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ val with_cmm_invariants : bool
235235
val reserved_header_bits : int
236236
(** How many bits of a block's header are reserved *)
237237

238+
val custom_ops_struct_size : int
239+
(** Size in bytes of the custom operations structure. *)
240+
238241
val flat_float_array : bool
239242
(** Whether the compiler and runtime automagically flatten float
240243
arrays *)

0 commit comments

Comments
 (0)