Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the number of memory allocations in def-use #4904

Merged
merged 16 commits into from
Sep 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Get rid of immortal data structures holding things forever
Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
  • Loading branch information
asl committed Sep 10, 2024
commit 5a5705d8116dd8ddfd0ad53ea8aa974308b062d5
56 changes: 18 additions & 38 deletions frontends/p4/simplifyDefUse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,40 +99,6 @@ class HasUses {
void doneWatching() { tracker = SliceTracker(); }
};

/// Creates temporary expressions from declarations and updates the refMap and typeMap
class DeclarationToExpression {
static DeclarationToExpression *instance;

/// Stores the temporary expressions so they can be reused
ordered_map<const IR::Declaration *, const IR::PathExpression *> paths;

public:
static DeclarationToExpression *getInstance() {
if (!instance) instance = new DeclarationToExpression;
return instance;
}

const IR::PathExpression *getExpression(const IR::Declaration *decl, ReferenceMap *refMap,
TypeMap *typeMap) {
CHECK_NULL(decl);
CHECK_NULL(refMap);
CHECK_NULL(typeMap);

auto expr = ::P4::get(paths, decl);
if (!expr) {
expr = new IR::PathExpression(decl->name);
}
if (!refMap->getDeclaration(expr->path, false)) {
refMap->setDeclaration(expr->path, decl);
typeMap->setType(expr, typeMap->getType(decl, true));
paths[decl] = expr;
}
return expr;
}
};

DeclarationToExpression *DeclarationToExpression::instance = nullptr;

class HeaderDefinitions : public IHasDbPrint {
ReferenceMap *refMap;
TypeMap *typeMap;
Expand Down Expand Up @@ -369,6 +335,8 @@ class FindUninitialized : public Inspector {
/// For some simple expresssions keep here the read location sets.
/// This does not include location sets read by subexpressions.
absl::flat_hash_map<const IR::Expression *, const LocationSet *, Util::Hash> readLocations;
/// Stores the temporary expressions so they can be reused
absl::flat_hash_map<const IR::Declaration *, const IR::PathExpression *, Util::Hash> paths;
HasUses &hasUses; // output
/// If true the current statement is unreachable
bool unreachable = false;
Expand Down Expand Up @@ -400,6 +368,20 @@ class FindUninitialized : public Inspector {
return Inspector::init_apply(root);
}

const IR::PathExpression *getExpression(const IR::Declaration *decl) {
CHECK_NULL(decl);

auto expr = ::P4::get(paths, decl);
if (!expr) expr = new IR::PathExpression(decl->name);

if (!refMap->getDeclaration(expr->path, false)) {
refMap->setDeclaration(expr->path, decl);
typeMap->setType(expr, typeMap->getType(decl, true));
paths[decl] = expr;
}
return expr;
}

FindUninitialized(FindUninitialized *parent, ProgramPoint context)
: context(context),
refMap(parent->refMap),
Expand Down Expand Up @@ -1265,8 +1247,7 @@ class FindUninitialized : public Inspector {
TernaryBool::No);
} else {
// we can treat the argument passing as an assignment
auto param_expr = DeclarationToExpression::getInstance()->getExpression(
param, refMap, typeMap);
auto param_expr = getExpression(param);
processHeadersInAssignment(param_expr, expr->expression,
typeMap->getType(param_expr, true),
typeMap->getType(expr->expression, true));
Expand Down Expand Up @@ -1334,8 +1315,7 @@ class FindUninitialized : public Inspector {

if (auto actionCall = mi->to<ActionCall>()) {
if (auto param = actionCall->action->parameters->getParameter(p->name)) {
auto param_expr = DeclarationToExpression::getInstance()->getExpression(
param, refMap, typeMap);
auto param_expr = getExpression(param);
processHeadersInAssignment(expr->expression, param_expr,
typeMap->getType(expr->expression, true),
typeMap->getType(param_expr, true));
Expand Down
Loading