Skip to content

Conversation

@klausler
Copy link
Contributor

Folding hands complex exponentiations with constant arguments off to the native libm, and on a least on host, this can produce spurious warnings about division by zero and invalid arguments. Handle the case of a zero base specially to avoid that, and also emit better warnings for the undefined 0.**0 and (0.,0.)**0 cases. And add a test for these warnings and the existing related ones.

Folding hands complex exponentiations with constant arguments off to
the native libm, and on a least on host, this can produce spurious
warnings about division by zero and invalid arguments.  Handle
the case of a zero base specially to avoid that, and also emit
better warnings for the undefined 0.**0 and (0.,0.)**0 cases.
And add a test for these warnings and the existing related ones.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Jun 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 21, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

Folding hands complex exponentiations with constant arguments off to the native libm, and on a least on host, this can produce spurious warnings about division by zero and invalid arguments. Handle the case of a zero base specially to avoid that, and also emit better warnings for the undefined 0.**0 and (0.,0.)**0 cases. And add a test for these warnings and the existing related ones.


Full diff: https://github.com/llvm/llvm-project/pull/145179.diff

3 Files Affected:

  • (modified) flang/include/flang/Evaluate/complex.h (+1-1)
  • (modified) flang/lib/Evaluate/fold-implementation.h (+8-1)
  • (added) flang/test/Semantics/bug1046.f90 (+17)
diff --git a/flang/include/flang/Evaluate/complex.h b/flang/include/flang/Evaluate/complex.h
index 2dcd28b59968c..720ccaf512df6 100644
--- a/flang/include/flang/Evaluate/complex.h
+++ b/flang/include/flang/Evaluate/complex.h
@@ -45,7 +45,7 @@ template <typename REAL_TYPE> class Complex {
         im_.Compare(that.im_) == Relation::Equal;
   }
 
