[flang-commits] [flang] e939dbc - [flang][openacc] Add lowering for multiply operator
Valentin Clement via flang-commits
flang-commits at lists.llvm.org
Thu Jun 1 06:17:26 PDT 2023
Author: Valentin Clement
Date: 2023-06-01T22:17:04+09:00
New Revision: e939dbc31455d31d2648c6a0452e53c7055e2ed0
URL: https://github.com/llvm/llvm-project/commit/e939dbc31455d31d2648c6a0452e53c7055e2ed0
DIFF: https://github.com/llvm/llvm-project/commit/e939dbc31455d31d2648c6a0452e53c7055e2ed0.diff
LOG: [flang][openacc] Add lowering for multiply operator
Add support for the * operation in OpenACC lowering. Support is added
for the types currently supported.
Depends on D151564
Reviewed By: razvanlupusoru
Differential Revision: https://reviews.llvm.org/D151565
Added:
Modified:
flang/lib/Lower/OpenACC.cpp
flang/test/Lower/OpenACC/acc-reduction.f90
Removed:
################################################################################
diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index f33298763faa4..c59be17f2c6e1 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -559,10 +559,13 @@ getReductionOperator(const Fortran::parser::AccReductionOperator &op) {
static mlir::Value genReductionInitValue(mlir::OpBuilder &builder,
mlir::Location loc, mlir::Type ty,
mlir::acc::ReductionOperator op) {
- if (op != mlir::acc::ReductionOperator::AccAdd)
+ if (op != mlir::acc::ReductionOperator::AccAdd &&
+ op != mlir::acc::ReductionOperator::AccMul)
TODO(loc, "reduction operator");
- unsigned initValue = 0;
+ // 0 for +, ior, ieor
+ // 1 for *
+ unsigned initValue = op == mlir::acc::ReductionOperator::AccMul ? 1 : 0;
if (ty.isIntOrIndex())
return builder.create<mlir::arith::ConstantOp>(
@@ -583,6 +586,14 @@ static mlir::Value genCombiner(mlir::OpBuilder &builder, mlir::Location loc,
return builder.create<mlir::arith::AddFOp>(loc, value1, value2);
TODO(loc, "reduction add type");
}
+
+ if (op == mlir::acc::ReductionOperator::AccMul) {
+ if (ty.isIntOrIndex())
+ return builder.create<mlir::arith::MulIOp>(loc, value1, value2);
+ if (mlir::isa<mlir::FloatType>(ty))
+ return builder.create<mlir::arith::MulFOp>(loc, value1, value2);
+ TODO(loc, "reduction mul type");
+ }
TODO(loc, "reduction operator");
}
diff --git a/flang/test/Lower/OpenACC/acc-reduction.f90 b/flang/test/Lower/OpenACC/acc-reduction.f90
index 4c95b40b8c9ea..9dd40a71d99a5 100644
--- a/flang/test/Lower/OpenACC/acc-reduction.f90
+++ b/flang/test/Lower/OpenACC/acc-reduction.f90
@@ -2,6 +2,26 @@
! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s
+! CHECK-LABEL: acc.reduction.recipe @reduction_mul_f32 : f32 reduction_operator <mul> init {
+! CHECK: ^bb0(%{{.*}}: f32):
+! CHECK: %[[INIT:.*]] = arith.constant 1.000000e+00 : f32
+! CHECK: acc.yield %[[INIT]] : f32
+! CHECK: } combiner {
+! CHECK: ^bb0(%[[ARG0:.*]]: f32, %[[ARG1:.*]]: f32):
+! CHECK: %[[COMBINED:.*]] = arith.mulf %[[ARG0]], %[[ARG1]] {{.*}} : f32
+! CHECK: acc.yield %[[COMBINED]] : f32
+! CHECK: }
+
+! CHECK-LABEL: acc.reduction.recipe @reduction_mul_i32 : i32 reduction_operator <mul> init {
+! CHECK: ^bb0(%{{.*}}: i32):
+! CHECK: %[[INIT:.*]] = arith.constant 1 : i32
+! CHECK: acc.yield %[[INIT]] : i32
+! CHECK: } combiner {
+! CHECK: ^bb0(%[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32):
+! CHECK: %[[COMBINED:.*]] = arith.muli %[[ARG0]], %[[ARG1]] : i32
+! CHECK: acc.yield %[[COMBINED]] : i32
+! CHECK: }
+
! CHECK-LABEL: acc.reduction.recipe @reduction_add_f32 : f32 reduction_operator <add> init {
! CHECK: ^bb0(%{{.*}}: f32):
! CHECK: %[[INIT:.*]] = arith.constant 0.000000e+00 : f32
@@ -34,7 +54,7 @@ subroutine acc_reduction_add_int(a, b)
! CHECK-LABEL: func.func @_QPacc_reduction_add_int(
! CHECK-SAME: %{{.*}}: !fir.ref<!fir.array<100xi32>> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref<i32> {fir.bindc_name = "b"})
-! CHECK: acc.loop reduction(@reduction_add_i32 -> %[[B]] : !fir.ref<i32>) {
+! CHECK: acc.loop reduction(@reduction_add_i32 -> %[[B]] : !fir.ref<i32>)
subroutine acc_reduction_add_float(a, b)
real :: a(100), b
@@ -49,3 +69,31 @@ subroutine acc_reduction_add_float(a, b)
! CHECK-LABEL: func.func @_QPacc_reduction_add_float(
! CHECK-SAME: %{{.*}}: !fir.ref<!fir.array<100xf32>> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref<f32> {fir.bindc_name = "b"})
! CHECK: acc.loop reduction(@reduction_add_f32 -> %[[B]] : !fir.ref<f32>)
+
+subroutine acc_reduction_mul_int(a, b)
+ integer :: a(100)
+ integer :: i, b
+
+ !$acc loop reduction(*:b)
+ do i = 1, 100
+ b = b * a(i)
+ end do
+end subroutine
+
+! CHECK-LABEL: func.func @_QPacc_reduction_mul_int(
+! CHECK-SAME: %{{.*}}: !fir.ref<!fir.array<100xi32>> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref<i32> {fir.bindc_name = "b"})
+! CHECK: acc.loop reduction(@reduction_mul_i32 -> %[[B]] : !fir.ref<i32>)
+
+subroutine acc_reduction_mul_float(a, b)
+ real :: a(100), b
+ integer :: i
+
+ !$acc loop reduction(*:b)
+ do i = 1, 100
+ b = b * a(i)
+ end do
+end subroutine
+
+! CHECK-LABEL: func.func @_QPacc_reduction_mul_float(
+! CHECK-SAME: %{{.*}}: !fir.ref<!fir.array<100xf32>> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref<f32> {fir.bindc_name = "b"})
+! CHECK: acc.loop reduction(@reduction_mul_f32 -> %[[B]] : !fir.ref<f32>)
More information about the flang-commits
mailing list