diff --git a/src/analysis_and_optimization/Dataflow_utils.ml b/src/analysis_and_optimization/Dataflow_utils.ml index 59a6b4147d..bb2062fc7a 100644 --- a/src/analysis_and_optimization/Dataflow_utils.ml +++ b/src/analysis_and_optimization/Dataflow_utils.ml @@ -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 diff --git a/src/analysis_and_optimization/Mir_utils.ml b/src/analysis_and_optimization/Mir_utils.ml index 28d9b54aec..5c098ce1eb 100644 --- a/src/analysis_and_optimization/Mir_utils.ml +++ b/src/analysis_and_optimization/Mir_utils.ml @@ -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 @@ -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 diff --git a/src/middle/Stmt.ml b/src/middle/Stmt.ml index fa33ca2761..4b2c94e605 100644 --- a/src/middle/Stmt.ml +++ b/src/middle/Stmt.ml @@ -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>@[%a@]@;}" Fmt.(list pp_s ~sep:cut) stmts + | Profile (name, stmts) -> + Fmt.pf ppf "profile(%s){@;<1 2>@[%a@]@;}" name + Fmt.(list pp_s ~sep:cut) + stmts | Block stmts -> Fmt.pf ppf "{@;<1 2>@[%a@]@;}" Fmt.(list pp_s ~sep:cut) stmts | SList stmts -> Fmt.(list pp_s ~sep:cut |> vbox) ppf stmts diff --git a/test/integration/good/compiler-optimizations/cpp.expected b/test/integration/good/compiler-optimizations/cpp.expected index ac6f345221..d54d6644e0 100644 --- a/test/integration/good/compiler-optimizations/cpp.expected +++ b/test/integration/good/compiler-optimizations/cpp.expected @@ -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 locations_array__ = +static constexpr std::array 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)", @@ -4211,19 +4214,18 @@ class copy_prop_profile_model final : public model_base_crtp::min(); + current_statement__ = 16; context__.validate_dims("data initialization", "N", "int", std::vector{}); N = std::numeric_limits::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__]); @@ -4257,18 +4259,22 @@ class copy_prop_profile_model final : public model_base_crtp lcm_sym4__; - Eigen::Matrix X; + double lcm_sym6__; + double lcm_sym5__; + Eigen::Matrix lcm_sym4__; + stan::conditional_var_value_t> X; current_statement__ = 1; - X = in__.template read>(N, N); + X = in__.template read< + stan::conditional_var_value_t>>(N, N); { current_statement__ = 2; stan::math::validate_non_negative_index("vec", "N", N); Eigen::Matrix vec = Eigen::Matrix::Constant(N, std::numeric_limits::quiet_NaN()); - current_statement__ = 6; + current_statement__ = 7; { stan::math::profile profile__("test", const_cast(profiles__)); @@ -4279,23 +4285,28 @@ class copy_prop_profile_model final : public model_base_crtp::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 vec3 = Eigen::Matrix::Constant(N, std::numeric_limits::quiet_NaN()); { - current_statement__ = 9; + current_statement__ = 11; stan::math::validate_non_negative_index("vec4", "N", N); - Eigen::Matrix vec4 = - Eigen::Matrix::Constant(N, - std::numeric_limits::quiet_NaN()); + Eigen::Matrix 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__]); @@ -4367,8 +4378,7 @@ class copy_prop_profile_model final : public model_base_crtp::min(); Eigen::Matrix X = Eigen::Matrix::Constant(N, N, DUMMY_VAR__); lcm_sym1__ = stan::math::logical_gte(N, 1); @@ -4413,10 +4423,10 @@ class copy_prop_profile_model final : public model_base_crtp& 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__) {} @@ -4426,10 +4436,10 @@ class copy_prop_profile_model final : public model_base_crtp& 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__) {} diff --git a/test/integration/good/compiler-optimizations/cppO1.expected b/test/integration/good/compiler-optimizations/cppO1.expected index 167a95fdc0..a532e75cc3 100644 --- a/test/integration/good/compiler-optimizations/cppO1.expected +++ b/test/integration/good/compiler-optimizations/cppO1.expected @@ -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 locations_array__ = +static constexpr std::array 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)", @@ -1529,20 +1530,19 @@ class copy_prop_profile_model final : public model_base_crtp::min(); + current_statement__ = 16; context__.validate_dims("data initialization", "N", "int", std::vector{}); N = std::numeric_limits::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__]); } @@ -1587,7 +1587,7 @@ class copy_prop_profile_model final : public model_base_crtp>(Eigen::Matrix::Constant(N, std::numeric_limits::quiet_NaN( ))); - current_statement__ = 6; + current_statement__ = 7; { stan::math::profile profile__("test", const_cast(profiles__)); @@ -1597,24 +1597,30 @@ class copy_prop_profile_model final : public model_base_crtp vec3 = - Eigen::Matrix::Constant(N, DUMMY_VAR__); + stan::conditional_var_value_t> vec3 = + stan::conditional_var_value_t>(Eigen::Matrix::Constant(N, + std::numeric_limits::quiet_NaN( + ))); { - current_statement__ = 10; + current_statement__ = 11; stan::math::validate_non_negative_index("vec4", "N", N); Eigen::Matrix 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) { @@ -1684,8 +1690,7 @@ class copy_prop_profile_model final : public model_base_crtp::min(); Eigen::Matrix X = Eigen::Matrix::Constant(N, N, DUMMY_VAR__); for (int sym1__ = 1; sym1__ <= N; ++sym1__) {