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

Speed up TopoSort by 2.7-3.3x. #3946

Merged
merged 7 commits into from
Oct 17, 2023
Merged

Conversation

rmlarsen
Copy link
Contributor

@rmlarsen rmlarsen commented Sep 20, 2023

The main implementation in TopoSort::sort_worker is 11-12x faster. Overall, the complete sequence of building the graph and sorting is about 2.7-3.3x faster for the circuit I am working on. The overall impact in e.g. the replace_const_cells optimization pass is a ~35% speedup. End-to-end impact on our synthesis flow is about 3.3%.

Flame graph, zoomed on replace_const_cells, before:
image

Flame graph, zoomed on replace_const_cells, after:
image

Topdown view before:
image

Topdown view after:
image

…ort::sort_worker is 11-12x faster. Overall, the complete sequence of building the graph and sorting is about 2.5-3x faster. The overall impact in e.g. the replace_const_cells optimization pass is a ~25% speedup. End-to-end impact on our synthesis flow is about 3%.
@rmlarsen
Copy link
Contributor Author

rmlarsen commented Oct 2, 2023

What are your thoughts on this PR? I could back out the unrelated formatting changes in utils.h, if so desired.

@rmlarsen rmlarsen changed the title Speed up TopoSort. Speed up TopoSort by 2.5-3x. Oct 2, 2023
Copy link
Collaborator

@Ravenslofty Ravenslofty left a comment

Choose a reason for hiding this comment

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

seems good to me.

@rmlarsen
Copy link
Contributor Author

rmlarsen commented Oct 5, 2023

OK, I got rid of the needless formatting changes in kernel/utils.h. PTAL.

@rmlarsen
Copy link
Contributor Author

rmlarsen commented Oct 5, 2023

It looks like the test latches.ys in the xilinx directory is failing. Investigating.

… node, not an edge in glift and flatten.

Add back statement that inserts nodes in order in opt_expr.cc.
@rmlarsen
Copy link
Contributor Author

rmlarsen commented Oct 6, 2023

Fixed. BTW: I realized that this code is somewhat brittle, since the sort order in map and set for some uses of TopoSort depends on the sort order of pointers. This is generally a bad code smell, since it tends to make code non-deterministic.

@rmlarsen
Copy link
Contributor Author

rmlarsen commented Oct 6, 2023

OK, It finally passed. :-) PTAL.

@rmlarsen
Copy link
Contributor Author

rmlarsen commented Oct 6, 2023

Trimmed a little fat off my implementation of node(). This speeds the overall topological sorting up by another 10% in my case.

@rmlarsen rmlarsen changed the title Speed up TopoSort by 2.5-3x. Speed up TopoSort by 2.7-3.3x. Oct 6, 2023
@Ravenslofty
Copy link
Collaborator

In case you're wondering what's blocking this and #3975, I'd like somebody else to look over and approve it before I merge it, because I'm unfamiliar with the RTLIL internals; perhaps @mmicko?

@rmlarsen
Copy link
Contributor Author

rmlarsen commented Oct 6, 2023

In case you're wondering what's blocking this and #3975, I'd like somebody else to look over and approve it before I merge it, because I'm unfamiliar with the RTLIL internals; perhaps @mmicko?

Yeah, no worries. It's always good to have more eyes on it.

@Ravenslofty Ravenslofty added the discuss to be discussed at next dev jour fixe (see #devel-discuss at https://yosyshq.slack.com/) label Oct 8, 2023
@rmlarsen
Copy link
Contributor Author

@Ravenslofty any updates from today's meeting?

Copy link
Member

@povik povik left a comment

Choose a reason for hiding this comment

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

The code got less readable, unfortunately, but I suppose that's the price we ought to pay. Thanks @rmlarsen!

@Ravenslofty Ravenslofty merged commit d21c464 into YosysHQ:master Oct 17, 2023
15 checks passed
@povik
Copy link
Member

povik commented Oct 17, 2023

We could opt for a non-recursive algorithm (a code example I have at hand), but that might not be producing the sort order code users expect, especially if they supply the C template argument.

@rmlarsen
Copy link
Contributor Author

Thanks! Yeah, I've sometimes written it non-recursively, but in the profile it doesn't look like the recursion is a problem. One potential issue for large graphs is max allowed stack size, of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss to be discussed at next dev jour fixe (see #devel-discuss at https://yosyshq.slack.com/)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants