[flang-commits] [flang] 6f5df41 - [flang] Fold IS_CONTIGUOUS for TYPE(*) when possible

Jean Perier via flang-commits flang-commits at lists.llvm.org
Thu Apr 13 23:42:59 PDT 2023


Author: Jean Perier
Date: 2023-04-14T08:42:25+02:00
New Revision: 6f5df4199e64020d9290838a72ba79b74ed91269

URL: https://github.com/llvm/llvm-project/commit/6f5df4199e64020d9290838a72ba79b74ed91269
DIFF: https://github.com/llvm/llvm-project/commit/6f5df4199e64020d9290838a72ba79b74ed91269.diff

LOG: [flang] Fold IS_CONTIGUOUS for TYPE(*) when possible

TYPE(*) arguments fell through in IS_CONTIGUOUS folding
because they are not Expr<SomeType>. Expose entry point for
symbols in IsContiguous and use that.

The added test revealed that IS_CONTIGUOUS was folded to
false for assumed rank arguments. Fix this: the contiguity of
assumed rank without the CONTIGUOUS argument can only be
verified at runtime.

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

Added: 
    flang/test/Evaluate/rewrite03.f90

Modified: 
    flang/include/flang/Evaluate/check-expression.h
    flang/lib/Evaluate/check-expression.cpp
    flang/lib/Evaluate/fold-logical.cpp
    flang/lib/Semantics/check-declarations.cpp

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Evaluate/check-expression.h b/flang/include/flang/Evaluate/check-expression.h
index 78b92c4669f70..e942ad7ebfc46 100644
--- a/flang/include/flang/Evaluate/check-expression.h
+++ b/flang/include/flang/Evaluate/check-expression.h
@@ -110,6 +110,8 @@ extern template std::optional<bool> IsContiguous(
     const ComplexPart &, FoldingContext &);
 extern template std::optional<bool> IsContiguous(
     const CoarrayRef &, FoldingContext &);
+extern template std::optional<bool> IsContiguous(
+    const Symbol &, FoldingContext &);
 template <typename A>
 bool IsSimplyContiguous(const A &x, FoldingContext &context) {
   return IsContiguous(x, context).value_or(false);

diff  --git a/flang/lib/Evaluate/check-expression.cpp b/flang/lib/Evaluate/check-expression.cpp
index 57d25c8091872..d307af6a9e14c 100644
--- a/flang/lib/Evaluate/check-expression.cpp
+++ b/flang/lib/Evaluate/check-expression.cpp
@@ -768,11 +768,10 @@ class IsContiguousHelper
     } else if (ultimate.has<semantics::AssocEntityDetails>()) {
       return Base::operator()(ultimate); // use expr
     } else if (semantics::IsPointer(ultimate) ||
-        semantics::IsAssumedShape(ultimate)) {
+        semantics::IsAssumedShape(ultimate) || IsAssumedRank(ultimate)) {
       return std::nullopt;
-    } else if (const auto *details{
-                   ultimate.detailsIf<semantics::ObjectEntityDetails>()}) {
-      return !details->IsAssumedRank();
+    } else if (ultimate.has<semantics::ObjectEntityDetails>()) {
+      return true;
     } else {
       return Base::operator()(ultimate);
     }
@@ -936,6 +935,7 @@ template std::optional<bool> IsContiguous(const Component &, FoldingContext &);
 template std::optional<bool> IsContiguous(
     const ComplexPart &, FoldingContext &);
 template std::optional<bool> IsContiguous(const CoarrayRef &, FoldingContext &);
+template std::optional<bool> IsContiguous(const Symbol &, FoldingContext &);
 
 // IsErrorExpr()
 struct IsErrorExprHelper : public AnyTraverse<IsErrorExprHelper, bool> {

diff  --git a/flang/lib/Evaluate/fold-logical.cpp b/flang/lib/Evaluate/fold-logical.cpp
index 43406497a9e66..22df42c4d37ca 100644
--- a/flang/lib/Evaluate/fold-logical.cpp
+++ b/flang/lib/Evaluate/fold-logical.cpp
@@ -187,6 +187,10 @@ Expr<Type<TypeCategory::Logical, KIND>> FoldIntrinsicFunction(
         if (auto contiguous{IsContiguous(*expr, context)}) {
           return Expr<T>{*contiguous};
         }
+      } else if (auto *assumedType{args[0]->GetAssumedTypeDummy()}) {
+        if (auto contiguous{IsContiguous(*assumedType, context)}) {
+          return Expr<T>{*contiguous};
+        }
       }
     }
   } else if (name == "lge" || name == "lgt" || name == "lle" || name == "llt") {

diff  --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index da7d9c99ff863..9cfa96f47c2d6 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -586,9 +586,8 @@ void CheckHelper::CheckAssumedTypeEntity( // C709
             symbol.name());
       }
       if (details.IsArray() && details.shape().IsExplicitShape()) {
-        messages_.Say(
-            "Assumed-type array argument 'arg8' must be assumed shape,"
-            " assumed size, or assumed rank"_err_en_US,
+        messages_.Say("Assumed-type array argument '%s' must be assumed shape,"
+                      " assumed size, or assumed rank"_err_en_US,
             symbol.name());
       }
     }

diff  --git a/flang/test/Evaluate/rewrite03.f90 b/flang/test/Evaluate/rewrite03.f90
new file mode 100644
index 0000000000000..dfe89ede3449b
--- /dev/null
+++ b/flang/test/Evaluate/rewrite03.f90
@@ -0,0 +1,19 @@
+! RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
+! Tests rewrite of IS_CONTIGUOUS with TYPE(*) arguments.
+
+subroutine test_is_contiguous(assumed_size, assumed_shape, &
+      & assumed_shape_contiguous, assumed_rank, assumed_rank_contiguous)
+  type(*) :: assumed_size(*), assumed_shape(:), assumed_shape_contiguous(:)
+  type(*) :: assumed_rank(..), assumed_rank_contiguous(..)
+  contiguous :: assumed_shape_contiguous, assumed_rank_contiguous
+! CHECK: PRINT *, .true._4
+  print *, is_contiguous(assumed_size)
+! CHECK: PRINT *, .true._4
+  print *, is_contiguous(assumed_shape_contiguous)
+! CHECK: PRINT *, .true._4
+  print *, is_contiguous(assumed_rank_contiguous)
+! CHECK: PRINT *, is_contiguous(assumed_shape)
+  print *, is_contiguous(assumed_shape)
+! CHECK: PRINT *, is_contiguous(assumed_rank)
+  print *, is_contiguous(assumed_rank)
+end subroutine


        


More information about the flang-commits mailing list