Skip to content

Commit 78fe029

Browse files
tlivelyalexcrichton
authored andcommitted
[WebAssembly] Use standard intrinsics for f32x4 and f64x2 ops
Now that these instructions are no longer prototypes, we do not need to be careful about keeping them opt-in and can use the standard LLVM infrastructure for them. This commit removes the bespoke intrinsics we were using to represent these operations in favor of the corresponding target-independent intrinsics. The clang builtins are preserved because there is no standard way to easily represent these operations in C/C++. For consistency with the scalar codegen in the Wasm backend, the intrinsic used to represent {f32x4,f64x2}.nearest is @llvm.nearbyint even though @llvm.roundeven better captures the semantics of the underlying Wasm instruction. Replacing our use of @llvm.nearbyint with use of @llvm.roundeven is left to a potential future patch. Differential Revision: https://reviews.llvm.org/D100411
1 parent 9890f5e commit 78fe029

File tree

7 files changed

+37
-122
lines changed

7 files changed

+37
-122
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16819,19 +16819,19 @@ Value *CodeGenFunction::EmitWebAssemblyBuiltinExpr(unsigned BuiltinID,
1681916819
switch (BuiltinID) {
1682016820
case WebAssembly::BI__builtin_wasm_ceil_f32x4:
1682116821
case WebAssembly::BI__builtin_wasm_ceil_f64x2:
16822-
IntNo = Intrinsic::wasm_ceil;
16822+
IntNo = Intrinsic::ceil;
1682316823
break;
1682416824
case WebAssembly::BI__builtin_wasm_floor_f32x4:
1682516825
case WebAssembly::BI__builtin_wasm_floor_f64x2:
16826-
IntNo = Intrinsic::wasm_floor;
16826+
IntNo = Intrinsic::floor;
1682716827
break;
1682816828
case WebAssembly::BI__builtin_wasm_trunc_f32x4:
1682916829
case WebAssembly::BI__builtin_wasm_trunc_f64x2:
16830-
IntNo = Intrinsic::wasm_trunc;
16830+
IntNo = Intrinsic::trunc;
1683116831
break;
1683216832
case WebAssembly::BI__builtin_wasm_nearest_f32x4:
1683316833
case WebAssembly::BI__builtin_wasm_nearest_f64x2:
16834-
IntNo = Intrinsic::wasm_nearest;
16834+
IntNo = Intrinsic::nearbyint;
1683516835
break;
1683616836
default:
1683716837
llvm_unreachable("unexpected builtin ID");

clang/test/CodeGen/builtins-wasm.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -792,49 +792,49 @@ f64x2 pmax_f64x2(f64x2 x, f64x2 y) {
792792

793793
f32x4 ceil_f32x4(f32x4 x) {
794794
return __builtin_wasm_ceil_f32x4(x);
795-
// WEBASSEMBLY: call <4 x float> @llvm.wasm.ceil.v4f32(<4 x float> %x)
795+
// WEBASSEMBLY: call <4 x float> @llvm.ceil.v4f32(<4 x float> %x)
796796
// WEBASSEMBLY: ret
797797
}
798798

799799
f32x4 floor_f32x4(f32x4 x) {
800800
return __builtin_wasm_floor_f32x4(x);
801-
// WEBASSEMBLY: call <4 x float> @llvm.wasm.floor.v4f32(<4 x float> %x)
801+
// WEBASSEMBLY: call <4 x float> @llvm.floor.v4f32(<4 x float> %x)
802802
// WEBASSEMBLY: ret
803803
}
804804

805805
f32x4 trunc_f32x4(f32x4 x) {
806806
return __builtin_wasm_trunc_f32x4(x);
807-
// WEBASSEMBLY: call <4 x float> @llvm.wasm.trunc.v4f32(<4 x float> %x)
807+
// WEBASSEMBLY: call <4 x float> @llvm.trunc.v4f32(<4 x float> %x)
808808
// WEBASSEMBLY: ret
809809
}
810810

811811
f32x4 nearest_f32x4(f32x4 x) {
812812
return __builtin_wasm_nearest_f32x4(x);
813-
// WEBASSEMBLY: call <4 x float> @llvm.wasm.nearest.v4f32(<4 x float> %x)
813+
// WEBASSEMBLY: call <4 x float> @llvm.nearbyint.v4f32(<4 x float> %x)
814814
// WEBASSEMBLY: ret
815815
}
816816

817817
f64x2 ceil_f64x2(f64x2 x) {
818818
return __builtin_wasm_ceil_f64x2(x);
819-
// WEBASSEMBLY: call <2 x double> @llvm.wasm.ceil.v2f64(<2 x double> %x)
819+
// WEBASSEMBLY: call <2 x double> @llvm.ceil.v2f64(<2 x double> %x)
820820
// WEBASSEMBLY: ret
821821
}
822822

823823
f64x2 floor_f64x2(f64x2 x) {
824824
return __builtin_wasm_floor_f64x2(x);
825-
// WEBASSEMBLY: call <2 x double> @llvm.wasm.floor.v2f64(<2 x double> %x)
825+
// WEBASSEMBLY: call <2 x double> @llvm.floor.v2f64(<2 x double> %x)
826826
// WEBASSEMBLY: ret
827827
}
828828

829829
f64x2 trunc_f64x2(f64x2 x) {
830830
return __builtin_wasm_trunc_f64x2(x);
831-
// WEBASSEMBLY: call <2 x double> @llvm.wasm.trunc.v2f64(<2 x double> %x)
831+
// WEBASSEMBLY: call <2 x double> @llvm.trunc.v2f64(<2 x double> %x)
832832
// WEBASSEMBLY: ret
833833
}
834834

835835
f64x2 nearest_f64x2(f64x2 x) {
836836
return __builtin_wasm_nearest_f64x2(x);
837-
// WEBASSEMBLY: call <2 x double> @llvm.wasm.nearest.v2f64(<2 x double> %x)
837+
// WEBASSEMBLY: call <2 x double> @llvm.nearbyint.v2f64(<2 x double> %x)
838838
// WEBASSEMBLY: ret
839839
}
840840

llvm/include/llvm/IR/IntrinsicsWebAssembly.td

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -183,26 +183,6 @@ def int_wasm_pmax :
183183
[LLVMMatchType<0>, LLVMMatchType<0>],
184184
[IntrNoMem, IntrSpeculatable]>;
185185

186-
// TODO: Replace these instrinsics with normal ISel patterns once the
187-
// rounding instructions are merged to the proposal
188-
// (https://github.com/WebAssembly/simd/pull/232).
189-
def int_wasm_ceil :
190-
Intrinsic<[llvm_anyvector_ty],
191-
[LLVMMatchType<0>],
192-
[IntrNoMem, IntrSpeculatable]>;
193-
def int_wasm_floor :
194-
Intrinsic<[llvm_anyvector_ty],
195-
[LLVMMatchType<0>],
196-
[IntrNoMem, IntrSpeculatable]>;
197-
def int_wasm_trunc :
198-
Intrinsic<[llvm_anyvector_ty],
199-
[LLVMMatchType<0>],
200-
[IntrNoMem, IntrSpeculatable]>;
201-
def int_wasm_nearest :
202-
Intrinsic<[llvm_anyvector_ty],
203-
[LLVMMatchType<0>],
204-
[IntrNoMem, IntrSpeculatable]>;
205-
206186
// TODO: Replace these intrinsic with normal ISel patterns once the
207187
// load_zero instructions are merged to the proposal.
208188
def int_wasm_load32_zero :

llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ WebAssemblyTargetLowering::WebAssemblyTargetLowering(
180180
setOperationAction(Op, T, Legal);
181181

182182
// Expand float operations supported for scalars but not SIMD
183-
for (auto Op : {ISD::FCEIL, ISD::FFLOOR, ISD::FTRUNC, ISD::FNEARBYINT,
184-
ISD::FCOPYSIGN, ISD::FLOG, ISD::FLOG2, ISD::FLOG10,
183+
for (auto Op : {ISD::FCOPYSIGN, ISD::FLOG, ISD::FLOG2, ISD::FLOG10,
185184
ISD::FEXP, ISD::FEXP2, ISD::FRINT})
186185
for (auto T : {MVT::v4f32, MVT::v2f64})
187186
setOperationAction(Op, T, Expand);

llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,14 +1044,14 @@ defm NEG : SIMDUnaryFP<fneg, "neg", 225>;
10441044
defm SQRT : SIMDUnaryFP<fsqrt, "sqrt", 227>;
10451045

10461046
// Rounding: ceil, floor, trunc, nearest
1047-
defm CEIL : SIMDUnary<F32x4, int_wasm_ceil, "ceil", 0x67>;
1048-
defm FLOOR : SIMDUnary<F32x4, int_wasm_floor, "floor", 0x68>;
1049-
defm TRUNC: SIMDUnary<F32x4, int_wasm_trunc, "trunc", 0x69>;
1050-
defm NEAREST: SIMDUnary<F32x4, int_wasm_nearest, "nearest", 0x6a>;
1051-
defm CEIL : SIMDUnary<F64x2, int_wasm_ceil, "ceil", 0x74>;
1052-
defm FLOOR : SIMDUnary<F64x2, int_wasm_floor, "floor", 0x75>;
1053-
defm TRUNC: SIMDUnary<F64x2, int_wasm_trunc, "trunc", 0x7a>;
1054-
defm NEAREST: SIMDUnary<F64x2, int_wasm_nearest, "nearest", 0x94>;
1047+
defm CEIL : SIMDUnary<F32x4, fceil, "ceil", 0x67>;
1048+
defm FLOOR : SIMDUnary<F32x4, ffloor, "floor", 0x68>;
1049+
defm TRUNC: SIMDUnary<F32x4, ftrunc, "trunc", 0x69>;
1050+
defm NEAREST: SIMDUnary<F32x4, fnearbyint, "nearest", 0x6a>;
1051+
defm CEIL : SIMDUnary<F64x2, fceil, "ceil", 0x74>;
1052+
defm FLOOR : SIMDUnary<F64x2, ffloor, "floor", 0x75>;
1053+
defm TRUNC: SIMDUnary<F64x2, ftrunc, "trunc", 0x7a>;
1054+
defm NEAREST: SIMDUnary<F64x2, fnearbyint, "nearest", 0x94>;
10551055

10561056
//===----------------------------------------------------------------------===//
10571057
// Floating-point binary arithmetic

llvm/test/CodeGen/WebAssembly/simd-intrinsics.ll

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -722,39 +722,39 @@ define <4 x float> @pmax_v4f32(<4 x float> %a, <4 x float> %b) {
722722
; CHECK-NEXT: .functype ceil_v4f32 (v128) -> (v128){{$}}
723723
; CHECK-NEXT: f32x4.ceil $push[[R:[0-9]+]]=, $0{{$}}
724724
; CHECK-NEXT: return $pop[[R]]{{$}}
725-
declare <4 x float> @llvm.wasm.ceil.v4f32(<4 x float>)
725+
declare <4 x float> @llvm.ceil.v4f32(<4 x float>)
726726
define <4 x float> @ceil_v4f32(<4 x float> %a) {
727-
%v = call <4 x float> @llvm.wasm.ceil.v4f32(<4 x float> %a)
727+
%v = call <4 x float> @llvm.ceil.v4f32(<4 x float> %a)
728728
ret <4 x float> %v
729729
}
730730

731731
; CHECK-LABEL: floor_v4f32:
732732
; CHECK-NEXT: .functype floor_v4f32 (v128) -> (v128){{$}}
733733
; CHECK-NEXT: f32x4.floor $push[[R:[0-9]+]]=, $0{{$}}
734734
; CHECK-NEXT: return $pop[[R]]{{$}}
735-
declare <4 x float> @llvm.wasm.floor.v4f32(<4 x float>)
735+
declare <4 x float> @llvm.floor.v4f32(<4 x float>)
736736
define <4 x float> @floor_v4f32(<4 x float> %a) {
737-
%v = call <4 x float> @llvm.wasm.floor.v4f32(<4 x float> %a)
737+
%v = call <4 x float> @llvm.floor.v4f32(<4 x float> %a)
738738
ret <4 x float> %v
739739
}
740740

741741
; CHECK-LABEL: trunc_v4f32:
742742
; CHECK-NEXT: .functype trunc_v4f32 (v128) -> (v128){{$}}
743743
; CHECK-NEXT: f32x4.trunc $push[[R:[0-9]+]]=, $0{{$}}
744744
; CHECK-NEXT: return $pop[[R]]{{$}}
745-
declare <4 x float> @llvm.wasm.trunc.v4f32(<4 x float>)
745+
declare <4 x float> @llvm.trunc.v4f32(<4 x float>)
746746
define <4 x float> @trunc_v4f32(<4 x float> %a) {
747-
%v = call <4 x float> @llvm.wasm.trunc.v4f32(<4 x float> %a)
747+
%v = call <4 x float> @llvm.trunc.v4f32(<4 x float> %a)
748748
ret <4 x float> %v
749749
}
750750

751751
; CHECK-LABEL: nearest_v4f32:
752752
; CHECK-NEXT: .functype nearest_v4f32 (v128) -> (v128){{$}}
753753
; CHECK-NEXT: f32x4.nearest $push[[R:[0-9]+]]=, $0{{$}}
754754
; CHECK-NEXT: return $pop[[R]]{{$}}
755-
declare <4 x float> @llvm.wasm.nearest.v4f32(<4 x float>)
755+
declare <4 x float> @llvm.nearbyint.v4f32(<4 x float>)
756756
define <4 x float> @nearest_v4f32(<4 x float> %a) {
757-
%v = call <4 x float> @llvm.wasm.nearest.v4f32(<4 x float> %a)
757+
%v = call <4 x float> @llvm.nearbyint.v4f32(<4 x float> %a)
758758
ret <4 x float> %v
759759
}
760760

@@ -807,39 +807,39 @@ define <2 x double> @pmax_v2f64(<2 x double> %a, <2 x double> %b) {
807807
; CHECK-NEXT: .functype ceil_v2f64 (v128) -> (v128){{$}}
808808
; CHECK-NEXT: f64x2.ceil $push[[R:[0-9]+]]=, $0{{$}}
809809
; CHECK-NEXT: return $pop[[R]]{{$}}
810-
declare <2 x double> @llvm.wasm.ceil.v2f64(<2 x double>)
810+
declare <2 x double> @llvm.ceil.v2f64(<2 x double>)
811811
define <2 x double> @ceil_v2f64(<2 x double> %a) {
812-
%v = call <2 x double> @llvm.wasm.ceil.v2f64(<2 x double> %a)
812+
%v = call <2 x double> @llvm.ceil.v2f64(<2 x double> %a)
813813
ret <2 x double> %v
814814
}
815815

816816
; CHECK-LABEL: floor_v2f64:
817817
; CHECK-NEXT: .functype floor_v2f64 (v128) -> (v128){{$}}
818818
; CHECK-NEXT: f64x2.floor $push[[R:[0-9]+]]=, $0{{$}}
819819
; CHECK-NEXT: return $pop[[R]]{{$}}
820-
declare <2 x double> @llvm.wasm.floor.v2f64(<2 x double>)
820+
declare <2 x double> @llvm.floor.v2f64(<2 x double>)
821821
define <2 x double> @floor_v2f64(<2 x double> %a) {
822-
%v = call <2 x double> @llvm.wasm.floor.v2f64(<2 x double> %a)
822+
%v = call <2 x double> @llvm.floor.v2f64(<2 x double> %a)
823823
ret <2 x double> %v
824824
}
825825

826826
; CHECK-LABEL: trunc_v2f64:
827827
; CHECK-NEXT: .functype trunc_v2f64 (v128) -> (v128){{$}}
828828
; CHECK-NEXT: f64x2.trunc $push[[R:[0-9]+]]=, $0{{$}}
829829
; CHECK-NEXT: return $pop[[R]]{{$}}
830-
declare <2 x double> @llvm.wasm.trunc.v2f64(<2 x double>)
830+
declare <2 x double> @llvm.trunc.v2f64(<2 x double>)
831831
define <2 x double> @trunc_v2f64(<2 x double> %a) {
832-
%v = call <2 x double> @llvm.wasm.trunc.v2f64(<2 x double> %a)
832+
%v = call <2 x double> @llvm.trunc.v2f64(<2 x double> %a)
833833
ret <2 x double> %v
834834
}
835835

836836
; CHECK-LABEL: nearest_v2f64:
837837
; CHECK-NEXT: .functype nearest_v2f64 (v128) -> (v128){{$}}
838838
; CHECK-NEXT: f64x2.nearest $push[[R:[0-9]+]]=, $0{{$}}
839839
; CHECK-NEXT: return $pop[[R]]{{$}}
840-
declare <2 x double> @llvm.wasm.nearest.v2f64(<2 x double>)
840+
declare <2 x double> @llvm.nearbyint.v2f64(<2 x double>)
841841
define <2 x double> @nearest_v2f64(<2 x double> %a) {
842-
%v = call <2 x double> @llvm.wasm.nearest.v2f64(<2 x double> %a)
842+
%v = call <2 x double> @llvm.nearbyint.v2f64(<2 x double> %a)
843843
ret <2 x double> %v
844844
}
845845

llvm/test/CodeGen/WebAssembly/simd-unsupported.ll

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -366,38 +366,6 @@ define <2 x i64> @rotr_v2i64(<2 x i64> %x, <2 x i64> %y) {
366366
; 4 x f32
367367
; ==============================================================================
368368

369-
; CHECK-LABEL: ceil_v4f32:
370-
; CHECK: f32.ceil
371-
declare <4 x float> @llvm.ceil.v4f32(<4 x float>)
372-
define <4 x float> @ceil_v4f32(<4 x float> %x) {
373-
%v = call <4 x float> @llvm.ceil.v4f32(<4 x float> %x)
374-
ret <4 x float> %v
375-
}
376-
377-
; CHECK-LABEL: floor_v4f32:
378-
; CHECK: f32.floor
379-
declare <4 x float> @llvm.floor.v4f32(<4 x float>)
380-
define <4 x float> @floor_v4f32(<4 x float> %x) {
381-
%v = call <4 x float> @llvm.floor.v4f32(<4 x float> %x)
382-
ret <4 x float> %v
383-
}
384-
385-
; CHECK-LABEL: trunc_v4f32:
386-
; CHECK: f32.trunc
387-
declare <4 x float> @llvm.trunc.v4f32(<4 x float>)
388-
define <4 x float> @trunc_v4f32(<4 x float> %x) {
389-
%v = call <4 x float> @llvm.trunc.v4f32(<4 x float> %x)
390-
ret <4 x float> %v
391-
}
392-
393-
; CHECK-LABEL: nearbyint_v4f32:
394-
; CHECK: f32.nearest
395-
declare <4 x float> @llvm.nearbyint.v4f32(<4 x float>)
396-
define <4 x float> @nearbyint_v4f32(<4 x float> %x) {
397-
%v = call <4 x float> @llvm.nearbyint.v4f32(<4 x float> %x)
398-
ret <4 x float> %v
399-
}
400-
401369
; CHECK-LABEL: copysign_v4f32:
402370
; CHECK: f32.copysign
403371
declare <4 x float> @llvm.copysign.v4f32(<4 x float>, <4 x float>)
@@ -498,38 +466,6 @@ define <4 x float> @round_v4f32(<4 x float> %x) {
498466
; 2 x f64
499467
; ==============================================================================
500468

501-
; CHECK-LABEL: ceil_v2f64:
502-
; CHECK: f64.ceil
503-
declare <2 x double> @llvm.ceil.v2f64(<2 x double>)
504-
define <2 x double> @ceil_v2f64(<2 x double> %x) {
505-
%v = call <2 x double> @llvm.ceil.v2f64(<2 x double> %x)
506-
ret <2 x double> %v
507-
}
508-
509-
; CHECK-LABEL: floor_v2f64:
510-
; CHECK: f64.floor
511-
declare <2 x double> @llvm.floor.v2f64(<2 x double>)
512-
define <2 x double> @floor_v2f64(<2 x double> %x) {
513-
%v = call <2 x double> @llvm.floor.v2f64(<2 x double> %x)
514-
ret <2 x double> %v
515-
}
516-
517-
; CHECK-LABEL: trunc_v2f64:
518-
; CHECK: f64.trunc
519-
declare <2 x double> @llvm.trunc.v2f64(<2 x double>)
520-
define <2 x double> @trunc_v2f64(<2 x double> %x) {
521-
%v = call <2 x double> @llvm.trunc.v2f64(<2 x double> %x)
522-
ret <2 x double> %v
523-
}
524-
525-
; CHECK-LABEL: nearbyint_v2f64:
526-
; CHECK: f64.nearest
527-
declare <2 x double> @llvm.nearbyint.v2f64(<2 x double>)
528-
define <2 x double> @nearbyint_v2f64(<2 x double> %x) {
529-
%v = call <2 x double> @llvm.nearbyint.v2f64(<2 x double> %x)
530-
ret <2 x double> %v
531-
}
532-
533469
; CHECK-LABEL: copysign_v2f64:
534470
; CHECK: f64.copysign
535471
declare <2 x double> @llvm.copysign.v2f64(<2 x double>, <2 x double>)

0 commit comments

Comments
 (0)