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

via flang-commits flang-commits at lists.llvm.org
Fri Jan 10 09:06:16 PST 2025


================
@@ -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);
----------------
jeanPerier wrote:

I think this the same visitor as the one added below and I would suggest adding and isEqual version for `Fortran::evaluate::ArrayConstructorValue<A>`.

Then I think you can easily make the coverage complete using the logic below since this is not a "semantic" equality check, but a data structure check, so if one value is an ImpliedDo and the Other is an Expr, the data structure is different (even though the Fortran value of the expression may end-up being the same).

```
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;
              },
          },
          x.u, y.u);
```

Note that I suggest using explicit cases instead of auto to avoid silently breaking isEqual in the future if some new member is added to `ArrayConstructorValue`, even though that is very unlikely (auto would have been fine for an assert/TODO like you were adding).

https://github.com/llvm/llvm-project/pull/121181


More information about the flang-commits mailing list