Skip to content

How to distribute THREAD_MODEL=posix builds? #326

Closed

Description

@abrown

When attempting to use wasi-sdk to compile a program that might use threads (or shared memories, etc.), the program will fail to link because the libc used has not been compiled with all of the right features (i.e., --shared-memory which relies on --features='atomics,bulk-memory'):

$ cat << EOF > test.c
#include <stdio.h>
int main() {
  printf("...");
  return 0;
}
EOF

$ /opt/wasi-sdk/wasi-sdk-16.0/bin/clang -O3 --target=wasm32-wasi \
    --sysroot=/opt/wasi-sdk/wasi-sdk-16.0/share/wasi-sysroot \
    -mthread-model posix -Xlinker --features='atomics,bulk-memory' -Xlinker --shared-memory \
    test.c -o test.wasm -v
...
 "/opt/wasi-sdk/wasi-sdk-16.0/bin/wasm-ld" -m wasm32 -L/opt/wasi-sdk/wasi-sdk-16.0/share/wasi-sysroot/lib/wasm32-wasi /opt/wasi-sdk/wasi-sdk-16.0/share/wasi-sysroot/lib/wasm32-wasi/crt1-command.o --features=atomics,bulk-memory --shared-memory /tmp/test-86de01.o -lc /opt/wasi-sdk/wasi-sdk-16.0/lib/clang/14.0.4/lib/wasi/libclang_rt.builtins-wasm32.a -o test.wasm
/opt/wasi-sdk/wasi-sdk-16.0/bin/wasm-ld: /lib64/libtinfo.so.6: no version information available (required by /opt/wasi-sdk/wasi-sdk-16.0/bin/wasm-ld)
wasm-ld: error: --shared-memory is disallowed by errno.o because it was not compiled with 'atomics' or 'bulk-memory' features.
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)

There should be some way to package up the output of a make THREAD_MODEL=posix build in the released binaries and helpfully link in the right things when, e.g., -pthreads or -mthread-model=posix are used. Any thoughts on how to move forward on this?

