<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/150302>150302</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[MLIR][TOSA] Folding cast to bool gives wrong value
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir:tosa
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
GleasonK
</td>
</tr>
</table>
<pre>
I'm fixing what I believe to be a bug in StableHLO with our type convert folder, and I believe it may exist in other dialects as well?
Looking at https://www.mlplatform.org/tosa/tosa_spec.html#_cast:
```
if (is_same<out_t,bool_t>()) {
out = (in != 0) ? true : false;
}
```
However with a lack of special casing bools Convert / Cast-like ops these are folded incorrectly:
```
$ cat /tmp/t.mlir
func.func @const_f32_bool() -> tensor<i1> {
%cst = arith.constant dense<10> : tensor<i32>
%0 = tosa.cast %cst : (tensor<i32>) -> tensor<i1>
func.return %0 : tensor<i1>
}
$ mlir-opt /tmp/t.mlir --canonicalize
module {
func.func @const_f32_bool() -> tensor<i1> {
%0 = "tosa.const"() <{values = dense<false> : tensor<i1>}> : () -> tensor<i1>
return %0 : tensor<i1>
}
}
```
Input was `10 != 0` so probably should be folded to `true`(?) Not sure if there are other cast-like ops in upstream dialects that may have this issue as well?
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykVMFu4zYU_Jrny4MNirTs-KCDYq-7QdMu0O3doKgniw0lCuSTvenXF5SVTTaboocChgTBnOGb4XB0jPbcExWQ30N-WOiRWx-KXxzp6PtfF5Wvn4sHkNsOG_vN9me8tprxAStyli6E7LEi1FiNZ7Q9fmVdOfr8-AWvllv0Y0B-HgiN7y8UGBvvagog96j7-g2NZez0M9I3GznxeG4pYG21I8MRdcQrOQfqCKIEUT56_5SG0Ywt8xBBlSCPII_X63XVucFpbnzoVj6cQR7ZRz2_TnEgs2q5cyDVyejICTpxwkbMP1HaBkHe2XiKuiNQez_yiUHuK-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