jon.recoil.org

Module Flambda2_simplify.Specialization_cost

Continuation Specialization Cost

Continuation specialization is done in simplify on the way down. Consider a term of the form:

let_cont k x =
  let_cont k' y =
    ...
  in
  ..
  switch x with
  | 0 -> k' 0
  | 1 -> k' 1
in
..
switch .. with
| 0 -> k 0
| 1 -> k 1

The decision to specialize continuation k is made once Simplify has reached the bottom of the handler of k. This allows to know all of the relevant information before deciding whether to specialize k: all uses of k have been seen, and we have also seen the code for the handler of ĸ.

The type t represents an accumulator updated while doing the downwards pass on the handler of k to record enough information to estimate the cost of a specialization of k, and whether there was any reason to **not** specialize k regardless of the cost

type reason =
  1. | At_toplevel
  2. | Contains_static_consts
  3. | Contains_set_of_closures
type cost = {
  1. size_of_primitives : int;
}
type t = private
  1. | Can_specialize of cost
  2. | Cannot_specialize of {
    1. reason : reason;
    }

The current specialization status, stored in the dacc.

val print : Format.formatter -> t -> unit

Printing function.

Creation

val can_specialize : t

Create a Can_specialize value with zero intial cost.

val cannot_specialize : reason -> t

Create a value preventing any specialization of the current continuation.

Updating Costs

Add a primitive of the given size to the cost of specialization

val add_set_of_closures : Flambda2_terms.Set_of_closures.t -> t -> t

Add a set of closure containing ~num closures to the cost of specialization.

val add_lifted_set_of_closures : Flambda2_terms.Set_of_closures.t -> t -> t