Optionally compile wasmtime-bench-api with wasi-nn and wasi-crypto (#2677)

This adds the ability to add feature flags (e.g. `--features wasi-nn`) when compiling `wasmtime-bench-api` to allow benchmarking Wasmtime with WASI proposals included. Note that due to https://github.com/rust-lang/cargo/issues/5364, these features are only available:
 - in the `crates/bench-api` directory, e.g. `pushd crates/bench-api; cargo build --features wasi-crypto`
 - or from the top-level project directory using `-Zpackage-features`, e.g. `OPENVINO_INSTALL_DIR=/opt/intel/openvino cargo +nightly build -p wasmtime-bench-api -Zpackage-features --features wasi-nn`
This commit is contained in:
Andrew Brown
2021-02-23 07:18:37 -08:00
committed by GitHub
parent 98d3e6823f
commit 96556ed700
3 changed files with 35 additions and 3 deletions

2
Cargo.lock generated
View File

@@ -3148,6 +3148,8 @@ dependencies = [
"wasi-cap-std-sync", "wasi-cap-std-sync",
"wasmtime", "wasmtime",
"wasmtime-wasi", "wasmtime-wasi",
"wasmtime-wasi-crypto",
"wasmtime-wasi-nn",
"wat", "wat",
] ]

View File

@@ -19,6 +19,8 @@ anyhow = "1.0"
shuffling-allocator = { version = "1.1.1", optional = true } shuffling-allocator = { version = "1.1.1", optional = true }
wasmtime = { path = "../wasmtime", default-features = false } wasmtime = { path = "../wasmtime", default-features = false }
wasmtime-wasi = { path = "../wasi" } wasmtime-wasi = { path = "../wasi" }
wasmtime-wasi-crypto = { path = "../wasi-crypto", optional = true }
wasmtime-wasi-nn = { path = "../wasi-nn", optional = true }
wasi-cap-std-sync = { path = "../wasi-common/cap-std-sync" } wasi-cap-std-sync = { path = "../wasi-common/cap-std-sync" }
cap-std = "0.13" cap-std = "0.13"
@@ -27,3 +29,6 @@ wat = "1.0"
[features] [features]
default = ["shuffling-allocator"] default = ["shuffling-allocator"]
wasi-crypto = ["wasmtime-wasi-crypto"]
wasi-nn = ["wasmtime-wasi-nn"]

View File

@@ -223,9 +223,34 @@ impl BenchState {
cx = cx.env("WASM_BENCH_USE_SMALL_WORKLOAD", &val)?; cx = cx.env("WASM_BENCH_USE_SMALL_WORKLOAD", &val)?;
} }
let cx = cx.build()?; Wasi::new(linker.store(), cx.build()?).add_to_linker(&mut linker)?;
let wasi = Wasi::new(&store, cx);
wasi.add_to_linker(&mut linker)?; #[cfg(feature = "wasi-nn")]
{
use std::cell::RefCell;
use std::rc::Rc;
use wasmtime_wasi_nn::{WasiNn, WasiNnCtx};
let wasi_nn = WasiNn::new(linker.store(), Rc::new(RefCell::new(WasiNnCtx::new()?)));
wasi_nn.add_to_linker(&mut linker)?;
}
#[cfg(feature = "wasi-crypto")]
{
use std::cell::RefCell;
use std::rc::Rc;
use wasmtime_wasi_crypto::{
WasiCryptoAsymmetricCommon, WasiCryptoCommon, WasiCryptoCtx, WasiCryptoSignatures,
WasiCryptoSymmetric,
};
let cx_crypto = Rc::new(RefCell::new(WasiCryptoCtx::new()));
WasiCryptoCommon::new(linker.store(), cx_crypto.clone()).add_to_linker(linker)?;
WasiCryptoAsymmetricCommon::new(linker.store(), cx_crypto.clone())
.add_to_linker(linker)?;
WasiCryptoSignatures::new(linker.store(), cx_crypto.clone()).add_to_linker(linker)?;
WasiCryptoSymmetric::new(linker.store(), cx_crypto).add_to_linker(linker)?;
}
Ok(Self { Ok(Self {
engine, engine,