Skip to content

Sync rustc_codegen_cranelift #78624

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

Merged
merged 26 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2be0596
Use with_no_trimmed_paths
bjorn3 Oct 27, 2020
4206f9f
Prefer numeric associated constants in example
bstrie Oct 27, 2020
9842954
Merge pull request #1096 from bstrie/patch-1
bjorn3 Oct 27, 2020
5103a25
Rustup to rustc 1.49.0-nightly (07e968b64 2020-10-27)
bjorn3 Oct 28, 2020
4cc6b4f
Fix many clippy warnings
bjorn3 Oct 28, 2020
114be42
Rustup to rustc 1.49.0-nightly (ffe52882e 2020-10-30)
bjorn3 Oct 31, 2020
c067be0
Implement -Zfunction-sections
bjorn3 Oct 31, 2020
34be539
Use Pointer::dangling for ZST's in trans_const_value
bjorn3 Oct 31, 2020
6b1902a
Update Cranelift
bjorn3 Oct 31, 2020
f4e8af2
Update Cranelift
bjorn3 Nov 1, 2020
8063c37
Merge pull request #1099 from bjorn3/fix_rustc_bootstrap
bjorn3 Nov 1, 2020
d27f2f0
Rename trans to codegen
mominul Nov 1, 2020
c1cad03
Merge pull request #1100 from mominul/trans->codegen
bjorn3 Nov 1, 2020
c674c2c
Hide anonymous allocations from linked artifact
bjorn3 Nov 1, 2020
8b9c213
Fix transmutes between vectors and integers
bjorn3 Nov 1, 2020
324e63d
Ensure that sysroot build works with CARGO_TARGET_DIR set
bjorn3 Nov 1, 2020
cb36760
Split the actual tests out into scripts/tests.sh
bjorn3 Nov 1, 2020
1ea618a
Make it easier to use build_sysroot.sh
bjorn3 Nov 1, 2020
9410b58
Update build instructions
bjorn3 Nov 1, 2020
0c34f5a
Refactor the build system
bjorn3 Nov 2, 2020
8315730
Upload prebuilt cg_clif
bjorn3 Nov 2, 2020
520a61f
Merge pull request #1104 from bjorn3/build_system_refactor
bjorn3 Nov 2, 2020
646b00f
Revert unintentional change
bjorn3 Nov 2, 2020
54b1d10
Test bootstrapping of rustc using cg_clif
bjorn3 Nov 1, 2020
03f01bb
Merge pull request #1105 from bjorn3/test_rustc_bootstrapping
bjorn3 Nov 2, 2020
216c4ae
Merge commit '03f01bbe901d60b71cf2c5ec766aef5e532ab79d' into update_c…
bjorn3 Nov 3, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Bootstrap rustc using cg_clif

on:
- push

jobs:
bootstrap_rustc:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Cache cargo installed crates
uses: actions/cache@v2
with:
path: ~/.cargo/bin
key: ${{ runner.os }}-cargo-installed-crates

- name: Cache cargo registry and index
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
key: ${{ runner.os }}-cargo-registry-and-index-${{ hashFiles('**/Cargo.lock') }}

- name: Cache cargo target dir
uses: actions/cache@v2
with:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }}

- name: Prepare dependencies
run: |
git config --global user.email "[email protected]"
git config --global user.name "User"
./prepare.sh

- name: Test
run: |
# Enable backtraces for easier debugging
export RUST_BACKTRACE=1

./scripts/test_bootstrap.sh
11 changes: 10 additions & 1 deletion compiler/rustc_codegen_cranelift/.github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ jobs:
export COMPILE_RUNS=2
export RUN_RUNS=2

./test.sh --release
./test.sh

- name: Package prebuilt cg_clif
run: tar cvfJ cg_clif.tar.xz build

- name: Upload prebuilt cg_clif
uses: actions/upload-artifact@v2
with:
name: cg_clif-${{ runner.os }}
path: cg_clif.tar.xz
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ perf.data
perf.data.old
*.events
*.string*
/build_sysroot/sysroot
/build
/build_sysroot/sysroot_src
/rust
/rand
Expand Down
20 changes: 10 additions & 10 deletions compiler/rustc_codegen_cranelift/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cranelift-bforest"
version = "0.67.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#4fd90dccabb266e983740e1f5daf8bde9266b286"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#44cbdecea03c360ea82e6482f0cf6c614effef21"
dependencies = [
"cranelift-entity",
]

