-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[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
[Flang] Check if two ArrayConstructor's are Equal #121181
Conversation
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
@llvm/pr-subscribers-flang-openmp @llvm/pr-subscribers-flang-fir-hlfir Author: Thirumalai Shaktivel (Thirumalai-Shaktivel) ChangesThis also includes comparing the two ImpliedDo Details
Fixes: #104526 Full diff: https://github.com/llvm/llvm-project/pull/121181.diff 2 Files Affected:
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
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
5588a93
to
06b6e83
Compare
ping for review |
There was a problem hiding this 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.
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); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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)
)
@jeanPerier I have addressed your comments. |
There was a problem hiding this 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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should use isEqual
.
There was a problem hiding this comment.
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
checkCharacterType = x.LEN() == y.LEN(); | |
checkCharacterType = isEqual(x.LEN(), y.LEN()); |
or Should I add a isEqual for the Fortran::common::TypeCategory::Character
category?
There was a problem hiding this comment.
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
Thank you for the review! |
I will resolve conflicts and merge this PR, thank you, @jeanPerier for the review! |
This also includes comparing the two ImpliedDo
Details
elements and type
upper, stride and values
Fixes: #104526