Use -Zcodegen-backend instead of a rustc replacement in cargo-clif

This commit is contained in:
bjorn3 2022-02-13 19:31:49 +01:00
parent 617171e930
commit 2e65a8f2ca
10 changed files with 60 additions and 152 deletions

View File

@ -34,18 +34,6 @@ pub(crate) fn build_backend(
_ => unreachable!(),
}
// Set the rpath to make the cg_clif executable find librustc_codegen_cranelift without changing
// LD_LIBRARY_PATH
if cfg!(unix) {
if cfg!(target_os = "macos") {
rustflags += " -Csplit-debuginfo=unpacked \
-Clink-arg=-Wl,-rpath,@loader_path/../lib \
-Zosx-rpath-install-name";
} else {
rustflags += " -Clink-arg=-Wl,-rpath=$ORIGIN/../lib ";
}
}
cmd.env("RUSTFLAGS", rustflags);
eprintln!("[BUILD] rustc_codegen_cranelift");

View File

@ -33,19 +33,16 @@ pub(crate) fn build_sysroot(
.join(&cg_clif_dylib);
try_hard_link(cg_clif_build_dir.join(cg_clif_dylib), &cg_clif_dylib_path);
try_hard_link(
cg_clif_build_dir.join(get_file_name("cg_clif", "bin")),
target_dir.join("bin").join(get_file_name("cg_clif", "bin")),
);
// Build and copy cargo wrapper
let mut build_cargo_wrapper_cmd = Command::new("rustc");
build_cargo_wrapper_cmd
.arg("scripts/cargo-clif.rs")
.arg("-o")
.arg(target_dir.join("cargo-clif"))
.arg("-g");
spawn_and_wait(build_cargo_wrapper_cmd);
// Build and copy rustc and cargo wrappers
for wrapper in ["rustc-clif", "cargo-clif"] {
let mut build_cargo_wrapper_cmd = Command::new("rustc");
build_cargo_wrapper_cmd
.arg(PathBuf::from("scripts").join(format!("{wrapper}.rs")))
.arg("-o")
.arg(target_dir.join(wrapper))
.arg("-g");
spawn_and_wait(build_cargo_wrapper_cmd);
}
let default_sysroot = super::rustc_info::get_default_sysroot();

View File

@ -19,7 +19,7 @@ This will build your project with rustc_codegen_cranelift instead of the usual L
> You should prefer using the Cargo method.
```bash
$ $cg_clif_dir/build/bin/cg_clif my_crate.rs
$ $cg_clif_dir/build/rustc-clif my_crate.rs
```
## Jit mode
@ -38,7 +38,7 @@ $ $cg_clif_dir/build/cargo-clif jit
or
```bash
$ $cg_clif_dir/build/bin/cg_clif -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic my_crate.rs
$ $cg_clif_dir/build/rustc-clif -Zunstable-features -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
@ -54,7 +54,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 - -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic
echo "$@" | $cg_clif_dir/build/rustc-clif - -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic
}
function jit() {

View File

@ -5,20 +5,11 @@
use std::process::Command;
fn main() {
if env::var("RUSTC_WRAPPER").map_or(false, |wrapper| wrapper.contains("sccache")) {
eprintln!(
"\x1b[1;93m=== Warning: Unsetting RUSTC_WRAPPER to prevent interference with sccache ===\x1b[0m"
);
env::remove_var("RUSTC_WRAPPER");
}
let sysroot = PathBuf::from(env::current_exe().unwrap().parent().unwrap());
env::set_var("RUSTC", sysroot.join("bin/cg_clif".to_string() + env::consts::EXE_SUFFIX));
let mut rustdoc_flags = env::var("RUSTDOCFLAGS").unwrap_or(String::new());
rustdoc_flags.push_str(" -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend=");
rustdoc_flags.push_str(
let mut rustflags = String::new();
rustflags.push_str(" -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend=");
rustflags.push_str(
sysroot
.join(if cfg!(windows) { "bin" } else { "lib" })
.join(
@ -29,9 +20,10 @@ fn main() {
.to_str()
.unwrap(),
);
rustdoc_flags.push_str(" --sysroot ");
rustdoc_flags.push_str(sysroot.to_str().unwrap());
env::set_var("RUSTDOCFLAGS", rustdoc_flags);
rustflags.push_str(" --sysroot ");
rustflags.push_str(sysroot.to_str().unwrap());
env::set_var("RUSTFLAGS", env::var("RUSTFLAGS").unwrap_or(String::new()) + &rustflags);
env::set_var("RUSTDOCFLAGS", env::var("RUSTDOCFLAGS").unwrap_or(String::new()) + &rustflags);
// Ensure that the right toolchain is used
env::set_var("RUSTUP_TOOLCHAIN", env!("RUSTUP_TOOLCHAIN"));

View File

@ -1,6 +0,0 @@
# Note to people running shellcheck: this file should only be sourced, not executed directly.
set -e
export LD_LIBRARY_PATH="$(rustc --print sysroot)/lib:$LD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH="$(rustc --print sysroot)/lib:$DYLD_LIBRARY_PATH"

View File

@ -2,8 +2,7 @@
#![forbid(unsafe_code)]/* This line is ignored by bash
# This block is ignored by rustc
pushd $(dirname "$0")/../
source scripts/config.sh
RUSTC="$(pwd)/build/bin/cg_clif"
RUSTC="$(pwd)/build/rustc-clif"
popd
PROFILE=$1 OUTPUT=$2 exec $RUSTC -Zunstable-options -Cllvm-args=mode=jit -Cprefer-dynamic $0
#*/

36
scripts/rustc-clif.rs Normal file
View File

@ -0,0 +1,36 @@
use std::env;
use std::ffi::OsString;
#[cfg(unix)]
use std::os::unix::process::CommandExt;
use std::path::PathBuf;
use std::process::Command;
fn main() {
let sysroot = PathBuf::from(env::current_exe().unwrap().parent().unwrap());
let cg_clif_dylib_path = sysroot.join(if cfg!(windows) { "bin" } else { "lib" }).join(
env::consts::DLL_PREFIX.to_string() + "rustc_codegen_cranelift" + env::consts::DLL_SUFFIX,
);
let mut args = std::env::args_os().skip(1).collect::<Vec<_>>();
args.push(OsString::from("-Cpanic=abort"));
args.push(OsString::from("-Zpanic-abort-tests"));
let mut codegen_backend_arg = OsString::from("-Zcodegen-backend=");
codegen_backend_arg.push(cg_clif_dylib_path);
args.push(codegen_backend_arg);
if !args.contains(&OsString::from("--sysroot")) {
args.push(OsString::from("--sysroot"));
args.push(OsString::from(sysroot.to_str().unwrap()));
}
// Ensure that the right toolchain is used
env::set_var("RUSTUP_TOOLCHAIN", env!("RUSTUP_TOOLCHAIN"));
#[cfg(unix)]
Command::new("rustc").args(args).exec();
#[cfg(not(unix))]
std::process::exit(
Command::new("rustc").args(args).spawn().unwrap().wait().unwrap().code().unwrap_or(1),
);
}

View File

@ -2,7 +2,6 @@
set -e
./y.rs build --no-unstable-features
source scripts/config.sh
echo "[SETUP] Rust fork"
git clone https://github.com/rust-lang/rust.git || true
@ -52,7 +51,7 @@ changelog-seen = 2
ninja = false
[build]
rustc = "$(pwd)/../build/bin/cg_clif"
rustc = "$(pwd)/../build/rustc-clif"
cargo = "$(rustup which cargo)"
full-bootstrap = true
local-rebuild = true

View File

@ -2,8 +2,6 @@
set -e
source scripts/config.sh
export CG_CLIF_DISPLAY_CG_TIME=1
export CG_CLIF_DISABLE_INCR_CACHE=1
@ -31,8 +29,7 @@ if [[ "$(uname)" == 'Darwin' ]]; then
export RUSTFLAGS="$RUSTFLAGS -Clink-arg=-undefined -Clink-arg=dynamic_lookup"
fi
export RUSTC=false # ensure that cg_llvm isn't accidentally used
MY_RUSTC="$(pwd)/build/bin/cg_clif $RUSTFLAGS -L crate=target/out --out-dir target/out -Cdebuginfo=2"
MY_RUSTC="$(pwd)/build/rustc-clif $RUSTFLAGS -L crate=target/out --out-dir target/out -Cdebuginfo=2"
function no_sysroot_tests() {
echo "[BUILD] mini_core"
@ -124,7 +121,7 @@ function extended_sysroot_tests() {
if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then
echo "[BENCH COMPILE] ebobby/simple-raytracer"
hyperfine --runs "${RUN_RUNS:-10}" --warmup 1 --prepare "../build/cargo-clif clean" \
"RUSTC=rustc RUSTFLAGS='' cargo build" \
"RUSTFLAGS='' cargo build" \
"../build/cargo-clif build"
echo "[BENCH RUN] ebobby/simple-raytracer"

View File

@ -1,94 +0,0 @@
#![feature(rustc_private)]
#![warn(rust_2018_idioms)]
#![warn(unused_lifetimes)]
#![warn(unreachable_pub)]
extern crate rustc_data_structures;
extern crate rustc_driver;
extern crate rustc_interface;
extern crate rustc_session;
extern crate rustc_target;
use std::panic;
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
use rustc_interface::interface;
use rustc_session::config::{ErrorOutputType, TrimmedDefPaths};
use rustc_session::early_error;
use rustc_target::spec::PanicStrategy;
// FIXME use std::lazy::SyncLazy once it stabilizes
use once_cell::sync::Lazy;
const BUG_REPORT_URL: &str = "https://github.com/bjorn3/rustc_codegen_cranelift/issues/new";
static DEFAULT_HOOK: Lazy<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static>> =
Lazy::new(|| {
let hook = panic::take_hook();
panic::set_hook(Box::new(|info| {
// Invoke the default handler, which prints the actual panic message and optionally a backtrace
(*DEFAULT_HOOK)(info);
// Separate the output with an empty line
eprintln!();
// Print the ICE message
rustc_driver::report_ice(info, BUG_REPORT_URL);
}));
hook
});
#[derive(Default)]
pub struct CraneliftPassesCallbacks {
time_passes: bool,
}
impl rustc_driver::Callbacks for CraneliftPassesCallbacks {
fn config(&mut self, config: &mut interface::Config) {
// If a --prints=... option has been given, we don't print the "total"
// time because it will mess up the --prints output. See #64339.
self.time_passes = config.opts.prints.is_empty()
&& (config.opts.debugging_opts.time_passes || config.opts.debugging_opts.time);
config.opts.cg.panic = Some(PanicStrategy::Abort);
config.opts.debugging_opts.panic_abort_tests = true;
config.opts.maybe_sysroot = Some(config.opts.maybe_sysroot.clone().unwrap_or_else(|| {
std::env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_owned()
}));
config.opts.trimmed_def_paths = TrimmedDefPaths::GoodPath;
}
}
fn main() {
let start_time = std::time::Instant::now();
let start_rss = get_resident_set_size();
rustc_driver::init_rustc_env_logger();
let mut callbacks = CraneliftPassesCallbacks::default();
Lazy::force(&DEFAULT_HOOK); // Install ice hook
let exit_code = rustc_driver::catch_with_exit_code(|| {
let args = std::env::args_os()
.enumerate()
.map(|(i, arg)| {
arg.into_string().unwrap_or_else(|arg| {
early_error(
ErrorOutputType::default(),
&format!("Argument {} is not valid Unicode: {:?}", i, arg),
)
})
})
.collect::<Vec<_>>();
let mut run_compiler = rustc_driver::RunCompiler::new(&args, &mut callbacks);
run_compiler.set_make_codegen_backend(Some(Box::new(move |_| {
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { config: None })
})));
run_compiler.run()
});
if callbacks.time_passes {
let end_rss = get_resident_set_size();
print_time_passes_entry("total", start_time.elapsed(), start_rss, end_rss);
}
std::process::exit(exit_code)
}