regularized (some) types of pointers used in script optimization

This commit is contained in:
Vern Paxson 2023-12-08 15:00:29 -05:00 committed by Arne Welzel
parent dd389c0380
commit 709d410fcd
18 changed files with 85 additions and 93 deletions

View file

@ -16,7 +16,7 @@ shared_ptr<CPP_InitInfo> CPPCompile::RegisterAttributes(const AttributesPtr& att
if ( pa != processed_attrs.end() )
return pa->second;
attributes.AddKey(attrs, pfs.HashAttrs(attrs));
attributes.AddKey(attrs, pfs->HashAttrs(attrs));
// The cast is just so we can make an IntrusivePtr.
auto a_rep = const_cast<Attributes*>(attributes.GetRep(attrs));

View file

@ -124,8 +124,8 @@ namespace zeek::detail {
class CPPCompile {
public:
CPPCompile(std::vector<FuncInfo>& _funcs, ProfileFuncs& pfs, const std::string& gen_name, bool _standalone,
bool report_uncompilable);
CPPCompile(std::vector<FuncInfo>& _funcs, std::shared_ptr<ProfileFuncs> pfs, const std::string& gen_name,
bool _standalone, bool report_uncompilable);
~CPPCompile();
// Constructing a CPPCompile object does all of the compilation.
@ -191,7 +191,7 @@ public:
// However, we can't generate that code when first encountering
// the attribute, because doing so will need to refer to the names
// of types, and initially those are unavailable (because the type's
// representatives, per pfs.RepTypes(), might not have yet been
// representatives, per pfs->RepTypes(), might not have yet been
// tracked). So instead we track the associated CallExprInitInfo
// objects, and after all types have been tracked, then spin
// through them to generate the code.
@ -314,7 +314,7 @@ private:
std::vector<FuncInfo>& funcs;
// The global profile of all of the functions.
ProfileFuncs& pfs;
std::shared_ptr<ProfileFuncs> pfs;
// Script functions that we are able to compile. We compute
// these ahead of time so that when compiling script function A
@ -894,7 +894,7 @@ private:
// Returns the "representative" for a given type, used to ensure
// that we re-use the C++ variable corresponding to a type and
// don't instantiate redundant instances.
const Type* TypeRep(const Type* t) { return pfs.TypeRep(t); }
const Type* TypeRep(const Type* t) { return pfs->TypeRep(t); }
const Type* TypeRep(const TypePtr& t) { return TypeRep(t.get()); }
// Low-level C++ representations for types, of various flavors.

View file

@ -13,9 +13,9 @@ namespace zeek::detail {
using namespace std;
CPPCompile::CPPCompile(vector<FuncInfo>& _funcs, ProfileFuncs& _pfs, const string& gen_name, bool _standalone,
bool report_uncompilable)
: funcs(_funcs), pfs(_pfs), standalone(_standalone) {
CPPCompile::CPPCompile(vector<FuncInfo>& _funcs, std::shared_ptr<ProfileFuncs> _pfs, const string& gen_name,
bool _standalone, bool report_uncompilable)
: funcs(_funcs), pfs(std::move(_pfs)), standalone(_standalone) {
auto target_name = gen_name.c_str();
write_file = fopen(target_name, "w");
@ -99,21 +99,21 @@ void CPPCompile::Compile(bool report_uncompilable) {
GenProlog();
// Track all of the types we'll be using.
for ( const auto& t : pfs.RepTypes() ) {
for ( const auto& t : pfs->RepTypes() ) {
TypePtr tp{NewRef{}, (Type*)(t)};
types.AddKey(tp, pfs.HashType(t));
types.AddKey(tp, pfs->HashType(t));
}
NL();
for ( auto& g : pfs.AllGlobals() )
for ( auto& g : pfs->AllGlobals() )
CreateGlobal(g);
for ( const auto& e : pfs.Events() )
for ( const auto& e : pfs->Events() )
if ( AddGlobal(e, "gl") )
Emit("EventHandlerPtr %s_ev;", globals[string(e)]);
for ( const auto& t : pfs.RepTypes() ) {
for ( const auto& t : pfs->RepTypes() ) {
ASSERT(types.HasKey(t));
TypePtr tp{NewRef{}, (Type*)(t)};
RegisterType(tp);
@ -131,14 +131,14 @@ void CPPCompile::Compile(bool report_uncompilable) {
// be identical. In that case, we don't want to generate the lambda
// twice, but we do want to map the second one to the same body name.
unordered_map<string, const Stmt*> lambda_ASTs;
for ( const auto& l : pfs.Lambdas() ) {
for ( const auto& l : pfs->Lambdas() ) {
const auto& n = l->Name();
const auto body = l->Ingredients()->Body().get();
if ( lambda_ASTs.count(n) > 0 )
// Reuse previous body.
body_names[body] = body_names[lambda_ASTs[n]];
else {
DeclareLambda(l, pfs.ExprProf(l).get());
DeclareLambda(l, pfs->ExprProf(l).get());
lambda_ASTs[n] = body;
}
}
@ -151,12 +151,12 @@ void CPPCompile::Compile(bool report_uncompilable) {
CompileFunc(func);
lambda_ASTs.clear();
for ( const auto& l : pfs.Lambdas() ) {
for ( const auto& l : pfs->Lambdas() ) {
const auto& n = l->Name();
if ( lambda_ASTs.count(n) > 0 )
continue;
CompileLambda(l, pfs.ExprProf(l).get());
CompileLambda(l, pfs->ExprProf(l).get());
lambda_ASTs[n] = l->Ingredients()->Body().get();
}

View file

@ -151,7 +151,7 @@ string CPPCompile::GenNameExpr(const NameExpr* ne, GenType gt) {
if ( t->Tag() == TYPE_FUNC && ! is_global_var ) {
auto func = n->Name();
if ( globals.count(func) > 0 && pfs.BiFGlobals().count(n) == 0 )
if ( globals.count(func) > 0 && pfs->BiFGlobals().count(n) == 0 )
return GenericValPtrToGT(IDNameStr(n), t, gt);
}
@ -202,9 +202,9 @@ string CPPCompile::GenIncrExpr(const Expr* e, GenType gt, bool is_incr, bool top
// Make sure any newly created types are known to
// the profiler.
(void)pfs.HashType(one_e->GetType());
(void)pfs.HashType(rhs->GetType());
(void)pfs.HashType(assign->GetType());
(void)pfs->HashType(one_e->GetType());
(void)pfs->HashType(rhs->GetType());
(void)pfs->HashType(assign->GetType());
auto gen = GenExpr(assign, GEN_DONT_CARE, top_level);
@ -269,10 +269,10 @@ string CPPCompile::GenCallExpr(const CallExpr* c, GenType gt, bool top_level) {
//
// If it is a BiF *that's also a global variable*, then
// we need to look up the BiF version of the global.
if ( pfs.BiFGlobals().count(f_id) == 0 )
if ( pfs->BiFGlobals().count(f_id) == 0 )
gen += +"->AsFunc()";
else if ( pfs.Globals().count(f_id) > 0 )
else if ( pfs->Globals().count(f_id) > 0 )
// The BiF version has an extra "_", per
// AddBiF(..., true).
gen = globals[string(id_name) + "_"];
@ -511,8 +511,8 @@ string CPPCompile::GenAddToExpr(const Expr* e, GenType gt, bool top_level) {
// Make sure any newly created types are known to
// the profiler.
(void)pfs.HashType(rhs->GetType());
(void)pfs.HashType(assign->GetType());
(void)pfs->HashType(rhs->GetType());
(void)pfs->HashType(assign->GetType());
return GenExpr(assign, gt, top_level);
}
@ -542,8 +542,8 @@ string CPPCompile::GenRemoveFromExpr(const Expr* e, GenType gt, bool top_level)
// Make sure any newly created types are known to
// the profiler.
(void)pfs.HashType(rhs->GetType());
(void)pfs.HashType(assign->GetType());
(void)pfs->HashType(rhs->GetType());
(void)pfs->HashType(assign->GetType());
return GenExpr(assign, gt, top_level);
}

View file

@ -180,7 +180,7 @@ void CPPCompile::InitializeGlobals() {
for ( const auto& ginit : IDOptInfo::GetGlobalInitExprs() ) {
auto g = ginit.Id();
if ( pfs.Globals().count(g) == 0 )
if ( pfs->Globals().count(g) == 0 )
continue;
auto ic = ginit.IC();

View file

@ -9,9 +9,9 @@ using namespace std;
void CPPCompile::CreateGlobal(const ID* g) {
auto gn = string(g->Name());
bool is_bif = pfs.BiFGlobals().count(g) > 0;
bool is_bif = pfs->BiFGlobals().count(g) > 0;
if ( pfs.Globals().count(g) == 0 ) {
if ( pfs->Globals().count(g) == 0 ) {
// Only used in the context of calls. If it's compilable,
// then we'll call it directly.
if ( compilable_funcs.count(gn) > 0 ) {
@ -28,7 +28,7 @@ void CPPCompile::CreateGlobal(const ID* g) {
if ( AddGlobal(gn, "gl") ) { // We'll be creating this global.
Emit("IDPtr %s;", globals[gn]);
if ( pfs.Events().count(gn) > 0 )
if ( pfs->Events().count(gn) > 0 )
// This is an event that's also used as a variable.
Emit("EventHandlerPtr %s_ev;", globals[gn]);