Rust and GCC, two different ways
rustc_codegen_gcc
First up was Antoni Boucher to talk about the rustc_codegen_gcc
project.
The rustc compiler, he started, is based on LLVM; among other things, that
means that it does not support all of the architectures that GCC supports.
What LLVM does have
Rustc does have, though, an API that allows plugging in an
alternative code generator. This API can be used to plug in the GCC
code-generation machinery via libgccjit. That is the
approach taken by rustc_codegen_gcc.
Why would this be a useful thing to do? Boucher said that the Rust
language is increasing in popularity, but it needs support for more
architectures than LLVM can provide. The Rust for Linux work, in
particular, is highlighting this issue, but there are a lot of other users
out there too. Developers of embedded systems would benefit from better
architecture support, as would the Firefox browser.
A number of Rust features are supported by rustc_codegen_gcc now, including basic and aggregate types, variables, functions, atomics, thread-local storage, inline assembly, numerous intrinsics, and more. The compiler has support in the Compiler Explorer. The libcore tests pass, as do most of the user-interface tests. As an experiment, this compiler has been used to build Rust code for the m68k architecture; this work is still in an early stage, Boucher said, but it shows that it is indeed possible to build Rust programs for platforms that LLVM does not support.
There are still some problems to solve. A number of attributes still need support, as does generation of debug information. The quality of the generated code is not always the best. More work must be done to support new architectures. Link-time optimization is not yet supported, and so on. This work has also required a set of changes to libgccjit, most of which are still in review.
There are some other issues too, including the need to use a patched version of GCC until all of the changes have been merged upstream. Even then, there will be a need to backport those patches to allow the use of older GCC versions, which is important to be able to compile the kernel.
Even so, this project seems reasonably well advanced. Boucher noted that there was an active pull request to add rustc_codegen_gcc to the rustc compiler, but that's no longer true; that merge was done on September 29.
Native GCC
Philip Herron then took over to talk about the native GCC front end for Rust, known as gccrs. Rather than being a hybrid of LLVM and GCC, this compiler is a full implementation of the Rust language in the GNU toolchain. This work is written in C++ (easier to bootstrap, he said) and is intended to become a part of mainline GCC. It uses the existing binutils, and reuses the official Rust libraries (such as libcore, libstd, and libproc).
Once again, the speaker raised the question of "why?". He likes big
projects, he said, so this one looked attractive. It makes for an
interesting contrast with how the problems are solved in LLVM, and is a
good opportunity to see how GCC handles a modern, high-level language.
Once the work is done, it will be useful to compare the results in
terms of code size, register allocation, and energy efficiency.
There are a lot of benefits that come from having an independent implementation of Rust, he said. Tight integration with GCC will be useful for a number of projects, which will be able to benefit from GCC plugins as well. A GCC-based Rust compiler will make it easier to bootstrap rustc on new platforms. Support for link-time optimization, which tends not to work well in mixed-compiler situations, should be improved. And, of course, GCC brings support for more target architectures.
Work toward Rust support in GCC got started back in 2014, Herron said, but then it stalled out; the language was evolving too fast for the GCC developers to keep up with it. This effort was then restarted in 2019; the recent interest in Rust for the kernel is helping to drive this project. Various companies, including Open Source Security and Embecosm, are supporting development of the GCC-base Rust compiler. There is a detailed plan in place for a "minimum viable product" compiler to be released by the end of 2022.
Thus far, working support exists for core data structures and most control flow, though some of the control-flow work is still in progress. Generics and trait resolution work. Future work includes macros, imports, unstable features, and intrinsics. Amusingly, the current compiler can build "hello world", but it requires using unsafe code; the lack of macros means that println!() is unavailable and the C printf() function must be called instead.
Work planned for further in the future includes the borrow checker, which will be done in cooperation with the Polonius project. Incremental compilation is on the list, as is backporting the front-end to older GCC versions. Longer term, it is hoped that this work will help to drive Rust compiler compatibility testing in general.
The video
of these talks is available on YouTube.
| Index entries for this article | |
|---|---|
| Conference | Linux Plumbers Conference/2021 |
