Skip to content

Commit e939dbc

Browse files
committed
[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
1 parent 59ceb7d commit e939dbc

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

flang/lib/Lower/OpenACC.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,13 @@ getReductionOperator(const Fortran::parser::AccReductionOperator &op) {
559559
static mlir::Value genReductionInitValue(mlir::OpBuilder &builder,
560560
mlir::Location loc, mlir::Type ty,
561561
mlir::acc::ReductionOperator op) {
562-
if (op != mlir::acc::ReductionOperator::AccAdd)
562+
if (op != mlir::acc::ReductionOperator::AccAdd &&
563+
op != mlir::acc::ReductionOperator::AccMul)
563564
TODO(loc, "reduction operator");
564565

565-
unsigned initValue = 0;
566+
// 0 for +, ior, ieor
567+
// 1 for *
568+
unsigned initValue = op == mlir::acc::ReductionOperator::AccMul ? 1 : 0;
566569

567570
if (ty.isIntOrIndex())
568571
return builder.create<mlir::arith::ConstantOp>(
@@ -583,6 +586,14 @@ static mlir::Value genCombiner(mlir::OpBuilder &builder, mlir::Location loc,
583586
return builder.create<mlir::arith::AddFOp>(loc, value1, value2);
584587
TODO(loc, "reduction add type");
585588
}
589+
590+
if (op == mlir::acc::ReductionOperator::AccMul) {
591+
if (ty.isIntOrIndex())
592+
return builder.create<mlir::arith::MulIOp>(loc, value1, value2);
593+
if (mlir::isa<mlir::FloatType>(ty))
594+
return builder.create<mlir::arith::MulFOp>(loc, value1, value2);
595+
TODO(loc, "reduction mul type");
596+
}
586597
TODO(loc, "reduction operator");
587598
}
588599

flang/test/Lower/OpenACC/acc-reduction.f90

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22

33
! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s
44

5+
! CHECK-LABEL: acc.reduction.recipe @reduction_mul_f32 : f32 reduction_operator <mul> init {
6+
! CHECK: ^bb0(%{{.*}}: f32):
7+
! CHECK: %[[INIT:.*]] = arith.constant 1.000000e+00 : f32
8+
! CHECK: acc.yield %[[INIT]] : f32
9+
! CHECK: } combiner {
10+
! CHECK: ^bb0(%[[ARG0:.*]]: f32, %[[ARG1:.*]]: f32):
11+
! CHECK: %[[COMBINED:.*]] = arith.mulf %[[ARG0]], %[[ARG1]] {{.*}} : f32
12+
! CHECK: acc.yield %[[COMBINED]] : f32
13+
! CHECK: }
14+
15+
! CHECK-LABEL: acc.reduction.recipe @reduction_mul_i32 : i32 reduction_operator <mul> init {
16+
! CHECK: ^bb0(%{{.*}}: i32):
17+
! CHECK: %[[INIT:.*]] = arith.constant 1 : i32
18+
! CHECK: acc.yield %[[INIT]] : i32
19+
! CHECK: } combiner {
20+
! CHECK: ^bb0(%[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32):
21+
! CHECK: %[[COMBINED:.*]] = arith.muli %[[ARG0]], %[[ARG1]] : i32
22+
! CHECK: acc.yield %[[COMBINED]] : i32
23+
! CHECK: }
24+
525
! CHECK-LABEL: acc.reduction.recipe @reduction_add_f32 : f32 reduction_operator <add> init {
626
! CHECK: ^bb0(%{{.*}}: f32):
727
! CHECK: %[[INIT:.*]] = arith.constant 0.000000e+00 : f32
@@ -34,7 +54,7 @@ subroutine acc_reduction_add_int(a, b)
3454

3555
! CHECK-LABEL: func.func @_QPacc_reduction_add_int(
3656
! CHECK-SAME: %{{.*}}: !fir.ref<!fir.array<100xi32>> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref<i32> {fir.bindc_name = "b"})
37-
! CHECK: acc.loop reduction(@reduction_add_i32 -> %[[B]] : !fir.ref<i32>) {
57+
! CHECK: acc.loop reduction(@reduction_add_i32 -> %[[B]] : !fir.ref<i32>)
3858

3959
subroutine acc_reduction_add_float(a, b)
4060
real :: a(100), b
@@ -49,3 +69,31 @@ subroutine acc_reduction_add_float(a, b)
4969
! CHECK-LABEL: func.func @_QPacc_reduction_add_float(
5070
! CHECK-SAME: %{{.*}}: !fir.ref<!fir.array<100xf32>> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref<f32> {fir.bindc_name = "b"})
5171
! CHECK: acc.loop reduction(@reduction_add_f32 -> %[[B]] : !fir.ref<f32>)
72+
73+
subroutine acc_reduction_mul_int(a, b)
74+
integer :: a(100)
75+
integer :: i, b
76+
77+
!$acc loop reduction(*:b)
78+
do i = 1, 100
79+
b = b * a(i)
80+
end do
81+
end subroutine
82+
83+
! CHECK-LABEL: func.func @_QPacc_reduction_mul_int(
84+
! CHECK-SAME: %{{.*}}: !fir.ref<!fir.array<100xi32>> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref<i32> {fir.bindc_name = "b"})
85+
! CHECK: acc.loop reduction(@reduction_mul_i32 -> %[[B]] : !fir.ref<i32>)
86+
87+
subroutine acc_reduction_mul_float(a, b)
88+
real :: a(100), b
89+
integer :: i
90+
91+
!$acc loop reduction(*:b)
92+
do i = 1, 100
93+
b = b * a(i)
94+
end do
95+
end subroutine
96+
97+
! CHECK-LABEL: func.func @_QPacc_reduction_mul_float(
98+
! CHECK-SAME: %{{.*}}: !fir.ref<!fir.array<100xf32>> {fir.bindc_name = "a"}, %[[B:.*]]: !fir.ref<f32> {fir.bindc_name = "b"})
99+
! CHECK: acc.loop reduction(@reduction_mul_f32 -> %[[B]] : !fir.ref<f32>)

0 commit comments

Comments
 (0)