support for discarding ASTs once compiled via ZAM script optimization

This commit is contained in:
Vern Paxson 2023-07-20 09:39:05 -07:00
parent 79c53c9ed6
commit 91d70e6dd4
13 changed files with 55 additions and 17 deletions

View file

@ -31,8 +31,6 @@ void (*CPP_activation_hook)() = nullptr;
// Tracks all of the loaded functions (including event handlers and hooks).
static std::vector<FuncInfo> funcs;
static ZAMCompiler* ZAM = nullptr;
static bool generating_CPP = false;
static std::string CPP_dir; // where to generate C++ code
@ -78,12 +76,10 @@ const FuncInfo* analyze_global_stmts(Stmt* stmts)
auto sc = current_scope();
std::vector<IDPtr> empty_inits;
StmtPtr stmts_p{NewRef{}, stmts};
global_stmts = make_intrusive<ScriptFunc>(id);
global_stmts->AddBody(stmts_p, empty_inits, sc->Length());
funcs.emplace_back(global_stmts, sc, stmts_p, 0);
global_stmts->AddBody(stmts->ThisPtr(), empty_inits, sc->Length());
funcs.emplace_back(global_stmts, sc, stmts->ThisPtr(), 0);
return &funcs.back();
}
@ -253,15 +249,15 @@ static void optimize_func(ScriptFunc* f, std::shared_ptr<ProfileFunc> pf, ScopeP
if ( analysis_options.gen_ZAM_code )
{
ZAM = new ZAMCompiler(f, pf, scope, new_body, ud, rc);
ZAMCompiler ZAM(f, pf, scope, new_body, ud, rc);
new_body = ZAM->CompileBody();
new_body = ZAM.CompileBody();
if ( reporter->Errors() > 0 )
return;
if ( analysis_options.dump_ZAM )
ZAM->Dump();
ZAM.Dump();
f->ReplaceBody(body, new_body);
body = new_body;
@ -544,6 +540,25 @@ static void analyze_scripts_for_ZAM(std::unique_ptr<ProfileFuncs>& pfs)
finalize_functions(funcs);
}
void clear_script_analysis()
{
IDOptInfo::ClearGlobalInitExprs();
// We need to explicitly clear out the optimization information
// associated with identifiers. They have reference loops with
// the parent identifier that will prevent reclamation of the
// identifiers (and the optimization information) upon Unref'ing
// when discarding the scopes and ASTs.
for ( auto& f : funcs )
for ( auto& id : f.Scope()->OrderedVars() )
id->ClearOptInfo();
funcs.clear();
non_recursive_funcs.clear();
lambdas.clear();
when_lambdas.clear();
}
void analyze_scripts(bool no_unused_warnings)
{
init_options();