Skip to content
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

wasmtime: Implement Extended Constant Expressions proposal #8568

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,30 @@ fn main() -> anyhow::Result<()> {
// Skip running spec_testsuite tests if the submodule isn't checked
// out.
if spec_tests > 0 {
test_directory_module(out, "tests/spec_testsuite/proposals/memory64", strategy)?;
test_directory_module(
out,
"tests/spec_testsuite/proposals/extended-const",
strategy,
)?;
test_directory_module(
out,
"tests/spec_testsuite/proposals/function-references",
strategy,
)?;
test_directory_module(out, "tests/spec_testsuite/proposals/gc", strategy)?;
test_directory_module(out, "tests/spec_testsuite/proposals/memory64", strategy)?;
test_directory_module(
out,
"tests/spec_testsuite/proposals/multi-memory",
strategy,
)?;
test_directory_module(out, "tests/spec_testsuite/proposals/tail-call", strategy)?;
test_directory_module(out, "tests/spec_testsuite/proposals/threads", strategy)?;
test_directory_module(
out,
"tests/spec_testsuite/proposals/relaxed-simd",
strategy,
)?;
test_directory_module(out, "tests/spec_testsuite/proposals/tail-call", strategy)?;
} else {
println!(
"cargo:warning=The spec testsuite is disabled. To enable, run `git submodule \
Expand Down
12 changes: 12 additions & 0 deletions crates/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,12 @@ pub enum ConstOp {
F32Const(u32),
F64Const(u64),
V128Const(u128),
I32Add,
I64Add,
I32Sub,
I64Sub,
I32Mul,
I64Mul,
GlobalGet(GlobalIndex),
RefI31,
RefNull,
Expand All @@ -1343,6 +1349,12 @@ impl ConstOp {
O::F32Const { value } => Self::F32Const(value.bits()),
O::F64Const { value } => Self::F64Const(value.bits()),
O::V128Const { value } => Self::V128Const(u128::from_le_bytes(*value.bytes())),
O::I32Add => Self::I32Add,
O::I64Add => Self::I64Add,
O::I32Sub => Self::I32Sub,
O::I64Sub => Self::I64Sub,
O::I32Mul => Self::I32Mul,
O::I64Mul => Self::I64Mul,
O::RefNull { hty: _ } => Self::RefNull,
O::RefFunc { function_index } => Self::RefFunc(FuncIndex::from_u32(function_index)),
O::GlobalGet { global_index } => Self::GlobalGet(GlobalIndex::from_u32(global_index)),
Expand Down
15 changes: 15 additions & 0 deletions crates/wasmtime/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ impl Config {
ret.wasm_multi_value(true);
ret.wasm_bulk_memory(true);
ret.wasm_simd(true);
ret.wasm_extended_const(true);
ret.wasm_backtrace_details(WasmBacktraceDetails::Environment);

// This is on-by-default in `wasmparser` since it's a stage 4+ proposal
Expand Down Expand Up @@ -945,6 +946,20 @@ impl Config {
self
}

/// Configures whether the WebAssembly extended constant expressions
/// [proposal] will be enabled for compilation.
///
/// Note that Wasmtime may have bugs for this feature since it hasn't been
/// exercised much.
///
/// This is `false` by default.
///
/// [proposal]: https://github.com/webassembly/extended-const
pub fn wasm_extended_const(&mut self, enable: bool) -> &mut Self {
self.features.set(WasmFeatures::EXTENDED_CONST, enable);
self
}

/// Configures whether the WebAssembly component-model [proposal] will
/// be enabled for compilation.
///
Expand Down
45 changes: 35 additions & 10 deletions crates/wasmtime/src/runtime/vm/const_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::runtime::vm::{Instance, VMGcRef, ValRaw, I31};
use anyhow::{anyhow, bail, Result};
use smallvec::SmallVec;
use wasmtime_environ::{ConstExpr, FuncIndex, GlobalIndex, Module};
use wasmtime_environ::{ConstExpr, ConstOp, FuncIndex, GlobalIndex, Module};

/// An interpreter for const expressions.
///
Expand Down Expand Up @@ -64,15 +64,33 @@ impl ConstExprEvaluator {

for op in expr.ops() {
match op {
wasmtime_environ::ConstOp::I32Const(i) => self.stack.push(ValRaw::i32(*i)),
wasmtime_environ::ConstOp::I64Const(i) => self.stack.push(ValRaw::i64(*i)),
wasmtime_environ::ConstOp::F32Const(f) => self.stack.push(ValRaw::f32(*f)),
wasmtime_environ::ConstOp::F64Const(f) => self.stack.push(ValRaw::f64(*f)),
wasmtime_environ::ConstOp::V128Const(v) => self.stack.push(ValRaw::v128(*v)),
wasmtime_environ::ConstOp::GlobalGet(g) => self.stack.push(context.global_get(*g)?),
wasmtime_environ::ConstOp::RefNull => self.stack.push(ValRaw::null()),
wasmtime_environ::ConstOp::RefFunc(f) => self.stack.push(context.ref_func(*f)?),
wasmtime_environ::ConstOp::RefI31 => {
ConstOp::I32Const(i) => self.stack.push(ValRaw::i32(*i)),
ConstOp::I64Const(i) => self.stack.push(ValRaw::i64(*i)),
ConstOp::F32Const(f) => self.stack.push(ValRaw::f32(*f)),
ConstOp::F64Const(f) => self.stack.push(ValRaw::f64(*f)),
ConstOp::V128Const(v) => self.stack.push(ValRaw::v128(*v)),
ConstOp::I32Add => {
self.binop(|a, b| ValRaw::i32(a.get_i32().wrapping_add(b.get_i32())))?
}
ConstOp::I64Add => {
self.binop(|a, b| ValRaw::i64(a.get_i64().wrapping_add(b.get_i64())))?
}
ConstOp::I32Sub => {
self.binop(|a, b| ValRaw::i32(a.get_i32().wrapping_sub(b.get_i32())))?
}
ConstOp::I64Sub => {
self.binop(|a, b| ValRaw::i64(a.get_i64().wrapping_sub(b.get_i64())))?
}
ConstOp::I32Mul => {
self.binop(|a, b| ValRaw::i32(a.get_i32().wrapping_mul(b.get_i32())))?
}
ConstOp::I64Mul => {
self.binop(|a, b| ValRaw::i64(a.get_i64().wrapping_mul(b.get_i64())))?
}
ConstOp::GlobalGet(g) => self.stack.push(context.global_get(*g)?),
ConstOp::RefNull => self.stack.push(ValRaw::null()),
ConstOp::RefFunc(f) => self.stack.push(context.ref_func(*f)?),
ConstOp::RefI31 => {
let i = self.pop()?.get_i32();
let i31 = I31::wrapping_i32(i);
let raw = VMGcRef::from_i31(i31).as_raw_u32();
Expand All @@ -99,4 +117,11 @@ impl ConstExprEvaluator {
)
})
}

fn binop(&mut self, f: impl FnOnce(ValRaw, ValRaw) -> ValRaw) -> Result<()> {
let b = self.pop()?;
let a = self.pop()?;
self.stack.push(f(a, b));
Ok(())
}
}