Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_target/src/spec/targets/wasm32v1_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ pub(crate) fn target() -> Target {
// For now this target just never has an entry symbol no matter the output
// type, so unconditionally pass this.
"--no-entry",
// It is also necessary to explicitly set CPU and features since some changes
// 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
"--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).

Copy link
Member

Choose a reason for hiding this comment

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

This still doesn't put the --mllvm=-mcpu=... in compiler/rustc_codegen_ssa/src/back/linker.rs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you show demo code with RUSTC_LOG=debug where -C target-feature won't work? It seemed ok to me. The only problem is that with an empty file in the target_features section the features enabled by the user won't be displayed (since .bc files don't save features).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps you mean something like -C target-features=-mutable-globals and -C target-cpu=generic, but I don't understand why user would want to do that.

Copy link
Member

Choose a reason for hiding this comment

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

For wasm32v1-none it is maybe not all that relevant given that you probably don't want to enable any extra features, but for wasm32-unknown-unknown or wasm32-wasip1/2 if I use for example -Clinker-plugin-lto -Ctarget-features=+simd128 I would expect all functions to be compiled with simd128 enabled (including standard library functions) even if the leaf crate is empty (and thus nothing otherwise forces simd128 to be enabled). Supporting this requires passing --mllvm=-mcpu= and --mllvm=-mattr= in

fn push_linker_plugin_lto_args(&mut self) {
, at which point wasm32v1-none should behave correctly too without having to change the target spec afaik.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, you are suggesting to explicitly pass cpu and features to wasm-ld in push_linker_plugin_lto_args depending on what the user is using?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, with a fallback to whatever target cpu and features is in the target spec. This is also what we do for gcc for example:

&format!("-plugin-opt=mcpu={}", self.target_cpu),
Actually maybe getting the target features right would be non-trivial as that isn't plumbed through yet and not used for gcc either. So maybe only set the target cpu in linker.rs and just add a FIXME for the target features (and if you do need to set the target features just do it in the target spec)

"--mllvm=-mattr=+mutable-globals",
],
);
options.add_pre_link_args(
Expand All @@ -37,7 +42,7 @@ pub(crate) fn target() -> Target {
// Make sure clang uses LLD as its linker and is configured appropriately
// otherwise
"--target=wasm32-unknown-unknown",
"-Wl,--no-entry",
"-Wl,--no-entry,--mllvm=-mcpu=mvp,--mllvm=-mattr=+mutable-globals",
],
);

Expand Down
Loading