[flang-commits] [flang] 92e22c9 - [flang] Fix UBOUND() constant folding for parentheses expr

Mike Kashkarov via flang-commits flang-commits at lists.llvm.org
Thu Apr 28 02:56:58 PDT 2022


Author: Mike Kashkarov
Date: 2022-04-28T18:56:54+09:00
New Revision: 92e22c97e9ef0da76b52bf56a0ac5d4a312b1ea7

URL: https://github.com/llvm/llvm-project/commit/92e22c97e9ef0da76b52bf56a0ac5d4a312b1ea7
DIFF: https://github.com/llvm/llvm-project/commit/92e22c97e9ef0da76b52bf56a0ac5d4a312b1ea7.diff

LOG: [flang] Fix UBOUND() constant folding for parentheses expr

Similarly to LBOUND((x)) in https://reviews.llvm.org/D123838 - fix UBOUND((x))
folding for constant arrays to return shape instead of recurse scan.

Depends on D123520

Reviewed By: jeanPerier

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

Added: 
    

Modified: 
    flang/lib/Evaluate/fold-integer.cpp
    flang/test/Evaluate/folding08.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/fold-integer.cpp b/flang/lib/Evaluate/fold-integer.cpp
index 3ca1d45fb4bf5..f9cfdc7175cab 100644
--- a/flang/lib/Evaluate/fold-integer.cpp
+++ b/flang/lib/Evaluate/fold-integer.cpp
@@ -70,14 +70,31 @@ class GetConstantArrayBoundHelper {
         return x.lbounds();
       }
     } else {
-      return x.ComputeUbounds(dim_);
+      // Return the upper bound
+      if (arrayFromParenthesesExpr) {
+        // Underlying array comes from (x) expression - return shapes
+        if (dim_) {
+          return {x.shape().at(*dim_)};
+        } else {
+          return x.shape();
+        }
+      } else {
+        return x.ComputeUbounds(dim_);
+      }
     }
   }
 
   template <typename T> ConstantSubscripts Get(const Parentheses<T> &x) {
-    // LBOUND for (x) is [1, ..., 1] cause of temp variable inside
-    // parentheses (lower bound is omitted, the default value is 1).
-    return ConstantSubscripts(x.Rank(), ConstantSubscript{1});
+    // Cause of temp variable inside parentheses - return [1, ... 1] for lower
+    // bounds and shape for upper bounds
+    if (getLbound_) {
+      return ConstantSubscripts(x.Rank(), ConstantSubscript{1});
+    } else {
+      // Indicate that underlying array comes from parentheses expression.
+      // Continue to unwrap expression until we hit a constant
+      arrayFromParenthesesExpr = true;
+      return Get(x.left());
+    }
   }
 
   template <typename T> ConstantSubscripts Get(const Expr<T> &x) {
@@ -89,6 +106,7 @@ class GetConstantArrayBoundHelper {
 
   const std::optional<ConstantSubscript> dim_;
   const bool getLbound_;
+  bool arrayFromParenthesesExpr{false};
 };
 
 template <int KIND>

diff  --git a/flang/test/Evaluate/folding08.f90 b/flang/test/Evaluate/folding08.f90
index 9c9ba66473ebb..8c5296e889747 100644
--- a/flang/test/Evaluate/folding08.f90
+++ b/flang/test/Evaluate/folding08.f90
@@ -105,33 +105,45 @@ subroutine test3_bound_parameter
          ubound(a3, 2) == 1 .and. &
          ubound(a3, 3) == 6
   end subroutine
-  subroutine test4_lbound_parentheses
-    ! Test lbound with (x) expressions
+  subroutine test4_bound_parentheses
+    ! Test [ul]bound with (x) expressions
     integer :: a1(1) = 0
     logical, parameter :: test_lba1 = all(lbound((a1)) == [1])
+    logical, parameter :: test_uba1 = all(ubound((a1)) == [1])
     integer :: a2(0:2) = 0
     logical, parameter :: test_lba2 = all(lbound((a2)) == [1])
+    logical, parameter :: test_uba2 = all(ubound((a2)) == [3])
     integer :: a3(-1:0) = 0
     logical, parameter :: test_lba3 = all(lbound((a3)) == [1])
+    logical, parameter :: test_uba3 = all(ubound((a3)) == [2])
     integer :: a4(-5:-1, 2:5) = 0
     logical, parameter :: test_lba4 = all(lbound((a4)) == [1, 1])
+    logical, parameter :: test_uba4 = all(ubound((a4)) == [5, 4])
 
     ! Exercise with DIM=
     logical, parameter :: test_lba4_dim = lbound((a4), 1) == 1 .and. &
          lbound((a4), 2) == 1
+    logical, parameter :: test_uba4_dim = ubound((a4), 1) == 5 .and. &
+         ubound((a4), 2) == 4
 
     ! Exercise with parameter types
     integer, parameter :: pa1(1) = 0
     logical, parameter :: test_lbpa1 = all(lbound((pa1)) == [1])
+    logical, parameter :: test_ubpa1 = all(ubound((pa1)) == [1])
     integer, parameter :: pa2(0:2) = 0
     logical, parameter :: test_lbpa2 = all(lbound((pa2)) == [1])
+    logical, parameter :: test_ubpa2 = all(ubound((pa2)) == [3])
     integer, parameter :: pa3(-1:0) = 0
     logical, parameter :: test_lbpa3 = all(lbound((pa3)) == [1])
+    logical, parameter :: test_ubpa3 = all(ubound((pa3)) == [2])
     integer, parameter :: pa4(-5:-1, 2:5) = 0
     logical, parameter :: test_lbpa4 = all(lbound((pa4)) == [1, 1])
+    logical, parameter :: test_ubpa4 = all(ubound((pa4)) == [5, 4])
 
     ! Exercise with DIM=
     logical, parameter :: test_lbpa4_dim = lbound((pa4), 1) == 1 .and. &
          lbound((pa4), 2) == 1
+    logical, parameter :: test_ubpa4_dim = ubound((pa4), 1) == 5 .and. &
+         ubound((pa4), 2) == 4
   end
 end


        


More information about the flang-commits mailing list