Skip to content

Implement the atan2 HLSL Function #70096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
8 of 14 tasks
Tracked by #99235 ...
llvm-beanz opened this issue Oct 24, 2023 · 1 comment · Fixed by #113636
Closed
8 of 14 tasks
Tracked by #99235 ...

Implement the atan2 HLSL Function #70096

llvm-beanz opened this issue Oct 24, 2023 · 1 comment · Fixed by #113636
Assignees
Labels

Comments

@llvm-beanz
Copy link
Collaborator

llvm-beanz commented Oct 24, 2023

Note atan2 has been identified as a potential generic llvm intrinsic:
Investigation: #87367
RFC: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

  • Implement atan2 clang builtin,
  • Link atan2 clang builtin with hlsl_intrinsics.h
  • Add sema checks for atan2 to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for atan2 to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/atan2.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/atan2-errors.hlsl
  • Create the int_atan2 intrinsic in Intrinsics.td
  • Create the int_dx_atan2 intrinsic in IntrinsicsDirectX.td
  • Create the DXILOpMapping of int_atan2 to 17 in DXIL.td
  • Create the atan2.ll and atan2_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create the int_spv_atan2 intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the atan2 lowering and map it to int_spv_atan2 in SPIRVInstructionSelector::selectIntrinsic.
  • In SPIRVInstructionSelector.cpp: SPIRVInstructionSelector::spvSelect Add
  case TargetOpcode::G_FATAN2:
    return selectExtInst(ResVReg, ResType, I, CL::atan2, GL::Atan2);
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/atan2.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
17 Atan 6.0 ()

SPIR-V

Atan2:

Description:

Atan2

Arc tangent. Result is an angle, in radians, whose tangent is y / x.
The signs of x and y are used to determine what quadrant the angle
is in. The range of result values is [-π, π] . Result is undefined if
x and y are both 0.

The operand x and y must be a scalar or vector whose component type
is 16-bit or 32-bit floating-point.

Result Type and the type of all operands must be the same type.
Results are computed per component.

Number Operand 1 Operand 2 Operand 3 Operand 4

25

<id>
y

<id>
x

Test Case(s)

Example 1

//dxc atan2_test.hlsl -T lib_6_8  -enable-16bit-types -spirv -fspv-target-env=universal1.5 -fcgl -O0

export float4 fn(float4 p1, float4 p2) {
    return atan2(p1, p2);
}

HLSL:

Returns the arctangent of two values (x,y).

ret atan2(y, x)

Parameters

Item Description
y
[in] The y value.
x
[in] The x value.

Return Value

The arctangent of (y,x).

Remarks

The signs of the x and y parameters are used to determine the quadrant of the return values within the range of -π to π. The atan2 HLSL intrinsic function is well-defined for every point other than the origin, even if y equals 0 and x does not equal 0.

Type Description

Name Template Type Component Type Size
y same as input x float same dimension(s) as input x
x scalar, vector, or matrix float any
ret same as input x float same dimension(s) as input x

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 2 (DirectX HLSL) and higher shader models yes
Shader Model 1 (DirectX HLSL) vs_1_1

Requirements

Requirement Value
Header
Corecrt_math.h

See also

Intrinsic Functions (DirectX HLSL)

@llvm-beanz llvm-beanz converted this from a draft issue Oct 24, 2023
@llvm-beanz llvm-beanz added the HLSL HLSL Language Support label Oct 24, 2023
@farzonl farzonl changed the title [HLSL] implement atan2 intrinsic Implement the atan2 HLSL Function Jul 16, 2024
@tex3d
Copy link
Contributor

tex3d commented Aug 29, 2024

I can take this one.

tex3d added a commit to tex3d/llvm-project that referenced this issue Sep 9, 2024
Issue: llvm#70096

Changes:
- Doc updates:
  - `clang/docs/LanguageExtensions.rst` - Document the new elementwise atan2 builtin.
  - `llvm/docs/LangRef.rst` - Document the atan2 intrinsic
- TableGen:
  - `clang/include/clang/Basic/Builtins.td` - Implement the atan2 builtin.
  - `llvm/include/llvm/IR/Intrinsics.td` - Create the atan2 intrinsic
- Sema checking:
  - `clang/lib/Sema/SemaChecking.cpp` - Add generic sema checks to the atan2 builtin
  - `clang/lib/Sema/SemaHLSL` Add HLSL specifc sema checks to the atan2 builtin
