diff --git a/clang/include/clang/Basic/BuiltinsWebAssembly.def b/clang/include/clang/Basic/BuiltinsWebAssembly.def --- a/clang/include/clang/Basic/BuiltinsWebAssembly.def +++ b/clang/include/clang/Basic/BuiltinsWebAssembly.def @@ -174,5 +174,10 @@ TARGET_BUILTIN(__builtin_wasm_relaxed_swizzle_i8x16, "V16ScV16ScV16Sc", "nc", "relaxed-simd") +TARGET_BUILTIN(__builtin_wasm_relaxed_min_f32x4, "V4fV4fV4f", "nc", "relaxed-simd") +TARGET_BUILTIN(__builtin_wasm_relaxed_max_f32x4, "V4fV4fV4f", "nc", "relaxed-simd") +TARGET_BUILTIN(__builtin_wasm_relaxed_min_f64x2, "V2dV2dV2d", "nc", "relaxed-simd") +TARGET_BUILTIN(__builtin_wasm_relaxed_max_f64x2, "V2dV2dV2d", "nc", "relaxed-simd") + #undef BUILTIN #undef TARGET_BUILTIN diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -18325,6 +18325,28 @@ Function *Callee = CGM.getIntrinsic(Intrinsic::wasm_relaxed_swizzle); return Builder.CreateCall(Callee, {Src, Indices}); } + case WebAssembly::BI__builtin_wasm_relaxed_min_f32x4: + case WebAssembly::BI__builtin_wasm_relaxed_max_f32x4: + case WebAssembly::BI__builtin_wasm_relaxed_min_f64x2: + case WebAssembly::BI__builtin_wasm_relaxed_max_f64x2: { + Value *LHS = EmitScalarExpr(E->getArg(0)); + Value *RHS = EmitScalarExpr(E->getArg(1)); + unsigned IntNo; + switch (BuiltinID) { + case WebAssembly::BI__builtin_wasm_relaxed_min_f32x4: + case WebAssembly::BI__builtin_wasm_relaxed_min_f64x2: + IntNo = Intrinsic::wasm_relaxed_min; + break; + case WebAssembly::BI__builtin_wasm_relaxed_max_f32x4: + case WebAssembly::BI__builtin_wasm_relaxed_max_f64x2: + IntNo = Intrinsic::wasm_relaxed_max; + break; + default: + llvm_unreachable("unexpected builtin ID"); + } + Function *Callee = CGM.getIntrinsic(IntNo, LHS->getType()); + return Builder.CreateCall(Callee, {LHS, RHS}); + } default: return nullptr; } diff --git a/clang/test/CodeGen/builtins-wasm.c b/clang/test/CodeGen/builtins-wasm.c --- a/clang/test/CodeGen/builtins-wasm.c +++ b/clang/test/CodeGen/builtins-wasm.c @@ -737,3 +737,31 @@ return __builtin_wasm_relaxed_swizzle_i8x16(x, y); // WEBASSEMBLY: call <16 x i8> @llvm.wasm.relaxed.swizzle(<16 x i8> %x, <16 x i8> %y) } + +f32x4 relaxed_min_f32x4(f32x4 a, f32x4 b) { + return __builtin_wasm_relaxed_min_f32x4(a, b); + // WEBASSEMBLY: call <4 x float> @llvm.wasm.relaxed.min.v4f32( + // WEBASSEMBLY-SAME: <4 x float> %a, <4 x float> %b) + // WEBASSEMBLY-NEXT: ret +} + +f32x4 relaxed_max_f32x4(f32x4 a, f32x4 b) { + return __builtin_wasm_relaxed_max_f32x4(a, b); + // WEBASSEMBLY: call <4 x float> @llvm.wasm.relaxed.max.v4f32( + // WEBASSEMBLY-SAME: <4 x float> %a, <4 x float> %b) + // WEBASSEMBLY-NEXT: ret +} + +f64x2 relaxed_min_f64x2(f64x2 a, f64x2 b) { + return __builtin_wasm_relaxed_min_f64x2(a, b); + // WEBASSEMBLY: call <2 x double> @llvm.wasm.relaxed.min.v2f64( + // WEBASSEMBLY-SAME: <2 x double> %a, <2 x double> %b) + // WEBASSEMBLY-NEXT: ret +} + +f64x2 relaxed_max_f64x2(f64x2 a, f64x2 b) { + return __builtin_wasm_relaxed_max_f64x2(a, b); + // WEBASSEMBLY: call <2 x double> @llvm.wasm.relaxed.max.v2f64( + // WEBASSEMBLY-SAME: <2 x double> %a, <2 x double> %b) + // WEBASSEMBLY-NEXT: ret +} diff --git a/llvm/include/llvm/IR/IntrinsicsWebAssembly.td b/llvm/include/llvm/IR/IntrinsicsWebAssembly.td --- a/llvm/include/llvm/IR/IntrinsicsWebAssembly.td +++ b/llvm/include/llvm/IR/IntrinsicsWebAssembly.td @@ -205,6 +205,15 @@ [llvm_v16i8_ty, llvm_v16i8_ty], [IntrNoMem, IntrSpeculatable]>; +def int_wasm_relaxed_min : + Intrinsic<[llvm_anyvector_ty], + [LLVMMatchType<0>, LLVMMatchType<0>], + [IntrNoMem, IntrSpeculatable]>; +def int_wasm_relaxed_max : + Intrinsic<[llvm_anyvector_ty], + [LLVMMatchType<0>, LLVMMatchType<0>], + [IntrNoMem, IntrSpeculatable]>; + //===----------------------------------------------------------------------===// // Thread-local storage intrinsics //===----------------------------------------------------------------------===// diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td b/llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td --- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td +++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td @@ -1372,3 +1372,23 @@ [(set (v16i8 V128:$dst), (int_wasm_relaxed_swizzle (v16i8 V128:$src), (v16i8 V128:$mask)))], "i8x16.relaxed_swizzle\t$dst, $src, $mask", "i8x16.relaxed_swizzle", 162>; + +//===----------------------------------------------------------------------===// +// Relaxed floating-point min and max. +//===----------------------------------------------------------------------===// + +multiclass SIMD_RELAXED_FMINMAX simdopMin, bits<32> simdopMax> { + defm RELAXED_FMIN_#vec : + RELAXED_I<(outs V128:$dst), (ins V128:$a, V128:$b), (outs), (ins), + [(set (vec.vt V128:$dst), (int_wasm_relaxed_min + (vec.vt V128:$a), (vec.vt V128:$b)))], + vec.prefix#".relaxed_min\t$dst, $a, $b", vec.prefix#".relaxed_min", simdopMin>; + defm RELAXED_FMAX_#vec : + RELAXED_I<(outs V128:$dst), (ins V128:$a, V128:$b), (outs), (ins), + [(set (vec.vt V128:$dst), (int_wasm_relaxed_max + (vec.vt V128:$a), (vec.vt V128:$b)))], + vec.prefix#".relaxed_max\t$dst, $a, $b", vec.prefix#".relaxed_max", simdopMax>; +} + +defm "" : SIMD_RELAXED_FMINMAX; +defm "" : SIMD_RELAXED_FMINMAX; diff --git a/llvm/test/CodeGen/WebAssembly/simd-intrinsics.ll b/llvm/test/CodeGen/WebAssembly/simd-intrinsics.ll --- a/llvm/test/CodeGen/WebAssembly/simd-intrinsics.ll +++ b/llvm/test/CodeGen/WebAssembly/simd-intrinsics.ll @@ -682,6 +682,30 @@ ret <4 x float> %v } +; CHECK-LABEL: relaxed_min_v4f32: +; CHECK-NEXT: .functype relaxed_min_v4f32 (v128, v128) -> (v128){{$}} +; CHECK-NEXT: f32x4.relaxed_min $push[[R:[0-9]+]]=, $0, $1{{$}} +; CHECK-NEXT: return $pop[[R]]{{$}} +declare <4 x float> @llvm.wasm.relaxed.min.v4f32(<4 x float>, <4 x float>) +define <4 x float> @relaxed_min_v4f32(<4 x float> %a, <4 x float> %b) { + %v = call <4 x float> @llvm.wasm.relaxed.min.v4f32( + <4 x float> %a, <4 x float> %b + ) + ret <4 x float> %v +} + +; CHECK-LABEL: relaxed_max_v4f32: +; CHECK-NEXT: .functype relaxed_max_v4f32 (v128, v128) -> (v128){{$}} +; CHECK-NEXT: f32x4.relaxed_max $push[[R:[0-9]+]]=, $0, $1{{$}} +; CHECK-NEXT: return $pop[[R]]{{$}} +declare <4 x float> @llvm.wasm.relaxed.max.v4f32(<4 x float>, <4 x float>) +define <4 x float> @relaxed_max_v4f32(<4 x float> %a, <4 x float> %b) { + %v = call <4 x float> @llvm.wasm.relaxed.max.v4f32( + <4 x float> %a, <4 x float> %b + ) + ret <4 x float> %v +} + ; ============================================================================== ; 2 x f64 ; ============================================================================== @@ -780,3 +804,27 @@ ) ret <2 x double> %v } + +; CHECK-LABEL: relaxed_min_v2f64: +; CHECK-NEXT: .functype relaxed_min_v2f64 (v128, v128) -> (v128){{$}} +; CHECK-NEXT: f64x2.relaxed_min $push[[R:[0-9]+]]=, $0, $1{{$}} +; CHECK-NEXT: return $pop[[R]]{{$}} +declare <2 x double> @llvm.wasm.relaxed.min.v2f64(<2 x double>, <2 x double>) +define <2 x double> @relaxed_min_v2f64(<2 x double> %a, <2 x double> %b) { + %v = call <2 x double> @llvm.wasm.relaxed.min.v2f64( + <2 x double> %a, <2 x double> %b + ) + ret <2 x double> %v +} + +; CHECK-LABEL: relaxed_max_v2f64: +; CHECK-NEXT: .functype relaxed_max_v2f64 (v128, v128) -> (v128){{$}} +; CHECK-NEXT: f64x2.relaxed_max $push[[R:[0-9]+]]=, $0, $1{{$}} +; CHECK-NEXT: return $pop[[R]]{{$}} +declare <2 x double> @llvm.wasm.relaxed.max.v2f64(<2 x double>, <2 x double>) +define <2 x double> @relaxed_max_v2f64(<2 x double> %a, <2 x double> %b) { + %v = call <2 x double> @llvm.wasm.relaxed.max.v2f64( + <2 x double> %a, <2 x double> %b + ) + ret <2 x double> %v +} diff --git a/llvm/test/MC/WebAssembly/simd-encodings.s b/llvm/test/MC/WebAssembly/simd-encodings.s --- a/llvm/test/MC/WebAssembly/simd-encodings.s +++ b/llvm/test/MC/WebAssembly/simd-encodings.s @@ -806,4 +806,16 @@ # CHECK: i8x16.relaxed_swizzle # encoding: [0xfd,0xa2,0x01] i8x16.relaxed_swizzle + # CHECK: f32x4.relaxed_min # encoding: [0xfd,0xb4,0x01] + f32x4.relaxed_min + + # CHECK: f32x4.relaxed_max # encoding: [0xfd,0xe2,0x01] + f32x4.relaxed_max + + # CHECK: f64x2.relaxed_min # encoding: [0xfd,0xd4,0x01] + f64x2.relaxed_min + + # CHECK: f64x2.relaxed_max # encoding: [0xfd,0xee,0x01] + f64x2.relaxed_max + end_function