[llvm] [DirectX] Add atan2 intrinsic and expand for DXIL backend (p1) (PR #108865)
Farzon Lotfi via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 16 12:54:47 PDT 2024
================
@@ -305,6 +306,48 @@ static Value *expandNormalizeIntrinsic(CallInst *Orig) {
return Builder.CreateFMul(X, MultiplicandVec);
}
+static Value *expandAtan2Intrinsic(CallInst *Orig) {
+ Value *Y = Orig->getOperand(0);
+ Value *X = Orig->getOperand(1);
+ Type *Ty = X->getType();
+ IRBuilder<> Builder(Orig);
+
+ Value *Tan = Builder.CreateFDiv(Y, X);
+
+ Value *Atan =
+ Builder.CreateIntrinsic(Ty, Intrinsic::atan, {Tan}, nullptr, "Elt.Atan");
+
+ Constant *Pi = ConstantFP::get(Ty, llvm::numbers::pi);
+ Constant *HalfPi = ConstantFP::get(Ty, llvm::numbers::pi / 2);
+ Constant *NegHalfPi = ConstantFP::get(Ty, -llvm::numbers::pi / 2);
+ Constant *Zero = ConstantFP::get(Ty, 0);
+
+ Value *AtanAddPi = Builder.CreateFAdd(Atan, Pi);
+ Value *AtanSubPi = Builder.CreateFSub(Atan, Pi);
+
+ Value *Result = Atan;
+
+ Value *XLt0 = Builder.CreateFCmpOLT(X, Zero);
+ Value *XEq0 = Builder.CreateFCmpOEQ(X, Zero);
+
+ Value *YGe0 = Builder.CreateFCmpOGE(Y, Zero);
+ Value *YLt0 = Builder.CreateFCmpOLT(Y, Zero);
+
+ Value *XLt0AndYGe0 = Builder.CreateAnd(XLt0, YGe0);
+ Result = Builder.CreateSelect(XLt0AndYGe0, AtanAddPi, Result);
+
+ Value *XLt0AndYLt0 = Builder.CreateAnd(XLt0, YLt0);
+ Result = Builder.CreateSelect(XLt0AndYLt0, AtanSubPi, Result);
+
+ Value *XEq0AndYLt0 = Builder.CreateAnd(XEq0, YLt0);
+ Result = Builder.CreateSelect(XEq0AndYLt0, NegHalfPi, Result);
+
+ Value *XEq0AndYGe0 = Builder.CreateAnd(XEq0, YGe0);
+ Result = Builder.CreateSelect(XEq0AndYGe0, HalfPi, Result);
+
+ return Result;
----------------
farzonl wrote:
Correct you would not apply it to the enitre expansion just to atan2. Second those examples are all examples where we go from an llvm intrinsic to another llvm intrinsic which atan2 to atan is doing, hence why I ask the question. the idea would be that if atan2 had some attributes that atan did not have then we would copy them over.
https://github.com/llvm/llvm-project/pull/108865
More information about the llvm-commits
mailing list