- `clang/lib/CodeGen/CGBuiltin.cpp` - invoke the atan2 intrinsic on uses of the builtin
- `clang/lib/Headers/hlsl/hlsl_intrinsics.h` - Associate the atan2 builtin with the equivalent hlsl apis
tex3d added a commit to tex3d/llvm-project that referenced this issue Sep 10, 2024
Issue: llvm#70096

Changes:
- Doc updates:
  - `clang/docs/LanguageExtensions.rst` - Document the new elementwise atan2 builtin.
  - `llvm/docs/LangRef.rst` - Document the atan2 intrinsic
- TableGen:
  - `clang/include/clang/Basic/Builtins.td` - Implement the atan2 builtin.
  - `llvm/include/llvm/IR/Intrinsics.td` - Create the atan2 intrinsic
- Sema checking:
  - `clang/lib/Sema/SemaChecking.cpp` - Add generic sema checks to the atan2 builtin
  - `clang/lib/Sema/SemaHLSL` Add HLSL specifc sema checks to the atan2 builtin
- `clang/lib/CodeGen/CGBuiltin.cpp` - invoke the atan2 intrinsic on uses of the builtin
- `clang/lib/Headers/hlsl/hlsl_intrinsics.h` - Associate the atan2 builtin with the equivalent hlsl apis
tex3d added a commit to tex3d/llvm-project that referenced this issue Sep 13, 2024
Issue: llvm#70096

Changes:
- `llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp` - Expand atan2 intrinsic using atan for DXIL.
tex3d added a commit to tex3d/llvm-project that referenced this issue Sep 13, 2024
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.
farzonl pushed a commit that referenced this issue Sep 24, 2024
This change is part of this proposal:
https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

This preliminary work adds the intrinsic to llvm and expands using atan
intrinsic for DXIL backend, since DXIL has no atan2 op.

Part 1 for Implement the atan2 HLSL Function #70096.
farzonl pushed a commit that referenced this issue Sep 25, 2024
#109878)

This change is part of this proposal:
https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

This preliminary work adds the intrinsic to llvm and expands using atan
intrinsic for DXIL backend, since DXIL has no atan2 op.

Part 1 for Implement the atan2 HLSL Function #70096.

