[flang-commits] [PATCH] D154374: [flang] Fix overflow detection for folding SUM/PRODUCT

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Jul 3 10:27:31 PDT 2023


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

The overflow detection code in the templates that fold SUM and PRODUCT
was checking for overflow before performing the reduction, not after.
Fix and add tests.


https://reviews.llvm.org/D154374

Files:
  flang/lib/Evaluate/fold-reduction.h
  flang/test/Evaluate/errors01.f90


Index: flang/test/Evaluate/errors01.f90
===================================================================
--- flang/test/Evaluate/errors01.f90
+++ flang/test/Evaluate/errors01.f90
@@ -163,6 +163,8 @@
     real, parameter :: bad3 = dim(huge(1.),-.5*huge(1.))
     !CHECK: warning: DIM intrinsic folding overflow
     integer, parameter :: bad4 = dim(huge(1),-1)
+    !CHECK: warning: HYPOT intrinsic folding overflow
+    real, parameter :: bad5 = hypot(huge(0.), huge(0.))
     !CHECK: warning: overflow on REAL(8) to REAL(4) conversion
     x = 1.D40
   end subroutine
Index: flang/lib/Evaluate/fold-reduction.h
===================================================================
--- flang/lib/Evaluate/fold-reduction.h
+++ flang/lib/Evaluate/fold-reduction.h
@@ -260,12 +260,12 @@
         element = prod.value;
       }
     }};
+    auto result{Expr<T>{DoReduction<T>(*array, dim, identity, accumulator)}};
     if (overflow) {
       context.messages().Say(
           "PRODUCT() of %s data overflowed"_warn_en_US, T::AsFortran());
-    } else {
-      return Expr<T>{DoReduction<T>(*array, dim, identity, accumulator)};
     }
+    return result;
   }
   return Expr<T>{std::move(ref)};
 }
@@ -301,12 +301,12 @@
         element = sum.value;
       }
     }};
+    auto result{Expr<T>{DoReduction<T>(*array, dim, identity, accumulator)}};
     if (overflow) {
       context.messages().Say(
           "SUM() of %s data overflowed"_warn_en_US, T::AsFortran());
-    } else {
-      return Expr<T>{DoReduction<T>(*array, dim, identity, accumulator)};
     }
+    return result;
   }
   return Expr<T>{std::move(ref)};
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154374.536835.patch
Type: text/x-patch
Size: 1628 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230703/e7275dad/attachment-0001.bin>


More information about the flang-commits mailing list