Skip to content

Commit 39f1211

Browse files
lpw25stedolan
authored andcommitted
Only take the upper bounds of modes associated with allocations (#37)
1 parent aec6fde commit 39f1211

File tree

10 files changed

+54
-25
lines changed

10 files changed

+54
-25
lines changed

lambda/translcore.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ let extract_float = function
8686

8787
let transl_value_mode mode : Lambda.alloc_mode =
8888
let alloc_mode = Types.Value_mode.regional_to_global_alloc mode in
89-
match Types.Alloc_mode.constrain_upper alloc_mode with
89+
match Types.Alloc_mode.constrain_lower alloc_mode with
9090
| Global -> Alloc_heap
9191
| Local -> Alloc_local
9292

testsuite/tests/basic-modules/anonymous.ocamlc.reference

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
(let (f = (function param 0) s = (makemutable 0 ""))
1717
(seq
1818
(ignore
19-
(let (region *match* = (setfield_ptr 0 s "Hello World!"))
19+
(let (*match* = (setfield_ptr 0 s "Hello World!"))
2020
(makeblock 0)))
2121
(let
22-
(drop = (function param 0)
23-
region *match* = (apply drop (field 0 s)))
22+
(drop = (function param 0) *match* = (apply drop (field 0 s)))
2423
(makeblock 0 A B f s drop))))))))

testsuite/tests/basic-modules/anonymous.ocamlopt.reference

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
(setfield_ptr(root-init) 3 (global Anonymous!) s))
1919
(ignore
2020
(let
21-
(region *match* =
21+
(*match* =
2222
(setfield_ptr 0 (field 3 (global Anonymous!)) "Hello World!"))
2323
(makeblock 0)))
2424
(let (drop = (function param 0))
2525
(setfield_ptr(root-init) 4 (global Anonymous!) drop))
2626
(let
27-
(region *match* =
27+
(*match* =
2828
(apply (field 4 (global Anonymous!))
2929
(field 0 (field 3 (global Anonymous!)))))
3030
0)

testsuite/tests/basic/patmatch_for_multiple.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ match (3, 2, 1) with
5454
*match*/103 =a (field 1 *match*/99))
5555
(exit 5 *match*/99)))))
5656
with (6) 0)
57-
with (5 x/94) (seq (region (ignore x/94)) 1)))
57+
with (5 x/94) (seq (ignore x/94) 1)))
5858
- : bool = false
5959
|}];;

testsuite/tests/lib-buffer/test.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,12 @@ let uchar_map_of_spec spec =
155155
map
156156
;;
157157

158-
(* FIXME: Remove this once regions or tailcalls are fixed *)
159-
let escape : 'a -> unit = fun x -> ()
160-
161158
let test_spec_map msg utf_x_map buffer_add_utf_x_uchar =
162159
let b = Buffer.create 4 in
163160
let rec loop u =
164161
Buffer.clear b; buffer_add_utf_x_uchar b u;
165162
match Buffer.contents b = utf_x_map.(Uchar.to_int u) with
166-
| false as x ->
167-
escape x;
168-
failed (sprintf "%s of U+%04X" msg (Uchar.to_int u))
163+
| false -> failed (sprintf "%s of U+%04X" msg (Uchar.to_int u))
169164
| true ->
170165
if Uchar.equal u Uchar.max then passed msg else loop (Uchar.succ u)
171166
in

testsuite/tests/lib-dynlink-initializers/test10_main.native.reference

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
Error: Failure("Plugin error")
22
Raised at Stdlib.failwith in file "stdlib.ml", line 29, characters 17-33
33
Called from Test10_plugin.g in file "test10_plugin.ml", line 2, characters 15-38
4-
Called from Test10_plugin.f in file "test10_plugin.ml", line 6, characters 2-6
54
Called from Test10_plugin in file "test10_plugin.ml", line 10, characters 2-6
65
Called from Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", line 85, characters 12-29
76
Called from Dynlink.Native.run.(fun) in file "otherlibs/dynlink/native/dynlink.ml", line 85, characters 12-29

testsuite/tests/match-exception/tail_calls.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
The success continuation expression is in tail position.
66
*)
77

