Skip to content

Commit bff2507

Browse files
committed
perf: pass the tail-call handlers' Instruction in a register on arm64_32
On arm64_32 (watchOS) the Rust ABI passes the 8-byte Instruction by reference, so every dispatch stores it and the next handler loads it back. The C convention passes it in a register, and `C-unwind` still lets a host function's panic unwind. Other targets keep the Rust ABI.
1 parent 97fc951 commit bff2507

1 file changed

Lines changed: 57 additions & 24 deletions

File tree

‎crates/tinywasm/src/interpreter/executor/dispatch_become.rs‎

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,35 @@ use super::*;
33
struct Unbudgeted;
44
struct Bounded;
55

6-
type UnbudgetedHandler =
7-
for<'store, 'module> fn(&mut Executor<'store, 'module>, &[Instruction], usize, Instruction) -> ExecResult<()>;
8-
type BoundedHandler =
9-
for<'store, 'module> fn(&mut Executor<'store, 'module>, usize, Instruction, u32) -> ExecResult<()>;
6+
// Between two handlers only what the calling convention passes in registers stays out of memory.
7+
// On arm64_32 (watchOS) the Rust ABI passes an aggregate larger than a pointer, the 8-byte
8+
// `Instruction`, by reference, so every dispatch would store it and the next handler load it back.
9+
// The C convention passes it in a register; `C-unwind` still lets a host function's panic unwind.
10+
// Elsewhere the handlers keep the Rust ABI.
11+
macro_rules! handler_fn {
12+
($(#[$meta:meta])* fn $($rest:tt)*) => {
13+
#[cfg(all(target_arch = "aarch64", target_pointer_width = "32"))]
14+
#[allow(improper_ctypes_definitions)]
15+
$(#[$meta])* extern "C-unwind" fn $($rest)*
16+
#[cfg(not(all(target_arch = "aarch64", target_pointer_width = "32")))]
17+
$(#[$meta])* fn $($rest)*
18+
};
19+
}
20+
21+
macro_rules! handler_types {
22+
($($abi:literal)?) => {
23+
// Both sides are Rust, so the C convention's view of these types need not be FFI-safe.
24+
#[allow(improper_ctypes_definitions)]
25+
type UnbudgetedHandler =
26+
for<'store, 'module> $(extern $abi)? fn(&mut Executor<'store, 'module>, &[Instruction], usize, Instruction) -> ExecResult<()>;
27+
#[allow(improper_ctypes_definitions)]
28+
type BoundedHandler = for<'store, 'module> $(extern $abi)? fn(&mut Executor<'store, 'module>, usize, Instruction, u32) -> ExecResult<()>;
29+
};
30+
}
31+
#[cfg(all(target_arch = "aarch64", target_pointer_width = "32"))]
32+
handler_types!("C-unwind");
33+
#[cfg(not(all(target_arch = "aarch64", target_pointer_width = "32")))]
34+
handler_types!();
1035

1136
macro_rules! define_unbudgeted_tail_dispatch {
1237
($executor:ident, $instr_ptr:ident, $dispatch_next:ident, $dispatch_flow:ident;
@@ -21,7 +46,7 @@ macro_rules! define_unbudgeted_tail_dispatch {
2146
HANDLERS[opcode as usize]
2247
}
2348

24-
$(
49+
$(handler_fn! {
2550
#[allow(non_snake_case, unreachable_code, unused_imports, unused_macros, unused_variables)]
2651
fn $variant(
2752
$executor: &mut Executor<'_, '_>,
@@ -66,7 +91,7 @@ macro_rules! define_unbudgeted_tail_dispatch {
6691
$body;
6792
$dispatch_next!($instr_ptr + 1)
6893
}
69-
)*
94+
})*
7095
};
7196
}
7297

@@ -83,7 +108,7 @@ macro_rules! define_bounded_tail_dispatch {
83108
HANDLERS[opcode as usize]
84109
}
85110

86-
$(
111+
$(handler_fn! {
87112
#[allow(non_snake_case, unreachable_code, unused_imports, unused_macros, unused_variables)]
88113
fn $variant(
89114
$executor: &mut Executor<'_, '_>,
@@ -133,7 +158,7 @@ macro_rules! define_bounded_tail_dispatch {
133158
$body;
134159
$dispatch_next!($instr_ptr + 1)
135160
}
136-
)*
161+
})*
137162
};
138163
}
139164

@@ -143,16 +168,20 @@ impl Unbudgeted {
143168
// The handlers tail-call these cold paths instead of calling them: a call would make every
144169
// handler save a stack frame.
145170

146-
#[cold]
147-
#[inline(never)]
148-
fn handler_mismatch(_: &mut Executor<'_, '_>, _: &[Instruction], _: usize, _: Instruction) -> ExecResult<()> {
149-
unreachable!("instruction handler mismatch")
171+
handler_fn! {
172+
#[cold]
173+
#[inline(never)]
174+
fn handler_mismatch(_: &mut Executor<'_, '_>, _: &[Instruction], _: usize, _: Instruction) -> ExecResult<()> {
175+
unreachable!("instruction handler mismatch")
176+
}
150177
}
151178

152-
#[cold]
153-
#[inline(never)]
154-
fn invalid_instr_ptr(_: &mut Executor<'_, '_>, _: &[Instruction], instr_ptr: usize, _: Instruction) -> ExecResult<()> {
155-
unreachable!("instruction pointer {instr_ptr} out of range, this is a bug")
179+
handler_fn! {
180+
#[cold]
181+
#[inline(never)]
182+
fn invalid_instr_ptr(_: &mut Executor<'_, '_>, _: &[Instruction], instr_ptr: usize, _: Instruction) -> ExecResult<()> {
183+
unreachable!("instruction pointer {instr_ptr} out of range, this is a bug")
184+
}
156185
}
157186
}
158187

@@ -161,16 +190,20 @@ impl Bounded {
161190

162191
// Tail-called like `Unbudgeted`'s.
163192

164-
#[cold]
165-
#[inline(never)]
166-
fn handler_mismatch(_: &mut Executor<'_, '_>, _: usize, _: Instruction, _: u32) -> ExecResult<()> {
167-
unreachable!("instruction handler mismatch")
193+
handler_fn! {
194+
#[cold]
195+
#[inline(never)]
196+
fn handler_mismatch(_: &mut Executor<'_, '_>, _: usize, _: Instruction, _: u32) -> ExecResult<()> {
197+
unreachable!("instruction handler mismatch")
198+
}
168199
}
169200

170-
#[cold]
171-
#[inline(never)]
172-
fn invalid_instr_ptr(_: &mut Executor<'_, '_>, instr_ptr: usize, _: Instruction, _: u32) -> ExecResult<()> {
173-
unreachable!("instruction pointer {instr_ptr} out of range, this is a bug")
201+
handler_fn! {
202+
#[cold]
203+
#[inline(never)]
204+
fn invalid_instr_ptr(_: &mut Executor<'_, '_>, instr_ptr: usize, _: Instruction, _: u32) -> ExecResult<()> {
205+
unreachable!("instruction pointer {instr_ptr} out of range, this is a bug")
206+
}
174207
}
175208

176209
/// Runs up to `chunk_left` (at least 1) instructions from `executor.cf`.

0 commit comments

Comments
 (0)