Skip to content

Commit ea484d0

Browse files
authored
flambda-backend: Zero alloc annotation: assume a function never returns normally (#1831)
1 parent 387893c commit ea484d0

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

lambda/lambda.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,13 @@ type check_attribute =
496496
| Ignore_assert_all of property
497497
| Check of { property: property;
498498
strict: bool;
499-
assume: bool;
500499
loc: Location.t;
501500
}
501+
| Assume of { property: property;
502+
strict: bool;
503+
loc: Location.t;
504+
never_returns_normally: bool;
505+
}
502506

503507
type loop_attribute =
504508
| Always_loop (* [@loop] or [@loop always] *)

lambda/lambda.mli

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,13 @@ type check_attribute =
391391
then the property holds (but property violations on
392392
exceptional returns or divering loops are ignored).
393393
This definition may not be applicable to new properties. *)
394-
assume: bool;
395-
(* [assume=true] assume without checking that the
396-
property holds *)
397394
loc: Location.t;
398395
}
396+
| Assume of { property: property;
397+
strict: bool;
398+
loc: Location.t;
399+
never_returns_normally: bool;
400+
}
399401

400402
type loop_attribute =
401403
| Always_loop (* [@loop] or [@loop always] *)

lambda/printlambda.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,15 @@ let check_attribute ppf check =
634634
| Default_check -> ()
635635
| Ignore_assert_all p ->
636636
fprintf ppf "ignore assert all %s@ " (check_property p)
637-
| Check {property=p; assume; strict; loc = _} ->
638-
fprintf ppf "%s %s%s@ "
639-
(if assume then "assume" else "assert")
637+
| Assume {property=p; strict; never_returns_normally; loc = _} ->
638+
fprintf ppf "assume_%s%s%s@ "
640639
(check_property p)
641-
(if strict then " strict" else "")
640+
(if strict then "_strict" else "")
641+
(if never_returns_normally then "_never_returns_normally" else "")
642+
| Check {property=p; strict; loc = _} ->
643+
fprintf ppf "assert_%s%s@ "
644+
(check_property p)
645+
(if strict then "_strict" else "")
642646

643647
let function_attribute ppf t =
644648
if t.is_a_functor then

lambda/translattribute.ml

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,17 @@ let parse_property_attribute attr property =
251251
| Some {Parsetree.attr_name = {txt; loc}; attr_payload = payload}->
252252
parse_ids_payload txt loc
253253
~default:Default_check
254-
~empty:(Check { property; strict = false; assume = false; loc; } )
254+
~empty:(Check { property; strict = false; loc; } )
255255
[
256-
["assume"], Check { property; strict = false; assume = true; loc; };
257-
["strict"], Check { property; strict = true; assume = false; loc; };
258-
["assume"; "strict"], Check { property; strict = true; assume = true; loc; };
256+
["assume"],
257+
Assume { property; strict = false; never_returns_normally = false; loc; };
258+
["strict"], Check { property; strict = true; loc; };
259+
["assume"; "strict"],
260+
Assume { property; strict = true; never_returns_normally = false; loc; };
261+
["assume"; "never_returns_normally"],
262+
Assume { property; strict = false; never_returns_normally = true; loc; };
263+
["assume"; "strict"; "never_returns_normally"],
264+
Assume { property; strict = true; never_returns_normally = true; loc; };
259265
["ignore"], Ignore_assert_all property
260266
]
261267
payload
@@ -303,15 +309,15 @@ let get_property_attribute l p =
303309
(match attr, res with
304310
| None, Default_check -> ()
305311
| _, Default_check -> ()
306-
| None, (Check _ | Ignore_assert_all _ ) -> assert false
312+
| None, (Check _ | Assume _ | Ignore_assert_all _) -> assert false
307313
| Some _, Ignore_assert_all _ -> ()
308-
| Some attr, Check { assume; _ } ->
314+
| Some _, Assume _ -> ()
315+
| Some attr, Check _ ->
309316
if !Clflags.zero_alloc_check && !Clflags.native_code then
310317
(* The warning for unchecked functions will not trigger if the check is requested
311318
through the [@@@zero_alloc all] top-level annotation rather than through the
312319
function annotation [@zero_alloc]. *)
313-
if not assume then
314-
Builtin_attributes.register_property attr.attr_name);
320+
Builtin_attributes.register_property attr.attr_name);
315321
res
316322

317323
let get_poll_attribute l =
@@ -414,7 +420,8 @@ let assume_zero_alloc attributes =
414420
match parse_property_attribute attr p with
415421
| Default_check -> false
416422
| Ignore_assert_all _ -> false
417-
| Check { property = Zero_alloc; assume } -> assume
423+
| Assume { property = Zero_alloc; _ } -> true
424+
| Check { property = Zero_alloc; _ } -> false
418425

419426
let assume_zero_alloc attributes =
420427
(* This function is used for "look-ahead" to find attributes
@@ -428,9 +435,12 @@ let add_check_attribute expr loc attributes =
428435
| Zero_alloc -> "zero_alloc"
429436
in
430437
let to_string = function
431-
| Check { property; strict; assume; loc = _} ->
432-
Printf.sprintf "%s %s%s"
433-
(if assume then "assume" else "assert")
438+
| Check { property; strict; loc = _} ->
439+
Printf.sprintf "assert %s%s"
440+
(to_string property)
441+
(if strict then " strict" else "")
442+
| Assume { property; strict; loc = _} ->
443+
Printf.sprintf "assume %s%s"
434444
(to_string property)
435445
(if strict then " strict" else "")
436446
| Ignore_assert_all property ->
@@ -441,11 +451,13 @@ let add_check_attribute expr loc attributes =
441451
| Lfunction({ attr = { stub = false } as attr; } as funct) ->
442452
begin match get_property_attribute attributes Zero_alloc with
443453
| Default_check -> expr
444-
| (Ignore_assert_all p | Check { property = p; _ }) as check ->
454+
| (Ignore_assert_all p | Check { property = p; _ } | Assume { property = p; _ })
455+
as check ->
445456
begin match attr.check with
446457
| Default_check -> ()
447458
| Ignore_assert_all p'
448-
| Check { property = p'; strict = _; assume = _; loc = _; } ->
459+
| Assume { property = p'; strict = _; loc = _; }
460+
| Check { property = p'; strict = _; loc = _; } ->
449461
if p = p' then
450462
Location.prerr_warning loc
451463
(Warnings.Duplicated_attribute (to_string check));

0 commit comments

Comments
 (0)