[flang-commits] [flang] d65aeeb - [flang] Make sure derived-type finalization is done before return

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Wed Feb 1 05:56:25 PST 2023


Author: Valentin Clement
Date: 2023-02-01T14:56:18+01:00
New Revision: d65aeeb360e279dfc7c9ddf7c8555b585e9df0d4

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

LOG: [flang] Make sure derived-type finalization is done before return

Finalization needs to be done before the terminator. In case
of end program, this was done after it and trigger a verifier error.
This patch fixes this case.

Reviewed By: jeanPerier

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

Added: 
    

Modified: 
    flang/lib/Lower/Bridge.cpp
    flang/test/Lower/derived-type-finalization.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 4f9848d599ef7..492176f2e1305 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -3438,8 +3438,16 @@ class FirConverter : public Fortran::lower::AbstractConverter {
   void endNewFunction(Fortran::lower::pft::FunctionLikeUnit &funit) {
     setCurrentPosition(Fortran::lower::pft::stmtSourceLoc(funit.endStmt));
     if (funit.isMainProgram()) {
-      bridge.fctCtx().finalizeAndPop();
-      genExitRoutine();
+      if (!blockIsUnterminated()) {
+        auto insertPt = builder->saveInsertionPoint();
+        mlir::Block *currentBlock = builder->getBlock();
+        builder->setInsertionPoint(&currentBlock->back());
+        bridge.fctCtx().finalizeAndPop();
+        builder->restoreInsertionPoint(insertPt);
+      } else {
+        bridge.fctCtx().finalizeAndPop();
+        genExitRoutine();
+      }
     } else {
       genFIRProcedureExit(funit, funit.getSubprogramSymbol());
     }

diff  --git a/flang/test/Lower/derived-type-finalization.f90 b/flang/test/Lower/derived-type-finalization.f90
index 81a8a895e11e2..6dcf7cf41ffd9 100644
--- a/flang/test/Lower/derived-type-finalization.f90
+++ b/flang/test/Lower/derived-type-finalization.f90
@@ -149,3 +149,16 @@ subroutine test_finalize_intent_out(t)
 ! CHECK: return
 
 end module
+
+program p
+  use derived_type_finalization
+  type(t1) :: t
+  print *, 'end of program'
+end program
+
+! CHECK-LABEL: func.func @_QQmain() {
+! CHECK: %[[T:.*]] = fir.alloca !fir.type<_QMderived_type_finalizationTt1{a:i32}> {bindc_name = "t", uniq_name = "_QFEt"}
+! CHECK: %[[EMBOX:.*]] = fir.embox %[[T]] : (!fir.ref<!fir.type<_QMderived_type_finalizationTt1{a:i32}>>) -> !fir.box<!fir.type<_QMderived_type_finalizationTt1{a:i32}>>
+! CHECK: %[[BOX_NONE:.*]] = fir.convert %[[EMBOX]] : (!fir.box<!fir.type<_QMderived_type_finalizationTt1{a:i32}>>) -> !fir.box<none>
+! CHECK: %{{.*}} = fir.call @_FortranADestroy(%[[BOX_NONE]]) {{.*}} : (!fir.box<none>) -> none
+! CHECK: return


        


More information about the flang-commits mailing list