Skip to content

Commit 7bee159

Browse files
FIXES #137: make calling wasm from python 7x faster
1 parent f00d32a commit 7bee159

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

examples/simd_i8x16.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
how to call v128 SMID operations
3+
for more details see https://github.com/WebAssembly/simd/blob/main/proposals/simd/SIMD.md#integer-addition
4+
"""
5+
import ctypes
6+
7+
from functools import partial
8+
from wasmtime import Store, Module, Instance
9+
10+
11+
12+
store = Store()
13+
module = Module(store.engine, """
14+
(module
15+
(func $add_v128 (param $a v128) (param $b v128) (result v128)
16+
local.get $a
17+
local.get $b
18+
i8x16.add
19+
)
20+
(export "add_v128" (func $add_v128))
21+
)
22+
""")
23+
24+
instance = Instance(store, module, [])
25+
vector_type = ctypes.c_uint8*16
26+
add_v128 = partial(instance.exports(store)["add_v128"], store)
27+
a=vector_type(*(i for i in range(16)))
28+
b=vector_type(*(40+i for i in range(16)))
29+
c=add_v128(a, b)
30+
print([v for v in c])

wasmtime/_func.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,34 @@
77
from typing import Callable, Optional, Generic, TypeVar, List, Union, Tuple, cast as cast_type, Sequence
88
from ._exportable import AsExtern
99
from ._store import Storelike
10-
from ._bindings import wasmtime_val_raw_t
10+
from ._bindings import wasmtime_val_raw_t, wasm_valtype_kind
11+
from ._ffi import (
12+
WASMTIME_I32,
13+
WASMTIME_I64,
14+
WASMTIME_F32,
15+
WASMTIME_F64,
16+
WASMTIME_V128,
17+
WASMTIME_FUNCREF,
18+
WASMTIME_EXTERNREF,
19+
)
20+
1121

1222
T = TypeVar('T')
1323
FUNCTIONS: "Slab[Tuple]"
1424
LAST_EXCEPTION: Optional[Exception] = None
1525

26+
val_id2attr = {
27+
WASMTIME_I32.value: 'i32',
28+
WASMTIME_I64.value: 'i64',
29+
WASMTIME_F32.value: 'f32',
30+
WASMTIME_F64.value: 'f64',
31+
WASMTIME_V128.value: 'v128',
32+
WASMTIME_FUNCREF.value: 'funcref',
33+
WASMTIME_EXTERNREF.value: 'externref',
34+
}
35+
36+
def get_valtype_attr(ty):
37+
return val_id2attr[wasm_valtype_kind(ty._ptr)]
1638

1739
class Func:
1840
_func: ffi.wasmtime_func_t
@@ -84,9 +106,9 @@ def _init_call(self, ty):
84106
ty_results = ty.results
85107
params_n = len(ty_params)
86108
results_n = len(ty_results)
87-
self._params_str = (str(i) for i in ty_params)
88-
self._results_str = (str(i) for i in ty_results)
89-
self._results_str0 = str(ty_results[0]) if results_n else None
109+
self._params_str = (get_valtype_attr(i) for i in ty_params)
110+
self._results_str = (get_valtype_attr(i) for i in ty_results)
111+
self._results_str0 = get_valtype_attr(ty_results[0]) if results_n else None
90112
self._params_n = params_n
91113
self._results_n = results_n
92114
n = max(params_n, results_n)

0 commit comments

Comments
 (0)