Skip to content

Commit 3e82442

Browse files
authored
[SPIRV] Add tan intrinsic part 3 (#90278)
This change is an implementation of #87367's investigation on supporting IEEE math operations as intrinsics. Which was discussed in this RFC: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294 If you want an overarching view of how this will all connect see: #90088 Changes: - `llvm/docs/GlobalISel/GenericOpcode.rst` - Document the `G_FTAN` opcode - `llvm/include/llvm/IR/Intrinsics.td` - Create the tan intrinsic - `llvm/include/llvm/Support/TargetOpcodes.def` - Create a `G_FTAN` Opcode handler - `llvm/include/llvm/Target/GenericOpcodes.td` - Define the `G_FTAN` Opcode - `llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp` Map the tan intrinsic to `G_FTAN` Opcode - `llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp` - Map the `G_FTAN` opcode to the GLSL 4.5 and openCL tan instructions. - `llvm/lib/Target/SPIRV/SPIRVLegalizerInfo.cpp` - Define `G_FTAN` as a legal spirv target opcode.
1 parent 8296f06 commit 3e82442

File tree

8 files changed

+65
-2
lines changed

8 files changed

+65
-2
lines changed

llvm/docs/GlobalISel/GenericOpcode.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,8 @@ G_FLOG, G_FLOG2, G_FLOG10
592592

593593
Calculate the base-e, base-2, or base-10 respectively.
594594

595-
G_FCEIL, G_FCOS, G_FSIN, G_FSQRT, G_FFLOOR, G_FRINT, G_FNEARBYINT
596-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
595+
G_FCEIL, G_FCOS, G_FSIN, G_FTAN, G_FSQRT, G_FFLOOR, G_FRINT, G_FNEARBYINT
596+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
597597

598598
These correspond to the standard C functions of the same name.
599599

llvm/include/llvm/Support/TargetOpcodes.def

+3
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,9 @@ HANDLE_TARGET_OPCODE(G_FCOS)
781781
/// Floating point sine.
782782
HANDLE_TARGET_OPCODE(G_FSIN)
783783

784+
/// Floating point Tangent.
785+
HANDLE_TARGET_OPCODE(G_FTAN)
786+
784787
/// Floating point square root.
785788
HANDLE_TARGET_OPCODE(G_FSQRT)
786789

llvm/include/llvm/Target/GenericOpcodes.td

+7
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,13 @@ def G_FSIN : GenericInstruction {
988988
let hasSideEffects = false;
989989
}
990990

991+
// Floating point tangent of a value.
992+
def G_FTAN : GenericInstruction {
993+
let OutOperandList = (outs type0:$dst);
994+
let InOperandList = (ins type0:$src1);
995+
let hasSideEffects = false;
996+
}
997+
991998
// Floating point square root of a value.
992999
// This returns NaN for negative nonzero values.
9931000
// NOTE: Unlike libm sqrt(), this never sets errno. In all other respects it's

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,8 @@ unsigned IRTranslator::getSimpleIntrinsicOpcode(Intrinsic::ID ID) {
19451945
return TargetOpcode::G_FSIN;
19461946
case Intrinsic::sqrt:
19471947
return TargetOpcode::G_FSQRT;
1948+
case Intrinsic::tan:
1949+
return TargetOpcode::G_FTAN;
19481950
case Intrinsic::trunc:
19491951
return TargetOpcode::G_INTRINSIC_TRUNC;
19501952
case Intrinsic::readcyclecounter:

llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ bool SPIRVInstructionSelector::spvSelect(Register ResVReg,
467467
return selectExtInst(ResVReg, ResType, I, CL::cos, GL::Cos);
468468
case TargetOpcode::G_FSIN:
469469
return selectExtInst(ResVReg, ResType, I, CL::sin, GL::Sin);
470+
case TargetOpcode::G_FTAN:
471+
return selectExtInst(ResVReg, ResType, I, CL::tan, GL::Tan);
470472

471473
case TargetOpcode::G_FSQRT:
472474
return selectExtInst(ResVReg, ResType, I, CL::sqrt, GL::Sqrt);

llvm/lib/Target/SPIRV/SPIRVLegalizerInfo.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ SPIRVLegalizerInfo::SPIRVLegalizerInfo(const SPIRVSubtarget &ST) {
277277
G_FCEIL,
278278
G_FCOS,
279279
G_FSIN,
280+
G_FTAN,
280281
G_FSQRT,
281282
G_FFLOOR,
282283
G_FRINT,

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

+3
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,9 @@
674674
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
675675
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
676676
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
677+
# DEBUG-NEXT: G_FTAN (opcode {{[0-9]+}}): 1 type index, 0 imm indices
678+
# DEBUG-NEXT: .. type index coverage check SKIPPED: no rules defined
679+
# DEBUG-NEXT: .. imm index coverage check SKIPPED: no rules defined
677680
# DEBUG-NEXT: G_FSQRT (opcode {{[0-9]+}}): 1 type index, 0 imm indices
678681
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
679682
# DEBUG-NEXT: .. type index coverage check SKIPPED: user-defined predicate detected
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; RUN: llc -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 @tan_float(float noundef %a) {
11+
entry:
12+
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
13+
; CHECK: %[[#]] = OpExtInst %[[#float_32]] %[[#op_ext_glsl]] Tan %[[#arg0]]
14+
%elt.tan = call float @llvm.tan.f32(float %a)
15+
ret float %elt.tan
16+
}
17+
18+
define noundef half @tan_half(half noundef %a) {
19+
entry:
20+
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
21+
; CHECK: %[[#]] = OpExtInst %[[#float_16]] %[[#op_ext_glsl]] Tan %[[#arg0]]
22+
%elt.tan = call half @llvm.tan.f16(half %a)
23+
ret half %elt.tan
24+
}
25+
26+
define noundef <4 x float> @tan_float4(<4 x float> noundef %a) {
27+
entry:
28+
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
29+
; CHECK: %[[#]] = OpExtInst %[[#vec4_float_32]] %[[#op_ext_glsl]] Tan %[[#arg0]]
30+
%elt.tan = call <4 x float> @llvm.tan.v4f32(<4 x float> %a)
31+
ret <4 x float> %elt.tan
32+
}
33+
34+
define noundef <4 x half> @tan_half4(<4 x half> noundef %a) {
35+
entry:
36+
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
37+
; CHECK: %[[#]] = OpExtInst %[[#vec4_float_16]] %[[#op_ext_glsl]] Tan %[[#arg0]]
38+
%elt.tan = call <4 x half> @llvm.tan.v4f16(<4 x half> %a)
39+
ret <4 x half> %elt.tan
40+
}
41+
42+
declare half @llvm.tan.f16(half)
43+
declare float @llvm.tan.f32(float)
44+
declare <4 x half> @llvm.tan.v4f16(<4 x half>)
45+
declare <4 x float> @llvm.tan.v4f32(<4 x float>)

0 commit comments

Comments
 (0)