Skip to content

Commit 39ee23e

Browse files
committed
[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
1 parent bd5371e commit 39ee23e

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

flang/lib/Evaluate/fold-integer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ class GetConstantArrayLboundHelper {
5353
}
5454

5555
template <typename T> ConstantSubscripts GetLbound(const Parentheses<T> &x) {
56-
// Strip off the parentheses
57-
return GetLbound(x.left());
56+
// LBOUND for (x) is [1, ..., 1] cause of temp variable inside
57+
// parentheses (lower bound is omitted, the default value is 1).
58+
return ConstantSubscripts(x.Rank(), ConstantSubscript{1});
5859
}
5960

6061
template <typename T> ConstantSubscripts GetLbound(const Expr<T> &x) {

flang/test/Evaluate/folding08.f90

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,33 @@ subroutine test3_lbound_parameter
9595
lbound(a3, 2) == 1 .and. &
9696
lbound(a3, 3) == 4
9797
end subroutine
98+
subroutine test4_lbound_parentheses
99+
! Test lbound with (x) expressions
100+
integer :: a1(1) = 0
101+
logical, parameter :: test_lba1 = all(lbound((a1)) == [1])
102+
integer :: a2(0:2) = 0
103+
logical, parameter :: test_lba2 = all(lbound((a2)) == [1])
104+
integer :: a3(-1:0) = 0
105+
logical, parameter :: test_lba3 = all(lbound((a3)) == [1])
106+
integer :: a4(-5:-1, 2:5) = 0
107+
logical, parameter :: test_lba4 = all(lbound((a4)) == [1, 1])
108+
109+
! Exercise with DIM=
110+
logical, parameter :: test_lba4_dim = lbound((a4), 1) == 1 .and. &
111+
lbound((a4), 2) == 1
112+
113+
! Exercise with parameter types
114+
integer, parameter :: pa1(1) = 0
115+
logical, parameter :: test_lbpa1 = all(lbound((pa1)) == [1])
116+
integer, parameter :: pa2(0:2) = 0
117+
logical, parameter :: test_lbpa2 = all(lbound((pa2)) == [1])
118+
integer, parameter :: pa3(-1:0) = 0
119+
logical, parameter :: test_lbpa3 = all(lbound((pa3)) == [1])
120+
integer, parameter :: pa4(-5:-1, 2:5) = 0
121+
logical, parameter :: test_lbpa4 = all(lbound((pa4)) == [1, 1])
122+
123+
! Exercise with DIM=
124+
logical, parameter :: test_lbpa4_dim = lbound((pa4), 1) == 1 .and. &
125+
lbound((pa4), 2) == 1
126+
end
98127
end

flang/test/Evaluate/folding16.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module m
77
integer, parameter :: c(-1:1) = [33, 22, 11]
88
integer, parameter :: d(1:3) = [33, 22, 11]
99
integer, parameter :: e(-2:0) = ([33, 22, 11])
10-
logical, parameter :: test_1 = lbound((a),1)==-1 .and. lbound(b,1)==-1 .and. &
10+
logical, parameter :: test_1 = lbound((a),1)==1 .and. lbound(b,1)==-1 .and. &
1111
lbound(log(a),1)==1 .and. all(b==0)
1212
logical, parameter :: test_2 = all(c .eq. d)
1313
logical, parameter :: test_3 = all(c .eq. e)

0 commit comments

Comments
 (0)