Skip to content

Commit 1c5e0eb

Browse files
committed
migrate block topo sort
1 parent de8f97b commit 1c5e0eb

File tree

9 files changed

+31
-45
lines changed

9 files changed

+31
-45
lines changed

mlir/include/mlir/Analysis/TopologicalSortUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ bool computeTopologicalSorting(
104104
MutableArrayRef<Operation *> ops,
105105
function_ref<bool(Value, Operation *)> isOperandReady = nullptr);
106106

107+
/// Get a list of blocks that is sorted according to dominance. This sort is
108+
/// stable.
109+
SetVector<Block *> getBlocksSortedByDominance(Region &region);
110+
107111
} // end namespace mlir
108112

109113
#endif // MLIR_ANALYSIS_TOPOLOGICALSORTUTILS_H

mlir/include/mlir/Transforms/RegionUtils.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ LogicalResult eraseUnreachableBlocks(RewriterBase &rewriter,
8787
LogicalResult runRegionDCE(RewriterBase &rewriter,
8888
MutableArrayRef<Region> regions);
8989

90-
/// Get a list of blocks that is sorted according to dominance. This sort is
91-
/// stable.
92-
SetVector<Block *> getBlocksSortedByDominance(Region &region);
93-
9490
} // namespace mlir
9591

9692
#endif // MLIR_TRANSFORMS_REGIONUTILS_H_

mlir/lib/Analysis/SliceAnalysis.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "mlir/Analysis/SliceAnalysis.h"
14+
#include "mlir/Analysis/TopologicalSortUtils.h"
1415
#include "mlir/IR/Block.h"
15-
#include "mlir/IR/BuiltinOps.h"
1616
#include "mlir/IR/Operation.h"
17-
#include "mlir/IR/RegionGraphTraits.h"
1817
#include "mlir/Interfaces/SideEffectInterfaces.h"
1918
#include "mlir/Support/LLVM.h"
20-
#include "llvm/ADT/PostOrderIterator.h"
2119
#include "llvm/ADT/SetVector.h"
2220
#include "llvm/ADT/SmallPtrSet.h"
2321

@@ -167,23 +165,6 @@ mlir::getSlice(Operation *op, const BackwardSliceOptions &backwardSliceOptions,
167165
return topologicalSort(slice);
168166
}
169167

