[flang-commits] [flang] 1c39076 - [flang][acc] Name deviceptr objects on the exit of a local declare (#235089)
via flang-commits
flang-commits at lists.llvm.org
Sat Oct 24 12:57:56 PDT 2026
Author: Zhen Wang
Date: 2026-10-24T12:57:50-07:00
New Revision: 1c39076b571a0427d1501959815fcb52168e2ce8
URL: https://github.com/llvm/llvm-project/commit/1c39076b571a0427d1501959815fcb52168e2ce8
DIFF: https://github.com/llvm/llvm-project/commit/1c39076b571a0427d1501959815fcb52168e2ce8.diff
LOG: [flang][acc] Name deviceptr objects on the exit of a local declare (#235089)
A structured `acc declare deviceptr(a)` inside a procedure lowers to an
`acc.declare_enter` whose data operands include the deviceptr op, but
the matching `acc.declare_exit` carried no operands for it. The runtime
therefore saw the region opened at the enter and never closed, and the
entry registered for `a` survived the end of the procedure. With a dummy
argument this leaks one present table entry per call.
Append the deviceptr entry ops to the `acc.declare_exit` operands in
`genDeclareInFunction`, the same way the present clause is handled. No
copyout or delete is generated for deviceptr. The runtime conversion
then emits the data end call for the exit.
The lowering test checks that the exit names the deviceptr object and
that no delete is produced.
Added:
Modified:
flang/lib/Lower/OpenACC.cpp
flang/test/Lower/OpenACC/acc-declare.f90
Removed:
################################################################################
diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index 116d6e24df0f92..473829117b5d6f 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -4487,7 +4487,7 @@ genDeclareInFunction(Fortran::lower::AbstractConverter &converter,
const Fortran::parser::AccClauseList &accClauseList) {
llvm::SmallVector<mlir::Value> dataClauseOperands, copyEntryOperands,
copyinEntryOperands, createEntryOperands, copyoutEntryOperands,
- presentEntryOperands, deviceResidentEntryOperands;
+ presentEntryOperands, deviceResidentEntryOperands, devicePtrEntryOperands;
Fortran::lower::StatementContext stmtCtx;
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
@@ -4551,11 +4551,16 @@ genDeclareInFunction(Fortran::lower::AbstractConverter &converter,
} else if (const auto *devicePtrClause =
std::get_if<Fortran::parser::AccClause::Deviceptr>(
&clause.u)) {
+ auto crtDataStart = dataClauseOperands.size();
genDeclareDataOperandOperations<mlir::acc::DevicePtrOp,
mlir::acc::DevicePtrOp>(
devicePtrClause->v, converter, semanticsContext, stmtCtx,
dataClauseOperands, mlir::acc::DataClause::acc_deviceptr,
/*structured=*/true, /*implicit=*/false);
+ // No exit action, but the exit still names the object so the runtime
+ // closes the region it opened at the enter.
+ devicePtrEntryOperands.append(dataClauseOperands.begin() + crtDataStart,
+ dataClauseOperands.end());
} else if (const auto *linkClause =
std::get_if<Fortran::parser::AccClause::Link>(&clause.u)) {
genDeclareDataOperandOperations<mlir::acc::DeclareLinkOp,
@@ -4606,7 +4611,8 @@ genDeclareInFunction(Fortran::lower::AbstractConverter &converter,
openAccCtx.attachCleanup([&builder, loc, createEntryOperands,
copyEntryOperands, copyinEntryOperands,
copyoutEntryOperands, presentEntryOperands,
- deviceResidentEntryOperands, declareToken]() {
+ deviceResidentEntryOperands, devicePtrEntryOperands,
+ declareToken]() {
llvm::SmallVector<mlir::Value> operands;
operands.append(createEntryOperands);
operands.append(deviceResidentEntryOperands);
@@ -4614,6 +4620,7 @@ genDeclareInFunction(Fortran::lower::AbstractConverter &converter,
operands.append(copyinEntryOperands);
operands.append(copyoutEntryOperands);
operands.append(presentEntryOperands);
+ operands.append(devicePtrEntryOperands);
mlir::func::FuncOp funcOp = builder.getFunction();
auto ops = funcOp.getOps<mlir::acc::DeclareExitOp>();
diff --git a/flang/test/Lower/OpenACC/acc-declare.f90 b/flang/test/Lower/OpenACC/acc-declare.f90
index 4aa7203716d6ea..dd0a67347984b5 100644
--- a/flang/test/Lower/OpenACC/acc-declare.f90
+++ b/flang/test/Lower/OpenACC/acc-declare.f90
@@ -134,8 +134,11 @@ subroutine acc_declare_deviceptr(a)
! CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<100xi32>> {fir.bindc_name = "a"}) {
! CHECK: %[[DECL:.*]]:2 = hlfir.declare %[[ARG0]](%{{.*}}) dummy_scope %{{[0-9]+}} arg {{[0-9]+}} uniq_name("_QMacc_declareFacc_declare_deviceptrEa") {acc.declare = #acc.declare<dataClause = acc_deviceptr>} : (!fir.ref<!fir.array<100xi32>>, !fir.shape<1>, !fir.dscope) -> (!fir.ref<!fir.array<100xi32>>, !fir.ref<!fir.array<100xi32>>)
! CHECK: %[[DEVICEPTR:.*]] = acc.deviceptr varPtr(%[[DECL]]#0 : !fir.ref<!fir.array<100xi32>>) name("a") -> !fir.ref<!fir.array<100xi32>>
-! CHECK: acc.declare_enter dataOperands(%[[DEVICEPTR]] : !fir.ref<!fir.array<100xi32>>)
+! CHECK: %[[TOKEN:.*]] = acc.declare_enter dataOperands(%[[DEVICEPTR]] : !fir.ref<!fir.array<100xi32>>)
! CHECK: fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} {
+! CHECK: acc.declare_exit token(%[[TOKEN]]) dataOperands(%[[DEVICEPTR]] : !fir.ref<!fir.array<100xi32>>)
+! CHECK-NOT: acc.delete
+! CHECK: return
subroutine acc_declare_link(a)
integer :: a(100), i
@@ -207,7 +210,8 @@ subroutine acc_declare_deviceptr2()
! CHECK: %[[ALLOCA:.*]] = fir.alloca !fir.array<100xf32> <{bindc_name = "dataparam", uniq_name = "_QMacc_declareFacc_declare_deviceptr2Edataparam"}>
! CHECK: %[[DECL:.*]]:2 = hlfir.declare %[[ALLOCA]](%{{.*}}) uniq_name("_QMacc_declareFacc_declare_deviceptr2Edataparam") {acc.declare = #acc.declare<dataClause = acc_deviceptr>} : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<100xf32>>, !fir.ref<!fir.array<100xf32>>)
! CHECK: %[[DEVICEPTR:.*]] = acc.deviceptr varPtr(%[[DECL]]#0 : !fir.ref<!fir.array<100xf32>>) name("dataparam") -> !fir.ref<!fir.array<100xf32>>
-! CHECK: acc.declare_enter dataOperands(%[[DEVICEPTR]] : !fir.ref<!fir.array<100xf32>>)
+! CHECK: %[[TOKEN:.*]] = acc.declare_enter dataOperands(%[[DEVICEPTR]] : !fir.ref<!fir.array<100xf32>>)
+! CHECK: acc.declare_exit token(%[[TOKEN]]) dataOperands(%[[DEVICEPTR]] : !fir.ref<!fir.array<100xf32>>)
function acc_declare_in_func()
real :: a(1024)
More information about the flang-commits
mailing list