Skip to content

Commit 91ab70a

Browse files
authored
flambda-backend: Basic uniqueness extension (#1552)
1 parent 5be3cb8 commit 91ab70a

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

+14420
-8969
lines changed

.depend

Lines changed: 84 additions & 3 deletions
Large diffs are not rendered by default.

boot/menhir/parser.ml

Lines changed: 8010 additions & 7516 deletions
Large diffs are not rendered by default.

boot/menhir/parser.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type token =
77
| WHEN
88
| VIRTUAL
99
| VAL
10+
| UNIQUE
1011
| UNDERSCORE
1112
| UIDENT of (string)
1213
| TYPE
@@ -38,6 +39,7 @@ type token =
3839
| OR
3940
| OPTLABEL of (string)
4041
| OPEN
42+
| ONCE
4143
| OF
4244
| OBJECT
4345
| NONREC

boot/ocamlc

63.8 KB
Binary file not shown.

boot/ocamllex

0 Bytes
Binary file not shown.

compilerlibs/Makefile.compilerlibs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ TYPING = \
8484
typing/layouts.cmo \
8585
typing/primitive.cmo \
8686
typing/shape.cmo \
87+
typing/mode.cmo \
8788
typing/types.cmo \
8889
typing/btype.cmo \
8990
typing/oprint.cmo \
@@ -122,6 +123,7 @@ TYPING = \
122123
lambda/debuginfo.cmo lambda/lambda.cmo \
123124
typing/typedecl.cmo \
124125
typing/typeopt.cmo \
126+
typing/uniqueness_analysis.cmo \
125127
typing/rec_check.cmo \
126128
typing/typecore.cmo \
127129
typing/typeclass.cmo \

dune

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@
6969

7070
;; TYPING
7171
ident path layouts primitive shape types btype oprint subst predef datarepr
72-
cmi_format persistent_env env errortrace
72+
cmi_format persistent_env env errortrace mode
7373
typedtree printtyped ctype printtyp includeclass mtype envaux includecore
7474
tast_iterator tast_mapper signature_group cmt_format cms_format untypeast
7575
includemod includemod_errorprinter
7676
typetexp patterns printpat parmatch stypes typedecl typeopt rec_check
77-
typecore
77+
typecore mode uniqueness_analysis
7878
typeclass typemod typedecl_variance typedecl_properties
7979
typedecl_separability cmt2annot
8080
; manual update: mli only files
@@ -299,6 +299,8 @@
299299
(typeopt.mli as compiler-libs/typeopt.mli)
300300
(rec_check.mli as compiler-libs/rec_check.mli)
301301
(typecore.mli as compiler-libs/typecore.mli)
302+
(mode.mli as compiler-libs/mode.mli)
303+
(uniqueness_analysis.mli as compiler-libs/uniqueness_analysis.mli)
302304
(typeclass.mli as compiler-libs/typeclass.mli)
303305
(typemod.mli as compiler-libs/typemod.mli)
304306
(typedecl_variance.mli as compiler-libs/typedecl_variance.mli)

lambda/lambda.ml

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ type field_read_semantics =
4242

4343
include (struct
4444

45-
type alloc_mode =
45+
type locality_mode =
4646
| Alloc_heap
4747
| Alloc_local
4848

49+
type alloc_mode = locality_mode
50+
4951
type modify_mode =
5052
| Modify_heap
5153
| Modify_maybe_stack
@@ -64,31 +66,24 @@ include (struct
6466
let modify_heap = Modify_heap
6567

6668
let modify_maybe_stack : modify_mode =
67-
(* CR zqian: possible to move this check to a better place? *)
68-
(* idealy I don't want to do the checking here.
69-
if stack allocations are disabled, then the alloc_mode which this modify_mode
70-
depends on should be heap, which makes this modify_mode to be heap *)
71-
72-
(* one suggestion: move the check to optimize_allocation;
73-
if stack_allocation not enabled, force all allocations to be heap,
74-
which then propagates to all the other modes.
75-
*)
7669
if Config.stack_allocation then Modify_maybe_stack
7770
else Modify_heap
7871

7972
end : sig
8073

81-
type alloc_mode = private
74+
type locality_mode = private
8275
| Alloc_heap
8376
| Alloc_local
8477

78+
type alloc_mode = locality_mode
79+
8580
type modify_mode = private
8681
| Modify_heap
8782
| Modify_maybe_stack
8883

89-
val alloc_heap : alloc_mode
84+
val alloc_heap : locality_mode
9085

91-
val alloc_local : alloc_mode
86+
val alloc_local : locality_mode
9287

9388
val modify_heap : modify_mode
9489

lambda/lambda.mli

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,22 @@ type immediate_or_pointer =
3434
| Immediate
3535
| Pointer
3636

37-
type alloc_mode = private
37+
type locality_mode = private
3838
| Alloc_heap
3939
| Alloc_local
4040

41+
(** For now we don't have strong update, and thus uniqueness is irrelavent in
42+
middle and back-end; in the future this will be extended with uniqueness *)
43+
type alloc_mode = locality_mode
44+
4145
type modify_mode = private
4246
| Modify_heap
4347
| Modify_maybe_stack
4448

45-
val alloc_heap : alloc_mode
49+
val alloc_heap : locality_mode
4650

4751
(* Actually [Alloc_heap] if [Config.stack_allocation] is [false] *)
48-
val alloc_local : alloc_mode
52+
val alloc_local : locality_mode
4953

5054
val modify_heap : modify_mode
5155

lambda/translcore.ml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ let rec push_defaults loc bindings use_lhs arg_mode arg_sort cases
242242
when use_lhs || trivial_pat pat && exp.exp_desc <> Texp_unreachable ->
243243
[{case with c_rhs = wrap_bindings bindings exp}]
244244
| {c_lhs=pat; c_rhs=exp; c_guard=_} :: _ when bindings <> [] ->
245-
let mode = Value_mode.of_alloc arg_mode in
245+
let mode = Mode.Value.of_alloc arg_mode in
246246
let param = Typecore.name_cases "param" cases in
247247
let desc =
248248
{val_type = pat.pat_type; val_kind = Val_reg;
@@ -261,7 +261,8 @@ let rec push_defaults loc bindings use_lhs arg_mode arg_sort cases
261261
({exp with exp_type = pat.pat_type; exp_env = env; exp_desc =
262262
Texp_ident
263263
(Path.Pident param, mknoloc (Longident.Lident name),
264-
desc, Id_value)},
264+
desc, Id_value,
265+
(Mode.Value.uniqueness mode, Mode.Value.linearity mode))},
265266
arg_sort,
266267
cases, partial) }
267268
in
@@ -346,7 +347,7 @@ let can_apply_primitive p pmode pos args =
346347
else if pos <> Typedtree.Tail then true
347348
else begin
348349
let return_mode = Ctype.prim_mode pmode p.prim_native_repr_res in
349-
is_heap_mode (transl_alloc_mode return_mode)
350+
is_heap_mode (transl_locality_mode return_mode)
350351
end
351352
end
352353

@@ -370,7 +371,7 @@ and transl_exp1 ~scopes ~in_new_scope sort e =
370371

371372
and transl_exp0 ~in_new_scope ~scopes sort e =
372373
match e.exp_desc with
373-
| Texp_ident(path, _, desc, kind) ->
374+
| Texp_ident(path, _, desc, kind, _) ->
374375
transl_ident (of_location ~scopes e.exp_loc)
375376
e.exp_env e.exp_type path desc kind
376377
| Texp_constant cst ->
@@ -388,8 +389,8 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
388389
transl_function ~scopes e alloc_mode param arg_mode arg_sort ret_sort
389390
cases partial warnings region curry
390391
| Texp_apply({ exp_desc = Texp_ident(path, _, {val_kind = Val_prim p},
391-
Id_prim pmode);
392-
exp_type = prim_type; } as funct, oargs, pos, alloc_mode)
392+
Id_prim pmode, _);
393+
exp_type = prim_type; } as funct, oargs, pos, ap_mode)
393394
when can_apply_primitive p pmode pos oargs ->
394395
let rec cut_args prim_repr oargs =
395396
match prim_repr, oargs with
@@ -419,19 +420,19 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
419420
let inlined = Translattribute.get_inlined_attribute funct in
420421
let specialised = Translattribute.get_specialised_attribute funct in
421422
let position = transl_apply_position pos in
422-
let mode = transl_alloc_mode alloc_mode in
423+
let mode = transl_locality_mode ap_mode in
423424
let result_layout = layout_exp sort e in
424425
event_after ~scopes e
425426
(transl_apply ~scopes ~tailcall ~inlined ~specialised ~position ~mode
426427
~result_layout lam extra_args (of_location ~scopes e.exp_loc))
427428
end
428-
| Texp_apply(funct, oargs, position, alloc_mode) ->
429+
| Texp_apply(funct, oargs, position, ap_mode) ->
429430
let tailcall = Translattribute.get_tailcall_attribute funct in
430431
let inlined = Translattribute.get_inlined_attribute funct in
431432
let specialised = Translattribute.get_specialised_attribute funct in
432433
let result_layout = layout_exp sort e in
433434
let position = transl_apply_position position in
434-
let mode = transl_alloc_mode alloc_mode in
435+
let mode = transl_locality_mode ap_mode in
435436
event_after ~scopes e
436437
(transl_apply ~scopes ~tailcall ~inlined ~specialised ~result_layout
437438
~position ~mode (transl_exp ~scopes Sort.for_function funct)
@@ -519,7 +520,7 @@ and transl_exp0 ~in_new_scope ~scopes sort e =
519520
transl_record ~scopes e.exp_loc e.exp_env
520521
(Option.map transl_alloc_mode alloc_mode)
521522
fields representation extended_expression
522-
| Texp_field(arg, _, lbl, alloc_mode) ->
523+
| Texp_field(arg, _, lbl, _, alloc_mode) ->
523524
let targ = transl_exp ~scopes Sort.for_record arg in
524525
let sem =
525526
match lbl.lbl_mut with
@@ -1446,7 +1447,7 @@ and transl_record ~scopes loc env mode fields repres opt_init_expr =
14461447
Array.mapi
14471448
(fun i (lbl, definition) ->
14481449
match definition with
1449-
| Kept typ ->
1450+
| Kept (typ, _) ->
14501451
let field_kind =
14511452
must_be_value (layout env lbl.lbl_loc Sort.for_record_field typ)
14521453
in
@@ -1531,7 +1532,7 @@ and transl_record ~scopes loc env mode fields repres opt_init_expr =
15311532
the init record, we must have already checked for void. *)
15321533
layout_must_be_value lbl.lbl_loc lbl.lbl_layout;
15331534
match definition with
1534-
| Kept _type -> cont
1535+
| Kept (_type, _uu) -> cont
15351536
| Overridden (_lid, expr) ->
15361537
let upd =
15371538
match repres with
@@ -1740,12 +1741,12 @@ and transl_letop ~scopes loc env let_ ands param param_sort case case_sort
17401741
| Some (lhs, _) -> Typeopt.function_arg_layout env loc param_sort lhs
17411742
in
17421743
let return_layout = layout_exp case_sort case.c_rhs in
1743-
let curry = More_args { partial_mode = Alloc_mode.global } in
1744+
let curry = More_args { partial_mode = Mode.Alloc.legacy } in
17441745
let (kind, params, return, _region), body =
17451746
event_function ~scopes case.c_rhs
17461747
(function repr ->
17471748
transl_curried_function ~scopes ~arg_sort:param_sort ~arg_layout
1748-
~arg_mode:(Amode Global) ~return_sort:case_sort
1749+
~arg_mode:Mode.Alloc.legacy ~return_sort:case_sort
17491750
~return_layout case.c_rhs.exp_loc repr ~region:true ~curry partial
17501751
warnings param [case])
17511752
in

lambda/translmode.ml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
open Types
21
open Lambda
3-
let transl_alloc_mode alloc_mode =
4-
match Alloc_mode.constrain_lower alloc_mode with
2+
open Mode
3+
4+
let transl_locality_mode locality =
5+
match Locality.constrain_lower locality with
56
| Global -> alloc_heap
67
| Local -> alloc_local
78

8-
let transl_modify_mode alloc_mode =
9-
match Alloc_mode.constrain_lower alloc_mode with
9+
let transl_alloc_mode mode =
10+
(* we only take the locality axis *)
11+
transl_locality_mode (Alloc.locality mode)
12+
13+
let transl_modify_mode locality =
14+
match Locality.constrain_lower locality with
1015
| Global -> modify_heap
1116
| Local -> modify_maybe_stack

lambda/translmode.mli

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
val transl_alloc_mode : Types.alloc_mode -> Lambda.alloc_mode
1+
open Mode
22

3-
val transl_modify_mode : Types.alloc_mode -> Lambda.modify_mode
3+
val transl_locality_mode : Locality.t -> Lambda.locality_mode
4+
5+
val transl_alloc_mode : Alloc.t -> Lambda.alloc_mode
6+
7+
val transl_modify_mode : Locality.t -> Lambda.modify_mode

lambda/translprim.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ let gen_array_set_kind mode =
124124
let prim_sys_argv =
125125
Primitive.simple_on_values ~name:"caml_sys_argv" ~arity:1 ~alloc:true
126126

127-
let to_alloc_mode ~poly = function
127+
let to_locality ~poly = function
128128
| Prim_global, _ -> alloc_heap
129129
| Prim_local, _ -> alloc_local
130130
| Prim_poly, _ ->
131131
match poly with
132132
| None -> assert false
133-
| Some mode -> transl_alloc_mode mode
133+
| Some locality -> transl_locality_mode locality
134134

135135
let to_modify_mode ~poly = function
136136
| Prim_global, _ -> modify_heap
@@ -141,7 +141,7 @@ let to_modify_mode ~poly = function
141141
| Some mode -> transl_modify_mode mode
142142

143143
let lookup_primitive loc poly pos p =
144-
let mode = to_alloc_mode ~poly p.prim_native_repr_res in
144+
let mode = to_locality ~poly p.prim_native_repr_res in
145145
let arg_modes = List.map (to_modify_mode ~poly) p.prim_native_repr_args in
146146
let get_first_arg_mode () =
147147
match arg_modes with
@@ -855,8 +855,8 @@ let lambda_of_prim prim_name prim loc args arg_exps =
855855
let check_primitive_arity loc p =
856856
let mode =
857857
match p.prim_native_repr_res with
858-
| Prim_global, _ | Prim_poly, _ -> Some Alloc_mode.global
859-
| Prim_local, _ -> Some Alloc_mode.local
858+
| Prim_global, _ | Prim_poly, _ -> Some Mode.Locality.global
859+
| Prim_local, _ -> Some Mode.Locality.local
860860
in
861861
let prim = lookup_primitive loc mode Rc_normal p in
862862
let ok =
@@ -890,7 +890,7 @@ let transl_primitive loc p env ty ~poly_mode path =
890890
| None -> prim
891891
| Some prim -> prim
892892
in
893-
let to_alloc_mode = to_alloc_mode ~poly:poly_mode in
893+
let to_locality = to_locality ~poly:poly_mode in
894894
let rec make_params ty repr_args repr_res =
895895
match repr_args, repr_res with
896896
| [], (_, res_repr) ->
@@ -906,7 +906,7 @@ let transl_primitive loc p env ty ~poly_mode path =
906906
let arg_layout =
907907
Typeopt.layout env (to_location loc) (Sort.of_const arg_sort) arg_ty
908908
in
909-
let arg_mode = to_alloc_mode arg in
909+
let arg_mode = to_locality arg in
910910
let params, return = make_params ret_ty repr_args repr_res in
911911
{ name = Ident.create_local "prim";
912912
layout = arg_layout;
@@ -928,7 +928,7 @@ let transl_primitive loc p env ty ~poly_mode path =
928928
in
929929
let body = lambda_of_prim p.prim_name prim loc args None in
930930
let region =
931-
match to_alloc_mode p.prim_native_repr_res with
931+
match to_locality p.prim_native_repr_res with
932932
| Alloc_heap -> true
933933
| Alloc_local -> false
934934
in
@@ -938,7 +938,7 @@ let transl_primitive loc p env ty ~poly_mode path =
938938
| Alloc_heap :: args -> count_nlocal args
939939
| (Alloc_local :: _) as args -> List.length args
940940
in
941-
let nlocal = count_nlocal (List.map to_alloc_mode p.prim_native_repr_args) in
941+
let nlocal = count_nlocal (List.map to_locality p.prim_native_repr_args) in
942942
lfunction
943943
~kind:(Curried {nlocal})
944944
~params

lambda/translprim.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ val check_primitive_arity :
3535
val transl_primitive :
3636
Lambda.scoped_location -> Primitive.description -> Env.t ->
3737
Types.type_expr ->
38-
poly_mode:Types.alloc_mode option ->
38+
poly_mode:Mode.Locality.t option ->
3939
Path.t option ->
4040
Lambda.lambda
4141

4242
val transl_primitive_application :
4343
Lambda.scoped_location -> Primitive.description -> Env.t ->
44-
Types.type_expr -> Types.alloc_mode option -> Path.t ->
44+
Types.type_expr -> Mode.Locality.t option -> Path.t ->
4545
Typedtree.expression option ->
4646
Lambda.lambda list -> Typedtree.expression list ->
4747
Lambda.region_close -> Lambda.lambda

otherlibs/dynlink/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ COMPILERLIBS_SOURCES=\
108108
parsing/ast_mapper.ml \
109109
parsing/attr_helper.ml \
110110
parsing/pprintast.ml \
111+
typing/mode.ml \
111112
typing/path.ml \
112113
typing/shape.ml \
113114
typing/layouts.ml \

0 commit comments

Comments
 (0)