Skip to content

Commit 15c06bc

Browse files
authored
[mlir][sparse] comment cleanup in iteration graph sorter (#75508)
1 parent 7bc6c4a commit 15c06bc

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

mlir/lib/Dialect/SparseTensor/Transforms/Utils/IterationGraphSorter.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,23 @@ class AffineDimFinder : public AffineExprVisitor<AffineDimFinder> {
2929
explicit AffineDimFinder(ArrayRef<utils::IteratorType> itTypes)
3030
: iterTypes(itTypes) {}
3131

32-
// Override method from AffineExprVisitor.
32+
/// Overrides the visit method from AffineExprVisitor.
3333
void visitDimExpr(AffineDimExpr expr) {
3434
if (pickedDim == nullptr || pickIterType == iterTypes[expr.getPosition()])
3535
pickedDim = expr;
3636
}
3737

38-
/// Set the desired iterator type that we want to pick.
38+
/// Sets the desired iterator type that we want to pick.
3939
void setPickedIterType(utils::IteratorType iterType) {
4040
pickIterType = iterType;
4141
}
4242

43-
/// Get the desired AffineDimExpr.
43+
/// Gets the desired AffineDimExpr.
4444
AffineDimExpr getDimExpr() const {
4545
return llvm::cast<AffineDimExpr>(pickedDim);
4646
}
4747

48+
/// Walks the graph in post order to find dim expr.
4849
void walkPostOrder(AffineExpr expr) {
4950
pickedDim = nullptr;
5051
AffineExprVisitor<AffineDimFinder>::walkPostOrder(expr);
@@ -55,11 +56,11 @@ class AffineDimFinder : public AffineExprVisitor<AffineDimFinder> {
5556
AffineExpr pickedDim;
5657
/// The iterator type that we want.
5758
utils::IteratorType pickIterType;
58-
/// The mapping between dim=>iterator type.
59+
/// The mapping between levels and iterator types.
5960
ArrayRef<utils::IteratorType> iterTypes;
6061
};
6162

62-
// Flattens an affine expression into a list of AffineDimExprs.
63+
/// Flattens an affine expression into a list of AffineDimExprs.
6364
struct AffineDimCollector : public AffineExprVisitor<AffineDimCollector> {
6465
// Overrides method from AffineExprVisitor.
6566
void visitDimExpr(AffineDimExpr expr) { dims.push_back(expr); }
@@ -97,8 +98,8 @@ AffineMap IterationGraphSorter::topoSort() {
9798

9899
SmallVector<unsigned> loopOrder;
99100
while (!redIt.empty() || !parIt.empty()) {
100-
// We always prefer parallel loop over reduction loop because putting
101-
// reduction loop early might make the loop sequence inadmissible.
101+
// We always prefer a parallel loop over a reduction loop because putting
102+
// a reduction loop early might make the loop sequence inadmissible.
102103
auto &it = !parIt.empty() ? parIt : redIt;
103104
auto src = it.back();
104105
loopOrder.push_back(src);
@@ -114,6 +115,7 @@ AffineMap IterationGraphSorter::topoSort() {
114115
}
115116
}
116117

118+
// Return the topological sort on success.
117119
if (loopOrder.size() == numLoops)
118120
return AffineMap::getPermutationMap(loopOrder, out.getContext());
119121

@@ -164,13 +166,14 @@ IterationGraphSorter::IterationGraphSorter(
164166
}
165167

166168
AffineMap IterationGraphSorter::sort(SortMask mask, Value ignored) {
167-
// Reset the interation graph.
169+
// Reset the adjacency matrix that represents the iteration graph.
168170
for (auto &row : itGraph)
169171
std::fill(row.begin(), row.end(), false);
170172

171-
// Reset cached in-degree.
173+
// Reset in-degree.
172174
std::fill(inDegree.begin(), inDegree.end(), 0);
173175

176+
// Add the constraints for the loop to level map.
174177
for (auto [in, map] : llvm::zip(ins, loop2InsLvl)) {
175178
// Get map and encoding.
176179
const auto enc = getSparseTensorEncoding(in.getType());
@@ -180,11 +183,12 @@ AffineMap IterationGraphSorter::sort(SortMask mask, Value ignored) {
180183
addConstraints(in, map);
181184
}
182185

183-
// Get map and encoding.
186+
// Add the constraints for the output map.
184187
const auto enc = getSparseTensorEncoding(out.getType());
185188
if ((enc || includesDenseOutput(mask)) && out != ignored)
186189
addConstraints(out, loop2OutLvl);
187190

191+
// Return the topological sort (empty for cyclic).
188192
return topoSort();
189193
}
190194

@@ -196,6 +200,7 @@ void IterationGraphSorter::addConstraints(Value t, AffineMap loop2LvlMap) {
196200
}
197201
};
198202

203+
// Set up a reduction finder.
199204
AffineDimFinder finder(iterTypes);
200205
finder.setPickedIterType(utils::IteratorType::reduction);
201206

mlir/lib/Dialect/SparseTensor/Transforms/Utils/IterationGraphSorter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class IterationGraphSorter {
7979
// Loop itation types;
8080
SmallVector<utils::IteratorType> iterTypes;
8181

82-
// Adjacent matrix that represents the iteration graph.
82+
// Adjacency matrix that represents the iteration graph.
8383
std::vector<std::vector<bool>> itGraph;
8484

8585
// InDegree used for topo sort.

0 commit comments

Comments
 (0)