Skip to content

Commit 41fc1e9

Browse files
authored
Add a float64 layout and float# type. (#1528)
1 parent d4eca98 commit 41fc1e9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2269
-393
lines changed

backend/cmmgen.ml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,21 @@ let is_strict : kind_for_unboxing -> bool = function
391391
| Boxed_integer _ | Boxed_float | Boxed_vector _ -> false
392392
| Any -> true
393393

394+
(* [exttype_of_sort] and [machtype_of_sort] should be kept in sync with
395+
[Typeopt.layout_of_const_sort]. *)
396+
(* CR layouts v5: Void case should probably be typ_void *)
397+
let exttype_of_sort (s : Layouts.Sort.const) =
398+
match s with
399+
| Value -> XInt
400+
| Float64 -> XFloat
401+
| Void -> Misc.fatal_error "Cmmgen.exttype_of_sort: void encountered"
402+
403+
let machtype_of_sort (s : Layouts.Sort.const) =
404+
match s with
405+
| Value -> typ_val
406+
| Float64 -> typ_float
407+
| Void -> Misc.fatal_error "Cmmgen.machtype_of_sort: void encountered"
408+
394409
let rec is_unboxed_number_cmm = function
395410
| Cop(Calloc mode, [Cconst_natint (hdr, _); _], dbg)
396411
when Nativeint.equal hdr float_header ->
@@ -906,13 +921,8 @@ and transl_make_array dbg env kind mode args =
906921

907922
and transl_ccall env prim args dbg =
908923
let transl_arg native_repr arg =
909-
(* CR layouts v2: This match to be extended with
910-
| Same_as_ocaml_repr Float64 -> (XFloat, transl env arg)
911-
in the PR that adds Float64 *)
912924
match native_repr with
913-
| Same_as_ocaml_repr Value ->
914-
(XInt, transl env arg)
915-
| Same_as_ocaml_repr Void -> assert false
925+
| Same_as_ocaml_repr sort -> (exttype_of_sort sort, transl env arg)
916926
| Unboxed_float ->
917927
(XFloat, transl_unbox_float dbg env arg)
918928
| Unboxed_integer bi ->
@@ -945,12 +955,8 @@ and transl_ccall env prim args dbg =
945955
(ty1 :: tys, arg' :: args')
946956
in
947957
let typ_res, wrap_result =
948-
(* CR layouts v2: This match to be extended with
949-
| Same_as_ocaml_repr Float64 -> (typ_float, fun x -> x)
950-
in the PR that adds Float64 *)
951958
match prim.prim_native_repr_res with
952-
| _, Same_as_ocaml_repr Value -> (typ_val, fun x -> x)
953-
| _, Same_as_ocaml_repr Void -> assert false
959+
| _, Same_as_ocaml_repr sort -> (machtype_of_sort sort, fun x -> x)
954960
(* TODO: Allow Alloc_local on suitably typed C stubs *)
955961
| _, Unboxed_float -> (typ_float, box_float dbg alloc_heap)
956962
| _, Unboxed_integer Pint64 when size_int = 4 ->

middle_end/flambda2/from_lambda/closure_conversion.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,9 @@ let close_c_call acc env ~loc ~let_bound_ids_with_kinds
465465
in
466466
let kind_of_primitive_native_repr
467467
((_, repr) : Primitive.mode * Primitive.native_repr) =
468-
(* CR layouts v2: This match will be extended with [| Same_as_ocaml_repr
469-
Float64 -> K.naked_float] in the PR that adds Float64. *)
470468
match repr with
471-
| Same_as_ocaml_repr Value -> K.value
472-
| Same_as_ocaml_repr Void -> assert false
469+
| Same_as_ocaml_repr sort ->
470+
K.With_subkind.(kind (from_lambda (Typeopt.layout_of_const_sort sort)))
473471
| Unboxed_float -> K.naked_float
474472
| Unboxed_integer Pnativeint -> K.naked_nativeint
475473
| Unboxed_integer Pint32 -> K.naked_int32

