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 @@ -753,7 +753,10 @@ if (!fir::isa_trivial(reductionTy)) 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()); if (!fir::isa_trivial(ty)) ty = baseAddr.getType(); std::string recipeName = fir::getTypeAsString( @@ -764,7 +767,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 @@ -213,7 +213,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_int_array_1d(a, b) integer :: a(100) @@ -227,7 +228,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_add_int_array_1d( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref> {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_add_ref_100xi32 -> %[[B]] : !fir.ref>) +! CHECK: %[[RED_B:.*]] = acc.reduction varPtr(%[[B]] : !fir.ref>) bounds(%{{.*}}) -> !fir.ref> {name = "b"} +! CHECK: acc.loop reduction(@reduction_add_ref_100xi32 -> %[[RED_B]] : !fir.ref>) subroutine acc_reduction_add_int_array_2d(a, b) integer :: a(100, 10), b(100, 10) @@ -243,7 +245,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_add_int_array_2d( ! CHECK-SAME: %[[ARG0:.*]]: !fir.ref> {fir.bindc_name = "a"}, %[[ARG1:.*]]: !fir.ref> {fir.bindc_name = "b"}) { -! CHECK: acc.loop reduction(@reduction_add_ref_100x10xi32 -> %[[ARG1]] : !fir.ref>) { +! CHECK: %[[RED_ARG1:.*]] = acc.reduction varPtr(%[[ARG1]] : !fir.ref>) bounds(%{{.*}}, %{{.*}}) -> !fir.ref> {name = "b"} +! CHECK: acc.loop reduction(@reduction_add_ref_100x10xi32 -> %[[RED_ARG1]] : !fir.ref>) { ! CHECK: } attributes {collapse = 2 : i64} subroutine acc_reduction_add_int_array_3d(a, b) @@ -262,7 +265,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_add_int_array_3d( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[ARG1:.*]]: !fir.ref> {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_add_ref_100x10x2xi32 -> %[[ARG1]] : !fir.ref>) +! CHECK: %[[RED_ARG1:.*]] = acc.reduction varPtr(%[[ARG1]] : !fir.ref>) bounds(%{{.*}}, %{{.*}}, %{{.*}}) -> !fir.ref> {name = "b"} +! CHECK: acc.loop reduction(@reduction_add_ref_100x10x2xi32 -> %[[RED_ARG1]] : !fir.ref>) ! CHECK: } attributes {collapse = 3 : i64} subroutine acc_reduction_add_float(a, b) @@ -277,7 +281,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_add_float_array_1d(a, b) real :: a(100), b(100) @@ -291,7 +296,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_add_float_array_1d( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref> {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_add_ref_100xf32 -> %[[B]] : !fir.ref>) +! CHECK: %[[RED_B:.*]] = acc.reduction varPtr(%[[B]] : !fir.ref>) bounds(%{{.*}}) -> !fir.ref> {name = "b"} +! CHECK: acc.loop reduction(@reduction_add_ref_100xf32 -> %[[RED_B]] : !fir.ref>) subroutine acc_reduction_mul_int(a, b) integer :: a(100) @@ -305,7 +311,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_int_array_1d(a, b) integer :: a(100) @@ -319,7 +326,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_mul_int_array_1d( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref> {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_mul_ref_100xi32 -> %[[B]] : !fir.ref>) +! CHECK: %[[RED_B:.*]] = acc.reduction varPtr(%[[B]] : !fir.ref>) bounds(%{{.*}}) -> !fir.ref> {name = "b"} +! CHECK: acc.loop reduction(@reduction_mul_ref_100xi32 -> %[[RED_B]] : !fir.ref>) subroutine acc_reduction_mul_float(a, b) real :: a(100), b @@ -333,7 +341,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_mul_float_array_1d(a, b) real :: a(100), b(100) @@ -347,7 +356,8 @@ ! CHECK-LABEL: func.func @_QPacc_reduction_mul_float_array_1d( ! CHECK-SAME: %{{.*}}: !fir.ref> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref> {fir.bindc_name = "b"}) -! CHECK: acc.loop reduction(@reduction_mul_ref_100xf32 -> %[[B]] : !fir.ref>) +! CHECK: %[[RED_B:.*]] = acc.reduction varPtr(%[[B]] : !fir.ref>) bounds(%2) -> !fir.ref> {name = "b"} +! CHECK: acc.loop reduction(@reduction_mul_ref_100xf32 -> %[[RED_B]] : !fir.ref>) subroutine acc_reduction_min_int(a, b) integer :: a(100) @@ -361,7 +371,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 @@ -375,7 +386,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) @@ -389,7 +401,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 @@ -403,4 +416,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"; } @@ -239,6 +241,14 @@ "clause."; } +//===----------------------------------------------------------------------===// +// 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 @@ -81,6 +81,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 //===----------------------------------------------------------------------===//