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

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

Open
1 of 4 tasks
qobilidop opened this issue Sep 30, 2024 · 4 comments
Open
1 of 4 tasks

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

qobilidop opened this issue Sep 30, 2024 · 4 comments
Labels
bigtask A task that appears to require a big amount of work

Comments

@qobilidop
Copy link
Member

qobilidop commented Sep 30, 2024

I've been working on this project recently, and will do a lightning talk about it in the upcoming 2024 P4 Workshop. This issue is created for presenting the proposal, tracking the progress, and inviting further discussions with the community.

Proposal

P4HIR (P4 High-level IR) is an experimental MLIR dialect that aims to model the existing P4C IR by supporting bi-directional translations to and from the P4C IR. In more detail:

  1. Support P4C IR → P4HIR translation, so we can further develop MLIR-based P4C backend in the future, while reusing the existing P4C frontend.
  2. Support P4HIR → P4C IR translation, so we can further develop MLIR-based P4C frontend in the future, while reusing the existing P4C backend.
  3. A P4C IR → P4HIR → P4C IR round-trip might not result in exactly the same code, but as long as we get semantically equivalent code, this shall be fine.

The current proposal is pretty high-level. There are a lot to figure out in the technical details. I'd like to discuss with the community on those as we make progress pushing this idea forward.

Plan and progress

Other notes

P4C - MLIR concepts mapping

To give a brief introduction to MLIR for P4C developers, we can map some P4C concepts to corresponding MLIR concepts.

P4C concept MLIR concept
IR dialect
IR node operation (in regions with SSACFG semantics or graph-like semantics)
.def file .td file (ODS, TableGen)
pass pass
@fruffy fruffy added the bigtask A task that appears to require a big amount of work label Sep 30, 2024
@fruffy
Copy link
Collaborator

fruffy commented Sep 30, 2024

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
Copy link
Member Author

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
Copy link
Member Author

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
Copy link
Member Author

qobilidop commented Oct 4, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bigtask A task that appears to require a big amount of work
Projects
None yet
Development

No branches or pull requests

2 participants