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

Facilitate inheritance from RenameSymbols pass, deduplicate code #4682

Merged
merged 3 commits into from
May 27, 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
71 changes: 19 additions & 52 deletions frontends/p4/uniqueNames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ class FindActionCalls : public Inspector {

} // namespace

// Add a @name annotation ONLY if it does not already exist.
// Otherwise do nothing.
static const IR::Annotations *addNameAnnotation(cstring name, const IR::Annotations *annos) {
if (annos == nullptr) annos = IR::Annotations::empty;
return annos->addAnnotationIfNew(IR::Annotation::nameAnnotation, new IR::StringLiteral(name),
false);
}

UniqueNames::UniqueNames(ReferenceMap *refMap) : renameMap(new RenameMap) {
setName("UniqueNames");
visitDagOnce = false;
Expand All @@ -96,21 +88,25 @@ UniqueParameters::UniqueParameters(ReferenceMap *refMap, TypeMap *typeMap)
/**************************************************************************/

IR::ID *RenameSymbols::getName() const {
auto orig = getOriginal<IR::IDeclaration>();
auto newName = renameMap->get(orig);
return getName(getOriginal<IR::IDeclaration>());
}

IR::ID *RenameSymbols::getName(const IR::IDeclaration *decl) const {
auto newName = renameMap->get(decl);
if (!newName.has_value()) return nullptr;
auto name = new IR::ID(orig->getName().srcInfo, *newName, orig->getName().originalName);
auto name = new IR::ID(decl->getName().srcInfo, *newName, decl->getName().originalName);
return name;
}

const IR::Annotations *RenameSymbols::addNameAnnotation(cstring name,
const IR::Annotations *annos) {
if (annos == nullptr) annos = IR::Annotations::empty;
return annos->addAnnotationIfNew(IR::Annotation::nameAnnotation, new IR::StringLiteral(name),
false);
}

const IR::Node *RenameSymbols::postorder(IR::Declaration_Variable *decl) {
auto name = getName();
if (name != nullptr && *name != decl->name) {
auto annos = addNameAnnotation(decl->name, decl->annotations);
decl->name = *name;
decl->annotations = annos;
}
return decl;
return renameDeclWithNameAnno(decl);
}

const IR::Node *RenameSymbols::postorder(IR::Declaration_Constant *decl) {
Expand All @@ -120,12 +116,7 @@ const IR::Node *RenameSymbols::postorder(IR::Declaration_Constant *decl) {
}

const IR::Node *RenameSymbols::postorder(IR::Parameter *param) {
auto name = getName();
if (name != nullptr && *name != param->name.name) {
param->annotations = addNameAnnotation(param->name, param->annotations);
param->name = IR::ID(param->name.srcInfo, *name, param->name.originalName);
}
return param;
return renameDeclWithNameAnno(param);
}

const IR::Node *RenameSymbols::postorder(IR::PathExpression *expression) {
Expand All @@ -142,43 +133,19 @@ const IR::Node *RenameSymbols::postorder(IR::PathExpression *expression) {
}

const IR::Node *RenameSymbols::postorder(IR::Declaration_Instance *decl) {
auto name = getName();
if (name != nullptr && *name != decl->name) {
auto annos = addNameAnnotation(decl->name, decl->annotations);
decl->name = *name;
decl->annotations = annos;
}
return decl;
return renameDeclWithNameAnno(decl);
}

const IR::Node *RenameSymbols::postorder(IR::P4Table *decl) {
auto name = getName();
if (name != nullptr && *name != decl->name) {
auto annos = addNameAnnotation(decl->name, decl->annotations);
decl->name = *name;
decl->annotations = annos;
}
return decl;
return renameDeclWithNameAnno(decl);
}

const IR::Node *RenameSymbols::postorder(IR::P4Action *decl) {
auto name = getName();
if (name != nullptr && *name != decl->name) {
auto annos = addNameAnnotation(decl->name, decl->annotations);
decl->name = *name;
decl->annotations = annos;
}
return decl;
return renameDeclWithNameAnno(decl);
}

const IR::Node *RenameSymbols::postorder(IR::P4ValueSet *decl) {
auto name = getName();
if (name != nullptr && *name != decl->name) {
auto annos = addNameAnnotation(decl->name, decl->annotations);
decl->name = *name;
decl->annotations = annos;
}
return decl;
return renameDeclWithNameAnno(decl);
}

const IR::Node *RenameSymbols::postorder(IR::Argument *arg) {
Expand Down
21 changes: 21 additions & 0 deletions frontends/p4/uniqueNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,31 @@ class FindSymbols : public Inspector {
};

class RenameSymbols : public Transform {
protected:
ReferenceMap *refMap;
RenameMap *renameMap;

/// Get new name of the current declaration or nullptr if the declaration is not to be renamed.
IR::ID *getName() const;
/// Get new name of the given declaration or nullptr if the declaration is not to be renamed.
/// @param decl Declaration *in the original/non-transformed* P4 IR.
IR::ID *getName(const IR::IDeclaration *decl) const;

/// Add a @name annotation ONLY if it does not already exist.
/// Otherwise do nothing.
static const IR::Annotations *addNameAnnotation(cstring name, const IR::Annotations *annos);

/// Rename any declaration where we want to add @name annotation with the original name.
/// Has to be a template as there is no common base for declarations with annotations member.
template<typename D>
const IR::Node *renameDeclWithNameAnno(D *decl) {
vlstill marked this conversation as resolved.
Show resolved Hide resolved
auto name = getName();
if (name != nullptr && *name != decl->name) {
decl->annotations = addNameAnnotation(decl->name, decl->annotations);
decl->name = *name;
}
return decl;
}

public:
RenameSymbols(ReferenceMap *refMap, RenameMap *renameMap)
Expand Down
Loading