%0 = sparse_tensor.values %tensor : tensor<?x?xi64, #CSR64> to memref<?xi64>
%123 = sparse_tensor.values %tensor : tensor<?x?xi64, #CSR64> to memref<?xi64>
We should be able to optimize this code to have exactly one use of sparse_tensor.values. The only thing to watch out for is that there's no resizing that happens between. When there's resizing, then we need the second sparse_tensor.values or else we'll be using the wrong value.
This should not only work in straight-line code/basic blocks, but also in loops. If there's a use of sparse_tensor.values inside of a loop and %tensor is created outside of the loop and there is no resizing after the sparse_tensor.values use, we should be able to hoist out the sparse_tensor.values use.
This optimization idea similarly applies to sparse_tensor.pointers and sparse_tensor.indices. I think this optimization could be implemented for each of the 3 ops independently, e.g. the optimization for sparse_tensor.pointers should remove redundant sparse_tensor.pointers uses even if we resize the indices or values between each sparse_tensor.pointers use. We should sanity check that this is the case.
This should ideally be done in the --graphblas-optimize pass.
Suppose we have MLIR code like this:
We should be able to optimize this code to have exactly one use of
sparse_tensor.values
. The only thing to watch out for is that there's no resizing that happens between. When there's resizing, then we need the secondsparse_tensor.values
or else we'll be using the wrong value.This should not only work in straight-line code/basic blocks, but also in loops. If there's a use of
sparse_tensor.values
inside of a loop and%tensor
is created outside of the loop and there is no resizing after thesparse_tensor.values
use, we should be able to hoist out thesparse_tensor.values
use.This optimization idea similarly applies to
sparse_tensor.pointers
andsparse_tensor.indices
. I think this optimization could be implemented for each of the 3 ops independently, e.g. the optimization forsparse_tensor.pointers
should remove redundantsparse_tensor.pointers
uses even if we resize the indices or values between eachsparse_tensor.pointers
use. We should sanity check that this is the case.This should ideally be done in the
--graphblas-optimize
pass.