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
[stdlib] SIMD conformance to EqualityComparable #2412
base: nightly
Are you sure you want to change the base?
Conversation
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 wouldn't be against some more details (in the docs or something) since this seems a bit like black magic and using return types for overload resolution :)
Maybe the docstring would be a good place to explain how to actually use this overload?
Yeah that might be good. I'm not even sure this is something that would get pulled because of that, but figured i'd give it a shot since it's just a few lines and gives expected behavior |
816851c
to
725c356
Compare
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.
This is a pretty neat way to make SIMD
conform to EqualityComparable
, props to your cleverness!
This seems like something we should do in the short-term until we have conditional conformance and other things. Accepting this PR would unlock a bunch of other future nice things. To start with, we could then push this EqualityComparable
requirement into CollectionElement
, which would then mean we can now implement __eq__
and __ne__
for collections. Further, it would allow us to simplify things like List.count
and List.index
which currently use an ad-hoc composite trait to ensure the given type implements __eq__
since that isn’t a requirement of CollectionElement
.
I've added this PR to bring up with the team on Monday during our internal design discussions, and I'll circle back after we meet. But this is super cool, thanks for pushing on this!
stdlib/test/builtin/test_simd.mojo
Outdated
|
||
assert_equal(s1 == s1, SIMD[DType.bool, 2](True, True), "s1 == s1") | ||
assert_equal(s1 != s1, SIMD[DType.bool, 2](False, False), "s1 != s1") | ||
assert_equal(eq(s1, s1), True, "eq(s1, s1)") |
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.
Suggestion Use assert_true
in this line and assert_false
in the line below rather than assert_equal
with True
and False
, respectively? The intent will be clearer IMO. Ditto elsewhere in this file where that pattern shows up.
E.g.
assert_true(eq(s1, s1), "eq(s1, s1)")
or even drop eq
and friends entirely in favor of:
assert_true(s1 == s1, "s1 == s1")
What do you think? Is your intent of the existence of eq
and ne
to ensure SIMD
conforms to EqualityComparable
trait, or something else? If so, it's not needed as the compiler will ensure SIMD
conforms to the trait since you explicitly said SIMD
shall conform to it in its trait list. If it didn't implement the necessary requirements, mojo
would issue a hard error at compile time.
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.
The first should work, but i think the second one might cause issues.
however i should be able to do assert_equal(s1, s1)
and assert_not_equal(s1, s2)
The downside of using this trick:
the behavior of !=
changes slightly, depending if you're in the context of EqualityComparable
(because __bool__
will always use reduce_and
), so i was using the ne
function to ensure that.
I'll try it out in a lil bit though
sora pointed out that this would not be confluent under generalization, and we agreed that for now, |
we also agree that being explicit about fn __bool__(self: SIMD[type, 1]) -> Bool:
... |
ok this feel like the right way to go
|
constrain SIMD.__bool__() to size=1 change to explicit reduce for SIMD Signed-off-by: Max Brylski <helehex@gmail.com>
2dcd097
to
fa43162
Compare
You might need to coordinate with @leandrolcampos a bit because of #2375. |
I'm still not sure this should or will get pulled. It feels good writing simd code like this. You get a compiler error when converting a |
@helehex IMHO, the foot gun mitigation probably already made it worth it. |
another trade-off that should be mentioned: |
Thanks for pushing on this! I think this is a general direction we want to go with, but there's a couple things going on in this PR I'd recommend we decompose/split up into separate PRs. Specifically, I'd recommend
@abduld I'm also curious to get your general thoughts here as I know I've brought up my concerns with the existing |
everyone i've talked to likes how |
I agree we need a proper proposal though. |
removing the bool constructor wasn't necessary, but it helped weed out all the cases of implicit reduce and is also very much related |
it finally occurred to me that |
This allows
SIMD
to conform toEqualityComparable
, without losing any of the original behavior.It uses the 4th overload resolution rule to give the new methods lower precedence, while still conforming to
EqualityComparable