Skip to content

Commit 139688a

Browse files
authored
[SPIRV] Add atan2 function lowering (p2) (#110037)
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294 - Add generic opcode for atan2 - Add SPIRV lowering for atan2 Part 2 for Implement the atan2 HLSL Function #70096.
1 parent 74f276d commit 139688a

File tree

8 files changed

+69
-2
lines changed

8 files changed

+69
-2
lines changed

llvm/docs/GlobalISel/GenericOpcode.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,8 @@ G_FCEIL, G_FSQRT, G_FFLOOR, G_FRINT, G_FNEARBYINT
633633

634634
These correspond to the standard C functions of the same name.
635635

636-
G_FCOS, G_FSIN, G_FTAN, G_FACOS, G_FASIN, G_FATAN, G_FCOSH, G_FSINH, G_FTANH
637-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
636+
G_FCOS, G_FSIN, G_FTAN, G_FACOS, G_FASIN, G_FATAN, G_FATAN2, G_FCOSH, G_FSINH, G_FTANH
637+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
638638

639639
These correspond to the standard C trigonometry functions of the same name.
640640

llvm/include/llvm/Support/TargetOpcodes.def

+3
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,9 @@ HANDLE_TARGET_OPCODE(G_FASIN)
821821
/// Floating point arctangent.
822822
HANDLE_TARGET_OPCODE(G_FATAN)
823823

824+
/// Floating point arctangent of y/x.
825+
HANDLE_TARGET_OPCODE(G_FATAN2)
826+
824827
/// Floating point hyperbolic cosine.
825828
HANDLE_TARGET_OPCODE(G_FCOSH)
826829

llvm/include/llvm/Target/GenericOpcodes.td

+7
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,13 @@ def G_FATAN : GenericInstruction {
10481048
let hasSideEffects = false;
10491049
}
10501050

1051+
// Floating point arctangent of a value.
1052+
def G_FATAN2 : GenericInstruction {
1053+
let OutOperandList = (outs type0:$dst);
1054+
let InOperandList = (ins type0:$src1, type0:$src2);
1055+
let hasSideEffects = false;
1056+
}
1057+
10511058
// Floating point hyperbolic cosine of a value.
10521059
def G_FCOSH : GenericInstruction {
10531060
let OutOperandList = (outs type0:$dst);

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,8 @@ unsigned IRTranslator::getSimpleIntrinsicOpcode(Intrinsic::ID ID) {
18851885
return TargetOpcode::G_FASIN;
18861886
case Intrinsic::atan:
18871887
return TargetOpcode::G_FATAN;
1888+
case Intrinsic::atan2:
1889+
return TargetOpcode::G_FATAN2;
18881890
case Intrinsic::bswap:
18891891
return TargetOpcode::G_BSWAP;
18901892
case Intrinsic::bitreverse:

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,8 @@ bool SPIRVInstructionSelector::spvSelect(Register ResVReg,
540540
return selectExtInst(ResVReg, ResType, I, CL::asin, GL::Asin);
541541
case TargetOpcode::G_FATAN:
542542
return selectExtInst(ResVReg, ResType, I, CL::atan, GL::Atan);
543+
case TargetOpcode::G_FATAN2:
544+
return selectExtInst(ResVReg, ResType, I, CL::atan2, GL::Atan2);
543545
case TargetOpcode::G_FCOSH:
544546
return selectExtInst(ResVReg, ResType, I, CL::cosh, GL::Cosh);
545547
case TargetOpcode::G_FSINH:

llvm/lib/Target/SPIRV/SPIRVLegalizerInfo.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ SPIRVLegalizerInfo::SPIRVLegalizerInfo(const SPIRVSubtarget &ST) {
321321
G_FACOS,
322322
G_FASIN,
323323
G_FATAN,
324+
G_FATAN2,
324325
G_FCOSH,
325326
G_FSINH,
326327
G_FTANH,

llvm/test/CodeGen/AArch64/GlobalISel/legalizer-info-validation.mir

+3
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,9 @@
717717
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
718718
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
719719
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
720+
# DEBUG-NEXT: G_FATAN2 (opcode {{[0-9]+}}): 1 type index, 0 imm indices
721+
# DEBUG-NEXT: .. type index coverage check SKIPPED: no rules defined
722+
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
720723
# DEBUG-NEXT: G_FCOSH (opcode {{[0-9]+}}): 1 type index, 0 imm indices
721724
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
722725
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
2+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
3+
4+
; CHECK-DAG: %[[#op_ext_glsl:]] = OpExtInstImport "GLSL.std.450"
5+
; CHECK-DAG: %[[#float_32:]] = OpTypeFloat 32
6+
; CHECK-DAG: %[[#float_16:]] = OpTypeFloat 16
7+
; CHECK-DAG: %[[#vec4_float_32:]] = OpTypeVector %[[#float_32]] 4
8+
; CHECK-DAG: %[[#vec4_float_16:]] = OpTypeVector %[[#float_16]] 4
9+
10+
define noundef float @atan2_float(float noundef %a, float noundef %b) {
11+
entry:
12+
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
13+
; CHECK: %[[#arg1:]] = OpFunctionParameter %[[#]]
14+
; CHECK: %[[#]] = OpExtInst %[[#float_32]] %[[#op_ext_glsl]] Atan2 %[[#arg0]] %[[#arg1]]
15+
%elt.atan2 = call float @llvm.atan2.f32(float %a, float %b)
16+
ret float %elt.atan2
17+
}
18+
19+
define noundef half @atan2_half(half noundef %a, half noundef %b) {
20+
entry:
21+
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
22+
; CHECK: %[[#arg1:]] = OpFunctionParameter %[[#]]
23+
; CHECK: %[[#]] = OpExtInst %[[#float_16]] %[[#op_ext_glsl]] Atan2 %[[#arg0]] %[[#arg1]]
24+
%elt.atan2 = call half @llvm.atan2.f16(half %a, half %b)
25+
ret half %elt.atan2
26+
}
27+
28+
define noundef <4 x float> @atan2_float4(<4 x float> noundef %a, <4 x float> noundef %b) {
29+
entry:
30+
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
31+
; CHECK: %[[#arg1:]] = OpFunctionParameter %[[#]]
32+
; CHECK: %[[#]] = OpExtInst %[[#vec4_float_32]] %[[#op_ext_glsl]] Atan2 %[[#arg0]] %[[#arg1]]
33+
%elt.atan2 = call <4 x float> @llvm.atan2.v4f32(<4 x float> %a, <4 x float> %b)
34+
ret <4 x float> %elt.atan2
35+
}
36+
37+
define noundef <4 x half> @atan2_half4(<4 x half> noundef %a, <4 x half> noundef %b) {
38+
entry:
39+
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
40+
; CHECK: %[[#arg1:]] = OpFunctionParameter %[[#]]
41+
; CHECK: %[[#]] = OpExtInst %[[#vec4_float_16]] %[[#op_ext_glsl]] Atan2 %[[#arg0]] %[[#arg1]]
42+
%elt.atan2 = call <4 x half> @llvm.atan2.v4f16(<4 x half> %a, <4 x half> %b)
43+
ret <4 x half> %elt.atan2
44+
}
45+
46+
declare half @llvm.atan2.f16(half, half)
47+
declare float @llvm.atan2.f32(float, float)
48+
declare <4 x half> @llvm.atan2.v4f16(<4 x half>, <4 x half>)
49+
declare <4 x float> @llvm.atan2.v4f32(<4 x float>, <4 x float>)

0 commit comments

Comments
 (0)