[flang-commits] [flang] 287e9e9 - [flang] Do not initialize intent(out) polymorphic pointer or allocatable

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Sun Feb 26 01:45:01 PST 2023


Author: Valentin Clement
Date: 2023-02-26T10:44:55+01:00
New Revision: 287e9e988798c591e8472a76308afe6e25704896

URL: https://github.com/llvm/llvm-project/commit/287e9e988798c591e8472a76308afe6e25704896
DIFF: https://github.com/llvm/llvm-project/commit/287e9e988798c591e8472a76308afe6e25704896.diff

LOG: [flang] Do not initialize intent(out) polymorphic pointer or allocatable

Calling the runtime on disassociated pointer or unallocated
allocatable will trigger a segfault.

Reviewed By: PeteSteinfeld

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

Added: 
    

Modified: 
    flang/lib/Lower/ConvertVariable.cpp
    flang/test/Lower/default-initialization.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index 82960f96872ff..40c1cdc29f5f0 100644
--- a/flang/lib/Lower/ConvertVariable.cpp
+++ b/flang/lib/Lower/ConvertVariable.cpp
@@ -600,7 +600,10 @@ mustBeDefaultInitializedAtRuntime(const Fortran::lower::pft::Variable &var) {
   // Polymorphic intent(out) dummy might need default initialization
   // at runtime.
   if (Fortran::semantics::IsPolymorphic(sym) &&
-      Fortran::semantics::IsDummy(sym) && Fortran::semantics::IsIntentOut(sym))
+      Fortran::semantics::IsDummy(sym) &&
+      Fortran::semantics::IsIntentOut(sym) &&
+      !Fortran::semantics::IsAllocatable(sym) &&
+      !Fortran::semantics::IsPointer(sym))
     return true;
   // Local variables (including function results), and intent(out) dummies must
   // be default initialized at runtime if their type has default initialization.

diff  --git a/flang/test/Lower/default-initialization.f90 b/flang/test/Lower/default-initialization.f90
index c28622bdc6dbb..4c14fdf23512d 100644
--- a/flang/test/Lower/default-initialization.f90
+++ b/flang/test/Lower/default-initialization.f90
@@ -1,5 +1,5 @@
 ! Test default initialization of local and dummy variables (dynamic initialization)
-! RUN: bbc -emit-fir %s -o - | FileCheck %s
+! RUN: bbc -emit-fir -polymorphic-type %s -o - | FileCheck %s
 
 module test_dinit
   type t
@@ -159,8 +159,19 @@ subroutine noinit_intentinout_dummy(x)
     ! CHECK: return
   end subroutine 
 
+
+  subroutine test_pointer_intentout(a, b)
+    type(t), pointer, intent(out) :: a
+    class(t), pointer, intent(out) :: b
+  end subroutine
+
+! CHECK-LABEL: func.func @_QMtest_dinitPtest_pointer_intentout(
+! CHECK-NOT: fir.call @_FortranAInitialize
+
 end module
 
+! CHECK-LABEL: func.func @_QQmain
+
 ! End-to-end test for debug pruposes.
   use test_dinit
   type(t) :: at


        


More information about the flang-commits mailing list