Analysis_and_optimization.Memory_patterns
val matrix_set :
Middle.Expr.Typed.Meta.t Middle.Expr.Fixed.t ->
(Dataflow_types.vexpr * Middle.Expr.Typed.Meta.t) Core.Set.Poly.t
Return a Var expression of the name for each type containing an eigen matrix
val query_var_eigen_names : Middle.Expr.Typed.t -> string Core.Set.Poly.t
Return a set of all types containing autodiffable Eigen matrices in an expression.
Check whether one set is a nonzero subset of another set.
val count_single_idx : int -> Middle.Expr.Typed.t Middle.Index.t -> int
Check an Index to count how many times we see a single index.
val is_uni_eigen_loop_indexing :
bool ->
Middle.UnsizedType.t ->
Middle.Expr.Typed.t Middle.Index.t list ->
bool
Find indices on Matrix and Vector types that perform single cell access. Returns true if it finds a vector, row vector, matrix, or matrix with single cell access as well as an array of any of the above that is accessing the inner matrix types cell.
val query_stan_math_mem_pattern_support :
string ->
(Middle.UnsizedType.autodifftype * Middle.UnsizedType.t) list ->
bool
val is_fun_soa_supported : string -> Middle.Expr.Typed.t list -> bool
val query_initial_demotable_expr :
bool ->
acc:string Core.Set.Poly.t ->
Middle.Expr.Typed.t ->
string Core.Set.Poly.t
Query to find the initial set of objects that cannot be SoA. This is mostly recursing over expressions, with the exceptions being functions and indexing expressions. For the logic on functions see the docs for query_initial_demotable_funs
.
val query_initial_demotable_funs :
bool ->
string Core.Set.Poly.t ->
Middle.Expr.Typed.Meta.t Middle.Expr.Fixed.t Middle.Fun_kind.t ->
Middle.Expr.Typed.t list ->
string Core.Set.Poly.t
Query a function to detect if it or any of its used expression's objects or expressions should be demoted to AoS. * The logic here demotes the expressions in a function to AoS if the function's inner expression returns has a meta type containing a matrix and either of : (1) The function is user defined and the UDFs inputs are matrices. (2) The Stan math function cannot support AoS
val extract_nonderived_admatrix_types :
Middle.Expr.Typed.t ->
(Middle.UnsizedType.autodifftype * Middle.UnsizedType.t) list
* Recurse through subexpressions and return a list of Unsized types. * Recursion continues until * 1. A non-autodiffable type is found * 2. An autodiffable scalar is found * 3. A `Var` type is found that is an autodiffable matrix
val extract_nonderived_admatrix_types_fun :
Middle.Expr.Typed.Meta.t Middle.Expr.Fixed.t Middle.Fun_kind.t ->
Middle.Expr.Typed.t list ->
(Middle.UnsizedType.autodifftype * Middle.UnsizedType.t) list
* Recurse through functions to find nonderived ad matrix types. * Special cases for StanLib functions are for * - `check_matching_dims`: compiler function that has no effect on optimization * - `rep_*vector` These are templated in the C++ to cast up to `Var<Matrix>` types * - `rep_matrix`. When it's only a scalar being propogated an math library overload can upcast to `Var<Matrix>`
val contains_at_least_one_ad_matrix_or_all_data :
(Middle.UnsizedType.autodifftype * Middle.UnsizedType.t) list ->
bool
Checks if a list of types contains at least on ad matrix or if everything is derived from data
val query_initial_demotable_stmt :
bool ->
string Core.Set.Poly.t ->
(Middle.Expr.Typed.Meta.t, Middle.Stmt.Located.Meta.t) Middle.Stmt.Fixed.t ->
string Core.Set.Poly.t
Query to find the initial set of objects in statements that cannot be SoA. This is mostly recursive over expressions and statements, with the exception of functions and Assignments. * For assignments: We demote the LHS variable if any of the following are true: 1. A single cell of the LHS is being assigned within a loop. 2. The top level expression on the RHS is a combination of only data matrices and scalar types. Operations on data matrix and scalar values in Stan math will return a AoS matrix. We currently have no way to tell Stan math to return a SoA matrix. 3. None of the RHS's functions are able to accept SoA matrices and the rhs is not an internal compiler function. * We demote RHS variables if any of the following are true: 1. The LHS variable has previously or through this iteration been marked AoS. 2. The LHS is a tuple projection * For functions see the documentation for query_initial_demotable_funs
for the logic on demotion rules.
val query_demotable_stmt :
string Core.Set.Poly.t ->
(Middle.Expr.Typed.t, int) Middle.Stmt.Fixed.Pattern.t ->
string Core.Set.Poly.t
Look through a statement to see whether the objects used in it need to be modified from SoA to AoS. Returns the set of object names that need demoted in a statement, if any. This function looks at Assignment statements, and returns back the set of top level object names given: 1. If the name of the lhs assignee is in the aos_exits
, all the names of the expressions with a type containing a matrix are returned. 2. If the names of the rhs objects containing matrix types are in the subset of aos_exits.
val modify_kind :
?force_demotion:bool ->
string Core.Set.Poly.t ->
Middle.Expr.Typed.t Middle.Fun_kind.t ->
Middle.Expr.Typed.t list ->
Middle.Expr.Typed.t Middle.Fun_kind.t * Middle.Expr.Typed.t list
Modify a function and it's subexpressions from SoA <-> AoS and vice versa. This performs demotion for sub expressions recursively. The top level expression and it's sub expressions are demoted to SoA if 1. The names of the variables in the subexpressions returning objects holding matrices are all in the modifiable set. 2. The function does not support SoA 3. The force
argument is true
val modify_expr_pattern :
?force_demotion:bool ->
string Core.Set.Poly.t ->
Middle.Expr.Typed.t Middle.Expr.Fixed.Pattern.t ->
Middle.Expr.Typed.t Middle.Expr.Fixed.Pattern.t
Modify an expression and it's subexpressions from SoA <-> AoS and vice versa. The only real paths in the below is on the functions and ternary expressions. * The logic for functions is defined in modify_kind
. TernaryIf
is forcefully demoted to AoS if the type of the expression contains a matrix.
val modify_expr :
?force_demotion:bool ->
string Core.Set.Poly.t ->
Middle.Expr.Typed.t ->
Middle.Expr.Typed.t
Given a Set of strings containing the names of objects that can be modified from AoS <-> SoA and vice versa, modify them within the expression.
val modify_stmt_pattern :
(Middle.Expr.Typed.t, Middle.Stmt.Located.t) Middle.Stmt.Fixed.Pattern.t ->
string Core.Set.Poly.t ->
(Middle.Expr.Typed.t, Middle.Stmt.Located.t) Middle.Stmt.Fixed.Pattern.t
Modify statement patterns in the MIR from AoS <-> SoA and vice versa For Decl
and Assignment
's reading in parameters, we demote to AoS if the decl_id
(or assign name) is in the modifiable set and otherwise promote the statement to SoA
. For general Assignment
statements, we check if the assignee is in the demotable set. If so, we force demotion of all of the rhs expressions. All other statements recurse over their statements and expressions. *
val modify_stmt :
Middle.Stmt.Located.t ->
string Core.Set.Poly.t ->
Middle.Stmt.Located.t
Modify statement patterns in the MIR from AoS <-> SoA and vice versa
val collect_mem_pattern_variables :
Middle.Stmt.Located.t Core.List.t ->
(string
* Middle.Expr.Typed.Meta.t Middle.Stmt.Fixed.First.t Middle.SizedType.t)
list
val pp_mem_patterns : Stdlib.Format.formatter -> Middle.Program.Typed.t -> unit