[flang-commits] [flang] [flang] Fix for atand(Y, X), and implment atan2d(Y, X), atanpi(X), atanpi(Y, X), atan2pi(Y, X) (PR #79002)

Yi Wu via flang-commits flang-commits at lists.llvm.org
Mon Feb 5 02:13:48 PST 2024


https://github.com/yi-wu-arm updated https://github.com/llvm/llvm-project/pull/79002

>From 2db83a2127bac8af1c448caeacdf0526901a7645 Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 19 Jan 2024 15:34:55 +0000
Subject: [PATCH 01/14] now accept two input with fake result: x/pi

---
 flang/lib/Evaluate/intrinsics.cpp             |  1 +
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 44 ++++++++++++++-----
 2 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index b76ea4733abbd..d32c0bd2b424c 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -338,6 +338,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
     {"atand", {{"x", SameFloating}}, SameFloating},
     {"atan", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atand", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
+    {"atand2", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atan2", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atan2d", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atanh", {{"x", SameFloating}}, SameFloating},
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index f84be5d49cb11..8f9da0ef4c9df 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -141,6 +141,7 @@ static constexpr IntrinsicHandler handlers[]{
      {{{"pointer", asInquired}, {"target", asInquired}}},
      /*isElemental=*/false},
     {"atand", &I::genAtand},
+    {"atand2", &I::genAtand},
     {"bessel_jn",
      &I::genBesselJn,
      {{{"n1", asValue}, {"n2", asValue}, {"x", asValue}}},
@@ -2174,16 +2175,39 @@ mlir::Value IntrinsicLibrary::genAsind(mlir::Type resultType,
 // ATAND
 mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
                                        llvm::ArrayRef<mlir::Value> args) {
-  assert(args.size() == 1);
-  mlir::MLIRContext *context = builder.getContext();
-  mlir::FunctionType ftype =
-      mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
-  mlir::Value atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
-  llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
-  mlir::Value dfactor = builder.createRealConstant(
-      loc, mlir::FloatType::getF64(context), llvm::APFloat(180.0) / pi);
-  mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
-  return builder.create<mlir::arith::MulFOp>(loc, atan, factor);
+  // llvm::outs() << "args.size()=" << args.size() << "\n";
+
+  if (args.size() == 2) {
+    // used for atand(y,x), atand(y,x)
+    // llvm::outs() << "is present.\n";
+    mlir::MLIRContext *context = builder.getContext();
+    // if Y is present, X shall be REAL.
+    assert(fir::isa_real(args[0].getType()) &&
+           "Y shall be of the same type and kind as X.");
+    assert(fir::isa_real(args[1].getType()) &&
+           "if Y is present, X shall be REAL.");
+    llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
+    return builder.createRealConstant(loc, mlir::FloatType::getF64(context),
+                                      llvm::APFloat(180.0) / pi);
+  } else if (args.size() == 1) {
+    // used for atand(x)
+    // llvm::outs() << "not resent.\n";
+    mlir::MLIRContext *context = builder.getContext();
+    mlir::FunctionType ftype =
+        mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
+
+    mlir::Value atan =
+        getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
+    llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
+    mlir::Value dfactor = builder.createRealConstant(
+        loc, mlir::FloatType::getF64(context), llvm::APFloat(180.0) / pi);
+    mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
+    return builder.create<mlir::arith::MulFOp>(loc, atan, factor);
+  } else {
+    assert((args.size() >= 1 && args.size() <= 2) &&
+           "args size not correct must between 1 and 2");
+    return;
+  }
 }
 
 // ASSOCIATED

>From 239d013f096818ee665217cb4bed9fb28a16181e Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Mon, 22 Jan 2024 15:45:49 +0000
Subject: [PATCH 02/14] add test and implement atan2d

---
 .../flang/Optimizer/Builder/IntrinsicCall.h   |  1 +
 flang/lib/Evaluate/intrinsics.cpp             | 11 ++-
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 79 +++++++++++--------
 flang/test/Lower/Intrinsics/atan2d.f90        | 28 +++++++
 .../test/Lower/Intrinsics/atand-optional.f90  | 14 ++++
 flang/test/Lower/Intrinsics/atand.f90         | 24 +++++-
 6 files changed, 119 insertions(+), 38 deletions(-)
 create mode 100644 flang/test/Lower/Intrinsics/atan2d.f90
 create mode 100644 flang/test/Lower/Intrinsics/atand-optional.f90

diff --git a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
index 59a67c49fd610..0dc75beb5a933 100644
--- a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
+++ b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
@@ -174,6 +174,7 @@ struct IntrinsicLibrary {
                                   llvm::ArrayRef<fir::ExtendedValue>);
   mlir::Value genAnint(mlir::Type, llvm::ArrayRef<mlir::Value>);
   fir::ExtendedValue genAny(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
+  mlir::Value genAtan2d(mlir::Type, llvm::ArrayRef<mlir::Value>);
   fir::ExtendedValue
       genCommandArgumentCount(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
   mlir::Value genAsind(mlir::Type, llvm::ArrayRef<mlir::Value>);
diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index d32c0bd2b424c..a1e175f584b62 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -335,12 +335,15 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
                 common::Intent::In, {ArgFlag::canBeNull}}},
         DefaultLogical, Rank::elemental, IntrinsicClass::inquiryFunction},
     {"atan", {{"x", SameFloating}}, SameFloating},
-    {"atand", {{"x", SameFloating}}, SameFloating},
     {"atan", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
-    {"atand", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
-    {"atand2", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
+    {"atand",
+        {{"y", SameReal, Rank::scalar, Optionality::required,
+             common::Intent::In},
+            {"x", SameReal, Rank::scalar, Optionality::optional,
+                common::Intent::In}},
+        SameReal, Rank::scalar, IntrinsicClass::inquiryFunction},
     {"atan2", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
-    {"atan2d", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
+    {"atan2d", {{"y", SameReal}, {"x", SameReal}}, SameReal},
     {"atanh", {{"x", SameFloating}}, SameFloating},
     {"bessel_j0", {{"x", SameReal}}, SameReal},
     {"bessel_j1", {{"x", SameReal}}, SameReal},
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 8f9da0ef4c9df..e8de022e021d9 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -140,8 +140,8 @@ static constexpr IntrinsicHandler handlers[]{
      &I::genAssociated,
      {{{"pointer", asInquired}, {"target", asInquired}}},
      /*isElemental=*/false},
+    {"atan2d", &I::genAtan2d},
     {"atand", &I::genAtand},
-    {"atand2", &I::genAtand},
     {"bessel_jn",
      &I::genBesselJn,
      {{{"n1", asValue}, {"n2", asValue}, {"x", asValue}}},
@@ -2172,42 +2172,59 @@ mlir::Value IntrinsicLibrary::genAsind(mlir::Type resultType,
   return getRuntimeCallGenerator("asin", ftype)(builder, loc, {arg});
 }
 
+// ATAND
+// ATAN2D
+mlir::Value IntrinsicLibrary::genAtan2d(mlir::Type resultType,
+                                        llvm::ArrayRef<mlir::Value> args) {
+  assert(args.size() == 2);
+
+  mlir::Value y = fir::getBase(args[0]);
+  mlir::Value x = fir::getBase(args[1]);
+
+  // When Y == 0 X must not be 0
+  mlir::Value zero = builder.createRealZeroConstant(loc, y.getType());
+  mlir::Value cmpYEq0 = builder.create<mlir::arith::CmpFOp>(
+      loc, mlir::arith::CmpFPredicate::UEQ, y, zero);
+  mlir::Value cmpXEq0 = builder.create<mlir::arith::CmpFOp>(
+      loc, mlir::arith::CmpFPredicate::UEQ, x, zero);
+  mlir::Value terminationCheck =
+      builder.create<mlir::arith::AndIOp>(loc, cmpYEq0, cmpXEq0);
+  builder.genIfThenElse(loc, terminationCheck)
+      .genThen([&]() {
+        fir::runtime::genReportFatalUserError(builder, loc,
+                                              "When Y == 0 X must not be 0");
+      })
+      .end();
+
+  // atand(y,x) atan2d(y,x) == atan2(y,x) * 180/pi
+  mlir::Value atan = builder.create<mlir::math::Atan2Op>(loc, y, x);
+  mlir::MLIRContext *context = builder.getContext();
+  llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
+  mlir::Value dfactor = builder.createRealConstant(
+      loc, mlir::FloatType::getF64(context), llvm::APFloat(180.0) / pi);
+  mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
+  return builder.create<mlir::arith::MulFOp>(loc, atan, factor);
+}
+
 // ATAND
 mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
                                        llvm::ArrayRef<mlir::Value> args) {
-  // llvm::outs() << "args.size()=" << args.size() << "\n";
+  assert(args.size() == 2);
 
-  if (args.size() == 2) {
-    // used for atand(y,x), atand(y,x)
-    // llvm::outs() << "is present.\n";
-    mlir::MLIRContext *context = builder.getContext();
-    // if Y is present, X shall be REAL.
-    assert(fir::isa_real(args[0].getType()) &&
-           "Y shall be of the same type and kind as X.");
-    assert(fir::isa_real(args[1].getType()) &&
-           "if Y is present, X shall be REAL.");
-    llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
-    return builder.createRealConstant(loc, mlir::FloatType::getF64(context),
-                                      llvm::APFloat(180.0) / pi);
-  } else if (args.size() == 1) {
-    // used for atand(x)
-    // llvm::outs() << "not resent.\n";
-    mlir::MLIRContext *context = builder.getContext();
-    mlir::FunctionType ftype =
-        mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
-
-    mlir::Value atan =
-        getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
-    llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
-    mlir::Value dfactor = builder.createRealConstant(
-        loc, mlir::FloatType::getF64(context), llvm::APFloat(180.0) / pi);
-    mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
-    return builder.create<mlir::arith::MulFOp>(loc, atan, factor);
+  mlir::Value atan;
+
+  // atand(y,x) atan2d(y,x) == atan2(y,x) * 180/pi
+  if (isStaticallyPresent(args[1])) {
+    atan = builder.create<mlir::math::Atan2Op>(loc, args[0], args[1]);
   } else {
-    assert((args.size() >= 1 && args.size() <= 2) &&
-           "args size not correct must between 1 and 2");
-    return;
+    atan = builder.create<mlir::math::AtanOp>(loc, args[0]);
   }
+  mlir::MLIRContext *context = builder.getContext();
+  llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
+  mlir::Value dfactor = builder.createRealConstant(
+      loc, mlir::FloatType::getF64(context), llvm::APFloat(180.0) / pi);
+  mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
+  return builder.create<mlir::arith::MulFOp>(loc, atan, factor);
 }
 
 // ASSOCIATED
diff --git a/flang/test/Lower/Intrinsics/atan2d.f90 b/flang/test/Lower/Intrinsics/atan2d.f90
new file mode 100644
index 0000000000000..f94c2d79c0d4b
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/atan2d.f90
@@ -0,0 +1,28 @@
+! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+
+
+function test_real4_all_args(y,x)
+  real(4) :: x, y, test_real4
+  test_real4 = atan2d(y,x)
+end function
+
+! CHECK-LABEL: @_QPtest_real4_all_args
+! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
+! CHECK: fir.if %[[terminationCheck]]
+! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
+! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
+! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
+! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f32
+
+function test_real8_all_args(y,x)
+  real(8) :: x, y, test_real8
+  test_real8 = atan2d(y,x)
+end function
+
+! CHECK-LABEL: @_QPtest_real8_all_args
+! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
+! CHECK: fir.if %[[terminationCheck]]
+! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
+! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
+! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f64
diff --git a/flang/test/Lower/Intrinsics/atand-optional.f90 b/flang/test/Lower/Intrinsics/atand-optional.f90
new file mode 100644
index 0000000000000..b3b98144ead42
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/atand-optional.f90
@@ -0,0 +1,14 @@
+! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+
+function test_real4_all_args_optional(y,x)
+  real(4), optional :: x, y
+  real(4) :: test_real4
+  test_real4 = atand(y,x)
+end function
+
+! CHECK-LABEL: @_QPtest_real4_all_args_optional
+! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
+! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
+! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
+! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f32
diff --git a/flang/test/Lower/Intrinsics/atand.f90 b/flang/test/Lower/Intrinsics/atand.f90
index 2483bef46e60f..25d882b693ed8 100644
--- a/flang/test/Lower/Intrinsics/atand.f90
+++ b/flang/test/Lower/Intrinsics/atand.f90
@@ -1,5 +1,4 @@
 ! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
-! RUN: bbc --math-runtime=precise -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
 ! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
 
 function test_real4(x)
@@ -8,7 +7,6 @@ function test_real4(x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real4
-! CHECK-PRECISE: %[[atan:.*]] = fir.call @atanf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
 ! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f32
 ! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
@@ -20,7 +18,27 @@ function test_real8(x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real8
-! CHECK-PRECISE: %[[atan:.*]] = fir.call @atan({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f64) -> f64
 ! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f64
 ! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath<contract> : f64
+
+function test_real4_all_args(y,x)
+  real(4) :: x, y, test_real4
+  test_real4 = atand(y,x)
+end function
+
+! CHECK-LABEL: @_QPtest_real4_all_args
+! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
+! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
+! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
+! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f32
+
+function test_real8_all_args(y,x)
+  real(8) :: x, y, test_real8
+  test_real8 = atand(y,x)
+end function
+
+! CHECK-LABEL: @_QPtest_real8_all_args
+! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
+! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
+! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f64

>From 6e4d16ef6c0123ebfddd17f86195cfd226c37c84 Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 26 Jan 2024 11:39:05 +0000
Subject: [PATCH 03/14] add atan atanpi atan2pi and make the one input version
 accept --math-runtime as before

---
 .../flang/Optimizer/Builder/IntrinsicCall.h   |   3 +-
 flang/lib/Evaluate/intrinsics.cpp             |  12 +-
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 107 +++++++++++++-----
 .../test/Lower/Intrinsics/atand-optional.f90  |  14 ---
 flang/test/Lower/Intrinsics/atand.f90         |   7 ++
 5 files changed, 90 insertions(+), 53 deletions(-)
 delete mode 100644 flang/test/Lower/Intrinsics/atand-optional.f90

diff --git a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
index 0dc75beb5a933..0b0e418839e4b 100644
--- a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
+++ b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
@@ -174,7 +174,8 @@ struct IntrinsicLibrary {
                                   llvm::ArrayRef<fir::ExtendedValue>);
   mlir::Value genAnint(mlir::Type, llvm::ArrayRef<mlir::Value>);
   fir::ExtendedValue genAny(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
-  mlir::Value genAtan2d(mlir::Type, llvm::ArrayRef<mlir::Value>);
+  mlir::Value genAtan(mlir::Type, llvm::ArrayRef<mlir::Value>);
+  mlir::Value genAtanpi(mlir::Type, llvm::ArrayRef<mlir::Value>);
   fir::ExtendedValue
       genCommandArgumentCount(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
   mlir::Value genAsind(mlir::Type, llvm::ArrayRef<mlir::Value>);
diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index a1e175f584b62..ec52a7e57f4aa 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -336,14 +336,12 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
         DefaultLogical, Rank::elemental, IntrinsicClass::inquiryFunction},
     {"atan", {{"x", SameFloating}}, SameFloating},
     {"atan", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
-    {"atand",
-        {{"y", SameReal, Rank::scalar, Optionality::required,
-             common::Intent::In},
-            {"x", SameReal, Rank::scalar, Optionality::optional,
-                common::Intent::In}},
-        SameReal, Rank::scalar, IntrinsicClass::inquiryFunction},
+    {"atand", {{"x", SameFloating}}, SameFloating},
+    {"atand", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atan2", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
-    {"atan2d", {{"y", SameReal}, {"x", SameReal}}, SameReal},
+    {"atanpi", {{"x", OperandReal}}, OperandReal},
+    {"atanpi", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
+    {"atan2pi", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atanh", {{"x", SameFloating}}, SameFloating},
     {"bessel_j0", {{"x", SameReal}}, SameReal},
     {"bessel_j1", {{"x", SameReal}}, SameReal},
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index e8de022e021d9..980261ce5f909 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -140,8 +140,12 @@ static constexpr IntrinsicHandler handlers[]{
      &I::genAssociated,
      {{{"pointer", asInquired}, {"target", asInquired}}},
      /*isElemental=*/false},
-    {"atan2d", &I::genAtan2d},
+    {"atan", &I::genAtan},
+    {"atan2", &I::genAtan},
+    {"atan2d", &I::genAtand},
+    {"atan2pi", &I::genAtanpi},
     {"atand", &I::genAtand},
+    {"atanpi", &I::genAtanpi},
     {"bessel_jn",
      &I::genBesselJn,
      {{{"n1", asValue}, {"n2", asValue}, {"x", asValue}}},
@@ -906,16 +910,16 @@ static constexpr MathOperation mathOperations[] = {
      genLibCall},
     {"asinh", "casinh", genFuncType<Ty::Complex<8>, Ty::Complex<8>>,
      genLibCall},
-    {"atan", "atanf", genFuncType<Ty::Real<4>, Ty::Real<4>>,
-     genMathOp<mlir::math::AtanOp>},
-    {"atan", "atan", genFuncType<Ty::Real<8>, Ty::Real<8>>,
-     genMathOp<mlir::math::AtanOp>},
+    // {"atan", "atanf", genFuncType<Ty::Real<4>, Ty::Real<4>>,
+    //  genMathOp<mlir::math::AtanOp>},
+    // {"atan", "atan", genFuncType<Ty::Real<8>, Ty::Real<8>>,
+    //  genMathOp<mlir::math::AtanOp>},
     {"atan", "catanf", genFuncType<Ty::Complex<4>, Ty::Complex<4>>, genLibCall},
     {"atan", "catan", genFuncType<Ty::Complex<8>, Ty::Complex<8>>, genLibCall},
-    {"atan2", "atan2f", genFuncType<Ty::Real<4>, Ty::Real<4>, Ty::Real<4>>,
-     genMathOp<mlir::math::Atan2Op>},
-    {"atan2", "atan2", genFuncType<Ty::Real<8>, Ty::Real<8>, Ty::Real<8>>,
-     genMathOp<mlir::math::Atan2Op>},
+    // {"atan2", "atan2f", genFuncType<Ty::Real<4>, Ty::Real<4>, Ty::Real<4>>,
+    //  genMathOp<mlir::math::Atan2Op>},
+    // {"atan2", "atan2", genFuncType<Ty::Real<8>, Ty::Real<8>, Ty::Real<8>>,
+    //  genMathOp<mlir::math::Atan2Op>},
     {"atanh", "atanhf", genFuncType<Ty::Real<4>, Ty::Real<4>>, genLibCall},
     {"atanh", "atanh", genFuncType<Ty::Real<8>, Ty::Real<8>>, genLibCall},
     {"atanh", "catanhf", genFuncType<Ty::Complex<4>, Ty::Complex<4>>,
@@ -2173,14 +2177,8 @@ mlir::Value IntrinsicLibrary::genAsind(mlir::Type resultType,
 }
 
 // ATAND
-// ATAN2D
-mlir::Value IntrinsicLibrary::genAtan2d(mlir::Type resultType,
-                                        llvm::ArrayRef<mlir::Value> args) {
-  assert(args.size() == 2);
-
-  mlir::Value y = fir::getBase(args[0]);
-  mlir::Value x = fir::getBase(args[1]);
-
+void static atanNoneZeroCheck(mlir::Value y, mlir::Value x,
+                              fir::FirOpBuilder &builder, mlir::Location loc) {
   // When Y == 0 X must not be 0
   mlir::Value zero = builder.createRealZeroConstant(loc, y.getType());
   mlir::Value cmpYEq0 = builder.create<mlir::arith::CmpFOp>(
@@ -2195,10 +2193,51 @@ mlir::Value IntrinsicLibrary::genAtan2d(mlir::Type resultType,
                                               "When Y == 0 X must not be 0");
       })
       .end();
+}
+
+// ATAN, ATAN2
+mlir::Value IntrinsicLibrary::genAtan(mlir::Type resultType,
+                                      llvm::ArrayRef<mlir::Value> args) {
+  // assert for: atan(X), atan(Y,X), atan2(Y,X)
+  assert(args.size() >= 1 && args.size() <= 2);
+
+  mlir::Value atan;
+
+  // atan(Y,X) atan2(Y,X) == atan2(Y,X)
+  if (args.size() == 2) {
+    mlir::Value y = fir::getBase(args[0]);
+    mlir::Value x = fir::getBase(args[1]);
+    atanNoneZeroCheck(y, x, builder, loc);
+    atan = builder.create<mlir::math::Atan2Op>(loc, y, x);
+  } else {
+    mlir::MLIRContext *context = builder.getContext();
+    mlir::FunctionType ftype =
+        mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
+    atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
+  }
+  return atan;
+}
+
+// ATAND, ATAN2D
+mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
+                                       llvm::ArrayRef<mlir::Value> args) {
+  // assert for: atand(X), atand(Y,X), atan2d(Y,X)
+  assert(args.size() >= 1 && args.size() <= 2);
 
-  // atand(y,x) atan2d(y,x) == atan2(y,x) * 180/pi
-  mlir::Value atan = builder.create<mlir::math::Atan2Op>(loc, y, x);
+  mlir::Value atan;
   mlir::MLIRContext *context = builder.getContext();
+
+  // atand(Y,X) atan2d(Y,X) == atan2(Y,X) * 180/pi
+  if (args.size() == 2) {
+    mlir::Value y = fir::getBase(args[0]);
+    mlir::Value x = fir::getBase(args[1]);
+    atanNoneZeroCheck(y, x, builder, loc);
+    atan = builder.create<mlir::math::Atan2Op>(loc, y, x);
+  } else {
+    mlir::FunctionType ftype =
+        mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
+    atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
+  }
   llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
   mlir::Value dfactor = builder.createRealConstant(
       loc, mlir::FloatType::getF64(context), llvm::APFloat(180.0) / pi);
@@ -2206,25 +2245,31 @@ mlir::Value IntrinsicLibrary::genAtan2d(mlir::Type resultType,
   return builder.create<mlir::arith::MulFOp>(loc, atan, factor);
 }
 
-// ATAND
-mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
-                                       llvm::ArrayRef<mlir::Value> args) {
-  assert(args.size() == 2);
+// ATAN, ATAN2PI
+mlir::Value IntrinsicLibrary::genAtanpi(mlir::Type resultType,
+                                        llvm::ArrayRef<mlir::Value> args) {
+  // assert for: atand(X), atand(Y,X), atan2d(Y,X)
+  assert(args.size() >= 1 && args.size() <= 2);
 
   mlir::Value atan;
+  mlir::MLIRContext *context = builder.getContext();
 
-  // atand(y,x) atan2d(y,x) == atan2(y,x) * 180/pi
-  if (isStaticallyPresent(args[1])) {
-    atan = builder.create<mlir::math::Atan2Op>(loc, args[0], args[1]);
+  // atand(Y,X) atan2d(Y,X) == atan2(Y,X) * 180/pi
+  if (args.size() == 2) {
+    mlir::Value y = fir::getBase(args[0]);
+    mlir::Value x = fir::getBase(args[1]);
+    atanNoneZeroCheck(y, x, builder, loc);
+    atan = builder.create<mlir::math::Atan2Op>(loc, y, x);
   } else {
-    atan = builder.create<mlir::math::AtanOp>(loc, args[0]);
+    mlir::FunctionType ftype =
+        mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
+    atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
   }
-  mlir::MLIRContext *context = builder.getContext();
   llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
-  mlir::Value dfactor = builder.createRealConstant(
-      loc, mlir::FloatType::getF64(context), llvm::APFloat(180.0) / pi);
+  mlir::Value dfactor =
+      builder.createRealConstant(loc, mlir::FloatType::getF64(context), pi);
   mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
-  return builder.create<mlir::arith::MulFOp>(loc, atan, factor);
+  return builder.create<mlir::arith::DivFOp>(loc, atan, factor);
 }
 
 // ASSOCIATED
diff --git a/flang/test/Lower/Intrinsics/atand-optional.f90 b/flang/test/Lower/Intrinsics/atand-optional.f90
deleted file mode 100644
index b3b98144ead42..0000000000000
--- a/flang/test/Lower/Intrinsics/atand-optional.f90
+++ /dev/null
@@ -1,14 +0,0 @@
-! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
-! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
-
-function test_real4_all_args_optional(y,x)
-  real(4), optional :: x, y
-  real(4) :: test_real4
-  test_real4 = atand(y,x)
-end function
-
-! CHECK-LABEL: @_QPtest_real4_all_args_optional
-! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
-! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
-! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
-! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f32
diff --git a/flang/test/Lower/Intrinsics/atand.f90 b/flang/test/Lower/Intrinsics/atand.f90
index 25d882b693ed8..adb6f2005b069 100644
--- a/flang/test/Lower/Intrinsics/atand.f90
+++ b/flang/test/Lower/Intrinsics/atand.f90
@@ -1,4 +1,5 @@
 ! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+! RUN: bbc --math-runtime=precise -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
 ! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
 
 function test_real4(x)
@@ -7,6 +8,7 @@ function test_real4(x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real4
+! CHECK-PRECISE: %[[atan:.*]] = fir.call @atanf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
 ! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f32
 ! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
@@ -18,6 +20,7 @@ function test_real8(x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real8
+! CHECK-PRECISE: %[[atan:.*]] = fir.call @atanf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
 ! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f64
 ! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath<contract> : f64
@@ -28,6 +31,8 @@ function test_real4_all_args(y,x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real4_all_args
+! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
+! CHECK: fir.if %[[terminationCheck]]
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
 ! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
@@ -39,6 +44,8 @@ function test_real8_all_args(y,x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real8_all_args
+! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
+! CHECK: fir.if %[[terminationCheck]]
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
 ! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f64

>From dc81c5eb4f67233fedd49a9d9f5086e897c8f6be Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 26 Jan 2024 13:55:22 +0000
Subject: [PATCH 04/14] atanpi and atan2pi implementation and test fixes

---
 flang/lib/Evaluate/intrinsics.cpp             |  1 +
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 32 ++++++------
 flang/test/Lower/Intrinsics/atan2pi.f90       | 28 ++++++++++
 flang/test/Lower/Intrinsics/atand.f90         |  6 +--
 flang/test/Lower/Intrinsics/atandpi.f90       | 52 +++++++++++++++++++
 5 files changed, 100 insertions(+), 19 deletions(-)
 create mode 100644 flang/test/Lower/Intrinsics/atan2pi.f90
 create mode 100644 flang/test/Lower/Intrinsics/atandpi.f90

diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index ec52a7e57f4aa..b6c7a3b134e9a 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -339,6 +339,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
     {"atand", {{"x", SameFloating}}, SameFloating},
     {"atand", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atan2", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
+    {"atan2d", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atanpi", {{"x", OperandReal}}, OperandReal},
     {"atanpi", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atan2pi", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 980261ce5f909..b2cc6b4f09297 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -140,8 +140,8 @@ static constexpr IntrinsicHandler handlers[]{
      &I::genAssociated,
      {{{"pointer", asInquired}, {"target", asInquired}}},
      /*isElemental=*/false},
-    {"atan", &I::genAtan},
-    {"atan2", &I::genAtan},
+    // {"atan", &I::genAtan},
+    // {"atan2", &I::genAtan},
     {"atan2d", &I::genAtand},
     {"atan2pi", &I::genAtanpi},
     {"atand", &I::genAtand},
@@ -910,16 +910,16 @@ static constexpr MathOperation mathOperations[] = {
      genLibCall},
     {"asinh", "casinh", genFuncType<Ty::Complex<8>, Ty::Complex<8>>,
      genLibCall},
-    // {"atan", "atanf", genFuncType<Ty::Real<4>, Ty::Real<4>>,
-    //  genMathOp<mlir::math::AtanOp>},
-    // {"atan", "atan", genFuncType<Ty::Real<8>, Ty::Real<8>>,
-    //  genMathOp<mlir::math::AtanOp>},
+    {"atan", "atanf", genFuncType<Ty::Real<4>, Ty::Real<4>>,
+     genMathOp<mlir::math::AtanOp>},
+    {"atan", "atan", genFuncType<Ty::Real<8>, Ty::Real<8>>,
+     genMathOp<mlir::math::AtanOp>},
     {"atan", "catanf", genFuncType<Ty::Complex<4>, Ty::Complex<4>>, genLibCall},
     {"atan", "catan", genFuncType<Ty::Complex<8>, Ty::Complex<8>>, genLibCall},
-    // {"atan2", "atan2f", genFuncType<Ty::Real<4>, Ty::Real<4>, Ty::Real<4>>,
-    //  genMathOp<mlir::math::Atan2Op>},
-    // {"atan2", "atan2", genFuncType<Ty::Real<8>, Ty::Real<8>, Ty::Real<8>>,
-    //  genMathOp<mlir::math::Atan2Op>},
+    {"atan2", "atan2f", genFuncType<Ty::Real<4>, Ty::Real<4>, Ty::Real<4>>,
+     genMathOp<mlir::math::Atan2Op>},
+    {"atan2", "atan2", genFuncType<Ty::Real<8>, Ty::Real<8>, Ty::Real<8>>,
+     genMathOp<mlir::math::Atan2Op>},
     {"atanh", "atanhf", genFuncType<Ty::Real<4>, Ty::Real<4>>, genLibCall},
     {"atanh", "atanh", genFuncType<Ty::Real<8>, Ty::Real<8>>, genLibCall},
     {"atanh", "catanhf", genFuncType<Ty::Complex<4>, Ty::Complex<4>>,
@@ -2201,6 +2201,9 @@ mlir::Value IntrinsicLibrary::genAtan(mlir::Type resultType,
   // assert for: atan(X), atan(Y,X), atan2(Y,X)
   assert(args.size() >= 1 && args.size() <= 2);
 
+  mlir::MLIRContext *context = builder.getContext();
+  mlir::FunctionType ftype =
+        mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
   mlir::Value atan;
 
   // atan(Y,X) atan2(Y,X) == atan2(Y,X)
@@ -2210,9 +2213,6 @@ mlir::Value IntrinsicLibrary::genAtan(mlir::Type resultType,
     atanNoneZeroCheck(y, x, builder, loc);
     atan = builder.create<mlir::math::Atan2Op>(loc, y, x);
   } else {
-    mlir::MLIRContext *context = builder.getContext();
-    mlir::FunctionType ftype =
-        mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
     atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
   }
   return atan;
@@ -2224,8 +2224,8 @@ mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
   // assert for: atand(X), atand(Y,X), atan2d(Y,X)
   assert(args.size() >= 1 && args.size() <= 2);
 
-  mlir::Value atan;
   mlir::MLIRContext *context = builder.getContext();
+  mlir::Value atan;
 
   // atand(Y,X) atan2d(Y,X) == atan2(Y,X) * 180/pi
   if (args.size() == 2) {
@@ -2234,8 +2234,8 @@ mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
     atanNoneZeroCheck(y, x, builder, loc);
     atan = builder.create<mlir::math::Atan2Op>(loc, y, x);
   } else {
-    mlir::FunctionType ftype =
-        mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
+      mlir::FunctionType ftype =
+  mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
     atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
   }
   llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
diff --git a/flang/test/Lower/Intrinsics/atan2pi.f90 b/flang/test/Lower/Intrinsics/atan2pi.f90
new file mode 100644
index 0000000000000..5dedf2c349261
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/atan2pi.f90
@@ -0,0 +1,28 @@
+! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+
+
+function test_real4_all_args(y,x)
+  real(4) :: x, y, test_real4
+  test_real4 = atan2pi(y,x)
+end function
+
+! CHECK-LABEL: @_QPtest_real4_all_args
+! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
+! CHECK: fir.if %[[terminationCheck]]
+! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
+! CHECK: %[[dpi:.*]] = arith.constant 3.1415926535897931 : f64
+! CHECK: %[[pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
+! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f32
+
+function test_real8_all_args(y,x)
+  real(8) :: x, y, test_real8
+  test_real8 = atan2pi(y,x)
+end function
+
+! CHECK-LABEL: @_QPtest_real8_all_args
+! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
+! CHECK: fir.if %[[terminationCheck]]
+! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
+! CHECK: %[[pi:.*]] = arith.constant 3.1415926535897931 : f64
+! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f64
diff --git a/flang/test/Lower/Intrinsics/atand.f90 b/flang/test/Lower/Intrinsics/atand.f90
index adb6f2005b069..55d9ad2b93992 100644
--- a/flang/test/Lower/Intrinsics/atand.f90
+++ b/flang/test/Lower/Intrinsics/atand.f90
@@ -20,7 +20,7 @@ function test_real8(x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real8
-! CHECK-PRECISE: %[[atan:.*]] = fir.call @atanf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
+! CHECK-PRECISE: %[[atan:.*]] = fir.call @atan({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f64) -> f64
 ! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f64
 ! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath<contract> : f64
@@ -33,7 +33,7 @@ function test_real4_all_args(y,x)
 ! CHECK-LABEL: @_QPtest_real4_all_args
 ! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
 ! CHECK: fir.if %[[terminationCheck]]
-! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
+! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
 ! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f32
@@ -46,6 +46,6 @@ function test_real8_all_args(y,x)
 ! CHECK-LABEL: @_QPtest_real8_all_args
 ! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
 ! CHECK: fir.if %[[terminationCheck]]
-! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
+! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
 ! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f64
diff --git a/flang/test/Lower/Intrinsics/atandpi.f90 b/flang/test/Lower/Intrinsics/atandpi.f90
new file mode 100644
index 0000000000000..5889a0745021b
--- /dev/null
+++ b/flang/test/Lower/Intrinsics/atandpi.f90
@@ -0,0 +1,52 @@
+! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+! RUN: bbc --math-runtime=precise -emit-fir -hlfir=false %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-PRECISE"
+! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
+
+function test_real4(x)
+  real :: x, test_real4
+  test_real4 = atanpi(x)
+end function
+
+! CHECK-LABEL: @_QPtest_real4
+! CHECK-PRECISE: %[[atan:.*]] = fir.call @atanf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
+! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f32
+! CHECK: %[[dpi:.*]] = arith.constant 3.1415926535897931 : f64
+! CHECK: %[[pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
+! CHECK: %{{.*}} = arith.divf %[[atan]], %[[pi]] fastmath<contract> : f32
+
+function test_real8(x)
+  real(8) :: x, test_real8
+  test_real8 = atanpi(x)
+end function
+
+! CHECK-LABEL: @_QPtest_real8
+! CHECK-PRECISE: %[[atan:.*]] = fir.call @atan({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f64) -> f64
+! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f64
+! CHECK: %[[pi:.*]] = arith.constant 3.1415926535897931 : f64
+! CHECK: %{{.*}} = arith.divf %[[atan]], %[[pi]] fastmath<contract> : f64
+
+function test_real4_all_args(y,x)
+  real(4) :: x, y, test_real4
+  test_real4 = atanpi(y,x)
+end function
+
+! CHECK-LABEL: @_QPtest_real4_all_args
+! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
+! CHECK: fir.if %[[terminationCheck]]
+! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
+! CHECK: %[[dpi:.*]] = arith.constant 3.1415926535897931 : f64
+! CHECK: %[[pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
+! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f32
+
+function test_real8_all_args(y,x)
+  real(8) :: x, y, test_real8
+  test_real8 = atanpi(y,x)
+end function
+
+! CHECK-LABEL: @_QPtest_real8_all_args
+! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
+! CHECK: fir.if %[[terminationCheck]]
+! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
+! CHECK: %[[pi:.*]] = arith.constant 3.1415926535897931 : f64
+! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f64
+

>From 0fc11f3665154b3a83e390742fde928db914e560 Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 26 Jan 2024 14:27:14 +0000
Subject: [PATCH 05/14] remove genatan for atan and atan2, no need to rewrite
 it

---
 .../flang/Optimizer/Builder/IntrinsicCall.h   |  1 -
 flang/lib/Evaluate/intrinsics.cpp             |  2 +-
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 29 ++-----------------
 3 files changed, 3 insertions(+), 29 deletions(-)

diff --git a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
index 0b0e418839e4b..3f1e22ecca4cc 100644
--- a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
+++ b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
@@ -174,7 +174,6 @@ struct IntrinsicLibrary {
                                   llvm::ArrayRef<fir::ExtendedValue>);
   mlir::Value genAnint(mlir::Type, llvm::ArrayRef<mlir::Value>);
   fir::ExtendedValue genAny(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
-  mlir::Value genAtan(mlir::Type, llvm::ArrayRef<mlir::Value>);
   mlir::Value genAtanpi(mlir::Type, llvm::ArrayRef<mlir::Value>);
   fir::ExtendedValue
       genCommandArgumentCount(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index b6c7a3b134e9a..61bf0f2b48ad8 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -340,7 +340,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
     {"atand", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atan2", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atan2d", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
-    {"atanpi", {{"x", OperandReal}}, OperandReal},
+    {"atanpi", {{"x", SameFloating}}, SameFloating},
     {"atanpi", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atan2pi", {{"y", OperandReal}, {"x", OperandReal}}, OperandReal},
     {"atanh", {{"x", SameFloating}}, SameFloating},
diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index b2cc6b4f09297..d5c30dc4798a0 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -140,8 +140,6 @@ static constexpr IntrinsicHandler handlers[]{
      &I::genAssociated,
      {{{"pointer", asInquired}, {"target", asInquired}}},
      /*isElemental=*/false},
-    // {"atan", &I::genAtan},
-    // {"atan2", &I::genAtan},
     {"atan2d", &I::genAtand},
     {"atan2pi", &I::genAtanpi},
     {"atand", &I::genAtand},
@@ -2195,29 +2193,6 @@ void static atanNoneZeroCheck(mlir::Value y, mlir::Value x,
       .end();
 }
 
-// ATAN, ATAN2
-mlir::Value IntrinsicLibrary::genAtan(mlir::Type resultType,
-                                      llvm::ArrayRef<mlir::Value> args) {
-  // assert for: atan(X), atan(Y,X), atan2(Y,X)
-  assert(args.size() >= 1 && args.size() <= 2);
-
-  mlir::MLIRContext *context = builder.getContext();
-  mlir::FunctionType ftype =
-        mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
-  mlir::Value atan;
-
-  // atan(Y,X) atan2(Y,X) == atan2(Y,X)
-  if (args.size() == 2) {
-    mlir::Value y = fir::getBase(args[0]);
-    mlir::Value x = fir::getBase(args[1]);
-    atanNoneZeroCheck(y, x, builder, loc);
-    atan = builder.create<mlir::math::Atan2Op>(loc, y, x);
-  } else {
-    atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
-  }
-  return atan;
-}
-
 // ATAND, ATAN2D
 mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
                                        llvm::ArrayRef<mlir::Value> args) {
@@ -2234,8 +2209,8 @@ mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
     atanNoneZeroCheck(y, x, builder, loc);
     atan = builder.create<mlir::math::Atan2Op>(loc, y, x);
   } else {
-      mlir::FunctionType ftype =
-  mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
+    mlir::FunctionType ftype =
+        mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
     atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
   }
   llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);

>From ae80d521dc3b17a5236c76e528d2a5276d8a690c Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 26 Jan 2024 14:40:21 +0000
Subject: [PATCH 06/14] format and comment

---
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 4 ++--
 flang/test/Lower/Intrinsics/atandpi.f90       | 1 -
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index d5c30dc4798a0..7b0a22d2bf3ab 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -2223,13 +2223,13 @@ mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
 // ATAN, ATAN2PI
 mlir::Value IntrinsicLibrary::genAtanpi(mlir::Type resultType,
                                         llvm::ArrayRef<mlir::Value> args) {
-  // assert for: atand(X), atand(Y,X), atan2d(Y,X)
+  // assert for: atanpi(X), atanpi(Y,X), atan2pi(Y,X)
   assert(args.size() >= 1 && args.size() <= 2);
 
   mlir::Value atan;
   mlir::MLIRContext *context = builder.getContext();
 
-  // atand(Y,X) atan2d(Y,X) == atan2(Y,X) * 180/pi
+  // atanpi(Y,X) atan2pi(Y,X) == atan2(Y,X) / pi
   if (args.size() == 2) {
     mlir::Value y = fir::getBase(args[0]);
     mlir::Value x = fir::getBase(args[1]);
diff --git a/flang/test/Lower/Intrinsics/atandpi.f90 b/flang/test/Lower/Intrinsics/atandpi.f90
index 5889a0745021b..3d3d0416c72fe 100644
--- a/flang/test/Lower/Intrinsics/atandpi.f90
+++ b/flang/test/Lower/Intrinsics/atandpi.f90
@@ -49,4 +49,3 @@ function test_real8_all_args(y,x)
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
 ! CHECK: %[[pi:.*]] = arith.constant 3.1415926535897931 : f64
 ! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f64
-

>From 205d279d81f5a24fea5f50df73c10d8bec22eb80 Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 26 Jan 2024 16:37:27 +0000
Subject: [PATCH 07/14] rename test functions

---
 flang/test/Lower/Intrinsics/atan2d.f90  | 8 ++++----
 flang/test/Lower/Intrinsics/atan2pi.f90 | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/flang/test/Lower/Intrinsics/atan2d.f90 b/flang/test/Lower/Intrinsics/atan2d.f90
index f94c2d79c0d4b..dca60704bf9e8 100644
--- a/flang/test/Lower/Intrinsics/atan2d.f90
+++ b/flang/test/Lower/Intrinsics/atan2d.f90
@@ -2,12 +2,12 @@
 ! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
 
 
-function test_real4_all_args(y,x)
+function test_real4(y,x)
   real(4) :: x, y, test_real4
   test_real4 = atan2d(y,x)
 end function
 
-! CHECK-LABEL: @_QPtest_real4_all_args
+! CHECK-LABEL: @_QPtest_real4
 ! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
 ! CHECK: fir.if %[[terminationCheck]]
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
@@ -15,12 +15,12 @@ function test_real4_all_args(y,x)
 ! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f32
 
-function test_real8_all_args(y,x)
+function test_real8(y,x)
   real(8) :: x, y, test_real8
   test_real8 = atan2d(y,x)
 end function
 
-! CHECK-LABEL: @_QPtest_real8_all_args
+! CHECK-LABEL: @_QPtest_real8
 ! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
 ! CHECK: fir.if %[[terminationCheck]]
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
diff --git a/flang/test/Lower/Intrinsics/atan2pi.f90 b/flang/test/Lower/Intrinsics/atan2pi.f90
index 5dedf2c349261..ec0a4e17ddf95 100644
--- a/flang/test/Lower/Intrinsics/atan2pi.f90
+++ b/flang/test/Lower/Intrinsics/atan2pi.f90
@@ -2,12 +2,12 @@
 ! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck %s --check-prefixes="CHECK,CHECK-FAST"
 
 
-function test_real4_all_args(y,x)
+function test_real4(y,x)
   real(4) :: x, y, test_real4
   test_real4 = atan2pi(y,x)
 end function
 
-! CHECK-LABEL: @_QPtest_real4_all_args
+! CHECK-LABEL: @_QPtest_real4
 ! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
 ! CHECK: fir.if %[[terminationCheck]]
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
@@ -15,12 +15,12 @@ function test_real4_all_args(y,x)
 ! CHECK: %[[pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
 ! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f32
 
-function test_real8_all_args(y,x)
+function test_real8(y,x)
   real(8) :: x, y, test_real8
   test_real8 = atan2pi(y,x)
 end function
 
-! CHECK-LABEL: @_QPtest_real8_all_args
+! CHECK-LABEL: @_QPtest_real8
 ! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
 ! CHECK: fir.if %[[terminationCheck]]
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64

>From caa1c4953c196f101577c68ed08208623c8a934c Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Mon, 29 Jan 2024 15:36:50 +0000
Subject: [PATCH 08/14] rename functions

---
 flang/test/Lower/Intrinsics/atand.f90   | 8 ++++----
 flang/test/Lower/Intrinsics/atandpi.f90 | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/flang/test/Lower/Intrinsics/atand.f90 b/flang/test/Lower/Intrinsics/atand.f90
index 55d9ad2b93992..55c79f0687543 100644
--- a/flang/test/Lower/Intrinsics/atand.f90
+++ b/flang/test/Lower/Intrinsics/atand.f90
@@ -25,12 +25,12 @@ function test_real8(x)
 ! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[factor]] fastmath<contract> : f64
 
-function test_real4_all_args(y,x)
+function test_real4_yx(y,x)
   real(4) :: x, y, test_real4
   test_real4 = atand(y,x)
 end function
 
-! CHECK-LABEL: @_QPtest_real4_all_args
+! CHECK-LABEL: @_QPtest_real4_yx
 ! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
 ! CHECK: fir.if %[[terminationCheck]]
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
@@ -38,12 +38,12 @@ function test_real4_all_args(y,x)
 ! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f32
 
-function test_real8_all_args(y,x)
+function test_real8_yx(y,x)
   real(8) :: x, y, test_real8
   test_real8 = atand(y,x)
 end function
 
-! CHECK-LABEL: @_QPtest_real8_all_args
+! CHECK-LABEL: @_QPtest_real8
 ! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
 ! CHECK: fir.if %[[terminationCheck]]
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
diff --git a/flang/test/Lower/Intrinsics/atandpi.f90 b/flang/test/Lower/Intrinsics/atandpi.f90
index 3d3d0416c72fe..aa1cba164054b 100644
--- a/flang/test/Lower/Intrinsics/atandpi.f90
+++ b/flang/test/Lower/Intrinsics/atandpi.f90
@@ -25,12 +25,12 @@ function test_real8(x)
 ! CHECK: %[[pi:.*]] = arith.constant 3.1415926535897931 : f64
 ! CHECK: %{{.*}} = arith.divf %[[atan]], %[[pi]] fastmath<contract> : f64
 
-function test_real4_all_args(y,x)
+function test_real4_yx(y,x)
   real(4) :: x, y, test_real4
   test_real4 = atanpi(y,x)
 end function
 
-! CHECK-LABEL: @_QPtest_real4_all_args
+! CHECK-LABEL: @_QPtest_real4_yx
 ! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
 ! CHECK: fir.if %[[terminationCheck]]
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
@@ -38,12 +38,12 @@ function test_real4_all_args(y,x)
 ! CHECK: %[[pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
 ! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f32
 
-function test_real8_all_args(y,x)
+function test_real8_yx(y,x)
   real(8) :: x, y, test_real8
   test_real8 = atanpi(y,x)
 end function
 
-! CHECK-LABEL: @_QPtest_real8_all_args
+! CHECK-LABEL: @_QPtest_real8_yx
 ! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
 ! CHECK: fir.if %[[terminationCheck]]
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64

>From 3e418dcf7f6e34de6c922afffb1dbfd1862a9c1b Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Mon, 29 Jan 2024 16:00:59 +0000
Subject: [PATCH 09/14] more function renaming

---
 flang/test/Lower/Intrinsics/atand.f90 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/test/Lower/Intrinsics/atand.f90 b/flang/test/Lower/Intrinsics/atand.f90
index 55c79f0687543..27189b926ade5 100644
--- a/flang/test/Lower/Intrinsics/atand.f90
+++ b/flang/test/Lower/Intrinsics/atand.f90
@@ -43,7 +43,7 @@ function test_real8_yx(y,x)
   test_real8 = atand(y,x)
 end function
 
-! CHECK-LABEL: @_QPtest_real8
+! CHECK-LABEL: @_QPtest_real8_yx
 ! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
 ! CHECK: fir.if %[[terminationCheck]]
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64

>From a28bd0e6defad7d42155bb41ecf50df4ea1202c2 Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Wed, 31 Jan 2024 11:33:48 +0000
Subject: [PATCH 10/14] remove zero termination check and correct test

---
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 12 ++++--------
 flang/test/Lower/Intrinsics/atan2d.f90        |  4 ----
 flang/test/Lower/Intrinsics/atan2pi.f90       |  4 ----
 flang/test/Lower/Intrinsics/atand.f90         |  4 ----
 flang/test/Lower/Intrinsics/atandpi.f90       |  4 ----
 5 files changed, 4 insertions(+), 24 deletions(-)

diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 7b0a22d2bf3ab..13806b00c8a20 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -2204,10 +2204,8 @@ mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
 
   // atand(Y,X) atan2d(Y,X) == atan2(Y,X) * 180/pi
   if (args.size() == 2) {
-    mlir::Value y = fir::getBase(args[0]);
-    mlir::Value x = fir::getBase(args[1]);
-    atanNoneZeroCheck(y, x, builder, loc);
-    atan = builder.create<mlir::math::Atan2Op>(loc, y, x);
+    atan = builder.create<mlir::math::Atan2Op>(loc, fir::getBase(args[0]),
+                                               fir::getBase(args[1]));
   } else {
     mlir::FunctionType ftype =
         mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
@@ -2231,10 +2229,8 @@ mlir::Value IntrinsicLibrary::genAtanpi(mlir::Type resultType,
 
   // atanpi(Y,X) atan2pi(Y,X) == atan2(Y,X) / pi
   if (args.size() == 2) {
-    mlir::Value y = fir::getBase(args[0]);
-    mlir::Value x = fir::getBase(args[1]);
-    atanNoneZeroCheck(y, x, builder, loc);
-    atan = builder.create<mlir::math::Atan2Op>(loc, y, x);
+    atan = builder.create<mlir::math::Atan2Op>(loc, fir::getBase(args[0]),
+                                               fir::getBase(args[1]));
   } else {
     mlir::FunctionType ftype =
         mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
diff --git a/flang/test/Lower/Intrinsics/atan2d.f90 b/flang/test/Lower/Intrinsics/atan2d.f90
index dca60704bf9e8..6ebf2976266ab 100644
--- a/flang/test/Lower/Intrinsics/atan2d.f90
+++ b/flang/test/Lower/Intrinsics/atan2d.f90
@@ -8,8 +8,6 @@ function test_real4(y,x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real4
-! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
-! CHECK: fir.if %[[terminationCheck]]
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
 ! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
@@ -21,8 +19,6 @@ function test_real8(y,x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real8
-! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
-! CHECK: fir.if %[[terminationCheck]]
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
 ! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f64
diff --git a/flang/test/Lower/Intrinsics/atan2pi.f90 b/flang/test/Lower/Intrinsics/atan2pi.f90
index ec0a4e17ddf95..16b8eb4eba3e5 100644
--- a/flang/test/Lower/Intrinsics/atan2pi.f90
+++ b/flang/test/Lower/Intrinsics/atan2pi.f90
@@ -8,8 +8,6 @@ function test_real4(y,x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real4
-! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
-! CHECK: fir.if %[[terminationCheck]]
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
 ! CHECK: %[[dpi:.*]] = arith.constant 3.1415926535897931 : f64
 ! CHECK: %[[pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
@@ -21,8 +19,6 @@ function test_real8(y,x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real8
-! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
-! CHECK: fir.if %[[terminationCheck]]
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
 ! CHECK: %[[pi:.*]] = arith.constant 3.1415926535897931 : f64
 ! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f64
diff --git a/flang/test/Lower/Intrinsics/atand.f90 b/flang/test/Lower/Intrinsics/atand.f90
index 27189b926ade5..07ea56eff1e49 100644
--- a/flang/test/Lower/Intrinsics/atand.f90
+++ b/flang/test/Lower/Intrinsics/atand.f90
@@ -31,8 +31,6 @@ function test_real4_yx(y,x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real4_yx
-! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
-! CHECK: fir.if %[[terminationCheck]]
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
 ! CHECK: %[[dfactor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %[[factor:.*]] = fir.convert %[[dfactor]] : (f64) -> f32
@@ -44,8 +42,6 @@ function test_real8_yx(y,x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real8_yx
-! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
-! CHECK: fir.if %[[terminationCheck]]
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
 ! CHECK: %[[factor:.*]] = arith.constant 57.295779513082323 : f64
 ! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[factor]] fastmath<contract> : f64
diff --git a/flang/test/Lower/Intrinsics/atandpi.f90 b/flang/test/Lower/Intrinsics/atandpi.f90
index aa1cba164054b..f2adcf770326b 100644
--- a/flang/test/Lower/Intrinsics/atandpi.f90
+++ b/flang/test/Lower/Intrinsics/atandpi.f90
@@ -31,8 +31,6 @@ function test_real4_yx(y,x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real4_yx
-! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
-! CHECK: fir.if %[[terminationCheck]]
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
 ! CHECK: %[[dpi:.*]] = arith.constant 3.1415926535897931 : f64
 ! CHECK: %[[pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
@@ -44,8 +42,6 @@ function test_real8_yx(y,x)
 end function
 
 ! CHECK-LABEL: @_QPtest_real8_yx
-! CHECK: %[[terminationCheck:.*]] = arith.andi %[[YEq0:.*]], %[[XEq0:.*]] : i1
-! CHECK: fir.if %[[terminationCheck]]
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
 ! CHECK: %[[pi:.*]] = arith.constant 3.1415926535897931 : f64
 ! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f64

>From 4fc3a8c200356e42bb70f6765862e68f693fb73a Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Wed, 31 Jan 2024 16:36:46 +0000
Subject: [PATCH 11/14] use inv_pi with mul instead of pi with div

---
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp |  6 +++---
 flang/test/Lower/Intrinsics/atan2pi.f90       | 10 +++++-----
 .../Intrinsics/{atandpi.f90 => atanpi.f90}    | 20 +++++++++----------
 3 files changed, 18 insertions(+), 18 deletions(-)
 rename flang/test/Lower/Intrinsics/{atandpi.f90 => atanpi.f90} (64%)

diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 13806b00c8a20..756323e5dcde7 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -2236,11 +2236,11 @@ mlir::Value IntrinsicLibrary::genAtanpi(mlir::Type resultType,
         mlir::FunctionType::get(context, {resultType}, {args[0].getType()});
     atan = getRuntimeCallGenerator("atan", ftype)(builder, loc, args);
   }
-  llvm::APFloat pi = llvm::APFloat(llvm::numbers::pi);
+  llvm::APFloat inv_pi = llvm::APFloat(llvm::numbers::inv_pi);
   mlir::Value dfactor =
-      builder.createRealConstant(loc, mlir::FloatType::getF64(context), pi);
+      builder.createRealConstant(loc, mlir::FloatType::getF64(context), inv_pi);
   mlir::Value factor = builder.createConvert(loc, resultType, dfactor);
-  return builder.create<mlir::arith::DivFOp>(loc, atan, factor);
+  return builder.create<mlir::arith::MulFOp>(loc, atan, factor);
 }
 
 // ASSOCIATED
diff --git a/flang/test/Lower/Intrinsics/atan2pi.f90 b/flang/test/Lower/Intrinsics/atan2pi.f90
index 16b8eb4eba3e5..df72237610609 100644
--- a/flang/test/Lower/Intrinsics/atan2pi.f90
+++ b/flang/test/Lower/Intrinsics/atan2pi.f90
@@ -9,9 +9,9 @@ function test_real4(y,x)
 
 ! CHECK-LABEL: @_QPtest_real4
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
-! CHECK: %[[dpi:.*]] = arith.constant 3.1415926535897931 : f64
-! CHECK: %[[pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
-! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f32
+! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
+! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
+! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f32
 
 function test_real8(y,x)
   real(8) :: x, y, test_real8
@@ -20,5 +20,5 @@ function test_real8(y,x)
 
 ! CHECK-LABEL: @_QPtest_real8
 ! CHECK-FAST: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
-! CHECK: %[[pi:.*]] = arith.constant 3.1415926535897931 : f64
-! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f64
+! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
+! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f64
diff --git a/flang/test/Lower/Intrinsics/atandpi.f90 b/flang/test/Lower/Intrinsics/atanpi.f90
similarity index 64%
rename from flang/test/Lower/Intrinsics/atandpi.f90
rename to flang/test/Lower/Intrinsics/atanpi.f90
index f2adcf770326b..6382dbd1a30cf 100644
--- a/flang/test/Lower/Intrinsics/atandpi.f90
+++ b/flang/test/Lower/Intrinsics/atanpi.f90
@@ -10,9 +10,9 @@ function test_real4(x)
 ! CHECK-LABEL: @_QPtest_real4
 ! CHECK-PRECISE: %[[atan:.*]] = fir.call @atanf({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f32) -> f32
 ! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f32
-! CHECK: %[[dpi:.*]] = arith.constant 3.1415926535897931 : f64
-! CHECK: %[[pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
-! CHECK: %{{.*}} = arith.divf %[[atan]], %[[pi]] fastmath<contract> : f32
+! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
+! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
+! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[inv_pi]] fastmath<contract> : f32
 
 function test_real8(x)
   real(8) :: x, test_real8
@@ -22,8 +22,8 @@ function test_real8(x)
 ! CHECK-LABEL: @_QPtest_real8
 ! CHECK-PRECISE: %[[atan:.*]] = fir.call @atan({{%[A-Za-z0-9._]+}}) fastmath<contract> : (f64) -> f64
 ! CHECK-FAST: %[[atan:.*]] = math.atan %{{.*}} : f64
-! CHECK: %[[pi:.*]] = arith.constant 3.1415926535897931 : f64
-! CHECK: %{{.*}} = arith.divf %[[atan]], %[[pi]] fastmath<contract> : f64
+! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
+! CHECK: %{{.*}} = arith.mulf %[[atan]], %[[inv_pi]] fastmath<contract> : f64
 
 function test_real4_yx(y,x)
   real(4) :: x, y, test_real4
@@ -32,9 +32,9 @@ function test_real4_yx(y,x)
 
 ! CHECK-LABEL: @_QPtest_real4_yx
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f32
-! CHECK: %[[dpi:.*]] = arith.constant 3.1415926535897931 : f64
-! CHECK: %[[pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
-! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f32
+! CHECK: %[[dpi:.*]] = arith.constant 0.31830988618379069 : f64
+! CHECK: %[[inv_pi:.*]] = fir.convert %[[dpi]] : (f64) -> f32
+! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f32
 
 function test_real8_yx(y,x)
   real(8) :: x, y, test_real8
@@ -43,5 +43,5 @@ function test_real8_yx(y,x)
 
 ! CHECK-LABEL: @_QPtest_real8_yx
 ! CHECK: %[[atan2:.*]] = math.atan2 %{{.*}}, %{{.*}}: f64
-! CHECK: %[[pi:.*]] = arith.constant 3.1415926535897931 : f64
-! CHECK: %{{.*}} = arith.divf %[[atan2]], %[[pi]] fastmath<contract> : f64
+! CHECK: %[[inv_pi:.*]] = arith.constant 0.31830988618379069 : f64
+! CHECK: %{{.*}} = arith.mulf %[[atan2]], %[[inv_pi]] fastmath<contract> : f64

>From 899be6133b3a1245fbc369c65d220302a89c3aa2 Mon Sep 17 00:00:00 2001
From: Yi Wu <43659785+yi-wu-arm at users.noreply.github.com>
Date: Wed, 31 Jan 2024 16:30:30 +0000
Subject: [PATCH 12/14] Update flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Co-authored-by: jeanPerier <jean.perier.polytechnique at gmail.com>
---
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 756323e5dcde7..66fbca933193a 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -2227,7 +2227,7 @@ mlir::Value IntrinsicLibrary::genAtanpi(mlir::Type resultType,
   mlir::Value atan;
   mlir::MLIRContext *context = builder.getContext();
 
-  // atanpi(Y,X) atan2pi(Y,X) == atan2(Y,X) / pi
+  // atanpi = atan / pi
   if (args.size() == 2) {
     atan = builder.create<mlir::math::Atan2Op>(loc, fir::getBase(args[0]),
                                                fir::getBase(args[1]));

>From 24ce3c010dbb0033848582bac3a57f21376bc805 Mon Sep 17 00:00:00 2001
From: Yi Wu <43659785+yi-wu-arm at users.noreply.github.com>
Date: Wed, 31 Jan 2024 16:31:50 +0000
Subject: [PATCH 13/14] Update flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Co-authored-by: jeanPerier <jean.perier.polytechnique at gmail.com>
---
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 66fbca933193a..bc99922c29c4a 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -2218,7 +2218,7 @@ mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
   return builder.create<mlir::arith::MulFOp>(loc, atan, factor);
 }
 
-// ATAN, ATAN2PI
+// ATANPI, ATAN2PI
 mlir::Value IntrinsicLibrary::genAtanpi(mlir::Type resultType,
                                         llvm::ArrayRef<mlir::Value> args) {
   // assert for: atanpi(X), atanpi(Y,X), atan2pi(Y,X)

>From 5335907c4baff5030d5cbbdb8bf8cbefdddf1f46 Mon Sep 17 00:00:00 2001
From: Yi Wu <43659785+yi-wu-arm at users.noreply.github.com>
Date: Wed, 31 Jan 2024 16:31:57 +0000
Subject: [PATCH 14/14] Update flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Co-authored-by: jeanPerier <jean.perier.polytechnique at gmail.com>
---
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 21 +------------------
 1 file changed, 1 insertion(+), 20 deletions(-)

diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index bc99922c29c4a..a3536895ca3b7 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -2174,25 +2174,6 @@ mlir::Value IntrinsicLibrary::genAsind(mlir::Type resultType,
   return getRuntimeCallGenerator("asin", ftype)(builder, loc, {arg});
 }
 
-// ATAND
-void static atanNoneZeroCheck(mlir::Value y, mlir::Value x,
-                              fir::FirOpBuilder &builder, mlir::Location loc) {
-  // When Y == 0 X must not be 0
-  mlir::Value zero = builder.createRealZeroConstant(loc, y.getType());
-  mlir::Value cmpYEq0 = builder.create<mlir::arith::CmpFOp>(
-      loc, mlir::arith::CmpFPredicate::UEQ, y, zero);
-  mlir::Value cmpXEq0 = builder.create<mlir::arith::CmpFOp>(
-      loc, mlir::arith::CmpFPredicate::UEQ, x, zero);
-  mlir::Value terminationCheck =
-      builder.create<mlir::arith::AndIOp>(loc, cmpYEq0, cmpXEq0);
-  builder.genIfThenElse(loc, terminationCheck)
-      .genThen([&]() {
-        fir::runtime::genReportFatalUserError(builder, loc,
-                                              "When Y == 0 X must not be 0");
-      })
-      .end();
-}
-
 // ATAND, ATAN2D
 mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
                                        llvm::ArrayRef<mlir::Value> args) {
@@ -2202,7 +2183,7 @@ mlir::Value IntrinsicLibrary::genAtand(mlir::Type resultType,
   mlir::MLIRContext *context = builder.getContext();
   mlir::Value atan;
 
-  // atand(Y,X) atan2d(Y,X) == atan2(Y,X) * 180/pi
+  // atand = atan * 180/pi
   if (args.size() == 2) {
     atan = builder.create<mlir::math::Atan2Op>(loc, fir::getBase(args[0]),
                                                fir::getBase(args[1]));



More information about the flang-commits mailing list