[MLIR][SparseTensor] `--sparsifier` fails to legalize when a `linalg.generic` body uses an operation it does not model
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
## Reproducer
### small.mlir
```mlir
#map = affine_map<(d0, d1) -> (d0, d1)>
#sparse = #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : compressed, d1 : compressed) }>
func.func @main() -> tensor<2x4xi32> {
%cst = arith.constant dense<[[4, 0, 9, 4], [0, -1, -9, 0]]> : tensor<2x4xi32>
%c0 = arith.constant 0 : i32
%s = sparse_tensor.convert %cst : tensor<2x4xi32> to tensor<2x4xi32, #sparse>
%e = tensor.empty() : tensor<2x4xi32>
%f = linalg.fill ins(%c0 : i32) outs(%e : tensor<2x4xi32>) -> tensor<2x4xi32>
%r = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]}
ins(%s : tensor<2x4xi32, #sparse>) outs(%f : tensor<2x4xi32>) {
^bb0(%in: i32, %out: i32):
%a = arith.minsi %in, %out : i32
linalg.yield %a : i32
} -> tensor<2x4xi32>
return %r : tensor<2x4xi32>
}
```
### To reproduce
```bash
mlir-opt small.mlir -o /dev/null # parses and verifies
```
```
mlir-opt small.mlir --sparsifier -o /dev/null
```
### Crash:
```
small.mlir:7:8: error: failed to legalize unresolved source materialization from ('!llvm.ptr') to ('tensor<2x4xi32, #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : compressed, d1 : compressed) }>>') that remained live after conversion (no matching callback)
%s = sparse_tensor.convert %cst : tensor<2x4xi32> to tensor<2x4xi32, #sparse>
^
small.mlir:7:8: note: see current operation: %36 = "builtin.unrealized_conversion_cast"(%35) : (!llvm.ptr) -> tensor<2x4xi32, #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : compressed, d1 : compressed) }>>
small.mlir:10:8: note: see existing live user here:
%6 = linalg.generic {indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], iterator_types = ["parallel", "parallel"]} ins(%4 : tensor<2x4xi32, #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : compressed, d1 : compressed) }>>) outs(%5 : tensor<2x4xi32>) attrs = {sorted = true} {
^bb0(%in: i32, %out: i32):
%7 = arith.minsi %in, %out : i32
linalg.yield %7 : i32
} -> tensor<2x4xi32>
%r = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]}
^
```
Contributor guide
Assessment
This issue has not been assessed yet.