1# WIP libgccjit codegen backend for rust23[](https://web.libera.chat/#rustc_codegen_gcc)4[](https://matrix.to/#/#rustc_codegen_gcc:matrix.org)56This is a GCC codegen for rustc, which means it can be loaded by the existing rustc frontend, but benefits from GCC: more architectures are supported and GCC's optimizations are used.78**Despite its name, libgccjit can be used for ahead-of-time compilation, as is used here.**910## Motivation1112The primary goal of this project is to be able to compile Rust code on platforms unsupported by LLVM.13A secondary goal is to check if using the gcc backend will provide any run-time speed improvement for the programs compiled using rustc.1415## Getting Started1617Note: **This requires a patched libgccjit in order to work.18You need to use my [fork of gcc](https://github.com/rust-lang/gcc) which already includes these patches.**19The default configuration (see below in the [Quick start](#quick-start) section) will download a `libgccjit` built in the CI that already contains these patches, so you don't need to build this fork yourself if you use the default configuration.2021### Dependencies2223- rustup: follow instructions on the [official website](https://rustup.rs)24- consider to install DejaGnu which is necessary for running the libgccjit test suite. [website](https://www.gnu.org/software/dejagnu/#downloading)25- additional packages: `flex`, `libmpfr-dev`, `libgmp-dev`, `libmpc3`, `libmpc-dev`26 27### Quick start28291. Clone and configure the repository:30 ```bash31 git clone https://github.com/rust-lang/rustc_codegen_gcc32 cd rustc_codegen_gcc33 cp config.example.toml config.toml34 ```35362. Build and test:37 ```bash38 ./y.sh prepare # downloads and patches sysroot39 ./y.sh build --sysroot --release40 41 # Verify setup with a simple test42 ./y.sh cargo build --manifest-path tests/hello-world/Cargo.toml43 44 # Run full test suite (expect ~100 failing UI tests)45 ./y.sh test --release46 ```4748If you don't need to test GCC patches you wrote in our GCC fork, then the default configuration should49be all you need. You can update the `rustc_codegen_gcc` without worrying about GCC.5051### Building with your own GCC version5253If you wrote a patch for GCC and want to test it with this backend, you will need54to do a few more things.5556To build it (most of these instructions come from [here](https://gcc.gnu.org/onlinedocs/jit/internals/index.html), so don't hesitate to take a look there if you encounter an issue):5758```bash59$ git clone https://github.com/rust-lang/gcc60$ sudo apt install flex libmpfr-dev libgmp-dev libmpc3 libmpc-dev61$ mkdir gcc-build gcc-install62$ cd gcc-build63$ ../gcc/configure \64 --enable-host-shared \65 --enable-languages=jit \66 --enable-checking=release \ # it enables extra checks which allow to find bugs67 --disable-bootstrap \68 --disable-multilib \69 --prefix=$(pwd)/../gcc-install70$ make -j4 # You can replace `4` with another number depending on how many cores you have.71```7273If you want to run libgccjit tests, you will need to74* Enable the C++ language in the `configure` step:7576```bash77--enable-languages=jit,c++78```79* Install [dejagnu](https://www.gnu.org/software/dejagnu/#downloading) to run the tests:8081```bash82$ sudo apt install dejagnu83```8485Then to run libgccjit tests:8687```bash88$ cd gcc # from the `gcc-build` folder89$ make check-jit90# To run one specific test:91$ make check-jit RUNTESTFLAGS="-v -v -v jit.exp=jit.dg/test-asm.cc"92```9394**Put the path to your custom build of libgccjit in the file `config.toml`.**9596You now need to set the `gcc-path` value in `config.toml` with the result of this command:9798```bash99$ dirname $(readlink -f `find . -name libgccjit.so`)100```101102and to comment the `download-gccjit` setting:103104```toml105gcc-path = "[MY PATH]"106# download-gccjit = true107```108109Then you can run commands like this:110111```bash112$ ./y.sh prepare # download and patch sysroot src and install hyperfine for benchmarking113$ ./y.sh build --sysroot --release114```115116To run the tests:117118```bash119$ ./y.sh test --release120```121122## Usage123124You have to run these commands, in the corresponding order:125126```bash127$ ./y.sh prepare128$ ./y.sh build --sysroot129```130To check if all is working correctly, run:131132 ```bash133$ ./y.sh cargo build --manifest-path tests/hello-world/Cargo.toml134```135136### Cargo137138```bash139$ CHANNEL="release" $CG_GCCJIT_DIR/y.sh cargo run140```141142If you compiled cg_gccjit in debug mode (aka you didn't pass `--release` to `./y.sh test`) you should use `CHANNEL="debug"` instead or omit `CHANNEL="release"` completely.143144### Rustc145146If you want to run `rustc` directly, you can do so with:147148```bash149$ ./y.sh rustc my_crate.rs150```151152You can do the same manually (although we don't recommend it):153154```bash155$ LIBRARY_PATH="[gcc-path value]" LD_LIBRARY_PATH="[gcc-path value]" rustc +$(cat $CG_GCCJIT_DIR/rust-toolchain | grep 'channel' | cut -d '=' -f 2 | sed 's/"//g' | sed 's/ //g') -Cpanic=abort -Zcodegen-backend=$CG_GCCJIT_DIR/target/release/librustc_codegen_gcc.so --sysroot $CG_GCCJIT_DIR/build_sysroot/sysroot my_crate.rs156```157158## Environment variables159160 * _**CG_GCCJIT_DUMP_ALL_MODULES**_: Enables dumping of all compilation modules. When set to "1", a dump is created for each module during compilation and stored in `/tmp/reproducers/`.161 * _**CG_GCCJIT_DUMP_MODULE**_: Enables dumping of a specific module. When set with the module name, e.g., `CG_GCCJIT_DUMP_MODULE=module_name`, a dump of that specific module is created in `/tmp/reproducers/`.162 * _**CG_RUSTFLAGS**_: Send additional flags to rustc. Can be used to build the sysroot without unwinding by setting `CG_RUSTFLAGS=-Cpanic=abort`.163 * _**CG_GCCJIT_DUMP_TO_FILE**_: Dump a C-like representation to /tmp/gccjit_dumps and enable debug info in order to debug this C-like representation.164 * _**CG_GCCJIT_DUMP_RTL**_: Dumps RTL (Register Transfer Language) for virtual registers.165 * _**CG_GCCJIT_DUMP_RTL_ALL**_: Dumps all RTL passes.166 * _**CG_GCCJIT_DUMP_TREE_ALL**_: Dumps all tree (GIMPLE) passes.167 * _**CG_GCCJIT_DUMP_IPA_ALL**_: Dumps all Interprocedural Analysis (IPA) passes.168 * _**CG_GCCJIT_DUMP_CODE**_: Dumps the final generated code.169 * _**CG_GCCJIT_DUMP_GIMPLE**_: Dumps the initial GIMPLE representation.170 * _**CG_GCCJIT_DUMP_EVERYTHING**_: Enables dumping of all intermediate representations and passes.171 * _**CG_GCCJIT_KEEP_INTERMEDIATES**_: Keeps intermediate files generated during the compilation process.172 * _**CG_GCCJIT_VERBOSE**_: Enables verbose output from the GCC driver.173174## Extra documentation175176More specific documentation is available in the [`doc`](./doc) folder:177178 * [Common errors](./doc/errors.md)179 * [Debugging GCC LTO](./doc/debugging-gcc-lto.md)180 * [Debugging libgccjit](./doc/debugging-libgccjit.md)181 * [Git subtree sync](./doc/subtree.md)182 * [List of useful commands](./doc/tips.md)183 * [Send a patch to GCC](./doc/sending-gcc-patch.md)184185## Licensing186187While this crate is licensed under a dual Apache/MIT license, it links to `libgccjit` which is under the GPLv3+ and thus, the resulting toolchain (rustc + GCC codegen) will need to be released under the GPL license.188189However, programs compiled with `rustc_codegen_gcc` do not need to be released under a GPL license.
Findings
✓ No findings reported for this file.