Skip to content

Exprimental WASI support for ports/unix #13676

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 27 commits into
base: master
Choose a base branch
from

Conversation

yamt
Copy link
Contributor

@yamt yamt commented Feb 15, 2024

See comments in build-wasi.sh

Copy link

codecov bot commented Feb 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.57%. Comparing base (bdda91f) to head (5098523).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #13676   +/-   ##
=======================================
  Coverage   98.57%   98.57%           
=======================================
  Files         164      164           
  Lines       21349    21349           
=======================================
  Hits        21045    21045           
  Misses        304      304           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -154,8 +154,12 @@ typedef long mp_off_t;
// Don't default sys.argv and sys.path because we do that in main.
#define MICROPY_PY_SYS_PATH_ARGV_DEFAULTS (0)

#if defined(__wasi__)
Copy link
Contributor

@stinos stinos Feb 15, 2024

Choose a reason for hiding this comment

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

Should be done like

#ifndef  MICROPY_PY_SYS_EXECUTABLE
#define MICROPY_PY_SYS_EXECUTABLE (1)
#endif

so it works generically, then make a separate mpconfigvariant_wasi.h where it gets set to 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

who would include mpconfigvariant_wasi.h?

Copy link
Contributor

@stinos stinos Feb 15, 2024

Choose a reason for hiding this comment

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

That would be done automatically if you use make VARIANT=wasi (though, my mistake, it should actually be unix/variants/wasi/mpconfigvariant.h). Likewise instead of build.sh you'd implement the specific makefile options in unix/variants/wasi/mpconfigvariant.mk. Check out the other variants for examples. Also I think to have any chance of merging this ulab should probably not be included unconditionally because this makes it harder to build out of the box; on the other hand if this only works with non-approved upstream wasi patches it's not super useful for others anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please make this a new variant. It should then be possible to build it via make VARIANT=wasi.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok. i was not aware of the mechanism. thank you. let me try to adapt.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also I think to have any chance of merging this ulab should probably not be included unconditionally because this makes it harder to build out of the box; on the other hand if this only works with non-approved upstream wasi patches it's not super useful for others anyway.

sure.

wrt wasi patches, alternatively it's possible to put the setjmp/longjmp runtime within micropython itself. at least until when setjmp/longjmp gets ready in wasi. how do you think?

anyway, it won't be super useful until wasm EH is widely implemented in runtimes though.

Copy link
Member

Choose a reason for hiding this comment

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

If building unix for wasi requires patches to wasi/binaryen then IMO this PR should stay as a draft until those patches are no longer needed. You could put the setjmp/longjmp code here, but I guess the other patches are still needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i adapted to use VARIANT. it simplify a lot. thank you.

i'm not sure what should be done for UNAME_S logic in Makefile though.
a build for WASI is actually a cross compile. but the existing CROSS_COMPILE seems geared to "arm-none-eabi-gcc" style cross toolchain, which doesn't seem suitable to wasi-sdk style cross toolchain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If building unix for wasi requires patches to wasi/binaryen then IMO this PR should stay as a draft until those patches are no longer needed. You could put the setjmp/longjmp code here, but I guess the other patches are still needed.

FYI, all toolchain patches have been merged in the upstream since then.

@@ -91,7 +91,11 @@
#define MICROPY_PY_OS_ERRNO (1)
#define MICROPY_PY_OS_GETENV_PUTENV_UNSETENV (1)
#define MICROPY_PY_OS_SEP (1)
#if defined(__wasi__)
Copy link
Contributor

@stinos stinos Feb 15, 2024

Choose a reason for hiding this comment

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

Similar comment as above: instead of modifying this variant it might be better to create it separately

@dpgeorge dpgeorge added the board-definition New or updated board definition files. Combine with a port- label. label Feb 15, 2024
@dpgeorge
Copy link
Member

See semi-related #11723, which gets mpy-cross compiling to WASM (but not WASI at this stage).

@yamt yamt force-pushed the unix-wasi branch 2 times, most recently from af654be to 31296b5 Compare April 3, 2024 05:55
@yamt yamt force-pushed the unix-wasi branch 2 times, most recently from 478f48d to ab9cc99 Compare May 2, 2024 06:43
Copy link