(reland #108865 reverted in #109842 due to doc build break)
bob80905 pushed a commit that referenced this issue Sep 26, 2024
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.
tex3d added a commit to tex3d/llvm-project that referenced this issue Oct 21, 2024
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

- `VecFuncs.def`: define intrinsic to sleef/armpl mapping
- `LegalizerHelper.cpp`: add missing fewerElementsVector handling for the new atan2 intrinsic
- `AArch64ISelLowering.cpp`: Add arch64 specializations for lowering like neon instructions
- `AArch64LegalizerInfo.cpp`: Legalize atan2.

Part 5 for Implement the atan2 HLSL Function llvm#70096.
tex3d added a commit to tex3d/llvm-project that referenced this issue Oct 24, 2024
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

- `VecFuncs.def`: define intrinsic to sleef/armpl mapping
- `LegalizerHelper.cpp`: add missing fewerElementsVector handling for the new atan2 intrinsic
- `AArch64ISelLowering.cpp`: Add arch64 specializations for lowering like neon instructions
- `AArch64LegalizerInfo.cpp`: Legalize atan2.

Part 5 for Implement the atan2 HLSL Function llvm#70096.
tex3d added a commit that referenced this issue Oct 25, 2024
This change is part of this proposal:
https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

- `VecFuncs.def`: define intrinsic to sleef/armpl mapping
- `LegalizerHelper.cpp`: add missing fewerElementsVector handling for
the new atan2 intrinsic
- `AArch64ISelLowering.cpp`: Add arch64 specializations for lowering
like neon instructions
- `AArch64LegalizerInfo.cpp`: Legalize atan2.

Part 5 for Implement the atan2 HLSL Function #70096.
tex3d added a commit to tex3d/llvm-project that referenced this issue Oct 25, 2024
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

- `Builtins.td` - Add f16 support for libm atan2 builtin
- `CGBuiltin.cpp` - Emit constraint atan2 intrinsic for clang builtin

Part of Implement the atan2 HLSL Function llvm#70096.
tex3d added a commit to tex3d/llvm-project that referenced this issue Oct 25, 2024
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

This returns true from isTriviallyVectorizable for llvm.atan2 intrinsic.
It also adds llvm atan2 intrinsic equivalents to VecFuncs.def for massv.

Part of: Implement the atan2 HLSL Function llvm#70096.
tex3d added a commit to tex3d/llvm-project that referenced this issue Oct 28, 2024
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

This returns true from isTriviallyVectorizable for llvm.atan2 intrinsic.
It also adds llvm atan2 intrinsic equivalents to VecFuncs.def for massv.

Part of: Implement the atan2 HLSL Function llvm#70096.
tex3d added a commit to tex3d/llvm-project that referenced this issue Oct 28, 2024
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

- `Builtins.td` - Add f16 support for libm atan2 builtin
- `CGBuiltin.cpp` - Emit constraint atan2 intrinsic for clang builtin

Part of Implement the atan2 HLSL Function llvm#70096.
@tex3d tex3d moved this from Active to Needs Review in HLSL Support Oct 31, 2024
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this issue Nov 4, 2024
This change is part of this proposal:
https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

- `VecFuncs.def`: define intrinsic to sleef/armpl mapping
- `LegalizerHelper.cpp`: add missing fewerElementsVector handling for
the new atan2 intrinsic
- `AArch64ISelLowering.cpp`: Add arch64 specializations for lowering
like neon instructions
- `AArch64LegalizerInfo.cpp`: Legalize atan2.

Part 5 for Implement the atan2 HLSL Function llvm#70096.
tex3d added a commit to tex3d/llvm-project that referenced this issue Nov 7, 2024
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

This returns true from isTriviallyVectorizable for llvm.atan2 intrinsic.
It also adds llvm atan2 intrinsic equivalents to VecFuncs.def for massv.

Part of: Implement the atan2 HLSL Function llvm#70096.
tex3d added a commit to tex3d/llvm-project that referenced this issue Nov 7, 2024
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

- `Builtins.td` - Add f16 support for libm atan2 builtin
- `CGBuiltin.cpp` - Emit constraint atan2 intrinsic for clang builtin

Part of Implement the atan2 HLSL Function llvm#70096.
tex3d added a commit that referenced this issue Nov 9, 2024
…e veclibs (#113637)

This change is part of this proposal:
https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

- Return true for atan2 from isTriviallyVectorizable
- Add atan2 to VecFuncs.def for massv and accelerate libraries.
- Add atan2 to hasOptimizedCodeGen
- Add atan2 support in llvm/lib/Analysis/ValueTracking.cpp
llvm::getIntrinsicForCallSite and update vectorization tests
- Add atan2 name check to isLoweredToCall in
llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
- Note: there's no test coverage for these names in isLoweredToCall, except that Transforms/TailCallElim/inf-recursion.ll is impacted by the "fabs" case

Thanks to @jroelofs for the atan2 accelerate veclib and associated test
additions, plus the hasOptimizedCodeGen addition.

Part of: Implement the atan2 HLSL Function #70096.
tex3d added a commit to tex3d/llvm-project that referenced this issue Nov 12, 2024
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

- `Builtins.td` - Add f16 support for libm atan2 builtin
- `CGBuiltin.cpp` - Emit constraint atan2 intrinsic for clang builtin

Part of Implement the atan2 HLSL Function llvm#70096.
@tex3d tex3d closed this as completed in 5c2a133 Nov 12, 2024
@github-project-automation github-project-automation bot moved this from Needs Review to Closed in HLSL Support Nov 12, 2024
Groverkss pushed a commit to iree-org/llvm-project that referenced this issue Nov 15, 2024
…e veclibs (llvm#113637)

This change is part of this proposal:
https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294

- Return true for atan2 from isTriviallyVectorizable
- Add atan2 to VecFuncs.def for massv and accelerate libraries.
- Add atan2 to hasOptimizedCodeGen
- Add atan2 support in llvm/lib/Analysis/ValueTracking.cpp
llvm::getIntrinsicForCallSite and update vectorization tests
- Add atan2 name check to isLoweredToCall in
llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
- Note: there's no test coverage for these names in isLoweredToCall, except that Transforms/TailCallElim/inf-recursion.ll is impacted by the "fabs" case

Thanks to @jroelofs for the atan2 accelerate veclib and associated test
additions, plus the hasOptimizedCodeGen addition.

Part of: Implement the atan2 HLSL Function llvm#70096.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Closed
Development

Successfully merging a pull request may close this issue.

3 participants