Skip to content

Commit 9d08498

Browse files
committed
[flang] Clarify CheckReductionDIM()
This utility routine in constant folding should return false when a DIM= actual argument to a reduction intrinsic function has a value that prevents folding, and true when folding can proceed. The implementation was returning true in cases where a DIM= argument was present but not constant. Clarify the code and add commentary: a true result means that there is no DIM= actual argument present, or that a DIM= argument exists, is constant, and has a value that is in range. Differential Revision: https://reviews.llvm.org/D131101
1 parent 1d1a569 commit 9d08498

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

flang/lib/Evaluate/fold-reduction.cpp

+17-14
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,26 @@
1111
namespace Fortran::evaluate {
1212
bool CheckReductionDIM(std::optional<int> &dim, FoldingContext &context,
1313
ActualArguments &arg, std::optional<int> dimIndex, int rank) {
14-
if (dimIndex && static_cast<std::size_t>(*dimIndex) < arg.size()) {
15-
if (auto *dimConst{
16-
Folder<SubscriptInteger>{context}.Folding(arg[*dimIndex])}) {
17-
if (auto dimScalar{dimConst->GetScalarValue()}) {
18-
auto dimVal{dimScalar->ToInt64()};
19-
if (dimVal >= 1 && dimVal <= rank) {
20-
dim = dimVal;
21-
} else {
22-
context.messages().Say(
23-
"DIM=%jd is not valid for an array of rank %d"_err_en_US,
24-
static_cast<std::intmax_t>(dimVal), rank);
25-
return false;
26-
}
14+
if (!dimIndex || static_cast<std::size_t>(*dimIndex) >= arg.size() ||
15+
!arg[*dimIndex]) {
16+
dim.reset();
17+
return true; // no DIM= argument
18+
}
19+
if (auto *dimConst{
20+
Folder<SubscriptInteger>{context}.Folding(arg[*dimIndex])}) {
21+
if (auto dimScalar{dimConst->GetScalarValue()}) {
22+
auto dimVal{dimScalar->ToInt64()};
23+
if (dimVal >= 1 && dimVal <= rank) {
24+
dim = dimVal;
25+
return true; // DIM= exists and is a valid constant
26+
} else {
27+
context.messages().Say(
28+
"DIM=%jd is not valid for an array of rank %d"_err_en_US,
29+
static_cast<std::intmax_t>(dimVal), rank);
2730
}
2831
}
2932
}
30-
return true;
33+
return false; // DIM= bad or not scalar constant
3134
}
3235

3336
Constant<LogicalResult> *GetReductionMASK(

flang/lib/Evaluate/fold-reduction.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
namespace Fortran::evaluate {
1717

18-
// Fold and validate a DIM= argument. Returns false on error.
18+
// Fold and validate a DIM= argument. Returns true (with &dim empty)
19+
// when DIM= is not present or (with &dim set) when DIM= is present, constant,
20+
// and valid. Returns false, possibly with an error message, when
21+
// DIM= is present but either not constant or not valid.
1922
bool CheckReductionDIM(std::optional<int> &dim, FoldingContext &,
2023
ActualArguments &, std::optional<int> dimIndex, int rank);
2124

0 commit comments

Comments
 (0)