[flang-commits] [flang] 35cc2ec - [flang] Support FINDLOC/MAXLOC/MINLOC with scalar mask

Mike Kashkarov via flang-commits flang-commits at lists.llvm.org
Thu Apr 28 02:59:08 PDT 2022


Author: Mike Kashkarov
Date: 2022-04-28T18:59:04+09:00
New Revision: 35cc2ec4ed4a7a0862e62ad009d561f61e2d12c3

URL: https://github.com/llvm/llvm-project/commit/35cc2ec4ed4a7a0862e62ad009d561f61e2d12c3
DIFF: https://github.com/llvm/llvm-project/commit/35cc2ec4ed4a7a0862e62ad009d561f61e2d12c3.diff

LOG: [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

Added: 
    

Modified: 
    flang/lib/Evaluate/fold-integer.cpp
    flang/test/Evaluate/fold-findloc.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/fold-integer.cpp b/flang/lib/Evaluate/fold-integer.cpp
index f9cfdc7175ca..a1dbcd144c51 100644
--- a/flang/lib/Evaluate/fold-integer.cpp
+++ b/flang/lib/Evaluate/fold-integer.cpp
@@ -313,6 +313,15 @@ template <WhichLocation WHICH> class LocationHelper {
     array->SetLowerBoundsToOne();
     ConstantSubscripts at{array->lbounds()}, maskAt, resultIndices, resultShape;
     if (mask) {
+      if (auto scalarMask{mask->GetScalarValue()}) {
+        // Convert into array in case of scalar MASK= (for
+        // MAXLOC/MINLOC/FINDLOC mask should be be conformable)
+        ConstantSubscript n{GetSize(array->shape())};
+        std::vector<Scalar<LogicalResult>> mask_elements(
+            n, Scalar<LogicalResult>{scalarMask.value()});
+        *mask = Constant<LogicalResult>{
+            std::move(mask_elements), ConstantSubscripts{n}};
+      }
       mask->SetLowerBoundsToOne();
       maskAt = mask->lbounds();
     }

diff  --git a/flang/test/Evaluate/fold-findloc.f90 b/flang/test/Evaluate/fold-findloc.f90
index 5e79fce0b1c4..7297081bdfae 100644
--- a/flang/test/Evaluate/fold-findloc.f90
+++ b/flang/test/Evaluate/fold-findloc.f90
@@ -66,4 +66,19 @@ module m1
   logical, parameter :: test_char2 = all(minloc(a).eq.[1])
   logical, parameter :: test_char3 = all(maxloc(a, back=.true.).eq.[4])
   logical, parameter :: test_char4 = all(minloc(a, back=.true.).eq.[3])
+
+  ! Check with scalar MASK=
+  logical, parameter:: test_mia1_mt  = all(minloc(ia1, mask=.true.) == 1)
+  logical, parameter:: test_mia1_mtd = all(minloc(ia1, mask=.true., dim=1) == [1])
+  logical, parameter:: test_xia1_mt  = all(maxloc(ia1, mask=.true.) == 3)
+  logical, parameter:: test_xia1_mtd = all(maxloc(ia1, mask=.true., dim=1) == [3])
+  logical, parameter:: test_fia1_mt  = all(findloc(ia1, 1, mask=.true.) == 1)
+  logical, parameter:: test_fia1_mtd = all(findloc(ia1, 1, mask=.true., dim=1) == [1])
+
+  logical, parameter:: test_mia1_mf  = all(minloc(ia1, mask=.false.) == 0)
+  logical, parameter:: test_mia1_mfd = all(minloc(ia1, mask=.false., dim=1) == [0])
+  logical, parameter:: test_xia1_mf  = all(maxloc(ia1, mask=.false.) == 0)
+  logical, parameter:: test_xia1_mfd = all(maxloc(ia1, mask=.false., dim=1) == [0])
+  logical, parameter:: test_fia1_mf  = all(findloc(ia1, 1, mask=.false.) == 0)
+  logical, parameter:: test_fia1_mfd = all(findloc(ia1, 1, mask=.false., dim=1) == [0])
 end module


        


More information about the flang-commits mailing list