-  constexpr bool IsZero() const { return re_.IsZero() || im_.IsZero(); }
+  constexpr bool IsZero() const { return re_.IsZero() && im_.IsZero(); }
 
   constexpr bool IsInfinite() const {
     return re_.IsInfinite() || im_.IsInfinite();
diff --git a/flang/lib/Evaluate/fold-implementation.h b/flang/lib/Evaluate/fold-implementation.h
index b0f39e63d0941..b3780cea4e076 100644
--- a/flang/lib/Evaluate/fold-implementation.h
+++ b/flang/lib/Evaluate/fold-implementation.h
@@ -2156,7 +2156,14 @@ Expr<T> FoldOperation(FoldingContext &context, Power<T> &&x) {
       }
       return Expr<T>{Constant<T>{power.power}};
     } else {
-      if (auto callable{GetHostRuntimeWrapper<T, T, T>("pow")}) {
+      if (folded->first.IsZero()) {
+        if (folded->second.IsZero()) {
+          context.messages().Say(common::UsageWarning::FoldingException,
+              "REAL/COMPLEX 0**0 is not defined"_warn_en_US);
+        } else {
+          return Expr<T>(Constant<T>{folded->first}); // 0. ** nonzero -> 0.
+        }
+      } else if (auto callable{GetHostRuntimeWrapper<T, T, T>("pow")}) {
         return Expr<T>{
             Constant<T>{(*callable)(context, folded->first, folded->second)}};
       } else if (context.languageFeatures().ShouldWarn(
diff --git a/flang/test/Semantics/bug1046.f90 b/flang/test/Semantics/bug1046.f90
new file mode 100644
index 0000000000000..a266651f90c31
--- /dev/null
+++ b/flang/test/Semantics/bug1046.f90
@@ -0,0 +1,17 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
+!WARNING: INTEGER(4) 0**0 is not defined
+print *, 0**0
+!WARNING: REAL/COMPLEX 0**0 is not defined
+print *, 0**0.
+!WARNING: invalid argument on power with INTEGER exponent
+print *, 0.0**0
+!WARNING: REAL/COMPLEX 0**0 is not defined
+print *, 0.0**0.
+!WARNING: invalid argument on power with INTEGER exponent
+print *, (0.0, 0.0)**0
+!WARNING: REAL/COMPLEX 0**0 is not defined
+print *, (0.0, 0.0)**0.
+print *, (0.0, 0.0)**2.5
+end
+
+

@sscalpone sscalpone self-requested a review June 22, 2025 04:01
@klausler klausler merged commit a93d843 into llvm:main Jun 30, 2025
10 checks passed
@klausler klausler deleted the bug1046 branch June 30, 2025 17:21
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jun 30, 2025

LLVM Buildbot has detected a new failure on builder premerge-monolithic-linux running on premerge-linux-1 while building flang at step 7 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/153/builds/36501

Here is the relevant piece of the build log for the reference
Step 7 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'Flang :: Semantics/bug1046.f90' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
--- 
+++ 
@@ -1,6 +1,6 @@

actual at 3: INTEGER(4) 0**0 is not defined [-Wfolding-exception]
actual at 5: REAL/COMPLEX 0**0 is not defined [-Wfolding-exception]
actual at 7: invalid argument on power with INTEGER exponent [-Wfolding-exception]
actual at 9: REAL/COMPLEX 0**0 is not defined [-Wfolding-exception]
actual at 11: invalid argument on power with INTEGER exponent [-Wfolding-exception]
actual at 13: REAL/COMPLEX 0**0 is not defined [-Wfolding-exception]
expect at 3: INTEGER(4) 0**0 is not defined
expect at 5: REAL/COMPLEX 0**0 is not defined
expect at 7: invalid argument on power with INTEGER exponent
expect at 9: REAL/COMPLEX 0**0 is not defined
expect at 11: invalid argument on power with INTEGER exponent
expect at 13: REAL/COMPLEX 0**0 is not defined

FAIL

--
Command Output (stderr):
--
"/usr/bin/python3.10" /build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Semantics/test_errors.py /build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Semantics/bug1046.f90 /build/buildbot/premerge-monolithic-linux/build/bin/flang -fc1 -pedantic -Werror # RUN: at line 1
+ /usr/bin/python3.10 /build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Semantics/test_errors.py /build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Semantics/bug1046.f90 /build/buildbot/premerge-monolithic-linux/build/bin/flang -fc1 -pedantic -Werror

--

********************


@llvm-ci
Copy link
Collaborator

llvm-ci commented Jun 30, 2025

LLVM Buildbot has detected a new failure on builder ppc64le-flang-rhel-clang running on ppc64le-flang-rhel-test while building flang at step 6 "test-build-unified-tree-check-flang".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/157/builds/32323

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-flang) failure: test (failure)
******************** TEST 'Flang :: Semantics/bug1046.f90' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
--- 
+++ 
@@ -1,6 +1,6 @@

actual at 3: INTEGER(4) 0**0 is not defined [-Wfolding-exception]
actual at 5: REAL/COMPLEX 0**0 is not defined [-Wfolding-exception]
actual at 7: invalid argument on power with INTEGER exponent [-Wfolding-exception]
actual at 9: REAL/COMPLEX 0**0 is not defined [-Wfolding-exception]
actual at 11: invalid argument on power with INTEGER exponent [-Wfolding-exception]
actual at 13: REAL/COMPLEX 0**0 is not defined [-Wfolding-exception]
expect at 3: INTEGER(4) 0**0 is not defined
expect at 5: REAL/COMPLEX 0**0 is not defined
expect at 7: invalid argument on power with INTEGER exponent
expect at 9: REAL/COMPLEX 0**0 is not defined
expect at 11: invalid argument on power with INTEGER exponent
expect at 13: REAL/COMPLEX 0**0 is not defined

FAIL

--
Command Output (stderr):
--
"/home/buildbots/llvm-external-buildbots/workers/env/bin/python3.8" /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/llvm-project/flang/test/Semantics/test_errors.py /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/llvm-project/flang/test/Semantics/bug1046.f90 /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/bin/flang -fc1 -pedantic -Werror # RUN: at line 1
+ /home/buildbots/llvm-external-buildbots/workers/env/bin/python3.8 /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/llvm-project/flang/test/Semantics/test_errors.py /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/llvm-project/flang/test/Semantics/bug1046.f90 /home/buildbots/llvm-external-buildbots/workers/ppc64le-flang-rhel-test/ppc64le-flang-rhel-clang-build/build/bin/flang -fc1 -pedantic -Werror

--

********************


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:semantics flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants