Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang] Prevent bad expression rewrite 0*ARR -> 0 #79853

Merged
merged 1 commit into from
Jan 29, 2024
Merged

Conversation

klausler
Copy link
Contributor

Don't rewrite 0*X to 0 if X is not scalar. Up until now this hasn't shown up as a bug because a scalar 0 works in nearly all expressions where an array would be expected. But not in all cases -- this bad rewrite can cause generic procedure resolution to fail when it causes an actual argument to have an unsupported rank.

Don't rewrite 0*X to 0 if X is not scalar.  Up until now this
hasn't shown up as a bug because a scalar 0 works in nearly all
expressions where an array would be expected.  But not in all
cases -- this bad rewrite can cause generic procedure resolution
to fail when it causes an actual argument to have an unsupported
rank.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Jan 29, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 29, 2024

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

Don't rewrite 0*X to 0 if X is not scalar. Up until now this hasn't shown up as a bug because a scalar 0 works in nearly all expressions where an array would be expected. But not in all cases -- this bad rewrite can cause generic procedure resolution to fail when it causes an actual argument to have an unsupported rank.


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

2 Files Affected:

  • (modified) flang/lib/Evaluate/fold-implementation.h (+1-1)
  • (added) flang/test/Semantics/generic08.f90 (+22)
diff --git a/flang/lib/Evaluate/fold-implementation.h b/flang/lib/Evaluate/fold-implementation.h
index ed015da951090d5..798bc5f37f6f424 100644
--- a/flang/lib/Evaluate/fold-implementation.h
+++ b/flang/lib/Evaluate/fold-implementation.h
@@ -1927,7 +1927,7 @@ Expr<T> FoldOperation(FoldingContext &context, Multiply<T> &&x) {
       x.left() = Expr<T>{std::move(*c)};
     }
     if (auto c{GetScalarConstantValue<T>(x.left())}) {
-      if (c->IsZero()) {
+      if (c->IsZero() && x.right().Rank() == 0) {
         return std::move(x.left());
       } else if (c->CompareSigned(Scalar<T>{1}) == Ordering::Equal) {
         if (IsVariable(x.right())) {
diff --git a/flang/test/Semantics/generic08.f90 b/flang/test/Semantics/generic08.f90
new file mode 100644
index 000000000000000..327a7b21d320991
--- /dev/null
+++ b/flang/test/Semantics/generic08.f90
@@ -0,0 +1,22 @@
+! RUN: %flang_fc1 -fsyntax-only -pedantic %s  2>&1 | FileCheck %s --allow-empty
+! Regression test for pFUnit case: ensure that 0*ka doesn't get rewritten
+! into a scalar 0 and then fail generic resolution.
+! CHECK-NOT: error:
+program test
+  interface g
+    procedure s
+  end interface
+  integer(1) a(1)
+  a(1) = 2
+  call test(1_1, a)
+ contains
+  subroutine s(a1,a2)
+    integer(1) a1(:), a2(:)
+    print *, a1
+    print *, a2
+  end
+  subroutine test(j,ka)
+    integer(1) j, ka(:)
+    call g(int(j+0*ka,kind(ka)), ka)
+  end
+end

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@klausler klausler merged commit d83c977 into llvm:main Jan 29, 2024
6 checks passed
@klausler klausler deleted the pfunit3 branch January 29, 2024 22:25
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.

None yet

3 participants