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

transformations: (convert-riscv-to-llvm) #2468

Merged
merged 21 commits into from
Nov 12, 2024
Merged

Conversation

jorendumoulin
Copy link
Collaborator

This PR implements a conversion of riscv dialect to llvm using the inline assembly op.
This is useful such that any generated RISC-V code can also be used with an LLVM target like we use in snax-mlir.
Specifically, this will be used to reuse the lowering of snrt to riscv, without having to duplicate the entire snrt-to-riscv transform.

@AntonLydike

@jorendumoulin jorendumoulin added enhancement New feature or request transformations Changes or adds a transformatio backend Compiler backend in xDSL labels Apr 17, 2024
@jorendumoulin jorendumoulin self-assigned this Apr 17, 2024
@jorendumoulin
Copy link
Collaborator Author

Some further reflection on this approach:

It is functional, but the amount of unrealized conversion casts gets a bit ridiculous (see KULeuven-MICAS/snax-mlir#116). Also, the emission of riscv arithmetic in llvm inline assembly instead of regular llvm ir is a bit silly (not in the example).

I think the following approach would make more sense:

Create ops for the custom snitch operations (such as dmdst, dmsrc, dmstrd, ...) in the snrt dialect as snrt primitives, which still operate on basic mlir types (i32, i64) as opposed to riscv registers.

refactor the convert-snrt-to-riscv pass to first lower to the snrt primitives, and only later convert them to actual riscv operations. This makes the alternative path to llvm much easier and as such we can also avoid the unrealized conversion casts entirely.

The lowerings would look like this:

              snrt                                                   
              │                                                      
              │ lower-snrt                                           
              │                                                      
              │                                                      
              ▼                                                      
              arith + snrt primitives                                
                              │                                      
              │               │                                      
              │               │                                      
   ┌──────────┘               └───────┐                              
   │  convert-snrt-to-riscv           │  convert-snrt-to-llvm        
   │  convert-arith-to-riscv          │  convert-arith-to-llvm (MLIR)
   ▼                                  ▼                              
riscv                          llvm + llvm inline asm                

I can work on this, but I would like to get your opinion on this first @AntonLydike @JosseVanDelm .

@JosseVanDelm
Copy link
Collaborator

@jorendumoulin I agree with your suggestion.
Implementing the regular arithmetic directly as riscv operations feels like premature lowering, I think we have support for those arith to riscv lowerings in xdsl anyway.

CC @cappadokes

@cappadokes
Copy link
Collaborator

cappadokes commented Apr 17, 2024

Flattering your tag as may be @JosseVanDelm, I find Joren's mentioning of Anton much more meaningful. I don't have the knowledge to constructively comment on this discussion--but thanks again for considering me.

AntonLydike added a commit that referenced this pull request Jun 27, 2024
…tions (#2784)

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.

---------

Co-authored-by: Joren Dumoulin <[email protected]>
@AntonLydike
Copy link
Collaborator

Oh no, we hit the magic interactive tests again :(

@AntonLydike
Copy link
Collaborator

@jorendumoulin I did a pass to handle named registers in puts differently. We now just print the register name in the assembly instead of going through the SSA value.

Additionally, with #2797 we can now CSE delete the remaining reg_register ops left over by this pass.

I'll fix the problem with ops that return a riscv.reg<zero> register tomorrow, and that should hopefully make this PR complete.

@superlopuh
Copy link
Member

What's the latest on this?

Copy link

codecov bot commented Jul 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.19%. Comparing base (c5a1c4e) to head (eb4b558).
Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2468      +/-   ##
==========================================
+ Coverage   90.18%   90.19%   +0.01%     
==========================================
  Files         456      458       +2     
  Lines       57567    57705     +138     
  Branches     5541     5560      +19     
==========================================
+ Hits        51917    52049     +132     
- Misses       4191     4194       +3     
- Partials     1459     1462       +3     

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

@AntonLydike AntonLydike marked this pull request as ready for review July 18, 2024 09:22
@superlopuh
Copy link
Member

No longer [WIP]?

@AntonLydike
Copy link
Collaborator

Yes, this is now ready for your all eyes

Copy link
Collaborator Author

@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.

Indeed, no longer WIP

Thank you, this looks good to me!

xdsl/transforms/convert_riscv_to_llvm.py Outdated Show resolved Hide resolved
xdsl/transforms/convert_riscv_to_llvm.py Outdated Show resolved Hide resolved
xdsl/transforms/convert_riscv_to_llvm.py Outdated Show resolved Hide resolved
xdsl/transforms/convert_riscv_to_llvm.py Outdated Show resolved Hide resolved
@jorendumoulin jorendumoulin changed the title [WIP] transformations: (convert-riscv-to-llvm) transformations: (convert-riscv-to-llvm) Jul 18, 2024
@superlopuh
Copy link
Member

@jorendumoulin, I think if you could update this and get it in, it would be fabulous, I'm sure we'll find more use-cases for this functionality

@jorendumoulin
Copy link
Collaborator Author

I rebased and resolved all outstanding comments, I just don't really get what is meant with this final comment:

won't this break when returning 0?

constraints_string = ",".join(constraints)

# construct llvm inline asm op
register_width_int = builtin.IntegerType(self.xlen)
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 index type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

llvm dialect doesn't like index type

Copy link
Member

Choose a reason for hiding this comment

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

!!

@superlopuh
Copy link
Member

I rebased and resolved all outstanding comments, I just don't really get what is meant with this final comment:

won't this break when returning 0?

I think it may have been outdated, before the get registers for zero... I'm not 100% sure, I can no longer spot an issue.

@jorendumoulin
Copy link
Collaborator Author

Thanks! Apologies for the extreme delay on this! I hope I'll soon find some time to convert our snitch runtime stuff to use this flow :)

@jorendumoulin jorendumoulin merged commit 343dcb2 into main Nov 12, 2024
15 checks passed
@jorendumoulin jorendumoulin deleted the Joren/riscv-to-llvm branch November 12, 2024 15:19
@superlopuh
Copy link
Member

The important thing is that we got there in the end, thank you very much for picking it back up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Compiler backend in xDSL enhancement New feature or request transformations Changes or adds a transformatio
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants