Skip to content

fix(compiler/rustc_target): set correct linker flags for wasm32v1-none #145539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

StackOverflowExcept1on
Copy link
Contributor

@StackOverflowExcept1on StackOverflowExcept1on commented Aug 17, 2025

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 17, 2025
@rustbot
Copy link
Collaborator

rustbot commented Aug 17, 2025

These commits modify compiler targets.
(See the Target Tier Policy.)

// were made to WebAssembly starting with LLVM 20.1.0:
// https://releases.llvm.org/20.1.0/docs/ReleaseNotes.html#changes-to-the-webassembly-backend
"--features=+mutable-globals",
"--no-check-features",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disabling feature checking doesn't seem right to me. Why is that necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/llvm/llvm-project/blob/llvmorg-20.1.8/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp#L249-L257
because if there are 0 functions in the LLVM IR module, then AnyDefinedFuncs = false and this will result in all features from wasm32-unknown-unknown being enabled

Copy link
Contributor Author

@StackOverflowExcept1on StackOverflowExcept1on Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, you're right in sense. because it will break -C target-feature=+...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems that the correct fix is to somehow set the fallback to cpu="mvp" instead of cpu="" in wasm-ld

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe adding a --mllvm=-mcpu=<target_cpu> argument at

self.link_arg(&format!("--lto-{opt_level}"));
would work? I don't know if that is the correct way to pass the target cpu to wasm-ld though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it works

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although it seems that this is problem with -C target-feature=+...

// were made to WebAssembly starting with LLVM 20.1.0:
// https://releases.llvm.org/20.1.0/docs/ReleaseNotes.html#changes-to-the-webassembly-backend
"--features=+mutable-globals",
"--no-check-features",
"--mllvm=-mcpu=mvp",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing it at the place I indicated (the WasmLd implementation) has the advantage that it works for all wasm targets and also when the user passes -Ctarget-cpu to override the target default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's probably good idea, but we need WasmTM->CPU to be set from the command line.

Copy link
Member

@bjorn3 bjorn3 Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasm-ld can't notice any difference between setting this cli arg in the target spec and setting it in the WasmLd implementation I linked to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right. I checked in the debugger and it's also worth adding --mllvm=-mattr=+mutable-globals to change WasmTM->TargetFS (features set).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wasm32v1-none still creates incorrect object files
4 participants