[SCCIterator] Fix an issue in scc_member_iterator sorting
authorHongtao Yu <hoy@fb.com>
Tue, 21 Mar 2023 17:57:11 +0000 (10:57 -0700)
committerHongtao Yu <hoy@fb.com>
Mon, 27 Mar 2023 17:48:05 +0000 (10:48 -0700)
commit43290c95631a54c9a02b914d9f87dbc5e7c1375b
treec2a372e100b09b25373df60053e2aced57ff628c
parent5e2445ae65f0a8f1ac38049bdd2f0fd5572be488
[SCCIterator] Fix an issue in scc_member_iterator sorting

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.

Reviewed By: wenlei

Differential Revision: https://reviews.llvm.org/D130717
llvm/include/llvm/ADT/SCCIterator.h