[[package]]
name = "cranelift-codegen"
version = "0.67.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#4fd90dccabb266e983740e1f5daf8bde9266b286"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#44cbdecea03c360ea82e6482f0cf6c614effef21"
dependencies = [
"byteorder",
"cranelift-bforest",
Expand All @@ -70,7 +70,7 @@ dependencies = [
[[package]]
name = "cranelift-codegen-meta"
version = "0.67.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#4fd90dccabb266e983740e1f5daf8bde9266b286"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#44cbdecea03c360ea82e6482f0cf6c614effef21"
dependencies = [
"cranelift-codegen-shared",
"cranelift-entity",
Expand All @@ -79,17 +79,17 @@ dependencies = [
[[package]]
name = "cranelift-codegen-shared"
version = "0.67.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#4fd90dccabb266e983740e1f5daf8bde9266b286"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#44cbdecea03c360ea82e6482f0cf6c614effef21"

[[package]]
name = "cranelift-entity"
version = "0.67.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#4fd90dccabb266e983740e1f5daf8bde9266b286"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#44cbdecea03c360ea82e6482f0cf6c614effef21"

[[package]]
name = "cranelift-frontend"
version = "0.67.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#4fd90dccabb266e983740e1f5daf8bde9266b286"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#44cbdecea03c360ea82e6482f0cf6c614effef21"
dependencies = [
"cranelift-codegen",
"log",
Expand All @@ -100,7 +100,7 @@ dependencies = [
[[package]]
name = "cranelift-module"
version = "0.67.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#4fd90dccabb266e983740e1f5daf8bde9266b286"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#44cbdecea03c360ea82e6482f0cf6c614effef21"
dependencies = [
"anyhow",
"cranelift-codegen",
Expand All @@ -112,7 +112,7 @@ dependencies = [
[[package]]
name = "cranelift-native"
version = "0.67.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#4fd90dccabb266e983740e1f5daf8bde9266b286"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#44cbdecea03c360ea82e6482f0cf6c614effef21"
dependencies = [
"cranelift-codegen",
"raw-cpuid",
Expand All @@ -122,7 +122,7 @@ dependencies = [
[[package]]
name = "cranelift-object"
version = "0.67.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#4fd90dccabb266e983740e1f5daf8bde9266b286"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#44cbdecea03c360ea82e6482f0cf6c614effef21"
dependencies = [
"anyhow",
"cranelift-codegen",
Expand All @@ -135,7 +135,7 @@ dependencies = [
[[package]]
name = "cranelift-simplejit"
version = "0.67.0"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#4fd90dccabb266e983740e1f5daf8bde9266b286"
source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#44cbdecea03c360ea82e6482f0cf6c614effef21"
dependencies = [
"cranelift-codegen",
"cranelift-entity",
Expand Down
37 changes: 26 additions & 11 deletions compiler/rustc_codegen_cranelift/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,56 @@

> ⚠⚠⚠ 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). 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.
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).
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.

## Building
## Building and testing

```bash
$ git clone https://github.com/bjorn3/rustc_codegen_cranelift.git
$ cd rustc_codegen_cranelift
$ ./prepare.sh # download and patch sysroot src and install hyperfine for benchmarking
$ ./test.sh --release
$ ./build.sh
```

To run the test suite replace the last command with:

```bash
$ ./test.sh
```

This will implicitly build cg_clif too. Both `build.sh` and `test.sh` accept a `--debug` argument to
build in debug mode.

Alternatively you can download a pre built version from [GHA]. It is listed in the artifacts section
of workflow runs. Unfortunately due to GHA restrictions you need to be logged in to access it.

[GHA]: https://github.com/bjorn3/rustc_codegen_cranelift/actions?query=branch%3Amaster+event%3Apush+is%3Asuccess

## Usage

rustc_codegen_cranelift can be used as a near-drop-in replacement for `cargo build` or `cargo run` for existing projects.

Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`prepare.sh` and `test.sh`).
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`).

### Cargo

In the directory with your project (where you can do the usual `cargo build`), run:

```bash
$ $cg_clif_dir/cargo.sh run
$ $cg_clif_dir/build/cargo.sh run
```

This should build and run your project with rustc_codegen_cranelift instead of the usual LLVM backend.

If you compiled cg_clif in debug mode (aka you didn't pass `--release` to `./test.sh`) you should set `CHANNEL="debug"`.

### Rustc

> You should prefer using the Cargo method.

```bash
$ $cg_clif_dir/target/release/cg_clif my_crate.rs
$ $cg_clif_dir/build/cg_clif my_crate.rs
```

### Jit mode
Expand All @@ -47,13 +62,13 @@ In jit mode cg_clif will immediately execute your code without creating an execu
> The jit mode will probably need cargo integration to make this possible.

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

or

```bash
$ $cg_clif_dir/target/release/cg_clif --jit my_crate.rs
$ $cg_clif_dir/build/cg_clif --jit my_crate.rs
```

### Shell
Expand All @@ -62,7 +77,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/target/release/cg_clif - --jit
echo "$@" | $cg_clif_dir/build/cg_clif - --jit
}

function jit() {
Expand Down
47 changes: 47 additions & 0 deletions compiler/rustc_codegen_cranelift/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
set -e

# Settings
export CHANNEL="release"
build_sysroot=1
target_dir='build'
while [[ $# != 0 ]]; do
case $1 in
"--debug")
export CHANNEL="debug"
;;
"--without-sysroot")
build_sysroot=0
;;
"--target-dir")
target_dir=$2
shift
;;
*)
echo "Unknown flag '$1'"
echo "Usage: ./build.sh [--debug] [--without-sysroot] [--target-dir DIR]"
;;
esac
shift
done

# Build cg_clif
export RUSTFLAGS="-Zrun_dsymutil=no"
if [[ "$CHANNEL" == "release" ]]; then
cargo build --release
else
cargo build
fi

rm -rf $target_dir
mkdir $target_dir
cp -a target/$CHANNEL/cg_clif{,_build_sysroot} target/$CHANNEL/*rustc_codegen_cranelift* $target_dir/
cp -a rust-toolchain scripts/config.sh scripts/cargo.sh $target_dir

if [[ "$build_sysroot" == "1" ]]; then
echo "[BUILD] sysroot"
export CG_CLIF_INCR_CACHE_DISABLED=1
dir=$(pwd)
cd $target_dir
time $dir/build_sysroot/build_sysroot.sh
fi
27 changes: 15 additions & 12 deletions compiler/rustc_codegen_cranelift/build_sysroot/build_sysroot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
# Requires the CHANNEL env var to be set to `debug` or `release.`

set -e
cd $(dirname "$0")

pushd ../ >/dev/null
source ./scripts/config.sh
popd >/dev/null
source ./config.sh

# Cleanup for previous run
# v Clean target dir except for build scripts and incremental cache
rm -r target/*/{debug,release}/{build,deps,examples,libsysroot*,native} 2>/dev/null || true
rm -r sysroot/ 2>/dev/null || true
dir=$(pwd)

# Use rustc with cg_clif as hotpluggable backend instead of the custom cg_clif driver so that
# build scripts are still compiled using cg_llvm.
export RUSTC=$(pwd)/../"target/"$CHANNEL"/cg_clif_build_sysroot"
export RUSTC=$dir"/cg_clif_build_sysroot"
export RUSTFLAGS=$RUSTFLAGS" --clif"

cd $(dirname "$0")

# Cleanup for previous run
# v Clean target dir except for build scripts and incremental cache
rm -r target/*/{debug,release}/{build,deps,examples,libsysroot*,native} 2>/dev/null || true

# We expect the target dir in the default location. Guard against the user changing it.
export CARGO_TARGET_DIR=target

# Build libs
export RUSTFLAGS="$RUSTFLAGS -Zforce-unstable-if-unmarked -Cpanic=abort"
if [[ "$1" == "--release" ]]; then
if [[ "$1" != "--debug" ]]; then
sysroot_channel='release'
# FIXME Enable incremental again once rust-lang/rust#74946 is fixed
# FIXME Enable -Zmir-opt-level=2 again once it doesn't ice anymore
Expand All @@ -32,5 +35,5 @@ else
fi

# Copy files to sysroot
mkdir -p sysroot/lib/rustlib/$TARGET_TRIPLE/lib/
cp -r target/$TARGET_TRIPLE/$sysroot_channel/deps/* sysroot/lib/rustlib/$TARGET_TRIPLE/lib/
mkdir -p $dir/sysroot/lib/rustlib/$TARGET_TRIPLE/lib/
cp -a target/$TARGET_TRIPLE/$sysroot_channel/deps/* $dir/sysroot/lib/rustlib/$TARGET_TRIPLE/lib/
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fi

rm -rf $DST_DIR
mkdir -p $DST_DIR/library
cp -r $SRC_DIR/library $DST_DIR/
cp -a $SRC_DIR/library $DST_DIR/

pushd $DST_DIR
echo "[GIT] init"
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/clean_all.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash --verbose
set -e

rm -rf target/ build_sysroot/{sysroot/,sysroot_src/,target/} perf.data{,.old}
rm -rf target/ build/ build_sysroot/{sysroot_src/,target/} perf.data{,.old}
rm -rf rand/ regex/ simple-raytracer/
3 changes: 0 additions & 3 deletions compiler/rustc_codegen_cranelift/docs/env_vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,4 @@
object files when their content should have been changed by a change to cg_clif.</dd>
<dt>CG_CLIF_DISPLAY_CG_TIME</dt>
<dd>If "1", display the time it took to perform codegen for a crate</dd>
<dt>CG_CLIF_FUNCTION_SECTIONS</dt>
<dd>Use a single section for each function. This will often reduce the executable size at the
cost of making linking significantly slower.</dd>
</dl>
10 changes: 10 additions & 0 deletions compiler/rustc_codegen_cranelift/example/mini_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ unsafe impl Copy for u8 {}
unsafe impl Copy for u16 {}
unsafe impl Copy for u32 {}
unsafe impl Copy for u64 {}
unsafe impl Copy for u128 {}
unsafe impl Copy for usize {}
unsafe impl Copy for i8 {}
unsafe impl Copy for i16 {}
Expand Down Expand Up @@ -283,6 +284,15 @@ impl PartialEq for u64 {
}
}

impl PartialEq for u128 {
fn eq(&self, other: &u128) -> bool {
(*self) == (*other)
}
fn ne(&self, other: &u128) -> bool {
(*self) != (*other)
}
}

impl PartialEq for usize {
fn eq(&self, other: &usize) -> bool {
(*self) == (*other)
Expand Down
Loading