TNS
VOXPOP
How has the recent turmoil within the OpenAI offices changed your plans to use GPT in a business process or product in 2024?
Increased uncertainty means we are more likely to evaluate alternative AI chatbots and LLMs.
0%
No change in plans, though we will keep an eye on the situation.
0%
With Sam Altman back in charge, we are more likely to go all-in with GPT and LLMs.
0%
What recent turmoil?
0%
C++ / Linux / Open Source / Rust

Rust Support Is Being Built into the GNU GCC Compiler

Rust can benefit from the entire ecosystem of GCC tools and plugins created over GCC's 35-year history.
Mar 19th, 2023 7:53am by
Featued image for: Rust Support Is Being Built into the GNU GCC Compiler

At this year’s FOSDEM conference, which was held in Brussels, compiler engineer Arthur Cohen delivered a talk on a project based on a single-minded dream: to compile Rust code with the 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.

Extra Ecosystems

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.

Contributors Everywhere

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.

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.”

Cross Pollination

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!”

gccrs mugs - screenshot from FOSDEM 2023 talk by Arthur Cohen - Rust frontend for GCC.


WebReduce

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.