]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/Readme.md
Rollup merge of #80983 - bjorn3:no_dup_is_dllimport_foreign_item, r=nagisa
[rust.git] / compiler / rustc_codegen_cranelift / Readme.md
1 # WIP Cranelift codegen backend for rust
2
3 > ⚠⚠⚠ Certain kinds of FFI don't work yet. ⚠⚠⚠
4
5 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).
6 This has the potential to improve compilation times in debug mode.
7 If your project doesn't use any of the things listed under "Not yet supported", it should work fine.
8 If not please open an issue.
9
10 ## Building and testing
11
12 ```bash
13 $ git clone https://github.com/bjorn3/rustc_codegen_cranelift.git
14 $ cd rustc_codegen_cranelift
15 $ ./prepare.sh # download and patch sysroot src and install hyperfine for benchmarking
16 $ ./build.sh
17 ```
18
19 To run the test suite replace the last command with:
20
21 ```bash
22 $ ./test.sh
23 ```
24
25 This will implicitly build cg_clif too. Both `build.sh` and `test.sh` accept a `--debug` argument to
26 build in debug mode.
27
28 Alternatively you can download a pre built version from [GHA]. It is listed in the artifacts section
29 of workflow runs. Unfortunately due to GHA restrictions you need to be logged in to access it.
30
31 [GHA]: https://github.com/bjorn3/rustc_codegen_cranelift/actions?query=branch%3Amaster+event%3Apush+is%3Asuccess
32
33 ## Usage
34
35 rustc_codegen_cranelift can be used as a near-drop-in replacement for `cargo build` or `cargo run` for existing projects.
36
37 Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`prepare.sh` and `build.sh` or `test.sh`).
38
39 ### Cargo
40
41 In the directory with your project (where you can do the usual `cargo build`), run:
42
43 ```bash
44 $ $cg_clif_dir/build/cargo.sh run
45 ```
46
47 This should build and run your project with rustc_codegen_cranelift instead of the usual LLVM backend.
48
49 ### Rustc
50
51 > You should prefer using the Cargo method.
52
53 ```bash
54 $ $cg_clif_dir/build/bin/cg_clif my_crate.rs
55 ```
56
57 ### Jit mode
58
59 In jit mode cg_clif will immediately execute your code without creating an executable file.
60
61 > This requires all dependencies to be available as dynamic library.
62 > The jit mode will probably need cargo integration to make this possible.
63
64 ```bash
65 $ $cg_clif_dir/build/cargo.sh jit
66 ```
67
68 or
69
70 ```bash
71 $ $cg_clif_dir/build/bin/cg_clif -Cllvm-args=mode=jit -Cprefer-dynamic my_crate.rs
72 ```
73
74 There is also an experimental lazy jit mode. In this mode functions are only compiled once they are
75 first called. It currently does not work with multi-threaded programs. When a not yet compiled
76 function is called from another thread than the main thread, you will get an ICE.
77
78 ```bash
79 $ $cg_clif_dir/build/cargo.sh lazy-jit
80 ```
81
82 ### Shell
83
84 These are a few functions that allow you to easily run rust code from the shell using cg_clif as jit.
85
86 ```bash
87 function jit_naked() {
88     echo "$@" | $cg_clif_dir/build/bin/cg_clif - -Cllvm-args=mode=jit -Cprefer-dynamic
89 }
90
91 function jit() {
92     jit_naked "fn main() { $@ }"
93 }
94
95 function jit_calc() {
96     jit 'println!("0x{:x}", ' $@ ');';
97 }
98 ```
99
100 ## Env vars
101
102 [see env_vars.md](docs/env_vars.md)
103
104 ## Not yet supported
105
106 * Good non-rust abi support ([several problems](https://github.com/bjorn3/rustc_codegen_cranelift/issues/10))
107 * Inline assembly ([no cranelift support](https://github.com/bytecodealliance/wasmtime/issues/1041)
108     * On Linux there is support for invoking an external assembler for `global_asm!` and `asm!`.
109       `llvm_asm!` will remain unimplemented forever. `asm!` doesn't yet support reg classes. You
110       have to specify specific registers instead.
111 * SIMD ([tracked here](https://github.com/bjorn3/rustc_codegen_cranelift/issues/171), some basic things work)