Skip to content

Commit cbbe49f

Browse files
authored
flambda-backend: Change lambda constant type to support native unboxed float (#2071)
* change lambda constant type * fix upstream * fix test * remove the transl_constant function * define declare_const_result type * better error messages
1 parent 95507ca commit cbbe49f

File tree

13 files changed

+35
-37
lines changed

13 files changed

+35
-37
lines changed

bytecomp/emitcode.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
open Config
1919
open Misc
20-
open Asttypes
2120
open Lambda
2221
open Instruct
2322
open Opcodes

bytecomp/symtable.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
(* To assign numbers to globals and primitives *)
1919

2020
open Misc
21-
open Asttypes
2221
open Lambda
2322
open Cmo_format
2423

@@ -149,7 +148,8 @@ let rec transl_const = function
149148
Const_base(Const_int i) -> Obj.repr i
150149
| Const_base(Const_char c) -> Obj.repr c
151150
| Const_base(Const_string (s, _, _)) -> Obj.repr s
152-
| Const_base(Const_float f) -> Obj.repr (float_of_string f)
151+
| Const_base(Const_float f)
152+
| Const_base(Const_unboxed_float f) -> Obj.repr (float_of_string f)
153153
| Const_base(Const_int32 i) -> Obj.repr i
154154
| Const_base(Const_int64 i) -> Obj.repr i
155155
| Const_base(Const_nativeint i) -> Obj.repr i

lambda/lambda.ml

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
open Misc
1717
open Asttypes
1818

19+
type constant = Typedtree.constant
20+
1921
type mutable_flag = Immutable | Immutable_unique | Mutable
2022

2123
type compile_time_constant =
@@ -1094,19 +1096,6 @@ let transl_prim mod_name name =
10941096
| exception Not_found ->
10951097
fatal_error ("Primitive " ^ name ^ " not found.")
10961098

1097-
(* Translation of constants *)
1098-
1099-
let transl_constant loc (cst : Typedtree.constant) = match cst with
1100-
| Const_int c -> Lconst(Const_base (Const_int c))
1101-
| Const_char c -> Lconst(Const_base (Const_char c))
1102-
| Const_string (s,loc,d) -> Lconst(Const_base (Const_string (s,loc,d)))
1103-
| Const_float c -> Lconst(Const_base (Const_float c))
1104-
| Const_int32 c -> Lconst(Const_base (Const_int32 c))
1105-
| Const_int64 c -> Lconst(Const_base (Const_int64 c))
1106-
| Const_nativeint c -> Lconst(Const_base (Const_nativeint c))
1107-
| Const_unboxed_float f ->
1108-
Lprim (Punbox_float, [Lconst (Const_base (Const_float f))], loc)
1109-
11101099
(* Compile a sequence of expressions *)
11111100

11121101
let rec make_sequence fn = function
@@ -1546,13 +1535,14 @@ let primitive_may_allocate : primitive -> alloc_mode option = function
15461535
| Patomic_fetch_add
15471536
| Pdls_get -> None
15481537

1549-
let constant_layout = function
1538+
let constant_layout: constant -> layout = function
15501539
| Const_int _ | Const_char _ -> Pvalue Pintval
15511540
| Const_string _ -> Pvalue Pgenval
15521541
| Const_int32 _ -> Pvalue (Pboxedintval Pint32)
15531542
| Const_int64 _ -> Pvalue (Pboxedintval Pint64)
15541543
| Const_nativeint _ -> Pvalue (Pboxedintval Pnativeint)
15551544
| Const_float _ -> Pvalue Pfloatval
1545+
| Const_unboxed_float _ -> Punboxed_float
15561546

15571547
let structured_constant_layout = function
15581548
| Const_base const -> constant_layout const

lambda/lambda.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
open Asttypes
1919

20+
type constant = Typedtree.constant
21+
2022
(* Overriding Asttypes.mutable_flag *)
2123
type mutable_flag = Immutable | Immutable_unique | Mutable
2224

@@ -665,8 +667,6 @@ val transl_prim: string -> string -> lambda
665667
]}
666668
*)
667669

668-
val transl_constant : scoped_location -> Typedtree.constant -> lambda
669-
670670
val free_variables: lambda -> Ident.Set.t
671671

