[flang-commits] [flang] 6fa5353 - [flang] Fix folding of CMPLX

peter klausler via flang-commits flang-commits at lists.llvm.org
Thu Jun 3 13:11:40 PDT 2021


Author: peter klausler
Date: 2021-06-03T13:11:31-07:00
New Revision: 6fa5353a5696bf8c4c2689126a025330c72aa8f9

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

LOG: [flang] Fix folding of CMPLX

The code for folding calls to the intrinsic function CMPLX was
incorrectly dependent on the number of arguments to distinguish its
two cases (conversion from one kind of complex to another, and
composition of a complex value from real & imaginary parts).
This was wrong since the optional KIND= argument has already been
taken into account by intrinsic processing; instead, the type of
the first argument should decide the issue.

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

Added: 
    

Modified: 
    flang/lib/Evaluate/fold-complex.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/fold-complex.cpp b/flang/lib/Evaluate/fold-complex.cpp
index de541e1ead573..2e574aacc0d54 100644
--- a/flang/lib/Evaluate/fold-complex.cpp
+++ b/flang/lib/Evaluate/fold-complex.cpp
@@ -34,25 +34,23 @@ Expr<Type<TypeCategory::Complex, KIND>> FoldIntrinsicFunction(
     return FoldElementalIntrinsic<T, T>(
         context, std::move(funcRef), &Scalar<T>::CONJG);
   } else if (name == "cmplx") {
-    using Part = typename T::Part;
-    if (args.size() == 2) { // CMPLX(X, [KIND])
+    if (args.size() > 0 && args[0].has_value()) {
       if (auto *x{UnwrapExpr<Expr<SomeComplex>>(args[0])}) {
+        // CMPLX(X [, KIND]) with complex X
         return Fold(context, ConvertToType<T>(std::move(*x)));
+      } else {
+        // CMPLX(X [, Y [, KIND]]) with non-complex X
+        using Part = typename T::Part;
+        Expr<SomeType> re{std::move(*args[0].value().UnwrapExpr())};
+        Expr<SomeType> im{args.size() >= 2 && args[1].has_value()
+                ? std::move(*args[1]->UnwrapExpr())
+                : AsGenericExpr(Constant<Part>{Scalar<Part>{}})};
+        return Fold(context,
+            Expr<T>{
+                ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)),
+                    ToReal<KIND>(context, std::move(im))}});
       }
-      Expr<SomeType> re{std::move(*args[0].value().UnwrapExpr())};
-      Expr<SomeType> im{AsGenericExpr(Constant<Part>{Scalar<Part>{}})};
-      return Fold(context,
-          Expr<T>{ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)),
-              ToReal<KIND>(context, std::move(im))}});
     }
-    // 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>{}})};
-    return Fold(context,
-        Expr<T>{ComplexConstructor<KIND>{ToReal<KIND>(context, std::move(re)),
-            ToReal<KIND>(context, std::move(im))}});
   } else if (name == "merge") {
     return FoldMerge<T>(context, std::move(funcRef));
   }


        


More information about the flang-commits mailing list