Skip to content

Commit

Permalink
[flang][openacc] Add lowering for multiply operator
Browse files Browse the repository at this point in the history
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
  • Loading branch information
clementval committed Jun 1, 2023
1 parent 59ceb7d commit e939dbc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
15 changes: 13 additions & 2 deletions flang/lib/Lower/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand All @@ -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");
}

Expand Down
50 changes: 49 additions & 1 deletion flang/test/Lower/OpenACC/acc-reduction.f90
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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>)

0 comments on commit e939dbc

Please sign in to comment.