Skip to content

Commit aa5ae46

Browse files
mshinwellEkdohibs
authored andcommitted
Code review
1 parent 6b61d36 commit aa5ae46

29 files changed

+260
-211
lines changed

backend/cmm.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ and operation =
224224
| Copaque (* Sys.opaque_identity *)
225225
| Cbeginregion | Cendregion
226226
| Ctuple_field of int * machtype array
227+
(* the [machtype array] refers to the whole tuple *)
227228

228229
(* This is information used exclusively during construction of cmm terms by
229230
cmmgen, and thus irrelevant for selectgen and flambda2. *)

backend/cmm_helpers.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ module Extended_machtype = struct
10781078
typ_any_int
10791079
| Pvalue Pintval -> typ_tagged_int
10801080
| Pvalue _ -> typ_val
1081-
| Punboxed_product fields -> Array.concat @@ List.map of_layout fields
1081+
| Punboxed_product fields -> Array.concat (List.map of_layout fields)
10821082
end
10831083

10841084
let machtype_of_layout layout =

backend/selectgen.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -921,18 +921,20 @@ method emit_expr_aux (env:environment) exp :
921921
ret (self#insert_op_debug env Iopaque dbg rs rs)
922922
end
923923
| Cop(Ctuple_field(field, fields_layout), [arg], dbg) ->
924-
begin match self#emit_expr env arg with
924+
begin match self#emit_expr env arg with
925925
None -> None
926926
| Some loc_exp ->
927-
let flat_size a = Array.fold_left (fun acc t -> acc + Array.length t) 0 a in
927+
let flat_size a =
928+
Array.fold_left (fun acc t -> acc + Array.length t) 0 a
929+
in
928930
assert(Array.length loc_exp = flat_size fields_layout);
929931
let before = Array.sub fields_layout 0 field in
930932
let size_before = flat_size before in
931933
let field_slice =
932934
Array.sub loc_exp size_before (Array.length fields_layout.(field))
933935
in
934936
ret field_slice
935-
end
937+
end
936938
| Cop(op, args, dbg) ->
937939
begin match self#emit_parts_list env args with
938940
None -> None

middle_end/.ocamlformat-enable

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
clambda_layout.ml
2+
clambda_layout.mli
13
mangling.ml
24
mangling.mli

middle_end/clambda_layout.ml

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,43 @@
1+
(**************************************************************************)
2+
(* *)
3+
(* OCaml *)
4+
(* *)
5+
(* Pierre Chambart, OCamlPro *)
6+
(* *)
7+
(* Copyright 2023 OCamlPro SAS *)
8+
(* *)
9+
(* All rights reserved. This file is distributed under the terms of *)
10+
(* the GNU Lesser General Public License version 2.1, with the *)
11+
(* special exception on linking described in the file LICENSE. *)
12+
(* *)
13+
(**************************************************************************)
14+
115
type atom =
216
| Value
317
| Value_int
418
| Unboxed_float
519
| Unboxed_int of Lambda.boxed_integer
620

7-
let rec fold_left_layout (f : 'acc -> 'e -> atom -> 'acc)
8-
(acc : 'acc) (expr : Clambda.ulambda) (layout : Clambda_primitives.layout) : 'acc =
21+
let rec fold_left_layout (f : 'acc -> 'e -> atom -> 'acc) (acc : 'acc)
22+
(expr : Clambda.ulambda) (layout : Clambda_primitives.layout) : 'acc =
923
match layout with
10-
| Ptop ->
11-
Misc.fatal_error "[Ptop] can't be stored in a closure."
24+
| Ptop -> Misc.fatal_error "[Ptop] can't be stored in a closure."
1225
| Pbottom ->
1326
Misc.fatal_error
14-
"[Pbottom] should have been eliminated as dead code \
15-
and not stored in a closure."
27+
"[Pbottom] should have been eliminated as dead code and not stored in a \
28+
closure."
1629
| Punboxed_float -> f acc expr Unboxed_float
1730
| Punboxed_int bi -> f acc expr (Unboxed_int bi)
1831
| Pvalue Pintval -> f acc expr Value_int
1932
| Pvalue _ -> f acc expr Value
2033
| Punboxed_product layouts ->
21-
List.fold_left (fun acc (field, layout) ->
34+
List.fold_left
35+
(fun acc (field, layout) ->
2236
let expr : Clambda.ulambda =
23-
Uprim (Punboxed_product_field (field, layouts), [expr], Debuginfo.none) in
24-
fold_left_layout f acc expr layout) acc
37+
Uprim (Punboxed_product_field (field, layouts), [expr], Debuginfo.none)
38+
in
39+
fold_left_layout f acc expr layout)
40+
acc
2541
(List.mapi (fun i v -> i, v) layouts)
2642

2743
type ('visible, 'invisible) decomposition' =
@@ -30,10 +46,12 @@ type ('visible, 'invisible) decomposition' =
3046
| Product of ('visible, 'invisible) decomposition' array
3147

3248
type decomposition =
33-
| Atom of { offset : int; layout : atom }
49+
| Atom of
50+
{ offset : int;
51+
layout : atom
52+
}
3453
| Product of decomposition array
3554

36-
3755
let print_atom ppf = function
3856
| Value -> Format.fprintf ppf "val"
3957
| Value_int -> Format.fprintf ppf "int"
@@ -42,7 +60,8 @@ let print_atom ppf = function
4260
| Unboxed_int Pint64 -> Format.fprintf ppf "unboxed_int64"
4361
| Unboxed_int Pnativeint -> Format.fprintf ppf "unboxed_nativeint"
4462

45-
let equal_decomposition = (=)
63+
let equal_decomposition = ( = )
64+
4665
let rec print_decomposition ppf dec =
4766
match dec with
4867
| Atom { offset; layout } ->
@@ -54,30 +73,26 @@ let rec print_decomposition ppf dec =
5473

5574
let rec decompose (layout : Lambda.layout) : _ decomposition' =
5675
match layout with
57-
| Ptop ->
58-
Misc.fatal_error "[Ptop] can't be stored in a closure."
76+
| Ptop -> Misc.fatal_error "[Ptop] can't be stored in a closure."
5977
| Pbottom ->
6078
Misc.fatal_error
61-
"[Pbottom] should have been eliminated as dead code \
62-
and not stored in a closure."
79+
"[Pbottom] should have been eliminated as dead code and not stored in a \
80+
closure."
6381
| Punboxed_float -> Gc_invisible ((), Unboxed_float)
6482
| Punboxed_int bi -> Gc_invisible ((), Unboxed_int bi)
6583
| Pvalue Pintval -> Gc_invisible ((), Value_int)
6684
| Pvalue _ -> Gc_visible ((), Value)
67-
| Punboxed_product l ->
68-
Product (Array.of_list (List.map decompose l))
85+
| Punboxed_product l -> Product (Array.of_list (List.map decompose l))
6986

7087
let rec solidify (dec : (int, int) decomposition') : decomposition =
7188
match dec with
7289
| Gc_visible (offset, layout) -> Atom { offset; layout }
7390
| Gc_invisible (offset, layout) -> Atom { offset; layout }
74-
| Product a ->
75-
Product (Array.map solidify a)
91+
| Product a -> Product (Array.map solidify a)
7692

77-
let rec fold_decompose
78-
(f1 : 'acc -> 'a -> atom -> 'acc * 'b) (f2 : 'acc -> 'c -> atom -> 'acc * 'd)
79-
(acc : 'acc) (d : ('a, 'c) decomposition') :
80-
'acc * ('b, 'd) decomposition' =
93+
let rec fold_decompose (f1 : 'acc -> 'a -> atom -> 'acc * 'b)
94+
(f2 : 'acc -> 'c -> atom -> 'acc * 'd) (acc : 'acc)
95+
(d : ('a, 'c) decomposition') : 'acc * ('b, 'd) decomposition' =
8196
match d with
8297
| Gc_visible (v, layout) ->
8398
let acc, v = f1 acc v layout in
@@ -90,36 +105,22 @@ let rec fold_decompose
90105
acc, Product elts
91106

92107
let atom_size (layout : atom) =
93-
match layout with
94-
| Value
95-
| Value_int
96-
| Unboxed_float
97-
| Unboxed_int _ -> 1
108+
match layout with Value | Value_int | Unboxed_float | Unboxed_int _ -> 1
98109

99110
let assign_invisible_offsets init_pos (var, dec) =
100-
let f_visible acc () _layout =
101-
acc, ()
102-
in
103-
let f_invisible acc () layout =
104-
acc + atom_size layout, acc
105-
in
111+
let f_visible acc () _layout = acc, () in
112+
let f_invisible acc () layout = acc + atom_size layout, acc in
106113
let acc, dec = fold_decompose f_visible f_invisible init_pos dec in
107114
acc, (var, dec)
108115

109116
let assign_visible_offsets init_pos (var, dec) =
110-
let f_visible acc () layout =
111-
acc + atom_size layout, acc
112-
in
113-
let f_invisible acc off _layout =
114-
acc, off
115-
in
117+
let f_visible acc () layout = acc + atom_size layout, acc in
118+
let f_invisible acc off _layout = acc, off in
116119
let acc, dec = fold_decompose f_visible f_invisible init_pos dec in
117120
acc, (var, solidify dec)
118121

119122
let decompose_free_vars ~base_offset ~free_vars =
120-
let free_vars =
121-
List.map (fun (var, kind) -> var, decompose kind) free_vars
122-
in
123+
let free_vars = List.map (fun (var, kind) -> var, decompose kind) free_vars in
123124
let base_offset, free_vars =
124125
List.fold_left_map assign_invisible_offsets base_offset free_vars
125126
in

middle_end/clambda_layout.mli

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1+
(**************************************************************************)
2+
(* *)
3+
(* OCaml *)
4+
(* *)
5+
(* Pierre Chambart, OCamlPro *)
6+
(* *)
7+
(* Copyright 2023 OCamlPro SAS *)
8+
(* *)
9+
(* All rights reserved. This file is distributed under the terms of *)
10+
(* the GNU Lesser General Public License version 2.1, with the *)
11+
(* special exception on linking described in the file LICENSE. *)
12+
(* *)
13+
(**************************************************************************)
14+
115
type atom =
216
| Value
317
| Value_int
418
| Unboxed_float
519
| Unboxed_int of Lambda.boxed_integer
620

721
val fold_left_layout :
8-
('acc -> Clambda.ulambda -> atom -> 'acc) -> 'acc -> Clambda.ulambda ->
9-
Clambda_primitives.layout -> 'acc
22+
('acc -> Clambda.ulambda -> atom -> 'acc) ->
23+
'acc ->
24+
Clambda.ulambda ->
25+
Clambda_primitives.layout ->
26+
'acc
1027

1128
type decomposition =
12-
| Atom of { offset : int; layout : atom }
29+
| Atom of
30+
{ offset : int;
31+
layout : atom
32+
}
1333
| Product of decomposition array
1434

1535
val equal_decomposition : decomposition -> decomposition -> bool

middle_end/flambda2/from_lambda/closure_conversion.ml

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,8 @@ let close_one_function acc ~code_id ~external_env ~by_function_slot decl
16001600
let code =
16011601
Code.create code_id ~params_and_body
16021602
~free_names_of_params_and_body:(Acc.free_names acc) ~params_arity
1603-
~num_trailing_local_params:(Function_decl.num_trailing_local_params decl)
1603+
~num_trailing_complex_local_params:
1604+
(Function_decl.num_trailing_complex_local_params decl)
16041605
~result_arity:(Flambda_arity.unarize_t return)
16051606
~result_types:Unknown
16061607
~contains_no_escaping_local_allocs:
@@ -1701,7 +1702,7 @@ let close_functions acc external_env ~current_region function_declarations =
17011702
List.fold_left
17021703
(fun approx_map decl ->
17031704
(* The only fields of metadata which are used for this pass are
1704-
params_arity, is_tupled, num_trailing_local_params,
1705+
params_arity, is_tupled, num_trailing_complex_local_params,
17051706
contains_no_escaping_local_allocs, and result_arity. We try to
17061707
populate the different fields as much as possible, but put dummy
17071708
values when they are not yet computed or simply too expensive to
@@ -1729,8 +1730,8 @@ let close_functions acc external_env ~current_region function_declarations =
17291730
in
17301731
let metadata =
17311732
Code_metadata.create code_id ~params_arity
1732-
~num_trailing_local_params:
1733-
(Function_decl.num_trailing_local_params decl)
1733+
~num_trailing_complex_local_params:
1734+
(Function_decl.num_trailing_complex_local_params decl)
17341735
~result_arity:(Flambda_arity.unarize_t result_arity)
17351736
~result_types:Unknown
17361737
~contains_no_escaping_local_allocs:
@@ -2015,7 +2016,7 @@ let close_let_rec acc env ~function_declarations
20152016

20162017
let wrap_partial_application acc env apply_continuation (apply : IR.apply)
20172018
approx ~provided ~provided_arity ~missing_arity ~arity
2018-
~num_trailing_local_params ~contains_no_escaping_local_allocs =
2019+
~num_trailing_complex_local_params ~contains_no_escaping_local_allocs =
20192020
(* In case of partial application, creates a wrapping function from scratch to
20202021
allow inlining and lifting *)
20212022
let wrapper_id = Ident.create_local ("partial_" ^ Ident.name apply.func) in
@@ -2077,17 +2078,20 @@ let wrap_partial_application acc env apply_continuation (apply : IR.apply)
20772078
(Ident.Set.singleton apply.func)
20782079
all_args
20792080
in
2080-
let closure_alloc_mode, num_trailing_local_params =
2081-
let num_leading_heap_params =
2081+
let closure_alloc_mode, num_trailing_complex_local_params =
2082+
let num_leading_complex_heap_params =
20822083
(* This is a pre-unarization calculation so uses [num_params] not
20832084
[cardinal_unarized]. *)
2084-
Flambda_arity.num_params arity - num_trailing_local_params
2085+
Flambda_arity.num_params arity - num_trailing_complex_local_params
20852086
in
2086-
if num_provided <= num_leading_heap_params
2087-
then Lambda.alloc_heap, num_trailing_local_params
2087+
if num_provided <= num_leading_complex_heap_params
2088+
then Lambda.alloc_heap, num_trailing_complex_local_params
20882089
else
2089-
let num_supplied_local_args = num_provided - num_leading_heap_params in
2090-
Lambda.alloc_local, num_trailing_local_params - num_supplied_local_args
2090+
let num_supplied_local_args =
2091+
num_provided - num_leading_complex_heap_params
2092+
in
2093+
( Lambda.alloc_local,
2094+
num_trailing_complex_local_params - num_supplied_local_args )
20912095
in
20922096
if not (Lambda.sub_mode closure_alloc_mode apply.IR.mode)
20932097
then
@@ -2096,12 +2100,13 @@ let wrap_partial_application acc env apply_continuation (apply : IR.apply)
20962100
(Debuginfo.Scoped_location.string_of_scoped_location apply.IR.loc);
20972101
let function_declarations =
20982102
[ Function_decl.create ~let_rec_ident:(Some wrapper_id) ~function_slot
2099-
~kind:(Lambda.Curried { nlocal = num_trailing_local_params })
2103+
~kind:(Lambda.Curried { nlocal = num_trailing_complex_local_params })
21002104
~params ~params_arity ~removed_params:Ident.Set.empty
21012105
~return:apply.return_arity ~return_continuation ~exn_continuation
21022106
~my_region:apply.region ~body:fbody ~attr ~loc:apply.loc
2103-
~free_idents_of_body ~closure_alloc_mode ~num_trailing_local_params
2104-
~contains_no_escaping_local_allocs Recursive.Non_recursive ]
2107+
~free_idents_of_body ~closure_alloc_mode
2108+
~num_trailing_complex_local_params ~contains_no_escaping_local_allocs
2109+
Recursive.Non_recursive ]
21052110
in
21062111
let body acc env =
21072112
let arg = find_simple_from_id env wrapper_id in
@@ -2239,7 +2244,7 @@ let close_apply acc env (apply : IR.apply) : Expr_with_acc.t =
22392244
Some
22402245
( Code_metadata.params_arity metadata,
22412246
Code_metadata.is_tupled metadata,
2242-
Code_metadata.num_trailing_local_params metadata,
2247+
Code_metadata.num_trailing_complex_local_params metadata,
22432248
Code_metadata.contains_no_escaping_local_allocs metadata )
22442249
| Value_unknown -> None
22452250
| Value_symbol _ | Value_int _ | Block_approximation _ ->
@@ -2256,7 +2261,7 @@ let close_apply acc env (apply : IR.apply) : Expr_with_acc.t =
22562261
| Some
22572262
( arity,
22582263
is_tupled,
2259-
num_trailing_local_params,
2264+
num_trailing_complex_local_params,
22602265
contains_no_escaping_local_allocs ) -> (
22612266
let acc, _ = find_simples_and_arity acc env apply.args in
22622267
let split_args =
@@ -2327,7 +2332,7 @@ let close_apply acc env (apply : IR.apply) : Expr_with_acc.t =
23272332
inlined_attribute_on_partial_application_msg Inlined))
23282333
| Never_inlined | Hint_inlined | Default_inlined -> ());
23292334
wrap_partial_application acc env apply.continuation apply approx ~provided
2330-
~provided_arity ~missing_arity ~arity ~num_trailing_local_params
2335+
~provided_arity ~missing_arity ~arity ~num_trailing_complex_local_params
23312336
~contains_no_escaping_local_allocs
23322337
| Over_app { full; provided_arity; remaining; remaining_arity } ->
23332338
let full_args_call apply_continuation ~region acc =

middle_end/flambda2/from_lambda/closure_conversion_aux.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,15 +690,15 @@ module Function_decls = struct
690690
loc : Lambda.scoped_location;
691691
recursive : Recursive.t;
692692
closure_alloc_mode : Lambda.alloc_mode;
693-
num_trailing_local_params : int;
693+
num_trailing_complex_local_params : int;
694694
contains_no_escaping_local_allocs : bool
695695
}
696696

697697
let create ~let_rec_ident ~function_slot ~kind ~params ~params_arity
698698
~removed_params ~return ~return_continuation ~exn_continuation
699699
~my_region ~body ~(attr : Lambda.function_attribute) ~loc
700700
~free_idents_of_body recursive ~closure_alloc_mode
701-
~num_trailing_local_params ~contains_no_escaping_local_allocs =
701+
~num_trailing_complex_local_params ~contains_no_escaping_local_allocs =
702702
let let_rec_ident =
703703
match let_rec_ident with
704704
| None -> Ident.create_local "unnamed_function"
@@ -720,7 +720,7 @@ module Function_decls = struct
720720
loc;
721721
recursive;
722722
closure_alloc_mode;
723-
num_trailing_local_params;
723+
num_trailing_complex_local_params;
724724
contains_no_escaping_local_allocs
725725
}
726726

@@ -766,7 +766,8 @@ module Function_decls = struct
766766

767767
let closure_alloc_mode t = t.closure_alloc_mode
768768

769-
let num_trailing_local_params t = t.num_trailing_local_params
769+
let num_trailing_complex_local_params t =
770+
t.num_trailing_complex_local_params
770771

771772
let contains_no_escaping_local_allocs t =
772773
t.contains_no_escaping_local_allocs

0 commit comments

Comments
 (0)