Index: include/clang/Basic/BuiltinsWebAssembly.def =================================================================== --- include/clang/Basic/BuiltinsWebAssembly.def +++ include/clang/Basic/BuiltinsWebAssembly.def @@ -39,4 +39,14 @@ BUILTIN(__builtin_wasm_atomic_wait_i64, "iLLi*LLiLLi", "n") BUILTIN(__builtin_wasm_atomic_notify, "Uii*i", "n") +// SIMD builtins +BUILTIN(__builtin_wasm_extract_lane_s_i8x16, "iV16cIi", "ncV:128:") +BUILTIN(__builtin_wasm_extract_lane_u_i8x16, "iV16cIi", "ncV:128:") +BUILTIN(__builtin_wasm_extract_lane_s_i16x8, "iV8sIi", "ncV:128:") +BUILTIN(__builtin_wasm_extract_lane_u_i16x8, "iV8sIi", "ncV:128:") +BUILTIN(__builtin_wasm_extract_lane_i32x4, "iV4iIi", "ncV:128:") +BUILTIN(__builtin_wasm_extract_lane_i64x2, "WiV2WiIi", "ncV:128:") +BUILTIN(__builtin_wasm_extract_lane_f32x4, "fV4fIi", "ncV:128:") +BUILTIN(__builtin_wasm_extract_lane_f64x2, "dV2dIi", "ncV:128:") + #undef BUILTIN Index: lib/CodeGen/CGBuiltin.cpp =================================================================== --- lib/CodeGen/CGBuiltin.cpp +++ lib/CodeGen/CGBuiltin.cpp @@ -12427,6 +12427,28 @@ Value *Callee = CGM.getIntrinsic(Intrinsic::wasm_atomic_notify); return Builder.CreateCall(Callee, {Addr, Count}); } + case WebAssembly::BI__builtin_wasm_extract_lane_s_i8x16: + case WebAssembly::BI__builtin_wasm_extract_lane_s_i16x8: { + Value *Vec = EmitScalarExpr(E->getArg(0)); + Value *Lane = EmitScalarExpr(E->getArg(1)); + Value *Extract = Builder.CreateExtractElement(Vec, Lane); + return Builder.CreateSExt(Extract, ConvertType(E->getType())); + } + case WebAssembly::BI__builtin_wasm_extract_lane_u_i8x16: + case WebAssembly::BI__builtin_wasm_extract_lane_u_i16x8: { + Value *Vec = EmitScalarExpr(E->getArg(0)); + Value *Lane = EmitScalarExpr(E->getArg(1)); + Value *Extract = Builder.CreateExtractElement(Vec, Lane); + return Builder.CreateZExt(Extract, ConvertType(E->getType())); + } + case WebAssembly::BI__builtin_wasm_extract_lane_i32x4: + case WebAssembly::BI__builtin_wasm_extract_lane_i64x2: + case WebAssembly::BI__builtin_wasm_extract_lane_f32x4: + case WebAssembly::BI__builtin_wasm_extract_lane_f64x2: { + Value *Vec = EmitScalarExpr(E->getArg(0)); + Value *Lane = EmitScalarExpr(E->getArg(1)); + return Builder.CreateExtractElement(Vec, Lane); + } default: return nullptr; Index: test/CodeGen/builtins-wasm.c =================================================================== --- test/CodeGen/builtins-wasm.c +++ test/CodeGen/builtins-wasm.c @@ -1,7 +1,21 @@ -// RUN: %clang_cc1 -triple wasm32-unknown-unknown -O3 -emit-llvm -o - %s \ -// RUN: | FileCheck %s -check-prefix=WEBASSEMBLY32 -// RUN: %clang_cc1 -triple wasm64-unknown-unknown -O3 -emit-llvm -o - %s \ -// RUN: | FileCheck %s -check-prefix=WEBASSEMBLY64 +// RUN: %clang_cc1 -triple wasm32-unknown-unknown -fno-lax-vector-conversions \ +// RUN: -O3 -emit-llvm -o - %s \ +// RUN: | FileCheck %s -check-prefixes WEBASSEMBLY,WEBASSEMBLY32 +// RUN: %clang_cc1 -triple wasm64-unknown-unknown -fno-lax-vector-conversions \ +// RUN: -O3 -emit-llvm -o - %s \ +// RUN: | FileCheck %s -check-prefixes WEBASSEMBLY,WEBASSEMBLY64 + +// SIMD convenience types +typedef char i8x16 __attribute((vector_size(16))); +typedef short i16x8 __attribute((vector_size(16))); +typedef int i32x4 __attribute((vector_size(16))); +typedef long long i64x2 __attribute((vector_size(16))); +typedef unsigned char u8x16 __attribute((vector_size(16))); +typedef unsigned short u16x8 __attribute((vector_size(16))); +typedef unsigned int u32x4 __attribute((vector_size(16))); +typedef unsigned long long u64x2 __attribute((vector_size(16))); +typedef float f32x4 __attribute((vector_size(16))); +typedef double f64x2 __attribute((vector_size(16))); __SIZE_TYPE__ f0(void) { return __builtin_wasm_memory_size(0); @@ -68,3 +82,55 @@ // WEBASSEMBLY32: call i32 @llvm.wasm.atomic.notify(i32* %{{.*}}, i32 %{{.*}}) // WEBASSEMBLY64: call i32 @llvm.wasm.atomic.notify(i32* %{{.*}}, i32 %{{.*}}) } + +int f11(i8x16 v) { + return __builtin_wasm_extract_lane_s_i8x16(v, 13); + // WEBASSEMBLY: extractelement <16 x i8> %v, i32 13 + // WEBASSEMBLY-NEXT: sext + // WEBASSEMBLY-NEXT: ret +} + +int f12(i8x16 v) { + return __builtin_wasm_extract_lane_u_i8x16(v, 13); + // WEBASSEMBLY: extractelement <16 x i8> %v, i32 13 + // WEBASSEMBLY-NEXT: zext + // WEBASSEMBLY-NEXT: ret +} + +int f13(i16x8 v) { + return __builtin_wasm_extract_lane_s_i16x8(v, 7); + // WEBASSEMBLY: extractelement <8 x i16> %v, i32 7 + // WEBASSEMBLY-NEXT: sext + // WEBASSEMBLY-NEXT: ret +} + +int f14(i16x8 v) { + return __builtin_wasm_extract_lane_u_i16x8(v, 7); + // WEBASSEMBLY: extractelement <8 x i16> %v, i32 7 + // WEBASSEMBLY-NEXT: zext + // WEBASSEMBLY-NEXT: ret +} + +int f15(i32x4 v) { + return __builtin_wasm_extract_lane_i32x4(v, 3); + // WEBASSEMBLY: extractelement <4 x i32> %v, i32 3 + // WEBASSEMBLY-NEXT: ret +} + +long long f16(i64x2 v) { + return __builtin_wasm_extract_lane_i64x2(v, 1); + // WEBASSEMBLY: extractelement <2 x i64> %v, i32 1 + // WEBASSEMBLY-NEXT: ret +} + +float f17(f32x4 v) { + return __builtin_wasm_extract_lane_f32x4(v, 3); + // WEBASSEMBLY: extractelement <4 x float> %v, i32 3 + // WEBASSEMBLY-NEXT: ret +} + +double f18(f64x2 v) { + return __builtin_wasm_extract_lane_f64x2(v, 1); + // WEBASSEMBLY: extractelement <2 x double> %v, i32 1 + // WEBASSEMBLY-NEXT: ret +}