[flang-commits] [flang] baec06a - [flang] Fold calls to ISHFTC()

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Jul 22 18:22:08 PDT 2022


Author: Peter Klausler
Date: 2022-07-22T18:21:57-07:00
New Revision: baec06a9d499c76635378f570166066b522d4a62

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

LOG: [flang] Fold calls to ISHFTC()

The integer arithmetic template supports ISHFTC() but the
integer intrinsic folding code had yet to call it; finish
the job.

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

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

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

Removed: 
    


################################################################################
diff  --git a/flang/lib/Evaluate/fold-integer.cpp b/flang/lib/Evaluate/fold-integer.cpp
index eb8f0461ecb6..438424a81762 100644
--- a/flang/lib/Evaluate/fold-integer.cpp
+++ b/flang/lib/Evaluate/fold-integer.cpp
@@ -742,6 +742,26 @@ Expr<Type<TypeCategory::Integer, KIND>> FoldIntrinsicFunction(
           }
           return i.ISHFT(posVal);
         }));
+  } else if (name == "ishftc") {
+    if (args.at(2)) { // SIZE= is present
+      return FoldElementalIntrinsic<T, T, Int4, Int4>(context,
+          std::move(funcRef),
+          ScalarFunc<T, T, Int4, Int4>(
+              [&](const Scalar<T> &i, const Scalar<Int4> &shift,
+                  const Scalar<Int4> &size) -> Scalar<T> {
+                // Errors are caught in intrinsics.cpp
+                auto shiftVal{static_cast<int>(shift.ToInt64())};
+                auto sizeVal{static_cast<int>(size.ToInt64())};
+                return i.ISHFTC(shiftVal, sizeVal);
+              }));
+    } else { // no SIZE=
+      return FoldElementalIntrinsic<T, T, Int4>(context, std::move(funcRef),
+          ScalarFunc<T, T, Int4>(
+              [&](const Scalar<T> &i, const Scalar<Int4> &count) -> Scalar<T> {
+                auto countVal{static_cast<int>(count.ToInt64())};
+                return i.ISHFTC(countVal);
+              }));
+    }
   } else if (name == "lbound") {
     return LBOUND(context, std::move(funcRef));
   } else if (name == "leadz" || name == "trailz" || name == "poppar" ||
@@ -1053,7 +1073,7 @@ Expr<Type<TypeCategory::Integer, KIND>> FoldIntrinsicFunction(
   } else if (name == "ubound") {
     return UBOUND(context, std::move(funcRef));
   }
-  // TODO: dot_product, ishftc, matmul, sign
+  // TODO: dot_product, matmul, sign
   return Expr<T>{std::move(funcRef)};
 }
 

diff  --git a/flang/test/Evaluate/fold-ishftc.f90 b/flang/test/Evaluate/fold-ishftc.f90
new file mode 100644
index 000000000000..35c6bf3283b7
--- /dev/null
+++ b/flang/test/Evaluate/fold-ishftc.f90
@@ -0,0 +1,14 @@
+! RUN: %python %S/test_folding.py %s %flang_fc1
+! Tests folding of ISHFTC
+module m
+  integer, parameter :: shift8s(*) = ishftc(257, shift = [(ict, ict = -9, 9)], 8)
+  integer, parameter :: expect1(*) = 256 + [128, 1, 2, 4, 8, 16, 32, 64, 128, &
+                                            1, 2, 4, 8, 16, 32, 64, 128, 1, 2]
+  logical, parameter :: test_1 = all(shift8s == expect1)
+  integer, parameter :: sizes(*) = [(ishftc(257, ict, [(isz, isz = 1, 8)]), ict = -1, 1)]
+  integer, parameter :: expect2(*) = 256 + [[1, 2, 4, 8, 16, 32, 64, 128], &
+                                            [(1, j = 1, 8)], &
+                                            [1, (2, j = 2, 8)]]
+  logical, parameter :: test_2 = all(sizes == expect2)
+end module
+


        


More information about the flang-commits mailing list