Skip to content

Move unboxed float literals to stable #2172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ocaml/boot/menhir/parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ end = struct

let assert_unboxed_literals ~loc =
Language_extension.(
Jane_syntax_parsing.assert_extension_enabled ~loc Layouts Alpha)
Jane_syntax_parsing.assert_extension_enabled ~loc Layouts Stable)

let unboxed ~loc x =
assert_unboxed_literals ~loc:(make_loc loc);
Expand Down
2 changes: 1 addition & 1 deletion ocaml/parsing/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ end = struct

let assert_unboxed_literals ~loc =
Language_extension.(
Jane_syntax_parsing.assert_extension_enabled ~loc Layouts Alpha)
Jane_syntax_parsing.assert_extension_enabled ~loc Layouts Stable)

let unboxed ~loc x =
assert_unboxed_literals ~loc:(make_loc loc);
Expand Down
20 changes: 10 additions & 10 deletions ocaml/testsuite/tests/typing-layouts-float64/alloc.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(* TEST
* flambda2
flags = "-extension layouts_alpha"
flags = "-extension layouts"
** native
*)

Expand Down Expand Up @@ -66,17 +66,17 @@ struct

let[@inline never] step n estimate =
let new_term =
((of_float (-1.)) ** n)
/ ((n * (of_float 2.)) + (of_float 1.))
((-#1.) ** n)
/ ((n * #2.) + #1.)
in
estimate + new_term

let rec go n est =
if n > (of_float 10000.) then est else go (n+ (of_float 1.)) (step n est)
if n > #10000. then est else go (n+ #1.) (step n est)

let estimate () =
let est =
measure_alloc (fun () -> go (of_float 0.) (of_float 0.))
measure_alloc (fun () -> go #0. #0.)
in
Printf.printf "Unboxed:\n estimate: %f\n allocations: %s\n"
((to_float est) *. 4.) (get_allocations ())
Expand Down Expand Up @@ -123,9 +123,9 @@ let print_record_and_allocs s r =
(* Building a record should only allocate the box *)
let[@inline never] build x =
{ a = x;
b = Float_u.of_float 42.0;
b = #42.0;
c = consumer x x;
d = consumer x (Float_u.of_float 1.0) }
d = consumer x #1.0 }

let[@inline never] project_a r = r.a
let[@inline never] update_d r x = r.d <- x
Expand All @@ -136,11 +136,11 @@ let[@inline never] manipulate ({c; _} as r) =
match r with
| { b; _ } ->
update_d r (consumer b (project_a r));
r.b <- Float_u.sub c (Float_u.of_float 21.1);
r.b <- Float_u.sub c #21.1;
r.a

let _ =
let r = measure_alloc_value (fun () -> build (Float_u.of_float 3.14)) in
let r = measure_alloc_value (fun () -> build #3.14) in
print_record_and_allocs "Construction (40 bytes for record)" r;
let _ = measure_alloc (fun () -> manipulate r) in
print_record_and_allocs "Manipulation (0 bytes)" r
Expand All @@ -163,7 +163,7 @@ let[@inline never] cse_test b r =
(0., 0.)

let _ =
let r = build (Float_u.of_float 3.14) in
let r = build #3.14 in
let _ = measure_alloc_value (fun () -> cse_test false r) in
let allocs = get_exact_allocations () in
Printf.printf "CSE test (0 bytes):\n allocated bytes: %.2f\n" allocs
Expand Down
15 changes: 8 additions & 7 deletions ocaml/testsuite/tests/typing-layouts-float64/basics.ml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
(* TEST
* expect
* flambda2
** expect
flags = "-extension layouts_alpha"
* expect
** expect
flags = "-extension layouts_beta"
* expect
** expect
flags = "-extension layouts"
*)

Expand Down Expand Up @@ -746,7 +747,7 @@ type t14_1 = { x : float#; y : float# }
let f14_1 {x;y} = FU.sub x y

(* construction *)
let r14 = { x = FU.of_float 3.14; y = FU.of_float 2.72 }
let r14 = { x = #3.14; y = #2.72 }

let sum14_1 = FU.to_float (f14_1 r14)

Expand All @@ -759,14 +760,14 @@ type t14_2 = { mutable a : float#; b : float#; mutable c : float# }

let f14_3 ({b; c; _} as r) =
(* pure record update *)
let r' = { r with b = FU.of_float 20.0; c = r.a } in
let r' = { r with b = #20.0; c = r.a } in
(* mutation *)
r.a <- FU.sub r.a r'.b;
r'.a <- FU.of_float 42.0;
r'.a <- #42.0;
r'

let a, b, c, a', b', c' =
let r = {a = FU.of_float 3.1; b = FU.of_float (-0.42); c = FU.of_float 27.7 } in
let r = {a = #3.1; b = -#0.42; c = #27.7 } in
let r' = f14_3 r in
FU.to_float r.a,
FU.to_float r.b,
Expand Down
11 changes: 5 additions & 6 deletions ocaml/testsuite/tests/typing-layouts-float64/c_api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
(* This file tests using external C functions with float#. *)

external to_float : float# -> (float[@local_opt]) = "%box_float"
external of_float : (float[@local_opt]) -> float# = "%unbox_float"

let print_floatu s f = Printf.printf "%s: %.2f\n" s (to_float f)
let print_float s f = Printf.printf "%s: %.2f\n" s f
Expand All @@ -33,23 +32,23 @@ external sin_BU_U : (float[@unboxed]) -> float# = "sin_byte" "sin_U_U"
external sin_U_BU : float# -> (float[@unboxed]) = "sin_byte" "sin_U_U"

let sin_two =
let f = sin_U_U (of_float 2.) in
let f = sin_U_U #2. in
print_floatu "Test U -> U, sin two" f

let sin_three =
let f = sin_B_U 3. in
print_floatu "Test B -> U, sin three" f

let sin_four =
let f = sin_U_B (of_float 4.) in
let f = sin_U_B #4. in
print_float "Test U -> B, sin four" f

let sin_five =
let f = sin_BU_U 5. in
print_floatu "Test (B[@unboxed]) -> U, sin five" f

let sin_six =
let f = sin_U_BU (of_float 6.) in
let f = sin_U_BU #6. in
print_float "Test U -> (B[@unboxed]), sin six" f

(* If there are more than 5 args, you get an array in the bytecode version,
Expand All @@ -60,13 +59,13 @@ external sum_7 :

let sum_of_one_to_seven =
let f =
sum_7 (of_float 1.) 2. (of_float 3.) 4. (of_float 5.) 6. (of_float 7.)
sum_7 #1. 2. #3. 4. #5. 6. #7.
in
print_floatu "Function with many args, sum_of_one_to_seven" f

(* Non-inlined eta expansion *)
let[@inline never] sin_U_U' x = sin_U_U x

let sin_seven =
let f = sin_U_U' (of_float 7.) in
let f = sin_U_U' #7. in
print_floatu "Test U -> U eta expansion, sin seven" f
Loading