From d2c6208421721a88bf8355bcf666375b684cf5a6 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Mon, 5 Aug 2024 09:17:13 +0100 Subject: [PATCH] allow profiling without updating of hash values --- src/script_opt/ProfileFunc.cc | 20 +++++++++++++++----- src/script_opt/ProfileFunc.h | 19 ++++++++++++------- src/script_opt/ScriptOpt.cc | 10 +++------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/script_opt/ProfileFunc.cc b/src/script_opt/ProfileFunc.cc index a62e436774..53f71be393 100644 --- a/src/script_opt/ProfileFunc.cc +++ b/src/script_opt/ProfileFunc.cc @@ -546,7 +546,9 @@ void ProfileFunc::CheckRecordConstructor(TypePtr t) { } } -ProfileFuncs::ProfileFuncs(std::vector& funcs, is_compilable_pred pred, bool _full_record_hashes) { +ProfileFuncs::ProfileFuncs(std::vector& funcs, is_compilable_pred pred, bool _compute_func_hashes, + bool _full_record_hashes) { + compute_func_hashes = _compute_func_hashes; full_record_hashes = _full_record_hashes; for ( auto& f : funcs ) { @@ -558,6 +560,11 @@ ProfileFuncs::ProfileFuncs(std::vector& funcs, is_compilable_pred pred // Track the profile even if we're not compiling the function, since // the AST optimizer will still need it to reason about function-call // side effects. + + // Propagate previous hash if requested. + if ( ! compute_func_hashes && f.Profile() ) + pf->SetHashVal(f.Profile()->HashVal()); + f.SetProfile(std::move(pf)); func_profs[f.Func()] = f.ProfilePtr(); } @@ -805,15 +812,18 @@ void ProfileFuncs::ComputeTypeHashes(const std::vector& types) { } void ProfileFuncs::ComputeBodyHashes(std::vector& funcs) { - for ( auto& f : funcs ) - if ( ! f.ShouldSkip() ) - ComputeProfileHash(f.ProfilePtr()); + if ( compute_func_hashes ) + for ( auto& f : funcs ) + if ( ! f.ShouldSkip() ) + ComputeProfileHash(f.ProfilePtr()); for ( auto& l : lambdas ) { auto pf = ExprProf(l); func_profs[l->PrimaryFunc().get()] = pf; lambda_primaries[l->Name()] = l->PrimaryFunc().get(); - ComputeProfileHash(pf); + + if ( compute_func_hashes ) + ComputeProfileHash(pf); } } diff --git a/src/script_opt/ProfileFunc.h b/src/script_opt/ProfileFunc.h index e75688adb6..2ce4dfe562 100644 --- a/src/script_opt/ProfileFunc.h +++ b/src/script_opt/ProfileFunc.h @@ -347,13 +347,15 @@ using is_compilable_pred = bool (*)(const ProfileFunc*, const char** reason); // Collectively profile an entire collection of functions. class ProfileFuncs { public: - // Updates entries in "funcs" to include profiles. If pred is - // non-nil, then it is called for each profile to see whether it's - // compilable, and, if not, the FuncInfo is marked as ShouldSkip(). - // "full_record_hashes" controls whether the hashes for extended - // records covers their final, full form, or should only their - // original fields. - ProfileFuncs(std::vector& funcs, is_compilable_pred pred, bool full_record_hashes); + // Updates entries in "funcs" to include profiles. If pred is non-nil, + // then it is called for each profile to see whether it's compilable, + // and, if not, the FuncInfo is marked as ShouldSkip(). + // "compute_func_hashes" governs whether we compute hashes for the + // FuncInfo entries, or keep their existing ones. "full_record_hashes" + // controls whether the hashes for extended records covers their final, + // full form, or should only their original fields. + ProfileFuncs(std::vector& funcs, is_compilable_pred pred, bool compute_func_hashes, + bool full_record_hashes); // The following accessors provide a global profile across all of // the (non-skipped) functions in "funcs". See the comments for @@ -604,6 +606,9 @@ protected: // These can arise for example due to lambdas or record attributes. std::vector pending_exprs; + // Whether to compute new hashes for the FuncInfo entries. + bool compute_func_hashes; + // Whether the hashes for extended records should cover their final, // full form, or only their original fields. bool full_record_hashes; diff --git a/src/script_opt/ScriptOpt.cc b/src/script_opt/ScriptOpt.cc index c41e6cf429..99fd8d76df 100644 --- a/src/script_opt/ScriptOpt.cc +++ b/src/script_opt/ScriptOpt.cc @@ -391,7 +391,7 @@ static void use_CPP() { int num_used = 0; - auto pfs = std::make_unique(funcs, is_CPP_compilable, false); + auto pfs = std::make_unique(funcs, is_CPP_compilable, true, false); for ( auto& f : funcs ) { auto hash = f.Profile()->HashVal(); @@ -435,18 +435,16 @@ static void use_CPP() { reporter->FatalError("no C++ functions found to use"); } -static void generate_CPP() { +static void generate_CPP(std::shared_ptr pfs) { const auto gen_name = CPP_dir + "CPP-gen.cc"; const bool standalone = analysis_options.gen_standalone_CPP; const bool report = analysis_options.report_uncompilable; - auto pfs = std::make_shared(funcs, is_CPP_compilable, false); - CPPCompile cpp(funcs, pfs, gen_name, standalone, report); } -static void analyze_scripts_for_ZAM() { +static void analyze_scripts_for_ZAM(std::shared_ptr pfs) { if ( analysis_options.usage_issues > 0 && analysis_options.optimize_AST ) { fprintf(stderr, "warning: \"-O optimize-AST\" option is incompatible with -u option, " @@ -454,8 +452,6 @@ static void analyze_scripts_for_ZAM() { analysis_options.optimize_AST = false; } - auto pfs = std::make_shared(funcs, nullptr, true); - if ( analysis_options.profile_ZAM ) { #ifdef ENABLE_ZAM_PROFILE AST_blocks = std::make_unique(funcs);