Skip to content

[Flang] Check if two ArrayConstructor's are Equal #121181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 19, 2025

Conversation

Thirumalai-Shaktivel
Copy link
Member

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: #104526

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
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:openmp labels Dec 27, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 27, 2024

@llvm/pr-subscribers-flang-openmp

@llvm/pr-subscribers-flang-fir-hlfir

Author: Thirumalai Shaktivel (Thirumalai-Shaktivel)

Changes

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: #104526


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

2 Files Affected:

  • (modified) flang/include/flang/Lower/Support/Utils.h (+44-1)
  • (modified) flang/test/Lower/OpenMP/atomic-update.f90 (+15)
diff --git a/flang/include/flang/Lower/Support/Utils.h b/flang/include/flang/Lower/Support/Utils.h
index 1cc74521e22d88..b2f3673f9164cb 100644
--- a/flang/include/flang/Lower/Support/Utils.h
+++ b/flang/include/flang/Lower/Support/Utils.h
@@ -545,9 +545,52 @@ 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/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

Copy link

github-actions bot commented Dec 27, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@Thirumalai-Shaktivel
Copy link
Member Author

Thirumalai-Shaktivel commented Jan 10, 2025

ping for review
@tblah, @Leporacanthicus, @kiranchandramohan

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this, few comments inlined.

Comment on lines 552 to 561
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

return false;
}
}
return x.GetType() == y.GetType();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need a special case for characters for the length_ field that is not reflected in the GetType() (you can put that check under something like if constexpr (A::category == Fortran::common::TypeCategory::Character))

@Thirumalai-Shaktivel
Copy link
Member Author

@jeanPerier I have addressed your comments.

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit, LGTM otherwise, thanks for addressing my comments.

llvm::report_fatal_error("not implemented");
bool checkCharacterType = true;
if constexpr (A::category == Fortran::common::TypeCategory::Character) {
checkCharacterType = x.LEN() == y.LEN();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should use isEqual.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused, sorry about that.

Are you suggesting the following

Suggested change
checkCharacterType = x.LEN() == y.LEN();
checkCharacterType = isEqual(x.LEN(), y.LEN());

or Should I add a isEqual for the Fortran::common::TypeCategory::Character category?

Copy link
Member Author

@Thirumalai-Shaktivel Thirumalai-Shaktivel Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the first approach and implemented it: 6442b33, please let me know this works

@Thirumalai-Shaktivel
Copy link
Member Author

Thank you for the review!

@Thirumalai-Shaktivel
Copy link
Member Author

Thirumalai-Shaktivel commented Feb 19, 2025

I will resolve conflicts and merge this PR, thank you, @jeanPerier for the review!

@Thirumalai-Shaktivel Thirumalai-Shaktivel merged commit 160da73 into llvm:main Feb 19, 2025
5 of 7 checks passed
@Thirumalai-Shaktivel Thirumalai-Shaktivel deleted the llvm/atomic_01 branch February 19, 2025 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang:openmp flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Flang][OpenMP] ICE with "LLVM ERROR: not implemented" (omp atomic)
3 participants