Skip to content

Add wasi p2 example using rustc native p2 target#344

Draft
ludfjig wants to merge 1 commit intomainfrom
native_p2
Draft

Add wasi p2 example using rustc native p2 target#344
ludfjig wants to merge 1 commit intomainfrom
native_p2

Conversation

@ludfjig
Copy link
Contributor

@ludfjig ludfjig commented Jan 23, 2026

No description provided.

@ludfjig ludfjig added the kind/enhancement New feature or request label Jan 23, 2026
@ludfjig ludfjig requested a review from Copilot January 23, 2026 23:25
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a Monte Carlo Pi estimation example demonstrating the use of the native wasm32-wasip2 Rust target with wit-bindgen (as an alternative to cargo-component). The example showcases host-guest interaction where the guest component depends on the host to provide random numbers for the Monte Carlo simulation.

Changes:

  • Adds a new wasip2_guest crate that builds a WASI P2 component using native wasm32-wasip2 target
  • Implements a Monte Carlo Pi estimator as the guest component that imports a random number interface from the host
  • Adds host example monte_carlo_example that provides random number generation to the guest and runs the estimation

Reviewed changes

Copilot reviewed 6 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/wasip2_guest/wit/monte-carlo.wit WIT interface definition for the Monte Carlo estimator with random number import
src/wasip2_guest/src/lib.rs Guest component implementation of Monte Carlo Pi estimation in no_std mode
src/wasip2_guest/Cargo.toml Package configuration for the WASI P2 guest component
src/wasip2_guest/Cargo.lock Dependency lock file for the standalone guest workspace
src/hyperlight_wasm/examples/monte_carlo_example/main.rs Host example that provides RNG and runs the Monte Carlo estimation
src/hyperlight_wasm/Cargo.toml Adds monte_carlo_example entry and rand 0.8 dependency
Justfile Adds build and run targets for the Monte Carlo example
Cargo.lock Updates workspace dependencies including rand 0.8.5
.gitignore Broadens pattern to ignore all .wasm files instead of specific paths
Comments suppressed due to low confidence (1)

Justfile:67

  • The wasip2_guest crate should be included in the check target for consistency with other guest crates. Add a check command for wasip2_guest similar to how component_sample and rust_wasm_samples are checked.
check target=default-target:
    cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
    cd src/rust_wasm_samples  && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
    cd src/component_sample  && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
    cd src/wasm_runtime && cargo hyperlight check --profile={{ if target == "debug" {"dev"} else { target } }}
    cd src/hyperlight_wasm_macro && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
# Always build in release mode to avoid WASI dependencies (debug mode pulls in entire WASI for some reason)
rustup target add wasm32-wasip2
cd ./src/wasip2_guest && cargo build --lib --target wasm32-wasip2 --release
cargo run {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--features " + features } }} -p hyperlight-wasm-aot compile {{ if features =~ "gdb" {"--debug"} else {""} }} --component ./src/wasip2_guest/target/wasm32-wasip2/release/monte_carlo.wasm ./x64/release/monte_carlo.aot
Copy link
Contributor

Choose a reason for hiding this comment

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

should we update the existing to use wasip2 target?

hyperlight-wasm/Justfile

Lines 47 to 49 in 192a036

# use cargo component so we don't get all the wasi imports https://github.com/bytecodealliance/cargo-component?tab=readme-ov-file#relationship-with-wasm32-wasip2
# we also explicitly target wasm32-unknown-unknown since cargo component might try to pull in wasi imports https://github.com/bytecodealliance/cargo-component/issues/290
rustup target add wasm32-unknown-unknown
Looks like there were similar issues and now the custom wit works.

Copy link
Contributor Author

@ludfjig ludfjig Feb 3, 2026

Choose a reason for hiding this comment

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

Currently it only works in release mode, so I would say let's wait a bit longer. in debug profile, it still does bring in entire wasi

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

Labels

kind/enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants