Skip to content

Commit ad18833

Browse files
author
git apple-llvm automerger
committed
Merge commit '59fe39b2fb04' from apple/main into swift/next
2 parents b40a525 + 59fe39b commit ad18833

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
1919
#include "mlir/IR/Attributes.h"
2020
#include "mlir/IR/Module.h"
21+
#include "mlir/IR/RegionGraphTraits.h"
2122
#include "mlir/IR/StandardTypes.h"
2223
#include "mlir/Support/LLVM.h"
2324
#include "mlir/Target/LLVMIR/TypeTranslation.h"
2425
#include "llvm/ADT/TypeSwitch.h"
2526

27+
#include "llvm/ADT/PostOrderIterator.h"
2628
#include "llvm/ADT/SetVector.h"
2729
#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
2830
#include "llvm/IR/BasicBlock.h"
@@ -360,25 +362,17 @@ connectPHINodes(T &func, const DenseMap<Value, llvm::Value *> &valueMapping,
360362
}
361363
}
362364

363-
// TODO: implement an iterative version
364-
static void topologicalSortImpl(llvm::SetVector<Block *> &blocks, Block *b) {
365-
blocks.insert(b);
366-
for (Block *bb : b->getSuccessors()) {
367-
if (blocks.count(bb) == 0)
368-
topologicalSortImpl(blocks, bb);
369-
}
370-
}
371-
372365
/// Sort function blocks topologically.
373366
template <typename T>
374367
static llvm::SetVector<Block *> topologicalSort(T &f) {
375-
// For each blocks that has not been visited yet (i.e. that has no
376-
// predecessors), add it to the list and traverse its successors in DFS
377-
// preorder.
368+
// For each block that has not been visited yet (i.e. that has no
369+
// predecessors), add it to the list as well as its successors.
378370
llvm::SetVector<Block *> blocks;
379371
for (Block &b : f) {
380-
if (blocks.count(&b) == 0)
381-
topologicalSortImpl(blocks, &b);
372+
if (blocks.count(&b) == 0) {
373+
llvm::ReversePostOrderTraversal<Block *> traversal(&b);
374+
blocks.insert(traversal.begin(), traversal.end());
375+
}
382376
}
383377
assert(blocks.size() == f.getBlocks().size() && "some blocks are not sorted");
384378

0 commit comments

Comments
 (0)