metagraph-dev / mlir-graphblas

MLIR tools and dialect for GraphBLAS
https://mlir-graphblas.readthedocs.io/en/latest/
Apache License 2.0
15 stars 6 forks source link

Optimization pass to remove redundant sparse_tensor.pointers, sparse_tensor.indices, and sparse_tensor.values ops #250

Open paul-tqh-nguyen opened 2 years ago

paul-tqh-nguyen commented 2 years ago

Suppose we have MLIR code like this:

%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.