Skip to content

Optimization: Lazy code motion and SoA can combine to create uncompilable code #1463

@WardBrian

Description

@WardBrian

Initially reported stan-dev/cmdstan#1299

This stan code:

parameters {
  vector[1] x;
}
transformed parameters {
  vector[1] theta = 1 * x;
}
model {
    target += theta[1];  # using theta here seems necessary for the bug
}

When compiled with --Oexperimental, creates this C++:

      double lcm_sym8__;
      Eigen::Matrix<local_scalar_t__,-1,1> lcm_sym7__;
      current_statement__ = 1;
      auto x =
        in__.template read<
          stan::math::var_value<Eigen::Matrix<double,-1,1>>>(1);
      stan::math::var_value<Eigen::Matrix<double,-1,1>> theta =
        stan::math::var_value<Eigen::Matrix<double,-1,1>>(Eigen::Matrix<double,-1,1>::Constant(1,
                                                            std::numeric_limits<double>::quiet_NaN(
                                                              )));
      stan::model::assign(lcm_sym7__, stan::math::multiply(1, x),
        "assigning variable lcm_sym7__");

The assign here fails because lcm_sym7__ is AoS, but x (and therefor the return of multiply) is SoA.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions