Files
clang-p2996/mlir/lib/Transforms/TopologicalSort.cpp
Christian Ulmann b00e0c1671 [MLIR][Analysis] Consolidate topological sort utilities (#92563)
This PR attempts to consolidate the different topological sort utilities
into one place. It adds them to the analysis folder because the
`SliceAnalysis` uses some of these.

There are now two different sorting strategies: 
1. Sort only according to SSA use-def chains
2. Sort while taking regions into account. This requires a much more
elaborate traversal and cannot be applied on graph regions that easily.

This additionally reimplements the region aware topological sorting
because the previous implementation had an exponential space complexity.

I'm open to suggestions on how to combine this further or how to fuse
the test passes.
2024-05-22 08:48:10 +02:00

41 lines
1.2 KiB
C++

//===- TopologicalSort.cpp - Topological sort pass ------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Transforms/Passes.h"
#include "mlir/Analysis/TopologicalSortUtils.h"
#include "mlir/IR/RegionKindInterface.h"
namespace mlir {
#define GEN_PASS_DEF_TOPOLOGICALSORT
#include "mlir/Transforms/Passes.h.inc"
} // namespace mlir
using namespace mlir;
namespace {
struct TopologicalSortPass
: public impl::TopologicalSortBase<TopologicalSortPass> {
void runOnOperation() override {
// Topologically sort the regions of the operation without SSA dominance.
getOperation()->walk([](RegionKindInterface op) {
for (auto it : llvm::enumerate(op->getRegions())) {
if (op.hasSSADominance(it.index()))
continue;
for (Block &block : it.value())
sortTopologically(&block);
}
});
}
};
} // end anonymous namespace
std::unique_ptr<Pass> mlir::createTopologicalSortPass() {
return std::make_unique<TopologicalSortPass>();
}