Skip to content

Commit c6b2a7b

Browse files
committed
squash
1 parent 7814eeb commit c6b2a7b

38 files changed

+778
-158
lines changed

backend/cmm_helpers.ml

Lines changed: 94 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,22 @@ let custom_ops_unboxed_int32_odd_array =
10251025
Cconst_int (Config.custom_ops_struct_size, Debuginfo.none) ],
10261026
Debuginfo.none )
10271027

1028+
(* caml_unboxed_float32_array_ops refers to the first element of an array of two
1029+
custom ops. The array index indicates the number of (invalid) tailing
1030+
float32s (0 or 1). *)
1031+
let custom_ops_unboxed_float32_array =
1032+
Cconst_symbol
1033+
(Cmm.global_symbol "caml_unboxed_float32_array_ops", Debuginfo.none)
1034+
1035+
let custom_ops_unboxed_float32_even_array = custom_ops_unboxed_float32_array
1036+
1037+
let custom_ops_unboxed_float32_odd_array =
1038+
Cop
1039+
( Caddi,
1040+
[ custom_ops_unboxed_float32_array;
1041+
Cconst_int (Config.custom_ops_struct_size, Debuginfo.none) ],
1042+
Debuginfo.none )
1043+
10281044
let custom_ops_unboxed_int64_array =
10291045
Cconst_symbol
10301046
(Cmm.global_symbol "caml_unboxed_int64_array_ops", Debuginfo.none)
@@ -1033,7 +1049,8 @@ let custom_ops_unboxed_nativeint_array =
10331049
Cconst_symbol
10341050
(Cmm.global_symbol "caml_unboxed_nativeint_array_ops", Debuginfo.none)
10351051

1036-
let unboxed_int32_array_length arr dbg =
1052+
let unboxed_packed_array_length arr dbg ~custom_ops_base_symbol
1053+
~elements_per_word =
10371054
(* Checking custom_ops is needed to determine if the array contains an odd or
10381055
even number of elements *)
10391056
let res =
@@ -1054,17 +1071,27 @@ let unboxed_int32_array_length arr dbg =
10541071
( VP.create custom_ops_index_var,
10551072
(* compute index into custom ops array *)
10561073
lsr_int
1057-
(sub_int (Cvar custom_ops_var)
1058-
custom_ops_unboxed_int32_array dbg)
1074+
(sub_int (Cvar custom_ops_var) custom_ops_base_symbol dbg)
10591075
(int ~dbg custom_ops_size_log2)
10601076
dbg,
10611077
(* subtract index from length in int32s *)
10621078
sub_int
1063-
(mul_int (Cvar num_words_var) (int ~dbg 2) dbg)
1079+
(mul_int (Cvar num_words_var)
1080+
(int ~dbg elements_per_word)
1081+
dbg)
10641082
(Cvar custom_ops_index_var) dbg ) ) ))
10651083
in
10661084
tag_int res dbg
10671085

1086+
let unboxed_int32_array_length =
1087+
unboxed_packed_array_length
1088+
~custom_ops_base_symbol:custom_ops_unboxed_int32_array ~elements_per_word:2
1089+
1090+
let unboxed_float32_array_length =
1091+
unboxed_packed_array_length
1092+
~custom_ops_base_symbol:custom_ops_unboxed_float32_array
1093+
~elements_per_word:2
1094+
10681095
let unboxed_int64_or_nativeint_array_length arr dbg =
10691096
let res =
10701097
bind "arr" arr (fun arr ->
@@ -1179,24 +1206,31 @@ let sign_extend_32 dbg e =
11791206
[Cop (Clsl, [e; Cconst_int (32, dbg)], dbg); Cconst_int (32, dbg)],
11801207
dbg )
11811208

