Skip to content

[flang] Fix constant subscript operations #68352

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
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions flang/lib/Evaluate/constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ ConstantSubscripts ConstantBounds::ComputeUbounds(
std::optional<int> dim) const {
if (dim) {
CHECK(*dim < Rank());
return {lbounds_[*dim] + shape_[*dim] - 1};
return {lbounds_[*dim] + (shape_[*dim] - 1)};
} else {
ConstantSubscripts ubounds(Rank());
for (int i{0}; i < Rank(); ++i) {
ubounds[i] = lbounds_[i] + shape_[i] - 1;
ubounds[i] = lbounds_[i] + (shape_[i] - 1);
}
return ubounds;
}
Expand Down Expand Up @@ -73,7 +73,7 @@ ConstantSubscript ConstantBounds::SubscriptsToOffset(
for (auto j : index) {
auto lb{lbounds_[dim]};
auto extent{shape_[dim++]};
CHECK(j >= lb && j < lb + extent);
CHECK(j >= lb && j - lb < extent);
offset += stride * (j - lb);
stride *= extent;
}
Expand All @@ -93,10 +93,10 @@ bool ConstantBounds::IncrementSubscripts(
ConstantSubscript k{dimOrder ? (*dimOrder)[j] : j};
auto lb{lbounds_[k]};
CHECK(indices[k] >= lb);
if (++indices[k] < lb + shape_[k]) {
if (++indices[k] - lb < shape_[k]) {
return true;
} else {
CHECK(indices[k] == lb + std::max<ConstantSubscript>(shape_[k], 1));
CHECK(indices[k] - lb == std::max<ConstantSubscript>(shape_[k], 1));
indices[k] = lb;
}
}
Expand Down
6 changes: 6 additions & 0 deletions flang/test/Evaluate/folding08.f90
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,10 @@ subroutine test4_bound_parentheses
logical, parameter :: test_ubpa4_dim = ubound((pa4), 1) == 5 .and. &
ubound((pa4), 2) == 4
end
subroutine test5_max_ubound
! Test maximum ubound value
integer(8), parameter :: I64_MAX = INT(z'7fffffffffffffff', kind=8)
integer, parameter :: a5(I64_MAX - 2 : I64_MAX) = [1, 2, 3]
logical, parameter :: test_uba5 = ubound(a5, 1, kind=8) == I64_MAX
end subroutine
end
5 changes: 5 additions & 0 deletions flang/test/Semantics/reshape.f90
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ program reshaper
type(dType), parameter :: array19(*) = [dType::dType(field=[1,2])]
logical, parameter :: lVar = all(array19(:)%field(1) == [2])

! RESHAPE on array with maximum valid upper bound
integer(8), parameter :: I64_MAX = INT(z'7fffffffffffffff', kind=8)
integer, parameter :: array21(I64_MAX - 2 : I64_MAX) = [1, 2, 3]
integer, parameter :: array22(2) = RESHAPE(array21, [2])

!ERROR: Size of 'shape=' argument must not be greater than 15
CALL ext_sub(RESHAPE([(n, n=1,20)], &
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]))
Expand Down