[flang-commits] [flang] 2f2fce8 - [flang] Fold F08 parity intrinsic

Tarun Prabhu via flang-commits flang-commits at lists.llvm.org
Mon Aug 22 10:31:04 PDT 2022


Author: Tarun Prabhu
Date: 2022-08-22T11:29:45-06:00
New Revision: 2f2fce8eaa73d4565dfd93b5c448e3397270df91

URL: https://github.com/llvm/llvm-project/commit/2f2fce8eaa73d4565dfd93b5c448e3397270df91
DIFF: https://github.com/llvm/llvm-project/commit/2f2fce8eaa73d4565dfd93b5c448e3397270df91.diff

LOG: [flang] Fold F08 parity intrinsic

Perform compile-time folding of the F08 parity intrinsic when all parameters are compile-time constants.

Differential Revision: https://reviews.llvm.org/D129785

Added: 
    flang/test/Evaluate/fold-parity.f90

Modified: 
    flang/lib/Evaluate/fold-logical.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/fold-logical.cpp b/flang/lib/Evaluate/fold-logical.cpp
index 52c9e475e7dfe..bcf59a5d12136 100644
--- a/flang/lib/Evaluate/fold-logical.cpp
+++ b/flang/lib/Evaluate/fold-logical.cpp
@@ -22,9 +22,9 @@ static std::optional<Expr<SomeType>> ZeroExtend(const Constant<T> &c) {
       Constant<LargestInt>(std::move(exts), ConstantSubscripts(c.shape())));
 }
 
-// for ALL & ANY
+// for ALL, ANY & PARITY
 template <typename T>
-static Expr<T> FoldAllAny(FoldingContext &context, FunctionRef<T> &&ref,
+static Expr<T> FoldAllAnyParity(FoldingContext &context, FunctionRef<T> &&ref,
     Scalar<T> (Scalar<T>::*operation)(const Scalar<T> &) const,
     Scalar<T> identity) {
   static_assert(T::category == TypeCategory::Logical);
@@ -52,10 +52,10 @@ Expr<Type<TypeCategory::Logical, KIND>> FoldIntrinsicFunction(
   std::string name{intrinsic->name};
   using SameInt = Type<TypeCategory::Integer, KIND>;
   if (name == "all") {
-    return FoldAllAny(
+    return FoldAllAnyParity(
         context, std::move(funcRef), &Scalar<T>::AND, Scalar<T>{true});
   } else if (name == "any") {
-    return FoldAllAny(
+    return FoldAllAnyParity(
         context, std::move(funcRef), &Scalar<T>::OR, Scalar<T>{false});
   } else if (name == "associated") {
     bool gotConstant{true};
@@ -203,6 +203,9 @@ Expr<Type<TypeCategory::Logical, KIND>> FoldIntrinsicFunction(
     }
   } else if (name == "merge") {
     return FoldMerge<T>(context, std::move(funcRef));
+  } else if (name == "parity") {
+    return FoldAllAnyParity(
+        context, std::move(funcRef), &Scalar<T>::NEQV, Scalar<T>{false});
   } else if (name == "same_type_as") {
     // Type equality testing with SAME_TYPE_AS() ignores any type parameters.
     // Returns a constant truth value when the result is known now.

diff  --git a/flang/test/Evaluate/fold-parity.f90 b/flang/test/Evaluate/fold-parity.f90
new file mode 100644
index 0000000000000..55c369840ef49
--- /dev/null
+++ b/flang/test/Evaluate/fold-parity.f90
@@ -0,0 +1,38 @@
+! RUN: %python %S/test_folding.py %s %flang_fc1
+
+! Test fold parity intrinsic.
+
+module paritytest
+  logical, parameter :: test_1t = parity((/ .true. /))
+  logical, parameter :: test_1f = .not. parity((/ .false. /))
+
+  logical, parameter :: test_e1 = .not. parity((/ .true., .true. /))
+  logical, parameter :: test_o1 = parity((/ .true., .true., .true. /))
+  logical, parameter :: test_o12 = parity((/ .true., .true., .true., .false. /))
+
+  logical, parameter, dimension(2, 3) :: a32 = reshape((/&
+       .true., .true., .false., &
+       .true., .true., .true. &
+       /), shape(a32), order=(/2, 1/))
+
+  logical, parameter, dimension(2, 3) :: a32t = reshape((/&
+       .true., .true., .true., &
+       .true., .true., .true. &
+       /), shape(a32t))
+
+  logical, parameter, dimension(2, 3) :: a32f = reshape((/&
+       .false., .false., .false., &
+       .false., .false., .false. &
+       /), shape(a32f))
+
+  logical, parameter :: test_a32 = parity(a32)
+  logical, parameter :: test_a32t = .not. parity(a32t)
+  logical, parameter :: test_a32f = .not. parity(a32f)
+
+  logical, parameter :: test_a321 = &
+       all(parity(a32, 1) .EQV. (/ .false., .false., .true. /))
+
+  logical, parameter :: test_a322 = &
+       all(parity(a32, 2) .EQV. (/ .false., .true. /))
+
+end module paritytest


        


More information about the flang-commits mailing list