diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp --- a/flang/lib/Lower/OpenACC.cpp +++ b/flang/lib/Lower/OpenACC.cpp @@ -661,7 +661,10 @@ if (!fir::isa_trivial(fir::unwrapRefType(baseAddr.getType()))) TODO(operandLocation, "reduction with unsupported type"); - mlir::Type ty = fir::unwrapRefType(baseAddr.getType()); + auto op = createDataEntryOp( + builder, operandLocation, baseAddr, asFortran, bounds, + /*structured=*/true, mlir::acc::DataClause::acc_reduction); + mlir::Type ty = fir::unwrapRefType(op.getAccPtr().getType()); std::string recipeName = fir::getTypeAsString( ty, converter.getKindMap(), ("reduction_" + stringifyReductionOperator(mlirOp)).str()); @@ -670,7 +673,7 @@ operandLocation, ty, mlirOp); reductionRecipes.push_back(mlir::SymbolRefAttr::get( builder.getContext(), recipe.getSymName().str())); - reductionOperands.push_back(baseAddr); + reductionOperands.push_back(op.getAccPtr()); } } diff --git a/flang/test/Lower/OpenACC/acc-reduction.f90 b/flang/test/Lower/OpenACC/acc-reduction.f90 --- a/flang/test/Lower/OpenACC/acc-reduction.f90 +++ b/flang/test/Lower/OpenACC/acc-reduction.f90 @@ -98,7 +98,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_add_int( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_add_i32 -> %[[B]] : !fir.ref) +! CHECK: %[[RED_B:.*]] = acc.reduction varPtr(%[[B]] : !fir.ref) -> !fir.ref {name = "b"} +! CHECK: acc.loop reduction(@reduction_add_i32 -> %[[RED_B]] : !fir.ref) subroutine acc_reduction_add_float(a, b) real :: a(100), b @@ -112,7 +113,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_add_float( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_add_f32 -> %[[B]] : !fir.ref) +! CHECK: %[[RED_B:.*]] = acc.reduction varPtr(%[[B]] : !fir.ref) -> !fir.ref {name = "b"} +! CHECK: acc.loop reduction(@reduction_add_f32 -> %[[RED_B]] : !fir.ref) subroutine acc_reduction_mul_int(a, b) integer :: a(100) @@ -126,7 +128,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_mul_int( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_mul_i32 -> %[[B]] : !fir.ref) +! CHECK: %[[RED_B:.*]] = acc.reduction varPtr(%[[B]] : !fir.ref) -> !fir.ref {name = "b"} +! CHECK: acc.loop reduction(@reduction_mul_i32 -> %[[RED_B]] : !fir.ref) subroutine acc_reduction_mul_float(a, b) real :: a(100), b @@ -140,7 +143,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_mul_float( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_mul_f32 -> %[[B]] : !fir.ref) +! CHECK: %[[RED_B:.*]] = acc.reduction varPtr(%[[B]] : !fir.ref) -> !fir.ref {name = "b"} +! CHECK: acc.loop reduction(@reduction_mul_f32 -> %[[RED_B]] : !fir.ref) subroutine acc_reduction_min_int(a, b) @@ -155,7 +159,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_min_int( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_min_i32 -> %[[B]] : !fir.ref) +! CHECK: %[[RED_B:.*]] = acc.reduction varPtr(%[[B]] : !fir.ref) -> !fir.ref {name = "b"} +! CHECK: acc.loop reduction(@reduction_min_i32 -> %[[RED_B]] : !fir.ref) subroutine acc_reduction_min_float(a, b) real :: a(100), b @@ -169,7 +174,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_min_float( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_min_f32 -> %[[B]] : !fir.ref) +! CHECK: %[[RED_B:.*]] = acc.reduction varPtr(%[[B]] : !fir.ref) -> !fir.ref {name = "b"} +! CHECK: acc.loop reduction(@reduction_min_f32 -> %[[RED_B]] : !fir.ref) subroutine acc_reduction_max_int(a, b) integer :: a(100) @@ -183,7 +189,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_max_int( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_max_i32 -> %[[B]] : !fir.ref) +! CHECL: %[[RED_B:.*]] = acc.reduction varPtr(%[[B]] : !fir.ref) -> !fir.ref {name = "b"} +! CHECK: acc.loop reduction(@reduction_max_i32 -> %[[RED_B]] : !fir.ref) subroutine acc_reduction_max_float(a, b) real :: a(100), b @@ -197,4 +204,5 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_max_float( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_max_f32 -> %[[B]] : !fir.ref) +! CHECK: %[[RED_B:.*]] = acc.reduction varPtr(%[[B]] : !fir.ref) -> !fir.ref {name = "b"} +! CHECK: acc.loop reduction(@reduction_max_f32 -> %[[RED_B]] : !fir.ref) diff --git a/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td b/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td --- a/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td +++ b/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td @@ -94,6 +94,7 @@ def OpenACC_UpdateSelf : I64EnumAttrCase<"acc_update_self", 18>; def OpenACC_UpdateDevice : I64EnumAttrCase<"acc_update_device", 19>; def OpenACC_UseDevice : I64EnumAttrCase<"acc_use_device", 20>; +def OpenACC_Reduction : I64EnumAttrCase<"acc_reduction", 21>; def OpenACC_DataClauseEnum : I64EnumAttr<"DataClause", "data clauses supported by OpenACC", @@ -104,6 +105,7 @@ OpenACC_PrivateClause, OpenACC_FirstPrivateClause, OpenACC_IsDevicePtrClause, OpenACC_GetDevicePtrClause, OpenACC_UpdateHost, OpenACC_UpdateSelf, OpenACC_UpdateDevice, OpenACC_UseDevice, + OpenACC_Reduction, ]> { let cppNamespace = "::mlir::acc"; } @@ -222,6 +224,14 @@ let hasVerifier = 1; } +//===----------------------------------------------------------------------===// +// 2.5.15 reduction clause +//===----------------------------------------------------------------------===// +def OpenACC_ReductionOp : OpenACC_DataEntryOp<"reduction", + "mlir::acc::DataClause::acc_reduction"> { + let summary = "Represents reduction semantics for acc reduction clause."; +} + //===----------------------------------------------------------------------===// // 2.7.4 deviceptr clause //===----------------------------------------------------------------------===// diff --git a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp --- a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp +++ b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp @@ -61,6 +61,16 @@ return success(); } +//===----------------------------------------------------------------------===// +// ReductionOp +//===----------------------------------------------------------------------===// +LogicalResult acc::ReductionOp::verify() { + if (getDataClause() != acc::DataClause::acc_reduction) + return emitError("data clause associated with reduction operation must " + "match its intent"); + return success(); +} + //===----------------------------------------------------------------------===// // DevicePtrOp //===----------------------------------------------------------------------===//