blob: 28edb5795ce3c4802634cc660c9d0dfa9b3938a4 [file] [log] [blame] [view]
bjorn3a75f9bc2021-02-01 09:11:461# Cranelift codegen backend for rust
bjorn3d0ef7f42018-07-18 09:55:322
bjorn377f74ed2020-12-27 09:30:383The goal of this project is to create an alternative codegen backend for the rust compiler based on [Cranelift](https://github.com/bytecodealliance/wasmtime/blob/main/cranelift).
bjorn3285c7c62020-11-03 10:00:044This has the potential to improve compilation times in debug mode.
5If your project doesn't use any of the things listed under "Not yet supported", it should work fine.
6If not please open an issue.
bjorn358ffc472020-06-25 16:01:557
bjorn3dfc669b2023-11-16 21:15:078## Download using Rustup
9
10The Cranelift codegen backend is distributed in nightly builds on Linux and x86_64 macOS. If you want to
11install it using Rustup, you can do that by running:
12
13```bash
14$ rustup component add rustc-codegen-cranelift-preview --toolchain nightly
15```
16
17Once it is installed, you can enable it with one of the following approaches:
18- `CARGO_PROFILE_DEV_CODEGEN_BACKEND=cranelift cargo +nightly build -Zcodegen-backend`
bjorn3dfc669b2023-11-16 21:15:0719- Add the following to `.cargo/config.toml`:
20 ```toml
21 [unstable]
22 codegen-backend = true
23
24 [profile.dev]
25 codegen-backend = "cranelift"
26 ```
27- Add the following to `Cargo.toml`:
28 ```toml
29 # This line needs to come before anything else in Cargo.toml
30 cargo-features = ["codegen-backend"]
31
32 [profile.dev]
33 codegen-backend = "cranelift"
34 ```
35
36## Precompiled builds
37
38You can also download a pre-built version from the [releases] page.
39Extract the `dist` directory in the archive anywhere you want.
40If you want to use `cargo clif build` instead of having to specify the full path to the `cargo-clif` executable, you can add the `bin` subdirectory of the extracted `dist` directory to your `PATH`.
41(tutorial [for Windows](https://stackoverflow.com/a/44272417), and [for Linux/MacOS](https://unix.stackexchange.com/questions/26047/how-to-correctly-add-a-path-to-path/26059#26059)).
42
43[releases]: https://github.com/rust-lang/rustc_codegen_cranelift/releases/tag/dev
44
bjorn3285c7c62020-11-03 10:00:0445## Building and testing
bjorn3d0ef7f42018-07-18 09:55:3246
bjorn3dfc669b2023-11-16 21:15:0747If you want to build the backend manually, you can download it from GitHub and build it yourself:
48
bjorn3d0ef7f42018-07-18 09:55:3249```bash
bjorn3a3026102023-10-24 12:22:2350$ git clone https://github.com/rust-lang/rustc_codegen_cranelift
bjorn3d0ef7f42018-07-18 09:55:3251$ cd rustc_codegen_cranelift
bjorn381ea0b22023-06-15 17:56:0152$ ./y.sh build
bjorn3d0ef7f42018-07-18 09:55:3253```
54
bjorn3285c7c62020-11-03 10:00:0455To run the test suite replace the last command with:
56
57```bash
bjorn3e173b712025-02-26 11:05:2658$ ./y.sh prepare # only needs to be run the first time
bjorn3285c7c62020-11-03 10:00:0459$ ./test.sh
60```
61
bjorn381ea0b22023-06-15 17:56:0162For more docs on how to build and test see [build_system/usage.txt](build_system/usage.txt) or the help message of `./y.sh`.
63
bjorn30061bb72024-01-26 18:33:4564## Platform support
65
66|OS \ architecture|x86\_64|AArch64|Riscv64|s390x (System-Z)|
67|---|---|---|---|---|
68|Linux|✅|✅|✅[^no-rustup]|✅[^no-rustup]|
69|FreeBSD|✅[^no-rustup]|❓|❓|❓|
70|AIX|❌[^xcoff]|N/A|N/A|❌[^xcoff]|
71|Other unixes|❓|❓|❓|❓|
bjorn3c9b00752024-06-30 17:09:4672|macOS|✅|✅|N/A|N/A|
bjorn3060811a2024-08-10 19:23:0373|Windows|✅|❌|N/A|N/A|
bjorn30061bb72024-01-26 18:33:4574
75✅: Fully supported and tested
76❓: Maybe supported, not tested
77❌: Not supported at all
78
79Not all targets are available as rustup component for nightly. See notes in the platform support matrix.
80
81[^xcoff]: XCOFF object file format is not supported.
bjorn30061bb72024-01-26 18:33:4582[^no-rustup]: Not available as rustup component for nightly. You can build it yourself.
83
bjorn3d0ef7f42018-07-18 09:55:3284## Usage
85
Vitaly Shukelaeda840a2020-06-25 15:59:4686rustc_codegen_cranelift can be used as a near-drop-in replacement for `cargo build` or `cargo run` for existing projects.
87
bjorn381ea0b22023-06-15 17:56:0188Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`y.sh prepare` and `y.sh build` or `test.sh`).
bjorn352a43e92019-01-26 16:03:3589
Vitaly Shukelaeda840a2020-06-25 15:59:4690In the directory with your project (where you can do the usual `cargo build`), run:
91
bjorn378d39112019-09-12 18:27:1092```bash
bjorn398a276b2022-12-14 18:30:4693$ $cg_clif_dir/dist/cargo-clif build
bjorn378d39112019-09-12 18:27:1094```
95
bjorn307968a02021-03-29 08:45:0996This will build your project with rustc_codegen_cranelift instead of the usual LLVM backend.
Vitaly Shukelaeda840a2020-06-25 15:59:4697
bjorn307968a02021-03-29 08:45:0998For additional ways to use rustc_codegen_cranelift like the JIT mode see [usage.md](docs/usage.md).
bjorn3f8add192020-05-05 10:06:1599
AngelicosPhosphoros8aa3eba2023-05-21 17:53:02100## Building and testing with changes in rustc code
101
bjorn3f91bd782024-04-05 16:20:23102See [rustc_testing.md](docs/rustc_testing.md).
bjorn30061bb72024-01-26 18:33:45103
bjorn3d0ef7f42018-07-18 09:55:32104## Not yet supported
105
bjorn3a3026102023-10-24 12:22:23106* SIMD ([tracked here](https://github.com/rust-lang/rustc_codegen_cranelift/issues/171), `std::simd` fully works, `std::arch` is partially supported)
bjorn3b67610f2023-01-24 17:56:42107* Unwinding on panics ([no cranelift support](https://github.com/bytecodealliance/wasmtime/issues/1677), `-Cpanic=abort` is enabled by default)
bjorn307968a02021-03-29 08:45:09108
109## License
110
111Licensed under either of
112
113 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
114 http://www.apache.org/licenses/LICENSE-2.0)
115 * MIT license ([LICENSE-MIT](LICENSE-MIT) or
116 http://opensource.org/licenses/MIT)
117
118at your option.
119
120### Contribution
121
122Unless you explicitly state otherwise, any contribution intentionally submitted
123for inclusion in the work by you shall be dual licensed as above, without any
124additional terms or conditions.