8-
(* FIXME: Remove this once regions or tailcalls are fixed *)
9-
let escape : 'a -> unit = fun x -> ()
10-
118
let count_to_tr_match n =
129
let rec loop i =
1310
match
1411
i < n
1512
with exception Not_found -> ()
16-
| false as x -> escape x
13+
| false -> ()
1714
| true -> loop (i + 1)
1815
in loop 0
1916
;;

typing/typecore.ml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,22 @@ let eqmode ~loc ~env m1 m2 err =
277277
| Ok () -> ()
278278
| Error () -> raise (Error(loc, env, err))
279279

280+
let allocations : Alloc_mode.t list ref = ref []
281+
282+
let reset_allocations () = allocations := []
283+
284+
let register_allocation expected_mode =
285+
let alloc_mode = Value_mode.regional_to_global_alloc expected_mode.mode in
286+
match Alloc_mode.check_const alloc_mode with
287+
| Some _ -> ()
288+
| None -> allocations := alloc_mode :: !allocations
289+
290+
let optimise_allocations () =
291+
List.iter
292+
(fun mode -> ignore (Alloc_mode.constrain_upper mode))
293+
!allocations;
294+
reset_allocations ()
295+
280296
(* Typing of constants *)
281297

282298
let type_constant = function
@@ -2882,6 +2898,7 @@ and type_expect_
28822898
exp_attributes = sexp.pexp_attributes;
28832899
exp_env = env }
28842900
| Pexp_constant(Pconst_string (str, _, _) as cst) ->
2901+
register_allocation expected_mode;
28852902
let cst = constant_or_raise env loc cst in
28862903
(* Terrible hack for format strings *)
28872904
let ty_exp = expand_head env ty_expected in
@@ -3107,6 +3124,7 @@ and type_expect_
31073124
exp_env = env }
31083125
| Pexp_tuple sexpl ->
31093126
assert (List.length sexpl >= 2);
3127+
register_allocation expected_mode;
31103128
let subtypes = List.map (fun _ -> newgenvar ()) sexpl in
31113129
let to_unify = newgenty (Ttuple subtypes) in
31123130
with_explanation (fun () ->
@@ -3131,6 +3149,8 @@ and type_expect_
31313149
sarg ty_expected_explained sexp.pexp_attributes
31323150
| Pexp_variant(l, sarg) ->
31333151
(* Keep sharing *)
3152+
if sarg <> None then
3153+
register_allocation expected_mode;
31343154
let ty_expected0 = instance ty_expected in
31353155
let argument_mode = mode_subcomponent expected_mode in
31363156
begin try match
@@ -3168,6 +3188,7 @@ and type_expect_
31683188
end
31693189
| Pexp_record(lid_sexp_list, opt_sexp) ->
31703190
assert (lid_sexp_list <> []);
3191+
register_allocation expected_mode;
31713192
let opt_exp =
31723193
match opt_sexp with
31733194
None -> None
@@ -3347,6 +3368,7 @@ and type_expect_
33473368
exp_attributes = sexp.pexp_attributes;
33483369
exp_env = env }
33493370
| Pexp_array(sargl) ->
3371+
register_allocation expected_mode;
33503372
let ty = newgenvar() in
33513373
let to_unify = Predef.type_array ty in
33523374
with_explanation (fun () ->
@@ -3842,6 +3864,7 @@ and type_expect_
38423864
exp_env = env;
38433865
}
38443866
| Pexp_lazy e ->
3867+
register_allocation expected_mode;
38453868
let closure_mode = Value_mode.regional_to_global expected_mode.mode in
38463869
let ty = newgenvar () in
38473870
let to_unify = Predef.type_lazy_t ty in
@@ -4157,6 +4180,7 @@ and type_binding_op_ident env s =
41574180
and type_function ?in_function loc attrs env expected_mode
41584181
ty_expected_explained l has_local caselist =
41594182
let { ty = ty_expected; explanation } = ty_expected_explained in
4183+
register_allocation expected_mode;
41604184
let alloc_mode = Value_mode.regional_to_global_alloc expected_mode.mode in
41614185
let (loc_fun, ty_fun) =
41624186
match in_function with
@@ -4964,6 +4988,8 @@ and type_application env app_loc expected_mode funct funct_mode sargs =
49644988

