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

dialects: (riscv) remove Register class #1281

Merged
merged 17 commits into from Jul 18, 2023
Merged

Conversation

superlopuh
Copy link
Member

@superlopuh superlopuh commented Jul 15, 2023

When I initially wrote the register types, the reasoning in my mind was that I wanted an optional register name, and Data cannot accept an optional generic parameter. But actually an empty string seems more appropriate than None anyway. Now that we have floats in riscv, and some duplication, it feels like a good idea to simplify. This also includes a rename of RegisterType to RegisterAttr, since we don't exclusively use them as types.

After some discussions with @kingiler on the changes in #1269, I thought I'd play around with the code to see how to make the generics work for both Register/FloatRegister and RegisterType/FloatRegisterType. There were always some issues with it locally, and quite a lot of complexity due to the extra step of indirection. This is what led me to the changes in this PR, which I propose as an alternative to #1269. It's probably a good idea for reviewers to take a look at both to have a better view of multiple potential ways forward.

@codecov
Copy link

codecov bot commented Jul 15, 2023

Codecov Report

Patch coverage: 82.89% and project coverage change: -0.01 ⚠️

Comparison is base (ec75bbd) 89.50% compared to head (b53503b) 89.50%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1281      +/-   ##
==========================================
- Coverage   89.50%   89.50%   -0.01%     
==========================================
  Files         184      184              
  Lines       24270    24250      -20     
  Branches     3682     3676       -6     
==========================================
- Hits        21722    21704      -18     
+ Misses       1967     1966       -1     
+ Partials      581      580       -1     
Impacted Files Coverage Δ
...dsl/backend/riscv/lowering/riscv_arith_lowering.py 98.29% <ø> (ø)
xdsl/dialects/riscv.py 83.70% <80.00%> (-0.14%) ⬇️
tests/dialects/test_riscv.py 100.00% <100.00%> (ø)
tests/interpreters/test_riscv_func_interpreter.py 100.00% <100.00%> (ø)
xdsl/backend/riscv/register_allocation.py 91.20% <100.00%> (ø)
xdsl/dialects/riscv_func.py 78.12% <100.00%> (ø)
xdsl/dialects/snitch.py 94.00% <100.00%> (ø)
xdsl/transforms/lower_riscv_func.py 97.43% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@webmiche webmiche changed the title dialects: (xdsl) remove Register class dialects: (riscv) remove Register class Jul 15, 2023
immediate: AnyIntegerAttr | LabelAttr = attr_def(AnyIntegerAttr | LabelAttr)

def __init__(
self,
immediate: int | AnyIntegerAttr | str | LabelAttr,
*,
rd: RegisterType | Register | None = None,
rd: RegisterAttr | str | None = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use of str may lost errors that can catch by type checkers in previous implementation. Can you revert it?

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 still don't see the benefit of the type checking here.

Copy link
Member Author

Choose a reason for hiding this comment

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

If a client wants to give a bogus string then so be it, I don't see the problem. Almost all the time they're expected to use one of the RegisterAttr instances in Registers or None. I'd rather remove str than add back Register TBH, but I also don't see it as a serious potential risk.

Copy link
Member Author

Choose a reason for hiding this comment

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

BTW this is pretty much equivalent to allowing int instead of an IntegerAttr, there can be type mismatches, but we allow it for convenience's sake

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think so. At least IntegerAttr has a verifier.

Copy link
Member Author

Choose a reason for hiding this comment

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

So does this class

Copy link
Contributor

Choose a reason for hiding this comment

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

I just went over the codebase and the one location where this matters is xdsl/transforms/lower_riscv_func.py:

            ops.append(
                riscv.MVOp(
                    arg,
-                    rd=riscv.Register(f"a{i}"),
+                    rd=f"a{i}",
                )
            )

as well as similar changes in this very same file. Looking into the implementation of Register, I see that Register also did not verify the name of the Register that was passed in its initializer. Hence, the current change seems to just implement what was already there. If there is a more typesafe way to write lower_riscv_func.py, we could explore that in the future, but I would suggest to remain permissive for now. In the spirit of reducing feature-creep in our PRs, maybe lets get this one in and then get the custom printing in next?

@@ -550,15 +531,15 @@ def __init__(
self,
immediate: int | AnyIntegerAttr | str | LabelAttr,
*,
rd: RegisterType | Register | None = None,
rd: RegisterAttr | str | None = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as here.

@@ -636,7 +617,7 @@ def __init__(
rs1: Operation | SSAValue,
immediate: int | AnyIntegerAttr | str | LabelAttr,
*,
rd: RegisterType | Register | None = None,
rd: RegisterAttr | str | None = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above

Copy link
Collaborator

@compor compor left a comment

Choose a reason for hiding this comment

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

I like this better:

  • unallocated is exposed in the API and the internal representation can be anything
  • it's shorter to instantiate
  • and more intuitive, since the type is an attribute

xdsl/dialects/riscv.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@AntonLydike AntonLydike left a comment

Choose a reason for hiding this comment

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

LGTM overall, just not sure the name change from type to attr is necessary?

xdsl/dialects/riscv.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@kingiler kingiler left a comment

Choose a reason for hiding this comment

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

LGTM

xdsl/dialects/riscv.py Outdated Show resolved Hide resolved
@superlopuh superlopuh requested a review from kingiler July 17, 2023 10:15
Copy link
Collaborator

@kingiler kingiler left a comment

Choose a reason for hiding this comment

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

At least IntergerAttr has a verifier.

@superlopuh superlopuh requested a review from kingiler July 17, 2023 10:21
xdsl/dialects/riscv.py Outdated Show resolved Hide resolved
xdsl/dialects/riscv.py Show resolved Hide resolved
@superlopuh superlopuh requested a review from kingiler July 17, 2023 14:24
@tobiasgrosser
Copy link
Contributor

@superlopuh @kingiler and all, this looks very nice. I did a round of reviews and hope these help us to move forward with this patch (without sacrificing code quality).

Copy link
Contributor

@tobiasgrosser tobiasgrosser left a comment

Choose a reason for hiding this comment

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

This LGTM. @superlopuh could you address my comments if that feels right to you and @kingiler could you check if after the comments have been addressed this PR is good-to-go?

@superlopuh
Copy link
Member Author

superlopuh commented Jul 18, 2023

@kingiler I've addressed all the comments, would be good to merge this soon. Are there any other changes that you think I should make before merging?

@superlopuh superlopuh merged commit b9fb446 into main Jul 18, 2023
10 checks passed
@superlopuh superlopuh deleted the sasha/wm/register-attr branch July 18, 2023 08:10
@kingiler
Copy link
Collaborator

LGTM

@tobiasgrosser
Copy link
Contributor

Thank you all.

1 similar comment
@tobiasgrosser
Copy link
Contributor

Thank you all.

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

Successfully merging this pull request may close these issues.

None yet

6 participants