[flang-commits] [flang] 91bd4c6 - [flang] Preserve useResultSymbolShape_ option when folding array constructor shape

Jean Perier via flang-commits flang-commits at lists.llvm.org
Fri Feb 24 00:06:55 PST 2023


Author: Jean Perier
Date: 2023-02-24T09:06:30+01:00
New Revision: 91bd4c6e81ac8aebf1089eb52b935484925f2f58

URL: https://github.com/llvm/llvm-project/commit/91bd4c6e81ac8aebf1089eb52b935484925f2f58
DIFF: https://github.com/llvm/llvm-project/commit/91bd4c6e81ac8aebf1089eb52b935484925f2f58.diff

LOG: [flang] Preserve useResultSymbolShape_ option when folding array constructor shape

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.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Evaluate/shape.h b/flang/include/flang/Evaluate/shape.h
index 9183c7ddf893d..0539d8f19b55a 100644
--- a/flang/include/flang/Evaluate/shape.h
+++ b/flang/include/flang/Evaluate/shape.h
@@ -168,8 +168,9 @@ class GetShapeHelper
     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 {

diff  --git a/flang/test/Evaluate/rewrite01.f90 b/flang/test/Evaluate/rewrite01.f90
index d841699688db8..e971ff29d2b88 100644
--- a/flang/test/Evaluate/rewrite01.f90
+++ b/flang/test/Evaluate/rewrite01.f90
@@ -195,4 +195,15 @@ subroutine may_change_p_bounds(p)
   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


        


More information about the flang-commits mailing list