xref: /aosp_15_r20/external/llvm/test/CodeGen/AMDGPU/rsq.ll (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker; RUN: llc -march=amdgcn -mattr=-fp32-denormals -verify-machineinstrs -enable-unsafe-fp-math < %s | FileCheck -check-prefix=SI-UNSAFE -check-prefix=SI %s
2*9880d681SAndroid Build Coastguard Worker; RUN: llc -march=amdgcn -mattr=-fp32-denormals -verify-machineinstrs < %s | FileCheck -check-prefix=SI-SAFE -check-prefix=SI %s
3*9880d681SAndroid Build Coastguard Worker
4*9880d681SAndroid Build Coastguard Workerdeclare i32 @llvm.amdgcn.workitem.id.x() nounwind readnone
5*9880d681SAndroid Build Coastguard Workerdeclare float @llvm.sqrt.f32(float) nounwind readnone
6*9880d681SAndroid Build Coastguard Workerdeclare double @llvm.sqrt.f64(double) nounwind readnone
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard Worker; SI-LABEL: {{^}}rsq_f32:
9*9880d681SAndroid Build Coastguard Worker; SI: v_rsq_f32_e32
10*9880d681SAndroid Build Coastguard Worker; SI: s_endpgm
11*9880d681SAndroid Build Coastguard Workerdefine void @rsq_f32(float addrspace(1)* noalias %out, float addrspace(1)* noalias %in) nounwind {
12*9880d681SAndroid Build Coastguard Worker  %val = load float, float addrspace(1)* %in, align 4
13*9880d681SAndroid Build Coastguard Worker  %sqrt = call float @llvm.sqrt.f32(float %val) nounwind readnone
14*9880d681SAndroid Build Coastguard Worker  %div = fdiv float 1.0, %sqrt
15*9880d681SAndroid Build Coastguard Worker  store float %div, float addrspace(1)* %out, align 4
16*9880d681SAndroid Build Coastguard Worker  ret void
17*9880d681SAndroid Build Coastguard Worker}
18*9880d681SAndroid Build Coastguard Worker
19*9880d681SAndroid Build Coastguard Worker; SI-LABEL: {{^}}rsq_f64:
20*9880d681SAndroid Build Coastguard Worker; SI-UNSAFE: v_rsq_f64_e32
21*9880d681SAndroid Build Coastguard Worker; SI-SAFE: v_sqrt_f64_e32
22*9880d681SAndroid Build Coastguard Worker; SI: s_endpgm
23*9880d681SAndroid Build Coastguard Workerdefine void @rsq_f64(double addrspace(1)* noalias %out, double addrspace(1)* noalias %in) nounwind {
24*9880d681SAndroid Build Coastguard Worker  %val = load double, double addrspace(1)* %in, align 4
25*9880d681SAndroid Build Coastguard Worker  %sqrt = call double @llvm.sqrt.f64(double %val) nounwind readnone
26*9880d681SAndroid Build Coastguard Worker  %div = fdiv double 1.0, %sqrt
27*9880d681SAndroid Build Coastguard Worker  store double %div, double addrspace(1)* %out, align 4
28*9880d681SAndroid Build Coastguard Worker  ret void
29*9880d681SAndroid Build Coastguard Worker}
30*9880d681SAndroid Build Coastguard Worker
31*9880d681SAndroid Build Coastguard Worker; SI-LABEL: {{^}}rsq_f32_sgpr:
32*9880d681SAndroid Build Coastguard Worker; SI: v_rsq_f32_e32 {{v[0-9]+}}, {{s[0-9]+}}
33*9880d681SAndroid Build Coastguard Worker; SI: s_endpgm
34*9880d681SAndroid Build Coastguard Workerdefine void @rsq_f32_sgpr(float addrspace(1)* noalias %out, float %val) nounwind {
35*9880d681SAndroid Build Coastguard Worker  %sqrt = call float @llvm.sqrt.f32(float %val) nounwind readnone
36*9880d681SAndroid Build Coastguard Worker  %div = fdiv float 1.0, %sqrt
37*9880d681SAndroid Build Coastguard Worker  store float %div, float addrspace(1)* %out, align 4
38*9880d681SAndroid Build Coastguard Worker  ret void
39*9880d681SAndroid Build Coastguard Worker}
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker; Recognize that this is rsqrt(a) * rcp(b) * c,
42*9880d681SAndroid Build Coastguard Worker; not 1 / ( 1 / sqrt(a)) * rcp(b) * c.
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker; SI-LABEL: @rsqrt_fmul
45*9880d681SAndroid Build Coastguard Worker; SI-DAG: buffer_load_dword [[A:v[0-9]+]], {{v\[[0-9]+:[0-9]+\]}}, {{s\[[0-9]+:[0-9]+\]}}, 0 addr64{{$}}
46*9880d681SAndroid Build Coastguard Worker; SI-DAG: buffer_load_dword [[B:v[0-9]+]], {{v\[[0-9]+:[0-9]+\]}}, {{s\[[0-9]+:[0-9]+\]}}, 0 addr64 offset:4
47*9880d681SAndroid Build Coastguard Worker; SI-DAG: buffer_load_dword [[C:v[0-9]+]], {{v\[[0-9]+:[0-9]+\]}}, {{s\[[0-9]+:[0-9]+\]}}, 0 addr64 offset:8
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker; SI-UNSAFE-DAG: v_rsq_f32_e32 [[RSQA:v[0-9]+]], [[A]]
50*9880d681SAndroid Build Coastguard Worker; SI-UNSAFE-DAG: v_rcp_f32_e32 [[RCPB:v[0-9]+]], [[B]]
51*9880d681SAndroid Build Coastguard Worker; SI-UNSAFE-DAG: v_mul_f32_e32 [[TMP:v[0-9]+]], [[RCPB]], [[RSQA]]
52*9880d681SAndroid Build Coastguard Worker; SI-UNSAFE: v_mul_f32_e32 [[RESULT:v[0-9]+]], [[TMP]], [[C]]
53*9880d681SAndroid Build Coastguard Worker; SI-UNSAFE: buffer_store_dword [[RESULT]]
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard Worker; SI-SAFE-NOT: v_rsq_f32
56*9880d681SAndroid Build Coastguard Worker
57*9880d681SAndroid Build Coastguard Worker; SI: s_endpgm
58*9880d681SAndroid Build Coastguard Workerdefine void @rsqrt_fmul(float addrspace(1)* %out, float addrspace(1)* %in) {
59*9880d681SAndroid Build Coastguard Worker  %tid = call i32 @llvm.amdgcn.workitem.id.x() nounwind readnone
60*9880d681SAndroid Build Coastguard Worker  %out.gep = getelementptr float, float addrspace(1)* %out, i32 %tid
61*9880d681SAndroid Build Coastguard Worker  %gep.0 = getelementptr float, float addrspace(1)* %in, i32 %tid
62*9880d681SAndroid Build Coastguard Worker  %gep.1 = getelementptr float, float addrspace(1)* %gep.0, i32 1
63*9880d681SAndroid Build Coastguard Worker  %gep.2 = getelementptr float, float addrspace(1)* %gep.0, i32 2
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker  %a = load volatile float, float addrspace(1)* %gep.0
66*9880d681SAndroid Build Coastguard Worker  %b = load volatile float, float addrspace(1)* %gep.1
67*9880d681SAndroid Build Coastguard Worker  %c = load volatile float, float addrspace(1)* %gep.2
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker  %x = call float @llvm.sqrt.f32(float %a)
70*9880d681SAndroid Build Coastguard Worker  %y = fmul float %x, %b
71*9880d681SAndroid Build Coastguard Worker  %z = fdiv float %c, %y
72*9880d681SAndroid Build Coastguard Worker  store float %z, float addrspace(1)* %out.gep
73*9880d681SAndroid Build Coastguard Worker  ret void
74*9880d681SAndroid Build Coastguard Worker}
75