diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp index 98a3aced3f528..d10169e236471 100644 --- a/flang/lib/Lower/OpenACC.cpp +++ b/flang/lib/Lower/OpenACC.cpp @@ -1622,16 +1622,18 @@ static bool isSupportedReductionType(mlir::Type ty) { return fir::isa_trivial(ty); } -static void -genReductions(const Fortran::parser::AccObjectListWithReduction &objectList, - Fortran::lower::AbstractConverter &converter, - Fortran::semantics::SemanticsContext &semanticsContext, - Fortran::lower::StatementContext &stmtCtx, - llvm::SmallVectorImpl &reductionOperands, - llvm::SmallVector &reductionRecipes, - llvm::ArrayRef async, - llvm::ArrayRef asyncDeviceTypes, - llvm::ArrayRef asyncOnlyDeviceTypes) { +static void genReductions( + const Fortran::parser::AccObjectListWithReduction &objectList, + Fortran::lower::AbstractConverter &converter, + Fortran::semantics::SemanticsContext &semanticsContext, + Fortran::lower::StatementContext &stmtCtx, + llvm::SmallVectorImpl &reductionOperands, + llvm::SmallVector &reductionRecipes, + llvm::ArrayRef async, + llvm::ArrayRef asyncDeviceTypes, + llvm::ArrayRef asyncOnlyDeviceTypes, + llvm::SmallVectorImpl> + *symbolPairs = nullptr) { fir::FirOpBuilder &builder = converter.getFirOpBuilder(); const auto &objects = std::get(objectList.t); const auto &op = std::get(objectList.t); @@ -1644,6 +1646,8 @@ genReductions(const Fortran::parser::AccObjectListWithReduction &objectList, Fortran::semantics::Symbol &symbol = getSymbolFromAccObject(accObject); Fortran::semantics::MaybeExpr designator = Fortran::common::visit( [&](auto &&s) { return ea.Analyze(s); }, accObject.u); + bool isWholeSymbol = + !designator || Fortran::evaluate::UnwrapWholeSymbolDataRef(*designator); fir::factory::AddrAndBoundsInfo info = Fortran::lower::gatherDataOperandAddrAndBounds< mlir::acc::DataBoundsOp, mlir::acc::DataBoundsType>( @@ -1680,6 +1684,11 @@ genReductions(const Fortran::parser::AccObjectListWithReduction &objectList, reductionRecipes.push_back(mlir::SymbolRefAttr::get( builder.getContext(), recipe.getSymName().str())); reductionOperands.push_back(op.getAccVar()); + // Track the symbol and its corresponding mlir::Value if requested so that + // accesses inside the compute/loop regions use the acc.reduction variable. + if (symbolPairs && isWholeSymbol) + symbolPairs->emplace_back(op.getAccVar(), + Fortran::semantics::SymbolRef(symbol)); } } @@ -2545,7 +2554,8 @@ static mlir::acc::LoopOp createLoopOp( &clause.u)) { genReductions(reductionClause->v, converter, semanticsContext, stmtCtx, reductionOperands, reductionRecipes, /*async=*/{}, - /*asyncDeviceTypes=*/{}, /*asyncOnlyDeviceTypes=*/{}); + /*asyncDeviceTypes=*/{}, /*asyncOnlyDeviceTypes=*/{}, + &dataOperandSymbolPairs); } else if (std::get_if(&clause.u)) { for (auto crtDeviceTypeAttr : crtDeviceTypes) seqDeviceTypes.push_back(crtDeviceTypeAttr); @@ -2995,7 +3005,8 @@ static Op createComputeOp( if (!combinedConstructs) { genReductions(reductionClause->v, converter, semanticsContext, stmtCtx, reductionOperands, reductionRecipes, async, - asyncDeviceTypes, asyncOnlyDeviceTypes); + asyncDeviceTypes, asyncOnlyDeviceTypes, + &dataOperandSymbolPairs); } else { auto crtDataStart = dataClauseOperands.size(); genDataOperandOperations( diff --git a/flang/test/Lower/OpenACC/acc-reduction-remapping.f90 b/flang/test/Lower/OpenACC/acc-reduction-remapping.f90 new file mode 100644 index 0000000000000..6ee365f278678 --- /dev/null +++ b/flang/test/Lower/OpenACC/acc-reduction-remapping.f90 @@ -0,0 +1,160 @@ +! Test remapping of variables appearing in OpenACC reduction clause +! to the related acc dialect data operation result. + +! This tests checks how the hlfir.declare is recreated and used inside +! the acc compute region. + +! RUN: bbc -fopenacc -emit-hlfir %s -o - | FileCheck %s + +subroutine scalar_combined(x, y) + real :: y, x(100) + !$acc parallel copyin(x) reduction(+:y) + do i=1,100 + y = y + x(i) + end do + !$acc end parallel +end subroutine + +subroutine scalar_split(x, y) + real :: y, x(100) + !$acc parallel copyin(x) copyout(y) + !$acc loop reduction(+:y) + do i=1,100 + y = y + x(i) + end do + !$acc end parallel +end subroutine + +subroutine array_combined(x, y, n) + integer(8) :: n + real :: y(n), x(100, n) + !$acc parallel copyin(x) reduction(+:y) + do j=1,n + do i=1,100 + y(j) = y(j) + x(i, j) + end do + end do + !$acc end parallel +end subroutine + +subroutine array_split(x, y, n) + integer(8) :: n + real :: y(n), x(100, n) + !$acc parallel copyin(x) copyout(y) + !$acc loop reduction(+:y) + do j=1,n + do i=1,100 + y(j) = y(j) + x(i, j) + end do + end do + !$acc end parallel +end subroutine + +! CHECK-LABEL: func.func @_QPscalar_combined( +! CHECK: %[[DUMMY_SCOPE_0:.*]] = fir.dummy_scope : !fir.dscope +! CHECK: %[[DECLARE_Y:.*]]:2 = hlfir.declare %{{.*}} dummy_scope %[[DUMMY_SCOPE_0]] arg 2 {uniq_name = "_QFscalar_combinedEy"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) +! CHECK: %[[REDUCTION_Y:.*]] = acc.reduction varPtr(%[[DECLARE_Y]]#0 : !fir.ref) -> !fir.ref {name = "y"} +! CHECK: acc.parallel {{.*}} reduction(@reduction_add_ref_f32 -> %[[REDUCTION_Y]] : !fir.ref) { +! CHECK: %[[DUMMY_SCOPE_1:.*]] = fir.dummy_scope : !fir.dscope +! CHECK: %[[DECLARE_RED_PAR:.*]]:2 = hlfir.declare %[[REDUCTION_Y]] dummy_scope %[[DUMMY_SCOPE_1]] arg 2 {uniq_name = "_QFscalar_combinedEy"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) +! CHECK: %[[PRIVATE_I:.*]] = acc.private varPtr({{.*}}) -> !fir.ref {implicit = true, name = "i"} +! CHECK: acc.loop private(@privatization_ref_i32 -> %[[PRIVATE_I]] : !fir.ref) {{.*}} { +! CHECK: %[[DECLARE_I:.*]]:2 = hlfir.declare %[[PRIVATE_I]] {uniq_name = "_QFscalar_combinedEi"} : (!fir.ref) -> (!fir.ref, !fir.ref) +! CHECK: fir.store %[[VAL_0:.*]] to %[[DECLARE_I]]#0 : !fir.ref +! CHECK: %[[LOAD_RED:.*]] = fir.load %[[DECLARE_RED_PAR]]#0 : !fir.ref +! CHECK: {{.*}} = hlfir.designate {{.*}} : (!fir.ref>, i64) -> !fir.ref +! CHECK: {{.*}} = fir.load {{.*}} : !fir.ref +! CHECK: %[[ADDF:.*]] = arith.addf %[[LOAD_RED]], {{.*}} {{.*}}: f32 +! CHECK: hlfir.assign %[[ADDF]] to %[[DECLARE_RED_PAR]]#0 : f32, !fir.ref +! CHECK: acc.yield +! CHECK: } +! CHECK: acc.yield +! CHECK: } +! CHECK: return +! CHECK: } +! +! +! CHECK-LABEL: func.func @_QPscalar_split( +! CHECK: %[[DUMMY_SCOPE_0:.*]] = fir.dummy_scope : !fir.dscope +! CHECK: %[[DECLARE_Y:.*]]:2 = hlfir.declare %{{.*}} dummy_scope %[[DUMMY_SCOPE_0]] arg 2 {uniq_name = "_QFscalar_splitEy"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) +! CHECK: %[[CREATE_Y:.*]] = acc.create varPtr(%[[DECLARE_Y]]#0 : !fir.ref) -> !fir.ref {dataClause = #acc, name = "y"} +! CHECK: acc.parallel dataOperands({{.*}}%[[CREATE_Y]] : {{.*}}) { +! CHECK: %[[DUMMY_SCOPE_1:.*]] = fir.dummy_scope : !fir.dscope +! CHECK: %[[DECLARE_Y_PAR:.*]]:2 = hlfir.declare %[[CREATE_Y]] dummy_scope %[[DUMMY_SCOPE_1]] arg 2 {uniq_name = "_QFscalar_splitEy"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) +! CHECK: %[[REDUCTION_Y:.*]] = acc.reduction varPtr(%[[DECLARE_Y_PAR]]#0 : !fir.ref) -> !fir.ref {name = "y"} +! CHECK: %[[PRIVATE_I:.*]] = acc.private varPtr({{.*}}) -> !fir.ref {implicit = true, name = "i"} +! CHECK: acc.loop private(@privatization_ref_i32 -> %[[PRIVATE_I]] : !fir.ref) reduction(@reduction_add_ref_f32 -> %[[REDUCTION_Y]] : !fir.ref) {{.*}} { +! CHECK: %[[DUMMY_SCOPE_2:.*]] = fir.dummy_scope : !fir.dscope +! CHECK: %[[DECLARE_RED:.*]]:2 = hlfir.declare %[[REDUCTION_Y]] dummy_scope %[[DUMMY_SCOPE_2]] arg 2 {uniq_name = "_QFscalar_splitEy"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) +! CHECK: %[[LOAD_RED:.*]] = fir.load %[[DECLARE_RED]]#0 : !fir.ref +! CHECK: %[[ADDF:.*]] = arith.addf %[[LOAD_RED]], {{.*}} {{.*}}: f32 +! CHECK: hlfir.assign %[[ADDF]] to %[[DECLARE_RED]]#0 : f32, !fir.ref +! CHECK: acc.yield +! CHECK: } +! CHECK: acc.yield +! CHECK: } +! CHECK: return +! CHECK: } + + +! CHECK-LABEL: func.func @_QParray_combined( +! CHECK: %[[DUMMY_SCOPE_0:.*]] = fir.dummy_scope : !fir.dscope +! CHECK: %[[DECLARE_N:.*]]:2 = hlfir.declare %{{.*}} dummy_scope %[[DUMMY_SCOPE_0]] arg 3 {uniq_name = "_QFarray_combinedEn"} : (!fir.ref, !fir.dscope) -> (!fir.ref, !fir.ref) +! CHECK: %[[DECLARE_Y:.*]]:2 = hlfir.declare %{{.*}}({{.*}}) dummy_scope %[[DUMMY_SCOPE_0]] arg 2 {uniq_name = "_QFarray_combinedEy"} : (!fir.ref>, {{.*}}, !fir.dscope) -> (!fir.box>, !fir.ref>) +! CHECK: %[[REDUCTION_Y:.*]] = acc.reduction var(%[[DECLARE_Y]]#0 : !fir.box>) -> !fir.box> {name = "y"} +! CHECK: acc.parallel dataOperands({{.*}}) reduction(@reduction_add_box_Uxf32 -> %[[REDUCTION_Y]] : !fir.box>) { +! CHECK: %[[DUMMY_SCOPE_1:.*]] = fir.dummy_scope : !fir.dscope +! CHECK: %[[BOX_ADDR_RED:.*]] = fir.box_addr %[[REDUCTION_Y]] : (!fir.box>) -> !fir.ref> +! CHECK: %[[DECLARE_Y_PAR:.*]]:2 = hlfir.declare %[[BOX_ADDR_RED]]({{.*}}) dummy_scope %[[DUMMY_SCOPE_1]] arg 2 {uniq_name = "_QFarray_combinedEy"} : (!fir.ref>, {{.*}}, !fir.dscope) -> (!fir.box>, !fir.ref>) +! CHECK: %[[PRIVATE_J:.*]] = acc.private varPtr({{.*}}) -> !fir.ref {implicit = true, name = "j"} +! CHECK: acc.loop private(@privatization_ref_i32 -> %[[PRIVATE_J]] : !fir.ref) {{.*}} { +! CHECK: %[[PRIVATE_I:.*]] = acc.private varPtr({{.*}}) -> !fir.ref {implicit = true, name = "i"} +! CHECK: acc.loop private(@privatization_ref_i32 -> %[[PRIVATE_I]] : !fir.ref) {{.*}} { +! CHECK: %[[DESIGNATE_RED:.*]] = hlfir.designate %[[DECLARE_Y_PAR]]#0 ({{.*}}) : (!fir.box>, i64) -> !fir.ref +! CHECK: %[[LOAD_OLD:.*]] = fir.load %[[DESIGNATE_RED]] : !fir.ref +! CHECK: {{.*}} = hlfir.designate {{.*}} : (!fir.box>, i64, i64) -> !fir.ref +! CHECK: {{.*}} = fir.load {{.*}} : !fir.ref +! CHECK: %[[ADDF:.*]] = arith.addf %[[LOAD_OLD]], {{.*}} {{.*}}: f32 +! CHECK: %[[DESIGNATE_RED2:.*]] = hlfir.designate %[[DECLARE_Y_PAR]]#0 ({{.*}}) : (!fir.box>, i64) -> !fir.ref +! CHECK: hlfir.assign %[[ADDF]] to %[[DESIGNATE_RED2]] : f32, !fir.ref +! CHECK: acc.yield +! CHECK: } +! CHECK: acc.yield +! CHECK: } +! CHECK: acc.yield +! CHECK: } +! CHECK: return +! CHECK: } + + +! CHECK-LABEL: func.func @_QParray_split( +! CHECK: %[[DUMMY_SCOPE_0:.*]] = fir.dummy_scope : !fir.dscope +! CHECK: %[[DECLARE_Y:.*]]:2 = hlfir.declare %{{.*}}({{.*}}) dummy_scope %[[DUMMY_SCOPE_0]] arg 2 {uniq_name = "_QFarray_splitEy"} : (!fir.ref>, {{.*}}, !fir.dscope) -> (!fir.box>, !fir.ref>) +! CHECK: %[[CREATE_Y:.*]] = acc.create var(%[[DECLARE_Y]]#0 : !fir.box>) -> !fir.box> {dataClause = #acc, name = "y"} +! CHECK: acc.parallel dataOperands({{.*}}%[[CREATE_Y]] : {{.*}}) { +! CHECK: %[[DUMMY_SCOPE_1:.*]] = fir.dummy_scope : !fir.dscope +! CHECK: %[[BOX_ADDR_Y:.*]] = fir.box_addr %[[CREATE_Y]] : (!fir.box>) -> !fir.ref> +! CHECK: %[[DECLARE_Y_PAR:.*]]:2 = hlfir.declare %[[BOX_ADDR_Y]]({{.*}}) dummy_scope %[[DUMMY_SCOPE_1]] arg 2 {uniq_name = "_QFarray_splitEy"} : (!fir.ref>, {{.*}}, !fir.dscope) -> (!fir.box>, !fir.ref>) +! CHECK: %[[REDUCTION_Y:.*]] = acc.reduction var(%[[DECLARE_Y_PAR]]#0 : !fir.box>) -> !fir.box> {name = "y"} +! CHECK: %[[PRIVATE_J:.*]] = acc.private varPtr({{.*}}) -> !fir.ref {implicit = true, name = "j"} +! CHECK: acc.loop private(@privatization_ref_i32 -> %[[PRIVATE_J]] : !fir.ref) reduction(@reduction_add_box_Uxf32 -> %[[REDUCTION_Y]] : !fir.box>) {{.*}} { +! CHECK: %[[BOX_ADDR_RED:.*]] = fir.box_addr %[[REDUCTION_Y]] : (!fir.box>) -> !fir.ref> +! CHECK: %[[DUMMY_SCOPE_2:.*]] = fir.dummy_scope : !fir.dscope +! CHECK: %[[DECLARE_Y_LOOP_PAR:.*]]:2 = hlfir.declare %[[BOX_ADDR_RED]]({{.*}}) dummy_scope %[[DUMMY_SCOPE_2]] arg 2 {uniq_name = "_QFarray_splitEy"} : (!fir.ref>, {{.*}}, !fir.dscope) -> (!fir.box>, !fir.ref>) +! CHECK: %[[PRIVATE_I:.*]] = acc.private varPtr({{.*}}) -> !fir.ref {implicit = true, name = "i"} +! CHECK: acc.loop private(@privatization_ref_i32 -> %[[PRIVATE_I]] : !fir.ref) {{.*}} { +! CHECK: %[[DESIGNATE_RED:.*]] = hlfir.designate %[[DECLARE_Y_LOOP_PAR]]#0 ({{.*}}) : (!fir.box>, i64) -> !fir.ref +! CHECK: %[[LOAD_OLD:.*]] = fir.load %[[DESIGNATE_RED]] : !fir.ref +! CHECK: {{.*}} = hlfir.designate {{.*}} : (!fir.box>, i64, i64) -> !fir.ref +! CHECK: {{.*}} = fir.load {{.*}} : !fir.ref +! CHECK: %[[ADDF:.*]] = arith.addf %[[LOAD_OLD]], {{.*}} {{.*}}: f32 +! CHECK: %[[DESIGNATE_RED2:.*]] = hlfir.designate %[[DECLARE_Y_LOOP_PAR]]#0 ({{.*}}) : (!fir.box>, i64) -> !fir.ref +! CHECK: hlfir.assign %[[ADDF]] to %[[DESIGNATE_RED2]] : f32, !fir.ref +! CHECK: acc.yield +! CHECK: } +! CHECK: acc.yield +! CHECK: } +! CHECK: acc.yield +! CHECK: } +! CHECK: return +! CHECK: }