File: unbox_closures.ml

package info (click to toggle)
ocaml 5.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,124 kB
  • sloc: ml: 355,439; ansic: 51,636; sh: 25,098; asm: 5,413; makefile: 3,673; python: 919; javascript: 273; awk: 253; perl: 59; fortran: 21; cs: 9
file content (87 lines) | stat: -rw-r--r-- 4,031 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(**************************************************************************)
(*                                                                        *)
(*                                 OCaml                                  *)
(*                                                                        *)
(*                       Pierre Chambart, OCamlPro                        *)
(*           Mark Shinwell and Leo White, Jane Street Europe              *)
(*                                                                        *)
(*   Copyright 2013--2016 OCamlPro SAS                                    *)
(*   Copyright 2014--2016 Jane Street Group LLC                           *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

[@@@ocaml.warning "+a-4-9-30-40-41-42-66"]
open! Int_replace_polymorphic_compare

module ASA = Augment_specialised_args
module W = ASA.What_to_specialise
module E = Inline_and_simplify_aux.Env

module Transform = struct
  let pass_name = "unbox-closures"

  let precondition ~env ~(set_of_closures : Flambda.set_of_closures) =
    !Clflags.unbox_closures
      && not (E.at_toplevel env)
      && not (Variable.Map.is_empty set_of_closures.free_vars)

  let what_to_specialise ~env ~(set_of_closures : Flambda.set_of_closures) =
    let what_to_specialise = W.create ~set_of_closures in
    if not (precondition ~env ~set_of_closures) then
      what_to_specialise
    else begin
      let round = E.round env in
      let num_closure_vars = Variable.Map.cardinal set_of_closures.free_vars in
      let module B = Inlining_cost.Benefit in
      let saved_by_not_building_closure =
        (* For the moment assume that we're going to cause all functions in the
           set to become closed. *)
        B.remove_prims (B.remove_call B.zero) num_closure_vars
      in
      Flambda_iterators.fold_function_decls_ignoring_stubs set_of_closures
        ~init:what_to_specialise
        ~f:(fun ~fun_var ~(function_decl : Flambda.function_declaration)
              what_to_specialise ->
          let body_size = Inlining_cost.lambda_size function_decl.body in
          (* If the function is small enough, make a direct call surrogate
             for it, so that indirect calls are not penalised by having to
             bounce through the stub.  (Making such a surrogate involves
             duplicating the function.) *)
          let small_enough_to_duplicate =
            let module W = Inlining_cost.Whether_sufficient_benefit in
            let wsb =
              W.create_estimate ~original_size:0
                ~toplevel:false
                ~branch_depth:0
                ~new_size:((body_size / !Clflags.unbox_closures_factor) + 1)
                ~benefit:saved_by_not_building_closure
                ~lifting:false
                ~round
            in
            W.evaluate wsb
          in
          let what_to_specialise =
            if small_enough_to_duplicate then
              W.make_direct_call_surrogate_for what_to_specialise ~fun_var
            else
              what_to_specialise
          in
          let bound_by_the_closure =
            Flambda_utils.variables_bound_by_the_closure
              (Closure_id.wrap fun_var)
              set_of_closures.function_decls
          in
          Variable.Set.fold (fun inner_free_var what_to_specialise ->
              W.new_specialised_arg what_to_specialise
                ~fun_var ~group:inner_free_var
                ~definition:(Existing_inner_free_var inner_free_var))
            bound_by_the_closure
            what_to_specialise)
    end
end

include ASA.Make (Transform)