From f496950001c47724867912d863858db0a81b4309 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Wed, 5 Aug 2020 14:00:01 -0700 Subject: [PATCH] [WebAssembly] Fix types in wasm_simd128.h and add tests 47f7174ffa changed the types used in the Wasm SIMD builtin functions, but not all of their uses in wasm_simd128.h were updated. This commit fixes wasm_simd128.h and adds tests to make sure similar problems do not pass uncaught in the future. Differential Revision: https://reviews.llvm.org/D85347 --- clang/lib/Headers/wasm_simd128.h | 44 ++++++++++++++--------------- clang/test/Headers/wasm.c | 48 ++++++++++++++++++++++++++++++++ clang/test/Headers/wasm.cpp | 10 +++++++ 3 files changed, 80 insertions(+), 22 deletions(-) create mode 100644 clang/test/Headers/wasm.c create mode 100644 clang/test/Headers/wasm.cpp diff --git a/clang/lib/Headers/wasm_simd128.h b/clang/lib/Headers/wasm_simd128.h index b15264607d93..ac88516ac924 100644 --- a/clang/lib/Headers/wasm_simd128.h +++ b/clang/lib/Headers/wasm_simd128.h @@ -279,7 +279,7 @@ static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i8x16_splat(int8_t __a) { (__builtin_wasm_extract_lane_s_i8x16((__i8x16)(__a), __i)) #define wasm_u8x16_extract_lane(__a, __i) \ - (__builtin_wasm_extract_lane_u_i8x16((__i8x16)(__a), __i)) + (__builtin_wasm_extract_lane_u_i8x16((__u8x16)(__a), __i)) #define wasm_i8x16_replace_lane(__a, __i, __b) \ ((v128_t)__builtin_wasm_replace_lane_i8x16((__i8x16)(__a), __i, __b)) @@ -292,7 +292,7 @@ static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i16x8_splat(int16_t __a) { (__builtin_wasm_extract_lane_s_i16x8((__i16x8)(__a), __i)) #define wasm_u16x8_extract_lane(__a, __i) \ - (__builtin_wasm_extract_lane_u_i16x8((__i16x8)(__a), __i)) + (__builtin_wasm_extract_lane_u_i16x8((__u16x8)(__a), __i)) #define wasm_i16x8_replace_lane(__a, __i, __b) \ ((v128_t)__builtin_wasm_replace_lane_i16x8((__i16x8)(__a), __i, __b)) @@ -622,8 +622,8 @@ wasm_i8x16_add_saturate(v128_t __a, v128_t __b) { static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u8x16_add_saturate(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_add_saturate_u_i8x16((__i8x16)__a, - (__i8x16)__b); + return (v128_t)__builtin_wasm_add_saturate_u_i8x16((__u8x16)__a, + (__u8x16)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i8x16_sub(v128_t __a, @@ -639,8 +639,8 @@ wasm_i8x16_sub_saturate(v128_t __a, v128_t __b) { static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u8x16_sub_saturate(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_sub_saturate_u_i8x16((__i8x16)__a, - (__i8x16)__b); + return (v128_t)__builtin_wasm_sub_saturate_u_i8x16((__u8x16)__a, + (__u8x16)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i8x16_min(v128_t __a, @@ -650,7 +650,7 @@ static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i8x16_min(v128_t __a, static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u8x16_min(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_min_u_i8x16((__i8x16)__a, (__i8x16)__b); + return (v128_t)__builtin_wasm_min_u_i8x16((__u8x16)__a, (__u8x16)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i8x16_max(v128_t __a, @@ -660,12 +660,12 @@ static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i8x16_max(v128_t __a, static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u8x16_max(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_max_u_i8x16((__i8x16)__a, (__i8x16)__b); + return (v128_t)__builtin_wasm_max_u_i8x16((__u8x16)__a, (__u8x16)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u8x16_avgr(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_avgr_u_i8x16((__i8x16)__a, (__i8x16)__b); + return (v128_t)__builtin_wasm_avgr_u_i8x16((__u8x16)__a, (__u8x16)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i16x8_abs(v128_t __a) { @@ -712,8 +712,8 @@ wasm_i16x8_add_saturate(v128_t __a, v128_t __b) { static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u16x8_add_saturate(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_add_saturate_u_i16x8((__i16x8)__a, - (__i16x8)__b); + return (v128_t)__builtin_wasm_add_saturate_u_i16x8((__u16x8)__a, + (__u16x8)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i16x8_sub(v128_t __a, @@ -729,8 +729,8 @@ wasm_i16x8_sub_saturate(v128_t __a, v128_t __b) { static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u16x8_sub_saturate(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_sub_saturate_u_i16x8((__i16x8)__a, - (__i16x8)__b); + return (v128_t)__builtin_wasm_sub_saturate_u_i16x8((__u16x8)__a, + (__u16x8)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i16x8_mul(v128_t __a, @@ -745,7 +745,7 @@ static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i16x8_min(v128_t __a, static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u16x8_min(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_min_u_i16x8((__i16x8)__a, (__i16x8)__b); + return (v128_t)__builtin_wasm_min_u_i16x8((__u16x8)__a, (__u16x8)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i16x8_max(v128_t __a, @@ -755,12 +755,12 @@ static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i16x8_max(v128_t __a, static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u16x8_max(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_max_u_i16x8((__i16x8)__a, (__i16x8)__b); + return (v128_t)__builtin_wasm_max_u_i16x8((__u16x8)__a, (__u16x8)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u16x8_avgr(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_avgr_u_i16x8((__i16x8)__a, (__i16x8)__b); + return (v128_t)__builtin_wasm_avgr_u_i16x8((__u16x8)__a, (__u16x8)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i32x4_abs(v128_t __a) { @@ -816,7 +816,7 @@ static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i32x4_min(v128_t __a, static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u32x4_min(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_min_u_i32x4((__i32x4)__a, (__i32x4)__b); + return (v128_t)__builtin_wasm_min_u_i32x4((__u32x4)__a, (__u32x4)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i32x4_max(v128_t __a, @@ -826,7 +826,7 @@ static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i32x4_max(v128_t __a, static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u32x4_max(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_max_u_i32x4((__i32x4)__a, (__i32x4)__b); + return (v128_t)__builtin_wasm_max_u_i32x4((__u32x4)__a, (__u32x4)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_i64x2_neg(v128_t __a) { @@ -1077,8 +1077,8 @@ wasm_i8x16_narrow_i16x8(v128_t __a, v128_t __b) { static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u8x16_narrow_i16x8(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_narrow_u_i8x16_i16x8((__i16x8)__a, - (__i16x8)__b); + return (v128_t)__builtin_wasm_narrow_u_i8x16_i16x8((__u16x8)__a, + (__u16x8)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS @@ -1089,8 +1089,8 @@ wasm_i16x8_narrow_i32x4(v128_t __a, v128_t __b) { static __inline__ v128_t __DEFAULT_FN_ATTRS wasm_u16x8_narrow_i32x4(v128_t __a, v128_t __b) { - return (v128_t)__builtin_wasm_narrow_u_i16x8_i32x4((__i32x4)__a, - (__i32x4)__b); + return (v128_t)__builtin_wasm_narrow_u_i16x8_i32x4((__u32x4)__a, + (__u32x4)__b); } static __inline__ v128_t __DEFAULT_FN_ATTRS diff --git a/clang/test/Headers/wasm.c b/clang/test/Headers/wasm.c new file mode 100644 index 000000000000..521511659530 --- /dev/null +++ b/clang/test/Headers/wasm.c @@ -0,0 +1,48 @@ +// RUN: %clang_cc1 -triple wasm32-unknown-unknown -target-feature +simd128 -fsyntax-only -ffreestanding %s -verify +// RUN: %clang_cc1 -triple wasm32-unknown-unknown -target-feature +simd128 -fsyntax-only -ffreestanding -flax-vector-conversions=none %s -verify +// RUN: %clang_cc1 -triple wasm32-unknown-unknown -target-feature +simd128 -fsyntax-only -ffreestanding -x c++ %s -verify +// expected-no-diagnostics + +#if defined(__wasm__) && defined(__wasm_simd128__) + +#include + +// Test that macros are correct as well +int main() { + v128_t v; + v = wasm_i8x16_const(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); + v = wasm_i16x8_const(0, 1, 2, 3, 4, 5, 6, 7); + v = wasm_i32x4_const(0, 1, 2, 3); + v = wasm_i64x2_const(0, 1); + v = wasm_f32x4_const(0.0, 1.0, 2.0, 3.0); + v = wasm_f64x2_const(0.0, 1.0); + + int8_t i8 = wasm_i8x16_extract_lane(v, 0); + uint8_t u8 = wasm_u8x16_extract_lane(v, 15); + v = wasm_i8x16_replace_lane(v, 0, 42); + + int16_t i16 = wasm_i16x8_extract_lane(v, 0); + uint16_t u16 = wasm_u16x8_extract_lane(v, 7); + v = wasm_i16x8_replace_lane(v, 0, 42); + + int32_t i32 = wasm_i32x4_extract_lane(v, 0); + v = wasm_i32x4_replace_lane(v, 0, 42); + + int64_t i64 = wasm_i64x2_extract_lane(v, 0); + v = wasm_i64x2_replace_lane(v, 0, 42); + + float f32 = wasm_f32x4_extract_lane(v, 0); + v = wasm_f32x4_replace_lane(v, 0, 42.0); + + double f64 = wasm_f64x2_extract_lane(v, 0); + v = wasm_f64x2_replace_lane(v, 0, 42.0); + + wasm_v8x16_shuffle(v, v, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); + wasm_v16x8_shuffle(v, v, 0, 1, 2, 3, 4, 5, 6, 7); + wasm_v32x4_shuffle(v, v, 0, 1, 2, 3); + wasm_v64x2_shuffle(v, v, 0, 1); + + return 0; +} + +#endif diff --git a/clang/test/Headers/wasm.cpp b/clang/test/Headers/wasm.cpp new file mode 100644 index 000000000000..4024f4a295a7 --- /dev/null +++ b/clang/test/Headers/wasm.cpp @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -triple wasm32-unknown-unknown -target-feature +simd128 -fsyntax-only -ffreestanding %s -verify +// expected-no-diagnostics + +#if defined(__wasm__) && defined(__wasm_simd128__) + +extern "C++" { +#include +} + +#endif