|
18 | 18 | #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
|
19 | 19 | #include "mlir/IR/Attributes.h"
|
20 | 20 | #include "mlir/IR/Module.h"
|
| 21 | +#include "mlir/IR/RegionGraphTraits.h" |
21 | 22 | #include "mlir/IR/StandardTypes.h"
|
22 | 23 | #include "mlir/Support/LLVM.h"
|
23 | 24 | #include "mlir/Target/LLVMIR/TypeTranslation.h"
|
24 | 25 | #include "llvm/ADT/TypeSwitch.h"
|
25 | 26 |
|
| 27 | +#include "llvm/ADT/PostOrderIterator.h" |
26 | 28 | #include "llvm/ADT/SetVector.h"
|
27 | 29 | #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
|
28 | 30 | #include "llvm/IR/BasicBlock.h"
|
@@ -360,25 +362,17 @@ connectPHINodes(T &func, const DenseMap<Value, llvm::Value *> &valueMapping,
|
360 | 362 | }
|
361 | 363 | }
|
362 | 364 |
|
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 |
| - |
372 | 365 | /// Sort function blocks topologically.
|
373 | 366 | template <typename T>
|
374 | 367 | 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. |
378 | 370 | llvm::SetVector<Block *> blocks;
|
379 | 371 | 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 | + } |
382 | 376 | }
|
383 | 377 | assert(blocks.size() == f.getBlocks().size() && "some blocks are not sorted");
|
384 | 378 |
|
|
0 commit comments