<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61042>61042</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[MLIR]Error when using `-convert-scf-to-spirv` with message `Failure to legalize unresolved materialization`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
sweead
</td>
</tr>
</table>
<pre>
Hello, I ran into an error when using `mlir-opt -convert-scf-to-spirv` on the following mixed IR.
To be specific, IR was successfully lowered to the `cf `dialect via `-convert-scf-to-cf`.
I'm not sure if this is a bug, it seems the problem is with type conversions.
IR:
```
//mlir-opt test.mlir -convert-scf-to-spirv
#map0 = affine_map<(d0, d1) -> (d0, d1)>
#map1 = affine_map<(d0, d1) -> (d0)>
module attributes {
spv.target_env = #spv.target_env<
#spv.vce<v1.0, [Shader], [SPV_KHR_storage_buffer_storage_class]>, #spv.resource_limits<>>
} {
func.func @test_argmax(%arg0: tensor<14x19xf32>) {
%0 = bufferization.to_memref %arg0 : memref<14x19xf32>
%true = arith.constant true
scf.if %true {
%c1 = arith.constant 1 : index
} else {
%c1 = arith.constant 1 : index
}
%1 = linalg.init_tensor [14] : tensor<14xi32>
%c0_i32 = arith.constant 0 : i32
%c14 = arith.constant 14 : index
%2 = memref.alloc() {alignment = 128 : i64} : memref<14xi32>
%3 = bufferization.to_tensor %2 : memref<14xi32>
linalg.fill ins(%c0_i32 : i32) outs(%2 : memref<14xi32>)
%4 = bufferization.to_tensor %2 : memref<14xi32>
%5 = linalg.init_tensor [14] : tensor<14xf32>
%cst = arith.constant -3.40282347E+38 : f32
%c14_0 = arith.constant 14 : index
%6 = memref.alloc() {alignment = 128 : i64} : memref<14xf32>
%7 = bufferization.to_tensor %6 : memref<14xf32>
linalg.fill ins(%cst : f32) outs(%6 : memref<14xf32>)
%8 = bufferization.to_tensor %6 : memref<14xf32>
%9 = memref.alloc() {alignment = 128 : i64} : memref<14xi32>
memref.copy %2, %9 : memref<14xi32> to memref<14xi32>
%10 = bufferization.to_tensor %9 : memref<14xi32>
%11 = memref.alloc() {alignment = 128 : i64} : memref<14xf32>
memref.copy %6, %11 : memref<14xf32> to memref<14xf32>
%12 = bufferization.to_tensor %11 : memref<14xf32>
linalg.generic {indexing_maps = [#map0, #map1, #map1], iterator_types = ["parallel", "reduction"]} ins(%0 : memref<14x19xf32>) outs(%9, %11 : memref<14xi32>, memref<14xf32>) {
^bb0(%arg1: f32, %arg2: i32, %arg3: f32):
%15 = linalg.index 1 : index
%16 = arith.index_cast %15 : index to i32
%17 = arith.cmpf ogt, %arg1, %arg3 : f32
%18 = arith.select %17, %arg1, %arg3 : f32
%19 = arith.select %17, %16, %arg2 : i32
linalg.yield %19, %18 : i32, f32
}
%13 = bufferization.to_tensor %11 : memref<14xf32>
%14 = bufferization.to_tensor %9 : memref<14xi32>
return
}
} // end module
```
error message:
```
test.mlir:39:13: error: failed to legalize unresolved materialization from 'index' to 'i32' that remained live after conversion
%16 = arith.index_cast %15 : index to i32
^
test.mlir:39:13: note: see current operation: %26 = "builtin.unrealized_conversion_cast"(%25) : (index) -> i32
test.mlir:42:13: note: see existing live user here: %32 = "spv.Select"(%28, %26, %arg2) : (i1, i32, i32) -> i32
%19 = arith.select %17, %16, %arg2 : i32
^
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0WN1u6joWfhpzs0SUOD-QCy52C9WpZkYa9YzmFplkJXjk2Mh2aHuefmQ7QKChPdXeW6ooTrw-f-vfC2YMbyXiiuQPJF_PWG_3Sq_MKyKrZztVv6_-QCEUoY_wDJpJ4NIqYBJQa6XhdY8SesNlC6SIO8H1XB0szCslj6jt3FTN3Kq5OXB9JEUMSoLdIzRKCPXqpDr-hjU8v0RA4jWJf_xHwQ7BHLDiDa_8sS_wygyYvqrQmKYX4h2EekWNNVjl4UgRV437rDkTWFk4cuaWtzSqhhRxFA56JnT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