Skip to content

Commit d9588a6

Browse files
committed
Compiler: inlining, increase laziness
1 parent e4fbebc commit d9588a6

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

compiler/lib/inline.ml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ type context =
192192
; live_vars : int array (** Occurence count of all variables *)
193193
; inline_count : int ref (** Inlining statistics *)
194194
; env : info Var.Map.t (** Functions that are candidate for inlining *)
195-
; in_loop : bool (** Whether the current block is in a loop *)
196-
; has_closures : bool ref (** Whether the current function contains closures *)
195+
; in_loop : bool Lazy.t (** Whether the current block is in a loop *)
196+
; has_closures : bool Lazy.t ref (** Whether the current function contains closures *)
197197
; current_function : Var.t option (** Name of the current function *)
198198
; enclosing_function : Var.t option
199199
(** Name of the function enclosing the current function *)
@@ -389,7 +389,10 @@ and relevant_arguments ~context info args =
389389
List.compare_length_with info'.params ~len:arity = 0
390390
&& should_inline
391391
~context:
392-
{ context with in_loop = context.in_loop || contains_loop ~context info }
392+
{ context with
393+
in_loop =
394+
lazy (Lazy.force context.in_loop || contains_loop ~context info)
395+
}
393396
info'
394397
[]
395398
then Var.Map.add param arg m
@@ -413,15 +416,15 @@ and should_inline ~context info args =
413416
closure_count ~context info = 0
414417
|| Option.is_none context.enclosing_function
415418
|| Option.equal Var.equal info.enclosing_function context.current_function
416-
|| (not !(context.has_closures))
419+
|| (not (Lazy.force !(context.has_closures)))
417420
&& Option.equal Var.equal info.enclosing_function context.enclosing_function
418421
| `Wasm, _ | `JavaScript, `Double_translation -> true
419422
| `JavaScript, `Jspi -> assert false)
420423
&& (functor_like ~context info
421424
|| (context.live_vars.(Var.idx info.f) = 1
422425
&&
423426
match Config.target () with
424-
| `Wasm when context.in_loop ->
427+
| `Wasm when Lazy.force context.in_loop ->
425428
(* Avoid inlining in a loop since, if the loop is not hot,
426429
the code might never get optimized *)
427430
body_size ~context info < 30 && not (contains_loop ~context info)
@@ -600,7 +603,7 @@ and inline_function ~context i x f args rem state =
600603
then (
601604
let branch, p = state in
602605
incr context.inline_count;
603-
if closure_count ~context info > 0 then context.has_closures := true;
606+
if closure_count ~context info > 0 then context.has_closures := lazy true;
604607
context.live_vars.(Var.idx f) <- context.live_vars.(Var.idx f) - 1;
605608
let p, params, cont =
606609
if context.live_vars.(Var.idx f) > 0
@@ -639,8 +642,8 @@ let inline ~profile ~inline_count p ~live_vars =
639642
(context : context)
640643
->
641644
let p = context.p in
642-
let has_closures = ref (closure_count_uncached ~context pc > 0) in
643-
let in_loop = blocks_in_loop p pc in
645+
let has_closures = ref (lazy (closure_count_uncached ~context pc > 0)) in
646+
let in_loop = lazy (blocks_in_loop p pc) in
644647
let context =
645648
{ context with has_closures; enclosing_function; current_function }
646649
in
@@ -660,7 +663,8 @@ let inline ~profile ~inline_count p ~live_vars =
660663
then p
661664
else
662665
inline_in_block
663-
~context:{ context with in_loop = Addr.Set.mem pc in_loop }
666+
~context:
667+
{ context with in_loop = lazy (Addr.Set.mem pc (Lazy.force in_loop)) }
664668
pc
665669
block
666670
p)
@@ -696,8 +700,8 @@ let inline ~profile ~inline_count p ~live_vars =
696700
; live_vars
697701
; inline_count
698702
; env = Var.Map.empty
699-
; in_loop = false
700-
; has_closures = ref false
703+
; in_loop = lazy false
704+
; has_closures = ref (lazy false)
701705
; current_function = None
702706
; enclosing_function = None
703707
})

0 commit comments

Comments
 (0)