170-
/// TODO: deduplicate
171-
static SetVector<Block *> getTopologicallySortedBlocks(Region &region) {
172-
// For each block that has not been visited yet (i.e. that has no
173-
// predecessors), add it to the list as well as its successors.
174-
SetVector<Block *> blocks;
175-
for (Block &b : region) {
176-
if (blocks.count(&b) == 0) {
177-
llvm::ReversePostOrderTraversal<Block *> traversal(&b);
178-
blocks.insert(traversal.begin(), traversal.end());
179-
}
180-
}
181-
assert(blocks.size() == region.getBlocks().size() &&
182-
"some blocks are not sorted");
183-
184-
return blocks;
185-
}
186-
187168
/// Computes the common ancestor region of all operations in `ops`. Remembers
188169
/// all the traversed regions in `traversedRegions`.
189170
static Region *findCommonParentRegion(const SetVector<Operation *> &ops,
@@ -220,7 +201,7 @@ static void topoSortRegion(Region &region,
220201
const DenseSet<Region *> &relevantRegions,
221202
const SetVector<Operation *> &toSort,
222203
SetVector<Operation *> &result) {
223-
SetVector<Block *> sortedBlocks = getTopologicallySortedBlocks(region);
204+
SetVector<Block *> sortedBlocks = getBlocksSortedByDominance(region);
224205
for (Block *block : sortedBlocks) {
225206
for (Operation &op : *block) {
226207
if (toSort.contains(&op))

mlir/lib/Analysis/TopologicalSortUtils.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "mlir/Analysis/TopologicalSortUtils.h"
10+
#include "mlir/IR/Block.h"
1011
#include "mlir/IR/OpDefinition.h"
12+
#include "mlir/IR/RegionGraphTraits.h"
13+
14+
#include "llvm/ADT/PostOrderIterator.h"
15+
#include "llvm/ADT/SetVector.h"
1116

1217
using namespace mlir;
1318

@@ -146,3 +151,19 @@ bool mlir::computeTopologicalSorting(
146151

147152
return allOpsScheduled;
148153
}
154+
155+
SetVector<Block *> mlir::getBlocksSortedByDominance(Region &region) {
156+
// For each block that has not been visited yet (i.e. that has no
157+
// predecessors), add it to the list as well as its successors.
158+
SetVector<Block *> blocks;
159+
for (Block &b : region) {
160+
if (blocks.count(&b) == 0) {
161+
llvm::ReversePostOrderTraversal<Block *> traversal(&b);
162+
blocks.insert(traversal.begin(), traversal.end());
163+
}
164+
}
165+
assert(blocks.size() == region.getBlocks().size() &&
166+
"some blocks are not sorted");
167+
168+
return blocks;
169+
}

mlir/lib/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "mlir/Target/LLVMIR/Dialect/OpenACC/OpenACCToLLVMIRTranslation.h"
15+
#include "mlir/Analysis/TopologicalSortUtils.h"
1516
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1617
#include "mlir/Dialect/OpenACC/OpenACC.h"
1718
#include "mlir/IR/BuiltinOps.h"
1819
#include "mlir/IR/Operation.h"
1920
#include "mlir/Support/LLVM.h"
2021
#include "mlir/Target/LLVMIR/Dialect/OpenMPCommon.h"
2122
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
22-
#include "mlir/Transforms/RegionUtils.h"
2323

2424
#include "llvm/ADT/TypeSwitch.h"
2525
#include "llvm/Frontend/OpenMP/OMPConstants.h"

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313
#include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
14+
#include "mlir/Analysis/TopologicalSortUtils.h"
1415
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1516
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
1617
#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "AttrKindDetail.h"
1717
#include "DebugTranslation.h"
1818
#include "LoopAnnotationTranslation.h"
19+
#include "mlir/Analysis/TopologicalSortUtils.h"
1920
#include "mlir/Dialect/DLTI/DLTI.h"
2021
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
2122
#include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
@@ -33,7 +34,6 @@
3334
#include "mlir/Support/LogicalResult.h"
3435
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
3536
#include "mlir/Target/LLVMIR/TypeToLLVM.h"
36-
#include "mlir/Transforms/RegionUtils.h"
3737

3838
#include "llvm/ADT/PostOrderIterator.h"
3939
#include "llvm/ADT/SetVector.h"

mlir/lib/Transforms/Mem2Reg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
#include "mlir/Transforms/Mem2Reg.h"
1010
#include "mlir/Analysis/DataLayoutAnalysis.h"
1111
#include "mlir/Analysis/SliceAnalysis.h"
12+
#include "mlir/Analysis/TopologicalSortUtils.h"
1213
#include "mlir/IR/Builders.h"
1314
#include "mlir/IR/Dominance.h"
1415
#include "mlir/IR/PatternMatch.h"
1516
#include "mlir/IR/Value.h"
1617
#include "mlir/Interfaces/ControlFlowInterfaces.h"
1718
#include "mlir/Interfaces/MemorySlotInterfaces.h"
1819
#include "mlir/Transforms/Passes.h"
19-
#include "mlir/Transforms/RegionUtils.h"
2020
#include "llvm/ADT/STLExtras.h"
2121
#include "llvm/Support/GenericIteratedDominanceFrontier.h"
2222

mlir/lib/Transforms/Utils/RegionUtils.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include "llvm/ADT/DepthFirstIterator.h"
2121
#include "llvm/ADT/PostOrderIterator.h"
22-
#include "llvm/ADT/SmallSet.h"
2322

2423
#include <deque>
2524

@@ -836,19 +835,3 @@ LogicalResult mlir::simplifyRegions(RewriterBase &rewriter,
836835
return success(eliminatedBlocks || eliminatedOpsOrArgs ||
837836
mergedIdenticalBlocks);
838837
}
839-
840-
SetVector<Block *> mlir::getBlocksSortedByDominance(Region &region) {
841-
// For each block that has not been visited yet (i.e. that has no
842-
// predecessors), add it to the list as well as its successors.
843-
SetVector<Block *> blocks;
844-
for (Block &b : region) {
845-
if (blocks.count(&b) == 0) {
846-
llvm::ReversePostOrderTraversal<Block *> traversal(&b);
847-
blocks.insert(traversal.begin(), traversal.end());
848-
}
849-
}
850-
assert(blocks.size() == region.getBlocks().size() &&
851-
"some blocks are not sorted");
852-
853-
return blocks;
854-
}

0 commit comments

Comments
 (0)