Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions flang/include/flang/Lower/StatementContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,26 @@ class StatementContext {
}
}

/// Make cleanup calls. Retain the stack top list for a repeat call.
/// Make a cleanup call. Retain the stack top list for a repeat call.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can keep the original comment since we generate 0 to N cleanup calls.

void finalizeAndKeep() {
assert(!cufs.empty() && "invalid finalize statement context");
if (cufs.back())
(*cufs.back())();
}

/// Make cleanup calls. Clear the stack top list.
/// Make a cleanup call. Clear the stack top list.
void finalizeAndReset() {
finalizeAndKeep();
cufs.back().reset();
}

/// Make cleanup calls. Pop the stack top list.
/// Pop the stack top list.
void pop() { cufs.pop_back(); }

/// Make a cleanup call. Pop the stack top list.
void finalizeAndPop() {
finalizeAndKeep();
cufs.pop_back();
pop();
}

bool hasCode() const {
Expand Down
37 changes: 17 additions & 20 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1621,13 +1621,19 @@ class FirConverter : public Fortran::lower::AbstractConverter {
// Termination of symbolically referenced execution units
//===--------------------------------------------------------------------===//

/// END of program
/// Exit of a routine
///
/// Generate the cleanup block before the program exits
void genExitRoutine() {

if (blockIsUnterminated())
builder->create<mlir::func::ReturnOp>(toLocation());
/// Generate the cleanup block before the routine exits
void genExitRoutine(bool earlyReturn, mlir::ValueRange retval = {}) {
if (blockIsUnterminated()) {
bridge.openAccCtx().finalizeAndKeep();
bridge.fctCtx().finalizeAndKeep();
builder->create<mlir::func::ReturnOp>(toLocation(), retval);
}
if (!earlyReturn) {
bridge.openAccCtx().pop();
bridge.fctCtx().pop();
}
}

/// END of procedure-like constructs
Expand Down Expand Up @@ -1684,9 +1690,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
resultRef = builder->createConvert(loc, resultRefType, resultRef);
return builder->create<fir::LoadOp>(loc, resultRef);
});
bridge.openAccCtx().finalizeAndPop();
bridge.fctCtx().finalizeAndPop();
builder->create<mlir::func::ReturnOp>(loc, resultVal);
genExitRoutine(false, resultVal);
}

/// Get the return value of a call to \p symbol, which is a subroutine entry
Expand All @@ -1712,13 +1716,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
} else if (Fortran::semantics::HasAlternateReturns(symbol)) {
mlir::Value retval = builder->create<fir::LoadOp>(
toLocation(), getAltReturnResult(symbol));
bridge.openAccCtx().finalizeAndPop();
bridge.fctCtx().finalizeAndPop();
builder->create<mlir::func::ReturnOp>(toLocation(), retval);
genExitRoutine(false, retval);
} else {
bridge.openAccCtx().finalizeAndPop();
bridge.fctCtx().finalizeAndPop();
genExitRoutine();
genExitRoutine(false);
}
}

Expand Down Expand Up @@ -5018,8 +5018,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
it->stmtCtx.finalizeAndKeep();
}
if (funit->isMainProgram()) {
bridge.fctCtx().finalizeAndKeep();
genExitRoutine();
genExitRoutine(true);
return;
}
mlir::Location loc = toLocation();
Expand Down Expand Up @@ -5478,9 +5477,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
void endNewFunction(Fortran::lower::pft::FunctionLikeUnit &funit) {
setCurrentPosition(Fortran::lower::pft::stmtSourceLoc(funit.endStmt));
if (funit.isMainProgram()) {
bridge.openAccCtx().finalizeAndPop();
bridge.fctCtx().finalizeAndPop();
genExitRoutine();
genExitRoutine(false);
} else {
genFIRProcedureExit(funit, funit.getSubprogramSymbol());
}
Expand Down
14 changes: 14 additions & 0 deletions flang/test/Lower/CUDA/cuda-return.cuf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s

! Check if finalization works with a return statement

program main
integer, device :: a(10)
return
end

! CHECK: func.func @_QQmain() attributes {fir.bindc_name = "main"} {
! CHECK: %[[DECL:.*]]:2 = hlfir.declare
! CHECK-NEXT: cuf.free %[[DECL]]#1 : !fir.ref<!fir.array<10xi32>>
! CHECK-NEXT: return
! CHECK-NEXT: }
Loading