Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync rustc_codegen_cranelift #80408

Merged
merged 34 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d404840
Merge commit '5988bbd24aa87732bfa1d111ba00bcdaa22c481a' into sync_cg_…
bjorn3 Nov 27, 2020
3d357e6
Sync from rust 760430e6fdd70cdb09b5b6d696905c0ee0ea27c8
bjorn3 Nov 29, 2020
7760894
Allow cranelift to handle atomic pointers
oli-obk Nov 29, 2020
a8cda1a
Rustup to rustc 1.50.0-nightly (f4db9ffb2 2020-12-02)
bjorn3 Dec 3, 2020
64e7ff2
Update Cranelift
bjorn3 Dec 3, 2020
d95d03a
Support #[repr(simd)] on array wrappers
bjorn3 Dec 3, 2020
3f28a49
Update Cranelift
bjorn3 Dec 4, 2020
5f21ff2
Inline codegen_mono_item
bjorn3 Oct 11, 2020
994b91a
Optimize branches when the target is statically known to a jump
bjorn3 Dec 9, 2020
aec0fc5
Auto merge of #77611 - oli-obk:atomic_miri_leakage, r=nagisa
bors Dec 9, 2020
42b267d
Move binder for dyn to each list item
jackh726 Dec 11, 2020
f8e22bf
Update Cranelift
bjorn3 Dec 12, 2020
3f47f93
Enable Cranelift optimizations when optimizing
bjorn3 Dec 12, 2020
cd21269
Fix Cranelift link in readme (#1118)
Jjagg Dec 13, 2020
44b3310
Also emit vcode when emitting clif ir while using new style backends
bjorn3 Dec 14, 2020
5f6c32a
cg_clif: fix build with split dwarf
davidtwco Nov 8, 2020
0f57e1c
Sync from rust caeb3335c052f286f6e7257ac0ff21e4f73fd8c5
bjorn3 Dec 17, 2020
8e234d5
Rustup to rustc 1.50.0-nightly (b32e6e6ac 2020-12-16)
bjorn3 Dec 17, 2020
a921a47
Auto merge of #79945 - jackh726:existential_trait_ref, r=nikomatsakis
bors Dec 17, 2020
06e77b9
Sync from rust fee693d08e98d25f566075cbed73e12236c05abd
bjorn3 Dec 18, 2020
32f92aa
Rustup to rustc 1.50.0-nightly (eb4fc71dc 2020-12-17)
bjorn3 Dec 18, 2020
54ba238
Remove implementation of intrinsics that are now lowered
bjorn3 Dec 18, 2020
6c1fc32
Make BoundRegion have a kind of BoungRegionKind
jackh726 Dec 18, 2020
dfe6495
Sync from rust 28d73a3ee3e2c32f2768d1cbc9b42cb63472e9e9
bjorn3 Dec 23, 2020
510616f
Rustup to rustc 1.50.0-nightly (bb1fbbf84 2020-12-22)
bjorn3 Dec 23, 2020
c556e4d
Use PIC in JIT mode too
bjorn3 Dec 17, 2020
20ffea6
Change the way JIT mode is selected
bjorn3 Dec 25, 2020
0b9b253
Move finalize CodegenCx timer out of codegen mono items timer
bjorn3 Dec 25, 2020
3f6a3b5
Implement lazy compilation in JIT mode
bjorn3 Dec 25, 2020
35f4a25
Add documentation
bjorn3 Dec 25, 2020
2ca8daa
Merge pull request #1120 from bjorn3/lazy_jit
bjorn3 Dec 25, 2020
5b32489
Set rust-analyzer importMergeBehaviour option to last
bjorn3 Dec 25, 2020
dbee136
Rustfmt
bjorn3 Dec 25, 2020
52cf01c
Merge commit 'dbee13661efa269cb4cd57bb4c6b99a19732b484' into sync_cg_…
bjorn3 Dec 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_codegen_cranelift/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
// source for rustc_* is not included in the rust-src component; disable the errors about this
"rust-analyzer.diagnostics.disabled": ["unresolved-extern-crate"],
"rust-analyzer.assist.importMergeBehaviour": "last",
"rust-analyzer.cargo.loadOutDirsFromCheck": true,
"rust-analyzer.linkedProjects": [
"./Cargo.toml",
Expand Down
55 changes: 28 additions & 27 deletions compiler/rustc_codegen_cranelift/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cranelift-bforest"
version = "0.68.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
dependencies = [
"cranelift-entity",
]

[[package]]
name = "cranelift-codegen"
version = "0.68.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
dependencies = [
"byteorder",
"cranelift-bforest",
Expand All @@ -76,7 +76,7 @@ dependencies = [
[[package]]
name = "cranelift-codegen-meta"
version = "0.68.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
dependencies = [
"cranelift-codegen-shared",
"cranelift-entity",
Expand All @@ -85,28 +85,46 @@ dependencies = [
[[package]]
name = "cranelift-codegen-shared"
version = "0.68.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"

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

[[package]]
name = "cranelift-frontend"
version = "0.68.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
dependencies = [
"cranelift-codegen",
"log",
"smallvec",
"target-lexicon",
]

[[package]]
name = "cranelift-jit"
version = "0.68.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
dependencies = [
"anyhow",
"cranelift-codegen",
"cranelift-entity",
"cranelift-module",
"cranelift-native",
"errno",
"libc",
"log",
"region",
"target-lexicon",
"winapi",
]

[[package]]
name = "cranelift-module"
version = "0.68.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
dependencies = [
"anyhow",
"cranelift-codegen",
Expand All @@ -118,7 +136,7 @@ dependencies = [
[[package]]
name = "cranelift-native"
version = "0.68.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
dependencies = [
"cranelift-codegen",
"raw-cpuid",
Expand All @@ -128,7 +146,7 @@ dependencies = [
[[package]]
name = "cranelift-object"
version = "0.68.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb"
dependencies = [
"anyhow",
"cranelift-codegen",
Expand All @@ -138,23 +156,6 @@ dependencies = [
"target-lexicon",
]

[[package]]
name = "cranelift-simplejit"
version = "0.68.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#19640367dbf0da7093e61add3306c8d092644fb3"
dependencies = [
"cranelift-codegen",
"cranelift-entity",
"cranelift-module",
"cranelift-native",
"errno",
"libc",
"log",
"region",
"target-lexicon",
"winapi",
]

[[package]]
name = "crc32fast"
version = "1.2.1"
Expand Down Expand Up @@ -325,9 +326,9 @@ dependencies = [
"ar",
"cranelift-codegen",
"cranelift-frontend",
"cranelift-jit",
"cranelift-module",
"cranelift-object",
"cranelift-simplejit",
"gimli",
"indexmap",
"libloading",
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_codegen_cranelift/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ crate-type = ["dylib"]
cranelift-codegen = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", features = ["unwind"] }
cranelift-frontend = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
cranelift-module = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
cranelift-simplejit = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", optional = true }
cranelift-jit = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", optional = true }
cranelift-object = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" }
target-lexicon = "0.11.0"
gimli = { version = "0.23.0", default-features = false, features = ["write"]}
Expand All @@ -27,15 +27,15 @@ libloading = { version = "0.6.0", optional = true }
#cranelift-codegen = { path = "../wasmtime/cranelift/codegen" }
#cranelift-frontend = { path = "../wasmtime/cranelift/frontend" }
#cranelift-module = { path = "../wasmtime/cranelift/module" }
#cranelift-simplejit = { path = "../wasmtime/cranelift/simplejit" }
#cranelift-jit = { path = "../wasmtime/cranelift/jit" }
#cranelift-object = { path = "../wasmtime/cranelift/object" }

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

[features]
default = ["jit", "inline_asm"]
jit = ["cranelift-simplejit", "libloading"]
jit = ["cranelift-jit", "libloading"]
inline_asm = []

[profile.dev]
Expand Down
14 changes: 11 additions & 3 deletions compiler/rustc_codegen_cranelift/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> ⚠⚠⚠ Certain kinds of FFI don't work yet. ⚠⚠⚠

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).
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).
This has the potential to improve compilation times in debug mode.
If your project doesn't use any of the things listed under "Not yet supported", it should work fine.
If not please open an issue.
Expand Down Expand Up @@ -68,7 +68,15 @@ $ $cg_clif_dir/build/cargo.sh jit
or

```bash
$ $cg_clif_dir/build/bin/cg_clif --jit my_crate.rs
$ $cg_clif_dir/build/bin/cg_clif -Cllvm-args=mode=jit -Cprefer-dynamic my_crate.rs
```

There is also an experimental lazy jit mode. In this mode functions are only compiled once they are
first called. It currently does not work with multi-threaded programs. When a not yet compiled
function is called from another thread than the main thread, you will get an ICE.

```bash
$ $cg_clif_dir/build/cargo.sh lazy-jit
```

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

```bash
function jit_naked() {
echo "$@" | $cg_clif_dir/build/bin/cg_clif - --jit
echo "$@" | $cg_clif_dir/build/bin/cg_clif - -Cllvm-args=mode=jit -Cprefer-dynamic
}

function jit() {
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_cranelift/build_sysroot/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"

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

[[package]]
name = "cfg-if"
Expand Down Expand Up @@ -141,9 +141,9 @@ dependencies = [

[[package]]
name = "libc"
version = "0.2.80"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb"
dependencies = [
"rustc-std-workspace-core",
]
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_cranelift/build_sysroot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ version = "0.0.0"

[dependencies]
core = { path = "./sysroot_src/library/core" }
compiler_builtins = "0.1"
alloc = { path = "./sysroot_src/library/alloc" }
std = { path = "./sysroot_src/library/std", features = ["panic_unwind", "backtrace"] }
test = { path = "./sysroot_src/library/test" }

alloc_system = { path = "./alloc_system" }

compiler_builtins = { version = "=0.1.36", default-features = false }

[patch.crates-io]
rustc-std-workspace-core = { path = "./sysroot_src/library/rustc-std-workspace-core" }
rustc-std-workspace-alloc = { path = "./sysroot_src/library/rustc-std-workspace-alloc" }
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_cranelift/example/std_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ fn main() {
let stderr = ::std::io::stderr();
let mut stderr = stderr.lock();

// FIXME support lazy jit when multi threading
#[cfg(not(lazy_jit))]
std::thread::spawn(move || {
println!("Hello from another thread!");
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nightly-2020-11-27
nightly-2020-12-23
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_cranelift/scripts/cargo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ cmd=$1
shift || true

if [[ "$cmd" = "jit" ]]; then
cargo "+${TOOLCHAIN}" rustc "$@" -- --jit
cargo "+${TOOLCHAIN}" rustc "$@" -- -Cllvm-args=mode=jit -Cprefer-dynamic
elif [[ "$cmd" = "lazy-jit" ]]; then
cargo "+${TOOLCHAIN}" rustc "$@" -- -Cllvm-args=mode=jit-lazy -Cprefer-dynamic
else
cargo "+${TOOLCHAIN}" "$cmd" "$@"
fi
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/scripts/filter_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pushd $(dirname "$0")/../
source build/config.sh
popd
PROFILE=$1 OUTPUT=$2 exec $RUSTC $RUSTFLAGS --jit $0
PROFILE=$1 OUTPUT=$2 exec $RUSTC $RUSTFLAGS -Cllvm-args=mode=jit -Cprefer-dynamic $0
#*/

//! This program filters away uninteresting samples and trims uninteresting frames for stackcollapse
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_codegen_cranelift/scripts/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ function no_sysroot_tests() {

if [[ "$JIT_SUPPORTED" = "1" ]]; then
echo "[JIT] mini_core_hello_world"
CG_CLIF_JIT_ARGS="abc bcd" $MY_RUSTC --jit example/mini_core_hello_world.rs --cfg jit --target "$HOST_TRIPLE"
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"

echo "[JIT-lazy] mini_core_hello_world"
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"
else
echo "[JIT] mini_core_hello_world (skipped)"
fi
Expand All @@ -37,7 +40,10 @@ function base_sysroot_tests() {

if [[ "$JIT_SUPPORTED" = "1" ]]; then
echo "[JIT] std_example"
$MY_RUSTC --jit example/std_example.rs --target "$HOST_TRIPLE"
$MY_RUSTC -Cllvm-args=mode=jit -Cprefer-dynamic example/std_example.rs --target "$HOST_TRIPLE"

echo "[JIT-lazy] std_example"
$MY_RUSTC -Cllvm-args=mode=jit-lazy -Cprefer-dynamic example/std_example.rs --cfg lazy_jit --target "$HOST_TRIPLE"
else
echo "[JIT] std_example (skipped)"
fi
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_cranelift/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl AddConstructor for ObjectProduct {
}

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

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

pub(crate) fn make_module(sess: &Session, name: String) -> ObjectModule {
let mut builder = ObjectBuilder::new(
crate::build_isa(sess, true),
crate::build_isa(sess),
name + ".o",
cranelift_module::default_libcall_names(),
)
Expand Down
36 changes: 30 additions & 6 deletions compiler/rustc_codegen_cranelift/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ pub(crate) fn codegen_fn<'tcx>(
context.eliminate_unreachable_code(cx.module.isa()).unwrap();
context.dce(cx.module.isa()).unwrap();

context.want_disasm = crate::pretty_clif::should_write_ir(tcx);

// Define function
let module = &mut cx.module;
tcx.sess.time("define function", || {
Expand All @@ -140,6 +142,16 @@ pub(crate) fn codegen_fn<'tcx>(
&clif_comments,
);

if let Some(mach_compile_result) = &context.mach_compile_result {
if let Some(disasm) = &mach_compile_result.disasm {
crate::pretty_clif::write_ir_file(
tcx,
&format!("{}.vcode", tcx.symbol_name(instance).name),
|file| file.write_all(disasm.as_bytes()),
)
}
}

// Define debuginfo for function
let isa = cx.module.isa();
let debug_context = &mut cx.debug_context;
Expand Down Expand Up @@ -307,7 +319,9 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Module>) {
} => {
let discr = codegen_operand(fx, discr).load_scalar(fx);

if switch_ty.kind() == fx.tcx.types.bool.kind() {
let use_bool_opt = switch_ty.kind() == fx.tcx.types.bool.kind()
|| (targets.iter().count() == 1 && targets.iter().next().unwrap().0 == 0);
if use_bool_opt {
assert_eq!(targets.iter().count(), 1);
let (then_value, then_block) = targets.iter().next().unwrap();
let then_block = fx.get_block(then_block);
Expand All @@ -325,12 +339,22 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Module>) {
let discr = crate::optimize::peephole::maybe_unwrap_bint(&mut fx.bcx, discr);
let discr =
crate::optimize::peephole::make_branchable_value(&mut fx.bcx, discr);
if test_zero {
fx.bcx.ins().brz(discr, then_block, &[]);
fx.bcx.ins().jump(else_block, &[]);
if let Some(taken) = crate::optimize::peephole::maybe_known_branch_taken(
&fx.bcx, discr, test_zero,
) {
if taken {
fx.bcx.ins().jump(then_block, &[]);
} else {
fx.bcx.ins().jump(else_block, &[]);
}
} else {
fx.bcx.ins().brnz(discr, then_block, &[]);
fx.bcx.ins().jump(else_block, &[]);
if test_zero {
fx.bcx.ins().brz(discr, then_block, &[]);
fx.bcx.ins().jump(else_block, &[]);
} else {
fx.bcx.ins().brnz(discr, then_block, &[]);
fx.bcx.ins().jump(else_block, &[]);
}
}
} else {
let mut switch = ::cranelift_frontend::Switch::new();
Expand Down
Loading