[flang-commits] [flang] 3915789 - Folding in the front end was replacing calls to LEN and dropping

Eric Schweitz via flang-commits flang-commits at lists.llvm.org
Wed Feb 9 16:50:40 PST 2022


Author: Eric Schweitz
Date: 2022-02-09T16:50:28-08:00
New Revision: 3915789503344e9d4ba8c3e01b9b1dc717a49f44

URL: https://github.com/llvm/llvm-project/commit/3915789503344e9d4ba8c3e01b9b1dc717a49f44
DIFF: https://github.com/llvm/llvm-project/commit/3915789503344e9d4ba8c3e01b9b1dc717a49f44.diff

LOG: Folding in the front end was replacing calls to LEN and dropping
arguments even in situations where the arguments are required to compute
the LEN value at runtime.

Add tests.

Differential Revision: https://reviews.llvm.org/D119373

Added: 
    

Modified: 
    flang/lib/Evaluate/fold-integer.cpp
    flang/test/Evaluate/rewrite01.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/fold-integer.cpp b/flang/lib/Evaluate/fold-integer.cpp
index 2618949b29d84..6a110cbe7e8ce 100644
--- a/flang/lib/Evaluate/fold-integer.cpp
+++ b/flang/lib/Evaluate/fold-integer.cpp
@@ -677,7 +677,11 @@ Expr<Type<TypeCategory::Integer, KIND>> FoldIntrinsicFunction(
       return std::visit(
           [&](auto &kx) {
             if (auto len{kx.LEN()}) {
-              return Fold(context, ConvertToType<T>(*std::move(len)));
+              if (IsScopeInvariantExpr(*len)) {
+                return Fold(context, ConvertToType<T>(*std::move(len)));
+              } else {
+                return Expr<T>{std::move(funcRef)};
+              }
             } else {
               return Expr<T>{std::move(funcRef)};
             }

diff  --git a/flang/test/Evaluate/rewrite01.f90 b/flang/test/Evaluate/rewrite01.f90
index f5fc57899f162..1627dc62c1b9d 100644
--- a/flang/test/Evaluate/rewrite01.f90
+++ b/flang/test/Evaluate/rewrite01.f90
@@ -76,4 +76,44 @@ subroutine lbound_test(x, n, m)
   !CHECK: PRINT *, 1_4
   print *, lbound(returns_array_3(), dim=1)
 end subroutine
+
+!CHECK: len_test
+subroutine len_test(a,b, c, d, e, n, m)
+  character(*), intent(in) :: a
+  character(*) :: b
+  external b
+  character(10), intent(in) :: c
+  character(10) :: d
+  external d
+  integer, intent(in) :: n, m
+  character(n), intent(in) :: e
+
+  !CHECK: PRINT *, int(a%len,kind=4)
+  print *, len(a)
+  !CHECK: PRINT *, 5_4
+  print *, len(a(1:5))
+  !CHECK: PRINT *, len(b(a))
+  print *, len(b(a))
+  !CHECK: PRINT *, len(b(a)//a)
+  print *, len(b(a) // a)
+  !CHECK: PRINT *, 10_4
+  print *, len(c)
+  !CHECK: PRINT *, len(c(int(i,kind=8):int(j,kind=8)))
+  print *, len(c(i:j))
+  !CHECK: PRINT *, 5_4
+  print *, len(c(1:5))
+  !CHECK: PRINT *, 10_4
+  print *, len(d(c))
+  !CHECK: PRINT *, 20_4
+  print *, len(d(c) // c)
+  !CHECK: PRINT *, 0_4
+  print *, len(a(10:4))
+  !CHECK: PRINT *, int(max(0_8,int(m,kind=8)-int(n,kind=8)+1_8),kind=4)
+  print *, len(a(n:m))
+  !CHECK: PRINT *, len(b(a(int(n,kind=8):int(m,kind=8))))
+  print *, len(b(a(n:m)))
+  !CHECK: PRINT *, int(max(0_8,max(0_8,int(n,kind=8))-4_8+1_8),kind=4)
+  print *, len(e(4:))
+end subroutine len_test
+
 end module


        


More information about the flang-commits mailing list