Open qobilidop opened 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.
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.
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.
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:
P4
dialect. See https://github.com/barinsim/p4c-mlir/blob/main/extensions/mlir/include/P4Ops.td for an overview of the operations defined. It seems to have captured most P4 primitives.P4
dialect in https://github.com/barinsim/p4c-mlir/tree/main/extensions/mlir/p4c-mlir-translate. Also see https://github.com/barinsim/p4c-mlir/tree/main/extensions/mlir/test/translate for examples of this translation.P4
dialect to LLVM
dialect in https://github.com/barinsim/p4c-mlir/tree/main/extensions/mlir/p4dialect-llvm-translate. This part seems to be WIP.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?
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