Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- name: Run tests (wasm32-wasip1)
run: |
echo "Running wasm32-wasip1 tests with wasmtime "
cargo test --verbose --target wasm32-wasip1 --features wasmtime
cargo test --verbose --target wasm32-wasip1 --features wasmtime --release

- name: Check formatting
run: cargo fmt --all --check
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ trace = []

[dependencies]
anyhow = "1.0.86"
arrayvec = { version = "0.7", features = ["serde"] }
byteorder = "1.5.0"
clap = { version = "4.5.7", features = ["derive"] }
fancy-regex = "0.14.0"
Expand Down
78 changes: 78 additions & 0 deletions doc/folding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Operand Folding

Operand folding is a preprocessing optimization that identifies patterns of consecutive instructions that can be merged into a single operation. This eliminates intermediate register allocations and reduces dispatch overhead.

```
Before folding:
i32.const 42 ; r0 = 42
local.set 0 ; local[0] = r0

After folding:
i32.const 42 -> local[0] ; store 42 directly to local[0]
```

## Folding Types

### Source Folding

Folds constant values and local.get operations into consuming instructions.

```
Before:
i32.const 10 ; r0 = 10
i32.const 20 ; r1 = 20
i32.add ; r2 = r0 + r1

After:
i32.add (const 10), (const 20) -> r0
```

Supported source operands:
- `i32.const`, `i64.const`, `f32.const`, `f64.const`
- `local.get` (typed: i32, i64, f32, f64)

### Destination Folding

Folds `local.set` into the preceding instruction that produces the value.

```
Before:
i32.add ; r0 = a + b
local.set 0 ; local[0] = r0

After:
i32.add -> local[0] ; result directly to local
```

When destination folding is applied, the instruction uses `RegOrLocal::Local` instead of `RegOrLocal::Reg` for its destination.

### Address Folding (Memory Operations)

For memory load/store operations, folds constant addresses.

```
Before:
i32.const 100 ; r0 = 100 (address)
i32.load ; r1 = memory[r0]

After:
i32.load (addr: const 100) -> r1
```

## Implementation

Folding is performed during the preprocessing phase using a peek-ahead mechanism:

1. **Pending Operand Stack**: When a foldable source instruction (const, local.get) is encountered, it is pushed to a pending stack instead of generating a register instruction.

2. **Consumer Check**: When a consuming instruction is processed, it checks the pending stack for compatible operands.

3. **Destination Check**: After processing an instruction, the parser peeks ahead to check if the next instruction is `local.set`. If so, the destination is changed from register to local.

## Limitations

- Folding only occurs for immediately adjacent instructions
- Control flow instructions (block, loop, if) break folding chains
- Reference types (funcref, externref) are not folded
- Type mismatch between pending operand and consumer prevents folding

2 changes: 1 addition & 1 deletion src/execution/elem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl ElemAddr {
.map(|i| Ref::FuncAddr(funcs.get_by_idx(FuncIdx(*i as u32)).clone()))
.collect();
ElemAddr(Rc::new(RefCell::new(ElemInst {
_type_: type_.clone(),
_type_: *type_,
_elem: elem,
})))
}
Expand Down
64 changes: 41 additions & 23 deletions src/execution/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use super::module::*;
use super::value::{Val, WasiFuncAddr};
use crate::error::RuntimeError;
use crate::structure::{module::*, types::*};
use std::cell::{Ref, RefCell};
use std::cell::UnsafeCell;
use std::fmt::{self, Debug};
use std::rc::{Rc, Weak};

/// Reference-counted handle to a function instance.
/// Uses UnsafeCell for zero-cost access in the interpreter hot path.
/// Safety: WebAssembly execution is single-threaded and operations don't overlap.
#[derive(Clone)]
pub struct FuncAddr(Rc<RefCell<FuncInst>>);
pub struct FuncAddr(Rc<UnsafeCell<FuncInst>>);

/// Function instance variants: runtime (Wasm), host, or WASI.
pub enum FuncInst {
Expand All @@ -31,10 +33,9 @@ pub enum FuncInst {

impl Debug for FuncAddr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0.try_borrow() {
Ok(guard) => write!(f, "FuncAddr({:?})", *guard),
Err(_) => write!(f, "FuncAddr(<Locked>)"),
}
// Safety: Single-threaded access
let inst = unsafe { &*self.0.get() };
write!(f, "FuncAddr({:?})", inst)
}
}

