Skip to content

Commit

Permalink
[mlir][sparse] Reducing computational complexity
Browse files Browse the repository at this point in the history
This is a followup to D128847.  The `AffineMap::getPermutedPosition` method performs a linear scan of the map, thus the previous implementation had asymptotic complexity of `O(|topSort| * |m|)`.  This change reduces that to `O(|topSort| + |m|)`.

Reviewed By: aartbik

Differential Revision: https://reviews.llvm.org/D129011
  • Loading branch information
wrengr committed Jul 1, 2022
1 parent b19cbda commit 875ee0e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion mlir/lib/Dialect/SparseTensor/Transforms/Sparsification.cpp
Expand Up @@ -115,9 +115,16 @@ struct CodeGen {
static AffineMap permute(MLIRContext *context, AffineMap m,
std::vector<unsigned> &topSort) {
unsigned sz = topSort.size();
assert(m.getNumResults() == sz && "TopoSort/AffineMap size mismatch");
// Construct the inverse of `m`; to avoid the asymptotic complexity
// of calling `m.getPermutedPosition` repeatedly.
SmallVector<unsigned, 4> inv(sz);
for (unsigned i = 0; i < sz; i++)
inv[i] = m.getDimPosition(i);
// Construct the permutation.
SmallVector<unsigned, 4> perm(sz);
for (unsigned i = 0; i < sz; i++)
perm[i] = m.getPermutedPosition(topSort[i]);
perm[i] = inv[topSort[i]];
return AffineMap::getPermutationMap(perm, context);
}

Expand Down

0 comments on commit 875ee0e

Please sign in to comment.