Skip to content

Commit fa9bd85

Browse files
authored
zero_alloc: cleanup attributes in Lambda (#2723)
* Remove [Ignore_assert_all] and [opt] from Lambda onwards * format * Fix tests * fix tests (no local allocation mode) * Zero_alloc_attribute.t = Lambda.zero_alloc_attribute
1 parent 663ce58 commit fa9bd85

18 files changed

+407
-536
lines changed

middle_end/flambda2/from_lambda/closure_conversion.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,8 +1880,7 @@ let make_unboxed_function_wrapper acc function_slot ~unarized_params:params
18801880
(Poll_attribute.from_lambda (Function_decl.poll_attribute decl))
18811881
~zero_alloc_attribute:
18821882
(Zero_alloc_attribute.from_lambda
1883-
(Function_decl.zero_alloc_attribute decl)
1884-
(Debuginfo.Scoped_location.to_location (Function_decl.loc decl)))
1883+
(Function_decl.zero_alloc_attribute decl))
18851884
~is_a_functor:(Function_decl.is_a_functor decl)
18861885
~is_opaque:false ~recursive ~newer_version_of:None ~cost_metrics
18871886
~inlining_arguments:(Inlining_arguments.create ~round:0)
@@ -2249,8 +2248,7 @@ let close_one_function acc ~code_id ~external_env ~by_function_slot
22492248
(Poll_attribute.from_lambda (Function_decl.poll_attribute decl))
22502249
~zero_alloc_attribute:
22512250
(Zero_alloc_attribute.from_lambda
2252-
(Function_decl.zero_alloc_attribute decl)
2253-
(Debuginfo.Scoped_location.to_location (Function_decl.loc decl)))
2251+
(Function_decl.zero_alloc_attribute decl))
22542252
~is_a_functor:(Function_decl.is_a_functor decl)
22552253
~is_opaque:(Function_decl.is_opaque decl)
22562254
~recursive ~newer_version_of:None ~cost_metrics
@@ -2379,7 +2377,6 @@ let close_functions acc external_env ~current_region function_declarations =
23792377
let zero_alloc_attribute =
23802378
Zero_alloc_attribute.from_lambda
23812379
(Function_decl.zero_alloc_attribute decl)
2382-
(Debuginfo.Scoped_location.to_location (Function_decl.loc decl))
23832380
in
23842381
let cost_metrics = Cost_metrics.zero in
23852382
let dbg = Debuginfo.from_location (Function_decl.loc decl) in

middle_end/flambda2/parser/fexpr_to_flambda.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ let rec expr env (e : Fexpr.expr) : Flambda.Expr.t =
925925
~first_complex_local_param:(Flambda_arity.num_params params_arity)
926926
~result_arity ~result_types:Unknown ~result_mode
927927
~contains_no_escaping_local_allocs:false ~stub:false ~inline
928-
~zero_alloc_attribute:Default_check
928+
~zero_alloc_attribute:Default_zero_alloc
929929
(* CR gyorsh: should [check] be set properly? *)
930930
~is_a_functor:false ~is_opaque:false ~recursive
931931
~cost_metrics (* CR poechsel: grab inlining arguments from fexpr. *)

