Skip to content

[flang] Fix folding of SHAPE(SPREAD(source,dim,ncopies=-1)) #141146

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

Merged
merged 1 commit into from
May 28, 2025

Conversation

klausler
Copy link
Contributor

The number of copies on the new dimension must be clamped via MAX(0, ncopies) so that it is no less than zero.

Fixes #141119.

The number of copies on the new dimension must be clamped via
MAX(0, ncopies) so that it is no less than zero.

Fixes llvm#141119.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels May 22, 2025
@llvmbot
Copy link
Member

llvmbot commented May 22, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

The number of copies on the new dimension must be clamped via MAX(0, ncopies) so that it is no less than zero.

Fixes #141119.


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

2 Files Affected:

  • (modified) flang/lib/Evaluate/shape.cpp (+4-3)
  • (modified) flang/test/Evaluate/fold-spread.f90 (+1)
diff --git a/flang/lib/Evaluate/shape.cpp b/flang/lib/Evaluate/shape.cpp
index ac4811e9978eb..776866d1416d2 100644
--- a/flang/lib/Evaluate/shape.cpp
+++ b/flang/lib/Evaluate/shape.cpp
@@ -1073,8 +1073,8 @@ auto GetShapeHelper::operator()(const ProcedureRef &call) const -> Result {
         }
       }
     } else if (intrinsic->name == "spread") {
-      // SHAPE(SPREAD(ARRAY,DIM,NCOPIES)) = SHAPE(ARRAY) with NCOPIES inserted
-      // at position DIM.
+      // SHAPE(SPREAD(ARRAY,DIM,NCOPIES)) = SHAPE(ARRAY) with MAX(0,NCOPIES)
+      // inserted at position DIM.
       if (call.arguments().size() == 3) {
         auto arrayShape{
             (*this)(UnwrapExpr<Expr<SomeType>>(call.arguments().at(0)))};
@@ -1086,7 +1086,8 @@ auto GetShapeHelper::operator()(const ProcedureRef &call) const -> Result {
             if (*dim >= 1 &&
                 static_cast<std::size_t>(*dim) <= arrayShape->size() + 1) {
               arrayShape->emplace(arrayShape->begin() + *dim - 1,
-                  ConvertToType<ExtentType>(common::Clone(*nCopies)));
+                  Extremum<SubscriptInteger>{Ordering::Greater, ExtentExpr{0},
+                      ConvertToType<ExtentType>(common::Clone(*nCopies))});
               return std::move(*arrayShape);
             }
           }
diff --git a/flang/test/Evaluate/fold-spread.f90 b/flang/test/Evaluate/fold-spread.f90
index b7e493ee061c8..c8b87e8c87811 100644
--- a/flang/test/Evaluate/fold-spread.f90
+++ b/flang/test/Evaluate/fold-spread.f90
@@ -12,4 +12,5 @@ module m1
   logical, parameter :: test_log4 = all(any(spread([.false., .true.], 2, 2), dim=2) .eqv. [.false., .true.])
   logical, parameter :: test_m2toa3 = all(spread(reshape([(j,j=1,6)],[2,3]),1,4) == &
                                           reshape([((j,k=1,4),j=1,6)],[4,2,3]))
+  logical, parameter :: test_shape_neg = all(shape(spread(0,1,-1)) == [0])
 end module

Copy link
Contributor

@DanielCChen DanielCChen left a comment

Choose a reason for hiding this comment

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

LGTM.
It fixed our test case.
Thanks!

@klausler klausler merged commit a6432b9 into llvm:main May 28, 2025
14 checks passed
@klausler klausler deleted the bug141119 branch May 28, 2025 21:01
google-yfyang pushed a commit to google-yfyang/llvm-project that referenced this pull request May 29, 2025
)

The number of copies on the new dimension must be clamped via MAX(0,
ncopies) so that it is no less than zero.

Fixes llvm#141119.
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
)

The number of copies on the new dimension must be clamped via MAX(0,
ncopies) so that it is no less than zero.

Fixes llvm#141119.
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.

[flang] Incorrect shape of the result of intrinsic SPREAD
5 participants