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
Show file tree
Hide file tree
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
Next Next commit
Improve const-correctness
Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
  • Loading branch information
asl committed Sep 10, 2024
commit d01998e8b9dc9bc4709e5dc6c621a24783fa535d
9 changes: 4 additions & 5 deletions frontends/p4/def_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ void ComputeWriteSet::enterScope(const IR::ParameterList *parameters,

if (parameters != nullptr) {
for (auto p : parameters->parameters) {
StorageLocation *loc = allDefinitions->getOrAddStorage(p);
const StorageLocation *loc = allDefinitions->getOrAddStorage(p);
if (loc == nullptr) continue;
if (p->direction == IR::Direction::In || p->direction == IR::Direction::InOut ||
p->direction == IR::Direction::None)
Expand All @@ -410,8 +410,7 @@ void ComputeWriteSet::enterScope(const IR::ParameterList *parameters,
if (locals != nullptr) {
for (auto d : *locals) {
if (d->is<IR::Declaration_Variable>()) {
StorageLocation *loc = allDefinitions->getOrAddStorage(d);
if (loc != nullptr) {
if (const StorageLocation *loc = allDefinitions->getOrAddStorage(d)) {
defs->setDefinition(loc, uninit);
defs->setDefinition(loc->getValidBits(), startPoints);
defs->setDefinition(loc->getLastIndexField(), startPoints);
Expand All @@ -434,7 +433,7 @@ void ComputeWriteSet::exitScope(const IR::ParameterList *parameters,
currentDefinitions = currentDefinitions->cloneDefinitions();
if (parameters != nullptr) {
for (auto p : parameters->parameters) {
StorageLocation *loc = allDefinitions->getStorage(p);
const StorageLocation *loc = allDefinitions->getStorage(p);
if (loc != nullptr) {
LOG5("Removing location " << loc);
currentDefinitions->removeLocation(loc);
Expand All @@ -444,7 +443,7 @@ void ComputeWriteSet::exitScope(const IR::ParameterList *parameters,
if (locals != nullptr) {
for (auto d : *locals) {
if (d->is<IR::Declaration_Variable>()) {
StorageLocation *loc = allDefinitions->getStorage(d);
const StorageLocation *loc = allDefinitions->getStorage(d);
if (loc != nullptr) {
LOG5("Removing location " << loc);
currentDefinitions->removeLocation(loc);
Expand Down
14 changes: 7 additions & 7 deletions frontends/p4/def_use.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class LocationSet : public IHasDbPrint {
/// Maps a declaration to its associated storage.
class StorageMap : public IHasDbPrint {
/// Storage location for each declaration.
hvec_map<const IR::IDeclaration *, StorageLocation *> storage;
hvec_map<const IR::IDeclaration *, const StorageLocation *> storage;
StorageFactory factory;

public:
Expand All @@ -366,19 +366,19 @@ class StorageMap : public IHasDbPrint {
CHECK_NULL(refMap);
CHECK_NULL(typeMap);
}
StorageLocation *add(const IR::IDeclaration *decl) {
const StorageLocation *add(const IR::IDeclaration *decl) {
CHECK_NULL(decl);
auto type = typeMap->getType(decl->getNode(), true);
auto loc = factory.create(type, decl->getName() + "/" + decl->externalName());
if (loc != nullptr) storage.emplace(decl, loc);
return loc;
}
StorageLocation *getOrAdd(const IR::IDeclaration *decl) {
auto s = getStorage(decl);
const StorageLocation *getOrAdd(const IR::IDeclaration *decl) {
const auto *s = getStorage(decl);
if (s != nullptr) return s;
return add(decl);
}
StorageLocation *getStorage(const IR::IDeclaration *decl) const {
const StorageLocation *getStorage(const IR::IDeclaration *decl) const {
CHECK_NULL(decl);
auto result = ::P4::get(storage, decl);
return result;
Expand Down Expand Up @@ -580,11 +580,11 @@ class AllDefinitions : public IHasDbPrint {
atPoint[point] = defs;
}

StorageLocation *getStorage(const IR::IDeclaration *decl) const {
const StorageLocation *getStorage(const IR::IDeclaration *decl) const {
return storageMap.getStorage(decl);
}

StorageLocation *getOrAddStorage(const IR::IDeclaration *decl) {
const StorageLocation *getOrAddStorage(const IR::IDeclaration *decl) {
return storageMap.getOrAdd(decl);
}

Expand Down