[flang-commits] [PATCH] D148128: [flang] Fold IS_CONTIGUOUS for TYPE(*) when possible

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Apr 12 06:54:46 PDT 2023


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

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148128

Files:
  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
  flang/test/Evaluate/rewrite03.f90


Index: flang/test/Evaluate/rewrite03.f90
===================================================================
--- /dev/null
+++ 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
Index: flang/lib/Semantics/check-declarations.cpp
===================================================================
--- flang/lib/Semantics/check-declarations.cpp
+++ flang/lib/Semantics/check-declarations.cpp
@@ -587,9 +587,8 @@
             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());
       }
     }
Index: flang/lib/Evaluate/fold-logical.cpp
===================================================================
--- flang/lib/Evaluate/fold-logical.cpp
+++ flang/lib/Evaluate/fold-logical.cpp
@@ -187,6 +187,10 @@
         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") {
Index: flang/lib/Evaluate/check-expression.cpp
===================================================================
--- flang/lib/Evaluate/check-expression.cpp
+++ flang/lib/Evaluate/check-expression.cpp
@@ -768,11 +768,11 @@
     } 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();
+      return true;
     } else {
       return Base::operator()(ultimate);
     }
@@ -936,6 +936,7 @@
 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> {
Index: flang/include/flang/Evaluate/check-expression.h
===================================================================
--- flang/include/flang/Evaluate/check-expression.h
+++ flang/include/flang/Evaluate/check-expression.h
@@ -110,6 +110,8 @@
     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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148128.512817.patch
Type: text/x-patch
Size: 4118 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230412/463bb1df/attachment-0001.bin>


More information about the flang-commits mailing list