[flang-commits] [PATCH] D142942: [flang] Handle missing substring upper bound better when folding

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Mon Jan 30 16:17:23 PST 2023


klausler created this revision.
klausler added a reviewer: vzakhari.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

When folding a substring of a named constant or character literal,
acquire the value of a missing upper bound from the base object.


https://reviews.llvm.org/D142942

Files:
  flang/lib/Evaluate/variable.cpp
  flang/test/Evaluate/fold-substr.f90
  flang/test/Lower/character-local-variables.f90


Index: flang/test/Lower/character-local-variables.f90
===================================================================
--- flang/test/Lower/character-local-variables.f90
+++ flang/test/Lower/character-local-variables.f90
@@ -116,9 +116,8 @@
 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
Index: flang/test/Evaluate/fold-substr.f90
===================================================================
--- flang/test/Evaluate/fold-substr.f90
+++ flang/test/Evaluate/fold-substr.f90
@@ -14,4 +14,9 @@
   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
Index: flang/lib/Evaluate/variable.cpp
===================================================================
--- flang/lib/Evaluate/variable.cpp
+++ flang/lib/Evaluate/variable.cpp
@@ -258,7 +258,20 @@
     }
   }
   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() ||


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142942.493447.patch
Type: text/x-patch
Size: 2442 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230131/b235c865/attachment.bin>


More information about the flang-commits mailing list