Skip to content

Commit b685597

Browse files
authored
[flang] Fix crash in reduction folding (#87201)
A reduction folding template assumed lower bounds were 1. Fixes #86935.
1 parent 92ecc22 commit b685597

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

flang/lib/Evaluate/fold-reduction.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,25 +182,27 @@ static Constant<T> DoReduction(const Constant<ARRAY> &array,
182182
ConstantSubscript &maskDimAt{maskAt[*dim - 1]};
183183
ConstantSubscript maskDimLbound{maskDimAt};
184184
for (auto n{GetSize(resultShape)}; n-- > 0;
185-
IncrementSubscripts(at, array.shape()),
186-
IncrementSubscripts(maskAt, mask.shape())) {
187-
dimAt = dimLbound;
188-
maskDimAt = maskDimLbound;
185+
array.IncrementSubscripts(at), mask.IncrementSubscripts(maskAt)) {
189186
elements.push_back(identity);
190-
bool firstUnmasked{true};
191-
for (ConstantSubscript j{0}; j < dimExtent; ++j, ++dimAt, ++maskDimAt) {
192-
if (mask.At(maskAt).IsTrue()) {
193-
accumulator(elements.back(), at, firstUnmasked);
194-
firstUnmasked = false;
187+
if (dimExtent > 0) {
188+
dimAt = dimLbound;
189+
maskDimAt = maskDimLbound;
190+
bool firstUnmasked{true};
191+
for (ConstantSubscript j{0}; j < dimExtent; ++j, ++dimAt, ++maskDimAt) {
192+
if (mask.At(maskAt).IsTrue()) {
193+
accumulator(elements.back(), at, firstUnmasked);
194+
firstUnmasked = false;
195+
}
195196
}
197+
--dimAt, --maskDimAt;
196198
}
197199
accumulator.Done(elements.back());
198200
}
199201
} else { // no DIM=, result is scalar
200202
elements.push_back(identity);
201203
bool firstUnmasked{true};
202-
for (auto n{array.size()}; n-- > 0; IncrementSubscripts(at, array.shape()),
203-
IncrementSubscripts(maskAt, mask.shape())) {
204+
for (auto n{array.size()}; n-- > 0;
205+
array.IncrementSubscripts(at), mask.IncrementSubscripts(maskAt)) {
204206
if (mask.At(maskAt).IsTrue()) {
205207
accumulator(elements.back(), at, firstUnmasked);
206208
firstUnmasked = false;

flang/test/Evaluate/folding32.f90

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
! RUN: %python %S/test_folding.py %s %flang_fc1
2+
! Fold NORM2 reduction of array with non-default lower bound
3+
module m
4+
real, parameter :: a(2:3) = 0.0
5+
logical, parameter :: test1 = norm2(a) == 0.
6+
end

0 commit comments

Comments
 (0)