p4lang / project-ideas

Ideas for P4 Projects.
Apache License 2.0
6 stars 0 forks source link

P4HIR: an experimental MLIR dialect that models P4C IR #20

Open qobilidop opened 1 month ago

qobilidop commented 1 month ago

This project idea is created to invite further discussion of my 2024 P4 Workshop talk:

Proposal

P4HIR (P4 High-level IR) is an experimental MLIR dialect that aims to model the existing P4C IR as closely as possible, so it could serve as a straightforward bridge between the existing P4C infra and a potential MLIR ecosystem involving P4.

More detailed design of P4HIR will be figured out during the implementation.

Plan

Progress

fruffy commented 1 month ago

I have also been thinking of starting something like this. Partially because I wanted to learn more about MLIR-style compilers. Is the work planned to be open-source? I would consider contributing it as an extension first (and with that a separate repository), since LLVM is such a large dependency.

It might be a good idea to have this back end produce code that is executable by a target, e.g., BMv2. Not sure if that is also planned.

qobilidop commented 1 month ago

Yes, open-source is the plan! I think having a separate repo is a good idea. I've been working in a local P4C fork in a subdirectory. But it shall be straightforward to move to a separate repo.

qobilidop commented 1 month ago

It might be a good idea to have this back end produce code that is executable by a target, e.g., BMv2. Not sure if that is also planned.

Agree with this! My thought is that the BMv2 JSON format could be defined as another MLIR dialect, and the BMv2 backend could be ported to be transformations from P4HIR dialect to the BMv2 dialect, with a relatively trivial final codegen from BMv2 dialect to BMv2 JSON.

This might be a great idea for a standalone follow-up work, once we have a relatively mature P4HIR that covers enough P4C IR to be useful.

qobilidop commented 1 month ago

After talking to @asl today, I learned that there is an existing work that basically does this already: https://github.com/barinsim/p4c-mlir

Here's my summary of what this project does:

I feel this could be a pretty good starting point for further development. But as @asl suggested, maybe we should hold one step back and present a detailed design for review before diving into the implementation.

@barinsim If you are still working on this and are interested in upstreaming these work, maybe we could have some discussions?