Skip to content

Commit

Permalink
Support optimizing for size for wasm-bench (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 authored Feb 18, 2025
1 parent cddad99 commit c5a16c5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/wasm-bench/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[target.thumbv7em-none-eabi]
runner = "probe-rs run --chip=nRF52840_xxAA"
runner = "../../scripts/wrapper.sh probe-rs run --chip=nRF52840_xxAA"
rustflags = ["-Clink-arg=-Tlink.x"]
4 changes: 4 additions & 0 deletions crates/wasm-bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ codegen-units = 1
lto = true
panic = "abort"

[profile.release-size]
inherits = "release"
opt-level = "z"

[lints]
clippy.literal-string-with-formatting-args = "allow"
clippy.mod-module-files = "warn"
Expand Down
29 changes: 13 additions & 16 deletions crates/wasm-bench/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

fn main() {
let out = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
let target = if std::env::var_os("CARGO_FEATURE_TARGET_LINUX").is_some() {
Target::Linux
} else if std::env::var_os("CARGO_FEATURE_TARGET_NORDIC").is_some() {
Expand All @@ -38,28 +33,30 @@ fn main() {
} else {
panic!("one of runtime-{{base,wasm3,wasmi,wasmtime}} must be enabled")
};
let out = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
let memory = match target {
Target::Linux => None,
Target::Nordic => Some(include_bytes!("memory-nordic.x").as_slice()),
Target::Riscv => Some(include_bytes!("memory-riscv.x").as_slice()),
Target::Nordic => Some("memory-nordic.x"),
Target::Riscv => Some("memory-riscv.x"),
};
if let Some(memory) = memory {
println!("cargo:rerun-if-changed={memory}");
std::fs::copy(memory, out.join("memory.x")).unwrap();
println!("cargo:rustc-link-search={}", out.display());
File::create(out.join("memory.x")).unwrap().write_all(memory).unwrap();
}
let module = if runtime == Runtime::Wasmtime && target.is_embedded() {
const PATH: &str = "../../third_party/wasm3/wasm-coremark/coremark-minimal.wasm";
println!("cargo:rerun-if-changed={PATH}");
let mut module = std::fs::read(PATH).unwrap();
if runtime == Runtime::Wasmtime && target.is_embedded() {
let mut config = wasmtime::Config::new();
config.target("pulley32").unwrap();
let engine = wasmtime::Engine::new(&config).unwrap();
&engine.precompile_module(WASM).unwrap()
} else {
WASM
};
std::fs::write(out.join("module.bin"), module).unwrap();
module = engine.precompile_module(&module).unwrap();
}
println!("cargo:warning=module size is {} bytes", module.len());
std::fs::write(out.join("module.bin"), &module).unwrap();
}

const WASM: &[u8] = include_bytes!("../../third_party/wasm3/wasm-coremark/coremark-minimal.wasm");

#[derive(Clone, Copy, PartialEq, Eq)]
enum Target {
Linux,
Expand Down
14 changes: 12 additions & 2 deletions crates/wasm-bench/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,25 @@ case "$2" in
*) e "Unsupported runtime: $2" ;;
esac

case "$3" in
perf) PROFILE=--release ;;
size) PROFILE=--profile=release-size ;;
*) e "Unsupported profile: $3" ;;
esac

# See test.sh for supported (and tested) combinations.
case $1-$2 in
*-base|linux-*|nordic-wasmi|nordic-wasmtime) ;;
*) e "Unsupported combination: $1 $2" ;;
esac

FEATURES=--features=target-$1,runtime-$2
shift 2
set -- --release $TARGET $FEATURES "$@"
BUILD_STD='-Zbuild-std=core,alloc -Zbuild-std-features=panic_immediate_abort'
[ $3 = size ] && BUILD_STD="$BUILD_STD,optimize_for_size"
shift 3
set -- $PROFILE $BUILD_STD $TARGET $FEATURES "$@"

WASEFIRE_WRAPPER_EXEC=n ../../scripts/wrapper.sh probe-rs
WASEFIRE_WRAPPER_EXEC=n ../../scripts/wrapper.sh cargo-size
[ -z "$TARGET" ] || x ../../scripts/wrapper.sh cargo-size "$@"
x cargo run "$@"

0 comments on commit c5a16c5

Please # to comment.