Expand Down Expand Up @@ -74,7 +75,7 @@ impl Debug for FuncInst {
impl FuncAddr {
/// Allocates a placeholder function (replaced later during instantiation).
pub fn alloc_empty() -> FuncAddr {
FuncAddr(Rc::new(RefCell::new(FuncInst::RuntimeFunc {
FuncAddr(Rc::new(UnsafeCell::new(FuncInst::RuntimeFunc {
type_: FuncType {
params: Vec::new(),
results: Vec::new(),
Expand All @@ -93,7 +94,7 @@ impl FuncAddr {
/// Allocates a WASI function instance.
pub fn alloc_wasi(wasi_func_addr: WasiFuncAddr) -> FuncAddr {
let func_type = wasi_func_addr.func_type.to_func_type();
FuncAddr(Rc::new(RefCell::new(FuncInst::WasiFunc {
FuncAddr(Rc::new(UnsafeCell::new(FuncInst::WasiFunc {
type_: func_type,
wasi_func_addr,
})))
Expand All @@ -102,29 +103,38 @@ impl FuncAddr {
/// Replaces placeholder with actual function definition.
pub fn replace(&self, func: Func, module: Weak<ModuleInst>) {
let upgraded_module = module.upgrade().expect("Module weak ref expired");
let func_type = upgraded_module.types.get_by_idx(func.type_.clone()).clone();
let func_type = upgraded_module.types.get_by_idx(func.type_).clone();
drop(upgraded_module);

let new_inst = FuncInst::RuntimeFunc {
type_: func_type,
module: module,
code: func,
};
*self.0.borrow_mut() = new_inst;
// Safety: Single-threaded access, no overlapping borrows
unsafe {
*self.0.get() = new_inst;
}
}

/// Returns the function's type signature.
pub fn func_type(&self) -> FuncType {
match &*self.0.borrow() {
FuncInst::RuntimeFunc { type_, .. } => type_.clone(),
FuncInst::HostFunc { type_, .. } => type_.clone(),
FuncInst::WasiFunc { type_, .. } => type_.clone(),
/// Returns a reference to the function's type signature.
/// Zero-copy access - no allocation.
#[inline]
pub fn func_type(&self) -> &FuncType {
// Safety: Single-threaded access, no overlapping mutable access
let inst = unsafe { &*self.0.get() };
match inst {
FuncInst::RuntimeFunc { type_, .. } => type_,
FuncInst::HostFunc { type_, .. } => type_,
FuncInst::WasiFunc { type_, .. } => type_,
}
}

/// Extracts runtime function details if this is a Wasm function.
pub fn get_runtime_func_details(&self) -> Option<(FuncType, Weak<ModuleInst>, Func)> {
match &*self.0.borrow() {
// Safety: Single-threaded access
let inst = unsafe { &*self.0.get() };
match inst {
FuncInst::RuntimeFunc {
type_,
module,
Expand All @@ -141,15 +151,19 @@ impl FuncAddr {
FuncType,
Rc<dyn Fn(Vec<Val>) -> Result<Option<Val>, RuntimeError>>,
)> {
match &*self.0.borrow() {
// Safety: Single-threaded access
let inst = unsafe { &*self.0.get() };
match inst {
FuncInst::HostFunc { type_, host_code } => Some((type_.clone(), host_code.clone())),
_ => None,
}
}

/// Extracts WASI function details if this is a WASI function.
pub fn get_wasi_func_details(&self) -> Option<(FuncType, WasiFuncAddr)> {
match &*self.0.borrow() {
// Safety: Single-threaded access
let inst = unsafe { &*self.0.get() };
match inst {
FuncInst::WasiFunc {
type_,
wasi_func_addr,
Expand All @@ -158,13 +172,17 @@ impl FuncAddr {
}
}

/// Returns a borrow of the underlying function instance.
pub fn read_lock(&self) -> Ref<FuncInst> {
self.0.borrow()
/// Returns a reference to the underlying function instance.
/// # Safety
/// Caller must ensure no mutable access occurs during the lifetime of the reference.
#[inline]
pub fn read_lock(&self) -> &FuncInst {
// Safety: Single-threaded access, caller ensures no mutable access
unsafe { &*self.0.get() }
}

/// Returns a reference to the inner Rc.
pub fn get_rc(&self) -> &Rc<RefCell<FuncInst>> {
pub fn get_rc(&self) -> &Rc<UnsafeCell<FuncInst>> {
&self.0
}
}
2 changes: 1 addition & 1 deletion src/execution/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl GlobalAddr {
pub fn new(type_: &GlobalType, value: Val) -> GlobalAddr {
GlobalAddr {
global_inst: Rc::new(RefCell::new(GlobalInst {
_type_: type_.clone(),
_type_: *type_,
value: value,
})),
}
Expand Down
Loading