Skip to content

Commit

Permalink
[clang][hlsl] Add atan2 intrinsic part 3
Browse files Browse the repository at this point in the history
Issue: llvm#70096

Changes:
- `llvm/docs/GlobalISel/GenericOpcode.rst` - Document the G_FATAN2 opcode
- `llvm/include/llvm/Support/TargetOpcodes.def` - Create a G_FATAN2 Opcode handler
- `llvm/include/llvm/Target/GenericOpcodes.td` - Define the G_FATAN2 Opcode
- `llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp` Map the atan2 intrinsic to G_FATAN2 Opcode
- `llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp` - Map the G_FATAN2 opcode to the GLSL 4.5 and openCL atan2 instructions.
- `llvm/lib/Target/SPIRV/SPIRVLegalizerInfo.cpp` - Define G_FATAN2 as a legal spirv target opcode.
  • Loading branch information
tex3d committed Sep 10, 2024
1 parent 9bbd916 commit 87d72e1
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions llvm/docs/GlobalISel/GenericOpcode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,8 @@ G_FCEIL, G_FSQRT, G_FFLOOR, G_FRINT, G_FNEARBYINT

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

G_FCOS, G_FSIN, G_FTAN, G_FACOS, G_FASIN, G_FATAN, G_FCOSH, G_FSINH, G_FTANH
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
G_FCOS, G_FSIN, G_FTAN, G_FACOS, G_FASIN, G_FATAN, G_FCOSH, G_FSINH, G_FTANH, G_FATAN2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/Support/TargetOpcodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,9 @@ HANDLE_TARGET_OPCODE(G_FASIN)
/// Floating point arctangent.
HANDLE_TARGET_OPCODE(G_FATAN)

/// Floating point arctangent of y/x.
HANDLE_TARGET_OPCODE(G_FATAN2)

/// Floating point hyperbolic cosine.
HANDLE_TARGET_OPCODE(G_FCOSH)

Expand Down
7 changes: 7 additions & 0 deletions llvm/include/llvm/Target/GenericOpcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,13 @@ def G_FATAN : GenericInstruction {
let hasSideEffects = false;
}

// Floating point arctangent of a value.
def G_FATAN2 : GenericInstruction {
let OutOperandList = (outs type0:$dst);
let InOperandList = (ins type0:$src1, type0:$src2);
let hasSideEffects = false;
}

// Floating point hyperbolic cosine of a value.
def G_FCOSH : GenericInstruction {
let OutOperandList = (outs type0:$dst);
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,8 @@ unsigned IRTranslator::getSimpleIntrinsicOpcode(Intrinsic::ID ID) {
return TargetOpcode::G_FASIN;
case Intrinsic::atan:
return TargetOpcode::G_FATAN;
case Intrinsic::atan2:
return TargetOpcode::G_FATAN2;
case Intrinsic::bswap:
return TargetOpcode::G_BSWAP;
case Intrinsic::bitreverse:
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ bool SPIRVInstructionSelector::spvSelect(Register ResVReg,
return selectExtInst(ResVReg, ResType, I, CL::asin, GL::Asin);
case TargetOpcode::G_FATAN:
return selectExtInst(ResVReg, ResType, I, CL::atan, GL::Atan);
case TargetOpcode::G_FATAN2:
return selectExtInst(ResVReg, ResType, I, CL::atan2, GL::Atan2);
case TargetOpcode::G_FCOSH:
return selectExtInst(ResVReg, ResType, I, CL::cosh, GL::Cosh);
case TargetOpcode::G_FSINH:
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/SPIRV/SPIRVLegalizerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ SPIRVLegalizerInfo::SPIRVLegalizerInfo(const SPIRVSubtarget &ST) {
G_FACOS,
G_FASIN,
G_FATAN,
G_FATAN2,
G_FCOSH,
G_FSINH,
G_FTANH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,10 @@
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
# DEBUG-NEXT: G_FATAN2 (opcode {{[0-9]+}}): 1 type index, 0 imm indices
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
# DEBUG-NEXT: .. the first uncovered imm index: 0, OK
# DEBUG-NEXT: G_FCOSH (opcode {{[0-9]+}}): 1 type index, 0 imm indices
# DEBUG-NEXT: .. opcode {{[0-9]+}} is aliased to {{[0-9]+}}
# DEBUG-NEXT: .. the first uncovered type index: 1, OK
Expand Down
49 changes: 49 additions & 0 deletions llvm/test/CodeGen/SPIRV/hlsl-intrinsics/atan2.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}

; CHECK-DAG: %[[#op_ext_glsl:]] = OpExtInstImport "GLSL.std.450"
; CHECK-DAG: %[[#float_32:]] = OpTypeFloat 32
; CHECK-DAG: %[[#float_16:]] = OpTypeFloat 16
; CHECK-DAG: %[[#vec4_float_32:]] = OpTypeVector %[[#float_32]] 4
; CHECK-DAG: %[[#vec4_float_16:]] = OpTypeVector %[[#float_16]] 4

define noundef float @atan2_float(float noundef %a, float noundef %b) {
entry:
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
; CHECK: %[[#arg1:]] = OpFunctionParameter %[[#]]
; CHECK: %[[#]] = OpExtInst %[[#float_32]] %[[#op_ext_glsl]] Atan2 %[[#arg0]] %[[#arg1]]
%elt.atan2 = call float @llvm.atan2.f32(float %a, float %b)
ret float %elt.atan2
}

define noundef half @atan2_half(half noundef %a, half noundef %b) {
entry:
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
; CHECK: %[[#arg1:]] = OpFunctionParameter %[[#]]
; CHECK: %[[#]] = OpExtInst %[[#float_16]] %[[#op_ext_glsl]] Atan2 %[[#arg0]] %[[#arg1]]
%elt.atan2 = call half @llvm.atan2.f16(half %a, half %b)
ret half %elt.atan2
}

define noundef <4 x float> @atan2_float4(<4 x float> noundef %a, <4 x float> noundef %b) {
entry:
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
; CHECK: %[[#arg1:]] = OpFunctionParameter %[[#]]
; CHECK: %[[#]] = OpExtInst %[[#vec4_float_32]] %[[#op_ext_glsl]] Atan2 %[[#arg0]] %[[#arg1]]
%elt.atan2 = call <4 x float> @llvm.atan2.v4f32(<4 x float> %a, <4 x float> %b)
ret <4 x float> %elt.atan2
}

define noundef <4 x half> @atan2_half4(<4 x half> noundef %a, <4 x half> noundef %b) {
entry:
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#]]
; CHECK: %[[#arg1:]] = OpFunctionParameter %[[#]]
; CHECK: %[[#]] = OpExtInst %[[#vec4_float_16]] %[[#op_ext_glsl]] Atan2 %[[#arg0]] %[[#arg1]]
%elt.atan2 = call <4 x half> @llvm.atan2.v4f16(<4 x half> %a, <4 x half> %b)
ret <4 x half> %elt.atan2
}

declare half @llvm.atan2.f16(half, half)
declare float @llvm.atan2.f32(float, float)
declare <4 x half> @llvm.atan2.v4f16(<4 x half>, <4 x half>)
declare <4 x float> @llvm.atan2.v4f32(<4 x float>, <4 x float>)

0 comments on commit 87d72e1

Please sign in to comment.