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

[WIP] transformations: (convert-riscv-to-llvm) #2468

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

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 <joren.dumoulin@kuleuven.be>
@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.

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.

None yet

4 participants