[flang-commits] [flang] a50cec7 - [flang] Fix CMPLX folding with complex arguments

Jean Perier via flang-commits flang-commits at lists.llvm.org
Fri Jul 31 01:27:13 PDT 2020


Author: Jean Perier
Date: 2020-07-31T10:26:08+02:00
New Revision: a50cec71ecea5680c15752d211db16d49d5b1f12

URL: https://github.com/llvm/llvm-project/commit/a50cec71ecea5680c15752d211db16d49d5b1f12
DIFF: https://github.com/llvm/llvm-project/commit/a50cec71ecea5680c15752d211db16d49d5b1f12.diff

LOG: [flang] Fix CMPLX folding with complex arguments

CMPLX folding was expecting only one arguments in case X argument
is complex. This is wrong since there is also the optional KIND
argument.

Reviewed By: schweitz

Differential Revision: https://reviews.llvm.org/D84936

Added: 
    

Modified: 
    flang/lib/Evaluate/fold-complex.cpp
    flang/test/Evaluate/folding01.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/fold-complex.cpp b/flang/lib/Evaluate/fold-complex.cpp
index ea96f045aa3e..8058b2dbd13c 100644
--- a/flang/lib/Evaluate/fold-complex.cpp
+++ b/flang/lib/Evaluate/fold-complex.cpp
@@ -36,7 +36,7 @@ Expr<Type<TypeCategory::Complex, KIND>> FoldIntrinsicFunction(
         context, std::move(funcRef), &Scalar<T>::CONJG);
   } else if (name == "cmplx") {
     using Part = typename T::Part;
-    if (args.size() == 1) {
+    if (args.size() == 2) { // CMPLX(X, [KIND])
       if (auto *x{UnwrapExpr<Expr<SomeComplex>>(args[0])}) {
         return Fold(context, ConvertToType<T>(std::move(*x)));
       }
@@ -46,7 +46,8 @@ Expr<Type<TypeCategory::Complex, KIND>> FoldIntrinsicFunction(
           Expr<T>{ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)),
               ToReal<KIND>(context, std::move(im))}});
     }
-    CHECK(args.size() == 2 || args.size() == 3);
+    // CMPLX(X, [Y, KIND])
+    CHECK(args.size() == 3);
     Expr<SomeType> re{std::move(*args[0].value().UnwrapExpr())};
     Expr<SomeType> im{args[1] ? std::move(*args[1].value().UnwrapExpr())
                               : AsGenericExpr(Constant<Part>{Scalar<Part>{}})};

diff  --git a/flang/test/Evaluate/folding01.f90 b/flang/test/Evaluate/folding01.f90
index 09004649e9d5..465b22752cec 100644
--- a/flang/test/Evaluate/folding01.f90
+++ b/flang/test/Evaluate/folding01.f90
@@ -63,6 +63,14 @@ module m
   logical, parameter :: test_ne_i1 =.NOT.(2.NE.2)
   logical, parameter :: test_ne_i2 = -2.NE.2
 
+! Check conversions
+  logical, parameter :: test_cmplx1 = cmplx((1._4, -1._4)).EQ.((1._4, -1._4))
+  logical, parameter :: test_cmplx2 = cmplx((1._4, -1._4), 8).EQ.((1._8, -1._8))
+  logical, parameter :: test_cmplx3 = cmplx(1._4, -1._4).EQ.((1._4, -1._4))
+  logical, parameter :: test_cmplx4 = cmplx(1._4, -1._4, 8).EQ.((1._8, -1._8))
+  logical, parameter :: test_cmplx5 = cmplx(1._4).EQ.((1._4, 0._4))
+  logical, parameter :: test_cmplx6 = cmplx(1._4, kind=8).EQ.((1._8, 0._8))
+
 ! Check integer intrinsic operation folding
   logical, parameter :: test_unaryminus_i = (-(-1)).EQ.1
   logical, parameter :: test_unaryplus_i = (+1).EQ.1


        


More information about the flang-commits mailing list