Skip to content

Commit 34db3c3

Browse files
committed
[WebAssembly] SIMD integer abs instructions
Summary: These were merged to the SIMD proposal in WebAssembly/simd#128. Depends on D76397 to avoid merge conflicts. Reviewers: aheejin Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, sunfish, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D76399
1 parent 6343526 commit 34db3c3

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ WebAssemblyTargetLowering::WebAssemblyTargetLowering(
125125
for (auto T : {MVT::v16i8, MVT::v8i16})
126126
setOperationAction(Op, T, Legal);
127127

128+
// Support integer abs
129+
for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32})
130+
setOperationAction(ISD::ABS, T, Legal);
131+
128132
// Custom lower BUILD_VECTORs to minimize number of replace_lanes
129133
for (auto T : {MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v4f32, MVT::v2i64,
130134
MVT::v2f64})

llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,11 @@ multiclass SIMDReduce<SDNode op, string name, bits<32> baseInst> {
575575
// Integer vector negation
576576
def ivneg : PatFrag<(ops node:$in), (sub immAllZerosV, node:$in)>;
577577

578+
// Integer absolute value: abs
579+
defm ABS : SIMDUnary<v16i8, "i8x16", abs, "abs", 225>;
580+
defm ABS : SIMDUnary<v8i16, "i16x8", abs, "abs", 226>;
581+
defm ABS : SIMDUnary<v4i32, "i32x4", abs, "abs", 227>;
582+
578583
// Integer negation: neg
579584
defm NEG : SIMDUnaryInt<ivneg, "neg", 81>;
580585

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ define <16 x i8> @avgr_u_v16i8_wrap(<16 x i8> %x, <16 x i8> %y) {
118118
ret <16 x i8> %c
119119
}
120120

121+
; CHECK-LABEL: abs_v16i8:
122+
; NO-SIMD128-NOT: i8x16
123+
; SIMD128-NEXT: .functype abs_v16i8 (v128) -> (v128){{$}}
124+
; SIMD128-NEXT: i8x16.abs $push[[R:[0-9]+]]=, $0{{$}}
125+
; SIMD128-NEXT: return $pop[[R]]{{$}}
126+
define <16 x i8> @abs_v16i8(<16 x i8> %x) {
127+
%a = sub <16 x i8> zeroinitializer, %x
128+
%b = icmp slt <16 x i8> %x, zeroinitializer
129+
%c = select <16 x i1> %b, <16 x i8> %a, <16 x i8> %x
130+
ret <16 x i8> %c
131+
}
132+
121133
; CHECK-LABEL: neg_v16i8:
122134
; NO-SIMD128-NOT: i8x16
123135
; SIMD128-NEXT: .functype neg_v16i8 (v128) -> (v128){{$}}
@@ -431,6 +443,18 @@ define <8 x i16> @avgr_u_v8i16_wrap(<8 x i16> %x, <8 x i16> %y) {
431443
ret <8 x i16> %c
432444
}
433445

446+
; CHECK-LABEL: abs_v8i16:
447+
; NO-SIMD128-NOT: i16x8
448+
; SIMD128-NEXT: .functype abs_v8i16 (v128) -> (v128){{$}}
449+
; SIMD128-NEXT: i16x8.abs $push[[R:[0-9]+]]=, $0{{$}}
450+
; SIMD128-NEXT: return $pop[[R]]{{$}}
451+
define <8 x i16> @abs_v8i16(<8 x i16> %x) {
452+
%a = sub <8 x i16> zeroinitializer, %x
453+
%b = icmp slt <8 x i16> %x, zeroinitializer
454+
%c = select <8 x i1> %b, <8 x i16> %a, <8 x i16> %x
455+
ret <8 x i16> %c
456+
}
457+
434458
; CHECK-LABEL: neg_v8i16:
435459
; NO-SIMD128-NOT: i16x8
436460
; SIMD128-NEXT: .functype neg_v8i16 (v128) -> (v128){{$}}
@@ -713,6 +737,18 @@ define <4 x i32> @max_u_v4i32(<4 x i32> %x, <4 x i32> %y) {
713737
ret <4 x i32> %a
714738
}
715739

740+
; CHECK-LABEL: abs_v4i32:
741+
; NO-SIMD128-NOT: i32x4
742+
; SIMD128-NEXT: .functype abs_v4i32 (v128) -> (v128){{$}}
743+
; SIMD128-NEXT: i32x4.abs $push[[R:[0-9]+]]=, $0{{$}}
744+
; SIMD128-NEXT: return $pop[[R]]{{$}}
745+
define <4 x i32> @abs_v4i32(<4 x i32> %x) {
746+
%a = sub <4 x i32> zeroinitializer, %x
747+
%b = icmp slt <4 x i32> %x, zeroinitializer
748+
%c = select <4 x i1> %b, <4 x i32> %a, <4 x i32> %x
749+
ret <4 x i32> %c
750+
}
751+
716752
; CHECK-LABEL: neg_v4i32:
717753
; NO-SIMD128-NOT: i32x4
718754
; SIMD128-NEXT: .functype neg_v4i32 (v128) -> (v128){{$}}

llvm/test/MC/WebAssembly/simd-encodings.s

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,15 @@ main:
580580
# CHECK: i32x4.dot_i16x8_s # encoding: [0xfd,0xdb,0x01]
581581
i32x4.dot_i16x8_s
582582

583+
# CHECK: i8x16.abs # encoding: [0xfd,0xe1,0x01]
584+
i8x16.abs
585+
586+
# CHECK: i16x8.abs # encoding: [0xfd,0xe2,0x01]
587+
i16x8.abs
588+
589+
# CHECK: i32x4.abs # encoding: [0xfd,0xe3,0x01]
590+
i32x4.abs
591+
583592
# CHECK: i8x16.bitmask # encoding: [0xfd,0xe4,0x01]
584593
i8x16.bitmask
585594

0 commit comments

Comments
 (0)