<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/83530>83530</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Inconsistent results when using --arith-unsigned-when-equivalent
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
wangyongj1a
</td>
</tr>
</table>
<pre>
I have the following MLIR program:
test.mlir:
```
module {
func.func @func1() {
%c1506825994_i64 = arith.constant 1506825994 : i64
%c1111530998_i64 = arith.constant 1111530998 : i64
%c1357835903_i64 = arith.constant 1357835903 : i64
%c4995493184_i64 = arith.constant 4995493184 : i64
%false = arith.constant false
%21 = scf.if %false -> (i64) { //the result should be 1111530998
scf.yield %c4995493184_i64 : i64
} else {
scf.yield %c1111530998_i64 : i64
}
// 1357835903 % 1111530998 = 246304905
%22 = arith.remui %c1357835903_i64, %21 : i64 //the result should be 246304905
// 246304905 or 1357835903 = 1592721151
// 1110101011100101000010001001 or 1010000111011101110111001111111 = 1011110111011101111111011111111
%26 = arith.ori %22, %c1357835903_i64 : i64 //the result should be 1592721151
// 1011110111011101111111011111111 xor 1506825994(1011001110100000101011100001010) = 0000111001111101010100111110101(121547253)
%57 = arith.xori %26, %c1506825994_i64 : i64 //the result should be 121547253
// 121547253 - 1111530998 = -989983745
%67 = arith.subi %57, %c1111530998_i64 : i64 //the result should be -989983745
// min(-989983745, 1506825994)
%169 = arith.minsi %67, %c1506825994_i64 : i64 //the result should be -989983745
vector.print %169 : i64
return
}
}
```
When I tried ```mlir-opt --convert-scf-to-cf --convert-cf-to-llvm --arith-unsigned-when-equivalent test.mlir```, then the program was optimized to :
```
module {
func.func @func1() {
%c1506825994_i64 = arith.constant 1506825994 : i64
%c1111530998_i64 = arith.constant 1111530998 : i64
%c1357835903_i64 = arith.constant 1357835903 : i64
%c4995493184_i64 = arith.constant 4995493184 : i64
%false = arith.constant false
llvm.cond_br %false, ^bb1, ^bb2
^bb1: // pred: ^bb0
llvm.br ^bb3(%c4995493184_i64 : i64)
^bb2: // pred: ^bb0
llvm.br ^bb3(%c1111530998_i64 : i64)
^bb3(%0: i64): // 2 preds: ^bb1, ^bb2
llvm.br ^bb4
^bb4: // pred: ^bb3
%1 = arith.remui %c1357835903_i64, %0 : i64
%2 = arith.ori %1, %c1357835903_i64 : i64
%3 = arith.xori %2, %c1506825994_i64 : i64
%4 = arith.subi %3, %c1111530998_i64 : i64
%5 = arith.minui %4, %c1506825994_i64 : i64
vector.print %5 : i64
return
}
}
```
I've analyzed this program manually, and according to my analysis, the number values of the two operands in the arith minimum operation seems to be -989983745 and 1506825994. This operation should get the result of -989983745 for ```arith.minsi```, and 1506825994 for ```arith.minui```. However, in my test, the ```--arith-unsigned-when-equivalent``` pass seems to optimize the ```arith.minsi``` operation to ```arith.minui```, and will cause an inconsistent result according to my analysis.
I'm not sure if there is any bug in my program or the wrong usage of the ```--arith-unsigned-when-equivalent``` that caused this error.
My git version is 5b6e58c565cf809e4133a10ff9d9b096754bea1e.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsV0uP2zYQ_jX0ZSCDpEQ9Dj5ssll0geZSFOgxoCTKZiCRLknZcX99QUqWJb_yuPTSZNcWyeHMN9-M51tza-VWCbFB7ANiryveu502myNX25NW26-Er0pdnzbvsOMHAW4noNFtq49SbeHz7-9_wN7oreEdil8QfkX4xQnr1l0rzbSDUjz-hGWn674VgLIPwxqg6VW19i-AEuzfCaI5osXMBlFWEYbTnLKiSL7INAEUvwI30u3WlVbWceXgYgEofgGZJucQMHgghLAYF0X-0MNk8cBDzLI8ZgWOH3qYLO57SIqCJUVM8odZXCzuemh4a8W9e-FgYUpJsLNVs5bN5W6E4k-AaO49DywDom-IvvnyGmH71oHd6b6toRQzSi6-Ifg8SdHW95O6hp29ggiwL1W_9XJTnlsvUzt4uAuuKVsW7xVoksY4KTBbckJn3BnR9fJOYRH9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