Skip to content

Commit

Permalink
[flang] Fix overflow detection for folding SUM/PRODUCT
Browse files Browse the repository at this point in the history
The overflow detection code in the templates that fold SUM and PRODUCT
was checking for overflow before performing the reduction, not after.
Fix and add tests.

Differential Revision: https://reviews.llvm.org/D154374
  • Loading branch information
klausler committed Jul 3, 2023
1 parent b288e66 commit 58d7484
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions flang/lib/Evaluate/fold-reduction.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ static Expr<T> FoldProduct(
element = prod.value;
}
}};
auto result{Expr<T>{DoReduction<T>(*array, dim, identity, accumulator)}};
if (overflow) {
context.messages().Say(
"PRODUCT() of %s data overflowed"_warn_en_US, T::AsFortran());
} else {
return Expr<T>{DoReduction<T>(*array, dim, identity, accumulator)};
}
return result;
}
return Expr<T>{std::move(ref)};
}
Expand Down Expand Up @@ -301,12 +301,12 @@ static Expr<T> FoldSum(FoldingContext &context, FunctionRef<T> &&ref) {
element = sum.value;
}
}};
auto result{Expr<T>{DoReduction<T>(*array, dim, identity, accumulator)}};
if (overflow) {
context.messages().Say(
"SUM() of %s data overflowed"_warn_en_US, T::AsFortran());
} else {
return Expr<T>{DoReduction<T>(*array, dim, identity, accumulator)};
}
return result;
}
return Expr<T>{std::move(ref)};
}
Expand Down

0 comments on commit 58d7484

Please sign in to comment.