diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp index ad586cab7c4f3..bc579a0eeb435 100644 --- a/flang/lib/Lower/ConvertVariable.cpp +++ b/flang/lib/Lower/ConvertVariable.cpp @@ -625,6 +625,11 @@ mustBeDefaultInitializedAtRuntime(const Fortran::lower::pft::Variable &var) { return false; if (Fortran::semantics::IsDummy(sym) && !Fortran::semantics::IsIntentOut(sym)) return false; + // Polymorphic intent(out) dummy might need default initialization + // at runtime. + if (Fortran::semantics::IsPolymorphic(sym) && + Fortran::semantics::IsDummy(sym) && Fortran::semantics::IsIntentOut(sym)) + return true; // Local variables (including function results), and intent(out) dummies must // be default initialized at runtime if their type has default initialization. return hasDefaultInitialization(sym); diff --git a/flang/test/Lower/polymorphic.f90 b/flang/test/Lower/polymorphic.f90 index bd31cc75f8051..48c54a012c532 100644 --- a/flang/test/Lower/polymorphic.f90 +++ b/flang/test/Lower/polymorphic.f90 @@ -23,7 +23,7 @@ module polymorphic_test end type type, extends(p1) :: p2 - real :: c + real :: c = 10.5 end type type r1 @@ -738,6 +738,24 @@ subroutine test_unlimited_polymorphic_alloc_array_ret() ! CHECK: %[[RES:.*]] = fir.call @_QMpolymorphic_testPunlimited_polymorphic_alloc_array_ret() fastmath : () -> !fir.class>> ! CHECK: fir.save_result %[[RES]] to %[[RES_TMP]] : !fir.class>>, !fir.ref>>> + subroutine test_unlimited_polymorphic_intentout(a) + class(*), intent(out) :: a + end subroutine + +! CHECK-LABEL: func.func @_QMpolymorphic_testPtest_unlimited_polymorphic_intentout( +! CHECK-SAME: %[[ARG0:.*]]: !fir.class {fir.bindc_name = "a"}) { +! CHECK: %[[BOX_NONE:.*]] = fir.convert %[[ARG0]] : (!fir.class) -> !fir.box +! CHECK: %{{.*}} = fir.call @_FortranAInitialize(%[[BOX_NONE]], %{{.*}}, %{{.*}}) {{.*}} : (!fir.box, !fir.ref, i32) -> none + + subroutine test_polymorphic_intentout(a) + class(p1), intent(out) :: a + end subroutine + +! CHECK-LABEL: func.func @_QMpolymorphic_testPtest_polymorphic_intentout( +! CHECK-SAME: %[[ARG0:.*]]: !fir.class> {fir.bindc_name = "a"}) { +! CHECK: %[[BOX_NONE:.*]] = fir.convert %[[ARG0]] : (!fir.class>) -> !fir.box +! CHECK: %{{.*}} = fir.call @_FortranAInitialize(%[[BOX_NONE]], %{{.*}}, %{{.*}}) {{.*}} : (!fir.box, !fir.ref, i32) -> none + end module program test