more extensive ZAM inlining & compilation of lambdas

This commit is contained in:
Vern Paxson 2023-07-13 12:03:34 -07:00
parent b9949560c6
commit 1ff490b41c
10 changed files with 156 additions and 42 deletions

View file

@ -36,7 +36,7 @@ static ZAMCompiler* ZAM = nullptr;
static bool generating_CPP = false;
static std::string CPP_dir; // where to generate C++ code
static std::unordered_set<const ScriptFunc*> lambdas;
static std::unordered_map<const ScriptFunc*, LambdaExpr*> lambdas;
static std::unordered_set<const ScriptFunc*> when_lambdas;
static ScriptFuncPtr global_stmts;
@ -52,7 +52,7 @@ void analyze_lambda(LambdaExpr* l)
{
auto& pf = l->PrimaryFunc();
analyze_func(pf);
lambdas.insert(pf.get());
lambdas[pf.get()] = l;
}
void analyze_when_lambda(LambdaExpr* l)
@ -185,7 +185,7 @@ static void optimize_func(ScriptFunc* f, std::shared_ptr<ProfileFunc> pf, ScopeP
push_existing_scope(scope);
auto rc = std::make_shared<Reducer>();
auto rc = std::make_shared<Reducer>(f);
auto new_body = rc->Reduce(body);
if ( reporter->Errors() > 0 )
@ -496,22 +496,19 @@ static void analyze_scripts_for_ZAM(std::unique_ptr<ProfileFuncs>& pfs)
if ( inl )
{
for ( auto& f : funcs )
for ( auto& g : pfs->Globals() )
{
for ( const auto& g : f.Profile()->Globals() )
{
if ( g->GetType()->Tag() != TYPE_FUNC )
continue;
if ( g->GetType()->Tag() != TYPE_FUNC )
continue;
auto v = g->GetVal();
if ( ! v )
continue;
auto v = g->GetVal();
if ( ! v )
continue;
auto func = v->AsFunc();
auto func = v->AsFunc();
if ( inl->WasInlined(func) )
func_used_indirectly.insert(func);
}
if ( inl->WasInlined(func) )
func_used_indirectly.insert(func);
}
}
@ -520,7 +517,8 @@ static void analyze_scripts_for_ZAM(std::unique_ptr<ProfileFuncs>& pfs)
for ( auto& f : funcs )
{
auto func = f.Func();
bool is_lambda = lambdas.count(func) > 0;
auto l = lambdas.find(func);
bool is_lambda = l != lambdas.end();
if ( ! analysis_options.only_funcs.empty() || ! analysis_options.only_files.empty() )
{
@ -539,6 +537,9 @@ static void analyze_scripts_for_ZAM(std::unique_ptr<ProfileFuncs>& pfs)
optimize_func(func, f.ProfilePtr(), f.Scope(), new_body);
f.SetBody(new_body);
if ( is_lambda )
l->second->ReplaceBody(new_body);
did_one = true;
}