Skip to content
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

fhdl/structure: add check for equality for _Slice #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

maass-hamburg
Copy link

add check for equality for _Slice.

@maass-hamburg
Copy link
Author

@sbourdeauducq you may want to take a look

@maass-hamburg
Copy link
Author

@m-labs please take a look

@sbourdeauducq
Copy link
Member

sbourdeauducq commented Oct 1, 2024 via email

@maass-hamburg maass-hamburg force-pushed the structure_equal_check_slice branch from 1a21b4d to b25cc2e Compare October 18, 2024 10:08
@maass-hamburg
Copy link
Author

@sbourdeauducq the reason for that change is demonstated here: enjoy-digital/litex@d63d8d9. There oe1 and oe2 could be Slices and without this change, we would get a Error.

if isinstance(self, _Operator) and self.op == "==":
a, b = self.operands
if isinstance(a, Constant) and isinstance(b, Constant):
return a.value == b.value
if isinstance(a, Signal) and isinstance(b, Signal):
return a is b
if isinstance(a, _Slice) and isinstance(b, _Slice):
return (a.value is b.value) and (a.start == b.start) and (a.stop == b.stop)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it be a.value == b.value ? You could in theory slice a Constant, or slice a slice.

@maass-hamburg maass-hamburg force-pushed the structure_equal_check_slice branch from b25cc2e to 3db32f7 Compare October 18, 2024 11:34
if isinstance(self, _Operator) and self.op == "==":
a, b = self.operands
if isinstance(a, Constant) and isinstance(b, Constant):
return a.value == b.value
if isinstance(a, Signal) and isinstance(b, Signal):
return a is b
if isinstance(a, _Slice) and isinstance(b, _Slice):
return (a.value == b.value).__bool__ and (a.start == b.start) and (a.stop == b.stop)
Copy link
Member

Choose a reason for hiding this comment

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

.__bool__ ?

Copy link
Author

Choose a reason for hiding this comment

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

without that we are returning a _Operator object. With that we are getting a boolean value. Would have used bool(a.value == b.value) and just (a.value == b.value). But these don't work

Copy link
Member

@sbourdeauducq sbourdeauducq Oct 18, 2024

Choose a reason for hiding this comment

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

.__bool__ is a function which just always evaluates to True according to Pythons lax rules. This is not working either. Wat issue do you have with just (a.value == b.value)? The recursive call should just resolve to is when it's a Signal.

Copy link
Author

Choose a reason for hiding this comment

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

(a.value == b.value) would just return an object of type _Operator. bool(a.value == b.value) now works, it forces it to resolve.

Copy link
Author

Choose a reason for hiding this comment

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

@maass-hamburg maass-hamburg force-pushed the structure_equal_check_slice branch from 3db32f7 to 7845b72 Compare November 5, 2024 09:36
@@ -29,12 +29,16 @@ class _Value(DUID):
def __bool__(self):
# Special case: Constants and Signals are part of a set or used as
# dictionary keys, and Python needs to check for equality.
# The part for the Slice has been added to do the same for Slices of
# Signals, Constants, and Slices.
Copy link
Member

Choose a reason for hiding this comment

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

Why not just edit the comment to say "Constants, Signals and Slices" ?
Also this slice comparison is a bit janky, for instance s == s[:] evaluates to False even though they are the same.

@maass-hamburg maass-hamburg force-pushed the structure_equal_check_slice branch from 7845b72 to 85321af Compare December 5, 2024 10:59
Comment on lines 40 to 43
if isinstance(a, _Slice) and (isinstance(b, Constant) or isinstance(b, Signal)):
return (bool(a.value == b) and (a.start == 0) and (a.stop == len(b)))
if isinstance(b, _Slice) and (isinstance(a, Constant) or isinstance(a, Signal)):
return (bool(b.value == a) and (b.start == 0) and (b.stop == len(a)))
Copy link
Member

Choose a reason for hiding this comment

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

Yeah well this does not cover all cases, does it?
I was not saying slice comparison should be implemented and implemented fully, I'm just pointing out that it's not easy. A careful decision needs to be made.

Copy link
Author

Choose a reason for hiding this comment

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

I think it covers all cases. if one is a slice and the other is a Constant or a Signal. The slice can even be a slice of a slice and so on.

Copy link
Member

Choose a reason for hiding this comment

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

Those work?
s[:][:] == s
s[1:2][1:2] == s[2]

Copy link
Author

Choose a reason for hiding this comment

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

s[:][:] == s

this should

s[1:2][1:2] == s[2]

this should raise a error as s[1:2] would have a len of 1 and for the second [1:2] it would need to have a len of 2.
But s[1:2][0:1] == s[2] should work and return `false

Copy link
Member

Choose a reason for hiding this comment

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

How about s[1:3][1:2] == s[2]?

Copy link
Author

Choose a reason for hiding this comment

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

I rewrote it, so it works with s[1:3][1:2] == s[2].

add check for equality for _Slice.

Signed-off-by: Fin Maaß <[email protected]>
@maass-hamburg maass-hamburg force-pushed the structure_equal_check_slice branch from 85321af to a950eb8 Compare December 5, 2024 16:09
@maass-hamburg
Copy link
Author

@sbourdeauducq I rewrote it, it should now cover all cases with slices. It now resolves each slice until it is no longer a slice and than compares them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants