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

Generalization & minor refactoring in RenameMap #4677

Merged
merged 1 commit into from
May 27, 2024

Conversation

vlstill
Copy link
Contributor

@vlstill vlstill commented May 24, 2024

This is a slight generalization and refactor of P4::RenameMap. The existing interface stays the same, but there are two more ways to use it:

  • setNewName can now accept an optional bool argument. If set to true, the bug check for the case an an existing renaming is going to be replaced is disabled.
  • get member function that returns std::optional<cstring> was added. If the name has no mapping, this will return std::nullopt, while in getName there was a failure. This allows the user of the map to skip double searching the map.

@vlstill vlstill added the core Topics concerning the core segments of the compiler (frontend, midend, parser) label May 24, 2024
@vlstill vlstill self-assigned this May 24, 2024
@vlstill vlstill marked this pull request as ready for review May 24, 2024 14:11
@vlstill vlstill requested review from ChrisDodd and fruffy May 24, 2024 15:10
auto name = new IR::ID(orig->getName().srcInfo, newName, orig->getName().originalName);
auto newName = renameMap->get(orig);
if (!newName.has_value()) return nullptr;
auto name = new IR::ID(orig->getName().srcInfo, *newName, orig->getName().originalName);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tangential comment but what if originalName is nullptr? Then the name is lost.

Maybe this should call toString?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tangential comment but what if originalName is nullptr? Then the name is lost.

How would it be lost? It would just make an ID where the name is set and originalName is nullptr, which according to the IR::ID doc is perfectly fine and intended to be used for compiler-generated names (another one of the things I only discover because of checking something else :-/).

Copy link
Collaborator

@fruffy fruffy May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the IDeclaration has an IR::ID which only has a name, but not yet an original name (because it is the original). Now we create a new IR::ID and pass the newName along but not original name. originalName is never set and the initial name of the declaration is lost.

Although now that you point it out I am not sure whether that is intended or not...

Iirc I definitely have run into a variant of this problem before but never pursued fixing it. #4444

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two-parameter (source loc + name) and single param (just name) constructor of the IR::ID sets both name and originalName (https://github.com/p4lang/p4c/blob/main/ir/id.h#L37). So the only way to create IR::ID without original name should be explicitly setting it to nullptr (either in constructor, or afterward). So if there was no renaming and the ID originates from parsed code, it will have both names equal. After renaming it here the originalName is preserved.

It is possible there is a place where originalName is not preserved when creating IDs, but I'd say it is elsewhere (maybe in inlining from what you wrote).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

originalName should always be set for IDs that correspond to things in the source code -- originalName == nullptr implies that this is a purely compiler generated symbol with no "original" name. For these purely synthetic names, preserving whatever was first generated for them is probably not useful?

auto name = new IR::ID(orig->getName().srcInfo, newName, orig->getName().originalName);
auto newName = renameMap->get(orig);
if (!newName.has_value()) return nullptr;
auto name = new IR::ID(orig->getName().srcInfo, *newName, orig->getName().originalName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

originalName should always be set for IDs that correspond to things in the source code -- originalName == nullptr implies that this is a purely compiler generated symbol with no "original" name. For these purely synthetic names, preserving whatever was first generated for them is probably not useful?

@vlstill vlstill added this pull request to the merge queue May 27, 2024
Merged via the queue into main with commit ce5cb8c May 27, 2024
17 checks passed
@vlstill vlstill deleted the vstill/rename-map-refactor branch May 27, 2024 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Topics concerning the core segments of the compiler (frontend, midend, parser)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants