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

Thirumalai Shaktivel via flang-commits flang-commits at lists.llvm.org
Thu Dec 26 22:16:00 PST 2024


https://github.com/Thirumalai-Shaktivel updated https://github.com/llvm/llvm-project/pull/121181

>From f62a81ea481b1f283e1caa55ded4c590137b951a Mon Sep 17 00:00:00 2001
From: Thirumalai-Shaktivel <thirumalaishaktivel at gmail.com>
Date: Fri, 27 Dec 2024 05:21:24 +0000
Subject: [PATCH 1/2] [Flang] Check if two ArrayConstructor's are Equal This
 also includes comparing the two ImpliedDo

- 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
---
 flang/a.out                               | Bin 0 -> 16688 bytes
 flang/include/flang/Lower/Support/Utils.h |  44 +++++++++++++++++++++-
 flang/x.f90                               |  14 +++++++
 3 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100755 flang/a.out
 create mode 100644 flang/x.f90

diff --git a/flang/a.out b/flang/a.out
new file mode 100755
index 0000000000000000000000000000000000000000..3724c07d8deb63a66c15e93cc23a3f96080a0fcf
GIT binary patch
literal 16688
zcmeHOdu&tJ89xqXgcfjUi<Z%?xK9CV9Op^Wmf=7gZl;81=$N+la%0~lrha7hH4W9;
zg at snNQYI#$ihow6owU(u)o9gqj6YUqP#zQNU at v7;|HvxnoR)QTY!!42 at AsX1KIg{R
zUH8zWNj{SA?|i at S`_ALLuXC=?`C6c>qr&A9oMws736f4%*^FN)4z{Wcz%N?Fd>n5T
zpAnyce4XTMz263C?KDv_6Dt)z3rK#|lqrIrwrRo0EhHqrQfW+8z$jEd$Zs||DY+0^
zRiTWm&)4T6XC#k2=COwikQkkQ6r24uQZ4qcoQZF$xMUTwABp65K=~a|evCe4&&ctl
z=F_i5#j_AS&{6WpqY#CDTa{lSvOOcme)Bmi^zYQSLHV_-0t at ke)jme9ZzKFDE+04X
zY2KvjD|H?QR6H5g=@hk*aBp*CZ6s6^3CB`HHA5}UHO-CQ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Lower/Support/Utils.h
index 1cc74521e22d88..5eb39a08898f16 100644
--- a/flang/include/flang/Lower/Support/Utils.h
+++ b/flang/include/flang/Lower/Support/Utils.h
@@ -545,9 +545,51 @@ 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) {
+    using Expr = Fortran::evaluate::Expr<A>;
+    for (const auto &[xValue, yValue] : llvm::zip(x.values(), y.values())) {
+      bool checkValue = Fortran::common::visit(
+          common::visitors{
+              [&](const Expr &v, const Expr &w) { return isEqual(v, w); },
+              [&](const auto &, const auto &) {
+                llvm::report_fatal_error("isEqual is not handled yet for "
+                                         "the element type in ImpliedDo");
+                return false;
+              },
+          },
+          xValue.u, yValue.u);
+      if (!checkValue) {
+        return false;
+      }
+    }
+    return isEqual(x.lower(), y.lower()) && isEqual(x.upper(), y.upper()) &&
+           isEqual(x.stride(), y.stride());
+  }
+  template <typename A>
   static bool isEqual(const Fortran::evaluate::ArrayConstructor<A> &x,
                       const Fortran::evaluate::ArrayConstructor<A> &y) {
-    llvm::report_fatal_error("not implemented");
+    for (const auto &[xValue, yValue] : llvm::zip(x, y)) {
+      using Expr = Fortran::evaluate::Expr<A>;
+      using ImpliedDo = Fortran::evaluate::ImpliedDo<A>;
+      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 auto &, const auto &) {
+                llvm::report_fatal_error("isEqual is not handled yet for "
+                                         "the element type in ImpliedDo");
+                return false;
+              },
+          },
+          xValue.u, yValue.u);
+      if (!checkElement) {
+        return false;
+      }
+    }
+    return x.GetType() == y.GetType();
   }
   static bool isEqual(const Fortran::evaluate::ImpliedDoIndex &x,
                       const Fortran::evaluate::ImpliedDoIndex &y) {
diff --git a/flang/x.f90 b/flang/x.f90
new file mode 100644
index 00000000000000..db7bf9a8f901ac
--- /dev/null
+++ b/flang/x.f90
@@ -0,0 +1,14 @@
+program s
+    integer :: j = 0, k
+    integer :: n = 2
+
+    !$omp atomic
+    j = j + fun([ (k,k=1,n) ])
+    print *, j, fun([ (k,k=1,n) ])
+
+    contains
+        function fun(i) result(res)
+            integer :: i(2)
+            res = product(i)
+        end function
+end

>From ac45e595e14ebafca6a4b46844cacff2afb95066 Mon Sep 17 00:00:00 2001
From: Thirumalai-Shaktivel <thirumalaishaktivel at gmail.com>
Date: Fri, 27 Dec 2024 05:58:14 +0000
Subject: [PATCH 2/2] [Test] Add a test

---
 flang/test/Lower/OpenMP/atomic-update.f90 | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/flang/test/Lower/OpenMP/atomic-update.f90 b/flang/test/Lower/OpenMP/atomic-update.f90
index 16dae9d5f301c1..7d04745015faab 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