This is an archive of the discontinued LLVM Phabricator instance.

[pseudo] Add a fast-path to GLR reduce when both pop and push are trivial
ClosedPublic

Authored by sammccall on Jun 21 2022, 12:47 PM.

Details

Summary

In general we split a reduce into pop/push, so concurrently-available reductions
can run in the correct order. The data structures for this are expensive.

When only one reduction is possible at a time, we need not do this: we can pop
and immediately push instead.
Strictly this is correct whenever we yield one concurrent PushSpec.

This patch recognizes a trivial but common subset of these cases:

  • there must be no pending pushes and only one head available to pop
  • the head must have only one reduction rule
  • the reduction path must be a straight line (no multiple parents)

On my machine this speeds up by 2.12 -> 2.30 MB/s = 8%

Diff Detail

Event Timeline

sammccall created this revision.Jun 21 2022, 12:47 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 21 2022, 12:47 PM
sammccall requested review of this revision.Jun 21 2022, 12:47 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 21 2022, 12:47 PM
hokein accepted this revision.Jun 22 2022, 5:20 AM
hokein added inline comments.
clang-tools-extra/pseudo/lib/GLR.cpp
273

IMO, it is exactly what the reduction implementation would look like for a linear LR parsing :)

maybe encode the linear in the name?

274

The patch description is really nice document, and help to me to justify the code. I'd suggest adding them in the comment as well.

there must be no pending pushes and only one head available to pop
the head must have only one reduction rule
the reduction path must be a straight line (no multiple parents)

333–335

This seems very clever -- for trivial case, the main reduce loop is happening here.

337

we could save an extra call of Params.Table.getActions -- we call it at the beginning of the loop body and store the results in a local var, and use it in PopAndPushTrivial and here.

This revision is now accepted and ready to land.Jun 22 2022, 5:20 AM
hokein added inline comments.Jun 23 2022, 4:44 AM
clang-tools-extra/pseudo/lib/GLR.cpp
272

Thinking more about this, this trivial case seems to be triggered more often if we use a more powerful LR parsing algorithm -- a more powerful LR parser means less dead heads, and more linear cases.

sammccall marked 2 inline comments as done.Jun 23 2022, 9:29 AM
sammccall added inline comments.
clang-tools-extra/pseudo/lib/GLR.cpp
273

Thanks for making this connection, I hadn't completely realized we're just acting like an LR parser here and fundamentally that's why it's cheap.

I've amended the comment to call this out explicitly.

I'm not sure changing the *name* is better though, because the name has some jobs to do at the callsite:

  • convey that we're non just popping but pushing also (which is suprising)
  • suggest that this is handling some simple cases, but not the general case

I don't think "linear" actually conveys the second point. It partially describes *which* simple cases are handled.
So it would need to be tacked on like PopAndPushTrivialLinear and to me that's enough concepts that my brain has trouble digesting it.

This would be worth it if it were a critical part of the interface, but I don't think it is - it's rather important to the implementation instead.

274

Done. The new comment is based on the patch descriptions, with amendments to talk about the relationship to LR you pointed out.

333–335

Can't tell if "clever" is a good or bad thing :-)

Added a comment, it's definitely not obvious.

337

Yes, I benchmarked this and was surprised to see no difference at all!

I'd be tempted to do it anyway, but D128318 obsoletes this idea entirely, by making reduce lookup extremely cheap. At that point it's not worth messing up the signatures.

This revision was landed with ongoing or failed builds.Jun 23 2022, 9:29 AM
This revision was automatically updated to reflect the committed changes.
sammccall marked 2 inline comments as done.