[flang-commits] [PATCH] D103568: [flang] Fix folding of CMPLX

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Jun 3 13:11:55 PDT 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG6fa5353a5696: [flang] Fix folding of CMPLX (authored by klausler).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D103568/new/

https://reviews.llvm.org/D103568

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


Index: flang/lib/Evaluate/fold-complex.cpp
===================================================================
--- flang/lib/Evaluate/fold-complex.cpp
+++ flang/lib/Evaluate/fold-complex.cpp
@@ -34,25 +34,23 @@
     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));
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D103568.349667.patch
Type: text/x-patch
Size: 2068 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20210603/bae9251b/attachment-0001.bin>


More information about the flang-commits mailing list