Home

Awesome

P4 ➡ LLVM IR ➡ JSON

P4LLVM is an LLVM based compiler for P4, a networking language.

This project is built upon open-source P4 compiler called p4c. The LLVM IR emitter is a pass over p4c IR. We reuse the p4c front-end and translate p4c IR to LLVM IR.

If you use P4LLVM, please cite the following paper:

@inproceedings{2018p4llvm,
  title={P4LLVM: An LLVM Based P4 Compiler},
  author={Dangeti, Tharun Kumar and S, Venkata Keerthy and Upadrasta, Ramakrishna},
  booktitle={2018 IEEE 26th International Conference on Network Protocols (ICNP)},
  pages={424--429},
  year={2018},
  organization={IEEE}
}

Supporting materials can be found here

Image

Getting started

Install LLVM from source. We are using LLVM 7.0, not tested on the other versions.

Either do make install after make or export the path of llvm binaries.

Clone the P4lang repository. It includes submodules, so be sure to use --recursive to pull them in:

git clone --recursive https://github.com/IITH-Compilers/p4lang.git

If you forget to use --recursive, you can update the submodules at any time using:

git submodule update --init --recursive

Install dependencies.

Dependencies for this repository are same as the p4c compiler. We are listing them here for ease.

On Ubuntu you can use:

sudo apt-get install g++ git automake libtool libgc-dev bison flex libfl-dev libgmp-dev libboost-dev libboost-iostreams-dev libboost-graph-dev pkg-config python python-scapy python-ipaddr tcpdump cmake

Install protobuf 3.2.0
Build P4LLVM in a subdirectory named build.

How to run?

Once make is successful, execute p4c-llbm2-ss in build directory to run the code. For example,

./p4c-llbm2-ss ../p4lang/testdata/p4_16_samples/arith-bmv2.p4

This would create arith2-bmv2.p4.ll, a file with LLVM IR equivalent of arith2-bmv2.p4 and arith2-bmv2.p4.ll.json, a JSON file to target BMV2 compiler. arith2-bmv2.p4.ll.json file would be found under P4_16_sample directory.

Running with optimization

While running p4c-llbm2-ss, --optimize flag can be used to perform oz optimization of LLVM. For example,

./p4c-llbm2-ss --optimize ../p4lang/testdata/p4_16_samples/arith-bmv2.p4