middle_end/flambda2/simplify/simplify_apply_expr.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ let simplify_direct_partial_application ~simplify_expr dacc apply
631631
(Code_metadata.contains_no_escaping_local_allocs
632632
callee's_code_metadata)
633633
~stub:true ~inline:Default_inline ~poll_attribute:Default
634-
~zero_alloc_attribute:Zero_alloc_attribute.Default_check
634+
~zero_alloc_attribute:Zero_alloc_attribute.Default_zero_alloc
635635
~is_a_functor:false ~is_opaque:false ~recursive
636636
~cost_metrics:cost_metrics_of_body
637637
~inlining_arguments:(DE.inlining_arguments (DA.denv dacc))

middle_end/flambda2/simplify/simplify_set_of_closures.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ let simplify_function context ~outer_dacc function_slot code_id
510510
let code_metadata = Code_or_metadata.code_metadata code_or_metadata in
511511
let never_delete =
512512
match Code_metadata.zero_alloc_attribute code_metadata with
513-
| Default_check -> !Clflags.zero_alloc_check_assert_all
513+
| Default_zero_alloc -> false
514514
| Assume _ -> false
515515
| Check _ -> true
516516
in

middle_end/flambda2/terms/zero_alloc_attribute.ml

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
(* *)
1111
(**************************************************************************)
1212

13-
type t =
14-
| Default_check
15-
| Assume of
13+
type t = Lambda.zero_alloc_attribute =
14+
| Default_zero_alloc
15+
| Check of
1616
{ strict : bool;
17-
never_returns_normally : bool;
18-
never_raises : bool;
1917
loc : Location.t
2018
}
21-
| Check of
19+
| Assume of
2220
{ strict : bool;
21+
never_returns_normally : bool;
22+
never_raises : bool;
2323
loc : Location.t
2424
}
2525

2626
let print ppf t =
2727
match t with
28-
| Default_check -> ()
28+
| Default_zero_alloc -> ()
2929
| Assume { strict; never_returns_normally; never_raises; loc = _ } ->
3030
Format.fprintf ppf "@[assume_zero_alloc%s%s%s@]"
3131
(if strict then "_strict" else "")
@@ -35,25 +35,11 @@ let print ppf t =
3535
Format.fprintf ppf "@[assert_zero_alloc%s@]"
3636
(if strict then "_strict" else "")
3737

38-
let from_lambda : Lambda.zero_alloc_attribute -> Location.t -> t =
39-
fun a loc ->
40-
match a with
41-
| Default_zero_alloc ->
42-
if !Clflags.zero_alloc_check_assert_all
43-
&& Builtin_attributes.is_zero_alloc_check_enabled ~opt:false
44-
then Check { strict = false; loc }
45-
else Default_check
46-
| Ignore_assert_all -> Default_check
47-
| Assume { strict; never_returns_normally; never_raises; loc; arity = _ } ->
48-
Assume { strict; never_returns_normally; never_raises; loc }
49-
| Check { strict; opt; loc; arity = _ } ->
50-
if Builtin_attributes.is_zero_alloc_check_enabled ~opt
51-
then Check { strict; loc }
52-
else Default_check
38+
let from_lambda : Lambda.zero_alloc_attribute -> t = Fun.id
5339

5440
let equal x y =
5541
match x, y with
56-
| Default_check, Default_check -> true
42+
| Default_zero_alloc, Default_zero_alloc -> true
5743
| Check { strict = s1; loc = loc1 }, Check { strict = s2; loc = loc2 } ->
5844
Bool.equal s1 s2 && Location.compare loc1 loc2 = 0
5945
| ( Assume
@@ -70,8 +56,8 @@ let equal x y =
7056
} ) ->
7157
Bool.equal s1 s2 && Bool.equal n1 n2 && Bool.equal r1 r2
7258
&& Location.compare loc1 loc2 = 0
73-
| (Default_check | Check _ | Assume _), _ -> false
59+
| (Default_zero_alloc | Check _ | Assume _), _ -> false
7460

7561
let is_default : t -> bool = function
76-
| Default_check -> true
62+
| Default_zero_alloc -> true
7763
| Check _ | Assume _ -> false

middle_end/flambda2/terms/zero_alloc_attribute.mli

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
(* *)
1111
(**************************************************************************)
1212
(** [@zero_alloc ...] annotations on function declaration (not call sites) *)
13-
type t =
14-
| Default_check
15-
| Assume of
13+
type t = Lambda.zero_alloc_attribute =
14+
| Default_zero_alloc
15+
| Check of
1616
{ strict : bool;
17-
never_returns_normally : bool;
18-
never_raises : bool;
1917
loc : Location.t
2018
}
21-
| Check of
19+
| Assume of
2220
{ strict : bool;
21+
never_returns_normally : bool;
22+
never_raises : bool;
2323
loc : Location.t
2424
}
2525

@@ -29,4 +29,4 @@ val equal : t -> t -> bool
2929

3030
val is_default : t -> bool
3131

32-
val from_lambda : Lambda.zero_alloc_attribute -> Location.t -> t
32+
val from_lambda : Lambda.zero_alloc_attribute -> t

