[flang][OpenMP] Fix min reduction initialization (#73102)

Initialization of reduction variable for min-reduction is set to largest
negative value. As such, in presence of non-negative operands, min
reduction gives incorrect output. This patch initialises min-reduction
to use the maximum positive value instead, so that it can produce
correct output for the entire range of real valued operands.

Fixes https://github.com/llvm/llvm-project/issues/73101
This commit is contained in:
NimishMishra 2023-11-22 03:40:18 -08:00 committed by GitHub
parent 95828eed41
commit 956cf0e5de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 3 deletions

View File

@ -754,7 +754,7 @@ static mlir::Value getReductionInitValue(mlir::Location loc, mlir::Type type,
if (auto ty = type.dyn_cast<mlir::FloatType>()) {
const llvm::fltSemantics &sem = ty.getFloatSemantics();
return builder.createRealConstant(
loc, type, llvm::APFloat::getSmallest(sem, /*Negative=*/true));
loc, type, llvm::APFloat::getLargest(sem, /*Negative=*/false));
}
unsigned bits = type.getIntOrFloatBitWidth();
int64_t maxInt = llvm::APInt::getSignedMaxValue(bits).getSExtValue();

View File

@ -2,7 +2,7 @@
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s
!CHECK: omp.reduction.declare @[[MIN_DECLARE_F:.*]] : f32 init {
!CHECK: %[[MAXIMUM_VAL_F:.*]] = arith.constant -1.401300e-45 : f32
!CHECK: %[[MAXIMUM_VAL_F:.*]] = arith.constant 3.40282347E+38 : f32
!CHECK: omp.yield(%[[MAXIMUM_VAL_F]] : f32)
!CHECK: combiner
!CHECK: ^bb0(%[[ARG0_F:.*]]: f32, %[[ARG1_F:.*]]: f32):

View File

@ -2,7 +2,7 @@
! RUN: %flang_fc1 -emit-hlfir -fopenmp -o - %s 2>&1 | FileCheck %s
!CHECK: omp.reduction.declare @[[MIN_DECLARE_F:.*]] : f32 init {
!CHECK: %[[MAXIMUM_VAL_F:.*]] = arith.constant -1.401300e-45 : f32
!CHECK: %[[MAXIMUM_VAL_F:.*]] = arith.constant 3.40282347E+38 : f32
!CHECK: omp.yield(%[[MAXIMUM_VAL_F]] : f32)
!CHECK: combiner
!CHECK: ^bb0(%[[ARG0_F:.*]]: f32, %[[ARG1_F:.*]]: f32):