-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
[WebAssembly] Change the default linker for wasm32-wasip2
#84569
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-driver Author: Alex Crichton (alexcrichton) ChangesThis commit changes the default linker in the WebAssembly toolchain for the The Full diff: https://github.com/llvm/llvm-project/pull/84569.diff 3 Files Affected:
diff --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index b8c2573d6265fb..a6c43c627f7206 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -221,6 +221,12 @@ WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
}
}
+const char *WebAssembly::getDefaultLinker() const {
+ if (getOS() == "wasip2")
+ return "wasm-component-ld";
+ return "wasm-ld";
+}
+
bool WebAssembly::IsMathErrnoDefault() const { return false; }
bool WebAssembly::IsObjCNonFragileABIDefault() const { return true; }
diff --git a/clang/lib/Driver/ToolChains/WebAssembly.h b/clang/lib/Driver/ToolChains/WebAssembly.h
index ae60f464c10818..76e0ca39bd748d 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.h
+++ b/clang/lib/Driver/ToolChains/WebAssembly.h
@@ -67,7 +67,7 @@ class LLVM_LIBRARY_VISIBILITY WebAssembly final : public ToolChain {
llvm::opt::ArgStringList &CmdArgs) const override;
SanitizerMask getSupportedSanitizers() const override;
- const char *getDefaultLinker() const override { return "wasm-ld"; }
+ const char *getDefaultLinker() const override;
CXXStdlibType GetDefaultCXXStdlibType() const override {
return ToolChain::CST_Libcxx;
diff --git a/clang/test/Driver/wasm-toolchain.c b/clang/test/Driver/wasm-toolchain.c
index f950283ec42aa0..5f18e56f79b657 100644
--- a/clang/test/Driver/wasm-toolchain.c
+++ b/clang/test/Driver/wasm-toolchain.c
@@ -197,3 +197,11 @@
// RUN: not %clang -### %s --target=wasm32-unknown-unknown --sysroot=%s/no-sysroot-there -fPIC -mno-mutable-globals %s 2>&1 \
// RUN: | FileCheck -check-prefix=PIC_NO_MUTABLE_GLOBALS %s
// PIC_NO_MUTABLE_GLOBALS: error: invalid argument '-fPIC' not allowed with '-mno-mutable-globals'
+
+// Test that `wasm32-wasip2` invokes a the `wasm-component-ld` linker by default
+// instead of `wasm-ld`.
+
+// RUN: %clang -### -O2 --target=wasm32-wasip2 %s --sysroot /foo 2>&1 \
+// RUN: | FileCheck -check-prefix=LINK_WASIP2 %s
+// LINK_WASIP2: "-cc1" {{.*}} "-o" "[[temp:[^"]*]]"
+// LINK_WASIP2: wasm-component-ld{{.*}}" "-L/foo/lib/wasm32-wasip2" "crt1.o" "[[temp]]" "-lc" "{{.*[/\\]}}libclang_rt.builtins-wasm32.a" "-o" "a.out"
|
cc @sunfishcode and @sbc100 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does wasm-component-ld accept any other input types other than the ones that wasm-ld accepts?
Does wasm-component-ld call wasm-ld internally?
Do we expect clang users to be building compound components using a single clang command? i.e. will they be somehow supplying input files that describe how the componend links? Would it make more sense to have clang default to building core modules and have the component creation be a higher level thing built on top clang outputs?
Regarding WebAssembly/wasi-sdk and WebAssembly/wasi-libc, is there any reason why simple programs wouldn't be core modules? Won't most C/C++ programs still be build-able as just core modules? |
Currently it accepts no extra inputs, but in the future I'd expect that it'll grow an option or two of its own. Currently it hand-codes the list of options to forward to
In step (2) it's using component type information smuggled through LLVM/wasm-ld through custom sections to create the final output component.
If by compound you mean linking multiple components internally, then no. The intention is that
That's actually what we currently have to day with converting I do plan on having a flag (also answering the question about wasm-component-ld-specific-flags) to not emit a component and instead skip the componentization step. This step could also be done by using
Yep, they'll all still be buildable as core modules (as that's the internals of a component anyway, a big thing produced by LLVM). With the p2 target though the thinking is that components are the default output, not core wasm modules. Another future feature on the theoretical roadmap for wasm-component-ld is that Joel has componentize-py built on "dynamic linking" of a sort which gets |
I see, so So the core module + metadata is kind of isomorphic with that single-core-module-component? |
I was more asking about whether the file types is accepts are anything more than object files and libraries. i.e. can you pass other core modules and have wasm-component-ld build a component that contains more than one core module? Also, can you say more about the metadata that is being using to drive wasm-componenet-ld? If the core module is all based on canonical ABI what extra metdata is needed/planned? |
Sort of and sort of not. At a high level you're correct, but at a technical level this isn't correct. The subtle differences are:
Going from core module + metadata into a component is a pretty nontrivial operation, so the raw output of
Ah sory I misunderstood! Currently wasm-component-ld takes no other inputs and it assumes everything goes into
Certainly! The canonical ABI gives the ability to say that for any component model function type it corresponds to a particular core wasm function type. That operation is not reversible, though, you can't go from core wasm back to the component model function type. Thus the metadata carries along this information. That way the componentization process draws a mapping from component model import/export to core wasm import/export and then can synthesize the right type in the component binary format. The short answer to your question, though, is |
ping @sbc100, happy to answer any more questions if you have them! I was tentatively hoping this could get backported to an 18.1.x release so we could get a wasi-sdk release with p1/p2/etc |
This commit changes the default linker in the WebAssembly toolchain for the `wasm32-wasip2` target. This target is being added to the WebAssembly/wasi-sdk and WebAssembly/wasi-libc projects to target the Component Model by default, in contrast with the preexisting `wasm32-wasi` target (in the process of being renamed to `wasm32-wasip1`) which outputs a core WebAssembly module by default. The `wasm-component-ld` project currently lives in my GitHub account at https://github.com/alexcrichton/wasm-component-ld and isn't necessarily "official" yet, but it's expected to continue to evolve as the `wasm32-wasip2` target continues to shape up and evolve.
fb90a73
to
d1a1b22
Compare
At @sunfishcode's request I've pushed up a second commit which pass the path to |
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
This commit adds an explicit argument that's passed to `wasm-component-ld` containing the location of `wasm-ld` itself. This enables `wasm-component-ld` to avoid hunting around looking for it and instead use the install that's paired with Clang itself. Additionally this reinterprets the `-fuse-ld=lld` argument to explicitly requesting the `wasm-ld` linker flavor, even on `wasm32-wasip2` targets.
d1a1b22
to
7284fd4
Compare
@alexcrichton Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
/cherry-pick d66121d |
This commit changes the default linker in the WebAssembly toolchain for the `wasm32-wasip2` target. This target is being added to the WebAssembly/wasi-sdk and WebAssembly/wasi-libc projects to target the Component Model by default, in contrast with the preexisting `wasm32-wasi` target (in the process of being renamed to `wasm32-wasip1`) which outputs a core WebAssembly module by default. The `wasm-component-ld` project currently lives in my GitHub account at https://github.com/alexcrichton/wasm-component-ld and isn't necessarily "official" yet, but it's expected to continue to evolve as the `wasm32-wasip2` target continues to shape up and evolve. (cherry picked from commit d66121d)
/pull-request #85802 |
This commit changes the default linker in the WebAssembly toolchain for the `wasm32-wasip2` target. This target is being added to the WebAssembly/wasi-sdk and WebAssembly/wasi-libc projects to target the Component Model by default, in contrast with the preexisting `wasm32-wasi` target (in the process of being renamed to `wasm32-wasip1`) which outputs a core WebAssembly module by default. The `wasm-component-ld` project currently lives in my GitHub account at https://github.com/alexcrichton/wasm-component-ld and isn't necessarily "official" yet, but it's expected to continue to evolve as the `wasm32-wasip2` target continues to shape up and evolve. (cherry picked from commit d66121d)
@alexcrichton This is breaking
|
Yes it's expected that wasi-sdk will need changes to incorporate this PR. I'm waiting for this to be in an LLVM release before updating wasi-sdk. |
This commit changes the default linker in the WebAssembly toolchain for the `wasm32-wasip2` target. This target is being added to the WebAssembly/wasi-sdk and WebAssembly/wasi-libc projects to target the Component Model by default, in contrast with the preexisting `wasm32-wasi` target (in the process of being renamed to `wasm32-wasip1`) which outputs a core WebAssembly module by default. The `wasm-component-ld` project currently lives in my GitHub account at https://github.com/alexcrichton/wasm-component-ld and isn't necessarily "official" yet, but it's expected to continue to evolve as the `wasm32-wasip2` target continues to shape up and evolve.
@alexcrichton Not sure if you've seen but this has been released as part of LLVM 18.1.2: https://github.com/llvm/llvm-project/tree/llvmorg-18.1.2/clang/lib/Driver/ToolChains |
Oop no I did not, thank you for the heads up! I'll work on updating wasi-sdk now. |
No problem, I have wasi-sdk all working in a Docker container now with your latest version of wasm-component-ld so I should be thanking you! |
This commit changes the default linker in the WebAssembly toolchain for the
wasm32-wasip2
target. This target is being added to the WebAssembly/wasi-sdk and WebAssembly/wasi-libc projects to target the Component Model by default, in contrast with the preexistingwasm32-wasi
target (in the process of being renamed towasm32-wasip1
) which outputs a core WebAssembly module by default.The
wasm-component-ld
project currently lives in my GitHub account at https://github.com/alexcrichton/wasm-component-ld and isn't necessarily "official" yet, but it's expected to continue to evolve as thewasm32-wasip2
target continues to shape up and evolve.