This example (without the data at declaration which is ilegal)
functions {
real foo(data matrix X, vector paramv) {
matrix[2,2] Xs = X[1:2];
return sum( Xs * paramv);
}
}
without optimization:
template <typename T1__>
stan::promote_args_t<T1__>
foo(const Eigen::Matrix<double, -1, -1>& X,
const Eigen::Matrix<T1__, -1, 1>& paramv, std::ostream* pstream__) {
using local_scalar_t__ = stan::promote_args_t<T1__>;
const static bool propto__ = true;
(void) propto__;
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
(void) DUMMY_VAR__; // suppress unused var warning
try {
Eigen::Matrix<local_scalar_t__, -1, -1> Xs;
Xs = Eigen::Matrix<local_scalar_t__, -1, -1>(2, 2);
stan::math::fill(Xs, DUMMY_VAR__);
current_statement__ = 1;
assign(Xs, nil_index_list(),
rvalue(X, cons_list(index_min_max(1, 2), nil_index_list()), "X"),
"assigning variable Xs");
current_statement__ = 2;
return sum(multiply(Xs, paramv));
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
// Next line prevents compiler griping about no return
throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
}
}
template <typename T1__>
stan::promote_args_t<T1__>
foo(const Eigen::Matrix<double, -1, -1>& X,
const Eigen::Matrix<T1__, -1, 1>& paramv, std::ostream* pstream__) {
using local_scalar_t__ = stan::promote_args_t<T1__>;
const static bool propto__ = true;
(void) propto__;
local_scalar_t__ DUMMY_VAR__(std::numeric_limits<double>::quiet_NaN());
(void) DUMMY_VAR__; // suppress unused var warning
try {
Eigen::Matrix<double, -1, -1> lcm_sym2__;
double lcm_sym1__;
{
Eigen::Matrix<double, -1, -1> Xs;
Xs = Eigen::Matrix<double, -1, -1>(2, 2);
stan::math::fill(Xs, std::numeric_limits<double>::quiet_NaN());
assign(lcm_sym2__, nil_index_list(),
rvalue(X, cons_list(index_min_max(1, 2), nil_index_list()), "X"),
"assigning variable lcm_sym2__");
current_statement__ = 2;
return sum(multiply(lcm_sym2__, paramv));
}
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
// Next line prevents compiler griping about no return
throw std::runtime_error("*** IF YOU SEE THIS, PLEASE REPORT A BUG ***");
}
}
So yeah, optimization for this case work really nice.
Without optimization the Xs is defined as
Eigen::Matrix<local_scalar_t__, -1, -1> Xs;
with optimization its
Eigen::Matrix<double, -1, -1> Xs;
If you want to see the diff: https://www.diffchecker.com/6utjyyLB