Issue 136353
Summary [WebAssembly] Incorrect stackification of effectful instructions
Labels new issue
Assignees
Reporter SingleAccretion
    Reading the stackification code, I came across this in `WebAssemblyRegStackify.cpp / static void query`:
```
// These instructions have hasUnmodeledSideEffects() returning true
// because they trap on overflow and invalid so they can't be arbitrarily
// moved, however in the specific case of register stackifying, it is safe
// to move them because overflow and invalid are Undefined Behavior.
```
Which prompted me to think about this this example:
```llvm
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
target triple = "wasm32-unknown-wasi"

declare i32 @extern_func(i32, i32)

define i32 @func(i32 %0, i32 %1) {
  %call_value = call i32 @extern_func(i32 %0, i32 %1)
  %div = udiv i32 %0, %1
  %sub = sub i32 %div, %call_value
  ret i32 %sub
}
```
Running this through `llc` we get:
```
> llc test.ll -o test.o --filetype=obj -O1 && wasm-objdump -d test.o

test.o: file format wasm 0x1

Code Disassembly:

000055 func[1] <func>:
 000056: 20 00                      | local.get 0
 000058: 20 01 | local.get 1
 00005a: 6e                         | i32.div_u
 00005b: 20 00                      | local.get 0
 00005d: 20 01 | local.get 1
 00005f: 10 80 80 80 80 00          | call 0 <env.extern_func>
 000065: 6b                         | i32.sub
 000066: 0b                         | end
```
This `udiv` and the call get swapped. This does not look correct, since we now have _introduced UB_ (a trap) under the following conditions:
1) The divisor (`%1`) is zero.
2) `extern_func` throws (either a WASM exception or a JS exception).
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to