[flang-commits] [PATCH] D144512: [flang] Preserve useResultSymbolShape_ option when folding array constructor shape

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Tue Feb 21 11:57:08 PST 2023


jeanPerier created this revision.
jeanPerier added a reviewer: klausler.
jeanPerier added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
jeanPerier requested review of this revision.

By default evaluate::GetShape(expr) may return a compiler generated expression
using symbols that are part of function interfaces if there are function
references in "expr".
It is not right to replace an inquiry of "expr" with such compiler
generated expression since the call context would be lost, along with
the meaning of the inquiry expression.
Inquiry folding uses GetContextFreeShape(expr) that sets-up
useResultSymbolShape_ in GetShapeHelper to prevent such bad rewrites. But this did not
work properly with array constructor: GetShapeHelper made a call to
GetShape, ignoring and losing the "useResultSymbolShape_" instruction.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144512

Files:
  flang/include/flang/Evaluate/shape.h
  flang/test/Evaluate/rewrite01.f90


Index: flang/test/Evaluate/rewrite01.f90
===================================================================
--- flang/test/Evaluate/rewrite01.f90
+++ flang/test/Evaluate/rewrite01.f90
@@ -195,4 +195,15 @@
   end associate
 end subroutine
 
+!CHECK-LABEL: array_constructor
+subroutine array_constructor()
+  interface
+    function return_allocatable()
+     real, allocatable :: return_allocatable(:)
+    end function
+  end interface
+  !CHECK: PRINT *, size([REAL(4)::return_allocatable(),return_allocatable()])
+  print *, size([return_allocatable(), return_allocatable()])
+end subroutine
+
 end module
Index: flang/include/flang/Evaluate/shape.h
===================================================================
--- flang/include/flang/Evaluate/shape.h
+++ flang/include/flang/Evaluate/shape.h
@@ -168,8 +168,9 @@
     return common::visit(
         common::visitors{
             [&](const Expr<T> &x) -> MaybeExtentExpr {
-              if (auto xShape{
-                      context_ ? GetShape(*context_, x) : GetShape(x)}) {
+              if (auto xShape{!useResultSymbolShape_ ? (*this)(x)
+                          : context_                 ? GetShape(*context_, x)
+                                                     : GetShape(x)}) {
                 // Array values in array constructors get linearized.
                 return GetSize(std::move(*xShape));
               } else {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144512.499262.patch
Type: text/x-patch
Size: 1407 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230221/8d801cfb/attachment-0001.bin>


More information about the flang-commits mailing list