build(flake): Replace self-packaged cranelift with rustup's

Motivation
==========
The self-packed one sort-of worked, but was rather flaky, while the one
from rustup obviously works, as it's an officially distributed part.

Running it
==========
Instead of the `cargo clif [build, etc.]` you now need to run (on nightly):
```
RUSTFLAGS="-Zcodegen-backend=cranelift" cargo build
```

Performance
===========
On my system I noticed, that a debug build takes around 20 sec *more*
with cranelift, compared to the default LLVM backend.

One possible hypothesis, for that could be, that cranelift does
not link via `mold` but instead still uses GNU's `gold`.

On the other hand, the performance decrease could also be caused
by the fact, that it's an early preview and some optimizations are
still lacking.
This commit is contained in:
Benedikt Peetz 2023-10-31 18:07:24 +01:00
parent c1b426d590
commit df07c3fc24
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
2 changed files with 8 additions and 127 deletions

View File

@ -76,9 +76,7 @@
"flake-compat": "flake-compat",
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay",
"rustc_cranelift_backend": "rustc_cranelift_backend",
"rustc_cranelift_backend_src": "rustc_cranelift_backend_src"
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
@ -104,36 +102,6 @@
"type": "github"
}
},
"rustc_cranelift_backend": {
"flake": false,
"locked": {
"lastModified": 1698679294,
"narHash": "sha256-plG6ifmuMwy7pr0KE19Oz+HKLH+4UDuuXZ1u0s9RtNQ=",
"owner": "bjorn3",
"repo": "rustc_codegen_cranelift",
"rev": "9a33f82140c6da6e5808253309c674554b93e9fe",
"type": "github"
},
"original": {
"owner": "bjorn3",
"ref": "dev",
"repo": "rustc_codegen_cranelift",
"type": "github"
}
},
"rustc_cranelift_backend_src": {
"flake": false,
"locked": {
"lastModified": 1698679361,
"narHash": "sha256-vc7T92wEfufuPc8q2/xq4mQK/uFfoG7pKOVWYxbHzds=",
"type": "tarball",
"url": "https://github.com/bjorn3/rustc_codegen_cranelift/releases/download/dev/cg_clif-x86_64-unknown-linux-gnu.tar.xz"
},
"original": {
"type": "tarball",
"url": "https://github.com/bjorn3/rustc_codegen_cranelift/releases/download/dev/cg_clif-x86_64-unknown-linux-gnu.tar.xz"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,

101
flake.nix
View File

@ -29,16 +29,6 @@
flake-utils.follows = "flake-utils";
};
};
# cranelift
rustc_cranelift_backend = {
url = "github:bjorn3/rustc_codegen_cranelift/dev";
flake = false;
};
rustc_cranelift_backend_src = {
url = "https://github.com/bjorn3/rustc_codegen_cranelift/releases/download/dev/cg_clif-x86_64-unknown-linux-gnu.tar.xz";
flake = false;
};
};
outputs = {
@ -47,8 +37,6 @@
crane,
flake-utils,
rust-overlay,
rustc_cranelift_backend,
rustc_cranelift_backend_src,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
@ -74,94 +62,20 @@
done
'';
};
c_rust = pkgs.rust-bin.fromRustupToolchainFile "${rustc_cranelift_backend}/rust-toolchain";
rcb = pkgs.stdenv.mkDerivation {
pname = "rustc_cranelift_backend";
version = "1.0";
buildInputs = [c_rust];
nativeBuildInputs = with pkgs; [gawk fd];
srcs = ["${rustc_cranelift_backend_src}" "${c_rust}"];
sourceRoot = ".";
postUnpack = ''
rust_dir="$(fd . --max-depth 1 | awk '!/source/ && !/env-vars/')"
# remove unneeded stuff
rm -r "$rust_dir"/{bin,nix-support,share};
rm -r "$rust_dir"/lib/rustlib/{etc,src,rustc-src};
cp -r "$rust_dir"/. source/extra_dependencies
rm -r "$rust_dir"
cd source
'';
postPatch = ''
# patch bins
for file in $(fd . --type file);do
file="$(file --mime "$file" | awk 'BEGIN{FS=":"}/application\/x-pie-executable/{print $1}')";
if [ "$file" ]; then
echo "file: '$file' matches";
correct_interpreter_path="$(ldd "$file" | tail -n1 | awk 'BEGIN{FS="=> "} {print $2}' | awk 'BEGIN{FS=" "}{print $1}')"
echo "correct interpreter path is: '$correct_interpreter_path'"
patchelf --set-interpreter "$correct_interpreter_path" "$file"
if [ "$(patchelf --print-interpreter "$file")" = "$correct_interpreter_path" ];then
echo "Set interpreter";
else
echo "Failed to set interprter, the interpreter still is $(patchelf --print-interpreter )";
exit 1
fi
fi
done
# patch libs
all_files=$(mktemp);
for file in $(fd .);do
canonical_path="$(readlink -f "$file")"
file="$(file --mime "$canonical_path" | awk 'BEGIN{FS=":"}/application\/x-sharedlib/{print $1}')";
if [ "$file" ]; then
echo "$file" >> $all_files;
fi
done
while read -r file; do
echo "___________________";
echo "Checking file: '$file'";
is_missing="$(ldd "$file" | awk 'BEGIN{FS="=>"}{if (/not found/){print $1}}')"
if [ "$is_missing" ];then
echo "Warning: The following things are missing:";
for line in $is_missing; do
echo " $line";
done
echo
for line in $is_missing; do
echo "Searching for a substitute for '$line'"
substitute="$(grep "$line" "$all_files" | awk 'BEGIN{FS=" "}{print $1}' | tail -n1)";
if [ "$substitute" ]; then
echo "Found '$substitute', which can substitute '$line'"
echo "Patching.."
patchelf --replace-needed "$line" "$(readlink -f "$substitute")" "$file";
else
echo "Error: Failed to find a substitute"
fi
done
else
echo "This file is not missing anything"
fi
done < $all_files
rm $all_files
'';
installPhase = ''
install -d $out/
cp -r ./. $out/
'';
};
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};
nightly = false;
nightly = true;
rust =
if nightly
then pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default)
then
(pkgs.rust-bin.selectLatestNightlyWith (toolchain:
toolchain.default))
.override {
extensions = ["rustc-codegen-cranelift-preview"];
}
else pkgs.rust-bin.stable.latest.default;
craneLib = (crane.mkLib pkgs).overrideToolchain rust;
@ -169,7 +83,6 @@
nativeBuildInputs = with pkgs; [
pkg-config
mold'
rcb
];
buildInputs = with pkgs; [
openssl