emscripten-core / emscripten-core/emscripten

"signext" attribute possibly leads micompilation

Open
#19,709 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 6h
Merged PRs (30d)
109

Description

When using LLVM 17.0.0 (and older) version, below LLVM IR are compiled to weird wasm binary.
```
define i32 @foo(i1 signext noundef %cond, i32 noundef %y) {
%e = zext i1 %cond to i32
%r = sub i32 %y, %e
ret i32 %r
}
```
after compiling,
```
(module
(type (;0;) (func))
(type (;1;) (func (param i32 i32) (result i32)))
(func $__wasm_call_ctors (type 0))
(func $foo (type 1) (param i32 i32) (result i32)
local.get 1
local.get 0
i32.add)
(memory (;0;) 2)
(global $__stack_pointer (mut i32) (i32.const 66560))
(global (;1;) i32 (i32.const 1024))
(global (;2;) i32 (i32.const 1024))
(global (;3;) i32 (i32.const 1024))
(global (;4;) i32 (i32.const 66560))
(global (;5;) i32 (i32.const 1024))
(global (;6;) i32 (i32.const 66560))
(global (;7;) i32 (i32.const 131072))
(global (;8;) i32 (i32.const 0))
(global (;9;) i32 (i32.const 1))
(export "memory" (memory 0))
(export "__wasm_call_ctors" (func $__wasm_call_ctors))
(export "foo" (func $foo))
(export "__dso_handle" (global 1))
(export "__data_end" (global 2))
(export "__stack_low" (global 3))
(export "__stack_high" (global 4))
(export "__global_base" (global 5))
(export "__heap_base" (global 6))
(export "__heap_end" (global 7))
(export "__memory_base" (global 8))
(export "__table_base" (global 9)))
```
If first argument (%cond) is 1, second argument is 0 , %cond is zero-extended to still 1 and result is 0 - 1 = -1 in llvm. But in wasm, first argument just added to second argument, so the result is 0 + 1 = 1.

When i Imported above function foo in JS, the results is different from what i intended. I think it is related to some ABIs.
https://github.com/rustwasm/team/issues/88

I think current compilation is wrong because wasm's functions can be exported and can be used in other environments.
I found a similar issue in the past.
https://github.com/llvm/llvm-project/issues/57181

Thank you.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.