Skip to content

Commit c69bd92

Browse files
authored
flambda-backend: Implement unboxed float literals (#2025)
* implement unboxed float literals * fix test * allocation test * remove unused error * make unboxed literals alpha * add native test * clean up test output * pass args for alloc test * remove print_flush * refactor Untypeast.constant * add comment * add missing build_other case
1 parent 2698bd3 commit c69bd92

File tree

19 files changed

+306
-130
lines changed

19 files changed

+306
-130
lines changed

boot/menhir/parser.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ end = struct
11871187

11881188
let assert_unboxed_literals ~loc =
11891189
Language_extension.(
1190-
Jane_syntax_parsing.assert_extension_enabled ~loc Layouts Beta)
1190+
Jane_syntax_parsing.assert_extension_enabled ~loc Layouts Alpha)
11911191

11921192
let unboxed ~loc x =
11931193
assert_unboxed_literals ~loc:(make_loc loc);

lambda/matching.ml

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,7 @@ let can_group discr pat =
11721172
| Constant (Const_char _), Constant (Const_char _)
11731173
| Constant (Const_string _), Constant (Const_string _)
11741174
| Constant (Const_float _), Constant (Const_float _)
1175+
| Constant (Const_unboxed_float _), Constant (Const_unboxed_float _)
11751176
| Constant (Const_int32 _), Constant (Const_int32 _)
11761177
| Constant (Const_int64 _), Constant (Const_int64 _)
11771178
| Constant (Const_nativeint _), Constant (Const_nativeint _) ->
@@ -1195,7 +1196,7 @@ let can_group discr pat =
11951196
( Any
11961197
| Constant
11971198
( Const_int _ | Const_char _ | Const_string _ | Const_float _
1198-
| Const_int32 _ | Const_int64 _ | Const_nativeint _ )
1199+
| Const_unboxed_float _ | Const_int32 _ | Const_int64 _ | Const_nativeint _ )
11991200
| Construct _ | Tuple _ | Record _ | Array _ | Variant _ | Lazy ) ) ->
12001201
false
12011202

