Skip to content

Commit 92e22c9

Browse files
committed
[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
1 parent bf0bcb5 commit 92e22c9

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

flang/lib/Evaluate/fold-integer.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,31 @@ class GetConstantArrayBoundHelper {
7070
return x.lbounds();
7171
}
7272
} else {
73-
return x.ComputeUbounds(dim_);
73+
// Return the upper bound
74+
if (arrayFromParenthesesExpr) {
75+
// Underlying array comes from (x) expression - return shapes
76+
if (dim_) {
77+
return {x.shape().at(*dim_)};
78+
} else {
79+
return x.shape();
80+
}
81+
} else {
82+
return x.ComputeUbounds(dim_);
83+
}
7484
}
7585
}
7686

7787
template <typename T> ConstantSubscripts Get(const Parentheses<T> &x) {
78-
// LBOUND for (x) is [1, ..., 1] cause of temp variable inside
79-
// parentheses (lower bound is omitted, the default value is 1).
80-
return ConstantSubscripts(x.Rank(), ConstantSubscript{1});
88+
// Cause of temp variable inside parentheses - return [1, ... 1] for lower
89+
// bounds and shape for upper bounds
90+
if (getLbound_) {
91+
return ConstantSubscripts(x.Rank(), ConstantSubscript{1});
92+
} else {
93+
// Indicate that underlying array comes from parentheses expression.
94+
// Continue to unwrap expression until we hit a constant
95+
arrayFromParenthesesExpr = true;
96+
return Get(x.left());
97+
}
8198
}
8299

83100
template <typename T> ConstantSubscripts Get(const Expr<T> &x) {
@@ -89,6 +106,7 @@ class GetConstantArrayBoundHelper {
89106

90107
const std::optional<ConstantSubscript> dim_;
91108
const bool getLbound_;
109+
bool arrayFromParenthesesExpr{false};
92110
};
93111

94112
template <int KIND>

flang/test/Evaluate/folding08.f90

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,33 +105,45 @@ subroutine test3_bound_parameter
105105
ubound(a3, 2) == 1 .and. &
106106
ubound(a3, 3) == 6
107107
end subroutine
108-
subroutine test4_lbound_parentheses
109-
! Test lbound with (x) expressions
108+
subroutine test4_bound_parentheses
109+
! Test [ul]bound with (x) expressions
110110
integer :: a1(1) = 0
111111
logical, parameter :: test_lba1 = all(lbound((a1)) == [1])
112+
logical, parameter :: test_uba1 = all(ubound((a1)) == [1])
112113
integer :: a2(0:2) = 0
113114
logical, parameter :: test_lba2 = all(lbound((a2)) == [1])
115+
logical, parameter :: test_uba2 = all(ubound((a2)) == [3])
114116
integer :: a3(-1:0) = 0
115117
logical, parameter :: test_lba3 = all(lbound((a3)) == [1])
118+
logical, parameter :: test_uba3 = all(ubound((a3)) == [2])
116119
integer :: a4(-5:-1, 2:5) = 0
117120
logical, parameter :: test_lba4 = all(lbound((a4)) == [1, 1])
121+
logical, parameter :: test_uba4 = all(ubound((a4)) == [5, 4])
118122

119123
! Exercise with DIM=
120124
logical, parameter :: test_lba4_dim = lbound((a4), 1) == 1 .and. &
121125
lbound((a4), 2) == 1
126+
logical, parameter :: test_uba4_dim = ubound((a4), 1) == 5 .and. &
127+
ubound((a4), 2) == 4
122128

123129
! Exercise with parameter types
124130
integer, parameter :: pa1(1) = 0
125131
logical, parameter :: test_lbpa1 = all(lbound((pa1)) == [1])
132+
logical, parameter :: test_ubpa1 = all(ubound((pa1)) == [1])
126133
integer, parameter :: pa2(0:2) = 0
127134
logical, parameter :: test_lbpa2 = all(lbound((pa2)) == [1])
135+
logical, parameter :: test_ubpa2 = all(ubound((pa2)) == [3])
128136
integer, parameter :: pa3(-1:0) = 0
129137
logical, parameter :: test_lbpa3 = all(lbound((pa3)) == [1])
138+
logical, parameter :: test_ubpa3 = all(ubound((pa3)) == [2])
130139
integer, parameter :: pa4(-5:-1, 2:5) = 0
131140
logical, parameter :: test_lbpa4 = all(lbound((pa4)) == [1, 1])
141+
logical, parameter :: test_ubpa4 = all(ubound((pa4)) == [5, 4])
132142

133143
! Exercise with DIM=
134144
logical, parameter :: test_lbpa4_dim = lbound((pa4), 1) == 1 .and. &
135145
lbound((pa4), 2) == 1
146+
logical, parameter :: test_ubpa4_dim = ubound((pa4), 1) == 5 .and. &
147+
ubound((pa4), 2) == 4
136148
end
137149
end

0 commit comments

Comments
 (0)