From: Valentin Clement Date: Fri, 23 Jun 2023 17:26:39 +0000 (-0700) Subject: [flang][mlir][openacc] Add acc.reduction operation as data entry operation X-Git-Tag: upstream/17.0.6~4065 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cd91f3a69bad144a83b2e3b7caeb9f8d161a3a45;p=platform%2Fupstream%2Fllvm.git [flang][mlir][openacc] Add acc.reduction operation as data entry operation acc.reduction operation is used as data entry operation for the reduction operands. Reviewed By: jeanPerier Differential Revision: https://reviews.llvm.org/D153367 --- diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp index 55365a7..6c30d42 100644 --- a/flang/lib/Lower/OpenACC.cpp +++ b/flang/lib/Lower/OpenACC.cpp @@ -753,7 +753,10 @@ genReductions(const Fortran::parser::AccObjectListWithReduction &objectList, 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 @@ genReductions(const Fortran::parser::AccObjectListWithReduction &objectList, 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 index 13e4ad4..50c4e78 100644 --- a/flang/test/Lower/OpenACC/acc-reduction.f90 +++ b/flang/test/Lower/OpenACC/acc-reduction.f90 @@ -213,7 +213,8 @@ end subroutine ! 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 @@ end subroutine ! 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 @@ end subroutine ! 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 @@ end subroutine ! 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 @@ end subroutine ! 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 @@ end subroutine ! 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 @@ end subroutine ! 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 @@ end subroutine ! 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 @@ end subroutine ! 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 @@ end subroutine ! 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 @@ end subroutine ! 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 @@ end subroutine ! 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 @@ end subroutine ! 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 @@ end subroutine ! 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 index e5b017f..32882bd 100644 --- a/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td +++ b/mlir/include/mlir/Dialect/OpenACC/OpenACCOps.td @@ -94,6 +94,7 @@ def OpenACC_UpdateHost : I64EnumAttrCase<"acc_update_host", 17>; 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 @@ def OpenACC_DataClauseEnum : I64EnumAttr<"DataClause", OpenACC_PrivateClause, OpenACC_FirstPrivateClause, OpenACC_IsDevicePtrClause, OpenACC_GetDevicePtrClause, OpenACC_UpdateHost, OpenACC_UpdateSelf, OpenACC_UpdateDevice, OpenACC_UseDevice, + OpenACC_Reduction, ]> { let cppNamespace = "::mlir::acc"; } @@ -240,6 +242,14 @@ def OpenACC_FirstprivateOp : OpenACC_DataEntryOp<"firstprivate", } //===----------------------------------------------------------------------===// +// 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 //===----------------------------------------------------------------------===// def OpenACC_DevicePtrOp : OpenACC_DataEntryOp<"deviceptr", diff --git a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp index d4052a0..9b04d6c 100644 --- a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp +++ b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp @@ -82,6 +82,16 @@ LogicalResult acc::FirstprivateOp::verify() { } //===----------------------------------------------------------------------===// +// 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 //===----------------------------------------------------------------------===// LogicalResult acc::DevicePtrOp::verify() {