Skip to content

Commit

Permalink
FIXES bytecodealliance#137: make calling wasm from python 7x faster
Browse files Browse the repository at this point in the history
  • Loading branch information
muayyad-alsadi committed Mar 27, 2023
1 parent f00d32a commit 7bee159
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
30 changes: 30 additions & 0 deletions examples/simd_i8x16.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
how to call v128 SMID operations
for more details see https://github.com/WebAssembly/simd/blob/main/proposals/simd/SIMD.md#integer-addition
"""
import ctypes

from functools import partial
from wasmtime import Store, Module, Instance



store = Store()
module = Module(store.engine, """
(module
(func $add_v128 (param $a v128) (param $b v128) (result v128)
local.get $a
local.get $b
i8x16.add
)
(export "add_v128" (func $add_v128))
)
""")

instance = Instance(store, module, [])
vector_type = ctypes.c_uint8*16
add_v128 = partial(instance.exports(store)["add_v128"], store)
a=vector_type(*(i for i in range(16)))
b=vector_type(*(40+i for i in range(16)))
c=add_v128(a, b)
print([v for v in c])
30 changes: 26 additions & 4 deletions wasmtime/_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,34 @@
from typing import Callable, Optional, Generic, TypeVar, List, Union, Tuple, cast as cast_type, Sequence
from ._exportable import AsExtern
from ._store import Storelike
from ._bindings import wasmtime_val_raw_t
from ._bindings import wasmtime_val_raw_t, wasm_valtype_kind
from ._ffi import (
WASMTIME_I32,
WASMTIME_I64,
WASMTIME_F32,
WASMTIME_F64,
WASMTIME_V128,
WASMTIME_FUNCREF,
WASMTIME_EXTERNREF,
)


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

val_id2attr = {
WASMTIME_I32.value: 'i32',
WASMTIME_I64.value: 'i64',
WASMTIME_F32.value: 'f32',
WASMTIME_F64.value: 'f64',
WASMTIME_V128.value: 'v128',
WASMTIME_FUNCREF.value: 'funcref',
WASMTIME_EXTERNREF.value: 'externref',
}

def get_valtype_attr(ty):
return val_id2attr[wasm_valtype_kind(ty._ptr)]

class Func:
_func: ffi.wasmtime_func_t
Expand Down Expand Up @@ -84,9 +106,9 @@ def _init_call(self, ty):
ty_results = ty.results
params_n = len(ty_params)
results_n = len(ty_results)
self._params_str = (str(i) for i in ty_params)
self._results_str = (str(i) for i in ty_results)
self._results_str0 = str(ty_results[0]) if results_n else None
self._params_str = (get_valtype_attr(i) for i in ty_params)
self._results_str = (get_valtype_attr(i) for i in ty_results)
self._results_str0 = get_valtype_attr(ty_results[0]) if results_n else None
self._params_n = params_n
self._results_n = results_n
n = max(params_n, results_n)
Expand Down

0 comments on commit 7bee159

Please sign in to comment.