672672
val transl_module_path: scoped_location -> Env.t -> Path.t -> lambda

lambda/matching.ml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,7 +2400,7 @@ let rec do_tests_fail value_kind loc fail tst arg = function
24002400
| [] -> fail
24012401
| (c, act) :: rem ->
24022402
Lifthenelse
2403-
( Lprim (tst, [ arg; c ], loc),
2403+
( Lprim (tst, [ arg; Lconst (Const_base c) ], loc),
24042404
do_tests_fail value_kind loc fail tst arg rem,
24052405
act, value_kind )
24062406

@@ -2409,16 +2409,15 @@ let rec do_tests_nofail value_kind loc tst arg = function
24092409
| [ (_, act) ] -> act
24102410
| (c, act) :: rem ->
24112411
Lifthenelse
2412-
( Lprim (tst, [ arg; c ], loc),
2412+
( Lprim (tst, [ arg; Lconst (Const_base c) ], loc),
24132413
do_tests_nofail value_kind loc tst arg rem,
24142414
act, value_kind )
24152415

2416-
let make_test_sequence value_kind loc fail tst lt_tst arg const_lambda_list transl_const =
2416+
let make_test_sequence value_kind loc fail tst lt_tst arg const_lambda_list =
24172417
let const_lambda_list = sort_lambda_list const_lambda_list in
24182418
let hs, const_lambda_list, fail =
24192419
share_actions_tree value_kind const_lambda_list fail
24202420
in
2421-
let const_lambda_list = List.map (fun (c, l) -> transl_const c, l) const_lambda_list in
24222421
let rec make_test_sequence const_lambda_list =
24232422
if List.length const_lambda_list >= 4 && lt_tst <> Pignore then
24242423
split_sequence const_lambda_list
@@ -2431,7 +2430,7 @@ let make_test_sequence value_kind loc fail tst lt_tst arg const_lambda_list tran
24312430
rev_split_at (List.length const_lambda_list / 2) const_lambda_list
24322431
in
24332432
Lifthenelse
2434-
( Lprim (lt_tst, [ arg; fst (List.hd list2) ], loc),
2433+
( Lprim (lt_tst, [ arg; Lconst (Const_base (fst (List.hd list2))) ], loc),
24352434
make_test_sequence list1,
24362435
make_test_sequence list2, value_kind )
24372436
in
@@ -2828,7 +2827,6 @@ let mk_failaction_pos partial seen ctx defs =
28282827
let combine_constant value_kind loc arg cst partial ctx def
28292828
(const_lambda_list, total, _pats) =
28302829
let fail, local_jumps = mk_failaction_neg partial ctx def in
2831-
let transl_const = transl_constant loc in
28322830
let lambda1 =
28332831
match cst with
28342832
| Const_int _ ->
@@ -2868,27 +2866,27 @@ let combine_constant value_kind loc arg cst partial ctx def
28682866
| Const_float _ ->
28692867
make_test_sequence value_kind loc fail (Pfloatcomp CFneq)
28702868
(Pfloatcomp CFlt) arg
2871-
const_lambda_list transl_const
2869+
const_lambda_list
28722870
| Const_unboxed_float _ ->
28732871
make_test_sequence value_kind loc fail
28742872
(Punboxed_float_comp CFneq)
28752873
(Punboxed_float_comp CFlt)
2876-
arg const_lambda_list transl_const
2874+
arg const_lambda_list
28772875
| Const_int32 _ ->
28782876
make_test_sequence value_kind loc fail
28792877
(Pbintcomp (Pint32, Cne))
28802878
(Pbintcomp (Pint32, Clt))
2881-
arg const_lambda_list transl_const
2879+
arg const_lambda_list
28822880
| Const_int64 _ ->
28832881
make_test_sequence value_kind loc fail
28842882
(Pbintcomp (Pint64, Cne))
28852883
(Pbintcomp (Pint64, Clt))
2886-
arg const_lambda_list transl_const
2884+
arg const_lambda_list
28872885
| Const_nativeint _ ->
28882886
make_test_sequence value_kind loc fail
28892887
(Pbintcomp (Pnativeint, Cne))
28902888
(Pbintcomp (Pnativeint, Clt))
2891-
arg const_lambda_list transl_const
2889+
arg const_lambda_list
28922890
in
28932891
(lambda1, Jumps.union local_jumps total)
28942892

lambda/printlambda.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ let rec struct_const ppf = function
2626
| Const_base(Const_string (s, _, _)) -> fprintf ppf "%S" s
2727
| Const_immstring s -> fprintf ppf "#%S" s
2828
| Const_base(Const_float f) -> fprintf ppf "%s" f
29+
| Const_base(Const_unboxed_float f) ->
30+
let s =
31+
match String.split_on_char '-' f with
32+
| [""; f] -> "-#" ^ f
33+
| [f] -> "#" ^ f
34+
| _ -> Misc.fatal_errorf "Invalid Const_unboxed_float constant: %s" f
35+
in
36+
fprintf ppf "%s" s
2937
| Const_base(Const_int32 n) -> fprintf ppf "%lil" n
3038
| Const_base(Const_int64 n) -> fprintf ppf "%LiL" n
3139
| Const_base(Const_nativeint n) -> fprintf ppf "%nin" n

lambda/simplif.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
(* Elimination of useless Llet(Alias) bindings.
1717
Also transform let-bound references into variables. *)
1818

19-
open Asttypes
2019
open Lambda
2120
open Debuginfo.Scoped_location
2221

lambda/translcore.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
389389
| Texp_ident(path, _, desc, kind, _) ->
390390
transl_ident (of_location ~scopes e.exp_loc)
391391
e.exp_env e.exp_type path desc kind
392-
| Texp_constant cst -> transl_constant (of_location ~scopes e.exp_loc) cst
392+
| Texp_constant cst -> Lconst (Const_base cst)
393393
| Texp_let(rec_flag, pat_expr_list, body) ->
394394
let return_layout = layout_exp sort body in
395395
transl_let ~scopes ~return_layout rec_flag pat_expr_list

lambda/translobj.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
(* *)
1414
(**************************************************************************)
1515

16-
open Asttypes
1716
open Lambda
1817

1918
(* Get oo primitives identifiers *)

lambda/translprim.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
(* Translation of primitives *)
1717

18-
open Asttypes
1918
open Primitive
2019
open Types
2120
open Typedtree

middle_end/closure/closure.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
(* Introduction of closures, uncurrying, recognition of direct calls *)
1717

1818
open Misc
19-
open Asttypes
2019
open Primitive
2120
open Lambda
2221
open Switch
@@ -1028,6 +1027,9 @@ let rec close ({ backend; fenv; cenv ; mutable_vars; kinds; catch_env } as env)
10281027
| Const_base (Const_string (s, _, _)) ->
10291028
str (Uconst_string s)
10301029
| Const_base(Const_float x) -> str (Uconst_float (float_of_string x))
1030+
| Const_base(Const_unboxed_float _) ->
1031+
(* CR alanechang: implement unboxed float constants in closure *)
1032+
Misc.fatal_error "Unboxed float constants are not supported in closure. Consider using flambda2."
10311033
| Const_base(Const_int32 x) -> str (Uconst_int32 x)
10321034
| Const_base(Const_int64 x) -> str (Uconst_int64 x)
10331035
| Const_base(Const_nativeint x) -> str (Uconst_nativeint x)

middle_end/flambda/closure_conversion.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ let rec declare_const t (const : Lambda.structured_constant)
123123
match const with
124124
| Const_base (Const_int c) -> (Const (Int c), Names.const_int)
125125
| Const_base (Const_char c) -> (Const (Char c), Names.const_char)
126+
| Const_base (Const_unboxed_float _) ->
127+
(* CR alanechang: implement unboxed float constants in flambda *)
128+
Misc.fatal_error "Unboxed float constants are not supported in flambda. Consider using flambda2."
126129
| Const_base (Const_string (s, _, _)) ->
127130
let const, name =
128131
(Flambda.Allocated_const (Immutable_string s),

testsuite/tests/typing-layouts/literals_native.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(* TEST
2+
* flambda2
23
flags = "-extension layouts_alpha"
3-
* native
4-
* bytecode
4+
** native
5+
** bytecode
56
*)
67

78
(*****************************************)

0 commit comments

Comments
 (0)