This is an archive of the discontinued LLVM Phabricator instance.

[SCCIterator] Fix an issue in scc_member_iterator sorting
ClosedPublic

Authored by hoy on Jul 28 2022, 10:49 AM.

Details

Summary

Members in an scc are supposed to be sorted in a top-down or topological order based on edge weights. Previously this is achived by building a MST out of the SCC and enforcing an BFS walk on the MST. A BFS on a tree does give a top-down topological order, however, the MST built here isn't really a tree. This is becuase of a trick done to avoid expansive detection of a cycle on a directed graph when an edge is added. When the MST is built, its edges are considered undirected. But in reality they are directed, thus a BST walk doesn't necessarily give a topological order. I'm tweaking the BFS walk slightly to yield a topological order.

Basically I'm using Kahn's algorithm on MST to compute a topological traversal order. The algorithm starts from nodes that have no incoming edge. These nodes are "roots" of the MST forest. This ensures that nodes are visited before their descendants are, thus ensures a topological traversal order of the MST.

Diff Detail

Event Timeline

hoy created this revision.Jul 28 2022, 10:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 28 2022, 10:49 AM
Herald added subscribers: modimo, wenlei. · View Herald Transcript
hoy requested review of this revision.Jul 28 2022, 10:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 28 2022, 10:49 AM
hoy updated this revision to Diff 507057.Mar 21 2023, 11:05 AM

Updating D130717: [SCCIterator] Fix an issue in scc_member_iterator sorting

I'm tweaking the BFS walk slightly to yield a topological order.

Would be good to spell out the tweak in the description.

llvm/include/llvm/ADT/SCCIterator.h
346–349

nit: this comment seems slightly misplaced? It describes the initialization of the queue on line 353. And slightly duplicated with the comment on line 354. Maybe move there and merge the comments.

370

Do we need to check the visited flag still? If not, we can remove the flag?

hoy edited the summary of this revision. (Show Details)Mar 27 2023, 9:24 AM
hoy added inline comments.
llvm/include/llvm/ADT/SCCIterator.h
346–349

The comment was supposed to explain a bit on the Kahn's algorithm. The next comment was mainly for getting a deterministic walk. I just tweaked them a little bit. Let me know if it looks good now.

370

Good point. It's used any more up since here but used when initializing the queue. I'm removing the setting of it in this loop.

hoy updated this revision to Diff 508697.Mar 27 2023, 9:25 AM

Addressing comments.

wenlei accepted this revision.Mar 27 2023, 10:25 AM

lgtm, thanks.

This revision is now accepted and ready to land.Mar 27 2023, 10:25 AM
This revision was landed with ongoing or failed builds.Mar 27 2023, 10:48 AM
This revision was automatically updated to reflect the committed changes.
nikic added a subscriber: nikic.Mar 29 2023, 12:01 AM

Shouldn't there be a test for this change?

hoy added a comment.Mar 30 2023, 9:00 AM

Shouldn't there be a test for this change?

Good point. I'll add a test.