Activity

  1. abrown commented on Sep 15, 2022

    @abrown
    ContributorAuthor
  2. changed the title [-]How to package [/-] [+]How to distribute `THREAD_MODEL=posix` builds?[/+] on Sep 15, 2022
  3. sbc100 commented on Sep 15, 2022

    @sbc100
    Member

    I would suggest that, at least initially you just build a completely separate sysroot. e.g. wasi-sysroot-mt?

    In he long run we could come up with some kind of scheme where clang you select the library subdirectory within the sysroot differently, just like it does for architecture. e.g. -L<sysroot>/lib/wasm32-wasi/ would become something like -L<sysroot>/lib/wasm32-wasi/mt/. That why we could have single sysroot (and indeed a single architecture within that sysroot) that contains both a the mutlithreaded and singlethreaded libraries.

    But I think two different sysroots should for a the time being.

  4. abrown commented on Sep 19, 2022

    @abrown
    ContributorAuthor

    Here's what we discussed today: wasi-libc would get distributed with a new subdirectory mt containing a whole new sysroot, the output of make THREAD_MODEL=posix. Then, we would need to change clang to append mt to the sysroot path when it detects flags that require a reentrant build (e.g., -pthread). @sunfishcode, @sbc100, does that sound right? (And maybe it's just a restatement of what @sbc100 just said above?)

  5. sbc100 commented on Sep 19, 2022

    @sbc100
    Member

    I think we discussed not a separate sysroot just a new directory inside the existing sysroot (e.g. lib/was32-unknown-unknown/mt)? This will require a clang patch.

    The completely separate sysroot is what we could use to try this out without first landing the clang patch.

  6. abrown commented on Sep 19, 2022

    @abrown
    ContributorAuthor

    Oh, ok, then I was misunderstanding what was said. So the contents of mt would instead contain the threads-compatible versions of all of the lib artifacts (e.g., crt1.o, libc.a, libm.a, libwasi-emulated-*.a, etc.)?

  7. sbc100 commented on Sep 19, 2022

    @sbc100
    Member

    Basically yes, although crt1.o tends to be handled separately ... it might be special handling. Take a look a the current clang driver code for now it picks the lib directly along with how it picks crt1.o.

  8. penzn commented on Sep 23, 2022

    @penzn
    Collaborator

    I think we discussed not a separate sysroot just a new directory inside the existing sysroot (e.g. lib/was32-unknown-unknown/mt)?

    It can also be treated as a 'feature' of the target, and maybe triple can become wasm32-unknown-unknown-mt or wasm32-unknown-mt. The upside of that, in my opinion, is that it better reflects that 'mt' and 'non-mt' object files are more or less incompatible (mixing them leads to link errors, in all or some cases).

  9. sbc100 commented on Sep 23, 2022

    @sbc100
    Member

    Sure, if you think we can shoehorn it into the tipple that would great!

  10. sbc100 commented on Sep 23, 2022

    @sbc100
    Member

    That might be a bit tricky tough... what happens if you build with -triple=wasm32-unknown + -pthread .. should that fail because you didn't specify a triple that supports threading?

  11. sbc100 commented on Sep 23, 2022

    @sbc100
    Member

    I guess when the triple gets expanded from what the user specified on the command line and the missing components are added we could inject the -mt suffiix if the -pthread flag is present.

  12. penzn commented on Sep 23, 2022

    @penzn
    Collaborator

    I guess when the triple gets expanded from what the user specified on the command line and the missing components are added we could inject the -mt suffiix if the -pthread flag is present.

    Probably, I'll look at the driver and see what would be easier.

  13. alexcrichton commented on Sep 27, 2022

    @alexcrichton
    Collaborator

    One thing that may be worth pointing out here from a Rust-perspective is that Rust can probably only really support this if the string is a brand new target. Rust additionally has precompiled artifacts (the Rust standard library) and I don't think that infrastructurally it's really possible to change the Rust compiler and distribution infrastructure for just the wasm target to have two sysroots in the similar manner that's being proposed for C here.

    Personally I have always hoped to avoid having a threaded and non-threaded target for all wasm targets but at the current point in time I think it's the best solution at least for Rust. I'm sure, though, that whatever naming scheme is used for a C target (if that's settled on) will want to be mirrored in Rust as well.

  14. sunfishcode commented on Sep 27, 2022

    @sunfishcode
    Member

    To add to what Alex said here, adding a new triple ends up having a lot of administrative overhead.

    I'd like to second the above ideas of having threading-enabled artifacts to be available in the same sysroot, under the same target triple, and teaching the clang and rust drivers how to find them, so that we don't need a whole separate target.

    On a tangential note, I expect I'll have a hard time remembering the name "mt" stands for; could we name the artifacts something with "thread" in the name?

  15. alexcrichton commented on Sep 27, 2022

    @alexcrichton
    Collaborator

    To clarify, though, as far as I know that's not a viable strategy for Rust (using the same target with separate sysroots). Implementing that for Rust would involve a fair amount of change to the release process, building, etc, that's all wasm-specific and doesn't have any precedent. I would be quite skeptical that unless someone is already signed up to do the work that this would never get done because it's too different from what's already there. Put another way, if a new target isn't made I don't think that Rust will, in any near-to-mid timeframe, gain support for precompiled artifacts with multithreading.

    Broadly speaking I do personally care about Rust having good multithreading support for wasm, but not to the point that I can justify refactoring Rust's own distribution mechanism to accomodate this use case. I very much agree that adding new targets is a very big hammer but that's what I mean by I don't see a better solution at this time. Rust has an intermediate solution, though, that C doesn't where it's easier, at least on the nightly Rust channel, to compile the standard library from source with atomics/threads enabled.

  16. 18 remaining items

  17. sunfishcode commented on Nov 22, 2022

    @sunfishcode
    Member

    I like the idea of just accepting this. It's advantageous for WASI to be able to "fit in" within existing cross-compilation filesystem layouts.

  18. infinity0 commented on Nov 22, 2022

    @infinity0

    I guess I don't quite see why we shouldn't assume that "sysroot-is-target" when --sysroot is passed?

    I'm saying these two things are orthogonal and in general one should preserve orthogonality in flag behaviours. If you assume "sysroot-is-target", this should be done regardless of whether --sysroot was passed explicitly or a default value was chosen for it. This general principle of programming makes it easier to compose lower-level tools into higher-level tools, as the behaviours are more predictable.

    Take your example about sysroot builders, yes if they only focus on wasm32-wasi today it make seem "redundant" but if they extend their functionality or someone composes them with another tool, then it's not redundant and in fact it would be less messy.

    Thats basically what your clang patch does today doesn't it?

    I think you're confusing me with @paravoid , we are both Debian developers but I'm giving an alternative opinion here. I didn't write any patches for clang, but I do maintain rustc in Debian as well (where the question about include paths is moot).

  19. sbc100 commented on Nov 22, 2022

    @sbc100
    Member

    Regarding the duplication if headers, assuming we stick with our decision to treat wasm32-wasi-pthreads as a separate target, how should we handle the header redundancy? Should we:

    a. Ship the headers twice, once under each target subdirectory
    b. Ship the headers once in wasm32-wasi and have clang search or headers under the specific target (wasm32-wasi-pthreads) first and fall back to wasm32-wasi?

    (Any other options?)

    I imagine there is some precedent for (a) ? e.g. /usr/include/x86_64-linux-gnu/ and /usr/include/i386-linux-gnu/ contains a lot of identical files?

  20. sunfishcode commented on Nov 22, 2022

    @sunfishcode
    Member

    The wasi-sdk headers appear to be about 9 MiB installed. That's not free, but if it's significantly simpler to duplicate the headers than to figure out some kind of sharing configuration, I propose that we just duplicate it.

  21. sbc100 commented on Dec 17, 2022

    @sbc100
    Member

    Can we close this now that #331 has landed?

  22. abrown commented on Dec 19, 2022

    @abrown
    ContributorAuthor

    Will #331 be shipped in the next version of wasi-sdk? I'm not too sure what the release process is but the original intent of this issue is that future versions of wasi-libc would have both targets. I think we can close this once that is figured out.

  23. sbc100 commented on Dec 19, 2022

    @sbc100
    Member

    Yes the next version should have two different target triples in the sysroot I think... although there maybe a little more work to make that happen still.

  24. sbc100 commented on Dec 19, 2022

    @sbc100
    Member

    the question of how we should distribute is kind of answered though: under a different target triple.

  25. abrown commented on Dec 21, 2022

    @abrown
    ContributorAuthor

    I agree, but I'm concerned with solving the end problem: @sbc100 or @sunfishcode, what else needs to happen for the THREAD_MODEL=posix builds to be released in the next version of wasi-sdk? If it's just a wasi-sdk thing at this point, I'll open an issue there with whatever you suggest and we can close this one.

    One other point, though: as I was writing up the proposal to add this new target to Rust, I began to wonder whether wasm32-wasi-pthread is in fact the right name for the target. I wonder if perhaps the Rust target might make more sense as wasm32-wasi-threads and then I foresee some confusion (use wasm32-wasi-threads for Rust, use wasm32-wasi-pthread for C, etc.). Can we quickly bikeshed the name of this target before we release anything?

  26. sbc100 commented on Dec 21, 2022

    @sbc100
    Member

    I'd be fine with calling the clang triple wasm32-wasi-threads if you prefer .. internally withing clang/llvm I believe we do use threadmodel=posix, but I guess that is an implementation detail.

  27. sbc100 commented on Dec 21, 2022

    @sbc100
    Member

    It interesting that using pthread in the singular form makes sense to me in the triple but not thread in the singular.

  28. sbc100 commented on Dec 21, 2022

    @sbc100
    Member

    (earlier comment deleted)

  29. sunfishcode commented on Dec 21, 2022

    @sunfishcode
    Member

    The current release process isn't very automated. I take the build artifacts from CI and manually upload them to the release. So to add more artifacts here, we just need to (a) make sure CI produces them, and (b) that whoever does the next release knows to upload those artifacts.

  30. abrown commented on Jan 19, 2023

    @abrown
    ContributorAuthor

    Ok, I think the "how" is resolved at this point so we can close this. @yamt has WebAssembly/wasi-sdk#274 open to add wasm32-wasi-threads support to wasi-sdk. With that in place, the right answer for "how to distribute THREAD_MODEL=posix builds will be to use some new-ish version of wasi-sdk and tell the compiler to target wasm32-wasi-threads — all of that can be documented in the wasi-sdk repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions