[flang-commits] [PATCH] D136904: [flang] Warn about overflow from folding complex ABS()

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Oct 27 17:08:01 PDT 2022


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

Emit a warning when the result of folding a call to
ABS() with a complex argument results in an overflow.


https://reviews.llvm.org/D136904

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


Index: flang/test/Evaluate/errors01.f90
===================================================================
--- flang/test/Evaluate/errors01.f90
+++ flang/test/Evaluate/errors01.f90
@@ -105,5 +105,7 @@
     real, parameter :: ok2 = scale(1.0, -99999) ! 0.0
     !CHECK: SCALE intrinsic folding overflow
     real, parameter :: bad1 = scale(1.0, 99999)
+    !CHECK: complex ABS intrinsic folding overflow
+    real, parameter :: bad2 = abs(cmplx(huge(0.),huge(0.)))
   end subroutine
 end module
Index: flang/lib/Evaluate/fold-real.cpp
===================================================================
--- flang/lib/Evaluate/fold-real.cpp
+++ flang/lib/Evaluate/fold-real.cpp
@@ -105,8 +105,14 @@
           context, std::move(funcRef), &Scalar<T>::ABS);
     } else if (auto *z{UnwrapExpr<Expr<SomeComplex>>(args[0])}) {
       return FoldElementalIntrinsic<T, ComplexT>(context, std::move(funcRef),
-          ScalarFunc<T, ComplexT>([](const Scalar<ComplexT> &z) -> Scalar<T> {
-            return z.ABS().value;
+          ScalarFunc<T, ComplexT>([&name, &context](
+                                      const Scalar<ComplexT> &z) -> Scalar<T> {
+            ValueWithRealFlags<Scalar<T>> y{z.ABS()};
+            if (y.flags.test(RealFlag::Overflow)) {
+              context.messages().Say(
+                  "complex ABS intrinsic folding overflow"_warn_en_US, name);
+            }
+            return y.value;
           }));
     } else {
       common::die(" unexpected argument type inside abs");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136904.471334.patch
Type: text/x-patch
Size: 1512 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221028/922febf8/attachment-0001.bin>


More information about the flang-commits mailing list