<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/59394>59394</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [mlir][SparseTensor] Sparsification pass crashes with subi in reduction loop
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          qcolombet
      </td>
    </tr>
</table>

<pre>
    I don't know if I'm somehow building the IR incorrectly, but I can crash the `sparsification` pass with a somewhat simple IR example (See attached [tmp.txt](https://github.com/llvm/llvm-project/files/10184899/tmp.txt) for the mlir file. Github doesn't like `.mlir` file extension.)

Essentially I'm doing: `res = sparse_input[i] - res` and the sparsification chokes on it.
If I do `res = res - sparse_input[i]` everything is fine (i.e., swap the argument of the `arith.subi` operation)
If I do `res = sparse_input[i] * res` everything is fine as well (i.e., replace `arith.subi` with `arith.muli`).

To reproduce:
```
mlir-opt -sparsification repro.mlir -o - 
```

Where repro.mlir is:
```
#SparseVector = #sparse_tensor.encoding<{dimLevelType = ["compressed"]}>

#trait = {
  indexing_maps = [ 
    affine_map<(i) -> (i)>,  // a (in)
 affine_map<(i) -> ()>    // x (out)
  ],  
  iterator_types = ["reduction"]
}

func.func @sparse_reduction_subi(%argx: tensor<i32>,
                             %arga: tensor<?xi32, #SparseVector>)
 -> tensor<i32> {
  %0 = linalg.generic #trait
 ins(%arga: tensor<?xi32, #SparseVector>)
      outs(%argx: tensor<i32>) {
      ^bb(%a: i32, %x: i32):
        %t = arith.subi %a, %x: i32 
 linalg.yield %t : i32 
  } -> tensor<i32>
  return %0 : tensor<i32>
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVV1zszYT_TXrmx0zWEBsLriI4_idzLxXfTLtZUbAAmqERCUR2_--IwGOk_ppp2U8YCTtxzlnl-XWilYRFZDtITus-Og6bYo_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