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) Add HasInsntrait and implement trait for some operations #2784

Merged
merged 7 commits into from
Jun 27, 2024

Conversation

AntonLydike
Copy link
Collaborator

In order to make progress on #2468 I factored out the insn representation. This will hopefully also make it usable for the RISC-V backend efforts.

@AntonLydike AntonLydike added dialects Changes on the dialects backend Compiler backend in xDSL labels Jun 26, 2024
@AntonLydike AntonLydike self-assigned this Jun 26, 2024
@AntonLydike AntonLydike changed the title Add HasInsntrait and implement trait for some operations dialects: (riscv) Add HasInsntrait and implement trait for some operations Jun 26, 2024
Copy link

codecov bot commented Jun 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.79%. Comparing base (9d9bbdd) to head (d73b3c8).
Report is 16 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2784      +/-   ##
==========================================
- Coverage   89.80%   89.79%   -0.01%     
==========================================
  Files         381      388       +7     
  Lines       48226    48548     +322     
  Branches     7377     7445      +68     
==========================================
+ Hits        43308    43595     +287     
- Misses       3765     3785      +20     
- Partials     1153     1168      +15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jorendumoulin
Copy link
Collaborator

If these functions are still implemented as risc-v ops, does that not undo the progress of pr #2784 ? As these ops operate on riscv-register operands in comparison to just i32 or index variables, we will have to do an arith-to-riscv pass anyway or insert a lot of UnrealizedConversionCastOps instead. Why not implement them as operations in the snrt dialect (where the operands are not risc-v registers) where this trait could be used?

@AntonLydike
Copy link
Collaborator Author

If these functions are still implemented as risc-v ops, does that not undo the progress of pr #2784 ?

I think you linked to the wrong PR here.

As these ops operate on riscv-register operands in comparison to just i32 or index variables, we will have to do an arith-to-riscv pass anyway or insert a lot of UnrealizedConversionCastOps instead.

I don't know if I can quite follow. The riscv backend of @superlopuh is already able to convert arith to riscv dialect, but for the snax work we want to go through the llvm dialect, to which arith can also already be lowered.

Why not implement them as operations in the snrt dialect (where the operands are not risc-v registers) where this trait could be used?

Because snrt dialect ops map to multiple individual instructions, making it impractical to carry this information imo.

With this plan we gain both:

  • A more flexible conversion pattern that converts riscv-to-llvm
  • The ability to convert riscv operations to .insns further down the line in the riscv backend, so that that project can move away from a custom riscv toolchain as well.

@@ -462,6 +463,8 @@ class DMSourceOp(IRDLOperation, RISCVInstruction):
ptrlo = operand_def(riscv.IntRegisterType)
ptrhi = operand_def(riscv.IntRegisterType)

traits = frozenset([StaticInsnsRepresentation(".insn r 0x2b, 0, 0, x0, {0}, {1}")])
Copy link
Member

Choose a reason for hiding this comment

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

What would be the best way to test this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I have tested this in verilator simulations, and once this is in we can convert the tests in ci to use this system over at snax-mlir.
Without simulations, I'm not sure.

Copy link
Member

Choose a reason for hiding this comment

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

Here a pytest test that checks that these operations have the exact string you would expect would be good, just to make sure that something in the formatting infrastructure doesn't break, for example

Copy link
Collaborator

@jorendumoulin jorendumoulin left a comment

Choose a reason for hiding this comment

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

Okay, here again I wast just a bit worried about the unrealized conversian cast resolution

xdsl/backend/riscv/traits.py Outdated Show resolved Hide resolved
xdsl/backend/riscv/traits.py Outdated Show resolved Hide resolved
xdsl/traits.py Outdated Show resolved Hide resolved
xdsl/traits.py Outdated Show resolved Hide resolved
Comment on lines 32 to 35
def test_insn_repr(op: RISCVInstruction):
trait = op.get_trait(HasInsnRepresentation)
assert trait is not None
assert trait.get_insn(op) == ground_truth[op.name[13:]]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Pyright complains that: Method "get_insn" cannot be called because it is abstract and unimplemented (reportGeneralTypeIssues)

But get_insn is an abstract method on HasInsnRepresentation, which itself is ABC, so I can never get it "not implemented" as I will always get a subclass of this trait (that by definition has to implement the method). How the heck am I supposed to fix this? @superlopuh @math-fehr

Copy link
Member

Choose a reason for hiding this comment

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

I think it's because the get_insn type parameter is invariant, meaning it says that it returns only instances of this type exactly, not subclasses. I think we want the covariant version instead.

Copy link
Member

Choose a reason for hiding this comment

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

Turns out I was wrong, it was indeed a Pyright bug

xdsl/traits.py Show resolved Hide resolved
@AntonLydike AntonLydike merged commit 772df11 into main Jun 27, 2024
10 checks passed
@AntonLydike AntonLydike deleted the anton/add-insns-trait branch June 27, 2024 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Compiler backend in xDSL dialects Changes on the dialects
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants