Skip to content

Commit

Permalink
Add simd128 implementation to wasm demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ictrobot committed Sep 19, 2024
1 parent 08c5c24 commit feede2d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 26 deletions.
2 changes: 1 addition & 1 deletion crates/aoc_wasm/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<script src="./web.mjs" type="module"></script>
<link rel="modulepreload" href="./aoc.mjs" />
<link rel="modulepreload" href="./worker.mjs" />
<link rel="preload" href="./aoc.wasm" as="fetch" crossorigin />
<link rel="preload" href="./aoc-simd128.wasm" as="fetch" crossorigin />
</head>
<body class="is-flex is-flex-direction-column">
<nav class="navbar" role="navigation" aria-label="main navigation">
Expand Down
24 changes: 23 additions & 1 deletion crates/aoc_wasm/web/web.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
import {Aoc} from "./aoc.mjs";

const module = await WebAssembly.compileStreaming(fetch("./aoc.wasm"));
const MODULE_PATHS = [
"./aoc-simd128.wasm",
"./aoc.wasm",
];

let module;
for (const path of MODULE_PATHS) {
try {
module = await WebAssembly.compileStreaming(fetch(path));
console.log("Using " + path);
break;
} catch (err) {
if (err instanceof WebAssembly.CompileError) {
console.warn("Compiling " + path + " failed: " + err);
} else {
throw err;
}
}
}
if (module === undefined) {
throw new Error("Failed to load WebAssembly module");
}

const worker = new Worker("./worker.mjs", {type: "module"});
worker.postMessage(["init", module]);

Expand Down
1 change: 1 addition & 0 deletions crates/utils/src/multiversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ macro_rules! multiversion {
::std::sync::LazyLock::new(#[cfg_attr(target_family = "wasm", expect(unreachable_code))] || {
use $crate::multiversion::Version::*;

// Instant::now() isn't implemented in WebAssembly, so hardcode implementations
#[cfg(all(target_family = "wasm", target_feature = "simd128"))]
return Array256;
#[cfg(all(target_family = "wasm"))]
Expand Down
51 changes: 30 additions & 21 deletions crates/xtask/src/cmd/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,40 @@ pub fn main(args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>> {
delete_dir(&output)?;
}

run_cargo(&["clean", "--doc"])?;
run_cargo(&["doc", "--no-deps"])?;
run_cargo(&[
"build",
"-p",
"aoc_wasm",
"--lib",
"--target=wasm32-unknown-unknown",
"--release",
])?;

create_dir(&output)?;

let output_wasm = output.join("aoc.wasm");
copy_file(
repo_dir_path()
.join("target")
.join("wasm32-unknown-unknown")
.join("release")
.join("aoc_wasm.wasm"),
&output_wasm,
)?;
run_cargo(&["clean", "--doc"], &[])?;
run_cargo(&["doc", "--no-deps"], &[])?;

let mut rewrites = vec![];
add_rewrite(&mut rewrites, &output_wasm)?;
for (features, name) in [
("", "aoc.wasm"),
("-C target_feature=+simd128", "aoc-simd128.wasm"),
] {
run_cargo(
&[
"build",
"-p",
"aoc_wasm",
"--lib",
"--target=wasm32-unknown-unknown",
"--release",
],
&[("RUSTFLAGS", features)],
)?;

let output_wasm = output.join(name);
copy_file(
repo_dir_path()
.join("target")
.join("wasm32-unknown-unknown")
.join("release")
.join("aoc_wasm.wasm"),
&output_wasm,
)?;

add_rewrite(&mut rewrites, &output_wasm)?;
}

let web = repo_dir_path().join("crates").join("aoc_wasm").join("web");
for file in ["aoc.mjs", "worker.mjs", "web.mjs", "index.html"] {
Expand Down
19 changes: 16 additions & 3 deletions crates/xtask/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::error::Error;
use std::fmt::Write;
use std::fs::{create_dir_all, read_to_string, remove_dir_all, write};
use std::path::{Path, PathBuf};
use std::process::Command;
Expand Down Expand Up @@ -144,9 +145,21 @@ fn cargo_var(key: &str) -> String {
.unwrap_or_else(|_| panic!("expected {key} environment variable to be set by cargo"))
}

pub fn run_cargo(args: &[&str]) -> Result<(), Box<dyn Error>> {
println!("running cargo with {args:?}");
let status = Command::new(cargo_var("CARGO")).args(args).status()?;
pub fn run_cargo(args: &[&str], envs: &[(&str, &str)]) -> Result<(), Box<dyn Error>> {
println!(
"running \"{}cargo {}\"",
envs.iter().fold(String::new(), |mut acc, &(k, v)| {
write!(acc, "{k}='{v}' ").unwrap();
acc
}),
args.join(" "),
);

let status = Command::new(cargo_var("CARGO"))
.args(args)
.envs(envs.iter().copied())
.status()?;

if status.success() {
Ok(())
} else {
Expand Down

0 comments on commit feede2d

Please sign in to comment.