Index: include/clang/Basic/BuiltinsWebAssembly.def =================================================================== --- include/clang/Basic/BuiltinsWebAssembly.def +++ include/clang/Basic/BuiltinsWebAssembly.def @@ -56,4 +56,14 @@ BUILTIN(__builtin_wasm_replace_lane_f32x4, "V4fV4fIif", "nc") BUILTIN(__builtin_wasm_replace_lane_f64x2, "V2dV2dIid", "nc") +BUILTIN(__builtin_wasm_add_saturate_s_i8x16, "V16cV16cV16c", "nc") +BUILTIN(__builtin_wasm_add_saturate_u_i8x16, "V16cV16cV16c", "nc") +BUILTIN(__builtin_wasm_add_saturate_s_i16x8, "V8sV8sV8s", "nc") +BUILTIN(__builtin_wasm_add_saturate_u_i16x8, "V8sV8sV8s", "nc") + +BUILTIN(__builtin_wasm_sub_saturate_s_i8x16, "V16cV16cV16c", "nc") +BUILTIN(__builtin_wasm_sub_saturate_u_i8x16, "V16cV16cV16c", "nc") +BUILTIN(__builtin_wasm_sub_saturate_s_i16x8, "V8sV8sV8s", "nc") +BUILTIN(__builtin_wasm_sub_saturate_u_i16x8, "V8sV8sV8s", "nc") + #undef BUILTIN Index: lib/CodeGen/CGBuiltin.cpp =================================================================== --- lib/CodeGen/CGBuiltin.cpp +++ lib/CodeGen/CGBuiltin.cpp @@ -12485,6 +12485,40 @@ llvm_unreachable("unexpected builtin ID"); } } + case WebAssembly::BI__builtin_wasm_add_saturate_s_i8x16: + case WebAssembly::BI__builtin_wasm_add_saturate_u_i8x16: + case WebAssembly::BI__builtin_wasm_add_saturate_s_i16x8: + case WebAssembly::BI__builtin_wasm_add_saturate_u_i16x8: + case WebAssembly::BI__builtin_wasm_sub_saturate_s_i8x16: + case WebAssembly::BI__builtin_wasm_sub_saturate_u_i8x16: + case WebAssembly::BI__builtin_wasm_sub_saturate_s_i16x8: + case WebAssembly::BI__builtin_wasm_sub_saturate_u_i16x8: { + unsigned IntNo; + switch (BuiltinID) { + case WebAssembly::BI__builtin_wasm_add_saturate_s_i8x16: + case WebAssembly::BI__builtin_wasm_add_saturate_s_i16x8: + IntNo = Intrinsic::wasm_add_saturate_signed; + break; + case WebAssembly::BI__builtin_wasm_add_saturate_u_i8x16: + case WebAssembly::BI__builtin_wasm_add_saturate_u_i16x8: + IntNo = Intrinsic::wasm_add_saturate_unsigned; + break; + case WebAssembly::BI__builtin_wasm_sub_saturate_s_i8x16: + case WebAssembly::BI__builtin_wasm_sub_saturate_s_i16x8: + IntNo = Intrinsic::wasm_sub_saturate_signed; + break; + case WebAssembly::BI__builtin_wasm_sub_saturate_u_i8x16: + case WebAssembly::BI__builtin_wasm_sub_saturate_u_i16x8: + IntNo = Intrinsic::wasm_sub_saturate_unsigned; + break; + default: + llvm_unreachable("unexpected builtin ID"); + } + Value *LHS = EmitScalarExpr(E->getArg(0)); + Value *RHS = EmitScalarExpr(E->getArg(1)); + Value *Callee = CGM.getIntrinsic(IntNo, ConvertType(E->getType())); + return Builder.CreateCall(Callee, {LHS, RHS}); + } default: return nullptr; Index: test/CodeGen/builtins-wasm.c =================================================================== --- test/CodeGen/builtins-wasm.c +++ test/CodeGen/builtins-wasm.c @@ -172,3 +172,59 @@ // WEBASSEMBLY: insertelement <2 x double> %v, double %x, i32 1 // WEBASSEMBLY-NEXT: ret } + +i8x16 f25(i8x16 x, i8x16 y) { + return __builtin_wasm_add_saturate_s_i8x16(x, y); + // WEBASSEMBLY: call <16 x i8> @llvm.wasm.add.saturate.signed.v16i8( + // WEBASSEMBLY-SAME: <16 x i8> %x, <16 x i8> %y) + // WEBASSEMBLY-NEXT: ret +} + +i8x16 f26(i8x16 x, i8x16 y) { + return __builtin_wasm_add_saturate_u_i8x16(x, y); + // WEBASSEMBLY: call <16 x i8> @llvm.wasm.add.saturate.unsigned.v16i8( + // WEBASSEMBLY-SAME: <16 x i8> %x, <16 x i8> %y) + // WEBASSEMBLY-NEXT: ret +} + +i16x8 f27(i16x8 x, i16x8 y) { + return __builtin_wasm_add_saturate_s_i16x8(x, y); + // WEBASSEMBLY: call <8 x i16> @llvm.wasm.add.saturate.signed.v8i16( + // WEBASSEMBLY-SAME: <8 x i16> %x, <8 x i16> %y) + // WEBASSEMBLY-NEXT: ret +} + +i16x8 f28(i16x8 x, i16x8 y) { + return __builtin_wasm_add_saturate_u_i16x8(x, y); + // WEBASSEMBLY: call <8 x i16> @llvm.wasm.add.saturate.unsigned.v8i16( + // WEBASSEMBLY-SAME: <8 x i16> %x, <8 x i16> %y) + // WEBASSEMBLY-NEXT: ret +} + +i8x16 f29(i8x16 x, i8x16 y) { + return __builtin_wasm_sub_saturate_s_i8x16(x, y); + // WEBASSEMBLY: call <16 x i8> @llvm.wasm.sub.saturate.signed.v16i8( + // WEBASSEMBLY-SAME: <16 x i8> %x, <16 x i8> %y) + // WEBASSEMBLY-NEXT: ret +} + +i8x16 f30(i8x16 x, i8x16 y) { + return __builtin_wasm_sub_saturate_u_i8x16(x, y); + // WEBASSEMBLY: call <16 x i8> @llvm.wasm.sub.saturate.unsigned.v16i8( + // WEBASSEMBLY-SAME: <16 x i8> %x, <16 x i8> %y) + // WEBASSEMBLY-NEXT: ret +} + +i16x8 f31(i16x8 x, i16x8 y) { + return __builtin_wasm_sub_saturate_s_i16x8(x, y); + // WEBASSEMBLY: call <8 x i16> @llvm.wasm.sub.saturate.signed.v8i16( + // WEBASSEMBLY-SAME: <8 x i16> %x, <8 x i16> %y) + // WEBASSEMBLY-NEXT: ret +} + +i16x8 f32(i16x8 x, i16x8 y) { + return __builtin_wasm_sub_saturate_u_i16x8(x, y); + // WEBASSEMBLY: call <8 x i16> @llvm.wasm.sub.saturate.unsigned.v8i16( + // WEBASSEMBLY-SAME: <8 x i16> %x, <8 x i16> %y) + // WEBASSEMBLY-NEXT: ret +}