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

Yi Wu via flang-commits flang-commits at lists.llvm.org
Mon Jan 22 08:10:11 PST 2024


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

Both call the `mlir::math::Atan2op`
However, based on the new (working draft) standard 2023, there are some differences between these two intrinsics:
atand(y,x): no check for 0
atan2d(y,x): when y==0, x must not be 0
Fix: https://github.com/llvm/llvm-project/issues/78568

>From 916e9bc2f7ffea890c3f7f6553947f9d873be8e9 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 1/2] 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 da6d5970089884..96c1a9219aeff1 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -336,6 +336,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 ac7d4fbe23e673..e95b9a88461e5a 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -139,6 +139,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}}},
@@ -2130,16 +2131,39 @@ IntrinsicLibrary::genAny(mlir::Type resultType,
 
 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 3a43a4003f3b42452aa9c7bc9c33d704050ed33a 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 2/2] 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 80f79d42fc2b75..e85ad402fb2c33 100644
--- a/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
+++ b/flang/include/flang/Optimizer/Builder/IntrinsicCall.h
@@ -174,6 +174,7 @@ struct IntrinsicLibrary {
   mlir::Value genAnint(mlir::Type, llvm::ArrayRef<mlir::Value>);
   fir::ExtendedValue genAny(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
   mlir::Value genAtand(mlir::Type, llvm::ArrayRef<mlir::Value>);
+  mlir::Value genAtan2d(mlir::Type, llvm::ArrayRef<mlir::Value>);
   fir::ExtendedValue
       genCommandArgumentCount(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
   fir::ExtendedValue genAssociated(mlir::Type,
diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp
index 96c1a9219aeff1..c059fa3502c35e 100644
--- a/flang/lib/Evaluate/intrinsics.cpp
+++ b/flang/lib/Evaluate/intrinsics.cpp
@@ -333,12 +333,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 e95b9a88461e5a..b685878ad8c60f 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -138,8 +138,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}}},
@@ -2129,41 +2129,58 @@ IntrinsicLibrary::genAny(mlir::Type resultType,
   return readAndAddCleanUp(resultMutableBox, resultType, "ANY");
 }
 
+// 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 00000000000000..f94c2d79c0d4b7
--- /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 00000000000000..b3b98144ead42e
--- /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 2483bef46e60f6..25d882b693ed81 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



More information about the flang-commits mailing list