llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.25k stars 12.08k forks source link

thin LTO produces function signature error #91700

Open yamt opened 6 months ago

yamt commented 6 months ago

a test code: https://github.com/yamt/garbage/tree/1f7dc06dd2f6c63bf0bb27f58ffe2c98fdbaa953/lto2

it fails with -flto=thin. i'm not sure if the problem is specific to wasm or it's just wasm being picky on function type happened to expose it.

spacetanuki% ./build.sh          
+ CC='/opt/wasi-sdk-22.0/bin/clang -Os -flto=thin'
+ /opt/wasi-sdk-22.0/bin/clang -Os -flto=thin -c a.c
+ /opt/wasi-sdk-22.0/bin/clang -Os -flto=thin -c main.c
+ /opt/wasi-sdk-22.0/bin/clang -Os -flto=thin main.o a.o
wasm-ld: warning: function signature mismatch: f
>>> defined as (i32) -> void in a.o
>>> defined as () -> void in lto.tmp
spacetanuki% toywasm --wasi a.out
Error: [trap] unreachable executed (4): unreachable at 000080
frame[  3] funcpc 000080 (a.out:signature_mismatch:f) callerpc 000108
  param [0] = 00000000
frame[  2] funcpc 000100 (a.out:.Lf_bitcast) callerpc 0000fb
frame[  1] funcpc 0000f5 (a.out:__main_void) callerpc 0000ba
frame[  0] funcpc 000086 (a.out:_start)
  local [0] = 00000000
2024-05-10 14:20:55 (1715318455.896787000): [105704600] instance_execute_func failed with -1
2024-05-10 14:20:55 (1715318455.897278000): [105704600] invoke failed with -1
spacetanuki% 

the same program builds and works fine with full LTO and no LTO.

spacetanuki% CC="/opt/wasi-sdk-22.0/bin/clang -Os -flto=full" ./build.sh
+ CC='/opt/wasi-sdk-22.0/bin/clang -Os -flto=full'
+ /opt/wasi-sdk-22.0/bin/clang -Os -flto=full -c a.c
+ /opt/wasi-sdk-22.0/bin/clang -Os -flto=full -c main.c
+ /opt/wasi-sdk-22.0/bin/clang -Os -flto=full main.o a.o
spacetanuki% CC="/opt/wasi-sdk-22.0/bin/clang -Os" ./build.sh 
+ CC='/opt/wasi-sdk-22.0/bin/clang -Os'
+ /opt/wasi-sdk-22.0/bin/clang -Os -c a.c
+ /opt/wasi-sdk-22.0/bin/clang -Os -c main.c
+ /opt/wasi-sdk-22.0/bin/clang -Os main.o a.o
spacetanuki% 

the same result with clang built from the latest LLVM (46435ac19e09039fb146fa6c12da0e640a66d435)

i found the issue when trying to build wasi-libc with thin LTO.

EugeneZelenko commented 6 months ago

@teresajohnson