Skip to content

Commit 5257fa1

Browse files
authored
[flang][openacc] Attach post allocate action on the correct operation (#106805)
In some cases (when using stat), the action was attached to the invisible fir.result op. Apply same fix as in #89662.
1 parent 6f81c87 commit 5257fa1

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

flang/lib/Lower/OpenACC.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4293,23 +4293,30 @@ void Fortran::lower::attachDeclarePostAllocAction(
42934293
const Fortran::semantics::Symbol &sym) {
42944294
std::stringstream fctName;
42954295
fctName << converter.mangleName(sym) << declarePostAllocSuffix.str();
4296-
mlir::Operation &op = builder.getInsertionBlock()->back();
4296+
mlir::Operation *op = &builder.getInsertionBlock()->back();
4297+
4298+
if (auto resOp = mlir::dyn_cast<fir::ResultOp>(*op)) {
4299+
assert(resOp.getOperands().size() == 0 &&
4300+
"expect only fir.result op with no operand");
4301+
op = op->getPrevNode();
4302+
}
4303+
assert(op && "expect operation to attach the post allocation action");
42974304

4298-
if (op.hasAttr(mlir::acc::getDeclareActionAttrName())) {
4299-
auto attr = op.getAttrOfType<mlir::acc::DeclareActionAttr>(
4305+
if (op->hasAttr(mlir::acc::getDeclareActionAttrName())) {
4306+
auto attr = op->getAttrOfType<mlir::acc::DeclareActionAttr>(
43004307
mlir::acc::getDeclareActionAttrName());
4301-
op.setAttr(mlir::acc::getDeclareActionAttrName(),
4302-
mlir::acc::DeclareActionAttr::get(
4303-
builder.getContext(), attr.getPreAlloc(),
4304-
/*postAlloc=*/builder.getSymbolRefAttr(fctName.str()),
4305-
attr.getPreDealloc(), attr.getPostDealloc()));
4308+
op->setAttr(mlir::acc::getDeclareActionAttrName(),
4309+
mlir::acc::DeclareActionAttr::get(
4310+
builder.getContext(), attr.getPreAlloc(),
4311+
/*postAlloc=*/builder.getSymbolRefAttr(fctName.str()),
4312+
attr.getPreDealloc(), attr.getPostDealloc()));
43064313
} else {
4307-
op.setAttr(mlir::acc::getDeclareActionAttrName(),
4308-
mlir::acc::DeclareActionAttr::get(
4309-
builder.getContext(),
4310-
/*preAlloc=*/{},
4311-
/*postAlloc=*/builder.getSymbolRefAttr(fctName.str()),
4312-
/*preDealloc=*/{}, /*postDealloc=*/{}));
4314+
op->setAttr(mlir::acc::getDeclareActionAttrName(),
4315+
mlir::acc::DeclareActionAttr::get(
4316+
builder.getContext(),
4317+
/*preAlloc=*/{},
4318+
/*postAlloc=*/builder.getSymbolRefAttr(fctName.str()),
4319+
/*preDealloc=*/{}, /*postDealloc=*/{}));
43134320
}
43144321
}
43154322

flang/test/Lower/OpenACC/acc-declare.f90

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,20 @@ module acc_declare_allocatable_test3
455455

456456
! CHECK-LABEL: acc.global_ctor @_QMacc_declare_allocatable_test3Edata1_acc_ctor
457457
! CHECK-LABEL: acc.global_ctor @_QMacc_declare_allocatable_test3Edata2_acc_ctor
458+
459+
module acc_declare_post_action_stat
460+
real, dimension(:), allocatable :: x, y
461+
!$acc declare create(x,y)
462+
463+
contains
464+
465+
subroutine init()
466+
integer :: stat
467+
allocate(x(10), y(10), stat=stat)
468+
end subroutine
469+
end module
470+
471+
! CHECK-LABEL: func.func @_QMacc_declare_post_action_statPinit()
472+
! CHECK: fir.call @_FortranAAllocatableAllocate({{.*}}) fastmath<contract> {acc.declare_action = #acc.declare_action<postAlloc = @_QMacc_declare_post_action_statEx_acc_declare_update_desc_post_alloc>} : (!fir.ref<!fir.box<none>>, i1, !fir.box<none>, !fir.ref<i8>, i32) -> i32
473+
! CHECK: fir.if
474+
! CHECK: fir.call @_FortranAAllocatableAllocate({{.*}}) fastmath<contract> {acc.declare_action = #acc.declare_action<postAlloc = @_QMacc_declare_post_action_statEy_acc_declare_update_desc_post_alloc>} : (!fir.ref<!fir.box<none>>, i1, !fir.box<none>, !fir.ref<i8>, i32) -> i32

0 commit comments

Comments
 (0)