Issue 60655
Summary [Clang][WebAssembly] When compile with -O2, simd intrinsic wasm_i64x2_shl is optimized to wrong value.
Labels new issue
Assignees
Reporter Changqing-JING
    ## Environment
### OS
Ubuntu 22.04
```shell
$ uname -a
Linux ubuntu 5.15.0-25-generic #25-Ubuntu SMP Wed Mar 30 15:54:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
```
### Clang version
```shell
$ clang --version
clang version 17.0.0 (https://github.com/llvm/llvm-project.git 5d10753314ed58e1f55d41118c8f082c5fc7b2d7)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/jcq/workspace/github/llvm-project/build/bin
```

## Reproduce steps
1. Compile save following code to main.c
```C
#include <wasm_simd128.h>

int main(void){

    uint8_t buff[16] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    
    v128_t a = wasm_v128_load(buff);
    v128_t r2 = wasm_i64x2_shl(a, 64);

 unsigned char* p2 = (unsigned char*)&r2;

    int sum = 0;

 for(int i = 0;i<16;i++){
      sum += p2[i];
    }

    return sum;
}
```
2. Build the C code with clang
```shell
clang -O2 --target=wasm32 -msimd128 -c -o main.O2.wasm main.c
```
3. Review the wasm by wasm2wat:
```shell
$ wasm2wat --enable-all -o main.O2.wat main.O2.wasm
$ cat main.O2.wat
(module
  (type (;0;) (func (result i32)))
  (type (;1;) (func (param i32 i32) (result i32)))
  (import "env" "__linear_memory" (memory (;0;) 0))
  (import "env" "__indirect_function_table" (table (;0;) 0 funcref))
  (func $__original_main (type 0) (result i32)
    i32.const 0)
  (func $main (type 1) (param i32 i32) (result i32)
    call $__original_main))
```
#### Observed behavior
After optimization of clang, the main function returns 0

#### Expected behavior
After optimization of clang, the main function returns 16. Because according to WebAssembly standard, left shift by 64 need to be truncated to left shift by 0. 
![image](https://user-images.githubusercontent.com/59640930/218049500-6b496aad-0ceb-4ff7-9e50-b5b1ed640435.png)
https://github.com/WebAssembly/simd/blob/main/proposals/simd/SIMD.md
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to