Skip to content

Commit bd65b13

Browse files
authored
flambda-backend: Allow unboxed float32s in mixed blocks (ocaml-flambda#2550)
1 parent 66fbd07 commit bd65b13

21 files changed

+4537
-2365
lines changed

lambda/lambda.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ and block_shape =
343343
value_kind list option
344344

345345
and flat_element = Types.flat_element =
346-
Imm | Float | Float64 | Bits32 | Bits64 | Word
346+
Imm | Float | Float64 | Float32 | Bits32 | Bits64 | Word
347347
and flat_element_read =
348348
| Flat_read of flat_element (* invariant: not [Float] *)
349349
| Flat_read_float of alloc_mode
@@ -1249,7 +1249,7 @@ let get_mixed_block_element = Types.get_mixed_product_element
12491249
let flat_read_non_float flat_element =
12501250
match flat_element with
12511251
| Float -> Misc.fatal_error "flat_element_read_non_float Float"
1252-
| Imm | Float64 | Bits32 | Bits64 | Word as flat_element ->
1252+
| Imm | Float64 | Float32 | Bits32 | Bits64 | Word as flat_element ->
12531253
Flat_read flat_element
12541254

12551255
let flat_read_float alloc_mode = Flat_read_float alloc_mode
@@ -1789,6 +1789,7 @@ let layout_of_mixed_field (kind : mixed_block_read) =
17891789
match proj with
17901790
| Imm -> layout_int
17911791
| Float64 -> layout_unboxed_float Pfloat64
1792+
| Float32 -> layout_unboxed_float Pfloat32
17921793
| Bits32 -> layout_unboxed_int32
17931794
| Bits64 -> layout_unboxed_int64
17941795
| Word -> layout_unboxed_nativeint

lambda/lambda.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ and block_shape =
351351
value_kind list option
352352

353353
and flat_element = Types.flat_element =
354-
Imm | Float | Float64 | Bits32 | Bits64 | Word
354+
Imm | Float | Float64 | Float32 | Bits32 | Bits64 | Word
355355
and flat_element_read = private
356356
| Flat_read of flat_element (* invariant: not [Float] *)
357357
| Flat_read_float of alloc_mode

lambda/matching.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2183,7 +2183,7 @@ let get_expr_args_record ~scopes head (arg, _mut, sort, layout) rem =
21832183
else
21842184
let read =
21852185
match flat_suffix.(pos - value_prefix_len) with
2186-
| Imm | Float64 | Bits32 | Bits64 | Word as non_float ->
2186+
| Imm | Float64 | Float32 | Bits32 | Bits64 | Word as non_float ->
21872187
flat_read_non_float non_float
21882188
| Float ->
21892189
(* TODO: could optimise to Alloc_local sometimes *)

lambda/translcore.ml

+1-4
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ let layout_pat sort p = layout p.pat_env p.pat_loc sort p.pat_type
5555

5656
let check_record_field_sort loc sort =
5757
match Jkind.Sort.get_default_value sort with
58-
| Value | Float64 | Bits32 | Bits64 | Word -> ()
59-
| Float32 ->
60-
(* CR mslater: (float32) float32# records *)
61-
Misc.fatal_error "Found unboxed float32 record field."
58+
| Value | Float64 | Float32 | Bits32 | Bits64 | Word -> ()
6259
| Void -> raise (Error (loc, Illegal_void_record_field))
6360

6461
(* Forward declaration -- to be filled in by Translmod.transl_module *)

testsuite/tests/mixed-blocks/constructor_args.ml

+52-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(* TEST
2-
flags = "-extension layouts_beta";
2+
flags = "-extension layouts_beta -extension small_numbers";
3+
include beta;
34
flambda2;
45
{
56
native;
@@ -11,6 +12,7 @@
1112
(*****************************************)
1213
(* Prelude: Functions on unboxed numbers *)
1314

15+
module Float32_u = Beta.Float32_u
1416
module Float_u = Stdlib__Float_u
1517
module Int32_u = Stdlib__Int32_u
1618
module Int64_u = Stdlib__Int64_u
@@ -34,6 +36,9 @@ type t =
3436
| Mixed6 of float * int32# * float#
3537
| Mixed7 of float * int64# * float# * nativeint#
3638
| Mixed8 of float * int32# * float# * int64# * float#
39+
| Mixed9 of float * float# * float32#
40+
| Mixed10 of float * float32# * float# * int64# * float#
41+
| Mixed11 of float * int32# * float32# * float# * int64# * nativeint#
3742
| Uniform2 of float * float
3843

3944
type t_ext = ..
@@ -47,6 +52,9 @@ type t_ext +=
4752
| Ext_mixed6 of float * int32# * float#
4853
| Ext_mixed7 of float * int64# * float# * nativeint#
4954
| Ext_mixed8 of float * int32# * float# * int64# * float#
55+
| Ext_mixed9 of float * float# * float32#
56+
| Ext_mixed10 of float * float32# * float# * int64# * float#
57+
| Ext_mixed11 of float * int32# * float32# * float# * int64# * nativeint#
5058

5159
let sprintf = Printf.sprintf
5260

@@ -75,6 +83,17 @@ let to_string = function
7583
sprintf "Mixed8 (%f, %i, %f, %i, %f)"
7684
x1 (Int32_u.to_int x2) (Float_u.to_float x3) (Int64_u.to_int x4)
7785
(Float_u.to_float x5)
86+
| Mixed9 (x1, x2, x3) ->
87+
sprintf "Mixed9 (%f, %f, %f)" x1 (Float_u.to_float x2)
88+
(Float_u.to_float (Float32_u.to_float x3))
89+
| Mixed10 (x1, x2, x3, x4, x5) ->
90+
sprintf "Mixed10 (%f, %f, %f, %i, %f)"
91+
x1 (Float_u.to_float (Float32_u.to_float x2)) (Float_u.to_float x3)
92+
(Int64_u.to_int x4) (Float_u.to_float x5)
93+
| Mixed11 (x1, x2, x3, x4, x5, x6) ->
94+
sprintf "Mixed11 (%f, %i, %f, %f, %i, %i)"
95+
x1 (Int32_u.to_int x2) (Float_u.to_float (Float32_u.to_float x3))
96+
(Float_u.to_float x4) (Int64_u.to_int x5) (Nativeint_u.to_int x6)
7897
| Uniform2 (x1, x2) -> sprintf "Uniform2 (%f, %f)" x1 x2
7998

8099
let ext_to_string = function
@@ -100,6 +119,17 @@ let ext_to_string = function
100119
sprintf "Ext_mixed8 (%f, %i, %f, %i, %f)"
101120
x1 (Int32_u.to_int x2) (Float_u.to_float x3) (Int64_u.to_int x4)
102121
(Float_u.to_float x5)
122+
| Ext_mixed9 (x1, x2, x3) ->
123+
sprintf "Ext_mixed9 (%f, %f, %f)" x1 (Float_u.to_float x2)
124+
(Float_u.to_float (Float32_u.to_float x3))
125+
| Ext_mixed10 (x1, x2, x3, x4, x5) ->
126+
sprintf "Ext_mixed10 (%f, %f, %f, %i, %f)"
127+
x1 (Float_u.to_float (Float32_u.to_float x2)) (Float_u.to_float x3)
128+
(Int64_u.to_int x4) (Float_u.to_float x5)
129+
| Ext_mixed11 (x1, x2, x3, x4, x5, x6) ->
130+
sprintf "Ext_mixed11 (%f, %i, %f, %f, %i, %i)"
131+
x1 (Int32_u.to_int x2) (Float_u.to_float (Float32_u.to_float x3))
132+
(Float_u.to_float x4) (Int64_u.to_int x5) (Nativeint_u.to_int x6)
103133
| _ -> "<ext>"
104134

105135
let print t = print_endline (" " ^ to_string t)
@@ -128,12 +158,12 @@ let () = run #17.0
128158
exercise an optimization code path.
129159
*)
130160

131-
let sum uf uf' f f' i i32 i64 i_n =
161+
let sum uf uf' f f' i i32 i64 i_n f32 =
132162
Float_u.to_float uf +. Float_u.to_float uf' +. f +. f' +.
133163
Int32_u.to_float i32 +. Int64_u.to_float i64 +. Nativeint_u.to_float i_n
134-
+. float_of_int i
164+
+. float_of_int i +. (Float_u.to_float (Float32_u.to_float f32))
135165

136-
let construct_and_destruct uf uf' f f' i i32 i64 i_n =
166+
let construct_and_destruct uf uf' f f' i i32 i64 i_n f32 =
137167
let Constant = Constant in
138168
let Uniform1 f = Uniform1 f in
139169
let Mixed1 uf = Mixed1 uf in
@@ -144,6 +174,9 @@ let construct_and_destruct uf uf' f f' i i32 i64 i_n =
144174
let Mixed6 (f, i32, uf) = Mixed6 (f, i32, uf) in
145175
let Mixed7 (f, i64, uf, i_n) = Mixed7 (f, i64, uf, i_n) in
146176
let Mixed8 (f, i32, uf, i64, uf') = Mixed8 (f, i32, uf, i64, uf') in
177+
let Mixed9 (f, uf, f32) = Mixed9 (f, uf, f32) in
178+
let Mixed10 (f, f32, uf, i64, uf') = Mixed10 (f, f32, uf, i64, uf') in
179+
let Mixed11 (f, i32, f32, uf, i64, i_n) = Mixed11 (f, i32, f32, uf, i64, i_n) in
147180
let Ext_mixed1 uf = Ext_mixed1 uf in
148181
let Ext_mixed2 (f, uf) = Ext_mixed2 (f, uf) in
149182
let Ext_mixed3 (f, uf, uf') = Ext_mixed3 (f, uf, uf') in
@@ -152,8 +185,11 @@ let construct_and_destruct uf uf' f f' i i32 i64 i_n =
152185
let Ext_mixed6 (f, i32, uf) = Ext_mixed6 (f, i32, uf) in
153186
let Ext_mixed7 (f, i64, uf, i_n) = Ext_mixed7 (f, i64, uf, i_n) in
154187
let Ext_mixed8 (f, i32, uf, i64, uf') = Ext_mixed8 (f, i32, uf, i64, uf') in
188+
let Ext_mixed9 (f, uf, f32) = Ext_mixed9 (f, uf, f32) in
189+
let Ext_mixed10 (f, f32, uf, i64, uf') = Ext_mixed10 (f, f32, uf, i64, uf') in
190+
let Ext_mixed11 (f, i32, f32, uf, i64, i_n) = Ext_mixed11 (f, i32, f32, uf, i64, i_n) in
155191
let Uniform2 (f, f') = Uniform2 (f, f') in
156-
sum uf uf' f f' i i32 i64 i_n
192+
sum uf uf' f f' i i32 i64 i_n f32
157193
[@@ocaml.warning "-partial-match"]
158194

159195
let () =
@@ -165,10 +201,11 @@ let () =
165201
and i32 = #12l
166202
and i64 = #42L
167203
and i_n = #56n
204+
and f32 = #1.2s
168205
in
169206
let () =
170-
let sum1 = sum uf uf' f f' i i32 i64 i_n in
171-
let sum2 = construct_and_destruct uf uf' f f' i i32 i64 i_n in
207+
let sum1 = sum uf uf' f f' i i32 i64 i_n f32 in
208+
let sum2 = construct_and_destruct uf uf' f f' i i32 i64 i_n f32 in
172209
Printf.printf
173210
"Test (construct and destruct): %f = %f (%s)\n"
174211
sum1
@@ -218,7 +255,7 @@ let _ =
218255
let go x y z =
219256
let f =
220257
match x with
221-
| Mixed5 (f1, uf1, i, i32_1, i_n, i64) ->
258+
| Mixed11 (f1, i32_1, f32, uf1, i64, i_n) ->
222259
(* Close over the fields we projected out *)
223260
(fun () ->
224261
match y, z with
@@ -228,7 +265,6 @@ let go x y z =
228265
Mixed3 (f2, uf2, uf3) ->
229266
[ f1;
230267
Float_u.to_float uf1;
231-
float_of_int i;
232268
Int32_u.to_float i32_1;
233269
Nativeint_u.to_float i_n;
234270
Int64_u.to_float i64;
@@ -238,6 +274,7 @@ let go x y z =
238274
f3;
239275
Float_u.to_float uf4;
240276
Int32_u.to_float i32_2;
277+
Float32.to_float (Float32_u.to_float32 f32);
241278
]
242279
| _ -> assert false
243280
)
@@ -249,7 +286,6 @@ let test () =
249286
let f1 = 4.0
250287
and f2 = 42.0
251288
and f3 = 36.0
252-
and i = 3
253289
and i32_1 = #3l
254290
and i32_2 = -#10l
255291
and i64 = -#20L
@@ -258,8 +294,9 @@ let test () =
258294
and uf2 = #32.0
259295
and uf3 = #47.5
260296
and uf4 = #47.8
297+
and f32 = #1.2s
261298
in
262-
let x = Mixed5 (f1, uf1, i, i32_1, i_n, i64) in
299+
let x = Mixed11 (f1, i32_1, f32, uf1, i64, i_n) in
263300
let y = Mixed3 (f2, uf2, uf3) in
264301
let z = Mixed4 (f3, uf4, i32_2) in
265302
(* These results should match as [go] is symmetric in
@@ -292,11 +329,10 @@ let go_recursive x y z =
292329
let rec f_odd n =
293330
if n < 7 then f_even (n+1)
294331
else match x with
295-
| Mixed5 (f1, uf1, i, i32_1, i_n, i64) ->
332+
| Mixed11 (f1, i32_1, f32, uf1, i64, i_n) ->
296333
[ float_of_int n;
297334
f1;
298335
Float_u.to_float uf1;
299-
float_of_int i;
300336
Int32_u.to_float i32_1;
301337
Nativeint_u.to_float i_n;
302338
Int64_u.to_float i64;
@@ -306,6 +342,7 @@ let go_recursive x y z =
306342
f3;
307343
Float_u.to_float uf4;
308344
Int32_u.to_float i32_2;
345+
Float32.to_float (Float32_u.to_float32 f32);
309346
]
310347
| _ -> assert false
311348
and f_even n = f_odd (n+1) in
@@ -318,7 +355,6 @@ let test_recursive () =
318355
let f1 = 4.0
319356
and f2 = 42.0
320357
and f3 = 36.0
321-
and i = 3
322358
and i32_1 = #3l
323359
and i32_2 = -#10l
324360
and i64 = -#20L
@@ -327,8 +363,9 @@ let test_recursive () =
327363
and uf2 = #32.0
328364
and uf3 = #47.5
329365
and uf4 = #47.8
366+
and f32 = #1.2s
330367
in
331-
let x = Mixed5 (f1, uf1, i, i32_1, i_n, i64) in
368+
let x = Mixed11 (f1, i32_1, f32, uf1, i64, i_n) in
332369
let y = Mixed3 (f2, uf2, uf3) in
333370
let z = Mixed4 (f3, uf4, i32_2) in
334371
(* These results should match as [go_recursive] is symmetric in

testsuite/tests/mixed-blocks/constructor_args.reference

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Test (construction)
33
Ext_mixed1 8.000000
44
Mixed2 (3.000000, 4.500000)
55
Mixed3 (6.000000, 17.000000, 5.000000)
6-
Test (construct and destruct): 149.700000 = 149.700000 (PASS)
6+
Test (construct and destruct): 150.900000 = 150.900000 (PASS)
77
Test (mixed constructors in recursive groups):
88
Mixed1 4.000000
99
Mixed2 (5.000000, 4.000000)
@@ -19,7 +19,6 @@ Test (pattern matching).
1919
4.000
2020
17.000
2121
3.000
22-
3.000
2322
174.000
2423
-20.000
2524
42.000
@@ -28,13 +27,13 @@ Test (pattern matching).
2827
36.000
2928
47.800
3029
-10.000
30+
1.200
3131
Test (pattern matching, recursive closure).
3232
Contents of fields:
3333
7.000
3434
4.000
3535
17.000
3636
3.000
37-
3.000
3837
174.000
3938
-20.000
4039
42.000
@@ -43,3 +42,4 @@ Test (pattern matching, recursive closure).
4342
36.000
4443
47.800
4544
-10.000
45+
1.200

0 commit comments

Comments
 (0)