bjorn3 | a75f9bc | 2021-02-01 09:11:46 | [diff] [blame] | 1 | # Cranelift codegen backend for rust |
bjorn3 | d0ef7f4 | 2018-07-18 09:55:32 | [diff] [blame] | 2 | |
bjorn3 | 77f74ed | 2020-12-27 09:30:38 | [diff] [blame] | 3 | The 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). |
bjorn3 | 285c7c6 | 2020-11-03 10:00:04 | [diff] [blame] | 4 | This has the potential to improve compilation times in debug mode. |
| 5 | If your project doesn't use any of the things listed under "Not yet supported", it should work fine. |
| 6 | If not please open an issue. |
bjorn3 | 58ffc47 | 2020-06-25 16:01:55 | [diff] [blame] | 7 | |
bjorn3 | dfc669b | 2023-11-16 21:15:07 | [diff] [blame] | 8 | ## Download using Rustup |
| 9 | |
| 10 | The Cranelift codegen backend is distributed in nightly builds on Linux and x86_64 macOS. If you want to |
| 11 | install it using Rustup, you can do that by running: |
| 12 | |
| 13 | ```bash |
| 14 | $ rustup component add rustc-codegen-cranelift-preview --toolchain nightly |
| 15 | ``` |
| 16 | |
| 17 | Once 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` |
bjorn3 | dfc669b | 2023-11-16 21:15:07 | [diff] [blame] | 19 | - 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 | |
| 38 | You can also download a pre-built version from the [releases] page. |
| 39 | Extract the `dist` directory in the archive anywhere you want. |
| 40 | If 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 | |
bjorn3 | 285c7c6 | 2020-11-03 10:00:04 | [diff] [blame] | 45 | ## Building and testing |
bjorn3 | d0ef7f4 | 2018-07-18 09:55:32 | [diff] [blame] | 46 | |
bjorn3 | dfc669b | 2023-11-16 21:15:07 | [diff] [blame] | 47 | If you want to build the backend manually, you can download it from GitHub and build it yourself: |
| 48 | |
bjorn3 | d0ef7f4 | 2018-07-18 09:55:32 | [diff] [blame] | 49 | ```bash |
bjorn3 | a302610 | 2023-10-24 12:22:23 | [diff] [blame] | 50 | $ git clone https://github.com/rust-lang/rustc_codegen_cranelift |
bjorn3 | d0ef7f4 | 2018-07-18 09:55:32 | [diff] [blame] | 51 | $ cd rustc_codegen_cranelift |
bjorn3 | 81ea0b2 | 2023-06-15 17:56:01 | [diff] [blame] | 52 | $ ./y.sh build |
bjorn3 | d0ef7f4 | 2018-07-18 09:55:32 | [diff] [blame] | 53 | ``` |
| 54 | |
bjorn3 | 285c7c6 | 2020-11-03 10:00:04 | [diff] [blame] | 55 | To run the test suite replace the last command with: |
| 56 | |
| 57 | ```bash |
bjorn3 | e173b71 | 2025-02-26 11:05:26 | [diff] [blame] | 58 | $ ./y.sh prepare # only needs to be run the first time |
bjorn3 | 285c7c6 | 2020-11-03 10:00:04 | [diff] [blame] | 59 | $ ./test.sh |
| 60 | ``` |
| 61 | |
bjorn3 | 81ea0b2 | 2023-06-15 17:56:01 | [diff] [blame] | 62 | For 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 | |
bjorn3 | 0061bb7 | 2024-01-26 18:33:45 | [diff] [blame] | 64 | ## 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|❓|❓|❓|❓| |
bjorn3 | c9b0075 | 2024-06-30 17:09:46 | [diff] [blame] | 72 | |macOS|✅|✅|N/A|N/A| |
bjorn3 | 060811a | 2024-08-10 19:23:03 | [diff] [blame] | 73 | |Windows|✅|❌|N/A|N/A| |
bjorn3 | 0061bb7 | 2024-01-26 18:33:45 | [diff] [blame] | 74 | |
| 75 | ✅: Fully supported and tested |
| 76 | ❓: Maybe supported, not tested |
| 77 | ❌: Not supported at all |
| 78 | |
| 79 | Not 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. |
bjorn3 | 0061bb7 | 2024-01-26 18:33:45 | [diff] [blame] | 82 | [^no-rustup]: Not available as rustup component for nightly. You can build it yourself. |
| 83 | |
bjorn3 | d0ef7f4 | 2018-07-18 09:55:32 | [diff] [blame] | 84 | ## Usage |
| 85 | |
Vitaly Shukela | eda840a | 2020-06-25 15:59:46 | [diff] [blame] | 86 | rustc_codegen_cranelift can be used as a near-drop-in replacement for `cargo build` or `cargo run` for existing projects. |
| 87 | |
bjorn3 | 81ea0b2 | 2023-06-15 17:56:01 | [diff] [blame] | 88 | Assuming `$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`). |
bjorn3 | 52a43e9 | 2019-01-26 16:03:35 | [diff] [blame] | 89 | |
Vitaly Shukela | eda840a | 2020-06-25 15:59:46 | [diff] [blame] | 90 | In the directory with your project (where you can do the usual `cargo build`), run: |
| 91 | |
bjorn3 | 78d3911 | 2019-09-12 18:27:10 | [diff] [blame] | 92 | ```bash |
bjorn3 | 98a276b | 2022-12-14 18:30:46 | [diff] [blame] | 93 | $ $cg_clif_dir/dist/cargo-clif build |
bjorn3 | 78d3911 | 2019-09-12 18:27:10 | [diff] [blame] | 94 | ``` |
| 95 | |
bjorn3 | 07968a0 | 2021-03-29 08:45:09 | [diff] [blame] | 96 | This will build your project with rustc_codegen_cranelift instead of the usual LLVM backend. |
Vitaly Shukela | eda840a | 2020-06-25 15:59:46 | [diff] [blame] | 97 | |
bjorn3 | 07968a0 | 2021-03-29 08:45:09 | [diff] [blame] | 98 | For additional ways to use rustc_codegen_cranelift like the JIT mode see [usage.md](docs/usage.md). |
bjorn3 | f8add19 | 2020-05-05 10:06:15 | [diff] [blame] | 99 | |
AngelicosPhosphoros | 8aa3eba | 2023-05-21 17:53:02 | [diff] [blame] | 100 | ## Building and testing with changes in rustc code |
| 101 | |
bjorn3 | f91bd78 | 2024-04-05 16:20:23 | [diff] [blame] | 102 | See [rustc_testing.md](docs/rustc_testing.md). |
bjorn3 | 0061bb7 | 2024-01-26 18:33:45 | [diff] [blame] | 103 | |
bjorn3 | d0ef7f4 | 2018-07-18 09:55:32 | [diff] [blame] | 104 | ## Not yet supported |
| 105 | |
bjorn3 | a302610 | 2023-10-24 12:22:23 | [diff] [blame] | 106 | * SIMD ([tracked here](https://github.com/rust-lang/rustc_codegen_cranelift/issues/171), `std::simd` fully works, `std::arch` is partially supported) |
bjorn3 | b67610f | 2023-01-24 17:56:42 | [diff] [blame] | 107 | * Unwinding on panics ([no cranelift support](https://github.com/bytecodealliance/wasmtime/issues/1677), `-Cpanic=abort` is enabled by default) |
bjorn3 | 07968a0 | 2021-03-29 08:45:09 | [diff] [blame] | 108 | |
| 109 | ## License |
| 110 | |
| 111 | Licensed 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 | |
| 118 | at your option. |
| 119 | |
| 120 | ### Contribution |
| 121 | |
| 122 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 123 | for inclusion in the work by you shall be dual licensed as above, without any |
| 124 | additional terms or conditions. |