[flang-commits] [flang] 160da73 - [Flang] Check if two ArrayConstructor's are Equal (#121181)

via flang-commits flang-commits at lists.llvm.org
Wed Feb 19 01:51:35 PST 2025


Author: Thirumalai Shaktivel
Date: 2025-02-19T15:21:31+05:30
New Revision: 160da73b8583b2f94cba81b220710684a9590da3

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

LOG: [Flang] Check if two ArrayConstructor's are Equal (#121181)

This also includes comparing the two ImpliedDo

Details
- For ArrayConstructor, check if x and y have the same
  elements and type
- For ImpliedDo, check if x and y have the same lower,
  upper, stride and values

Fixes: https://github.com/llvm/llvm-project/issues/104526

Added: 
    

Modified: 
    flang/lib/Lower/Support/Utils.cpp
    flang/test/Lower/OpenMP/atomic-update.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/Support/Utils.cpp b/flang/lib/Lower/Support/Utils.cpp
index 5a9a839330364..ed2700c42fc55 100644
--- a/flang/lib/Lower/Support/Utils.cpp
+++ b/flang/lib/Lower/Support/Utils.cpp
@@ -478,9 +478,47 @@ class IsEqualEvaluateExpr {
     return isEqual(x.proc(), y.proc()) && isEqual(x.arguments(), y.arguments());
   }
   template <typename A>
+  static bool isEqual(const Fortran::evaluate::ImpliedDo<A> &x,
+                      const Fortran::evaluate::ImpliedDo<A> &y) {
+    return isEqual(x.values(), y.values()) && isEqual(x.lower(), y.lower()) &&
+           isEqual(x.upper(), y.upper()) && isEqual(x.stride(), y.stride());
+  }
+  template <typename A>
+  static bool isEqual(const Fortran::evaluate::ArrayConstructorValues<A> &x,
+                      const Fortran::evaluate::ArrayConstructorValues<A> &y) {
+    using Expr = Fortran::evaluate::Expr<A>;
+    using ImpliedDo = Fortran::evaluate::ImpliedDo<A>;
+    for (const auto &[xValue, yValue] : llvm::zip(x, y)) {
+      bool checkElement = Fortran::common::visit(
+          common::visitors{
+              [&](const Expr &v, const Expr &w) { return isEqual(v, w); },
+              [&](const ImpliedDo &v, const ImpliedDo &w) {
+                return isEqual(v, w);
+              },
+              [&](const Expr &, const ImpliedDo &) { return false; },
+              [&](const ImpliedDo &, const Expr &) { return false; },
+          },
+          xValue.u, yValue.u);
+      if (!checkElement) {
+        return false;
+      }
+    }
+    return true;
+  }
+  static bool isEqual(const Fortran::evaluate::SubscriptInteger &x,
+                      const Fortran::evaluate::SubscriptInteger &y) {
+    return x == y;
+  }
+  template <typename A>
   static bool isEqual(const Fortran::evaluate::ArrayConstructor<A> &x,
                       const Fortran::evaluate::ArrayConstructor<A> &y) {
-    llvm::report_fatal_error("not implemented");
+    bool checkCharacterType = true;
+    if constexpr (A::category == Fortran::common::TypeCategory::Character) {
+      checkCharacterType = isEqual(*x.LEN(), *y.LEN());
+    }
+    using Base = Fortran::evaluate::ArrayConstructorValues<A>;
+    return isEqual((Base)x, (Base)y) &&
+           (x.GetType() == y.GetType() && checkCharacterType);
   }
   static bool isEqual(const Fortran::evaluate::ImpliedDoIndex &x,
                       const Fortran::evaluate::ImpliedDoIndex &y) {

diff  --git a/flang/test/Lower/OpenMP/atomic-update.f90 b/flang/test/Lower/OpenMP/atomic-update.f90
index 16dae9d5f301c..7d04745015faa 100644
--- a/flang/test/Lower/OpenMP/atomic-update.f90
+++ b/flang/test/Lower/OpenMP/atomic-update.f90
@@ -185,4 +185,19 @@ program OmpAtomicUpdate
   !$omp atomic update
     w = max(w,x,y,z)
 
+!CHECK:  %[[IMP_DO:.*]] = hlfir.elemental %{{.*}} unordered : (!fir.shape<1>) -> !hlfir.expr<?xi32> {
+!CHECK:  ^bb0(%{{.*}}: index):
+!          [...]
+!CHECK:    %[[ADD_I1:.*]] = arith.addi {{.*}} : i32
+!CHECK:    hlfir.yield_element %[[ADD_I1]] : i32
+!CHECK:  }
+!        [...]
+!CHECK:  %[[SUM:.*]] = hlfir.sum %[[IMP_DO]]
+!CHECK:  omp.atomic.update %[[VAL_X_DECLARE]]#1 : !fir.ref<i32> {
+!CHECK:  ^bb0(%[[ARG0:.*]]: i32):
+!CHECK:    %[[ADD_I2:.*]] = arith.addi %[[ARG0]], %[[SUM]] : i32
+!CHECK:    omp.yield(%[[ADD_I2]] : i32)
+!CHECK:  }
+  !$omp atomic update
+    x = x + sum([ (y+2, y=1, z) ])
 end program OmpAtomicUpdate


        


More information about the flang-commits mailing list