49654989
and type_construct env expected_mode loc lid sarg ty_expected_explained attrs =
49664990
let { ty = ty_expected; explanation } = ty_expected_explained in
4991+
if sarg <> None then
4992+
register_allocation expected_mode;
49674993
let argument_mode = mode_subcomponent expected_mode in
49684994
let expected_type =
49694995
try

typing/typecore.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ val generalizable: int -> type_expr -> bool
127127
val reset_delayed_checks: unit -> unit
128128
val force_delayed_checks: unit -> unit
129129

130+
val reset_allocations: unit -> unit
131+
val optimise_allocations: unit -> unit
132+
133+
130134
val name_pattern : string -> Typedtree.pattern list -> Ident.t
131135
val name_cases : string -> Typedtree.value Typedtree.case list -> Ident.t
132136

typing/typemod.ml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,12 +2458,9 @@ and type_structure ?(toplevel = false) funct_body anchor env sstr =
24582458
if toplevel then run ()
24592459
else Builtin_attributes.warning_scope [] run
24602460

2461-
let type_toplevel_phrase env s =
2462-
Env.reset_required_globals ();
2463-
let (str, sg, to_remove_from_sg, env) =
2464-
type_structure ~toplevel:true false None env s in
2465-
remove_mode_variables env sg;
2466-
begin match str.str_items with
2461+
(* The toplevel will print some types not present in the signature *)
2462+
let remove_mode_variables_for_toplevel str =
2463+
match str.str_items with
24672464
| [{ str_desc =
24682465
( Tstr_eval (exp, _)
24692466
| Tstr_value (Nonrecursive,
@@ -2473,7 +2470,15 @@ let type_toplevel_phrase env s =
24732470
even though they do not appear in sg *)
24742471
Ctype.remove_mode_variables exp.exp_type
24752472
| _ -> ()
2476-
end;
2473+
2474+
let type_toplevel_phrase env s =
2475+
Env.reset_required_globals ();
2476+
Typecore.reset_allocations ();
2477+
let (str, sg, to_remove_from_sg, env) =
2478+
type_structure ~toplevel:true false None env s in
2479+
remove_mode_variables env sg;
2480+
remove_mode_variables_for_toplevel str;
2481+
Typecore.optimise_allocations ();
24772482
(str, sg, to_remove_from_sg, env)
24782483

24792484
let type_module_alias = type_module ~alias:true true false None
@@ -2649,6 +2654,7 @@ let type_implementation sourcefile outputprefix modulename initial_env ast =
26492654
Cmt_format.clear ();
26502655
Misc.try_finally (fun () ->
26512656
Typecore.reset_delayed_checks ();
2657+
Typecore.reset_allocations ();
26522658
Env.reset_required_globals ();
26532659
if !Clflags.print_types then (* #7656 *)
26542660
Warnings.parse_options false "-32-34-37-38-60";
@@ -2657,6 +2663,7 @@ let type_implementation sourcefile outputprefix modulename initial_env ast =
26572663
let simple_sg = Signature_names.simplify finalenv names sg in
26582664
if !Clflags.print_types then begin
26592665
Typecore.force_delayed_checks ();
2666+
Typecore.optimise_allocations ();
26602667
Printtyp.wrap_printing_env ~error:false initial_env
26612668
(fun () -> fprintf std_formatter "%a@."
26622669
(Printtyp.printed_signature sourcefile) simple_sg
@@ -2679,6 +2686,7 @@ let type_implementation sourcefile outputprefix modulename initial_env ast =
26792686
sourcefile sg intf_file dclsig
26802687
in
26812688
Typecore.force_delayed_checks ();
2689+
Typecore.optimise_allocations ();
26822690
(* It is important to run these checks after the inclusion test above,
26832691
so that value declarations which are not used internally but
26842692
exported are not reported as being unused. *)
@@ -2695,6 +2703,7 @@ let type_implementation sourcefile outputprefix modulename initial_env ast =
26952703
check_nongen_schemes finalenv simple_sg;
26962704
normalize_signature simple_sg;
26972705
Typecore.force_delayed_checks ();
2706+
Typecore.optimise_allocations ();
26982707
(* See comment above. Here the target signature contains all
26992708
the value being exported. We can still capture unused
27002709
declarations like "let x = true;; let x = 1;;", because in this

0 commit comments

Comments
 (0)