blob: d3e4afc8e830577134bb19de06286d7cf47cfa18 [file] [log] [blame]
Matt Arsenault70b92822017-11-12 23:53:44 +00001; RUN: llc -mtriple=amdgcn--amdhsa -mcpu=kaveri -verify-machineinstrs < %s | FileCheck -enable-var-scope -check-prefix=GCN -check-prefix=CI %s
Matt Arsenault1349a042018-05-22 06:32:10 +00002; RUN: llc -mtriple=amdgcn--amdhsa -mcpu=tonga -verify-machineinstrs < %s | FileCheck -enable-var-scope -check-prefix=GCN -check-prefix=VI -check-prefix=GFX89 %s
3; RUN: llc -mtriple=amdgcn--amdhsa -mcpu=gfx900 -verify-machineinstrs < %s | FileCheck -enable-var-scope -check-prefix=GCN -check-prefix=GFX9 -check-prefix=GFX89 %s
Matt Arsenaultc79dc702016-11-15 02:25:28 +00004
5; DAGCombiner will transform:
6; (fabs (f16 bitcast (i16 a))) => (f16 bitcast (and (i16 a), 0x7FFFFFFF))
7; unless isFabsFree returns true
8
Matt Arsenaulteb522e62017-02-27 22:15:25 +00009; GCN-LABEL: {{^}}s_fabs_free_f16:
Matt Arsenaultb81495d2017-09-20 05:01:53 +000010; GCN: {{flat|global}}_load_ushort [[VAL:v[0-9]+]],
Matt Arsenaultc79dc702016-11-15 02:25:28 +000011; GCN: v_and_b32_e32 [[RESULT:v[0-9]+]], 0x7fff, [[VAL]]
Matt Arsenault4e309b02017-07-29 01:03:53 +000012; GCN: {{flat|global}}_store_short v{{\[[0-9]+:[0-9]+\]}}, [[RESULT]]
Matt Arsenaultc79dc702016-11-15 02:25:28 +000013
Matt Arsenault3dbeefa2017-03-21 21:39:51 +000014define amdgpu_kernel void @s_fabs_free_f16(half addrspace(1)* %out, i16 %in) {
Matt Arsenaultc79dc702016-11-15 02:25:28 +000015 %bc= bitcast i16 %in to half
16 %fabs = call half @llvm.fabs.f16(half %bc)
17 store half %fabs, half addrspace(1)* %out
18 ret void
19}
20
Matt Arsenaulteb522e62017-02-27 22:15:25 +000021; GCN-LABEL: {{^}}s_fabs_f16:
Matt Arsenaultc79dc702016-11-15 02:25:28 +000022; CI: flat_load_ushort [[VAL:v[0-9]+]],
Matt Arsenault70b92822017-11-12 23:53:44 +000023; CI: v_and_b32_e32 [[RESULT:v[0-9]+]], 0x7fff, [[VAL]]
Matt Arsenaultc79dc702016-11-15 02:25:28 +000024; CI: flat_store_short v{{\[[0-9]+:[0-9]+\]}}, [[RESULT]]
Matt Arsenault3dbeefa2017-03-21 21:39:51 +000025define amdgpu_kernel void @s_fabs_f16(half addrspace(1)* %out, half %in) {
Matt Arsenaultc79dc702016-11-15 02:25:28 +000026 %fabs = call half @llvm.fabs.f16(half %in)
27 store half %fabs, half addrspace(1)* %out
28 ret void
29}
30
31; FIXME: Should be able to use single and
Matt Arsenaulteb522e62017-02-27 22:15:25 +000032; GCN-LABEL: {{^}}s_fabs_v2f16:
Matt Arsenault9dba9bd2017-02-02 02:27:04 +000033; CI: s_movk_i32 [[MASK:s[0-9]+]], 0x7fff
Matt Arsenaulteb522e62017-02-27 22:15:25 +000034; CI: v_and_b32_e32 v{{[0-9]+}}, [[MASK]]
35; CI: v_lshlrev_b32_e32 v{{[0-9]+}}, 16,
36; CI: v_and_b32_e32 v{{[0-9]+}}, [[MASK]]
37; CI: v_or_b32_e32
Matt Arsenaultc79dc702016-11-15 02:25:28 +000038
Matt Arsenault1349a042018-05-22 06:32:10 +000039; GFX89: s_load_dword [[VAL:s[0-9]+]]
40; GFX89: s_and_b32 s{{[0-9]+}}, [[VAL]], 0x7fff7fff
Matt Arsenault3dbeefa2017-03-21 21:39:51 +000041define amdgpu_kernel void @s_fabs_v2f16(<2 x half> addrspace(1)* %out, <2 x half> %in) {
Matt Arsenaultc79dc702016-11-15 02:25:28 +000042 %fabs = call <2 x half> @llvm.fabs.v2f16(<2 x half> %in)
43 store <2 x half> %fabs, <2 x half> addrspace(1)* %out
44 ret void
45}
46
Matt Arsenaulteb522e62017-02-27 22:15:25 +000047; GCN-LABEL: {{^}}s_fabs_v4f16:
Matt Arsenault9dba9bd2017-02-02 02:27:04 +000048; CI: s_movk_i32 [[MASK:s[0-9]+]], 0x7fff
Matt Arsenaulteb522e62017-02-27 22:15:25 +000049; CI: v_and_b32_e32 v{{[0-9]+}}, [[MASK]]
50; CI: v_and_b32_e32 v{{[0-9]+}}, [[MASK]]
51; CI: v_and_b32_e32 v{{[0-9]+}}, [[MASK]]
52; CI: v_and_b32_e32 v{{[0-9]+}}, [[MASK]]
Matt Arsenaultc79dc702016-11-15 02:25:28 +000053
Matt Arsenault1349a042018-05-22 06:32:10 +000054
55; GFX89: s_load_dword s
56; GFX89: s_load_dword s
57; GFX89: s_mov_b32 [[MASK:s[0-9]+]], 0x7fff7fff
58; GFX89: s_and_b32 s{{[0-9]+}}, s{{[0-9]+}}, [[MASK]]
59; GFX89: s_and_b32 s{{[0-9]+}}, s{{[0-9]+}}, [[MASK]]
Matt Arsenaultc79dc702016-11-15 02:25:28 +000060
Matt Arsenault4e309b02017-07-29 01:03:53 +000061; GCN: {{flat|global}}_store_dwordx2
Matt Arsenault3dbeefa2017-03-21 21:39:51 +000062define amdgpu_kernel void @s_fabs_v4f16(<4 x half> addrspace(1)* %out, <4 x half> %in) {
Matt Arsenaultc79dc702016-11-15 02:25:28 +000063 %fabs = call <4 x half> @llvm.fabs.v4f16(<4 x half> %in)
64 store <4 x half> %fabs, <4 x half> addrspace(1)* %out
65 ret void
66}
67
68; GCN-LABEL: {{^}}fabs_fold_f16:
Matt Arsenaultb81495d2017-09-20 05:01:53 +000069; GCN: {{flat|global}}_load_ushort [[IN0:v[0-9]+]]
70; GCN: {{flat|global}}_load_ushort [[IN1:v[0-9]+]]
Matt Arsenault9dba9bd2017-02-02 02:27:04 +000071
Matt Arsenaultc79dc702016-11-15 02:25:28 +000072; CI-DAG: v_cvt_f32_f16_e32 [[CVT0:v[0-9]+]], [[IN0]]
Matt Arsenault9dba9bd2017-02-02 02:27:04 +000073; CI-DAG: v_cvt_f32_f16_e64 [[ABS_CVT1:v[0-9]+]], |[[IN1]]|
Matt Arsenault6c29c5a2017-07-10 19:53:57 +000074; CI: v_mul_f32_e32 [[RESULT:v[0-9]+]], [[ABS_CVT1]], [[CVT0]]
Matt Arsenaultc79dc702016-11-15 02:25:28 +000075; CI: v_cvt_f16_f32_e32 [[CVTRESULT:v[0-9]+]], [[RESULT]]
76; CI: flat_store_short v{{\[[0-9]+:[0-9]+\]}}, [[CVTRESULT]]
77
78; VI-NOT: and
79; VI: v_mul_f16_e64 [[RESULT:v[0-9]+]], |[[IN1]]|, [[IN0]]
80; VI: flat_store_short v{{\[[0-9]+:[0-9]+\]}}, [[RESULT]]
Matt Arsenault3dbeefa2017-03-21 21:39:51 +000081define amdgpu_kernel void @fabs_fold_f16(half addrspace(1)* %out, half %in0, half %in1) {
Matt Arsenaultc79dc702016-11-15 02:25:28 +000082 %fabs = call half @llvm.fabs.f16(half %in0)
83 %fmul = fmul half %fabs, %in1
84 store half %fmul, half addrspace(1)* %out
85 ret void
86}
87
Matt Arsenaulteb522e62017-02-27 22:15:25 +000088; GCN-LABEL: {{^}}v_fabs_v2f16:
Matt Arsenault4e309b02017-07-29 01:03:53 +000089; GCN: {{flat|global}}_load_dword [[VAL:v[0-9]+]]
Matt Arsenaulteb522e62017-02-27 22:15:25 +000090; GCN: v_and_b32_e32 v{{[0-9]+}}, 0x7fff7fff, [[VAL]]
Matt Arsenault3dbeefa2017-03-21 21:39:51 +000091define amdgpu_kernel void @v_fabs_v2f16(<2 x half> addrspace(1)* %out, <2 x half> addrspace(1)* %in) #0 {
Matt Arsenaulteb522e62017-02-27 22:15:25 +000092 %tid = call i32 @llvm.amdgcn.workitem.id.x()
93 %gep.in = getelementptr inbounds <2 x half>, <2 x half> addrspace(1)* %in, i32 %tid
94 %gep.out = getelementptr inbounds <2 x half>, <2 x half> addrspace(1)* %in, i32 %tid
95 %val = load <2 x half>, <2 x half> addrspace(1)* %gep.in, align 2
96 %fabs = call <2 x half> @llvm.fabs.v2f16(<2 x half> %val)
97 store <2 x half> %fabs, <2 x half> addrspace(1)* %gep.out
98 ret void
99}
100
101; GCN-LABEL: {{^}}fabs_free_v2f16:
102; GCN: s_load_dword [[VAL:s[0-9]+]]
103; GCN: s_and_b32 s{{[0-9]+}}, [[VAL]], 0x7fff7fff
Matt Arsenault3dbeefa2017-03-21 21:39:51 +0000104define amdgpu_kernel void @fabs_free_v2f16(<2 x half> addrspace(1)* %out, i32 %in) #0 {
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000105 %bc = bitcast i32 %in to <2 x half>
106 %fabs = call <2 x half> @llvm.fabs.v2f16(<2 x half> %bc)
107 store <2 x half> %fabs, <2 x half> addrspace(1)* %out
108 ret void
109}
110
111; GCN-LABEL: {{^}}v_fabs_fold_v2f16:
Matt Arsenault4e309b02017-07-29 01:03:53 +0000112; GCN: {{flat|global}}_load_dword [[VAL:v[0-9]+]]
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000113
114; CI: v_cvt_f32_f16_e32
115; CI: v_cvt_f32_f16_e32
116; CI: v_mul_f32_e64 v{{[0-9]+}}, |v{{[0-9]+}}|, v{{[0-9]+}}
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000117; CI: v_cvt_f16_f32
Matt Arsenault86e02ce2017-03-15 19:04:26 +0000118; CI: v_mul_f32_e64 v{{[0-9]+}}, |v{{[0-9]+}}|, v{{[0-9]+}}
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000119; CI: v_cvt_f16_f32
120
Sam Kolton5f7f32c2017-12-04 16:22:32 +0000121; VI: v_mul_f16_sdwa v{{[0-9]+}}, |v{{[0-9]+}}|, v{{[0-9]+}} dst_sel:WORD_1 dst_unused:UNUSED_PAD src0_sel:WORD_1 src1_sel:WORD_1
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000122; VI: v_mul_f16_e64 v{{[0-9]+}}, |v{{[0-9]+}}|, v{{[0-9]+}}
123
124; GFX9: v_and_b32_e32 [[FABS:v[0-9]+]], 0x7fff7fff, [[VAL]]
125; GFX9: v_pk_mul_f16 v{{[0-9]+}}, [[FABS]], v{{[0-9]+$}}
Matt Arsenault3dbeefa2017-03-21 21:39:51 +0000126define amdgpu_kernel void @v_fabs_fold_v2f16(<2 x half> addrspace(1)* %out, <2 x half> addrspace(1)* %in) #0 {
Alexander Timofeev982aee62017-07-04 17:32:00 +0000127 %tid = call i32 @llvm.amdgcn.workitem.id.x()
Matt Arsenault6c29c5a2017-07-10 19:53:57 +0000128 %gep = getelementptr <2 x half>, <2 x half> addrspace(1)* %in, i32 %tid
Alexander Timofeev982aee62017-07-04 17:32:00 +0000129 %val = load <2 x half>, <2 x half> addrspace(1)* %gep
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000130 %fabs = call <2 x half> @llvm.fabs.v2f16(<2 x half> %val)
131 %fmul = fmul <2 x half> %fabs, %val
132 store <2 x half> %fmul, <2 x half> addrspace(1)* %out
133 ret void
134}
135
Matt Arsenaultfcc5ba42018-04-26 19:21:32 +0000136; GCN-LABEL: {{^}}v_extract_fabs_fold_v2f16:
137; GCN-DAG: {{flat|global}}_load_dword [[VAL:v[0-9]+]]
138; CI-DAG: v_mul_f32_e32 v{{[0-9]+}}, 4.0, v{{[0-9]+}}
139; CI-DAG: v_add_f32_e32 v{{[0-9]+}}, 2.0, v{{[0-9]+}}
140
Matt Arsenault1349a042018-05-22 06:32:10 +0000141; GFX89-DAG: v_mul_f16_e64 v{{[0-9]+}}, |[[VAL]]|, 4.0
Matt Arsenaultfcc5ba42018-04-26 19:21:32 +0000142; GFX89-DAG: v_mov_b32_e32 [[CONST2:v[0-9]+]], 0x4000
Matt Arsenault1349a042018-05-22 06:32:10 +0000143; GFX89-DAG: v_add_f16_sdwa v{{[0-9]+}}, |[[VAL]]|, [[CONST2]] dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_1 src1_sel:DWORD
Matt Arsenaultfcc5ba42018-04-26 19:21:32 +0000144define amdgpu_kernel void @v_extract_fabs_fold_v2f16(<2 x half> addrspace(1)* %in) #0 {
145 %tid = call i32 @llvm.amdgcn.workitem.id.x()
146 %gep.in = getelementptr inbounds <2 x half>, <2 x half> addrspace(1)* %in, i32 %tid
147 %val = load <2 x half>, <2 x half> addrspace(1)* %gep.in
148 %fabs = call <2 x half> @llvm.fabs.v2f16(<2 x half> %val)
149 %elt0 = extractelement <2 x half> %fabs, i32 0
150 %elt1 = extractelement <2 x half> %fabs, i32 1
151
152 %fmul0 = fmul half %elt0, 4.0
153 %fadd1 = fadd half %elt1, 2.0
154 store volatile half %fmul0, half addrspace(1)* undef
155 store volatile half %fadd1, half addrspace(1)* undef
156 ret void
157}
158
159; GCN-LABEL: {{^}}v_extract_fabs_no_fold_v2f16:
160; GCN: {{flat|global}}_load_dword [[VAL:v[0-9]+]]
Matt Arsenaultfcc5ba42018-04-26 19:21:32 +0000161; GCN: v_and_b32_e32 [[AND:v[0-9]+]], 0x7fff7fff, [[VAL]]
Matt Arsenault1349a042018-05-22 06:32:10 +0000162
163
164; VI: v_bfe_u32 v{{[0-9]+}}, v{{[0-9]+}}, 16, 15
165; VI: flat_store_short
166
Matt Arsenaultfcc5ba42018-04-26 19:21:32 +0000167; GFX9: global_store_short_d16_hi v{{\[[0-9]+:[0-9]+\]}}, [[AND]], off
168define amdgpu_kernel void @v_extract_fabs_no_fold_v2f16(<2 x half> addrspace(1)* %in) #0 {
169 %tid = call i32 @llvm.amdgcn.workitem.id.x()
170 %gep.in = getelementptr inbounds <2 x half>, <2 x half> addrspace(1)* %in, i32 %tid
171 %val = load <2 x half>, <2 x half> addrspace(1)* %gep.in
172 %fabs = call <2 x half> @llvm.fabs.v2f16(<2 x half> %val)
173 %elt0 = extractelement <2 x half> %fabs, i32 0
174 %elt1 = extractelement <2 x half> %fabs, i32 1
175 store volatile half %elt0, half addrspace(1)* undef
176 store volatile half %elt1, half addrspace(1)* undef
177 ret void
178}
179
Matt Arsenaulteb522e62017-02-27 22:15:25 +0000180declare half @llvm.fabs.f16(half) #1
181declare <2 x half> @llvm.fabs.v2f16(<2 x half>) #1
182declare <4 x half> @llvm.fabs.v4f16(<4 x half>) #1
183declare i32 @llvm.amdgcn.workitem.id.x() #1
184
185attributes #0 = { nounwind }
186attributes #1 = { nounwind readnone }