[flang-commits] [flang] 208fdcb - [flang][openacc] Add lowering for max operator

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Wed Jun 14 15:21:35 PDT 2023


Author: Valentin Clement
Date: 2023-06-14T15:21:23-07:00
New Revision: 208fdcb07fc627a3febf71f1f637c5aad3667283

URL: https://github.com/llvm/llvm-project/commit/208fdcb07fc627a3febf71f1f637c5aad3667283
DIFF: https://github.com/llvm/llvm-project/commit/208fdcb07fc627a3febf71f1f637c5aad3667283.diff

LOG: [flang][openacc] Add lowering for max operator

Add support for the max operator in the reduction
clause.

Depdns on D151671

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D151672

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 6b6b0b7ec98c6..1d5fd7460a643 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -532,7 +532,8 @@ static mlir::Value genReductionInitValue(fir::FirOpBuilder &builder,
                                          mlir::acc::ReductionOperator op) {
   if (op != mlir::acc::ReductionOperator::AccAdd &&
       op != mlir::acc::ReductionOperator::AccMul &&
-      op != mlir::acc::ReductionOperator::AccMin)
+      op != mlir::acc::ReductionOperator::AccMin &&
+      op != mlir::acc::ReductionOperator::AccMax)
     TODO(loc, "reduction operator");
 
   // min -> largest
@@ -551,6 +552,20 @@ static mlir::Value genReductionInitValue(fir::FirOpBuilder &builder,
           builder.getFloatAttr(
               ty, llvm::APFloat::getLargest(sem, /*negative=*/false)));
     }
+    // max -> least
+  } else if (op == mlir::acc::ReductionOperator::AccMax) {
+    if (ty.isIntOrIndex())
+      return builder.create<mlir::arith::ConstantOp>(
+          loc, ty,
+          builder.getIntegerAttr(
+              ty, llvm::APInt::getSignedMinValue(ty.getIntOrFloatBitWidth())
+                      .getSExtValue()));
+    if (auto floatTy = mlir::dyn_cast_or_null<mlir::FloatType>(ty))
+      return builder.create<mlir::arith::ConstantOp>(
+          loc, ty,
+          builder.getFloatAttr(
+              ty, llvm::APFloat::getSmallest(floatTy.getFloatSemantics(),
+                                             /*negative=*/true)));
   } else {
     // 0 for +, ior, ieor
     // 1 for *
@@ -562,6 +577,7 @@ static mlir::Value genReductionInitValue(fir::FirOpBuilder &builder,
       return builder.create<mlir::arith::ConstantOp>(
           loc, ty, builder.getFloatAttr(ty, initValue));
   }
+
   TODO(loc, "reduction type");
 }
 
@@ -587,6 +603,9 @@ static mlir::Value genCombiner(fir::FirOpBuilder &builder, mlir::Location loc,
   if (op == mlir::acc::ReductionOperator::AccMin)
     return fir::genMin(builder, loc, {value1, value2});
 
+  if (op == mlir::acc::ReductionOperator::AccMax)
+    return fir::genMax(builder, loc, {value1, value2});
+
   TODO(loc, "reduction operator");
 }
 

diff  --git a/flang/test/Lower/OpenACC/acc-reduction.f90 b/flang/test/Lower/OpenACC/acc-reduction.f90
index 1499cb3feea31..805cfaca36889 100644
--- a/flang/test/Lower/OpenACC/acc-reduction.f90
+++ b/flang/test/Lower/OpenACC/acc-reduction.f90
@@ -2,6 +2,28 @@
 
 ! RUN: bbc -fopenacc -emit-fir %s -o - | FileCheck %s
 
+! CHECK-LABEL: acc.reduction.recipe @reduction_max_f32 : f32 reduction_operator <max> init {
+! CHECK: ^bb0(%{{.*}}: f32):
+! CHECK:   %[[INIT:.*]] = arith.constant -1.401300e-45 : f32
+! CHECK:   acc.yield %[[INIT]] : f32
+! CHECK: } combiner {
+! CHECK: ^bb0(%[[ARG0:.*]]: f32, %[[ARG1:.*]]: f32):
+! CHECK:   %[[CMP:.*]] = arith.cmpf ogt, %[[ARG0]], %[[ARG1]] : f32
+! CHECK:   %[[SELECT:.*]] = arith.select %[[CMP]], %[[ARG0]], %[[ARG1]] : f32
+! CHECK:   acc.yield %[[SELECT]] : f32
+! CHECK: }
+
+! CHECK-LABEL: acc.reduction.recipe @reduction_max_i32 : i32 reduction_operator <max> init {
+! CHECK: ^bb0(%arg0: i32):
+! CHECK:   %[[INIT:.*]] = arith.constant -2147483648 : i32
+! CHECK:   acc.yield %[[INIT]] : i32
+! CHECK: } combiner {
+! CHECK: ^bb0(%[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32):
+! CHECK:   %[[CMP:.*]] = arith.cmpi sgt, %[[ARG0]], %[[ARG1]] : i32
+! CHECK:   %[[SELECT:.*]] = arith.select %[[CMP]], %[[ARG0]], %[[ARG1]] : i32
+! CHECK:   acc.yield %[[SELECT]] : i32
+! CHECK: }
+
 ! CHECK-LABEL: acc.reduction.recipe @reduction_min_f32 : f32 reduction_operator <min> init {
 ! CHECK: ^bb0(%{{.*}}: f32):
 ! CHECK:   %[[INIT:.*]] = arith.constant 3.40282347E+38 : f32
@@ -148,3 +170,31 @@ subroutine acc_reduction_min_float(a, b)
 ! CHECK-LABEL: func.func @_QPacc_reduction_min_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_min_f32 -> %[[B]] : !fir.ref<f32>)
+
+subroutine acc_reduction_max_int(a, b)
+  integer :: a(100)
+  integer :: i, b
+
+  !$acc loop reduction(max:b)
+  do i = 1, 100
+    b = max(b, a(i))
+  end do
+end subroutine
+
+! CHECK-LABEL: func.func @_QPacc_reduction_max_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_max_i32 -> %[[B]] : !fir.ref<i32>)
+
+subroutine acc_reduction_max_float(a, b)
+  real :: a(100), b
+  integer :: i
+
+  !$acc loop reduction(max:b)
+  do i = 1, 100
+    b = max(b, a(i))
+  end do
+end subroutine
+
+! CHECK-LABEL: func.func @_QPacc_reduction_max_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_max_f32 -> %[[B]] : !fir.ref<f32>)


        


More information about the flang-commits mailing list