github-actions bot commented May 2, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@yamt yamt marked this pull request as ready for review May 2, 2024 07:00
@yamt yamt force-pushed the unix-wasi branch 2 times, most recently from 16efb82 to b070ffc Compare May 2, 2024 07:08
@yamt yamt force-pushed the unix-wasi branch 2 times, most recently from b79d221 to 04ba2be Compare June 10, 2024 12:05
@yamt
Copy link
Contributor Author

yamt commented Jul 5, 2024

is there any TODO for this PR while waiting for LLVM 19?

@yamt
Copy link
Contributor Author

yamt commented Jul 26, 2024

the last push is a conflict resolution

@dpgeorge dpgeorge added port-unix and removed board-definition New or updated board definition files. Combine with a port- label. labels Aug 13, 2024
yamt added 16 commits December 12, 2024 12:10
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
VARIANT=wasi.
Also remove hardcoded ulab.

Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Also, update comments.

Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
To match the new name of the corresponding wasm-opt option.

Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Because it isn't too useful without network.

This might be a bit controversial because actually you can use
socket even with WASIp1 if you use one of non-standard
runtime-specific mechanisms to create/pass an open socket.
Anyway, it's easy to re-enable it if you want to use it.

Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Now WASI-SDK 25.0, which contains LLVM 19, has been released.
Simplify the requirements by using it.

Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
yamt added 2 commits December 12, 2024 12:40
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
@yamt
Copy link
Contributor Author

yamt commented Dec 12, 2024

now wasi-sdk 25 has been released and this only requires released versions of the toolchain.
can you consider to make this land?

@yamt
Copy link
Contributor Author

yamt commented Dec 13, 2024

the ci failure doesn't seem related to this PR.

Cloning into '/home/runner/work/micropython/micropython/lib/asf4'...
fatal: unable to access 'https://github.com/adafruit/asf4/': The requested URL returned error: 403
fatal: clone of 'https://github.com/adafruit/asf4' into submodule path '/home/runner/work/micropython/micropython/lib/asf4' failed
Failed to clone 'lib/asf4'. Retry scheduled

@yamt
Copy link
Contributor Author

yamt commented Apr 16, 2025

@dpgeorge @stinos does this have any blockers?

FROZEN_MANIFEST ?= $(VARIANT_DIR)/manifest.py

# WASI doesn't have FFI
MICROPY_PY_FFI=0
Copy link
Contributor

@stinos stinos Apr 17, 2025

Choose a reason for hiding this comment

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

Should have whitespace around the =, likewise in rest of file

@@ -0,0 +1,28 @@
# Build for WASI
Copy link
Contributor

Choose a reason for hiding this comment

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

Imo not needed to state the obvious.,

@@ -0,0 +1,3 @@
# include("$(PORT_DIR)/variants/manifest.py")
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this file only has comments can it just be omitted?

#define HAS_SIGNAL 1
#endif

#if HAS_SIGNAL != 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Just #if HAS_SIGNAL is good enough?

PROG=build-wasi/micropython

# Unlike emscripten, WASI doesn't provide a way to scan GC roots
# like WASM locals. Hopefully --spill-pointers can workaround it.
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you try this with e.g. long-running scripts?

@stinos
Copy link
Contributor

stinos commented Apr 17, 2025

Apart from some cosmetic changes (see somments):

  • can you squash this so only relevant commits are left? It probably comes down to a commit which makes some PP defs in the unix port configurable, a commit which fixes some unix build stuff, and then one commit which adds the WASI variant.
  • it seems like almost none of the other ports/variants require a .sh file to build them, the goal is to have builds like make VARIANT=wasi WASI_SDK=/opt/wasi-sdk-25.0. So I wonder how much of build-wasi.sh is really needed: all build flags can probably go in mpconfigvariant.mk? That would be more consistent with how variants are used in general. And I think, not 100% sure though, you could also get those post-build ${WASM_OPT} call in there by changing the STRIP definition to first strip then call wasm-opt.
  • in general ports/variants shouldn't go stale and the first step in making that happen is having the CI build it so errors can be caught early. Which would come down to building the WASI variant as part of the github workflows (see tools/ci.sh and workflows/ports_unix.yml), and then running it, for instance have it run all unit tests. Would that be possible somehow?

@yamt
Copy link
Contributor Author

yamt commented Apr 18, 2025

@stinos thank you for comments. i will take my time to digest.

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

Successfully merging this pull request may close these issues.

3 participants