middle_end/flambda2/to_cmm/to_cmm_set_of_closures.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ end)
351351

352352
let transl_check_attrib : Zero_alloc_attribute.t -> Cmm.codegen_option list =
353353
function
354-
| Default_check -> []
354+
| Default_zero_alloc -> []
355355
| Assume { strict; never_returns_normally; never_raises; loc } ->
356356
[Assume_zero_alloc { strict; never_returns_normally; never_raises; loc }]
357357
| Check { strict; loc } -> [Check_zero_alloc { strict; loc }]

ocaml/lambda/lambda.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -634,18 +634,14 @@ type poll_attribute =
634634
| Error_poll (* [@poll error] *)
635635
| Default_poll (* no [@poll] attribute *)
636636

637-
type zero_alloc_attribute = Builtin_attributes.zero_alloc_attribute =
637+
type zero_alloc_attribute =
638638
| Default_zero_alloc
639-
| Ignore_assert_all
640639
| Check of { strict: bool;
641-
opt: bool;
642-
arity: int;
643640
loc: Location.t;
644641
}
645642
| Assume of { strict: bool;
646643
never_returns_normally: bool;
647644
never_raises: bool;
648-
arity: int;
649645
loc: Location.t;
650646
}
651647

@@ -925,7 +921,7 @@ let default_function_attribute = {
925921
}
926922

927923
let default_stub_attribute =
928-
{ default_function_attribute with stub = true; zero_alloc = Ignore_assert_all }
924+
{ default_function_attribute with stub = true; zero_alloc = Default_zero_alloc }
929925

930926
let default_param_attribute = { unbox_param = false }
931927

ocaml/lambda/lambda.mli

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,23 +510,19 @@ type poll_attribute =
510510
| Error_poll (* [@poll error] *)
511511
| Default_poll (* no [@poll] attribute *)
512512

513-
type zero_alloc_attribute = Builtin_attributes.zero_alloc_attribute =
513+
type zero_alloc_attribute =
514514
| Default_zero_alloc
515-
| Ignore_assert_all
516515
| Check of { strict: bool;
517516
(* [strict=true] property holds on all paths.
518517
[strict=false] if the function returns normally,
519518
then the property holds (but property violations on
520519
exceptional returns or divering loops are ignored).
521520
This definition may not be applicable to new properties. *)
522-
opt: bool;
523-
arity: int;
524521
loc: Location.t;
525522
}
526523
| Assume of { strict: bool;
527524
never_returns_normally: bool;
528525
never_raises: bool;
529-
arity: int;
530526
loc: Location.t;
531527
}
532528

ocaml/lambda/printlambda.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -939,15 +939,12 @@ let name_of_primitive = function
939939
let zero_alloc_attribute ppf check =
940940
match check with
941941
| Default_zero_alloc -> ()
942-
| Ignore_assert_all ->
943-
fprintf ppf "ignore assert all zero_alloc@ "
944942
| Assume {strict; never_returns_normally; loc = _} ->
945943
fprintf ppf "assume_zero_alloc%s%s@ "
946944
(if strict then "_strict" else "")
947945
(if never_returns_normally then "_never_returns_normally" else "")
948-
| Check {strict; loc = _; opt} ->
949-
fprintf ppf "assert_zero_alloc%s%s@ "
950-
(if opt then "_opt" else "")
946+
| Check {strict; loc = _; } ->
947+
fprintf ppf "assert_zero_alloc%s@ "
951948
(if strict then "_strict" else "")
952949

953950
let function_attribute ppf t =

ocaml/lambda/translcore.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,21 @@ and transl_function ~in_new_scope ~scopes e params body
16081608
~mode ~return_sort ~return_mode
16091609
~scopes e.exp_loc repr ~region params body)
16101610
in
1611+
let zero_alloc : Lambda.zero_alloc_attribute =
1612+
match (zero_alloc : Builtin_attributes.zero_alloc_attribute) with
1613+
| Default_zero_alloc ->
1614+
if !Clflags.zero_alloc_check_assert_all &&
1615+
Builtin_attributes.is_zero_alloc_check_enabled ~opt:false
1616+
then Check { strict = false; loc = e.exp_loc }
1617+
else Default_zero_alloc
1618+
| Check { strict; opt; arity = _; loc } ->
1619+
if Builtin_attributes.is_zero_alloc_check_enabled ~opt
1620+
then Check { strict; loc }
1621+
else Default_zero_alloc
1622+
| Assume { strict; never_returns_normally; never_raises; loc; arity = _; } ->
1623+
Assume { strict; never_returns_normally; never_raises; loc }
1624+
| Ignore_assert_all -> Default_zero_alloc
1625+
in
16111626
let attr =
16121627
{ function_attribute_disallowing_arity_fusion with zero_alloc }
16131628
in

