From 198037119520d8cccafdc1fd511164c63d741aed Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Thu, 12 Mar 2020 11:17:19 +0100 Subject: [PATCH] Better use of env vars --- Readme.md | 16 ++++++++++++++-- config.sh | 3 +++ src/atomic_shim.rs | 2 +- src/driver.rs | 29 +++++++++++++++++------------ test.sh | 4 ++-- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/Readme.md b/Readme.md index 305a7ac991b..27adb7d3e48 100644 --- a/Readme.md +++ b/Readme.md @@ -18,7 +18,6 @@ $ ./test.sh --release ### Cargo ```bash -$ export CG_CLIF_INCR_CACHE=1 # Enable caching of object files in the incremental cache $ CHANNEL="release" $cg_clif_dir/cargo.sh run ``` @@ -27,10 +26,23 @@ If you compiled cg_clif in debug mode you should use `CHANNEL="debug"` instead o ### Rustc ```bash -$ export CG_CLIF_INCR_CACHE=1 # Enable caching of object files in the incremental cache $ rustc -Cpanic=abort -Zcodegen-backend=$cg_clif_dir/target/release/librustc_codegen_cranelift.so --sysroot $cg_clif_dir/build_sysroot/sysroot my_crate.rs ``` +## Env vars + +
+
CG_CLIF_JIT
+
Enable JIT mode to immediately run a program instead of writing an executable file.
+
CG_CLIF_JIT_ARGS
+
When JIT mode is enable pass these arguments to the program.
+
CG_CLIF_INCR_CACHE_DISABLE
+
Don't cache object files in the incremental cache. Useful during development of cg_clif + to make it possible to use incremental mode for all analyses performed by rustc without caching + object files when their content should have been changed by a change to cg_clif.
+
CG_CLIF_DISPLAY_CG_TIME
+
Display the time it took to perform codegen for a crate
+
## Not yet supported diff --git a/config.sh b/config.sh index 6a3cc715119..e2534da89e1 100644 --- a/config.sh +++ b/config.sh @@ -24,3 +24,6 @@ export RUSTC_LOG=warn # display metadata load errors export LD_LIBRARY_PATH="$(pwd)/target/out:$(pwd)/build_sysroot/sysroot/lib/rustlib/$TARGET_TRIPLE/lib" export DYLD_LIBRARY_PATH=$LD_LIBRARY_PATH + +export CG_CLIF_DISPLAY_CG_TIME=1 +export CG_CLIF_INCR_CACHE_DISABLED=1 diff --git a/src/atomic_shim.rs b/src/atomic_shim.rs index 17922e7bbcd..83c1b164fc1 100644 --- a/src/atomic_shim.rs +++ b/src/atomic_shim.rs @@ -9,7 +9,7 @@ use crate::prelude::*; pub static mut __cg_clif_global_atomic_mutex: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER; pub fn init_global_lock(module: &mut Module, bcx: &mut FunctionBuilder<'_>) { - if std::env::var("SHOULD_RUN").is_ok () { + if std::env::var("CG_CLIF_JIT").is_ok () { // When using JIT, dylibs won't find the __cg_clif_global_atomic_mutex data object defined here, // so instead define it in the cg_clif dylib. diff --git a/src/driver.rs b/src/driver.rs index 1ce2e1ce773..2fcd5e2932c 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -21,7 +21,7 @@ pub fn codegen_crate( ) -> Box { tcx.sess.abort_if_errors(); - if std::env::var("SHOULD_RUN").is_ok() + if std::env::var("CG_CLIF_JIT").is_ok() && tcx.sess.crate_types.get().contains(&CrateType::Executable) { #[cfg(not(target_arch = "wasm32"))] @@ -90,12 +90,12 @@ fn run_jit(tcx: TyCtxt<'_>) -> ! { let finalized_main: *const u8 = jit_module.get_finalized_function(main_func_id); - println!("Rustc codegen cranelift will JIT run the executable, because the SHOULD_RUN env var is set"); + println!("Rustc codegen cranelift will JIT run the executable, because the CG_CLIF_JIT env var is set"); let f: extern "C" fn(c_int, *const *const c_char) -> c_int = unsafe { ::std::mem::transmute(finalized_main) }; - let args = ::std::env::var("JIT_ARGS").unwrap_or_else(|_| String::new()); + let args = ::std::env::var("CG_CLIF_JIT_ARGS").unwrap_or_else(|_| String::new()); let args = args .split(" ") .chain(Some(&*tcx.crate_name(LOCAL_CRATE).as_str().to_string())) @@ -215,14 +215,14 @@ fn run_aot( let obj = product.emit(); std::fs::write(&tmp_file, obj).unwrap(); - let work_product = if std::env::var("CG_CLIF_INCR_CACHE").is_ok() { + let work_product = if std::env::var("CG_CLIF_INCR_CACHE_DISABLED").is_ok() { + None + } else { rustc_incremental::copy_cgu_workproducts_to_incr_comp_cache_dir( tcx.sess, &name, &[(WorkProductFileKind::Object, tmp_file.clone())], ) - } else { - None }; ModuleCodegenResult( @@ -251,6 +251,7 @@ fn run_aot( tcx.sess.cgu_reuse_tracker.set_actual_reuse(&cgu.name().as_str(), cgu_reuse); match cgu_reuse { + _ if std::env::var("CG_CLIF_INCR_CACHE_DISABLED").is_ok() => {} CguReuse::No => {} CguReuse::PreLto => { let incr_comp_session_dir = tcx.sess.incr_comp_session_dir(); @@ -491,12 +492,16 @@ fn trans_mono_item<'clif, 'tcx, B: Backend + 'static>( } fn time(sess: &Session, name: &'static str, f: impl FnOnce() -> R) -> R { - println!("[{}] start", name); - let before = std::time::Instant::now(); - let res = sess.time(name, f); - let after = std::time::Instant::now(); - println!("[{}] end time: {:?}", name, after - before); - res + if std::env::var("CG_CLIF_DISPLAY_CG_TIME").is_ok() { + println!("[{}] start", name); + let before = std::time::Instant::now(); + let res = sess.time(name, f); + let after = std::time::Instant::now(); + println!("[{}] end time: {:?}", name, after - before); + res + } else { + sess.time(name, f) + } } // Adapted from https://github.com/rust-lang/rust/blob/303d8aff6092709edd4dbd35b1c88e9aa40bf6d8/src/librustc_codegen_ssa/base.rs#L922-L953 diff --git a/test.sh b/test.sh index 78407ffbe50..bc4c807cbf2 100755 --- a/test.sh +++ b/test.sh @@ -22,7 +22,7 @@ echo "[BUILD] example" $RUSTC example/example.rs --crate-type lib echo "[JIT] mini_core_hello_world" -JIT_ARGS="abc bcd" SHOULD_RUN=1 $RUSTC --crate-type bin -Cprefer-dynamic example/mini_core_hello_world.rs --cfg jit +CG_CLIF_JIT=1 CG_CLIF_JIT_ARGS="abc bcd" $RUSTC --crate-type bin -Cprefer-dynamic example/mini_core_hello_world.rs --cfg jit echo "[AOT] mini_core_hello_world" $RUSTC example/mini_core_hello_world.rs --crate-name mini_core_hello_world --crate-type bin -g @@ -41,7 +41,7 @@ $RUSTC example/alloc_example.rs --crate-type bin ./target/out/alloc_example echo "[JIT] std_example" -SHOULD_RUN=1 $RUSTC --crate-type bin -Cprefer-dynamic example/std_example.rs +CG_CLIF_JIT=1 $RUSTC --crate-type bin -Cprefer-dynamic example/std_example.rs echo "[AOT] dst_field_align" # FIXME Re-add -Zmir-opt-level=2 once rust-lang/rust#67529 is fixed.