Skip to content

Commit

Permalink
Consistently treat profile blocks as blocks in optimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Jan 27, 2023
1 parent 6208d92 commit da5c617
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/analysis_and_optimization/Dataflow_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ let build_cf_graphs ?(flatten_loops = false) ?(blocks_after_body = true)
Set.Poly.diff substmt_state_unlooped.breaks in_state.breaks ]
in
({substmt_state_unlooped with exits= loop_exits}, loop_predecessors)
| Block _ when blocks_after_body ->
| (Block _ | Profile _) when blocks_after_body ->
(* Block statements are preceded by the natural exit points of the block
body *)
let block_predecessors = substmt_state_unlooped.exits in
Expand Down
3 changes: 2 additions & 1 deletion src/analysis_and_optimization/Mir_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ let rec var_declarations Stmt.Fixed.{pattern; _} : string Set.Poly.t =
| IfElse (_, s, None) | While (_, s) | For {body= s; _} -> var_declarations s
| IfElse (_, s1, Some s2) ->
Set.Poly.union (var_declarations s1) (var_declarations s2)
| Block slist | SList slist ->
| Block slist | SList slist | Profile (_, slist) ->
Set.Poly.union_list (List.map ~f:var_declarations slist)
| _ -> Set.Poly.empty

Expand Down Expand Up @@ -431,6 +431,7 @@ let cleanup_empty_stmts stmts =
let is_decl = function {pattern= Decl _; _} -> true | _ -> false in
let flatten_block s =
match s.pattern with
(* NB: Does not include Profile since we don't want to remove those blocks *)
| SList ls | Block ls ->
if List.for_all ~f:(Fn.non is_decl) ls then ls else [s]
| _ -> [s] in
Expand Down
6 changes: 4 additions & 2 deletions src/middle/Stmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ module Fixed = struct
| For {loopvar; lower; upper; body} ->
Fmt.pf ppf "for(%s in %a:%a) %a" loopvar pp_e lower pp_e upper pp_s
body
| Profile (_, stmts) ->
Fmt.pf ppf "{@;<1 2>@[<v>%a@]@;}" Fmt.(list pp_s ~sep:cut) stmts
| Profile (name, stmts) ->
Fmt.pf ppf "profile(%s){@;<1 2>@[<v>%a@]@;}" name
Fmt.(list pp_s ~sep:cut)
stmts
| Block stmts ->
Fmt.pf ppf "{@;<1 2>@[<v>%a@]@;}" Fmt.(list pp_s ~sep:cut) stmts
| SList stmts -> Fmt.(list pp_s ~sep:cut |> vbox) ppf stmts
Expand Down
70 changes: 40 additions & 30 deletions test/integration/good/compiler-optimizations/cpp.expected
Original file line number Diff line number Diff line change
Expand Up @@ -4172,18 +4172,21 @@ namespace copy_prop_profile_model_namespace {
using stan::model::model_base_crtp;
using namespace stan::math;
stan::math::profile_map profiles__;
static constexpr std::array<const char*, 16> locations_array__ =
static constexpr std::array<const char*, 19> locations_array__ =
{" (found before start of program)",
" (in 'copy-prop-profile.stan', line 5, column 2 to column 16)",
" (in 'copy-prop-profile.stan', line 9, column 13 to column 14)",
" (in 'copy-prop-profile.stan', line 9, column 2 to column 20)",
" (in 'copy-prop-profile.stan', line 12, column 15 to column 16)",
" (in 'copy-prop-profile.stan', line 12, column 4 to column 45)",
" (in 'copy-prop-profile.stan', line 13, column 4 to column 15)",
" (in 'copy-prop-profile.stan', line 11, column 2 to line 14, column 3)",
" (in 'copy-prop-profile.stan', line 16, column 2 to column 21)",
" (in 'copy-prop-profile.stan', line 18, column 13 to column 14)",
" (in 'copy-prop-profile.stan', line 18, column 2 to column 21)",
" (in 'copy-prop-profile.stan', line 20, column 15 to column 16)",
" (in 'copy-prop-profile.stan', line 20, column 4 to column 45)",
" (in 'copy-prop-profile.stan', line 21, column 4 to column 16)",
" (in 'copy-prop-profile.stan', line 19, column 2 to line 22, column 3)",
" (in 'copy-prop-profile.stan', line 24, column 2 to column 22)",
" (in 'copy-prop-profile.stan', line 2, column 2 to column 17)",
Expand Down Expand Up @@ -4211,19 +4214,18 @@ class copy_prop_profile_model final : public model_base_crtp<copy_prop_profile_m
// suppress unused var warning
(void) DUMMY_VAR__;
try {
int pos__;
pos__ = 1;
current_statement__ = 13;
int pos__ = std::numeric_limits<int>::min();
current_statement__ = 16;
context__.validate_dims("data initialization", "N", "int",
std::vector<size_t>{});
N = std::numeric_limits<int>::min();
current_statement__ = 13;
current_statement__ = 16;
N = context__.vals_i("N")[(1 - 1)];
current_statement__ = 13;
current_statement__ = 16;
stan::math::check_greater_or_equal(function__, "N", N, 0);
current_statement__ = 14;
current_statement__ = 17;
stan::math::validate_non_negative_index("X", "N", N);
current_statement__ = 15;
current_statement__ = 18;
stan::math::validate_non_negative_index("X", "N", N);
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
Expand Down Expand Up @@ -4257,18 +4259,22 @@ class copy_prop_profile_model final : public model_base_crtp<copy_prop_profile_m
// suppress unused var warning
(void) function__;
try {
local_scalar_t__ lcm_sym5__;
Eigen::Matrix<local_scalar_t__,1,-1> lcm_sym4__;
Eigen::Matrix<local_scalar_t__,-1,-1> X;
double lcm_sym6__;
double lcm_sym5__;
Eigen::Matrix<double,1,-1> lcm_sym4__;
stan::conditional_var_value_t<local_scalar_t__,
Eigen::Matrix<local_scalar_t__,-1,-1>> X;
current_statement__ = 1;
X = in__.template read<Eigen::Matrix<local_scalar_t__,-1,-1>>(N, N);
X = in__.template read<
stan::conditional_var_value_t<local_scalar_t__,
Eigen::Matrix<local_scalar_t__,-1,-1>>>(N, N);
{
current_statement__ = 2;
stan::math::validate_non_negative_index("vec", "N", N);
Eigen::Matrix<double,1,-1> vec =
Eigen::Matrix<double,1,-1>::Constant(N,
std::numeric_limits<double>::quiet_NaN());
current_statement__ = 6;
current_statement__ = 7;
{
stan::math::profile<local_scalar_t__> profile__("test",
const_cast<stan::math::profile_map&>(profiles__));
Expand All @@ -4279,23 +4285,28 @@ class copy_prop_profile_model final : public model_base_crtp<copy_prop_profile_m
std::numeric_limits<double>::quiet_NaN());
stan::model::assign(lcm_sym4__, stan::math::columns_dot_self(X),
"assigning variable lcm_sym4__");
stan::model::assign(vec2, lcm_sym4__, "assigning variable vec2");
current_statement__ = 6;
stan::model::assign(vec, vec2, "assigning variable vec");
}
lcm_sym5__ = stan::math::sum(lcm_sym4__);
lp_accum__.add(lcm_sym5__);
current_statement__ = 7;
current_statement__ = 8;
lp_accum__.add(stan::math::sum(vec));
current_statement__ = 9;
stan::math::validate_non_negative_index("vec3", "N", N);
Eigen::Matrix<double,1,-1> vec3 =
Eigen::Matrix<double,1,-1>::Constant(N,
std::numeric_limits<double>::quiet_NaN());
{
current_statement__ = 9;
current_statement__ = 11;
stan::math::validate_non_negative_index("vec4", "N", N);
Eigen::Matrix<double,1,-1> vec4 =
Eigen::Matrix<double,1,-1>::Constant(N,
std::numeric_limits<double>::quiet_NaN());
Eigen::Matrix<double,1,-1> vec4;
current_statement__ = 12;
stan::model::assign(vec4, lcm_sym4__, "assigning variable vec4");
current_statement__ = 13;
stan::model::assign(vec3, vec4, "assigning variable vec3");
}
current_statement__ = 12;
lp_accum__.add(lcm_sym5__);
current_statement__ = 15;
lp_accum__.add(stan::math::sum(vec3));
}
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
Expand Down Expand Up @@ -4367,8 +4378,7 @@ class copy_prop_profile_model final : public model_base_crtp<copy_prop_profile_m
(void) DUMMY_VAR__;
try {
int lcm_sym1__;
int pos__;
pos__ = 1;
int pos__ = std::numeric_limits<int>::min();
Eigen::Matrix<local_scalar_t__,-1,-1> X =
Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(N, N, DUMMY_VAR__);
lcm_sym1__ = stan::math::logical_gte(N, 1);
Expand Down Expand Up @@ -4413,10 +4423,10 @@ class copy_prop_profile_model final : public model_base_crtp<copy_prop_profile_m
constrained_param_names(std::vector<std::string>& param_names__, bool
emit_transformed_parameters__ = true, bool
emit_generated_quantities__ = true) const final {
for (int sym6__ = 1; sym6__ <= N; ++sym6__) {
for (int sym7__ = 1; sym7__ <= N; ++sym7__) {
for (int sym7__ = 1; sym7__ <= N; ++sym7__) {
for (int sym8__ = 1; sym8__ <= N; ++sym8__) {
param_names__.emplace_back(std::string() + "X" + '.' +
std::to_string(sym7__) + '.' + std::to_string(sym6__));
std::to_string(sym8__) + '.' + std::to_string(sym7__));
}
}
if (emit_transformed_parameters__) {}
Expand All @@ -4426,10 +4436,10 @@ class copy_prop_profile_model final : public model_base_crtp<copy_prop_profile_m
unconstrained_param_names(std::vector<std::string>& param_names__, bool
emit_transformed_parameters__ = true, bool
emit_generated_quantities__ = true) const final {
for (int sym6__ = 1; sym6__ <= N; ++sym6__) {
for (int sym7__ = 1; sym7__ <= N; ++sym7__) {
for (int sym7__ = 1; sym7__ <= N; ++sym7__) {
for (int sym8__ = 1; sym8__ <= N; ++sym8__) {
param_names__.emplace_back(std::string() + "X" + '.' +
std::to_string(sym7__) + '.' + std::to_string(sym6__));
std::to_string(sym8__) + '.' + std::to_string(sym7__));
}
}
if (emit_transformed_parameters__) {}
Expand Down
43 changes: 24 additions & 19 deletions test/integration/good/compiler-optimizations/cppO1.expected
Original file line number Diff line number Diff line change
Expand Up @@ -1488,13 +1488,14 @@ namespace copy_prop_profile_model_namespace {
using stan::model::model_base_crtp;
using namespace stan::math;
stan::math::profile_map profiles__;
static constexpr std::array<const char*, 18> locations_array__ =
static constexpr std::array<const char*, 19> locations_array__ =
{" (found before start of program)",
" (in 'copy-prop-profile.stan', line 5, column 2 to column 16)",
" (in 'copy-prop-profile.stan', line 9, column 13 to column 14)",
" (in 'copy-prop-profile.stan', line 9, column 2 to column 20)",
" (in 'copy-prop-profile.stan', line 12, column 15 to column 16)",
" (in 'copy-prop-profile.stan', line 12, column 4 to column 45)",
" (in 'copy-prop-profile.stan', line 13, column 4 to column 15)",
" (in 'copy-prop-profile.stan', line 11, column 2 to line 14, column 3)",
" (in 'copy-prop-profile.stan', line 16, column 2 to column 21)",
" (in 'copy-prop-profile.stan', line 18, column 13 to column 14)",
Expand Down Expand Up @@ -1529,20 +1530,19 @@ class copy_prop_profile_model final : public model_base_crtp<copy_prop_profile_m
// suppress unused var warning
(void) DUMMY_VAR__;
try {
int pos__;
pos__ = 1;
current_statement__ = 15;
int pos__ = std::numeric_limits<int>::min();
current_statement__ = 16;
context__.validate_dims("data initialization", "N", "int",
std::vector<size_t>{});
N = std::numeric_limits<int>::min();
current_statement__ = 15;
current_statement__ = 16;
N = context__.vals_i("N")[(1 - 1)];
current_statement__ = 15;
stan::math::check_greater_or_equal(function__, "N", N, 0);
current_statement__ = 16;
stan::math::validate_non_negative_index("X", "N", N);
stan::math::check_greater_or_equal(function__, "N", N, 0);
current_statement__ = 17;
stan::math::validate_non_negative_index("X", "N", N);
current_statement__ = 18;
stan::math::validate_non_negative_index("X", "N", N);
} catch (const std::exception& e) {
stan::lang::rethrow_located(e, locations_array__[current_statement__]);
}
Expand Down Expand Up @@ -1587,7 +1587,7 @@ class copy_prop_profile_model final : public model_base_crtp<copy_prop_profile_m
Eigen::Matrix<local_scalar_t__,1,-1>>(Eigen::Matrix<double,1,-1>::Constant(N,
std::numeric_limits<double>::quiet_NaN(
)));
current_statement__ = 6;
current_statement__ = 7;
{
stan::math::profile<local_scalar_t__> profile__("test",
const_cast<stan::math::profile_map&>(profiles__));
Expand All @@ -1597,24 +1597,30 @@ class copy_prop_profile_model final : public model_base_crtp<copy_prop_profile_m
current_statement__ = 5;
stan::model::assign(vec2, stan::math::columns_dot_self(X),
"assigning variable vec2");
current_statement__ = 6;
stan::model::assign(vec, vec2, "assigning variable vec");
}
current_statement__ = 7;
lp_accum__.add(stan::math::sum(vec2));
current_statement__ = 8;
lp_accum__.add(stan::math::sum(vec));
current_statement__ = 9;
stan::math::validate_non_negative_index("vec3", "N", N);
Eigen::Matrix<local_scalar_t__,1,-1> vec3 =
Eigen::Matrix<local_scalar_t__,1,-1>::Constant(N, DUMMY_VAR__);
stan::conditional_var_value_t<local_scalar_t__,
Eigen::Matrix<local_scalar_t__,1,-1>> vec3 =
stan::conditional_var_value_t<local_scalar_t__,
Eigen::Matrix<local_scalar_t__,1,-1>>(Eigen::Matrix<double,1,-1>::Constant(N,
std::numeric_limits<double>::quiet_NaN(
)));
{
current_statement__ = 10;
current_statement__ = 11;
stan::math::validate_non_negative_index("vec4", "N", N);
Eigen::Matrix<local_scalar_t__,1,-1> vec4;
current_statement__ = 11;
current_statement__ = 12;
stan::model::assign(vec4, stan::math::columns_dot_self(X),
"assigning variable vec4");
current_statement__ = 12;
current_statement__ = 13;
stan::model::assign(vec3, vec4, "assigning variable vec3");
}
current_statement__ = 14;
current_statement__ = 15;
lp_accum__.add(stan::math::sum(vec3));
}
} catch (const std::exception& e) {
Expand Down Expand Up @@ -1684,8 +1690,7 @@ class copy_prop_profile_model final : public model_base_crtp<copy_prop_profile_m
// suppress unused var warning
(void) DUMMY_VAR__;
try {
int pos__;
pos__ = 1;
int pos__ = std::numeric_limits<int>::min();
Eigen::Matrix<local_scalar_t__,-1,-1> X =
Eigen::Matrix<local_scalar_t__,-1,-1>::Constant(N, N, DUMMY_VAR__);
for (int sym1__ = 1; sym1__ <= N; ++sym1__) {
Expand Down

0 comments on commit da5c617

Please sign in to comment.