Skip to content

Commit 35cc2ec

Browse files
committed
[flang] Support FINDLOC/MAXLOC/MINLOC with scalar mask
Previously MASK= elements were accessed in assumption that mask is an array of input argument rank (and in combination with explicit DIM= argument we had out-of-bounds access), but for MAXLOC/MINLOC/FINDLOC mask should be be conformable and could be scalar. Add new regression tests with scalar mask for verification. Reviewed By: klausler Differential Revision: https://reviews.llvm.org/D124408
1 parent 92e22c9 commit 35cc2ec

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

flang/lib/Evaluate/fold-integer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,15 @@ template <WhichLocation WHICH> class LocationHelper {
313313
array->SetLowerBoundsToOne();
314314
ConstantSubscripts at{array->lbounds()}, maskAt, resultIndices, resultShape;
315315
if (mask) {
316+
if (auto scalarMask{mask->GetScalarValue()}) {
317+
// Convert into array in case of scalar MASK= (for
318+
// MAXLOC/MINLOC/FINDLOC mask should be be conformable)
319+
ConstantSubscript n{GetSize(array->shape())};
320+
std::vector<Scalar<LogicalResult>> mask_elements(
321+
n, Scalar<LogicalResult>{scalarMask.value()});
322+
*mask = Constant<LogicalResult>{
323+
std::move(mask_elements), ConstantSubscripts{n}};
324+
}
316325
mask->SetLowerBoundsToOne();
317326
maskAt = mask->lbounds();
318327
}

flang/test/Evaluate/fold-findloc.f90

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,19 @@ module m1
6666
logical, parameter :: test_char2 = all(minloc(a).eq.[1])
6767
logical, parameter :: test_char3 = all(maxloc(a, back=.true.).eq.[4])
6868
logical, parameter :: test_char4 = all(minloc(a, back=.true.).eq.[3])
69+
70+
! Check with scalar MASK=
71+
logical, parameter:: test_mia1_mt = all(minloc(ia1, mask=.true.) == 1)
72+
logical, parameter:: test_mia1_mtd = all(minloc(ia1, mask=.true., dim=1) == [1])
73+
logical, parameter:: test_xia1_mt = all(maxloc(ia1, mask=.true.) == 3)
74+
logical, parameter:: test_xia1_mtd = all(maxloc(ia1, mask=.true., dim=1) == [3])
75+
logical, parameter:: test_fia1_mt = all(findloc(ia1, 1, mask=.true.) == 1)
76+
logical, parameter:: test_fia1_mtd = all(findloc(ia1, 1, mask=.true., dim=1) == [1])
77+
78+
logical, parameter:: test_mia1_mf = all(minloc(ia1, mask=.false.) == 0)
79+
logical, parameter:: test_mia1_mfd = all(minloc(ia1, mask=.false., dim=1) == [0])
80+
logical, parameter:: test_xia1_mf = all(maxloc(ia1, mask=.false.) == 0)
81+
logical, parameter:: test_xia1_mfd = all(maxloc(ia1, mask=.false., dim=1) == [0])
82+
logical, parameter:: test_fia1_mf = all(findloc(ia1, 1, mask=.false.) == 0)
83+
logical, parameter:: test_fia1_mfd = all(findloc(ia1, 1, mask=.false., dim=1) == [0])
6984
end module

0 commit comments

Comments
 (0)