Module Analysis_and_optimization.Optimize

Inline all functions except for ones with forward declarations (e.g. recursive functions, mutually recursive functions, and functions without a definition

val static_loop_unrolling : Middle.Program.Typed.t -> Middle.Program.Typed.t

Unroll all for-loops with constant bounds, as long as they do not contain break or continue statements in their body at the top level

val one_step_loop_unrolling : Middle.Program.Typed.t -> Middle.Program.Typed.t

Unroll all loops for one iteration, as long as they do not contain break or continue statements in their body at the top level

Remove redundant SList constructors from the Mir that might have been introduced by other optimizations

Make sure that SList constructors directly under if, for, while or fundef constructors are replaced with Block constructors. This should probably be run before we generate code.

val constant_propagation : ?preserve_stability:bool -> Middle.Program.Typed.t -> Middle.Program.Typed.t

Propagate constant values through variable assignments

val expression_propagation : ?preserve_stability:bool -> Middle.Program.Typed.t -> Middle.Program.Typed.t

Propagate arbitrary expressions through variable assignments. This can be useful for opening up new possibilities for partial evaluation. It should be followed by some CSE or lazy code motion pass, however.

Propagate copies of variables through assignments.

val dead_code_elimination : Middle.Program.Typed.t -> Middle.Program.Typed.t

Eliminate semantically redundant code branches. This includes removing redundant assignments (because they will be overwritten) and removing redundant code in program branches that will never be reached.

Partially evaluate expressions in the program. This includes simplification using algebraic identities of logical and arithmetic operators as well as Stan math functions.

val lazy_code_motion : ?preserve_stability:bool -> Middle.Program.Typed.t -> Middle.Program.Typed.t

Perform partial redundancy elmination using the lazy code motion algorithm. This subsumes common subexpression elimination and loop-invariant code motion.

Assign the optimal ad-levels to local variables. That means, make sure that variables only ever get treated as autodiff variables if they have some dependency on a parameter

val allow_uninitialized_decls : Middle.Program.Typed.t -> Middle.Program.Typed.t

Marks Decl types such that, if the first assignment after the decl assigns to the full object, allow the object to be constructed but not uninitialized.

type optimization_settings = {
  1. function_inlining : bool;
  2. static_loop_unrolling : bool;
  3. one_step_loop_unrolling : bool;
  4. list_collapsing : bool;
  5. block_fixing : bool;
  6. allow_uninitialized_decls : bool;
  7. constant_propagation : bool;
  8. expression_propagation : bool;
  9. copy_propagation : bool;
  10. dead_code_elimination : bool;
  11. partial_evaluation : bool;
  12. lazy_code_motion : bool;
  13. optimize_ad_levels : bool;
  14. preserve_stability : bool;
  15. optimize_soa : bool;
}

Interface for turning individual optimizations on/off. Useful for testing and for top-level interface flags.

val all_optimizations : optimization_settings
val no_optimizations : optimization_settings
type optimization_level =
  1. | O0
  2. | O1
  3. | Oexperimental
val level_optimizations : optimization_level -> optimization_settings

Perform all optimizations in this module on the MIR in an appropriate order.