Skip to content

Commit

Permalink
[flang] Handle missing substring upper bound better when folding
Browse files Browse the repository at this point in the history
When folding a substring of a named constant or character literal,
acquire the value of a missing upper bound from the base object.

Differential Revision: https://reviews.llvm.org/D142942
  • Loading branch information
klausler committed Jan 31, 2023
1 parent bbddbe5 commit 8fed620
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
15 changes: 14 additions & 1 deletion flang/lib/Evaluate/variable.cpp
Expand Up @@ -258,7 +258,20 @@ static std::optional<Expr<SubscriptInteger>> SymbolLEN(const Symbol &symbol) {
}
}
if (auto dyType{DynamicType::From(ultimate)}) {
if (auto len{dyType->GetCharLength()}) {
auto len{dyType->GetCharLength()};
if (!len && ultimate.attrs().test(semantics::Attr::PARAMETER)) {
// Its initializer determines the length of an implied-length named
// constant.
if (const auto *object{
ultimate.detailsIf<semantics::ObjectEntityDetails>()}) {
if (object->init()) {
if (auto dyType2{DynamicType::From(*object->init())}) {
len = dyType2->GetCharLength();
}
}
}
}
if (len) {
if (auto constLen{ToInt64(*len)}) {
return Expr<SubscriptInteger>{std::max<std::int64_t>(*constLen, 0)};
} else if (ultimate.owner().IsDerivedType() ||
Expand Down
5 changes: 5 additions & 0 deletions flang/test/Evaluate/fold-substr.f90
Expand Up @@ -14,4 +14,9 @@ module m
logical, parameter :: test_05b = len(ca(:)(2:4)) == 3
logical, parameter :: test_06a = ca(1)(1:2)//ca(2)(2:3)//ca(3)(3:4) == "abfgkl"
logical, parameter :: test_06b = len(ca(1)(1:2)//ca(2)(2:3)//ca(3)(3:4)) == 6
logical, parameter :: test_07a = ca(1)(:2) == "ab"
logical, parameter :: test_07b = ca(1)(3:) == "cd"
logical, parameter :: test_07c = ca(1)(:-1) == ""
logical, parameter :: test_07d = ca(1)(5:) == ""
logical, parameter :: test_07e = ca(1)(:) == "abcd"
end module
5 changes: 2 additions & 3 deletions flang/test/Lower/character-local-variables.f90
Expand Up @@ -116,9 +116,8 @@ subroutine dyn_array_dyn_len_lb(l, n)
subroutine assumed_length_param(n)
character(*), parameter :: c(1)=(/"abcd"/)
integer :: n
! CHECK: %[[c4:.*]] = arith.constant 4 : index
! CHECK: %[[len:.*]] = fir.convert %[[c4]] : (index) -> i64
! CHECK: fir.store %[[len]] to %[[tmp:.*]] : !fir.ref<i64>
! CHECK: %[[c4:.*]] = arith.constant 4 : i64
! CHECK: fir.store %[[c4]] to %[[tmp:.*]] : !fir.ref<i64>
! CHECK: fir.call @_QPtake_int(%[[tmp]]) {{.*}}: (!fir.ref<i64>) -> ()
call take_int(len(c(n), kind=8))
end
Expand Down

0 comments on commit 8fed620

Please sign in to comment.