Skip to content

Commit 47db395

Browse files
committed
[clang][hlsl] Add atan2 intrinsic part 2
Issue: llvm#70096 Changes: - `llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp` - Expand atan2 intrinsic using atan for DXIL.
1 parent 41074d3 commit 47db395

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ using namespace llvm;
3535
static bool isIntrinsicExpansion(Function &F) {
3636
switch (F.getIntrinsicID()) {
3737
case Intrinsic::abs:
38+
case Intrinsic::atan2:
3839
case Intrinsic::exp:
3940
case Intrinsic::log:
4041
case Intrinsic::log10:
@@ -305,6 +306,48 @@ static Value *expandNormalizeIntrinsic(CallInst *Orig) {
305306
return Builder.CreateFMul(X, MultiplicandVec);
306307
}
307308

309+
static Value *expandAtan2Intrinsic(CallInst *Orig) {
310+
Value *Y = Orig->getOperand(0);
311+
Value *X = Orig->getOperand(1);
312+
Type *Ty = X->getType();
313+
IRBuilder<> Builder(Orig);
314+
315+
Value *Tan = Builder.CreateFDiv(Y, X);
316+
317+
Value *Atan =
318+
Builder.CreateIntrinsic(Ty, Intrinsic::atan, {Tan}, nullptr, "Elt.Atan");
319+
320+
Constant *Pi = ConstantFP::get(Ty, llvm::numbers::pi);
321+
Constant *HalfPi = ConstantFP::get(Ty, llvm::numbers::pi / 2);
322+
Constant *NegHalfPi = ConstantFP::get(Ty, -llvm::numbers::pi / 2);
323+
Constant *Zero = ConstantFP::get(Ty, 0);
324+
325+
Value *AtanAddPi = Builder.CreateFAdd(Atan, Pi);
326+
Value *AtanSubPi = Builder.CreateFSub(Atan, Pi);
327+
328+
Value *Result = Atan;
329+
330+
Value *XLt0 = Builder.CreateFCmpOLT(X, Zero);
331+
Value *XEq0 = Builder.CreateFCmpOEQ(X, Zero);
332+
333+
Value *YGe0 = Builder.CreateFCmpOGE(Y, Zero);
334+
Value *YLt0 = Builder.CreateFCmpOLT(Y, Zero);
335+
336+
Value *XLt0AndYGe0 = Builder.CreateAnd(XLt0, YGe0);
337+
Result = Builder.CreateSelect(XLt0AndYGe0, AtanAddPi, Result);
338+
339+
Value *XLt0AndYLt0 = Builder.CreateAnd(XLt0, YLt0);
340+
Result = Builder.CreateSelect(XLt0AndYLt0, AtanSubPi, Result);
341+
342+
Value *XEq0AndYLt0 = Builder.CreateAnd(XEq0, YLt0);
343+
Result = Builder.CreateSelect(XEq0AndYLt0, NegHalfPi, Result);
344+
345+
Value *XEq0AndYGe0 = Builder.CreateAnd(XEq0, YGe0);
346+
Result = Builder.CreateSelect(XEq0AndYGe0, HalfPi, Result);
347+
348+
return Result;
349+
}
350+
308351
static Value *expandPowIntrinsic(CallInst *Orig) {
309352

310353
Value *X = Orig->getOperand(0);
@@ -394,6 +437,9 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
394437
case Intrinsic::abs:
395438
Result = expandAbs(Orig);
396439
break;
440+
case Intrinsic::atan2:
441+
Result = expandAtan2Intrinsic(Orig);
442+
break;
397443
case Intrinsic::exp:
398444
Result = expandExpIntrinsic(Orig);
399445
break;

llvm/test/CodeGen/DirectX/atan2.ll

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
; RUN: opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
2+
3+
; Make sure correct dxil expansions for atan2 are generated for float and half.
4+
5+
define noundef float @atan2_float(float noundef %y, float noundef %x) {
6+
entry:
7+
; CHECK: [[DIV:%.+]] = fdiv float %y, %x
8+
; CHECK: [[TAN:%.+]] = call float @dx.op.unary.f32(i32 17, float [[DIV]])
9+
; CHECK-DAG: [[ADD_PI:%.+]] = fadd float [[TAN]], 0x400921FB60000000
10+
; CHECK-DAG: [[SUB_PI:%.+]] = fsub float [[TAN]], 0x400921FB60000000
11+
; CHECK-DAG: [[X_LT_0:%.+]] = fcmp olt float %x, 0.000000e+00
12+
; CHECK-DAG: [[X_EQ_0:%.+]] = fcmp oeq float %x, 0.000000e+00
13+
; CHECK-DAG: [[Y_GE_0:%.+]] = fcmp oge float %y, 0.000000e+00
14+
; CHECK-DAG: [[Y_LT_0:%.+]] = fcmp olt float %y, 0.000000e+00
15+
; CHECK: [[XLT0_AND_YGE0:%.+]] = and i1 [[X_LT_0]], [[Y_GE_0]]
16+
; CHECK: [[SELECT_ADD_PI:%.+]] = select i1 [[XLT0_AND_YGE0]], float [[ADD_PI]], float [[TAN]]
17+
; CHECK: [[XLT0_AND_YLT0:%.+]] = and i1 [[X_LT_0]], [[Y_LT_0]]
18+
; CHECK: [[SELECT_SUB_PI:%.+]] = select i1 [[XLT0_AND_YLT0]], float [[SUB_PI]], float [[SELECT_ADD_PI]]
19+
; CHECK: [[XEQ0_AND_YLT0:%.+]] = and i1 [[X_EQ_0]], [[Y_LT_0]]
20+
; CHECK: [[SELECT_NEGHPI:%.+]] = select i1 [[XEQ0_AND_YLT0]], float 0xBFF921FB60000000, float [[SELECT_SUB_PI]]
21+
; CHECK: [[XEQ0_AND_YGE0:%.+]] = and i1 [[X_EQ_0]], [[Y_GE_0]]
22+
; CHECK: [[SELECT_HPI:%.+]] = select i1 [[XEQ0_AND_YGE0]], float 0x3FF921FB60000000, float [[SELECT_NEGHPI]]
23+
; CHECK: ret float [[SELECT_HPI]]
24+
%elt.atan2 = call float @llvm.atan2.f32(float %y, float %x)
25+
ret float %elt.atan2
26+
}
27+
28+
define noundef half @atan2_half(half noundef %y, half noundef %x) {
29+
entry:
30+
; CHECK: [[DIV:%.+]] = fdiv half %y, %x
31+
; CHECK: [[TAN:%.+]] = call half @dx.op.unary.f16(i32 17, half [[DIV]])
32+
; CHECK-DAG: [[ADD_PI:%.+]] = fadd half [[TAN]], 0xH4248
33+
; CHECK-DAG: [[SUB_PI:%.+]] = fsub half [[TAN]], 0xH4248
34+
; CHECK-DAG: [[X_LT_0:%.+]] = fcmp olt half %x, 0xH0000
35+
; CHECK-DAG: [[X_EQ_0:%.+]] = fcmp oeq half %x, 0xH0000
36+
; CHECK-DAG: [[Y_GE_0:%.+]] = fcmp oge half %y, 0xH0000
37+
; CHECK-DAG: [[Y_LT_0:%.+]] = fcmp olt half %y, 0xH0000
38+
; CHECK: [[XLT0_AND_YGE0:%.+]] = and i1 [[X_LT_0]], [[Y_GE_0]]
39+
; CHECK: [[SELECT_ADD_PI:%.+]] = select i1 [[XLT0_AND_YGE0]], half [[ADD_PI]], half [[TAN]]
40+
; CHECK: [[XLT0_AND_YLT0:%.+]] = and i1 [[X_LT_0]], [[Y_LT_0]]
41+
; CHECK: [[SELECT_SUB_PI:%.+]] = select i1 [[XLT0_AND_YLT0]], half [[SUB_PI]], half [[SELECT_ADD_PI]]
42+
; CHECK: [[XEQ0_AND_YLT0:%.+]] = and i1 [[X_EQ_0]], [[Y_LT_0]]
43+
; CHECK: [[SELECT_NEGHPI:%.+]] = select i1 [[XEQ0_AND_YLT0]], half 0xHBE48, half [[SELECT_SUB_PI]]
44+
; CHECK: [[XEQ0_AND_YGE0:%.+]] = and i1 [[X_EQ_0]], [[Y_GE_0]]
45+
; CHECK: [[SELECT_HPI:%.+]] = select i1 [[XEQ0_AND_YGE0]], half 0xH3E48, half [[SELECT_NEGHPI]]
46+
; CHECK: ret half [[SELECT_HPI]]
47+
%elt.atan2 = call half @llvm.atan2.f16(half %y, half %x)
48+
ret half %elt.atan2
49+
}
50+
51+
declare half @llvm.atan2.f16(half, half)
52+
declare float @llvm.atan2.f32(float, float)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; RUN: not opt -S -dxil-op-lower -mtriple=dxil-pc-shadermodel6.3-library %s 2>&1 | FileCheck %s
2+
3+
; DXIL operation atan does not support double overload type
4+
; CHECK: in function atan2_double
5+
; CHECK-SAME: Cannot create ATan operation: Invalid overload type
6+
7+
define noundef double @atan2_double(double noundef %a, double noundef %b) #0 {
8+
entry:
9+
%1 = call double @llvm.atan2.f64(double %a, double %b)
10+
ret double %1
11+
}

0 commit comments

Comments
 (0)