[flang-commits] [flang] 39ee23e - [flang] Set LBOUND() folding for (x) expression as ones

Mike Kashkarov via flang-commits flang-commits at lists.llvm.org
Wed Apr 20 10:06:15 PDT 2022


Author: Mike Kashkarov
Date: 2022-04-20T20:06:11+03:00
New Revision: 39ee23ed5ab3f411b6945a3acbbdf22cd8a0a6cb

URL: https://github.com/llvm/llvm-project/commit/39ee23ed5ab3f411b6945a3acbbdf22cd8a0a6cb
DIFF: https://github.com/llvm/llvm-project/commit/39ee23ed5ab3f411b6945a3acbbdf22cd8a0a6cb.diff

LOG: [flang] Set LBOUND() folding for (x) expression as ones

Set LBOUND() constant folding for parentheses expr. as ones

Array bounds should not propagate throught omitted bounds specifications or
temporary variables - fix constant folding in case of Parentheses<T> expression
by explicitly returning array of ones (or scalar in case of DIM=).

Add set of tests for (x) bounds checks (w/ and w/o 'parameter' arrays)

Reviewed By: jeanPerier

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/fold-integer.cpp b/flang/lib/Evaluate/fold-integer.cpp
index cbecda0ecebda..a65200027433b 100644
--- a/flang/lib/Evaluate/fold-integer.cpp
+++ b/flang/lib/Evaluate/fold-integer.cpp
@@ -53,8 +53,9 @@ class GetConstantArrayLboundHelper {
   }
 
   template <typename T> ConstantSubscripts GetLbound(const Parentheses<T> &x) {
-    // Strip off the parentheses
-    return GetLbound(x.left());
+    // 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});
   }
 
   template <typename T> ConstantSubscripts GetLbound(const Expr<T> &x) {

diff  --git a/flang/test/Evaluate/folding08.f90 b/flang/test/Evaluate/folding08.f90
index a5b65e0246efb..3c56229d6eeb9 100644
--- a/flang/test/Evaluate/folding08.f90
+++ b/flang/test/Evaluate/folding08.f90
@@ -95,4 +95,33 @@ subroutine test3_lbound_parameter
          lbound(a3, 2) == 1 .and. &
          lbound(a3, 3) == 4
   end subroutine
+  subroutine test4_lbound_parentheses
+    ! Test lbound with (x) expressions
+    integer :: a1(1) = 0
+    logical, parameter :: test_lba1 = all(lbound((a1)) == [1])
+    integer :: a2(0:2) = 0
+    logical, parameter :: test_lba2 = all(lbound((a2)) == [1])
+    integer :: a3(-1:0) = 0
+    logical, parameter :: test_lba3 = all(lbound((a3)) == [1])
+    integer :: a4(-5:-1, 2:5) = 0
+    logical, parameter :: test_lba4 = all(lbound((a4)) == [1, 1])
+
+    ! Exercise with DIM=
+    logical, parameter :: test_lba4_dim = lbound((a4), 1) == 1 .and. &
+         lbound((a4), 2) == 1
+
+    ! Exercise with parameter types
+    integer, parameter :: pa1(1) = 0
+    logical, parameter :: test_lbpa1 = all(lbound((pa1)) == [1])
+    integer, parameter :: pa2(0:2) = 0
+    logical, parameter :: test_lbpa2 = all(lbound((pa2)) == [1])
+    integer, parameter :: pa3(-1:0) = 0
+    logical, parameter :: test_lbpa3 = all(lbound((pa3)) == [1])
+    integer, parameter :: pa4(-5:-1, 2:5) = 0
+    logical, parameter :: test_lbpa4 = all(lbound((pa4)) == [1, 1])
+
+    ! Exercise with DIM=
+    logical, parameter :: test_lbpa4_dim = lbound((pa4), 1) == 1 .and. &
+         lbound((pa4), 2) == 1
+  end
 end

diff  --git a/flang/test/Evaluate/folding16.f90 b/flang/test/Evaluate/folding16.f90
index 04474bd5e9970..e5624d40da644 100644
--- a/flang/test/Evaluate/folding16.f90
+++ b/flang/test/Evaluate/folding16.f90
@@ -7,7 +7,7 @@ module m
   integer, parameter :: c(-1:1) = [33, 22, 11]
   integer, parameter :: d(1:3) = [33, 22, 11]
   integer, parameter :: e(-2:0) = ([33, 22, 11])
-  logical, parameter :: test_1 = lbound((a),1)==-1 .and. lbound(b,1)==-1 .and. &
+  logical, parameter :: test_1 = lbound((a),1)==1 .and. lbound(b,1)==-1 .and. &
                                lbound(log(a),1)==1 .and. all(b==0)
   logical, parameter :: test_2 = all(c .eq. d)
   logical, parameter :: test_3 = all(c .eq. e)


        


More information about the flang-commits mailing list