Skip to content

Commit 591e3e6

Browse files
committed
[flang] Make EndProgramStmt a NOP + early return
Fix done in D143055 can be simpler by making EndProgramStmt a NOP and dealing with the exit in `endNewFunction` in a centralize way. Also add finalization when there is an early exit in the main program. Reviewed By: jeanPerier Differential Revision: https://reviews.llvm.org/D143065
1 parent 78f8808 commit 591e3e6

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

flang/lib/Lower/Bridge.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,6 @@ class FirConverter : public Fortran::lower::AbstractConverter {
950950
if (blockIsUnterminated())
951951
builder->create<mlir::func::ReturnOp>(toLocation());
952952
}
953-
void genFIR(const Fortran::parser::EndProgramStmt &) { genExitRoutine(); }
954953

955954
/// END of procedure-like constructs
956955
///
@@ -3059,6 +3058,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
30593058
getEval().getOwningProcedure();
30603059
assert(funit && "not inside main program, function or subroutine");
30613060
if (funit->isMainProgram()) {
3061+
bridge.fctCtx().finalizeAndKeep();
30623062
genExitRoutine();
30633063
return;
30643064
}
@@ -3114,6 +3114,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
31143114
void genFIR(const Fortran::parser::EndFunctionStmt &) {} // nop
31153115
void genFIR(const Fortran::parser::EndIfStmt &) {} // nop
31163116
void genFIR(const Fortran::parser::EndMpSubprogramStmt &) {} // nop
3117+
void genFIR(const Fortran::parser::EndProgramStmt &) {} // nop
31173118
void genFIR(const Fortran::parser::EndSelectStmt &) {} // nop
31183119
void genFIR(const Fortran::parser::EndSubroutineStmt &) {} // nop
31193120
void genFIR(const Fortran::parser::EntryStmt &) {} // nop
@@ -3438,16 +3439,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
34383439
void endNewFunction(Fortran::lower::pft::FunctionLikeUnit &funit) {
34393440
setCurrentPosition(Fortran::lower::pft::stmtSourceLoc(funit.endStmt));
34403441
if (funit.isMainProgram()) {
3441-
if (!blockIsUnterminated()) {
3442-
auto insertPt = builder->saveInsertionPoint();
3443-
mlir::Block *currentBlock = builder->getBlock();
3444-
builder->setInsertionPoint(&currentBlock->back());
3445-
bridge.fctCtx().finalizeAndPop();
3446-
builder->restoreInsertionPoint(insertPt);
3447-
} else {
3448-
bridge.fctCtx().finalizeAndPop();
3449-
genExitRoutine();
3450-
}
3442+
bridge.fctCtx().finalizeAndPop();
3443+
genExitRoutine();
34513444
} else {
34523445
genFIRProcedureExit(funit, funit.getSubprogramSymbol());
34533446
}

flang/test/Lower/derived-type-finalization.f90

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,20 @@ subroutine test_finalize_intent_out(t)
153153
program p
154154
use derived_type_finalization
155155
type(t1) :: t
156+
if (t%a == 10) return
156157
print *, 'end of program'
157158
end program
158159

159160
! CHECK-LABEL: func.func @_QQmain() {
160161
! CHECK: %[[T:.*]] = fir.alloca !fir.type<_QMderived_type_finalizationTt1{a:i32}> {bindc_name = "t", uniq_name = "_QFEt"}
161-
! CHECK: %[[EMBOX:.*]] = fir.embox %[[T]] : (!fir.ref<!fir.type<_QMderived_type_finalizationTt1{a:i32}>>) -> !fir.box<!fir.type<_QMderived_type_finalizationTt1{a:i32}>>
162-
! CHECK: %[[BOX_NONE:.*]] = fir.convert %[[EMBOX]] : (!fir.box<!fir.type<_QMderived_type_finalizationTt1{a:i32}>>) -> !fir.box<none>
163-
! CHECK: %{{.*}} = fir.call @_FortranADestroy(%[[BOX_NONE]]) {{.*}} : (!fir.box<none>) -> none
164-
! CHECK: return
162+
! CHECK: cf.cond_br %{{.*}}, ^bb1, ^bb2
163+
! CHECK: ^bb1:
164+
! CHECK: %[[EMBOX:.*]] = fir.embox %[[T]] : (!fir.ref<!fir.type<_QMderived_type_finalizationTt1{a:i32}>>) -> !fir.box<!fir.type<_QMderived_type_finalizationTt1{a:i32}>>
165+
! CHECK: %[[BOX_NONE:.*]] = fir.convert %[[EMBOX]] : (!fir.box<!fir.type<_QMderived_type_finalizationTt1{a:i32}>>) -> !fir.box<none>
166+
! CHECK: %{{.*}} = fir.call @_FortranADestroy(%[[BOX_NONE]]) {{.*}} : (!fir.box<none>) -> none
167+
! CHECK: return
168+
! CHECK: ^bb2:
169+
! CHECK: %[[EMBOX:.*]] = fir.embox %[[T]] : (!fir.ref<!fir.type<_QMderived_type_finalizationTt1{a:i32}>>) -> !fir.box<!fir.type<_QMderived_type_finalizationTt1{a:i32}>>
170+
! CHECK: %[[BOX_NONE:.*]] = fir.convert %[[EMBOX]] : (!fir.box<!fir.type<_QMderived_type_finalizationTt1{a:i32}>>) -> !fir.box<none>
171+
! CHECK: %{{.*}} = fir.call @_FortranADestroy(%[[BOX_NONE]]) {{.*}} : (!fir.box<none>) -> none
172+
! CHECK: return

0 commit comments

Comments
 (0)