Skip to content

Commit 28f543e

Browse files
authored
flambda-backend: float32 flambda2 support (#2362)
1 parent 2f20f89 commit 28f543e

14 files changed

+59
-15
lines changed

Makefile.common-jst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ install_for_test: _install
268268
# Various directories are put on the -I paths by tools/Makefile;
269269
# utils/ is one such, so we just dump the .cm* files in there for
270270
# various things.
271+
mkdir _runtest/external
272+
cp $(main_build)/external/float32/*.{cma,a,cmxa} _runtest/external
271273
mkdir _runtest/utils
272274
cp _install/lib/ocaml/compiler-libs/*.{cmi,cmx} _runtest/utils
273275
cp $(main_build)/$(ocamldir)/.ocamlcommon.objs/byte/*.cmo _runtest/utils

bytecomp/bytegen.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ let preserve_tailcall_for_prim = function
128128
| Pccall _ | Praise _ | Pnot | Pnegint | Paddint | Psubint | Pmulint
129129
| Pdivint _ | Pmodint _ | Pandint | Porint | Pxorint | Plslint | Plsrint
130130
| Pasrint | Pintcomp _ | Poffsetint _ | Poffsetref _ | Pintoffloat _
131-
| Pfloatofint (_, _) | Pnegfloat (_, _) | Pabsfloat (_, _)
131+
| Pfloatofint (_, _) | Pfloatoffloat32 _ | Pfloat32offloat _
132+
| Pnegfloat (_, _) | Pabsfloat (_, _)
132133
| Paddfloat (_, _) | Psubfloat (_, _) | Pmulfloat (_, _)
133134
| Pdivfloat (_, _) | Pfloatcomp (_, _) | Punboxed_float_comp (_, _)
134135
| Pstringlength | Pstringrefu | Pstringrefs
@@ -495,6 +496,8 @@ let comp_primitive stack_info p sz args =
495496
| Poffsetref n -> Koffsetref n
496497
| Pintoffloat Pfloat64 -> Kccall("caml_int_of_float", 1)
497498
| Pfloatofint (Pfloat64, _) -> Kccall("caml_float_of_int", 1)
499+
| Pfloatoffloat32 _ -> Kccall("caml_float_of_float32", 1)
500+
| Pfloat32offloat _ -> Kccall("caml_float32_of_float", 1)
498501
| Pnegfloat (Pfloat64, _) -> Kccall("caml_neg_float", 1)
499502
| Pabsfloat (Pfloat64, _) -> Kccall("caml_abs_float", 1)
500503
| Paddfloat (Pfloat64, _) -> Kccall("caml_add_float", 2)

bytecomp/symtable.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ let rec transl_const = function
148148
Const_base(Const_int i) -> Obj.repr i
149149
| Const_base(Const_char c) -> Obj.repr c
150150
| Const_base(Const_string (s, _, _)) -> Obj.repr s
151-
| Const_base(Const_float32 _) ->
152-
(* CR mslater: (float32) use float32 in the compiler *)
153-
assert false
151+
| Const_base(Const_float32 f)
154152
| Const_base(Const_float f)
155153
| Const_base(Const_unboxed_float f) -> Obj.repr (float_of_string f)
156154
| Const_base(Const_int32 i)

lambda/lambda.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ type primitive =
183183
| Poffsetint of int
184184
| Poffsetref of int
185185
(* Float operations *)
186+
| Pfloatoffloat32 of alloc_mode
187+
| Pfloat32offloat of alloc_mode
186188
| Pintoffloat of boxed_float
187189
| Pfloatofint of boxed_float * alloc_mode
188190
| Pnegfloat of boxed_float * alloc_mode
@@ -1647,6 +1649,8 @@ let primitive_may_allocate : primitive -> alloc_mode option = function
16471649
| Poffsetref _ -> None
16481650
| Pintoffloat _ -> None
16491651
| Pfloatofint (_, m) -> Some m
1652+
| Pfloatoffloat32 m -> Some m
1653+
| Pfloat32offloat m -> Some m
16501654
| Pnegfloat (_, m) | Pabsfloat (_, m)
16511655
| Paddfloat (_, m) | Psubfloat (_, m)
16521656
| Pmulfloat (_, m) | Pdivfloat (_, m) -> Some m
@@ -1791,6 +1795,8 @@ let primitive_result_layout (p : primitive) =
17911795
| Punboxed_product_field (field, layouts) -> (Array.of_list layouts).(field)
17921796
| Pmake_unboxed_product layouts -> layout_unboxed_product layouts
17931797
| Pfloatfield _ -> layout_boxed_float Pfloat64
1798+
| Pfloatoffloat32 _ -> layout_boxed_float Pfloat64
1799+
| Pfloat32offloat _ -> layout_boxed_float Pfloat32
17941800
| Pfloatofint (f, _) | Pnegfloat (f, _) | Pabsfloat (f, _)
17951801
| Paddfloat (f, _) | Psubfloat (f, _) | Pmulfloat (f, _) | Pdivfloat (f, _)
17961802
| Pbox_float (f, _) -> layout_boxed_float f
@@ -1861,7 +1867,7 @@ let primitive_result_layout (p : primitive) =
18611867
begin match kind with
18621868
| Pbigarray_unknown -> layout_any_value
18631869
| Pbigarray_float32 ->
1864-
(* CR mslater: (float32) bigarrays *)
1870+
(* float32 bigarrays return 64-bit floats for backward compatibility. *)
18651871
layout_boxed_float Pfloat64
18661872
| Pbigarray_float64 -> layout_boxed_float Pfloat64
18671873
| Pbigarray_sint8 | Pbigarray_uint8

lambda/lambda.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ type primitive =
145145
| Poffsetint of int
146146
| Poffsetref of int
147147
(* Float operations *)
148+
(* CR mslater: (float32) use a single cast primitive *)
149+
| Pfloatoffloat32 of alloc_mode
150+
| Pfloat32offloat of alloc_mode
148151
| Pintoffloat of boxed_float
149152
| Pfloatofint of boxed_float * alloc_mode
150153
| Pnegfloat of boxed_float * alloc_mode

lambda/printlambda.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ let primitive ppf = function
535535
| Pcompare_bints bi -> fprintf ppf "compare_bints %s" (boxed_integer_name bi)
536536
| Poffsetint n -> fprintf ppf "%i+" n
537537
| Poffsetref n -> fprintf ppf "+:=%i"n
538+
| Pfloatoffloat32 m -> print_boxed_float "float_of_float32" ppf Pfloat32 m
539+
| Pfloat32offloat m -> print_boxed_float "float32_of_float" ppf Pfloat64 m
538540
| Pintoffloat bf -> fprintf ppf "int_of_%s" (boxed_float_name bf)
539541
| Pfloatofint (bf,m) ->
540542
fprintf ppf "%s_of_int%s" (boxed_float_name bf) (alloc_kind m)
@@ -822,6 +824,8 @@ let name_of_primitive = function
822824
| Pcompare_bints _ -> "Pcompare"
823825
| Poffsetint _ -> "Poffsetint"
824826
| Poffsetref _ -> "Poffsetref"
827+
| Pfloatoffloat32 _ -> "Pfloatoffloat32"
828+
| Pfloat32offloat _ -> "Pfloat32offloat"
825829
| Pintoffloat _ -> "Pintoffloat"
826830
| Pfloatofint (_, _) -> "Pfloatofint"
827831
| Pnegfloat (_, _) -> "Pnegfloat"

lambda/tmc.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ let rec choice ctx t =
889889
| Pintcomp _ | Punboxed_int_comp _
890890
| Poffsetint _ | Poffsetref _
891891
| Pintoffloat _ | Pfloatofint (_, _)
892+
| Pfloatoffloat32 _ | Pfloat32offloat _
892893
| Pnegfloat (_, _) | Pabsfloat (_, _)
893894
| Paddfloat (_, _) | Psubfloat (_, _)
894895
| Pmulfloat (_, _) | Pdivfloat (_, _)

lambda/transl_array_comprehension.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ let initial_array ~loc ~array_kind ~array_size ~array_sizing =
713713
| Dynamic_size, Punboxedfloatarray Pfloat64 ->
714714
Mutable, Resizable_array.make ~loc array_kind (unboxed_float 0.)
715715
| (Fixed_size | Dynamic_size), Punboxedfloatarray Pfloat32 ->
716-
(* CR mslater: (float32) array support *)
716+
(* CR mslater: (float32) unboxed arrays *)
717717
assert false
718718
| Dynamic_size, Punboxedintarray Pint32 ->
719719
Mutable, Resizable_array.make ~loc array_kind (unboxed_int32 0l)
@@ -812,7 +812,7 @@ let body ~loc ~array_kind ~array_size ~array_sizing ~array ~index ~body =
812812
| Punboxedintarray _ ->
813813
set_element_in_bounds body
814814
| Punboxedfloatarray Pfloat32 ->
815-
(* CR mslater: (float32) array support *)
815+
(* CR mslater: (float32) unboxed arrays *)
816816
assert false
817817
in
818818
Lsequence

lambda/translprim.ml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,22 @@ let lookup_primitive loc ~poly_mode ~poly_sort pos p =
267267
| "%geint" -> Primitive ((Pintcomp Cge), 2)
268268
| "%incr" -> Primitive ((Poffsetref(1)), 1)
269269
| "%decr" -> Primitive ((Poffsetref(-1)), 1)
270-
(* CR mslater: (float32) primitives *)
270+
| "%floatoffloat32" -> Primitive (Pfloatoffloat32 mode, 1)
271+
| "%float32offloat" -> Primitive (Pfloat32offloat mode, 1)
272+
| "%intoffloat32" -> Primitive (Pintoffloat Pfloat32, 1)
273+
| "%float32ofint" -> Primitive (Pfloatofint (Pfloat32, mode), 1)
274+
| "%negfloat32" -> Primitive (Pnegfloat (Pfloat32, mode), 1)
275+
| "%absfloat32" -> Primitive (Pabsfloat (Pfloat32, mode), 1)
276+
| "%addfloat32" -> Primitive (Paddfloat (Pfloat32, mode), 2)
277+
| "%subfloat32" -> Primitive (Psubfloat (Pfloat32, mode), 2)
278+
| "%mulfloat32" -> Primitive (Pmulfloat (Pfloat32, mode), 2)
279+
| "%divfloat32" -> Primitive (Pdivfloat (Pfloat32, mode), 2)
280+
| "%eqfloat32" -> Primitive ((Pfloatcomp (Pfloat32, CFeq)), 2)
281+
| "%noteqfloat32" -> Primitive ((Pfloatcomp (Pfloat32, CFneq)), 2)
282+
| "%ltfloat32" -> Primitive ((Pfloatcomp (Pfloat32, CFlt)), 2)
283+
| "%lefloat32" -> Primitive ((Pfloatcomp (Pfloat32, CFle)), 2)
284+
| "%gtfloat32" -> Primitive ((Pfloatcomp (Pfloat32, CFgt)), 2)
285+
| "%gefloat32" -> Primitive ((Pfloatcomp (Pfloat32, CFge)), 2)
271286
| "%intoffloat" -> Primitive (Pintoffloat Pfloat64, 1)
272287
| "%floatofint" -> Primitive (Pfloatofint (Pfloat64, mode), 1)
273288
| "%negfloat" -> Primitive (Pnegfloat (Pfloat64, mode), 1)
@@ -643,6 +658,7 @@ let lookup_primitive loc ~poly_mode ~poly_sort pos p =
643658
Primitive ((Pfloatarray_set_128 {unsafe = false}), 3)
644659
| "%caml_floatarray_set128u" ->
645660
Primitive ((Pfloatarray_set_128 {unsafe = true}), 3)
661+
(* CR mslater: (float32) unboxed arrays *)
646662
| "%caml_unboxed_float_array_set128" ->
647663
Primitive ((Punboxed_float_array_set_128 {unsafe = false}), 3)
648664
| "%caml_unboxed_float_array_set128u" ->
@@ -684,6 +700,7 @@ let lookup_primitive loc ~poly_mode ~poly_sort pos p =
684700
| "%obj_magic" -> Primitive(Pobj_magic layout, 1)
685701
| "%array_to_iarray" -> Primitive (Parray_to_iarray, 1)
686702
| "%array_of_iarray" -> Primitive (Parray_of_iarray, 1)
703+
(* CR mslater: (float32) unboxed *)
687704
| "%unbox_float" -> Primitive(Punbox_float Pfloat64, 1)
688705
| "%box_float" -> Primitive(Pbox_float (Pfloat64, mode), 1)
689706
| "%get_header" -> Primitive (Pget_header mode, 1)
@@ -1407,6 +1424,8 @@ let lambda_primitive_needs_event_after = function
14071424
collect the call stack. *)
14081425
| Pduprecord _ | Pccall _
14091426
| Pfloatofint (_, _)
1427+
| Pfloatoffloat32 _
1428+
| Pfloat32offloat _
14101429
| Pnegfloat (_, _) | Pabsfloat (_, _)
14111430
| Paddfloat (_, _) | Psubfloat (_, _)
14121431
| Pmulfloat (_, _) | Pdivfloat (_, _)

middle_end/closure/closure.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,9 +1027,8 @@ let rec close ({ backend; fenv; cenv ; mutable_vars; kinds; catch_env } as env)
10271027
| Const_base (Const_string (s, _, _)) ->
10281028
str (Uconst_string s)
10291029
| Const_base(Const_float x) -> str (Uconst_float (float_of_string x))
1030-
| Const_base(Const_float32 _x) ->
1031-
(* CR mslater: (float32) middle end support *)
1032-
assert false
1030+
| Const_base(Const_float32 _) ->
1031+
Misc.fatal_error "float32 is not supported in closure. Consider using flambda2."
10331032
| Const_base (Const_unboxed_float _ | Const_unboxed_int32 _
10341033
| Const_unboxed_int64 _ | Const_unboxed_nativeint _) ->
10351034
(* CR alanechang: implement unboxed constants in closure *)

0 commit comments

Comments
 (0)