Skip to content

[mlir][sparse] comment cleanup in iteration graph sorter #75508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@ class AffineDimFinder : public AffineExprVisitor<AffineDimFinder> {
explicit AffineDimFinder(ArrayRef<utils::IteratorType> itTypes)
: iterTypes(itTypes) {}

// Override method from AffineExprVisitor.
/// Overrides the visit method from AffineExprVisitor.
void visitDimExpr(AffineDimExpr expr) {
if (pickedDim == nullptr || pickIterType == iterTypes[expr.getPosition()])
pickedDim = expr;
}

/// Set the desired iterator type that we want to pick.
/// Sets the desired iterator type that we want to pick.
void setPickedIterType(utils::IteratorType iterType) {
pickIterType = iterType;
}

/// Get the desired AffineDimExpr.
/// Gets the desired AffineDimExpr.
AffineDimExpr getDimExpr() const {
return llvm::cast<AffineDimExpr>(pickedDim);
}

/// Walks the graph in post order to find dim expr.
void walkPostOrder(AffineExpr expr) {
pickedDim = nullptr;
AffineExprVisitor<AffineDimFinder>::walkPostOrder(expr);
Expand All @@ -55,11 +56,11 @@ class AffineDimFinder : public AffineExprVisitor<AffineDimFinder> {
AffineExpr pickedDim;
/// The iterator type that we want.
utils::IteratorType pickIterType;
/// The mapping between dim=>iterator type.
/// The mapping between levels and iterator types.
ArrayRef<utils::IteratorType> iterTypes;
};

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

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

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

Expand Down Expand Up @@ -164,13 +166,14 @@ IterationGraphSorter::IterationGraphSorter(
}

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

// Reset cached in-degree.
// Reset in-degree.
std::fill(inDegree.begin(), inDegree.end(), 0);

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

// Get map and encoding.
// Add the constraints for the output map.
const auto enc = getSparseTensorEncoding(out.getType());
if ((enc || includesDenseOutput(mask)) && out != ignored)
addConstraints(out, loop2OutLvl);

// Return the topological sort (empty for cyclic).
return topoSort();
}

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

// Set up a reduction finder.
AffineDimFinder finder(iterTypes);
finder.setPickedIterType(utils::IteratorType::reduction);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class IterationGraphSorter {
// Loop itation types;
SmallVector<utils::IteratorType> iterTypes;

// Adjacent matrix that represents the iteration graph.
// Adjacency matrix that represents the iteration graph.
std::vector<std::vector<bool>> itGraph;

// InDegree used for topo sort.
Expand Down