Skip to content

Commit 63a3c0b

Browse files
authored
flambda-backend: Add support for using zero_alloc information from a signature (#2506)
1 parent e663621 commit 63a3c0b

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

typing/typecore.ml

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5056,6 +5056,44 @@ let add_check_attribute expr attributes =
50565056
end
50575057
| _ -> expr
50585058

5059+
let zero_alloc_of_application ~num_args attrs funct =
5060+
let zero_alloc =
5061+
Builtin_attributes.get_property_attribute ~in_signature:false
5062+
~default_arity:num_args attrs Zero_alloc
5063+
in
5064+
let zero_alloc =
5065+
match zero_alloc with
5066+
| Assume _ | Ignore_assert_all _ | Check _ ->
5067+
(* The user wrote a zero_alloc attribute on the application - keep it.
5068+
(Note that `ignore` and `check` aren't really allowed here, and will be
5069+
rejected by the call to `Builtin_attributes.assume_zero_alloc` below.)
5070+
*)
5071+
zero_alloc
5072+
| Default_check ->
5073+
(* We assume the call is zero_alloc if the function is known to be
5074+
zero_alloc. If the function is zero_alloc opt, then we need to be sure
5075+
that the opt checks were run to license this assumption. We judge
5076+
whether the opt checks were run based on the argument to the
5077+
[-zero-alloc-check] command line flag. *)
5078+
let use_opt =
5079+
match !Clflags.zero_alloc_check with
5080+
| Check_default | No_check -> false
5081+
| Check_all | Check_opt_only -> true
5082+
in
5083+
match funct.exp_desc with
5084+
| Texp_ident (_, _, { val_zero_alloc = (Check c); _ }, _, _)
5085+
when c.arity = num_args && (use_opt || not c.opt) ->
5086+
Builtin_attributes.Assume {
5087+
property = Zero_alloc;
5088+
strict = c.strict;
5089+
never_returns_normally = false;
5090+
arity = c.arity;
5091+
loc = c.loc
5092+
}
5093+
| _ -> Builtin_attributes.Default_check
5094+
in
5095+
Builtin_attributes.assume_zero_alloc ~is_check_allowed:false zero_alloc
5096+
50595097
let rec type_exp ?recarg env expected_mode sexp =
50605098
(* We now delegate everything to type_expect *)
50615099
type_expect ?recarg env expected_mode sexp
@@ -5384,12 +5422,8 @@ and type_expect_
53845422
type_application env loc expected_mode pm funct funct_mode sargs rt
53855423
in
53865424
let assume_zero_alloc =
5387-
let default_arity = List.length args in
5388-
let zero_alloc =
5389-
Builtin_attributes.get_property_attribute ~in_signature:false
5390-
~default_arity sfunct.pexp_attributes Zero_alloc
5391-
in
5392-
Builtin_attributes.assume_zero_alloc ~is_check_allowed:false zero_alloc
5425+
zero_alloc_of_application ~num_args:(List.length args)
5426+
sfunct.pexp_attributes funct
53935427
in
53945428

53955429
rue {

0 commit comments

Comments
 (0)