During this year’s FOSDEM conference in Brussels, compiler engineer Arthur Cohen gave a talk on his team’s ambitious project to compile Rust code with the GCC compiler. Rather than using Rust’s own compiler, rustc, the team aims to create a Rust frontend for the GNU Compiler Collection, which can bring significant benefits to the Rust ecosystem. Ultimately, the project can even help with efforts to bring Rust to the Linux kernel. While the task of reimplementing a compiler from scratch is certainly challenging, the potential benefits make the project well worth the effort.
GCC is a large program containing “multiple compilers from multiple languages that all share the same backend”. The team is working to add Rust to the existing compilers for C, C++, Fortran, and other languages. Access to the first version of gccrs will be provided in GCC 13, although it is still a work in progress. The ability to compile the Rust-for-Linux code was also not yet succeeded, and the ability to run the rustc compiler test suite in the process as well.
Rust’s contemporary compiler is an interface for the LLVM toolchain that makes it possible to run Rust on various systems. Despite of this, the team expects that by using GCC, Rust can take advantage of the extensive ecosystem of GCC tools and plugins that have been developed over the past 35 years. These include security plugins, static analyzers, link time optimization, and control flow integrity checks.
GCC and the team's new compiler is written in C++ language. So, they hope that their advanced Rust interface will eventually be backported into previous versions of GCC, making it easier to bring Rust to a broader range of systems. Because GCC is older than LLVM, as a result, it has more architectures and targets.
The project began in 2014 with the sole initiator and was resuscitated in 2019 when Open Source Security and Embecosm organizations financed it. Cohen expressed gratitude to GCC and non-GCC programmers who contributed code to the project. In 2022, there were more than 50 diverse contributors, including several students. Besides, the project is still participating in Google’s Summer of Code, providing capabilities to work on things namely gccrs Unicode support, better debugging for their high-level intermediate representation code, and enhancing its mistake handling.
It is worth adding, the most important Cohen’s mission is to help the project with efforts to bring Rust to the Linux kernel. The Linux kernel utilizes GCC and its corresponding security tools, which has implications for those who want to integrate Rust code in the Linux kernel. Enabling the mixing of rustc-compiled code and GCC-built code in the Linux kernel, the compilers will be incapable to carry out an full link-time optimization pass over each of the compiled code, as a result, missing control flow integrity.
In conclusion, the gccrs project is re-implementing the Rust compiler from scratch to give Rust its own frontend for the GCC toolchain. The project aims to enable Rust to benefit from the entire ecosystem of GCC tools and plugins. So, we can only wait for these novelties. Besides, if you want to be aware of other language upgrades read our news about Visual Studio’s C++ improvements.