fixes (to avoid collisions) for AST profiling's function hash computations

This commit is contained in:
Vern Paxson 2024-09-11 16:47:54 +02:00 committed by Christian Kreibich
parent a390b3665b
commit adf3648554
2 changed files with 25 additions and 10 deletions

View file

@ -584,8 +584,9 @@ ProfileFuncs::ProfileFuncs(std::vector<FuncInfo>& funcs, is_compilable_pred pred
// side effects.
// Propagate previous hash if requested.
if ( ! compute_func_hashes && f.Profile() )
pf->SetHashVal(f.Profile()->HashVal());
auto prev_pf = f.Profile();
if ( ! compute_func_hashes && prev_pf && prev_pf->HasHashVal() )
pf->SetHashVal(prev_pf->HashVal());
f.SetProfile(std::move(pf));
func_profs[f.Func()] = f.ProfilePtr();
@ -834,17 +835,20 @@ void ProfileFuncs::ComputeTypeHashes(const std::vector<const Type*>& types) {
}
void ProfileFuncs::ComputeBodyHashes(std::vector<FuncInfo>& funcs) {
if ( compute_func_hashes )
for ( auto& f : funcs )
if ( ! f.ShouldSkip() )
ComputeProfileHash(f.ProfilePtr());
for ( auto& f : funcs ) {
if ( f.ShouldSkip() )
continue;
auto pf = f.ProfilePtr();
if ( compute_func_hashes || ! pf->HasHashVal() )
ComputeProfileHash(f.ProfilePtr());
}
for ( auto& l : lambdas ) {
auto pf = ExprProf(l);
func_profs[l->PrimaryFunc().get()] = pf;
lambda_primaries[l->Name()] = l->PrimaryFunc().get();
if ( compute_func_hashes )
if ( compute_func_hashes || ! pf->HasHashVal() )
ComputeProfileHash(pf);
}
}
@ -856,6 +860,12 @@ void ProfileFuncs::ComputeProfileHash(std::shared_ptr<ProfileFunc> pf) {
// prevent collisions due to elements with simple hashes
// (such as Stmt's or Expr's that are only represented by
// the hash of their tag).
h = merge_p_hashes(h, p_hash("params"));
auto& ov = pf->ProfiledScope()->OrderedVars();
int n = pf->NumParams();
for ( int i = 0; i < n; ++i )
h = merge_p_hashes(h, p_hash(ov[i]->Name()));
h = merge_p_hashes(h, p_hash("stmts"));
for ( auto i : pf->Stmts() )
h = merge_p_hashes(h, p_hash(i->Tag()));

View file

@ -131,7 +131,11 @@ public:
// Set this function's hash to the given value; retrieve that value.
void SetHashVal(p_hash_type hash) { hash_val = hash; }
p_hash_type HashVal() const { return hash_val; }
p_hash_type HashVal() const {
ASSERT(hash_val);
return *hash_val;
}
bool HasHashVal() const { return bool(hash_val); }
protected:
// Construct the profile for the given function signature and body.
@ -286,7 +290,7 @@ protected:
std::vector<p_hash_type> addl_hashes;
// Associated hash value.
p_hash_type hash_val = 0;
std::optional<p_hash_type> hash_val;
// How many when statements appear in the function body. We could
// track these individually, but to date all that's mattered is
@ -610,7 +614,8 @@ protected:
// These can arise for example due to lambdas or record attributes.
std::vector<const Expr*> pending_exprs;
// Whether to compute new hashes for the FuncInfo entries.
// Whether to compute new hashes for the FuncInfo entries. If the FuncInfo
// doesn't have a hash, it will always be computed.
bool compute_func_hashes;
// Whether the hashes for extended records should cover their final,