Skip to content

Commit 426e557

Browse files
committed
Replace CG_CLIF_JIT with --jit
1 parent 838dd17 commit 426e557

File tree

9 files changed

+69
-23
lines changed

9 files changed

+69
-23
lines changed

Readme.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,24 @@ If you compiled cg_clif in debug mode (aka you didn't pass `--release` to `./tes
3636
> You should prefer using the Cargo method.
3737
3838
```bash
39-
$ rustc +$(cat $cg_clif_dir/rust-toolchain) -Cpanic=abort -Zcodegen-backend=$cg_clif_dir/target/release/librustc_codegen_cranelift.so --sysroot $cg_clif_dir/build_sysroot/sysroot my_crate.rs
39+
$ $cg_clif_dir/target/release/cg_clif my_crate.rs
40+
```
41+
42+
### Jit mode
43+
44+
In jit mode cg_clif will immediately execute your code without creating an executable file.
45+
This requires all dependencies to be available as dynamic library.
46+
The easiest way to achieve this is by creating a new dylib crate that has all your dependencies as dependencies of itself.
47+
Rustc will then link all your rlib dependencies into the dylib.
48+
49+
```bash
50+
$ $cg_clif_dir/cargo.sh jit
51+
```
52+
53+
or
54+
55+
```bash
56+
$ $cg_clif_dir/target/release/cg_clif --jit my_crate.rs
4057
```
4158

4259
### Shell
@@ -45,7 +62,7 @@ These are a few functions that allow you to easily run rust code from the shell
4562

4663
```bash
4764
function jit_naked() {
48-
echo "$@" | CG_CLIF_JIT=1 rustc -Zcodegen-backend=$cg_clif_dir/target/release/librustc_codegen_cranelift.so --sysroot $cg_clif_dir/build_sysroot/sysroot - -Cprefer-dynamic
65+
echo "$@" | $cg_clif_dir/target/release/cg_clif - --jit
4966
}
5067

5168
function jit() {

cargo.sh

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ TOOLCHAIN=$(cat rust-toolchain)
1212

1313
popd >/dev/null
1414

15-
if [[ $(rustc -V) != $(rustc +${TOOLCHAIN} -V) ]]; then
16-
echo "rustc_codegen_cranelift is build for $(rustc +${TOOLCHAIN} -V) but the default rustc version is $(rustc -V)."
17-
echo "Using $(rustc +${TOOLCHAIN} -V)."
18-
fi
19-
2015
cmd=$1
2116
shift
2217

18+
if [[ "$cmd" = "jit" ]]; then
19+
cargo +${TOOLCHAIN} rustc $@ -- --jit
20+
else
2321
cargo +${TOOLCHAIN} $cmd $@
22+
fi

docs/env_vars.md

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# List of env vars recognized by cg_clif
22

33
<dl>
4-
<dt>CG_CLIF_JIT</dt>
5-
<dd>Enable JIT mode to immediately run a program instead of writing an executable file.</dd>
64
<dt>CG_CLIF_JIT_ARGS</dt>
75
<dd>When JIT mode is enable pass these arguments to the program.</dd>
86
<dt>CG_CLIF_INCR_CACHE_DISABLED</dt>

scripts/filter_profile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CHANNEL="release"
55
pushd $(dirname "$0")/../
66
source scripts/config.sh
77
popd
8-
CG_CLIF_JIT=1 PROFILE=$1 OUTPUT=$2 exec rustc $RUSTFLAGS $0 --crate-type bin -Cprefer-dynamic
8+
PROFILE=$1 OUTPUT=$2 exec $RUSTC $RUSTFLAGS --jit $0
99
#*/
1010

1111
//! This program filters away uninteresting samples and trims uninteresting frames for stackcollapse

src/bin/cg_clif.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ fn main() {
5050
let mut callbacks = TimePassesCallbacks::default();
5151
rustc_driver::install_ice_hook();
5252
let exit_code = rustc_driver::catch_with_exit_code(|| {
53-
let args = std::env::args_os()
53+
let mut use_jit = false;
54+
55+
let mut args = std::env::args_os()
5456
.enumerate()
5557
.map(|(i, arg)| {
5658
arg.into_string().unwrap_or_else(|arg| {
@@ -60,14 +62,29 @@ fn main() {
6062
)
6163
})
6264
})
65+
.filter(|arg| {
66+
if arg == "--jit" {
67+
use_jit = true;
68+
false
69+
} else {
70+
true
71+
}
72+
})
6373
.collect::<Vec<_>>();
74+
if use_jit {
75+
args.push("-Cprefer-dynamic".to_string());
76+
}
6477
rustc_driver::run_compiler(
6578
&args,
6679
&mut callbacks,
6780
None,
6881
None,
69-
Some(Box::new(|_| {
70-
rustc_codegen_cranelift::__rustc_codegen_backend()
82+
Some(Box::new(move |_| {
83+
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend {
84+
config: rustc_codegen_cranelift::BackendConfig {
85+
use_jit,
86+
}
87+
})
7188
})),
7289
)
7390
});

src/driver/jit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>) -> ! {
8787

8888
let finalized_main: *const u8 = jit_module.get_finalized_function(main_func_id);
8989

90-
println!("Rustc codegen cranelift will JIT run the executable, because the CG_CLIF_JIT env var is set");
90+
println!("Rustc codegen cranelift will JIT run the executable, because --jit was passed");
9191

9292
let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
9393
unsafe { ::std::mem::transmute(finalized_main) };

src/driver/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ pub(crate) fn codegen_crate(
1616
tcx: TyCtxt<'_>,
1717
metadata: EncodedMetadata,
1818
need_metadata_module: bool,
19+
config: crate::BackendConfig,
1920
) -> Box<dyn Any> {
2021
tcx.sess.abort_if_errors();
2122

22-
if std::env::var("CG_CLIF_JIT").is_ok()
23-
&& tcx
23+
if config.use_jit {
24+
let is_executable = tcx
2425
.sess
2526
.crate_types()
26-
.contains(&rustc_session::config::CrateType::Executable)
27-
{
27+
.contains(&rustc_session::config::CrateType::Executable);
28+
if !is_executable {
29+
tcx.sess.fatal("can't jit non-executable crate");
30+
}
31+
2832
#[cfg(feature = "jit")]
2933
let _: ! = jit::run_jit(tcx);
3034

src/lib.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,14 @@ impl<'tcx, B: Backend + 'static> CodegenCx<'tcx, B> {
181181
}
182182
}
183183

184-
struct CraneliftCodegenBackend;
184+
#[derive(Copy, Clone, Debug)]
185+
pub struct BackendConfig {
186+
pub use_jit: bool,
187+
}
188+
189+
pub struct CraneliftCodegenBackend {
190+
pub config: BackendConfig,
191+
}
185192

186193
impl CodegenBackend for CraneliftCodegenBackend {
187194
fn init(&self, sess: &Session) {
@@ -223,7 +230,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
223230
metadata: EncodedMetadata,
224231
need_metadata_module: bool,
225232
) -> Box<dyn Any> {
226-
let res = driver::codegen_crate(tcx, metadata, need_metadata_module);
233+
let res = driver::codegen_crate(tcx, metadata, need_metadata_module, self.config);
227234

228235
rustc_symbol_mangling::test::report_symbol_names(tcx);
229236

@@ -345,5 +352,9 @@ fn build_isa(sess: &Session, enable_pic: bool) -> Box<dyn isa::TargetIsa + 'stat
345352
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
346353
#[no_mangle]
347354
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
348-
Box::new(CraneliftCodegenBackend)
355+
Box::new(CraneliftCodegenBackend {
356+
config: BackendConfig {
357+
use_jit: false,
358+
}
359+
})
349360
}

test.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ $RUSTC example/example.rs --crate-type lib --target $TARGET_TRIPLE
2929

3030
if [[ "$JIT_SUPPORTED" = "1" ]]; then
3131
echo "[JIT] mini_core_hello_world"
32-
CG_CLIF_JIT=1 CG_CLIF_JIT_ARGS="abc bcd" $RUSTC --crate-type bin -Cprefer-dynamic example/mini_core_hello_world.rs --cfg jit --target $HOST_TRIPLE
32+
CG_CLIF_JIT_ARGS="abc bcd" $RUSTC --jit example/mini_core_hello_world.rs --cfg jit --target $HOST_TRIPLE
3333
else
3434
echo "[JIT] mini_core_hello_world (skipped)"
3535
fi
@@ -52,7 +52,7 @@ $RUN_WRAPPER ./target/out/alloc_example
5252

5353
if [[ "$JIT_SUPPORTED" = "1" ]]; then
5454
echo "[JIT] std_example"
55-
CG_CLIF_JIT=1 $RUSTC --crate-type bin -Cprefer-dynamic example/std_example.rs --target $HOST_TRIPLE
55+
$RUSTC --jit example/std_example.rs --target $HOST_TRIPLE
5656
else
5757
echo "[JIT] std_example (skipped)"
5858
fi

0 commit comments

Comments
 (0)