@@ -36,10 +36,11 @@ let absorb_info (x : used_info) (y : used_info) =
3636 x.times < - x0 + y0;
3737 if captured then x.captured < - true
3838
39- (* The global table [occ] associates each let-bound identifier with its number
40- of uses. A use under a lambda or within a loop is treated as multiple uses,
41- since neither case is suitable for inlining. The local table [bv] tracks
42- locally let-bound variables and is reset when crossing lambdas and loops. *)
39+ (* The global table [occ] records each let-bound identifier's number of uses and
40+ whether it is captured by a function or loop. The local table [bv] tracks
41+ locally let-bound variables and is reset when crossing functions and loops,
42+ so uses of outer bindings are marked as captured. The optimizer uses the
43+ captured flag to restrict inlining without inflating the occurrence count. *)
4344let collect_occurs lam : occ_tbl =
4445 let occ : occ_tbl = Hash_ident. create 83 in
4546
@@ -63,8 +64,8 @@ let collect_occurs lam : occ_tbl =
6364 | Some r -> r.times < - r.times + 1
6465 | None -> (
6566 (* ident is not locally bound, therefore this is a use under a lambda
66- or within a loop. Increase use count by 2 -- enough so
67- that single-use optimizations will not apply . *)
67+ or within a loop. Mark it as captured so that single-use
68+ optimizations only apply when moving the value is safe . *)
6869 match Hash_ident. find_opt occ ident with
6970 | Some r -> absorb_info r {times = 1 ; captured = true }
7071 | None ->
@@ -82,8 +83,8 @@ let collect_occurs lam : occ_tbl =
8283 | Some r -> absorb_info r n
8384 | None -> (
8485 (* ident is not locally bound, therefore this is a use under a lambda
85- or within a loop. Increase use count by 2 -- enough so
86- that single-use optimizations will not apply . *)
86+ or within a loop. Mark it as captured so that single-use
87+ optimizations only apply when moving the value is safe . *)
8788 match Hash_ident. find_opt occ ident with
8889 | Some r -> absorb_info r {n with captured = true }
8990 | None ->
0 commit comments