mirror of
https://github.com/bytecodealliance/wasm-micro-runtime.git
synced 2025-05-08 20:56:13 +00:00

* Implement the first few SIMD opcodes for fast interpreter (v128.const, v128.any_true) (#3818) Tested on the following code: ``` (module (import "wasi_snapshot_preview1" "proc_exit" (func $proc_exit (param i32))) (memory (export "memory") 1) ;; WASI entry point (func $main (export "_start") v128.const i8x16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 v128.any_true if unreachable end v128.const i8x16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 v128.any_true i32.const 0 i32.eq if unreachable end i32.const 0 call $proc_exit ) ) ``` * implement POP_V128() This is to simplify the simd implementation for fast interpreter * Add all SIMD operations into wasm_interp_fast switch * Add V128 comparison operations Tested using ``` (module (import "wasi_snapshot_preview1" "proc_exit" (func $proc_exit (param i32))) (memory (export "memory") 1) (func $assert_true (param v128) local.get 0 v128.any_true i32.eqz if unreachable end ) (func $main (export "_start") ;; Test v128.not v128.const i8x16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 v128.not v128.const i8x16 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 i8x16.eq call $assert_true ;; Test v128.and v128.const i8x16 255 255 255 255 0 0 0 0 255 255 255 255 0 0 0 0 v128.const i8x16 255 255 0 0 255 255 0 0 255 255 0 0 255 255 0 0 v128.and v128.const i8x16 255 255 0 0 0 0 0 0 255 255 0 0 0 0 0 0 i8x16.eq call $assert_true ;; Test v128.andnot v128.const i8x16 255 255 255 255 0 0 0 0 255 255 255 255 0 0 0 0 v128.const i8x16 255 255 0 0 255 255 0 0 255 255 0 0 255 255 0 0 v128.andnot v128.const i8x16 0 0 255 255 0 0 0 0 0 0 255 255 0 0 0 0 i8x16.eq call $assert_true ;; Test v128.or v128.const i8x16 255 255 0 0 0 0 255 255 255 255 0 0 0 0 255 0 v128.const i8x16 0 0 255 255 255 255 0 0 0 0 255 255 255 255 0 0 v128.or v128.const i8x16 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 i8x16.eq call $assert_true ;; Test v128.xor v128.const i8x16 255 255 0 0 255 255 0 0 255 255 0 0 255 255 0 0 v128.const i8x16 255 255 255 255 0 0 0 0 255 255 255 255 0 0 0 0 v128.xor v128.const i8x16 0 0 255 255 255 255 0 0 0 0 255 255 255 255 0 0 i8x16.eq call $assert_true i32.const 0 call $proc_exit ) ) ``` * Add first NEON SIMD opcode implementations to fast interpreter (#3859) Add some implementations of SIMD opcodes using NEON instructions. Tested using: ```wast (module (import "wasi_snapshot_preview1" "proc_exit" (func $proc_exit (param i32))) (memory (export "memory") 1) (func $assert_true (param v128) local.get 0 v128.any_true i32.eqz if unreachable end ) (func $main (export "_start") i32.const 0 i32.const 32 memory.grow drop i32.const 0 v128.const i8x16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 v128.store i32.const 0 v128.load v128.const i8x16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 i8x16.eq call $assert_true i32.const 16 v128.const i8x16 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 v128.store i32.const 16 v128.load v128.const i8x16 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 i8x16.eq call $assert_true i32.const 0 v128.load v128.const i8x16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 i8x16.eq call $assert_true drop i32.const 0 i32.const 1 memory.grow drop i32.const 0 i64.const 0x7F80FF017E02FE80 i64.store i32.const 0 v128.load8x8_s v128.const i16x8 127 -128 -1 1 126 2 -2 -128 i16x8.eq call $assert_true i32.const 0 i64.const 0x80FE027E01FF807F i64.store i32.const 0 v128.load8x8_u v128.const i16x8 128 254 2 126 1 255 128 127 i16x8.eq call $assert_true i32.const 0 i64.const 0x8000FFFE7FFF0001 i64.store i32.const 0 v128.load16x4_s v128.const i32x4 -32768 -2 32767 1 i32x4.eq call $assert_true i32.const 0 i64.const 0x8000FFFE7FFF0001 i64.store i32.const 0 v128.load16x4_u v128.const i32x4 32768 65534 32767 1 i32x4.eq call $assert_true i32.const 0 i64.const 0x8000000000000001 i64.store i32.const 0 v128.load32x2_s v128.const i64x2 -2147483648 1 i64x2.eq call $assert_true i32.const 0 i64.const 0x8000000000000001 i64.store i32.const 0 v128.load32x2_u v128.const i64x2 2147483648 1 i64x2.eq call $assert_true call $proc_exit ) ) ``` * Emit imm for lane extract and replace (#3906) * Fix replacement value not being correct (#3919) * Implement load lanes opcodes for wasm (#3942) * Implement final SIMD opcodes: store lane (#4001) * Fix load/store (#4054) * Correctly use unsigned functions (#4055) * implement local and function calls for v128 in the fast interpreter * Fix splat opcodes, add V128 handling in preserve_referenced_local and reserve_block_ret * Fix incorrect memory overflow values + SIMD ifdefs * Fix load/load_splat macros * correct endif wasm loader * Update core/iwasm/interpreter/wasm_opcode.h * Fix spec tests when WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS is 0 * Resolve merge conflicts arising from main -> dev/simd_for_interp and implement fast interpreter const offset loader support for V128 * Enable SIMDe tests on CI * Document WAMR_BUILD_LIB_SIMDE --------- Co-authored-by: James Marsh <mrshnja@amazon.co.uk> Co-authored-by: jammar1 <108334558+jammar1@users.noreply.github.com> Co-authored-by: Maks Litskevich <makslit@amazon.com> Co-authored-by: Marcin Kolny <marcin.kolny@gmail.com> Co-authored-by: Wenyong Huang <wenyong.huang@intel.com>
227 lines
6.9 KiB
C
227 lines
6.9 KiB
C
/*
|
|
* Copyright (C) 2024 Amazon Inc. All rights reserved.
|
|
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
*/
|
|
#include "wasm_loader_common.h"
|
|
#include "bh_leb128.h"
|
|
#include "bh_log.h"
|
|
#if WASM_ENABLE_GC != 0
|
|
#include "../common/gc/gc_type.h"
|
|
#endif
|
|
|
|
void
|
|
wasm_loader_set_error_buf(char *error_buf, uint32 error_buf_size,
|
|
const char *string, bool is_aot)
|
|
{
|
|
if (error_buf != NULL) {
|
|
snprintf(error_buf, error_buf_size, "%s module load failed: %s",
|
|
is_aot ? "AOT" : "WASM", string);
|
|
}
|
|
}
|
|
|
|
#if WASM_ENABLE_MEMORY64 != 0
|
|
bool
|
|
check_memory64_flags_consistency(WASMModule *module, char *error_buf,
|
|
uint32 error_buf_size, bool is_aot)
|
|
{
|
|
uint32 i;
|
|
bool wasm64_flag, all_wasm64 = true, none_wasm64 = true;
|
|
|
|
for (i = 0; i < module->import_memory_count; ++i) {
|
|
wasm64_flag =
|
|
module->import_memories[i].u.memory.mem_type.flags & MEMORY64_FLAG;
|
|
all_wasm64 &= wasm64_flag;
|
|
none_wasm64 &= !wasm64_flag;
|
|
}
|
|
|
|
for (i = 0; i < module->memory_count; ++i) {
|
|
wasm64_flag = module->memories[i].flags & MEMORY64_FLAG;
|
|
all_wasm64 &= wasm64_flag;
|
|
none_wasm64 &= !wasm64_flag;
|
|
}
|
|
|
|
if (!(all_wasm64 || none_wasm64)) {
|
|
wasm_loader_set_error_buf(
|
|
error_buf, error_buf_size,
|
|
"inconsistent limits wasm64 flags for memory sections", is_aot);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
bool
|
|
wasm_memory_check_flags(const uint8 mem_flag, char *error_buf,
|
|
uint32 error_buf_size, bool is_aot)
|
|
{
|
|
/* Check whether certain features indicated by mem_flag are enabled in
|
|
* runtime */
|
|
if (mem_flag > MAX_PAGE_COUNT_FLAG) {
|
|
#if WASM_ENABLE_SHARED_MEMORY == 0
|
|
if (mem_flag & SHARED_MEMORY_FLAG) {
|
|
LOG_VERBOSE("shared memory flag was found, please enable shared "
|
|
"memory, lib-pthread or lib-wasi-threads");
|
|
wasm_loader_set_error_buf(error_buf, error_buf_size,
|
|
"invalid limits flags", is_aot);
|
|
return false;
|
|
}
|
|
#endif
|
|
#if WASM_ENABLE_MEMORY64 == 0
|
|
if (mem_flag & MEMORY64_FLAG) {
|
|
LOG_VERBOSE("memory64 flag was found, please enable memory64");
|
|
wasm_loader_set_error_buf(error_buf, error_buf_size,
|
|
"invalid limits flags", is_aot);
|
|
return false;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
if (mem_flag > MAX_PAGE_COUNT_FLAG + SHARED_MEMORY_FLAG + MEMORY64_FLAG) {
|
|
wasm_loader_set_error_buf(error_buf, error_buf_size,
|
|
"invalid limits flags", is_aot);
|
|
return false;
|
|
}
|
|
else if ((mem_flag & SHARED_MEMORY_FLAG)
|
|
&& !(mem_flag & MAX_PAGE_COUNT_FLAG)) {
|
|
wasm_loader_set_error_buf(error_buf, error_buf_size,
|
|
"shared memory must have maximum", is_aot);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool
|
|
wasm_table_check_flags(const uint8 table_flag, char *error_buf,
|
|
uint32 error_buf_size, bool is_aot)
|
|
{
|
|
/* Check whether certain features indicated by mem_flag are enabled in
|
|
* runtime */
|
|
if (table_flag > MAX_TABLE_SIZE_FLAG) {
|
|
if (table_flag & SHARED_TABLE_FLAG) {
|
|
wasm_loader_set_error_buf(error_buf, error_buf_size,
|
|
"tables cannot be shared", is_aot);
|
|
}
|
|
#if WASM_ENABLE_MEMORY64 == 0
|
|
if (table_flag & TABLE64_FLAG) {
|
|
wasm_loader_set_error_buf(error_buf, error_buf_size,
|
|
"invalid limits flags(table64 flag was "
|
|
"found, please enable memory64)",
|
|
is_aot);
|
|
return false;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
if (table_flag > MAX_TABLE_SIZE_FLAG + TABLE64_FLAG) {
|
|
wasm_loader_set_error_buf(error_buf, error_buf_size,
|
|
"invalid limits flags", is_aot);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/*
|
|
* compare with a bigger type set in `wasm_value_type_size_internal()`,
|
|
* this function will only cover global value type, function's param
|
|
* value type and function's result value type.
|
|
*
|
|
* please feel free to add more if there are more requirements
|
|
*/
|
|
bool
|
|
is_valid_value_type(uint8 type)
|
|
{
|
|
if (/* I32/I64/F32/F64, 0x7C to 0x7F */
|
|
(type >= VALUE_TYPE_F64 && type <= VALUE_TYPE_I32)
|
|
#if WASM_ENABLE_GC != 0
|
|
/* reference types, 0x65 to 0x70 */
|
|
|| wasm_is_type_reftype(type)
|
|
#elif WASM_ENABLE_REF_TYPES != 0
|
|
|| (type == VALUE_TYPE_FUNCREF || type == VALUE_TYPE_EXTERNREF)
|
|
#endif
|
|
#if WASM_ENABLE_SIMD != 0
|
|
|| type == VALUE_TYPE_V128 /* 0x7B */
|
|
#endif
|
|
)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
bool
|
|
is_valid_value_type_for_interpreter(uint8 value_type)
|
|
{
|
|
#if (WASM_ENABLE_WAMR_COMPILER == 0) && (WASM_ENABLE_JIT == 0) \
|
|
&& (WASM_ENABLE_FAST_INTERP == 0)
|
|
/*
|
|
* Note: regardless of WASM_ENABLE_SIMD, our interpreters don't have
|
|
* SIMD implemented. It's safer to reject v128, especially for the
|
|
* fast interpreter.
|
|
*/
|
|
if (value_type == VALUE_TYPE_V128)
|
|
return false;
|
|
#endif
|
|
return is_valid_value_type(value_type);
|
|
}
|
|
|
|
bool
|
|
is_valid_func_type(const WASMFuncType *func_type)
|
|
{
|
|
unsigned i;
|
|
for (i = 0;
|
|
i < (unsigned)(func_type->param_count + func_type->result_count);
|
|
i++) {
|
|
if (!is_valid_value_type(func_type->types[i]))
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/*
|
|
* Indices are represented as a u32.
|
|
*/
|
|
bool
|
|
is_indices_overflow(uint32 import, uint32 other, char *error_buf,
|
|
uint32 error_buf_size)
|
|
{
|
|
if (import > UINT32_MAX - other) {
|
|
snprintf(error_buf, error_buf_size,
|
|
"too many items in the index space(%" PRIu32 "+%" PRIu32 ").",
|
|
import, other);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool
|
|
read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
|
|
uint64 *p_result, char *error_buf, uint32 error_buf_size)
|
|
{
|
|
size_t offset = 0;
|
|
bh_leb_read_status_t status =
|
|
bh_leb_read(*p_buf, buf_end, maxbits, sign, p_result, &offset);
|
|
|
|
switch (status) {
|
|
case BH_LEB_READ_SUCCESS:
|
|
*p_buf += offset;
|
|
return true;
|
|
case BH_LEB_READ_TOO_LONG:
|
|
wasm_loader_set_error_buf(error_buf, error_buf_size,
|
|
"integer representation too long", false);
|
|
return false;
|
|
case BH_LEB_READ_OVERFLOW:
|
|
wasm_loader_set_error_buf(error_buf, error_buf_size,
|
|
"integer too large", false);
|
|
return false;
|
|
case BH_LEB_READ_UNEXPECTED_END:
|
|
wasm_loader_set_error_buf(error_buf, error_buf_size,
|
|
"unexpected end", false);
|
|
return false;
|
|
default:
|
|
bh_assert(false);
|
|
return false;
|
|
}
|
|
}
|