Skip to content

Commit

Permalink
[flang][openacc] Support .and. reduction operator
Browse files Browse the repository at this point in the history
Add support for the `.and.` reduction operator
in Flang/OpenACC lowering.

Depends on D154888

Reviewed By: razvanlupusoru

Differential Revision: https://reviews.llvm.org/D154896
  • Loading branch information
clementval committed Jul 12, 2023
1 parent a48445f commit 664575a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
23 changes: 16 additions & 7 deletions flang/lib/Lower/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,15 +687,16 @@ static R getReductionInitValue(mlir::acc::ReductionOperator op, mlir::Type ty) {
static mlir::Value genReductionInitValue(fir::FirOpBuilder &builder,
mlir::Location loc, mlir::Type ty,
mlir::acc::ReductionOperator op) {
if (op != mlir::acc::ReductionOperator::AccAdd &&
op != mlir::acc::ReductionOperator::AccMul &&
op != mlir::acc::ReductionOperator::AccMin &&
op != mlir::acc::ReductionOperator::AccMax &&
op != mlir::acc::ReductionOperator::AccIand &&
op != mlir::acc::ReductionOperator::AccIor &&
op != mlir::acc::ReductionOperator::AccXor)
if (op == mlir::acc::ReductionOperator::AccLor &&
op == mlir::acc::ReductionOperator::AccEqv &&
op == mlir::acc::ReductionOperator::AccNeqv)
TODO(loc, "reduction operator");

if (op == mlir::acc::ReductionOperator::AccLand) {
assert(mlir::isa<fir::LogicalType>(ty) && "expect fir.logical type");
return builder.createBool(loc, true);
}

if (ty.isIntOrIndex())
return builder.create<mlir::arith::ConstantOp>(
loc, ty,
Expand Down Expand Up @@ -809,6 +810,14 @@ static mlir::Value genCombiner(fir::FirOpBuilder &builder, mlir::Location loc,
if (op == mlir::acc::ReductionOperator::AccXor)
return builder.create<mlir::arith::XOrIOp>(loc, value1, value2);

if (op == mlir::acc::ReductionOperator::AccLand) {
mlir::Type i1 = builder.getI1Type();
mlir::Value v1 = builder.create<fir::ConvertOp>(loc, i1, value1);
mlir::Value v2 = builder.create<fir::ConvertOp>(loc, i1, value2);
mlir::Value add = builder.create<mlir::arith::AndIOp>(loc, v1, v2);
return builder.create<fir::ConvertOp>(loc, value1.getType(), add);
}

TODO(loc, "reduction operator");
}

Expand Down
23 changes: 23 additions & 0 deletions flang/test/Lower/OpenACC/acc-reduction.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

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

! CHECK-LABEL: acc.reduction.recipe @reduction_land_l32 : !fir.logical<4> reduction_operator <land> init {
! CHECK: ^bb0(%{{.*}}: !fir.logical<4>):
! CHECK: %[[CST:.*]] = arith.constant true
! CHECK: acc.yield %[[CST]] : i1
! CHECK: } combiner {
! CHECK: ^bb0(%[[ARG0:.*]]: !fir.logical<4>, %[[ARG1:.*]]: !fir.logical<4>):
! CHECK: %[[V1:.*]] = fir.convert %[[ARG0]] : (!fir.logical<4>) -> i1
! CHECK: %[[V2:.*]] = fir.convert %[[ARG1]] : (!fir.logical<4>) -> i1
! CHECK: %[[AND:.*]] = arith.andi %[[V1]], %[[V2]] : i1
! CHECK: %[[CONV:.*]] = fir.convert %[[AND]] : (i1) -> !fir.logical<4>
! CHECK: acc.yield %[[CONV]] : !fir.logical<4>
! CHECK: }

! CHECK-LABEL: acc.reduction.recipe @reduction_xor_i32 : i32 reduction_operator <xor> init {
! CHECK: ^bb0(%{{.*}}: i32):
! CHECK: %[[CST:.*]] = arith.constant 0 : i32
Expand Down Expand Up @@ -637,3 +650,13 @@ subroutine acc_reduction_ieor()
! CHECK-LABEL: func.func @_QPacc_reduction_ieor()
! CHECK: %[[RED:.*]] = acc.reduction varPtr(%{{.*}} : !fir.ref<i32>) -> !fir.ref<i32> {name = "i"}
! CHECK: acc.parallel reduction(@reduction_xor_i32 -> %[[RED]] : !fir.ref<i32>)

subroutine acc_reduction_and()
logical :: l
!$acc parallel reduction(.and.:l)
!$acc end parallel
end subroutine

! CHECK-LABEL: func.func @_QPacc_reduction_and()
! CHECK: %[[RED:.*]] = acc.reduction varPtr(%0 : !fir.ref<!fir.logical<4>>) -> !fir.ref<!fir.logical<4>> {name = "l"}
! CHECK: acc.parallel reduction(@reduction_land_l32 -> %[[RED]] : !fir.ref<!fir.logical<4>>)

0 comments on commit 664575a

Please sign in to comment.