diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp index 03a7cca1ab698..0b37fb2bd886b 100644 --- a/flang/lib/Lower/ConvertVariable.cpp +++ b/flang/lib/Lower/ConvertVariable.cpp @@ -660,6 +660,11 @@ static bool needEndFinalization(const Fortran::lower::pft::Variable &var) { if (!var.hasSymbol()) return false; const Fortran::semantics::Symbol &sym = var.getSymbol(); + const Fortran::semantics::Scope &owner = sym.owner(); + if (owner.kind() == Fortran::semantics::Scope::Kind::MainProgram) { + // The standard does not require finalizing main program variables. + return false; + } if (!Fortran::semantics::IsPointer(sym) && !Fortran::semantics::IsAllocatable(sym) && !Fortran::semantics::IsDummy(sym) && diff --git a/flang/test/Lower/derived-type-finalization.f90 b/flang/test/Lower/derived-type-finalization.f90 index 2194fd8d96451..d0fbfe8906c4d 100644 --- a/flang/test/Lower/derived-type-finalization.f90 +++ b/flang/test/Lower/derived-type-finalization.f90 @@ -248,20 +248,7 @@ subroutine local_t4() program p use derived_type_finalization type(t1) :: t - if (t%a == 10) return - print *, 'end of program' end program ! CHECK-LABEL: func.func @_QQmain() attributes {fir.bindc_name = "p"} { -! CHECK: %[[T:.*]] = fir.alloca !fir.type<_QMderived_type_finalizationTt1{a:i32}> {bindc_name = "t", uniq_name = "_QFEt"} -! CHECK: cf.cond_br %{{.*}}, ^bb1, ^bb2 -! CHECK: ^bb1: -! CHECK: %[[EMBOX:.*]] = fir.embox %[[T]] : (!fir.ref>) -> !fir.box> -! CHECK: %[[BOX_NONE:.*]] = fir.convert %[[EMBOX]] : (!fir.box>) -> !fir.box -! CHECK: %{{.*}} = fir.call @_FortranADestroy(%[[BOX_NONE]]) {{.*}} : (!fir.box) -> none -! CHECK: return -! CHECK: ^bb2: -! CHECK: %[[EMBOX:.*]] = fir.embox %[[T]] : (!fir.ref>) -> !fir.box> -! CHECK: %[[BOX_NONE:.*]] = fir.convert %[[EMBOX]] : (!fir.box>) -> !fir.box -! CHECK: %{{.*}} = fir.call @_FortranADestroy(%[[BOX_NONE]]) {{.*}} : (!fir.box) -> none -! CHECK: return +! CHECK-NOT: fir.call @_FortranADestroy