[flang-commits] [PATCH] D156754: [flang] Fix crash in folding of DPROD() with non-scalar arguments

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Jul 31 16:09:53 PDT 2023


klausler created this revision.
klausler added a reviewer: vzakhari.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert.
Herald added a reviewer: sscalpone.
Herald added a project: All.
klausler requested review of this revision.

DPROD(x,y) is defined as DBLE(x)*DBLE(y) and that's exactly how
the implementation of its rewriting and possible folding should 
be implemented, instead of the current code that only works when
both arguments are scalar and crashes otherwise.


https://reviews.llvm.org/D156754

Files:
  flang/lib/Evaluate/fold-real.cpp
  flang/test/Evaluate/fold-dprod.f90


Index: flang/test/Evaluate/fold-dprod.f90
===================================================================
--- /dev/null
+++ flang/test/Evaluate/fold-dprod.f90
@@ -0,0 +1,8 @@
+! RUN: %python %S/test_folding.py %s %flang_fc1
+! Tests folding of DPROD()
+module m
+  logical, parameter :: test_kind = kind(dprod(2., 3.)) == kind(0.d0)
+  logical, parameter :: test_ss = dprod(2., 3.) == 6.d0
+  logical, parameter :: test_sv = all(dprod(2., [3.,4.]) == [6.d0,8.d0])
+  logical, parameter :: test_vv = all(dprod([2.,3.], [4.,5.]) == [8.d0,15.0d0])
+end
Index: flang/lib/Evaluate/fold-real.cpp
===================================================================
--- flang/lib/Evaluate/fold-real.cpp
+++ flang/lib/Evaluate/fold-real.cpp
@@ -149,10 +149,15 @@
   } else if (name == "dot_product") {
     return FoldDotProduct<T>(context, std::move(funcRef));
   } else if (name == "dprod") {
-    if (auto scalars{GetScalarConstantArguments<T, T>(context, args)}) {
-      return Fold(context,
-          Expr<T>{Multiply<T>{
-              Expr<T>{std::get<0>(*scalars)}, Expr<T>{std::get<1>(*scalars)}}});
+    // Rewrite DPROD(x,y) -> DBLE(x)*DBLE(y)
+    if (args.at(0) && args.at(1)) {
+      const auto *xExpr{args[0]->UnwrapExpr()};
+      const auto *yExpr{args[1]->UnwrapExpr()};
+      if (xExpr && yExpr) {
+        return Fold(context,
+            ToReal<T::kind>(context, common::Clone(*xExpr)) *
+                ToReal<T::kind>(context, common::Clone(*yExpr)));
+      }
     }
   } else if (name == "epsilon") {
     return Expr<T>{Scalar<T>::EPSILON()};


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156754.545848.patch
Type: text/x-patch
Size: 1570 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230731/3bea01b6/attachment-0001.bin>


More information about the flang-commits mailing list