1182-
let unboxed_int32_array_ref arr index dbg =
1209+
let unboxed_packed_array_ref arr index dbg ~memory_chunk ~elements_per_word =
11831210
bind "arr" arr (fun arr ->
11841211
bind "index" index (fun index ->
11851212
let index =
1186-
(* Need to skip the custom_operations field. We add 2 element
1187-
offsets not 1 since the call to [array_indexing], below, is in
1188-
terms of 32-bit words. Then we multiply the offset by 2 to get 4
1189-
since we are manipulating a tagged int. *)
1190-
add_int index (int ~dbg 4) dbg
1213+
(* Need to skip the custom_operations field. We add
1214+
elements_per_word offsets not 1 since the call to
1215+
[array_indexing], below, is in terms of elements. Then we
1216+
multiply the offset by 2 since we are manipulating a tagged
1217+
int. *)
1218+
add_int index (int ~dbg (elements_per_word * 2)) dbg
11911219
in
11921220
let log2_size_addr = 2 in
1193-
(* N.B. The resulting value will be sign extended by the code
1194-
generated for a [Thirtytwo_signed] load. *)
11951221
Cop
1196-
( mk_load_mut Thirtytwo_signed,
1222+
( mk_load_mut memory_chunk,
11971223
[array_indexing log2_size_addr arr index dbg],
11981224
dbg )))
11991225

1226+
let unboxed_int32_array_ref =
1227+
unboxed_packed_array_ref ~memory_chunk:Thirtytwo_signed ~elements_per_word:2
1228+
1229+
let unboxed_float32_array_ref =
1230+
unboxed_packed_array_ref
1231+
~memory_chunk:(Single { reg = Float32 })
1232+
~elements_per_word:2
1233+
12001234
let unboxed_int64_or_nativeint_array_ref arr index dbg =
12011235
bind "arr" arr (fun arr ->
12021236
bind "index" index (fun index ->
@@ -1207,20 +1241,29 @@ let unboxed_int64_or_nativeint_array_ref arr index dbg =
12071241
in
12081242
int_array_ref arr index dbg))
12091243

1210-
let unboxed_int32_array_set arr ~index ~new_value dbg =
1244+
let unboxed_packed_array_set arr ~index ~new_value dbg ~memory_chunk
1245+
~elements_per_word =
12111246
bind "arr" arr (fun arr ->
12121247
bind "index" index (fun index ->
12131248
bind "new_value" new_value (fun new_value ->
12141249
let index =
1215-
(* See comment in [unboxed_int32_array_ref]. *)
1216-
add_int index (int ~dbg 4) dbg
1250+
(* See comment in [unboxed_packed_array_ref]. *)
1251+
add_int index (int ~dbg (elements_per_word * 2)) dbg
12171252
in
12181253
let log2_size_addr = 2 in
12191254
Cop
1220-
( Cstore (Thirtytwo_signed, Assignment),
1255+
( Cstore (memory_chunk, Assignment),
12211256
[array_indexing log2_size_addr arr index dbg; new_value],
12221257
dbg ))))
12231258

1259+
let unboxed_int32_array_set =
1260+
unboxed_packed_array_set ~memory_chunk:Thirtytwo_signed ~elements_per_word:2
1261+
1262+
let unboxed_float32_array_set =
1263+
unboxed_packed_array_set
1264+
~memory_chunk:(Single { reg = Float32 })
1265+
~elements_per_word:2
1266+
12241267
let unboxed_int64_or_nativeint_array_set arr ~index ~new_value dbg =
12251268
bind "arr" arr (fun arr ->
12261269
bind "index" index (fun index ->
@@ -1258,6 +1301,14 @@ let get_field_unboxed_int32 mutability ~block ~index dbg =
12581301
Cop
12591302
(Cload { memory_chunk; mutability; is_atomic = false }, [field_address], dbg)
12601303

1304+
let get_field_unboxed_float32 mutability ~block ~index dbg =
1305+
let memory_chunk = Single { reg = Float32 } in
1306+
(* CR layouts v5.1: We'll need to vary log2_size_addr to efficiently pack
1307+
* float32s *)
1308+
let field_address = array_indexing log2_size_addr block index dbg in
1309+
Cop
1310+
(Cload { memory_chunk; mutability; is_atomic = false }, [field_address], dbg)
1311+
12611312
let get_field_unboxed_int64_or_nativeint mutability ~block ~index dbg =
12621313
let memory_chunk = Word_int in
12631314
let field_address = array_indexing log2_size_addr block index dbg in
@@ -1280,6 +1331,15 @@ let setfield_unboxed_int32 arr ofs newval dbg =
12801331
[array_indexing log2_size_addr arr ofs dbg; newval],
12811332
dbg ))
12821333

1334+
let setfield_unboxed_float32 arr ofs newval dbg =
1335+
(* CR layouts v5.1: We will need to vary log2_size_addr when float32 fields
1336+
are efficiently packed. *)
1337+
return_unit dbg
1338+
(Cop
1339+
( Cstore (Single { reg = Float32 }, Assignment),
1340+
[array_indexing log2_size_addr arr ofs dbg; newval],
1341+
dbg ))
1342+
12831343
let setfield_unboxed_int64_or_nativeint arr ofs newval dbg =
12841344
return_unit dbg
12851345
(Cop
@@ -3076,9 +3136,7 @@ let arraylength kind arg dbg =
30763136
(* Note: we only support 64 bit targets now, so this is ok for
30773137
Punboxedfloatarray *)
30783138
Cop (Cor, [float_array_length_shifted hdr dbg; Cconst_int (1, dbg)], dbg)
3079-
| Punboxedfloatarray Pfloat32 ->
3080-
(* CR mslater: (float32) unboxed arrays *)
3081-
assert false
3139+
| Punboxedfloatarray Pfloat32 -> unboxed_float32_array_length arg dbg
30823140
| Punboxedintarray Pint64 | Punboxedintarray Pnativeint ->
30833141
unboxed_int64_or_nativeint_array_length arg dbg
30843142
| Punboxedintarray Pint32 -> unboxed_int32_array_length arg dbg
@@ -4006,6 +4064,22 @@ let allocate_unboxed_int32_array ~elements (mode : Lambda.alloc_mode) dbg =
40064064
in
40074065
Cop (Calloc mode, Cconst_natint (header, dbg) :: custom_ops :: payload, dbg)
40084066

4067+
let allocate_unboxed_float32_array ~elements (mode : Lambda.alloc_mode) dbg =
4068+
let header =
4069+
let size = 1 (* custom_ops field *) + ((List.length elements + 1) / 2) in
4070+
match mode with
4071+
| Alloc_heap -> custom_header ~size
4072+
| Alloc_local -> custom_local_header ~size
4073+
in
4074+
let custom_ops =
4075+
(* For odd-length unboxed float32 arrays there are 32 bits spare at the end
4076+
of the block, which are never read. *)
4077+
if List.length elements mod 2 = 0
4078+
then custom_ops_unboxed_float32_even_array
4079+
else custom_ops_unboxed_float32_odd_array
4080+
in
4081+
Cop (Calloc mode, Cconst_natint (header, dbg) :: custom_ops :: elements, dbg)
4082+
40094083
let allocate_unboxed_int64_or_nativeint_array custom_ops ~elements
40104084
(mode : Lambda.alloc_mode) dbg =
40114085
let header =

backend/cmm_helpers.mli

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,11 @@ val atomic_compare_and_set :
958958

959959
val emit_gc_roots_table : symbols:symbol list -> phrase list -> phrase list
960960

961+
(** Allocate a block to hold an unboxed float32 array for the given number of
962+
elements. *)
963+
val allocate_unboxed_float32_array :
964+
elements:Cmm.expression list -> Lambda.alloc_mode -> Debuginfo.t -> expression
965+
961966
(** Allocate a block to hold an unboxed int32 array for the given number of
962967
elements. *)
963968
val allocate_unboxed_int32_array :
@@ -973,13 +978,20 @@ val allocate_unboxed_int64_array :
973978
val allocate_unboxed_nativeint_array :
974979
elements:Cmm.expression list -> Lambda.alloc_mode -> Debuginfo.t -> expression
975980

981+
(** Compute the length of an unboxed float32 array. *)
982+
val unboxed_float32_array_length : expression -> Debuginfo.t -> expression
983+
976984
(** Compute the length of an unboxed int32 array. *)
977985
val unboxed_int32_array_length : expression -> Debuginfo.t -> expression
978986

979987
(** Compute the length of an unboxed int64 or unboxed nativeint array. *)
980988
val unboxed_int64_or_nativeint_array_length :
981989
expression -> Debuginfo.t -> expression
982990

991+
(** Read from an unboxed float32 array (without bounds check). *)
992+
val unboxed_float32_array_ref :
993+
expression -> expression -> Debuginfo.t -> expression
994+
983995
(** Read from an unboxed int32 array (without bounds check). *)
984996
val unboxed_int32_array_ref :
985997
expression -> expression -> Debuginfo.t -> expression
@@ -989,6 +1001,14 @@ val unboxed_int32_array_ref :
9891001
val unboxed_int64_or_nativeint_array_ref :
9901002
expression -> expression -> Debuginfo.t -> expression
9911003

1004+
(** Update an unboxed float32 array (without bounds check). *)
1005+
val unboxed_float32_array_set :
1006+
expression ->
1007+
index:expression ->
1008+
new_value:expression ->
1009+
Debuginfo.t ->
1010+
expression
1011+
9921012
(** Update an unboxed int32 array (without bounds check). *)
9931013
val unboxed_int32_array_set :
9941014
expression ->

middle_end/flambda2/from_lambda/closure_conversion_aux.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,9 @@ module Acc = struct
507507
| Set_of_closures _ | Boxed_float _ | Boxed_float32 _ | Boxed_int32 _
508508
| Boxed_int64 _ | Boxed_vec128 _ | Boxed_nativeint _
509509
| Immutable_float_block _ | Immutable_float_array _
510-
| Immutable_value_array _ | Empty_array _ | Immutable_int32_array _
511-
| Immutable_int64_array _ | Immutable_nativeint_array _ | Mutable_string _
512-
| Immutable_string _ ->
510+
| Immutable_float32_array _ | Immutable_value_array _ | Empty_array _
511+
| Immutable_int32_array _ | Immutable_int64_array _
512+
| Immutable_nativeint_array _ | Mutable_string _ | Immutable_string _ ->
513513
Value_unknown
514514
in
515515
let symbol_approximations =

middle_end/flambda2/from_lambda/lambda_to_flambda_primitives.ml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ let convert_array_kind (kind : L.array_kind) : converted_array_kind =
129129
| Paddrarray -> Array_kind Values
130130
| Pintarray -> Array_kind Immediates
131131
| Pfloatarray | Punboxedfloatarray Pfloat64 -> Array_kind Naked_floats
132-
| Punboxedfloatarray Pfloat32 ->
133-
(* CR mslater: (float32) unboxed arrays *)
134-
assert false
132+
| Punboxedfloatarray Pfloat32 -> Array_kind Naked_float32s
135133
| Punboxedintarray Pint32 -> Array_kind Naked_int32s
136134
| Punboxedintarray Pint64 -> Array_kind Naked_int64s
137135
| Punboxedintarray Pnativeint -> Array_kind Naked_nativeints
@@ -142,12 +140,12 @@ let convert_array_kind_for_length kind : P.Array_kind_for_length.t =
142140
| Float_array_opt_dynamic -> Float_array_opt_dynamic
143141

144142
module Array_ref_kind = struct
145-
(* CR mslater: (float32) unboxed arrays *)
146143
type t =
147144
| Immediates
148145
| Values
149146
| Naked_floats_to_be_boxed of L.alloc_mode
150147
| Naked_floats
148+
| Naked_float32s
151149
| Naked_int32s
152150
| Naked_int64s
153151
| Naked_nativeints
@@ -173,9 +171,7 @@ let convert_array_ref_kind (kind : L.array_ref_kind) : converted_array_ref_kind
173171
| Pintarray_ref -> Array_ref_kind Immediates
174172
| Pfloatarray_ref mode -> Array_ref_kind (Naked_floats_to_be_boxed mode)
175173
| Punboxedfloatarray_ref Pfloat64 -> Array_ref_kind Naked_floats
176-
| Punboxedfloatarray_ref Pfloat32 ->
177-
(* CR mslater: (float32) unboxed arrays *)
178-
assert false
174+
| Punboxedfloatarray_ref Pfloat32 -> Array_ref_kind Naked_float32s
179175
| Punboxedintarray_ref Pint32 -> Array_ref_kind Naked_int32s
180176
| Punboxedintarray_ref Pint64 -> Array_ref_kind Naked_int64s
181177
| Punboxedintarray_ref Pnativeint -> Array_ref_kind Naked_nativeints
@@ -188,6 +184,7 @@ let convert_array_ref_kind_for_length array_ref_kind : P.Array_kind_for_length.t
188184
| Array_ref_kind Immediates -> Array_kind Immediates
189185
| Array_ref_kind (Naked_floats | Naked_floats_to_be_boxed _) ->
190186
Array_kind Naked_floats
187+
| Array_ref_kind Naked_float32s -> Array_kind Naked_float32s
191188
| Array_ref_kind Naked_int32s -> Array_kind Naked_int32s
192189
| Array_ref_kind Naked_int64s -> Array_kind Naked_int64s
193190
| Array_ref_kind Naked_nativeints -> Array_kind Naked_nativeints
@@ -198,6 +195,7 @@ module Array_set_kind = struct
198195
| Values of P.Init_or_assign.t
199196
| Naked_floats
200197
| Naked_floats_to_be_unboxed
198+
| Naked_float32s
201199
| Naked_int32s
202200
| Naked_int64s
203201
| Naked_nativeints
@@ -213,6 +211,7 @@ let convert_intermediate_array_set_kind (kind : Array_set_kind.t) :
213211
| Immediates -> Immediates
214212
| Values init_or_assign -> Values init_or_assign
215213
| Naked_floats | Naked_floats_to_be_unboxed -> Naked_floats
214+
| Naked_float32s -> Naked_float32s
216215
| Naked_int32s -> Naked_int32s
217216
| Naked_int64s -> Naked_int64s
218217
| Naked_nativeints -> Naked_nativeints
@@ -231,9 +230,7 @@ let convert_array_set_kind (kind : L.array_set_kind) : converted_array_set_kind
231230
| Pintarray_set -> Array_set_kind Immediates
232231
| Pfloatarray_set -> Array_set_kind Naked_floats_to_be_unboxed
233232
| Punboxedfloatarray_set Pfloat64 -> Array_set_kind Naked_floats
234-
| Punboxedfloatarray_set Pfloat32 ->
235-
(* CR mslater: (float32) unboxed arrays *)
236-
assert false
233+
| Punboxedfloatarray_set Pfloat32 -> Array_set_kind Naked_float32s
237234
| Punboxedintarray_set Pint32 -> Array_set_kind Naked_int32s
238235
| Punboxedintarray_set Pint64 -> Array_set_kind Naked_int64s
239236
| Punboxedintarray_set Pnativeint -> Array_set_kind Naked_nativeints
@@ -246,6 +243,7 @@ let convert_array_set_kind_for_length array_set_kind : P.Array_kind_for_length.t
246243
| Array_set_kind Immediates -> Array_kind Immediates
247244
| Array_set_kind (Naked_floats | Naked_floats_to_be_unboxed) ->
248245
Array_kind Naked_floats
246+
| Array_set_kind Naked_float32s -> Array_kind Naked_float32s
249247
| Array_set_kind Naked_int32s -> Array_kind Naked_int32s
250248
| Array_set_kind Naked_int64s -> Array_kind Naked_int64s
251249
| Array_set_kind Naked_nativeints -> Array_kind Naked_nativeints
@@ -265,8 +263,7 @@ let convert_array_kind_to_duplicate_array_kind (kind : L.array_kind) :
265263
| Pfloatarray | Punboxedfloatarray Pfloat64 ->
266264
Duplicate_array_kind (Naked_floats { length = None })
267265
| Punboxedfloatarray Pfloat32 ->
268-
(* CR mslater: (float32) unboxed arrays *)
269-
assert false
266+
Duplicate_array_kind (Naked_float32s { length = None })
270267
| Punboxedintarray Pint32 ->
271268
Duplicate_array_kind (Naked_int32s { length = None })
272269
| Punboxedintarray Pint64 ->
@@ -596,7 +593,7 @@ let array_vector_access_validity_condition array ~size_int
596593
let width_in_scalars =
597594
match array_kind with
598595
| Naked_floats | Immediates | Naked_int64s | Naked_nativeints -> 2
599-
| Naked_int32s -> 4
596+
| Naked_int32s | Naked_float32s -> 4
600597
| Values ->
601598
Misc.fatal_error
602599
"Attempted to load/store a SIMD vector from/to a value array."
@@ -820,6 +817,8 @@ let array_load_unsafe ~array ~index (array_ref_kind : Array_ref_kind.t)
820817
~current_region
821818
| Naked_floats ->
822819
Binary (Array_load (Naked_floats, Scalar, Mutable), array, index)
820+
| Naked_float32s ->
821+
Binary (Array_load (Naked_float32s, Scalar, Mutable), array, index)
823822
| Naked_int32s ->
824823
Binary (Array_load (Naked_int32s, Scalar, Mutable), array, index)
825824
| Naked_int64s ->
@@ -831,8 +830,8 @@ let array_set_unsafe ~array ~index ~new_value
831830
(array_set_kind : Array_set_kind.t) : H.expr_primitive =
832831
let new_value =
833832
match array_set_kind with
834-
| Immediates | Values _ | Naked_floats | Naked_int32s | Naked_int64s
835-
| Naked_nativeints ->
833+
| Immediates | Values _ | Naked_floats | Naked_float32s | Naked_int32s
834+
| Naked_int64s | Naked_nativeints ->
836835
new_value
837836
| Naked_floats_to_be_unboxed -> unbox_float new_value
838837
in

0 commit comments

Comments
 (0)