Skip to content

Commit e919df5

Browse files
authored
[MLIR][Transforms] Correct block sorting utils name (NFC) (#92558)
This commit renames the name of the block sorting utility function to `getBlocksSortedByDominance`. A topological order is not defined on a general directed graph, so the previous name did not make sense.
1 parent 84aee95 commit e919df5

File tree

6 files changed

+33
-34
lines changed

6 files changed

+33
-34
lines changed

mlir/include/mlir/Transforms/RegionUtils.h

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

90-
/// Get a topologically sorted list of blocks of the given region.
91-
SetVector<Block *> getTopologicallySortedBlocks(Region &region);
90+
/// Get a list of blocks that is sorted according to dominance. This sort is
91+
/// stable.
92+
SetVector<Block *> getBlocksSortedByDominance(Region &region);
9293

9394
} // namespace mlir
9495

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ static LogicalResult convertDataOp(acc::DataOp &op,
392392
llvm::BasicBlock *endDataBlock = llvm::BasicBlock::Create(
393393
ctx, "acc.end_data", builder.GetInsertBlock()->getParent());
394394

395-
SetVector<Block *> blocks = getTopologicallySortedBlocks(op.getRegion());
395+
SetVector<Block *> blocks = getBlocksSortedByDominance(op.getRegion());
396396
for (Block *bb : blocks) {
397397
llvm::BasicBlock *llvmBB = moduleTranslation.lookupBlock(bb);
398398
if (bb->isEntryBlock()) {

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

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ static llvm::BasicBlock *convertOmpOpRegions(
199199

200200
// Convert blocks one by one in topological order to ensure
201201
// defs are converted before uses.
202-
SetVector<Block *> blocks = getTopologicallySortedBlocks(region);
202+
SetVector<Block *> blocks = getBlocksSortedByDominance(region);
203203
for (Block *bb : blocks) {
204204
llvm::BasicBlock *llvmBB = moduleTranslation.lookupBlock(bb);
205205
// Retarget the branch of the entry block to the entry block of the
@@ -2153,40 +2153,38 @@ getFirstOrLastMappedMemberPtr(mlir::omp::MapInfoOp mapInfo, bool first) {
21532153
llvm::SmallVector<size_t> indices(shape[0]);
21542154
std::iota(indices.begin(), indices.end(), 0);
21552155

2156-
llvm::sort(
2157-
indices.begin(), indices.end(), [&](const size_t a, const size_t b) {
2158-
auto indexValues = indexAttr.getValues<int32_t>();
2159-
for (int i = 0;
2160-
i < shape[1];
2161-
++i) {
2162-
int aIndex = indexValues[a * shape[1] + i];
2163-
int bIndex = indexValues[b * shape[1] + i];
2156+
llvm::sort(indices.begin(), indices.end(),
2157+
[&](const size_t a, const size_t b) {
2158+
auto indexValues = indexAttr.getValues<int32_t>();
2159+
for (int i = 0; i < shape[1]; ++i) {
2160+
int aIndex = indexValues[a * shape[1] + i];
2161+
int bIndex = indexValues[b * shape[1] + i];
21642162

2165-
if (aIndex == bIndex)
2166-
continue;
2163+
if (aIndex == bIndex)
2164+
continue;
21672165

2168-
if (aIndex != -1 && bIndex == -1)
2169-
return false;
2166+
if (aIndex != -1 && bIndex == -1)
2167+
return false;
21702168

2171-
if (aIndex == -1 && bIndex != -1)
2172-
return true;
2169+
if (aIndex == -1 && bIndex != -1)
2170+
return true;
21732171

2174-
// A is earlier in the record type layout than B
2175-
if (aIndex < bIndex)
2176-
return first;
2172+
// A is earlier in the record type layout than B
2173+
if (aIndex < bIndex)
2174+
return first;
21772175

2178-
if (bIndex < aIndex)
2179-
return !first;
2180-
}
2176+
if (bIndex < aIndex)
2177+
return !first;
2178+
}
21812179

2182-
// Iterated the entire list and couldn't make a decision, all elements
2183-
// were likely the same. Return false, since the sort comparator should
2184-
// return false for equal elements.
2185-
return false;
2186-
});
2180+
// Iterated the entire list and couldn't make a decision, all
2181+
// elements were likely the same. Return false, since the sort
2182+
// comparator should return false for equal elements.
2183+
return false;
2184+
});
21872185

2188-
return llvm::cast<mlir::omp::MapInfoOp>(
2189-
mapInfo.getMembers()[indices.front()].getDefiningOp());
2186+
return llvm::cast<mlir::omp::MapInfoOp>(
2187+
mapInfo.getMembers()[indices.front()].getDefiningOp());
21902188
}
21912189

21922190
/// This function calculates the array/pointer offset for map data provided

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
13311331

13321332
// Then, convert blocks one by one in topological order to ensure defs are
13331333
// converted before uses.
1334-
auto blocks = getTopologicallySortedBlocks(func.getBody());
1334+
auto blocks = getBlocksSortedByDominance(func.getBody());
13351335
for (Block *bb : blocks) {
13361336
CapturingIRBuilder builder(llvmContext);
13371337
if (failed(convertBlockImpl(*bb, bb->isEntryBlock(), builder,

mlir/lib/Transforms/Mem2Reg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ getOrCreateBlockIndices(BlockIndexCache &blockIndexCache, Region *region) {
517517
return it->second;
518518

519519
DenseMap<Block *, size_t> &blockIndices = it->second;
520-
SetVector<Block *> topologicalOrder = getTopologicallySortedBlocks(*region);
520+
SetVector<Block *> topologicalOrder = getBlocksSortedByDominance(*region);
521521
for (auto [index, block] : llvm::enumerate(topologicalOrder))
522522
blockIndices[block] = index;
523523
return blockIndices;

mlir/lib/Transforms/Utils/RegionUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ LogicalResult mlir::simplifyRegions(RewriterBase &rewriter,
837837
mergedIdenticalBlocks);
838838
}
839839

840-
SetVector<Block *> mlir::getTopologicallySortedBlocks(Region &region) {
840+
SetVector<Block *> mlir::getBlocksSortedByDominance(Region &region) {
841841
// For each block that has not been visited yet (i.e. that has no
842842
// predecessors), add it to the list as well as its successors.
843843
SetVector<Block *> blocks;

0 commit comments

Comments
 (0)