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

Remove ReferenceMap from top-level frontend passes #4947

Merged
merged 9 commits into from
Oct 10, 2024
Merged

Conversation

asl
Copy link
Contributor

@asl asl commented Oct 7, 2024

Finally, this removes ReferenceMap from top-level frontend passes:

  • RemoveAllUnusedDeclarations uses lightweight implementation of used set that was factored out from ResolveReferences
  • Added shortcut CheckShadow to execute ResolveReferences with shadow check enabled (and with "local" refmap)
  • Made refmap and internal implementation details of Evaluator, Inliner and SimplifyDefUse

@asl asl requested review from vlstill, ChrisDodd and fruffy October 7, 2024 16:20
RemoveRedundantParsers(ReferenceMap *refMap, TypeMap *typeMap, const RemoveUnusedPolicy &policy)
: PassManager{new TypeChecking(refMap, typeMap, true),
RemoveRedundantParsers(TypeMap *typeMap, const RemoveUnusedPolicy &policy)
: PassManager{new TypeChecking(nullptr, typeMap, true),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I missed this in the other PRs but passing in nullptr doesn't seem right to me. Any way to make the constructor safe and just use two parameters? I also ran into this with ConstantFolding, which now has an ambiguous constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this logic is pretty confusing, as depending whether refmap is passed or not the TypeChecking also runs ResolveReferences. And lots of passes rely on this.

But I think now we can add typemap-only constructor. And ensure that refmap is non-null on all other paths.

Copy link
Contributor

@vlstill vlstill left a comment

Choose a reason for hiding this comment

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

Thanks for all these changes!

I'm not sure about the changes to evaluator in frontend and inlining. From the comment in inlining, it seems like the pass itself does not need to re-run evaluator, that it is just to update its state for future use. But then the evaluator is not used again if it is not passed into Inlining. Also in the frontend, the evaluator "result" is not used.

As Evaluator is an inspector, presumably running it does not change anything, do we even need it in Frontend? Or is it because of errors it can produce? There seems to be just one...

::P4::error(ErrorType::ERR_INVALID, "%1%: Cannot evaluate to a compile-time constant",
arg->expression);

@asl
Copy link
Contributor Author

asl commented Oct 9, 2024

As Evaluator is an inspector, presumably running it does not change anything, do we even need it in Frontend? Or is it because of errors it can produce? There seems to be just one...

Yeah, I was confused as well. And applied the common logic: if something looks incomplete, then probably there is a downstream code that uses / changes this. So, I tried to keep the existing logic as much as possible.

The Evaluator itself is a bit special:

  • It does its own traversal
  • While it is an Inspector, it creates new nodes. Though, it does not attach them anywhere, they are used to build a hierarchy of CompileTimeValue / Block nodes similar to CFG.

The inliner (DiscoverInlining specifically) does depend on the evaluator as it seems to traverse Block's from the top level block, not the whole program. Given that Inliner runs in PassRepeated we'd need to update that CFG re-running evaluator.

I think we'd can just remove it from Frontend, yes.

@asl
Copy link
Contributor Author

asl commented Oct 9, 2024

I think I addressed the majority of review comments.

@fruffy I'm going to do TypeChecking changes in a separate PR.

@vlstill
Copy link
Contributor

vlstill commented Oct 9, 2024

The inliner (DiscoverInlining specifically) does depend on the evaluator as it seems to traverse Block's from the top level block, not the whole program. Given that Inliner runs in PassRepeated we'd need to update that CFG re-running evaluator.

Oh, I missed that inliner runs as PassRepeated, sorry.

@asl asl force-pushed the unused-def branch 2 times, most recently from 8f3dce3 to 6df3cde Compare October 9, 2024 19:12
@asl
Copy link
Contributor Author

asl commented Oct 9, 2024

Looks like both bazel and fedora are unhealthy. @grg @fruffy do you know what's going on?

@fruffy
Copy link
Collaborator

fruffy commented Oct 9, 2024

Looks like both bazel and fedora are unhealthy. @grg @fruffy do you know what's going on?

It's possible that one of the new includes is breaking the Bazel build related to alloc_trace. For Fedora, unclear how that error happens. Hope it is spurious.

@fruffy
Copy link
Collaborator

fruffy commented Oct 9, 2024

@smolkaj Any idea whether the Bazel failures are spurious or require a change?

@asl
Copy link
Contributor Author

asl commented Oct 10, 2024

@fruffy @smolkaj Bazel seems to be recovered. Only Fedora left...

@asl asl added this pull request to the merge queue Oct 10, 2024
Merged via the queue into p4lang:main with commit d2006d5 Oct 10, 2024
17 of 18 checks passed
@asl asl deleted the unused-def branch October 10, 2024 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants