Skip to content

Commit 55b52ee

Browse files
authored
Rollup merge of #80408 - bjorn3:sync_cg_clif-2020-12-27, r=bjorn3
Sync rustc_codegen_cranelift The highlight of this sync are two JIT mode improvements. The first is that it is now possible to use JIT mode when using `-Zcodegen-backend` instead of the custom driver using `-Cllvm-args=mode=jit`. The second one is a new JIT mode that lazily compiles functions when they are called the first time: https://github.com/bjorn3/rustc_codegen_cranelift/pull/1120 In addition this includes a few small runtime performance improvements and various fixes for rustc changes that didn't cause compilation to fail. r? ``@ghost`` ``@rustbot`` label +A-codegen +A-cranelift +T-compiler
2 parents 70b1a29 + 52cf01c commit 55b52ee

28 files changed

+489
-274
lines changed

compiler/rustc_codegen_cranelift/.vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
// source for rustc_* is not included in the rust-src component; disable the errors about this
33
"rust-analyzer.diagnostics.disabled": ["unresolved-extern-crate"],
4+
"rust-analyzer.assist.importMergeBehaviour": "last",
45
"rust-analyzer.cargo.loadOutDirsFromCheck": true,
56
"rust-analyzer.linkedProjects": [
67
"./Cargo.toml",

compiler/rustc_codegen_cranelift/Cargo.lock

+28-27
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
5050
[[package]]
5151
name = "cranelift-bforest"
5252
version = "0.68.0"
53-
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
53+
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
5454
dependencies = [
5555
"cranelift-entity",
5656
]
5757

5858
[[package]]
5959
name = "cranelift-codegen"
6060
version = "0.68.0"
61-
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
61+
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
6262
dependencies = [
6363
"byteorder",
6464
"cranelift-bforest",
@@ -76,7 +76,7 @@ dependencies = [
7676
[[package]]
7777
name = "cranelift-codegen-meta"
7878
version = "0.68.0"
79-
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
79+
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
8080
dependencies = [
8181
"cranelift-codegen-shared",
8282
"cranelift-entity",
@@ -85,28 +85,46 @@ dependencies = [
8585
[[package]]
8686
name = "cranelift-codegen-shared"
8787
version = "0.68.0"
88-
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
88+
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
8989

9090
[[package]]
9191
name = "cranelift-entity"
9292
version = "0.68.0"
93-
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
93+
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
9494

9595
[[package]]
9696
name = "cranelift-frontend"
9797
version = "0.68.0"
98-
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
98+
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
9999
dependencies = [
100100
"cranelift-codegen",
101101
"log",
102102
"smallvec",
103103
"target-lexicon",
104104
]
105105

106+
[[package]]
107+
name = "cranelift-jit"
108+
version = "0.68.0"
109+
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
110+
dependencies = [
111+
"anyhow",
112+
"cranelift-codegen",
113+
"cranelift-entity",
114+
"cranelift-module",
115+
"cranelift-native",
116+
"errno",
117+
"libc",
118+
"log",
119+
"region",
120+
"target-lexicon",
121+
"winapi",
122+
]
123+
106124
[[package]]
107125
name = "cranelift-module"
108126
version = "0.68.0"
109-
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
127+
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
110128
dependencies = [
111129
"anyhow",
112130
"cranelift-codegen",
@@ -118,7 +136,7 @@ dependencies = [
118136
[[package]]
119137
name = "cranelift-native"
120138
version = "0.68.0"
121-
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
139+
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
122140
dependencies = [
123141
"cranelift-codegen",
124142
"raw-cpuid",
@@ -128,7 +146,7 @@ dependencies = [
128146
[[package]]
129147
name = "cranelift-object"
130148
version = "0.68.0"
131-
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
149+
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
132150
dependencies = [
133151
"anyhow",
134152
"cranelift-codegen",
@@ -138,23 +156,6 @@ dependencies = [
138156
"target-lexicon",
139157
]
140158

141-
[[package]]
142-
name = "cranelift-simplejit"
143-
version = "0.68.0"
144-
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
145-
dependencies = [
146-
"cranelift-codegen",
147-
"cranelift-entity",
148-
"cranelift-module",
149-
"cranelift-native",
150-
"errno",
151-
"libc",
152-
"log",
153-
"region",
154-
"target-lexicon",
155-
"winapi",
156-
]
157-
158159
[[package]]
159160
name = "crc32fast"
160161
version = "1.2.1"
@@ -325,9 +326,9 @@ dependencies = [
325326
"ar",
326327
"cranelift-codegen",
327328
"cranelift-frontend",
329+
"cranelift-jit",
328330
"cranelift-module",
329331
"cranelift-object",
330-
"cranelift-simplejit",
331332
"gimli",
332333
"indexmap",
333334
"libloading",

compiler/rustc_codegen_cranelift/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ crate-type = ["dylib"]
1212
cranelift-codegen = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", features = ["unwind"] }
1313
cranelift-frontend = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
1414
cranelift-module = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
15-
cranelift-simplejit = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", optional = true }
15+
cranelift-jit = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", optional = true }
1616
cranelift-object = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
1717
target-lexicon = "0.11.0"
1818
gimli = { version = "0.23.0", default-features = false, features = ["write"]}
@@ -27,15 +27,15 @@ libloading = { version = "0.6.0", optional = true }
2727
#cranelift-codegen = { path = "../wasmtime/cranelift/codegen" }
2828
#cranelift-frontend = { path = "../wasmtime/cranelift/frontend" }
2929
#cranelift-module = { path = "../wasmtime/cranelift/module" }
30-
#cranelift-simplejit = { path = "../wasmtime/cranelift/simplejit" }
30+
#cranelift-jit = { path = "../wasmtime/cranelift/jit" }
3131
#cranelift-object = { path = "../wasmtime/cranelift/object" }
3232

3333
#[patch.crates-io]
3434
#gimli = { path = "../" }
3535

3636
[features]
3737
default = ["jit", "inline_asm"]
38-
jit = ["cranelift-simplejit", "libloading"]
38+
jit = ["cranelift-jit", "libloading"]
3939
inline_asm = []
4040

4141
[profile.dev]

compiler/rustc_codegen_cranelift/Readme.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> ⚠⚠⚠ Certain kinds of FFI don't work yet. ⚠⚠⚠
44
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/master/cranelift).
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).
66
This has the potential to improve compilation times in debug mode.
77
If your project doesn't use any of the things listed under "Not yet supported", it should work fine.
88
If not please open an issue.
@@ -68,7 +68,15 @@ $ $cg_clif_dir/build/cargo.sh jit
6868
or
6969

7070
```bash
71-
$ $cg_clif_dir/build/bin/cg_clif --jit my_crate.rs
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
7280
```
7381

7482
### Shell
@@ -77,7 +85,7 @@ These are a few functions that allow you to easily run rust code from the shell
7785

7886
```bash
7987
function jit_naked() {
80-
echo "$@" | $cg_clif_dir/build/bin/cg_clif - --jit
88+
echo "$@" | $cg_clif_dir/build/bin/cg_clif - -Cllvm-args=mode=jit -Cprefer-dynamic
8189
}
8290

8391
function jit() {

compiler/rustc_codegen_cranelift/build_sysroot/Cargo.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
4747

4848
[[package]]
4949
name = "cc"
50-
version = "1.0.65"
50+
version = "1.0.66"
5151
source = "registry+https://github.com/rust-lang/crates.io-index"
52-
checksum = "95752358c8f7552394baf48cd82695b345628ad3f170d607de3ca03b8dacca15"
52+
checksum = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48"
5353

5454
[[package]]
5555
name = "cfg-if"
@@ -141,9 +141,9 @@ dependencies = [
141141

142142
[[package]]
143143
name = "libc"
144-
version = "0.2.80"
144+
version = "0.2.81"
145145
source = "registry+https://github.com/rust-lang/crates.io-index"
146-
checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
146+
checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb"
147147
dependencies = [
148148
"rustc-std-workspace-core",
149149
]

compiler/rustc_codegen_cranelift/build_sysroot/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ version = "0.0.0"
55

66
[dependencies]
77
core = { path = "./sysroot_src/library/core" }
8-
compiler_builtins = "0.1"
98
alloc = { path = "./sysroot_src/library/alloc" }
109
std = { path = "./sysroot_src/library/std", features = ["panic_unwind", "backtrace"] }
1110
test = { path = "./sysroot_src/library/test" }
1211

1312
alloc_system = { path = "./alloc_system" }
1413

14+
compiler_builtins = { version = "=0.1.36", default-features = false }
15+
1516
[patch.crates-io]
1617
rustc-std-workspace-core = { path = "./sysroot_src/library/rustc-std-workspace-core" }
1718
rustc-std-workspace-alloc = { path = "./sysroot_src/library/rustc-std-workspace-alloc" }

compiler/rustc_codegen_cranelift/example/std_example.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ fn main() {
1515
let stderr = ::std::io::stderr();
1616
let mut stderr = stderr.lock();
1717

18+
// FIXME support lazy jit when multi threading
19+
#[cfg(not(lazy_jit))]
1820
std::thread::spawn(move || {
1921
println!("Hello from another thread!");
2022
});
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly-2020-11-27
1+
nightly-2020-12-23

compiler/rustc_codegen_cranelift/scripts/cargo.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ cmd=$1
1010
shift || true
1111

1212
if [[ "$cmd" = "jit" ]]; then
13-
cargo "+${TOOLCHAIN}" rustc "$@" -- --jit
13+
cargo "+${TOOLCHAIN}" rustc "$@" -- -Cllvm-args=mode=jit -Cprefer-dynamic
14+
elif [[ "$cmd" = "lazy-jit" ]]; then
15+
cargo "+${TOOLCHAIN}" rustc "$@" -- -Cllvm-args=mode=jit-lazy -Cprefer-dynamic
1416
else
1517
cargo "+${TOOLCHAIN}" "$cmd" "$@"
1618
fi

compiler/rustc_codegen_cranelift/scripts/filter_profile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
pushd $(dirname "$0")/../
55
source build/config.sh
66
popd
7-
PROFILE=$1 OUTPUT=$2 exec $RUSTC $RUSTFLAGS --jit $0
7+
PROFILE=$1 OUTPUT=$2 exec $RUSTC $RUSTFLAGS -Cllvm-args=mode=jit -Cprefer-dynamic $0
88
#*/
99

1010
//! This program filters away uninteresting samples and trims uninteresting frames for stackcollapse

compiler/rustc_codegen_cranelift/scripts/tests.sh

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ function no_sysroot_tests() {
1515

1616
if [[ "$JIT_SUPPORTED" = "1" ]]; then
1717
echo "[JIT] mini_core_hello_world"
18-
CG_CLIF_JIT_ARGS="abc bcd" $MY_RUSTC --jit example/mini_core_hello_world.rs --cfg jit --target "$HOST_TRIPLE"
18+
CG_CLIF_JIT_ARGS="abc bcd" $MY_RUSTC -Cllvm-args=mode=jit -Cprefer-dynamic example/mini_core_hello_world.rs --cfg jit --target "$HOST_TRIPLE"
19+
20+
echo "[JIT-lazy] mini_core_hello_world"
21+
CG_CLIF_JIT_ARGS="abc bcd" $MY_RUSTC -Cllvm-args=mode=jit-lazy -Cprefer-dynamic example/mini_core_hello_world.rs --cfg jit --target "$HOST_TRIPLE"
1922
else
2023
echo "[JIT] mini_core_hello_world (skipped)"
2124
fi
@@ -37,7 +40,10 @@ function base_sysroot_tests() {
3740

3841
if [[ "$JIT_SUPPORTED" = "1" ]]; then
3942
echo "[JIT] std_example"
40-
$MY_RUSTC --jit example/std_example.rs --target "$HOST_TRIPLE"
43+
$MY_RUSTC -Cllvm-args=mode=jit -Cprefer-dynamic example/std_example.rs --target "$HOST_TRIPLE"
44+
45+
echo "[JIT-lazy] std_example"
46+
$MY_RUSTC -Cllvm-args=mode=jit-lazy -Cprefer-dynamic example/std_example.rs --cfg lazy_jit --target "$HOST_TRIPLE"
4147
else
4248
echo "[JIT] std_example (skipped)"
4349
fi

compiler/rustc_codegen_cranelift/src/backend.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl AddConstructor for ObjectProduct {
162162
}
163163

164164
pub(crate) fn with_object(sess: &Session, name: &str, f: impl FnOnce(&mut Object)) -> Vec<u8> {
165-
let triple = crate::build_isa(sess, true).triple().clone();
165+
let triple = crate::build_isa(sess).triple().clone();
166166

167167
let binary_format = match triple.binary_format {
168168
target_lexicon::BinaryFormat::Elf => object::BinaryFormat::Elf,
@@ -193,7 +193,7 @@ pub(crate) fn with_object(sess: &Session, name: &str, f: impl FnOnce(&mut Object
193193

194194
pub(crate) fn make_module(sess: &Session, name: String) -> ObjectModule {
195195
let mut builder = ObjectBuilder::new(
196-
crate::build_isa(sess, true),
196+
crate::build_isa(sess),
197197
name + ".o",
198198
cranelift_module::default_libcall_names(),
199199
)

compiler/rustc_codegen_cranelift/src/base.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ pub(crate) fn codegen_fn<'tcx>(
118118
context.eliminate_unreachable_code(cx.module.isa()).unwrap();
119119
context.dce(cx.module.isa()).unwrap();
120120

121+
context.want_disasm = crate::pretty_clif::should_write_ir(tcx);
122+
121123
// Define function
122124
let module = &mut cx.module;
123125
tcx.sess.time("define function", || {
@@ -140,6 +142,16 @@ pub(crate) fn codegen_fn<'tcx>(
140142
&clif_comments,
141143
);
142144

145+
if let Some(mach_compile_result) = &context.mach_compile_result {
146+
if let Some(disasm) = &mach_compile_result.disasm {
147+
crate::pretty_clif::write_ir_file(
148+
tcx,
149+
&format!("{}.vcode", tcx.symbol_name(instance).name),
150+
|file| file.write_all(disasm.as_bytes()),
151+
)
152+
}
153+
}
154+
143155
// Define debuginfo for function
144156
let isa = cx.module.isa();
145157
let debug_context = &mut cx.debug_context;
@@ -307,7 +319,9 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Module>) {
307319
} => {
308320
let discr = codegen_operand(fx, discr).load_scalar(fx);
309321

310-
if switch_ty.kind() == fx.tcx.types.bool.kind() {
322+
let use_bool_opt = switch_ty.kind() == fx.tcx.types.bool.kind()
323+
|| (targets.iter().count() == 1 && targets.iter().next().unwrap().0 == 0);
324+
if use_bool_opt {
311325
assert_eq!(targets.iter().count(), 1);
312326
let (then_value, then_block) = targets.iter().next().unwrap();
313327
let then_block = fx.get_block(then_block);
@@ -325,12 +339,22 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Module>) {
325339
let discr = crate::optimize::peephole::maybe_unwrap_bint(&mut fx.bcx, discr);
326340
let discr =
327341
crate::optimize::peephole::make_branchable_value(&mut fx.bcx, discr);
328-
if test_zero {
329-
fx.bcx.ins().brz(discr, then_block, &[]);
330-
fx.bcx.ins().jump(else_block, &[]);
342+
if let Some(taken) = crate::optimize::peephole::maybe_known_branch_taken(
343+
&fx.bcx, discr, test_zero,
344+
) {
345+
if taken {
346+
fx.bcx.ins().jump(then_block, &[]);
347+
} else {
348+
fx.bcx.ins().jump(else_block, &[]);
349+
}
331350
} else {
332-
fx.bcx.ins().brnz(discr, then_block, &[]);
333-
fx.bcx.ins().jump(else_block, &[]);
351+
if test_zero {
352+
fx.bcx.ins().brz(discr, then_block, &[]);
353+
fx.bcx.ins().jump(else_block, &[]);
354+
} else {
355+
fx.bcx.ins().brnz(discr, then_block, &[]);
356+
fx.bcx.ins().jump(else_block, &[]);
357+
}
334358
}
335359
} else {
336360
let mut switch = ::cranelift_frontend::Switch::new();

0 commit comments

Comments
 (0)