ocaml/asmcomp/cmm_helpers.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,7 @@ let send_function (arity, result, mode) =
22102210
let fun_name = send_function_name arity result mode in
22112211
let fun_args =
22122212
[obj, typ_val; tag, typ_int; cache, typ_addr]
2213-
@ List.map (fun id -> (id, typ_val)) (List.tl args) in
2213+
@ List.combine (List.tl args) arity in
22142214
let fun_dbg = placeholder_fun_dbg ~human_name:fun_name in
22152215
Cfunction
22162216
{fun_name;

ocaml/asmcomp/cmmgen.ml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,21 @@ let join_unboxed_number_kind ~strict k1 k2 =
334334
k
335335
| _, _ -> No_unboxing
336336

337+
(* [exttype_of_sort] and [machtype_of_sort] should be kept in sync with
338+
[Typeopt.layout_of_const_sort]. *)
339+
(* CR layouts v5: Void case should probably be typ_void *)
340+
let exttype_of_sort (s : Layouts.Sort.const) =
341+
match s with
342+
| Value -> XInt
343+
| Float64 -> XFloat
344+
| Void -> Misc.fatal_error "Cmmgen.exttype_of_sort: void encountered"
345+
346+
let machtype_of_sort (s : Layouts.Sort.const) =
347+
match s with
348+
| Value -> typ_val
349+
| Float64 -> typ_float
350+
| Void -> Misc.fatal_error "Cmmgen.machtype_of_sort: void encountered"
351+
337352
let is_unboxed_number_cmm ~strict cmm =
338353
let r = ref No_result in
339354
let notify k =
@@ -828,13 +843,8 @@ and transl_make_array dbg env kind mode args =
828843

829844
and transl_ccall env prim args dbg =
830845
let transl_arg native_repr arg =
831-
(* CR layouts v2: This match to be extended with
832-
| Same_as_ocaml_repr Float64 -> (XFloat, transl env arg)
833-
in the PR that adds Float64 *)
834846
match native_repr with
835-
| Same_as_ocaml_repr Value ->
836-
(XInt, transl env arg)
837-
| Same_as_ocaml_repr Void -> assert false
847+
| Same_as_ocaml_repr sort -> (exttype_of_sort sort, transl env arg)
838848
| Unboxed_float ->
839849
(XFloat, transl_unbox_float dbg env arg)
840850
| Unboxed_integer bi ->
@@ -864,11 +874,7 @@ and transl_ccall env prim args dbg =
864874
in
865875
let typ_res, wrap_result =
866876
match prim.prim_native_repr_res with
867-
(* CR layouts v2: This match to be extended with
868-
| Same_as_ocaml_repr Float64 -> (typ_float, fun x -> x)
869-
in the PR that adds Float64 *)
870-
| _, Same_as_ocaml_repr Value -> (typ_val, fun x -> x)
871-
| _, Same_as_ocaml_repr Void -> assert false
877+
| _, Same_as_ocaml_repr sort -> (machtype_of_sort sort, fun x -> x)
872878
(* TODO: Allow Alloc_local on suitably typed C stubs *)
873879
| _, Unboxed_float -> (typ_float, box_float dbg alloc_heap)
874880
| _, Unboxed_integer Pint64 when size_int = 4 ->

ocaml/boot/menhir/parser.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -980,15 +980,15 @@ let mk_directive ~loc name arg =
980980
let check_layout loc id =
981981
begin
982982
match id with
983-
| ("any" | "value" | "void" | "immediate64" | "immediate") -> ()
983+
| ("any" | "value" | "void" | "immediate64" | "immediate" | "float64") -> ()
984984
| _ -> expecting loc "layout"
985985
end;
986986
let loc = make_loc loc in
987987
Attr.mk ~loc (mkloc id loc) (PStr [])
988988

989989
(* Unboxed literals *)
990990

991-
(* CR layouts v2: The [unboxed_*] functions will both be improved and lose
991+
(* CR layouts v2.5: The [unboxed_*] functions will both be improved and lose
992992
their explicit assert once we have real unboxed literals in Jane syntax; they
993993
may also get re-inlined at that point *)
994994
let unboxed_literals_extension = Language_extension.Layouts

ocaml/boot/ocamlc

7.4 KB
Binary file not shown.

ocaml/boot/ocamllex

0 Bytes
Binary file not shown.

ocaml/lambda/lambda.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ let layout_module = Pvalue Pgenval
648648
let layout_module_field = Pvalue Pgenval
649649
let layout_functor = Pvalue Pgenval
650650
let layout_boxed_float = Pvalue Pfloatval
651+
let layout_unboxed_float = Punboxed_float
651652
let layout_string = Pvalue Pgenval
652653
let layout_boxedint bi = Pvalue (Pboxedintval bi)
653654

@@ -1459,6 +1460,7 @@ let primitive_result_layout (p : primitive) =
14591460
| Pccall { prim_native_repr_res = _, Same_as_ocaml_repr s; _} ->
14601461
begin match s with
14611462
| Value -> layout_any_value
1463+
| Float64 -> layout_unboxed_float
14621464
| Void -> assert false
14631465
end
14641466
| Pccall { prim_native_repr_res = _, Unboxed_integer bi; _} ->

ocaml/lambda/lambda.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ val layout_functor : layout
535535
val layout_module_field : layout
536536
val layout_string : layout
537537
val layout_boxed_float : layout
538+
val layout_unboxed_float : layout
538539
val layout_boxedint : boxed_integer -> layout
539540
(* A layout that is Pgenval because it is the field of a block *)
540541
val layout_field : layout

ocaml/lambda/matching.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ exception Error of Location.t * error
106106

107107
let dbg = false
108108

109-
(* CR layouts v2: When we're ready to allow non-values, these can be deleted or
109+
(* CR layouts v5: When we're ready to allow non-values, these can be deleted or
110110
changed to check for void. *)
111111
let layout_must_be_value loc layout =
112112
match Layout.(sub layout (value ~why:V1_safety_check)) with
@@ -1772,7 +1772,7 @@ let get_pat_args_constr p rem =
17721772
List.iteri
17731773
(fun i arg -> layout_must_be_value arg.pat_loc cstr_arg_layouts.(i))
17741774
args;
1775-
(* CR layouts v2: This sanity check will have to go (or be replaced with a
1775+
(* CR layouts v5: This sanity check will have to go (or be replaced with a
17761776
void-specific check) when we have other non-value sorts *)
17771777
args @ rem
17781778
| _ -> assert false
@@ -1784,7 +1784,7 @@ let get_expr_args_constr ~scopes head (arg, _mut, sort, layout) rem =
17841784
| _ -> fatal_error "Matching.get_expr_args_constr"
17851785
in
17861786
let loc = head_loc ~scopes head in
1787-
(* CR layouts v2: This sanity check should be removed or changed to
1787+
(* CR layouts v5: This sanity check should be removed or changed to
17881788
specifically check for void when we add other non-value sorts. *)
17891789
Array.iter (fun layout -> layout_must_be_value head.pat_loc layout)
17901790
cstr.cstr_arg_layouts;
@@ -3999,7 +3999,7 @@ let for_multiple_match ~scopes ~return_layout loc paraml mode pat_act_list parti
39993999
partial)
40004000

40014001
(* Error report *)
4002-
(* CR layouts v2: This file didn't use to have the report_error infrastructure -
4002+
(* CR layouts v5: This file didn't use to have the report_error infrastructure -
40034003
I added it only for the void sanity checking in this module, which I'm not
40044004
sure is even needed. Reevaluate. *)
40054005
open Format

ocaml/lambda/transl_array_comprehension.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,6 @@ end
445445
446446
This function returns both a pair of said CPSed Lambda term and the let
447447
bindings generated by this term (as an [Iterator_bindings.t], which see). *)
448-
(* CR layouts v2: the value that is passed to this function for [transl_exp]
449-
(and all the other [~transl_exp] parameters in this file) must only be called
450-
on expressions whose types have sort value. Probably [transl_exp] will have
451-
been updated to allow other sorts by the time we allow array elements other
452-
than value, but check that. *)
453448
let iterator ~transl_exp ~scopes ~loc
454449
: comprehension_iterator -> (lambda -> lambda) * Iterator_bindings.t
455450
= function
@@ -832,6 +827,8 @@ let comprehension
832827
~array_sizing
833828
~array
834829
~index
830+
(* CR layouts v4: Ensure that the [transl_exp] here can cope
831+
with non-values. *)
835832
~body:(transl_exp ~scopes Sort.for_array_element comp_body)),
836833
(* If it was dynamically grown, cut it down to size *)
837834
match array_sizing with

ocaml/lambda/translcore.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ let layout_must_be_value loc layout =
4343
| Ok _ -> ()
4444
| Error e -> raise (Error (loc, Non_value_layout e))
4545

46-
(* CR layouts v2: In the places where this is used, we will want to allow
47-
#float, but not void yet (e.g., the left of a semicolon and loop bodies). we
46+
(* CR layouts v7: In the places where this is used, we will want to allow
47+
float#, but not void yet (e.g., the left of a semicolon and loop bodies). we
4848
still default to value before checking for void, to allow for sort variables
4949
arising in situations like
5050
@@ -860,7 +860,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
860860
| _ ->
861861
let oid = Ident.create_local "open" in
862862
let body, _ =
863-
(* CR layouts v2: Currently we only allow values at the top of a
863+
(* CR layouts v5: Currently we only allow values at the top of a
864864
module. When that changes, some adjustments may be needed
865865
here. *)
866866
List.fold_left (fun (body, pos) id ->
@@ -900,7 +900,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
900900
(We could probably calculate the layouts of these variables here
901901
rather than requiring them all to be value, but that would be even
902902
more hacky.) *)
903-
(* CR layouts v2: if we get close to releasing other layout somebody
903+
(* CR layouts v2.5: if we get close to releasing other layout somebody
904904
actually might put in a probe, check with the middle-end team about
905905
the status of fixing this. *)
906906
let path = Path.Pident id in
@@ -1411,7 +1411,7 @@ and transl_record ~scopes loc env mode fields repres opt_init_expr =
14111411
then begin
14121412
(* Allocate new record with given fields (and remaining fields
14131413
taken from init_expr if any *)
1414-
(* CR layouts v2: currently we raise if a non-value field is detected.
1414+
(* CR layouts v5: currently we raise if a non-value field is detected.
14151415
relax that. *)
14161416
let init_id = Ident.create_local "init" in
14171417
let lv =
@@ -1498,7 +1498,7 @@ and transl_record ~scopes loc env mode fields repres opt_init_expr =
14981498
of the copy *)
14991499
let copy_id = Ident.create_local "newrecord" in
15001500
let update_field cont (lbl, definition) =
1501-
(* CR layouts v2: remove this check to allow non-value fields. Even
1501+
(* CR layouts v5: remove this check to allow non-value fields. Even
15021502
in the current version we can reasonably skip it because if we built
15031503
the init record, we must have already checked for void. *)
15041504
layout_must_be_value lbl.lbl_loc lbl.lbl_layout;

ocaml/lambda/translmod.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type error =
4343

4444
exception Error of Location.t * error
4545

46-
(* CR layouts v2: This is used as part of the "void safety check" in the case of
46+
(* CR layouts v7: This is used as part of the "void safety check" in the case of
4747
[Tstr_eval], where we want to allow any sort (see comment on that case of
4848
typemod). Remove when we remove the safety check.
4949

ocaml/lambda/translprim.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ let lookup_primitive loc poly pos p =
416416
| "%obj_magic" -> Primitive(Pobj_magic Lambda.layout_any_value, 1)
417417
| "%array_to_iarray" -> Primitive (Parray_to_iarray, 1)
418418
| "%array_of_iarray" -> Primitive (Parray_of_iarray, 1)
419+
| "%unbox_float" -> Primitive(Punbox_float, 1)
420+
| "%box_float" -> Primitive(Pbox_float mode, 1)
419421
| s when String.length s > 0 && s.[0] = '%' ->
420422
raise(Error(loc, Unknown_builtin_primitive s))
421423
| _ -> External p
@@ -513,7 +515,7 @@ let glb_array_set_type t1 t2 =
513515
| Pfloatarray_set, Pfloatarray -> Pfloatarray_set
514516

515517
(* Specialize a primitive from available type information. *)
516-
(* CR layouts v2: This function had a loc argument added just to support the void
518+
(* CR layouts v7: This function had a loc argument added just to support the void
517519
check error message. Take it out when we remove that. *)
518520
let specialize_primitive env loc ty ~has_constant_constructor prim =
519521
let param_tys =

ocaml/parsing/asttypes.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type const_layout =
5656
| Void
5757
| Immediate64
5858
| Immediate
59+
| Float64
5960

6061
type label = string
6162

ocaml/parsing/builtin_attributes.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ let builtin_attrs =
8080
; "ppwarning"; "ocaml.ppwarning"
8181
; "explicit_arity"; "ocaml.explicit_arity"
8282
; "warn_on_literal_pattern"; "ocaml.warn_on_literal_pattern"
83+
; "float64"; "ocaml.float64"
8384
; "immediate"; "ocaml.immediate"
8485
; "immediate64"; "ocaml.immediate64"
8586
; "void"; "ocaml.void"
@@ -460,6 +461,7 @@ let layout ~legacy_immediate attrs =
460461
| "ocaml.any"|"any" -> Some (a, Any)
461462
| "ocaml.immediate"|"immediate" -> Some (a, Immediate)
462463
| "ocaml.immediate64"|"immediate64" -> Some (a, Immediate64)
464+
| "ocaml.float64"|"float64" -> Some (a, Float64)
463465
| _ -> None
464466
) attrs
465467
in
@@ -478,7 +480,7 @@ let layout ~legacy_immediate attrs =
478480
| Immediate | Immediate64 ->
479481
check (legacy_immediate
480482
|| Language_extension.(is_at_least Layouts Beta))
481-
| Any | Void ->
483+
| Any | Void | Float64 ->
482484
check Language_extension.(is_at_least Layouts Alpha)
483485

484486
(* The "ocaml.boxed (default)" and "ocaml.unboxed (default)"

ocaml/parsing/parser.mly

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,15 +757,15 @@ let mk_directive ~loc name arg =
757757
let check_layout loc id =
758758
begin
759759
match id with
760-
| ("any" | "value" | "void" | "immediate64" | "immediate") -> ()
760+
| ("any" | "value" | "void" | "immediate64" | "immediate" | "float64") -> ()
761761
| _ -> expecting loc "layout"
762762
end;
763763
let loc = make_loc loc in
764764
Attr.mk ~loc (mkloc id loc) (PStr [])
765765

766766
(* Unboxed literals *)
767767

768-
(* CR layouts v2: The [unboxed_*] functions will both be improved and lose
768+
(* CR layouts v2.5: The [unboxed_*] functions will both be improved and lose
769769
their explicit assert once we have real unboxed literals in Jane syntax; they
770770
may also get re-inlined at that point *)
771771
let unboxed_literals_extension = Language_extension.Layouts

ocaml/testsuite/tests/float-unboxing/float_subst_boxed_number.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ end
5050
let check_noalloc name f =
5151
let a0 = Gc.allocated_bytes () in
5252
let a1 = Gc.allocated_bytes () in
53-
let _x = f () in
53+
let _x = (f[@inlined never]) () in
5454
let a2 = Gc.allocated_bytes () in
5555
let alloc = (a2 -. 2. *. a1 +. a0) in
5656

ocaml/testsuite/tests/int64-unboxing/test.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let f () =
2424
let () =
2525
let a0 = Gc.allocated_bytes () in
2626
let a1 = Gc.allocated_bytes () in
27-
let _x = f () in
27+
let _x = (f[@inlined never]) () in
2828
let a2 = Gc.allocated_bytes () in
2929
let alloc = (a2 -. 2. *. a1 +. a0) in
3030
assert(alloc = 0.)

0 commit comments

Comments
 (0)