This is an archive of the discontinued LLVM Phabricator instance.

[mlir][sparse] Implement the rewrite for sparse_tensor.push_back a value n times.
ClosedPublic

Authored by bixia on Oct 24 2022, 5:53 PM.

Diff Detail

Event Timeline

bixia created this revision.Oct 24 2022, 5:53 PM
Herald added a project: Restricted Project. · View Herald Transcript
bixia requested review of this revision.Oct 24 2022, 5:53 PM
bixia updated this revision to Diff 470342.Oct 24 2022, 5:56 PM

Fix comment.

bixia updated this revision to Diff 470616.Oct 25 2022, 2:12 PM

Make n a runtime value.

bixia edited reviewers, added: Peiming; removed: PeimingLiu.Oct 26 2022, 3:47 PM
Peiming added inline comments.Oct 26 2022, 4:05 PM
mlir/lib/Dialect/SparseTensor/Transforms/SparseBufferRewriting.cpp
545

Isn't there any O(1) algorithm for finding the closest power of 2?

548

I think you can simply do ArrayRef<Type>(capacity.getType()) to avoid creating an extra SmallVector and copying value to it.

Peiming added inline comments.Oct 26 2022, 4:08 PM
mlir/lib/Dialect/SparseTensor/Transforms/SparseBufferRewriting.cpp
548

Actually you can simply do rewriter.create<scf::WhileOp>(loc, capactiy.getType(), capacity); as ArrayRef can be implicitly converted from a single element.

very neat, thanks for writing this so quickly!

mlir/lib/Dialect/SparseTensor/Transforms/SparseBufferRewriting.cpp
504

I think you need to keep the "buffer" in the "capacity(buffer)", as before

512

I actually like your original, which can now be

size(buffer) += n

530

this is of course very minor, but can we avoid creating the constant altogether for the default case and just set nIsOne directly (but of course, keep the check for when it is a runtime value that happens to be a constant 1)?

bixia updated this revision to Diff 471590.Oct 28 2022, 10:28 AM
bixia marked 4 inline comments as done.

Address review comments.

bixia added inline comments.Oct 28 2022, 10:29 AM
mlir/lib/Dialect/SparseTensor/Transforms/SparseBufferRewriting.cpp
530

Need to create the constant n for computing the newSize and updating size(buffer).
The current implementation handles push_back without n and push_back with explicit n==1 the same way.

545

the O(1) alg is like this. Per offline discussion, keep the current approach for simplicity.

If (a<b) {
  a = (a << (countLeadingZero(a) - countLeadingZero(b)))
  If (a < b)
    a = a*2
}

aartbik accepted this revision.Oct 28 2022, 12:12 PM
aartbik added inline comments.
mlir/lib/Dialect/SparseTensor/Transforms/SparseBufferRewriting.cpp
505

you are missing a

new_capacity = capacity(buffer)

before the while-loop (not that we actually run the comments ;-) but still...

544

Note that you already have the pseudo code above in the comments, so I would not repeat too much,
Just generate the while-loop to find new capacity would be enough, I think

This revision is now accepted and ready to land.Oct 28 2022, 12:12 PM
bixia updated this revision to Diff 471897.Oct 30 2022, 9:46 PM
bixia marked 2 inline comments as done.

Rebase.