Issue 202431
Summary [CIR] Constant folding of vectors is incorrect
Labels ClangIR
Assignees
Reporter andykaylor
    Constant folding of vectors in CIR is incorrect in two ways. First, when the result type of the compare is not a vector of i1, the result should be either 0 or -1, whereas we are currently folding to 0 or 1. Second, the folding of integer values is assuming that the value is signed, so the result is incorrectly computed for unsigned values.

Reproducer:

```
!u8i = !cir.int<u, 8>
!s8i = !cir.int<s, 8>

module  {
  cir.func @fold_cmp_vector_op_test() -> !cir.vector<4 x !s8i> {
    %vec_1 = cir.const #cir.const_vector<[#cir.int<255> : !u8i, #cir.int<0> : !u8i, #cir.int<1> : !u8i, #cir.int<254> : !u8i]> : !cir.vector<4 x !u8i>
    %vec_2 = cir.const #cir.const_vector<[#cir.int<254> : !u8i, #cir.int<255> : !u8i, #cir.int<255> : !u8i, #cir.int<255> : !u8i]> : !cir.vector<4 x !u8i>
    %new_vec = cir.vec.cmp(gt, %vec_1, %vec_2) : !cir.vector<4 x !u8i>, !cir.vector<4 x !s8i>
    cir.return %new_vec : !cir.vector<4 x !s8i>
  }
}
```
The compare should fold to `cir.const #cir.const_vector<[#cir.int<-1> : !s8i, #cir.int<0> : !8i, #cir.int<0> : !s8i, #cir.int<0> : !s8i]> : !cir.vector<4 x !s8i>`. Instead it folds to `cir.const #cir.const_vector<[#cir.int<1> : !s8i, #cir.int<1> : !8i, #cir.int<1> : !s8i, #cir.int<0> : !s8i]> : !cir.vector<4 x !s8i>`
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to