@@ -2399,7 +2400,7 @@ let rec do_tests_fail value_kind loc fail tst arg = function
23992400
| [] -> fail
24002401
| (c, act) :: rem ->
24012402
Lifthenelse
2402-
( Lprim (tst, [ arg; Lconst (Const_base c) ], loc),
2403+
( Lprim (tst, [ arg; c ], loc),
24032404
do_tests_fail value_kind loc fail tst arg rem,
24042405
act, value_kind )
24052406

@@ -2408,15 +2409,16 @@ let rec do_tests_nofail value_kind loc tst arg = function
24082409
| [ (_, act) ] -> act
24092410
| (c, act) :: rem ->
24102411
Lifthenelse
2411-
( Lprim (tst, [ arg; Lconst (Const_base c) ], loc),
2412+
( Lprim (tst, [ arg; c ], loc),
24122413
do_tests_nofail value_kind loc tst arg rem,
24132414
act, value_kind )
24142415

2415-
let make_test_sequence value_kind loc fail tst lt_tst arg const_lambda_list =
2416+
let make_test_sequence value_kind loc fail tst lt_tst arg const_lambda_list transl_const =
24162417
let const_lambda_list = sort_lambda_list const_lambda_list in
24172418
let hs, const_lambda_list, fail =
24182419
share_actions_tree value_kind const_lambda_list fail
24192420
in
2421+
let const_lambda_list = List.map (fun (c, l) -> transl_const c, l) const_lambda_list in
24202422
let rec make_test_sequence const_lambda_list =
24212423
if List.length const_lambda_list >= 4 && lt_tst <> Pignore then
24222424
split_sequence const_lambda_list
@@ -2429,7 +2431,7 @@ let make_test_sequence value_kind loc fail tst lt_tst arg const_lambda_list =
24292431
rev_split_at (List.length const_lambda_list / 2) const_lambda_list
24302432
in
24312433
Lifthenelse
2432-
( Lprim (lt_tst, [ arg; Lconst (Const_base (fst (List.hd list2))) ], loc),
2434+
( Lprim (lt_tst, [ arg; fst (List.hd list2) ], loc),
24332435
make_test_sequence list1,
24342436
make_test_sequence list2, value_kind )
24352437
in
@@ -2826,6 +2828,16 @@ let mk_failaction_pos partial seen ctx defs =
28262828
let combine_constant value_kind loc arg cst partial ctx def
28272829
(const_lambda_list, total, _pats) =
28282830
let fail, local_jumps = mk_failaction_neg partial ctx def in
2831+
let transl_const = function
2832+
| Const_int c -> Lconst(Const_base (Const_int c))
2833+
| Const_char c -> Lconst(Const_base (Const_char c))
2834+
| Const_string (s,loc,d) -> Lconst(Const_base (Const_string (s,loc,d)))
2835+
| Const_float c -> Lconst(Const_base (Const_float c))
2836+
| Const_int32 c -> Lconst(Const_base (Const_int32 c))
2837+
| Const_int64 c -> Lconst(Const_base (Const_int64 c))
2838+
| Const_nativeint c -> Lconst(Const_base (Const_nativeint c))
2839+
| Const_unboxed_float f -> Lconst (Const_base (Const_float f))
2840+
in
28292841
let lambda1 =
28302842
match cst with
28312843
| Const_int _ ->
@@ -2865,22 +2877,28 @@ let combine_constant value_kind loc arg cst partial ctx def
28652877
| Const_float _ ->
28662878
make_test_sequence value_kind loc fail (Pfloatcomp CFneq)
28672879
(Pfloatcomp CFlt) arg
2868-
const_lambda_list
2880+
const_lambda_list transl_const
2881+
| Const_unboxed_float _ ->
2882+
make_test_sequence value_kind loc fail
2883+
(Pfloatcomp CFneq)
2884+
(Pfloatcomp CFlt)
2885+
(Lprim (Pbox_float Lambda.alloc_local, [arg], loc))
2886+
const_lambda_list transl_const
28692887
| Const_int32 _ ->
28702888
make_test_sequence value_kind loc fail
28712889
(Pbintcomp (Pint32, Cne))
28722890
(Pbintcomp (Pint32, Clt))
2873-
arg const_lambda_list
2891+
arg const_lambda_list transl_const
28742892
| Const_int64 _ ->
28752893
make_test_sequence value_kind loc fail
28762894
(Pbintcomp (Pint64, Cne))
28772895
(Pbintcomp (Pint64, Clt))
2878-
arg const_lambda_list
2896+
arg const_lambda_list transl_const
28792897
| Const_nativeint _ ->
28802898
make_test_sequence value_kind loc fail
28812899
(Pbintcomp (Pnativeint, Cne))
28822900
(Pbintcomp (Pnativeint, Clt))
2883-
arg const_lambda_list
2901+
arg const_lambda_list transl_const
28842902
in
28852903
(lambda1, Jumps.union local_jumps total)
28862904

lambda/translcore.ml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,18 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
390390
transl_ident (of_location ~scopes e.exp_loc)
391391
e.exp_env e.exp_type path desc kind
392392
| Texp_constant cst ->
393-
Lconst(Const_base cst)
393+
begin match cst with
394+
| Const_int c -> Lconst(Const_base (Const_int c))
395+
| Const_char c -> Lconst(Const_base (Const_char c))
396+
| Const_string (s,loc,d) -> Lconst(Const_base (Const_string (s,loc,d)))
397+
| Const_float c -> Lconst(Const_base (Const_float c))
398+
| Const_int32 c -> Lconst(Const_base (Const_int32 c))
399+
| Const_int64 c -> Lconst(Const_base (Const_int64 c))
400+
| Const_nativeint c -> Lconst(Const_base (Const_nativeint c))
401+
| Const_unboxed_float f ->
402+
Lprim (Punbox_float, [Lconst (Const_base (Const_float f))],
403+
of_location ~scopes e.exp_loc)
404+
end
394405
| Texp_let(rec_flag, pat_expr_list, body) ->
395406
let return_layout = layout_exp sort body in
396407
transl_let ~scopes ~return_layout rec_flag pat_expr_list

parsing/parser.mly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ end = struct
962962

963963
let assert_unboxed_literals ~loc =
964964
Language_extension.(
965-
Jane_syntax_parsing.assert_extension_enabled ~loc Layouts Beta)
965+
Jane_syntax_parsing.assert_extension_enabled ~loc Layouts Alpha)
966966

967967
let unboxed ~loc x =
968968
assert_unboxed_literals ~loc:(make_loc loc);

testsuite/tests/typing-layouts-float64/alloc.ml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(* TEST
22
* flambda2
3-
flags = "-extension layouts_beta"
3+
flags = "-extension layouts_alpha"
44
** native
55
*)
66

@@ -167,3 +167,18 @@ let _ =
167167
let _ = measure_alloc_value (fun () -> cse_test false r) in
168168
let allocs = get_exact_allocations () in
169169
Printf.printf "CSE test (0 bytes):\n allocated bytes: %.2f\n" allocs
170+
171+
let[@inline never] literal_test x y =
172+
let open Float_u in
173+
(#1. + x) * (y - #4.) / (#3. ** #1.)
174+
175+
let print_allocs s =
176+
let allocs = get_exact_allocations () in
177+
Printf.printf
178+
"%s:\n allocated bytes: %.2f\n"
179+
s allocs
180+
181+
let _ =
182+
let r = measure_alloc (fun () -> literal_test #2. #3.) in
183+
assert (Float_u.equal r (-#1.));
184+
print_allocs "Float literals";

testsuite/tests/typing-layouts-float64/alloc.reference

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ Manipulation (0 bytes):
1818
d: 45.14
1919
CSE test (0 bytes):
2020
allocated bytes: 0.00
21+
Float literals:
22+
allocated bytes: 0.00

0 commit comments

Comments
 (0)