Skip to content

Commit 475d18f

Browse files
authored
[flang][runtime] Fix empty FINDLOC() results (#75251)
When FINDLOC() can't find its target value among the unmasked array elements, it must return a zero result. Its implementation doesn't sufficiently distinguish a zero result from a hit in an array with lower bound(s) less than one. Fix by adding a flag to distinguish the case with no hits from cases with hits. Fixes llvm-test-suite/Fortran/gfortran/regression/findloc_6.f90.
1 parent 8fc045e commit 475d18f

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

flang/runtime/findloc.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,27 @@ template <typename EQUALITY> class LocationAccumulator {
8484
public:
8585
LocationAccumulator(
8686
const Descriptor &array, const Descriptor &target, bool back)
87-
: array_{array}, target_{target}, back_{back} {
88-
Reinitialize();
89-
}
90-
void Reinitialize() {
91-
// per standard: result indices are all zero if no data
92-
for (int j{0}; j < rank_; ++j) {
93-
location_[j] = 0;
94-
}
95-
}
87+
: array_{array}, target_{target}, back_{back} {}
88+
void Reinitialize() { gotAnything_ = false; }
9689
template <typename A> void GetResult(A *p, int zeroBasedDim = -1) {
9790
if (zeroBasedDim >= 0) {
98-
*p = location_[zeroBasedDim] -
99-
array_.GetDimension(zeroBasedDim).LowerBound() + 1;
100-
} else {
91+
*p = gotAnything_ ? location_[zeroBasedDim] -
92+
array_.GetDimension(zeroBasedDim).LowerBound() + 1
93+
: 0;
94+
} else if (gotAnything_) {
10195
for (int j{0}; j < rank_; ++j) {
10296
p[j] = location_[j] - array_.GetDimension(j).LowerBound() + 1;
10397
}
98+
} else {
99+
// no unmasked hits? result is all zeroes
100+
for (int j{0}; j < rank_; ++j) {
101+
p[j] = 0;
102+
}
104103
}
105104
}
106105
template <typename IGNORED> bool AccumulateAt(const SubscriptValue at[]) {
107106
if (equality_(array_, at, target_)) {
107+
gotAnything_ = true;
108108
for (int j{0}; j < rank_; ++j) {
109109
location_[j] = at[j];
110110
}
@@ -119,6 +119,7 @@ template <typename EQUALITY> class LocationAccumulator {
119119
const Descriptor &target_;
120120
const bool back_{false};
121121
const int rank_{array_.rank()};
122+
bool gotAnything_{false};
122123
SubscriptValue location_[maxRank];
123124
const EQUALITY equality_{};
124125
};

0 commit comments

Comments
 (0)