[flang-commits] [flang] 78d59a6 - [flang] Warn about overflow from folding complex ABS()

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Sat Oct 29 16:07:15 PDT 2022


Author: Peter Klausler
Date: 2022-10-29T16:07:06-07:00
New Revision: 78d59a6500df1811e99b9413d09c3163b08c693e

URL: https://github.com/llvm/llvm-project/commit/78d59a6500df1811e99b9413d09c3163b08c693e
DIFF: https://github.com/llvm/llvm-project/commit/78d59a6500df1811e99b9413d09c3163b08c693e.diff

LOG: [flang] Warn about overflow from folding complex ABS()

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

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/fold-real.cpp b/flang/lib/Evaluate/fold-real.cpp
index 5e10deb4ca8a5..38ece3f21edd2 100644
--- a/flang/lib/Evaluate/fold-real.cpp
+++ b/flang/lib/Evaluate/fold-real.cpp
@@ -105,8 +105,14 @@ Expr<Type<TypeCategory::Real, KIND>> FoldIntrinsicFunction(
           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");

diff  --git a/flang/test/Evaluate/errors01.f90 b/flang/test/Evaluate/errors01.f90
index 50705be17a390..45f4cbe866f64 100644
--- a/flang/test/Evaluate/errors01.f90
+++ b/flang/test/Evaluate/errors01.f90
@@ -105,5 +105,7 @@ subroutine warnings
     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


        


More information about the flang-commits mailing list