[flang-commits] [flang] [Flang] Correct initial limit value in float min/maxloc reductions. (PR #81260)

David Green via flang-commits flang-commits at lists.llvm.org
Fri Feb 9 08:05:28 PST 2024


https://github.com/davemgreen created https://github.com/llvm/llvm-project/pull/81260

I was looking through to check whether Nan was being handled correctly, and couldn't work out why simple cases were behaving differently than they should. It turns out the initial limit values was backwards for minloc/maxloc reductions in general. This fixes that, introduced in #79469.

>From 185050c4f76beef5e779ef5f363250b4f1b31a13 Mon Sep 17 00:00:00 2001
From: David Green <david.green at arm.com>
Date: Fri, 9 Feb 2024 14:22:44 +0000
Subject: [PATCH] [Flang] Correct initial limit value in float min/maxloc
 reductions.

I was looking through to check whether Nan was being handled correctly, and
couldn't work out why simple cases were behaving differently. It turns out the
initial limit values was backwards for minloc/maxloc reductions. This fixes
that from #79469.
---
 flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp | 2 +-
 flang/test/HLFIR/maxloc-elemental.fir                           | 1 +
 flang/test/HLFIR/minloc-elemental.fir                           | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp b/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
index b1165a5ee19395..523671f0605f77 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
@@ -854,7 +854,7 @@ class MinMaxlocElementalConversion : public mlir::OpRewritePattern<Op> {
         const llvm::fltSemantics &sem = ty.getFloatSemantics();
         return builder.createRealConstant(
             loc, elementType,
-            llvm::APFloat::getLargest(sem, /*Negative=*/!isMax));
+            llvm::APFloat::getLargest(sem, /*Negative=*/isMax));
       }
       unsigned bits = elementType.getIntOrFloatBitWidth();
       int64_t limitInt =
diff --git a/flang/test/HLFIR/maxloc-elemental.fir b/flang/test/HLFIR/maxloc-elemental.fir
index 67cd9ee4bb75a7..b4a3ca0d86068f 100644
--- a/flang/test/HLFIR/maxloc-elemental.fir
+++ b/flang/test/HLFIR/maxloc-elemental.fir
@@ -110,6 +110,7 @@ func.func @_QPtest_float(%arg0: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "a
   return
 }
 // CHECK-LABEL: _QPtest_float
+// CHECK:        %cst = arith.constant -3.40282347E+38 : f32
 // CHECK:        %[[V11:.*]] = fir.do_loop %arg3 = %c0 to %[[V10:.*]] step %c1 iter_args(%arg4 = %cst) -> (f32) {
 // CHECK-NEXT:     %[[V14:.*]] = arith.addi %arg3, %c1 : index
 // CHECK-NEXT:     %[[V15:.*]] = hlfir.designate %[[V1:.*]]#0 (%[[V14]])  : (!fir.box<!fir.array<?xf32>>, index) -> !fir.ref<f32>
diff --git a/flang/test/HLFIR/minloc-elemental.fir b/flang/test/HLFIR/minloc-elemental.fir
index cb483d51e05d13..5cc608b65be8bc 100644
--- a/flang/test/HLFIR/minloc-elemental.fir
+++ b/flang/test/HLFIR/minloc-elemental.fir
@@ -295,6 +295,7 @@ func.func @_QPtest_float(%arg0: !fir.box<!fir.array<?xf32>> {fir.bindc_name = "a
   return
 }
 // CHECK-LABEL: _QPtest_float
+// CHECK:        %cst = arith.constant 3.40282347E+38 : f32
 // CHECK:        %[[V11:.*]] = fir.do_loop %arg3 = %c0 to %[[V10:.*]] step %c1 iter_args(%arg4 = %cst) -> (f32) {
 // CHECK-NEXT:     %[[V14:.*]] = arith.addi %arg3, %c1 : index
 // CHECK-NEXT:     %[[V15:.*]] = hlfir.designate %[[V1:.*]]#0 (%[[V14]])  : (!fir.box<!fir.array<?xf32>>, index) -> !fir.ref<f32>



More information about the flang-commits mailing list