Skip to content

Commit 675acb2

Browse files
committed
Check two operands are different. Refactor code. Add motivating test and for assumed shape, pointer and allocatable
1 parent c385240 commit 675acb2

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

flang/lib/Lower/OpenACC.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ genDataOperandOperations(const Fortran::parser::AccObjectList &objectList,
709709
bool setDeclareAttr = false) {
710710
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
711711
Fortran::evaluate::ExpressionAnalyzer ea{semanticsContext};
712+
const bool unwrapBoxAddr = true;
712713
for (const auto &accObject : objectList.v) {
713714
llvm::SmallVector<mlir::Value> bounds;
714715
std::stringstream asFortran;
@@ -736,19 +737,23 @@ genDataOperandOperations(const Fortran::parser::AccObjectList &objectList,
736737
Op op = createDataEntryOp<Op>(
737738
builder, operandLocation, baseAddr, asFortran, bounds, structured,
738739
implicit, dataClause, baseAddr.getType(), async, asyncDeviceTypes,
739-
asyncOnlyDeviceTypes, /*unwrapBoxAddr=*/true, info.isPresent);
740+
asyncOnlyDeviceTypes, unwrapBoxAddr, info.isPresent);
740741
dataOperands.push_back(op.getAccVar());
742+
741743
// For UseDeviceOp, if operand is one of a pair resulting from a
742744
// declare operation, create a UseDeviceOp for the other operand as well.
743745
if constexpr (std::is_same_v<Op, mlir::acc::UseDeviceOp>) {
744-
if (mlir::isa<hlfir::DeclareOp>(baseAddr.getDefiningOp())) {
745-
Op op = createDataEntryOp<Op>(
746-
builder, operandLocation, baseAddr.getDefiningOp()->getResult(1),
746+
if (auto declareOp = mlir::dyn_cast<hlfir::DeclareOp>(baseAddr.getDefiningOp())) {
747+
mlir::Value otherAddr = declareOp.getResult(1);
748+
if (baseAddr != otherAddr) {
749+
Op op = createDataEntryOp<Op>(
750+
builder, operandLocation, otherAddr,
747751
asFortran, bounds, structured, implicit, dataClause,
748-
baseAddr.getDefiningOp()->getResult(1).getType(), async,
749-
asyncDeviceTypes, asyncOnlyDeviceTypes, /*unwrapBoxAddr=*/true,
752+
otherAddr.getType(), async,
753+
asyncDeviceTypes, asyncOnlyDeviceTypes, unwrapBoxAddr,
750754
info.isPresent);
751-
dataOperands.push_back(op.getAccVar());
755+
dataOperands.push_back(op.getAccVar());
756+
}
752757
}
753758
}
754759
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
! This test checks whether the OpenACC use_device clause is applied on both results of hlfir.declare.
2+
3+
! RUN: bbc -fopenacc -emit-hlfir %s -o - | FileCheck %s
4+
5+
! Test for automatic variable appearing in use_device clause.
6+
subroutine test()
7+
integer :: N = 100
8+
real*8 :: b(-1:N)
9+
! CHECK: %[[A0:.*]] = fir.alloca !fir.array<?xf64>, %{{.*}} {bindc_name = "b", uniq_name = "_QFtestEb"}
10+
! CHECK: %[[A1:.*]] = fir.shape_shift {{.*}} : (index, index) -> !fir.shapeshift<1>
11+
! CHECK: %[[A:.*]]:2 = hlfir.declare %[[A0]](%[[A1]]) {uniq_name = "_QFtestEb"} : (!fir.ref<!fir.array<?xf64>>, !fir.shapeshift<1>) -> (!fir.box<!fir.array<?xf64>>, !fir.ref<!fir.array<?xf64>>)
12+
13+
!$acc data copy(b)
14+
! CHECK: %[[B:.*]] = acc.copyin var(%[[A]]#0 : !fir.box<!fir.array<?xf64>>) -> !fir.box<!fir.array<?xf64>> {dataClause = #acc<data_clause acc_copy>, name = "b"}
15+
! CHECK: acc.data dataOperands(%[[B]] : !fir.box<!fir.array<?xf64>>) {
16+
17+
!$acc host_data use_device(b)
18+
call vadd(b)
19+
!$acc end host_data
20+
! CHECK: %[[C:.*]] = acc.use_device var(%[[A]]#0 : !fir.box<!fir.array<?xf64>>) -> !fir.box<!fir.array<?xf64>> {name = "b"}
21+
! CHECK: %[[D:.*]] = acc.use_device varPtr(%[[A]]#1 : !fir.ref<!fir.array<?xf64>>) -> !fir.ref<!fir.array<?xf64>> {name = "b"}
22+
! CHECK: acc.host_data dataOperands(%[[C]], %[[D]] : !fir.box<!fir.array<?xf64>>, !fir.ref<!fir.array<?xf64>>) {
23+
! CHECK: fir.call @_QPvadd(%[[A]]#1) fastmath<contract> : (!fir.ref<!fir.array<?xf64>>) -> ()
24+
!$acc end data
25+
! CHECK: acc.copyout accVar(%[[B]] : !fir.box<!fir.array<?xf64>>) to var(%[[A]]#0 : !fir.box<!fir.array<?xf64>>) {dataClause = #acc<data_clause acc_copy>, name = "b"}
26+
end
27+
28+
! Test for allocatable, pointer and assumed-shape variables appearing in use_device clause.
29+
subroutine test2(a, b, c)
30+
integer :: N = 100
31+
real*8, allocatable :: a(:)
32+
real*8, target, allocatable :: d(:)
33+
real*8 :: b(:)
34+
real*8, pointer :: c(:)
35+
call allocate(a(N))
36+
call allocate(d(N))
37+
c => d
38+
! CHECK: %[[DS:.*]] = fir.dummy_scope : !fir.dscope
39+
! CHECK: %[[E:.*]]:2 = hlfir.declare %arg0 dummy_scope %[[DS]] {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFtest2Ea"} : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>, !fir.dscope) -> (!fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>)
40+
! CHECK: %[[F:.*]]:2 = hlfir.declare %arg1 dummy_scope %[[DS]] {uniq_name = "_QFtest2Eb"} : (!fir.box<!fir.array<?xf64>>, !fir.dscope) -> (!fir.box<!fir.array<?xf64>>, !fir.box<!fir.array<?xf64>>)
41+
! CHECK: %[[G:.*]]:2 = hlfir.declare %arg2 dummy_scope %[[DS]] {fortran_attrs = #fir.var_attrs<pointer>, uniq_name = "_QFtest2Ec"} : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>, !fir.dscope) -> (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>, !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>)
42+
43+
!$acc data copy(a,b,c,d)
44+
!$acc host_data use_device(a,b,c)
45+
call vadd2(a,b,c)
46+
!$acc end host_data
47+
48+
! CHECK: %[[H:.*]] = acc.use_device varPtr(%[[E]]#0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>> {name = "a"}
49+
! CHECK: %[[I:.*]] = acc.use_device varPtr(%[[E]]#1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>) -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>> {name = "a"}
50+
! CHECK: %[[J:.*]] = acc.use_device var(%[[F]]#0 : !fir.box<!fir.array<?xf64>>) -> !fir.box<!fir.array<?xf64>> {name = "b"}
51+
! CHECK: %[[K:.*]] = acc.use_device var(%[[F]]#1 : !fir.box<!fir.array<?xf64>>) -> !fir.box<!fir.array<?xf64>> {name = "b"}
52+
! CHECK: %[[L:.*]] = acc.use_device varPtr(%[[G]]#0 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>) -> !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>> {name = "c"}
53+
! CHECK: %[[M:.*]] = acc.use_device varPtr(%[[G]]#1 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>) -> !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>> {name = "c"}
54+
! CHECK: acc.host_data dataOperands(%[[H]], %[[I]], %[[J]], %[[K]], %[[L]], %[[M]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>, !fir.ref<!fir.box<!fir.heap<!fir.array<?xf64>>>>, !fir.box<!fir.array<?xf64>>, !fir.box<!fir.array<?xf64>>, !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>, !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf64>>>>) {
55+
56+
57+
58+
59+
!$acc end data
60+
61+
end

0 commit comments

Comments
 (0)