fix to -O gen-C++ for recent AST profiling changes for identifying function parameters

This commit is contained in:
Vern Paxson 2024-09-11 16:25:21 +02:00 committed by Christian Kreibich
parent 2b64e3b05e
commit d1c31927c4
2 changed files with 27 additions and 16 deletions

View file

@ -149,6 +149,7 @@ void CPPCompile::DeclareLocals(const ProfileFunc* pf, const IDPList* lambda_ids)
capture_names.insert(CaptureName(li)); capture_names.insert(CaptureName(li));
const auto& ls = pf->Locals(); const auto& ls = pf->Locals();
int num_params = static_cast<int>(pf->Params().size());
// Track whether we generated a declaration. This is just for // Track whether we generated a declaration. This is just for
// tidiness in the output. // tidiness in the output.
@ -162,7 +163,7 @@ void CPPCompile::DeclareLocals(const ProfileFunc* pf, const IDPList* lambda_ids)
// No need to declare these, they're passed in as parameters. // No need to declare these, they're passed in as parameters.
ln = cn; ln = cn;
else if ( params.count(l) == 0 ) { // Not a parameter, so must be a local. else if ( params.count(l) == 0 && l->Offset() >= num_params ) { // Not a parameter, so must be a local.
Emit("%s %s;", FullTypeName(l->GetType()), ln); Emit("%s %s;", FullTypeName(l->GetType()), ln);
did_decl = true; did_decl = true;
} }

View file

@ -41,7 +41,20 @@ ProfileFunc::ProfileFunc(const Func* func, const StmtPtr& body, bool _abs_rec_fi
} }
} }
Profile(profiled_func_t.get(), body); TrackType(profiled_func_t);
body->Traverse(this);
// Examine the locals and identify the parameters based on their offsets
// (being careful not to be fooled by captures that incidentally have low
// offsets). This approach allows us to accommodate function definitions
// that use different parameter names than appear in the original
// declaration.
num_params = profiled_func_t->Params()->NumFields();
for ( auto l : locals ) {
if ( captures.count(l) == 0 && l->Offset() < num_params )
params.insert(l);
}
} }
ProfileFunc::ProfileFunc(const Stmt* s, bool _abs_rec_fields) { ProfileFunc::ProfileFunc(const Stmt* s, bool _abs_rec_fields) {
@ -68,26 +81,23 @@ ProfileFunc::ProfileFunc(const Expr* e, bool _abs_rec_fields) {
captures_offsets[oid] = offset++; captures_offsets[oid] = offset++;
} }
Profile(func->GetType()->AsFuncType(), func->Ingredients()->Body()); auto ft = func->GetType()->AsFuncType();
} auto& body = func->Ingredients()->Body();
else
// We don't have a function type, so do the traversal
// directly.
e->Traverse(this);
}
void ProfileFunc::Profile(const FuncType* ft, const StmtPtr& body) {
num_params = ft->Params()->NumFields(); num_params = ft->Params()->NumFields();
assert(profiled_scope != nullptr);
auto& ov = profiled_scope->OrderedVars(); auto& ov = profiled_scope->OrderedVars();
for ( int i = 0; i < num_params; ++i ) for ( int i = 0; i < num_params; ++i )
params.insert(ov[i].get()); params.insert(ov[i].get());
TrackType(ft); TrackType(ft);
body->Traverse(this); body->Traverse(this);
}
else
// We don't have a function type, so do the traversal
// directly.
e->Traverse(this);
} }
TraversalCode ProfileFunc::PreStmt(const Stmt* s) { TraversalCode ProfileFunc::PreStmt(const Stmt* s) {