llvm / llvm-project

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

wasm-ld: Disabling on-by-default features doesn't work when LTO is used #109443

Open alexcrichton opened 2 hours ago

alexcrichton commented 2 hours ago

Given this input:

target triple = "wasm32-unknown-unknown"
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"

define void @foo(ptr %a) #0 {
  call void %a()
  ret void
}

attributes #0 = { "target-cpu"="mvp" }

Locally I see:

$ llvm-as wat.ll
$ wasm-ld wat.bc -o foo.wasm --no-entry --export foo 
$ wasm-tools validate -f=-reference-types foo.wasm
error: func 0 failed to validate

Caused by:
    0: zero byte expected (at offset 0x4b)

This issue is a reduction of https://github.com/rust-lang/rust/issues/130604 and is where a user is trying to disable reference types for their entire compilation and use LTO as well, but it looks like the "target-cpu" isn't taking effect with wasm-ld.

cc @sbc100 do you know what might be causing this?

sbc100 commented 2 hours ago

That does seem wrong. Just to confirm, if you compile to and object file (not a .bc file) this doesn't fail?

alexcrichton commented 2 hours ago

Right yeah, this works ok:

$ llc wat.ll -filetype=obj -o foo.o -mcpu=mvp
$ wasm-ld foo.o -o foo.wasm --no-entry --export foo
$ wasm-tools validate -f=-reference-types foo.wasm

Interestingly the -mcpu=mvp is required though. If I remove that flag then it additionally doesn't work. I forget all the ways that the CPU and such can be configured in LLVM, but I believe that passing -mcpu=mvp mirrors what the Rust compiler does at least. (I'm mostly not sure how the CLI flag -mcpu=mvp interacts with the "target-cpu"="mvp" attribute on functions)

llvmbot commented 1 hour ago

@llvm/issue-subscribers-lld-wasm

Author: Alex Crichton (alexcrichton)

Given this input: ```llvm target triple = "wasm32-unknown-unknown" target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20" define void @foo(ptr %a) #0 { call void %a() ret void } attributes #0 = { "target-cpu"="mvp" } ``` Locally I see: ``` $ llvm-as wat.ll $ wasm-ld wat.bc -o foo.wasm --no-entry --export foo $ wasm-tools validate -f=-reference-types foo.wasm error: func 0 failed to validate Caused by: 0: zero byte expected (at offset 0x4b) ``` This issue is a reduction of https://github.com/rust-lang/rust/issues/130604 and is where a user is trying to disable reference types for their entire compilation and use LTO as well, but it looks like the `"target-cpu"` isn't taking effect with `wasm-ld`. cc @sbc100 do you know what might be causing this?