Rust Support Is Being Built into the GNU GCC Compiler
“We’re actually reimplementing the compiler from scratch,” Cohen told the audience.
Before a roomful of Rust developers — who presumably use Rust’s own compiler, rustc — Cohen began his talk by joking that using the GNU’s open source GCC compiler “is good for you. It’s good for your health,” promising an overview of a project by a team “who was stupid enough to even think of reimplementing a Rust compiler from scratch.”
But beyond the self-deprecating humor lies an ambitious and important mission, as Cohen explained later. Giving Rust its own frontend for the GCC toolchain can bring real and tangible benefits.
And it may even help with efforts to bring Rust to the Linux kernel.
Rust’s current compiler is a frontend for the low level virtual machine (LLVM) toolchain, which can output compiled code for multiple kinds of machines. But by instead creating a frontend for the GNU Complier Collection (GCC) compiler, the team hopes Rust can benefit from the entire ecosystem of GCC tools and plugins created over GCC’s 35-year history, from security plugins and static analyzers to the plugins offering link time optimization and control flow integrity checks — not to mention the gdb debugger, the as assembler, and the linker ld.
And because their new compiler, like GCC itself, is written in C++, the team ultimately hopes to backport it into previous versions of GCC. “Hopefully that will help some systems get Rust… Because GCC is much older than LLVM, it has support for more architectures and more targets than LLVM. So technically, thanks to gccrs, you will now be able to run Rust on your favorite Soviet satellite and so on.”
Cohen began by concisely defining GCC as a big program containing “multiple compilers from multiple languages that all share the same backend — so the same sort of assembly emissions and optimizers and so on… You’ve got a C compiler, a C++ compiler, a Fortran compiler and so on…
“And we’re trying to add Rust to it.”
The first version of gccrs will be available in GCC 13 — although the tech site Phoronix noted in mid-February that it’s still a work in progress. “The borrow-checking code is notably absent right now. The support for built-ins and intrinsics is still a work in progress, and the almighty milestone of being able to compile the Rust-for-Linux code isn’t yet succeeded. The ability to also successfully run the rustc compiler test suite also remains a work in target…”
Phoronix’s conclusion? “It won’t be until next year with GCC 14 at least before this frontend will really be useful for developers.”
But progress is happening fast. Eight days later, Phoronix noted that an additional 103 patches had been included. “The project is starting to receive more and more contributions,” Cohen posted in the project’s mailing list for patches.
The work is the culmination of years of development. The project started in 2014 as a one-developer project and was revived in 2019 when two companies sponsored the project — Open Source Security and Embecosm. Cohen also expressed his thanks to GCC and non-GCC developers who contributed code to the project.
Thanks to @Embecosm and @grsecurity for sponsoring the #GCC Rust effort. The #GCC community looks forward to working with the @rustlang community and the @rust_foundation as adoption of the language accelerates. Developers welcome! Continually improving.
— GCC – GNU Toolchain (@gnutools) December 6, 2022
And Cohen’s talk pointed out that in 2022 there were more than 50 different contributors — including several students, “which I’m really proud of.” Cohen himself started working on the project during Google’s Summer of Code, and proudly notes that the project is still participating this year, offering opportunities to work on things like gccrs Unicode support, better debugging for their high-level intermediate representation code and improving its error handling.
They’re also receiving support from the Rust core team — and from GCC’s own team of developers.
But along the way, they’re also hoping to help Rust’s compiler team spot places where the language’s specification could be more clear. “We try to contribute back to the rustc project,” Cohen said, adding that gccrs “is built by people that love Rust and that want to push it forward in our own way.”
Toward the end of his talk, Cohen said that a big hope is to help efforts to bring Rust to the Linux kernel. The project’s official FAQ notes that the Linux kernel uses GCC — and its corresponding security tools — which has implications for those hoping to integrate Rust code in the Linux kernel. “If mixing rustc-compiled code and GCC-built code in the Linux kernel, the compilers will be unable to perform a full link-time optimization pass over all of the compiled code, leading to absent CFI (control flow integrity).”
But they’re also hoping for some “cross-pollination” with the Rust community. For example, Rust’s compiler team currently has a project to formalize the way Rust checks the “borrowing” of references to a variable (and its value) for use in other parts of a program. They’ve dubbed their effort “the Polonius” project (after the Shakespeare character who advised Hamlet to “neither a borrower nor a lender be.”) The gccrs team hopes to integrate those algorithms into their own compiler.
“If you’ve ever written Rust in your life… you’ve been sort of held at gunpoint by the borrow checker,” Cohen joked with his audience. “And that’s really a core part of the language experience, and we can’t really be a Rust compiler without a borrow checker.”
During his talk Cohen explained the technical details, like its parser, its abstract syntax tree, its high-level intermediate representation and “all of the extra fun Rust stuff that we have to handle, because it’s a really complex language.” The first version has now been merged into the test version of GCC 13, so “you can use it. You can start hacking on it,” Cohen told the audience, adding jokingly that in addition, “please report issues when it inevitably crashes and dies horribly.”
But his comments acknowledge a community that’s enthusiastic about the project, with dozens of developers offering feedback and support to help make their vision a reality. And the progress is continuing. Currently the gccrs team is working on supporting Rust’s constant generics.
And in another sign of creating a thriving and loyal community, Cohen told the audience at FOSDEM, “We’ve got mugs!
“If you do pull requests, we’ll send you a mug!”
- Wikipedia co-founder Jimmy Wales explained how they’re protecting their “open movement” and the larger open internet.
- How do you measure time on the moon? Standards body considers moon-orbiting atomic clocks to establish “lunar time.”
- How a retro-computing enthusiast is resurrecting the early-1990s dial-up service Prodigy.