[flang-commits] [flang] 8fed620 - [flang] Handle missing substring upper bound better when folding
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Tue Jan 31 08:18:10 PST 2023
Author: Peter Klausler
Date: 2023-01-31T08:18:00-08:00
New Revision: 8fed620e98f4abd772adb4a98d9e3f6cf050c524
URL: https://github.com/llvm/llvm-project/commit/8fed620e98f4abd772adb4a98d9e3f6cf050c524
DIFF: https://github.com/llvm/llvm-project/commit/8fed620e98f4abd772adb4a98d9e3f6cf050c524.diff
LOG: [flang] Handle missing substring upper bound better when folding
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
Added:
Modified:
flang/lib/Evaluate/variable.cpp
flang/test/Evaluate/fold-substr.f90
flang/test/Lower/character-local-variables.f90
Removed:
################################################################################
diff --git a/flang/lib/Evaluate/variable.cpp b/flang/lib/Evaluate/variable.cpp
index 1749425b6aec..b68772e8304c 100644
--- a/flang/lib/Evaluate/variable.cpp
+++ b/flang/lib/Evaluate/variable.cpp
@@ -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() ||
diff --git a/flang/test/Evaluate/fold-substr.f90 b/flang/test/Evaluate/fold-substr.f90
index c2b73c2affc9..b36887e7f4df 100644
--- a/flang/test/Evaluate/fold-substr.f90
+++ b/flang/test/Evaluate/fold-substr.f90
@@ -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
diff --git a/flang/test/Lower/character-local-variables.f90 b/flang/test/Lower/character-local-variables.f90
index e8be6bb6caf2..305f951ce51f 100644
--- a/flang/test/Lower/character-local-variables.f90
+++ b/flang/test/Lower/character-local-variables.f90
@@ -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
More information about the flang-commits
mailing list