ocaml/lambda/translmod.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ and apply_coercion_result loc strict funct params args cc_res =
162162
~return:Lambda.layout_module
163163
~attr:{ default_function_attribute with
164164
is_a_functor = true;
165-
zero_alloc = Ignore_assert_all;
165+
zero_alloc = Default_zero_alloc;
166166
stub = true; }
167167
~loc
168168
~mode:alloc_heap
@@ -572,7 +572,7 @@ let rec compile_functor ~scopes mexp coercion root_path loc =
572572
loop = Never_loop;
573573
is_a_functor = true;
574574
is_opaque = false;
575-
zero_alloc = Ignore_assert_all;
575+
zero_alloc = Default_zero_alloc;
576576
stub = false;
577577
tmc_candidate = false;
578578
may_fuse_arity = true;

ocaml/testsuite/tests/functors/functors.compilers.reference

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
(setglobal Functors!
22
(let
33
(O =
4-
(function {nlocal = 0} X is_a_functor always_inline
5-
ignore assert all zero_alloc never_loop
4+
(function {nlocal = 0} X is_a_functor always_inline never_loop
65
(let
76
(cow =
87
(function {nlocal = 0} x[int] : int (apply (field_imm 0 X) x))
98
sheep = (function {nlocal = 0} x[int] : int (+ 1 (apply cow x))))
109
(makeblock 0 cow sheep)))
1110
F =
12-
(function {nlocal = 0} X Y is_a_functor always_inline
13-
ignore assert all zero_alloc never_loop
11+
(function {nlocal = 0} X Y is_a_functor always_inline never_loop
1412
(let
1513
(cow =
1614
(function {nlocal = 0} x[int] : int
1715
(apply (field_imm 0 Y) (apply (field_imm 0 X) x)))
1816
sheep = (function {nlocal = 0} x[int] : int (+ 1 (apply cow x))))
1917
(makeblock 0 cow sheep)))
2018
F1 =
21-
(function {nlocal = 0} X Y is_a_functor always_inline
22-
ignore assert all zero_alloc never_loop
19+
(function {nlocal = 0} X Y is_a_functor always_inline never_loop
2320
(let
2421
(cow =
2522
(function {nlocal = 0} x[int] : int
2623
(apply (field_imm 0 Y) (apply (field_imm 0 X) x)))
2724
sheep = (function {nlocal = 0} x[int] : int (+ 1 (apply cow x))))
2825
(makeblock 0 sheep)))
2926
F2 =
30-
(function {nlocal = 0} X Y is_a_functor always_inline
31-
ignore assert all zero_alloc never_loop
27+
(function {nlocal = 0} X Y is_a_functor always_inline never_loop
3228
(let
3329
(X =a (makeblock 0 (field_imm 1 X))
3430
Y =a (makeblock 0 (field_imm 1 Y))
@@ -40,8 +36,7 @@
4036
M =
4137
(let
4238
(F =
43-
(function {nlocal = 0} X Y is_a_functor always_inline
44-
ignore assert all zero_alloc never_loop
39+
(function {nlocal = 0} X Y is_a_functor always_inline never_loop
4540
(let
4641
(cow =
4742
(function {nlocal = 0} x[int] : int
@@ -51,7 +46,6 @@
5146
(makeblock 0 cow sheep))))
5247
(makeblock 0
5348
(function {nlocal = 0} funarg funarg is_a_functor stub
54-
ignore assert all zero_alloc
5549
(let
5650
(let =
5751
(apply F (makeblock 0 (field_imm 1 funarg))

0 commit comments

Comments
 (0)