Skip to content

Commit 20ffea6

Browse files
committed
Change the way JIT mode is selected
1 parent c556e4d commit 20ffea6

File tree

9 files changed

+81
-49
lines changed

9 files changed

+81
-49
lines changed

Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ $ $cg_clif_dir/build/cargo.sh jit
6868
or
6969

7070
```bash
71-
$ $cg_clif_dir/build/bin/cg_clif --jit my_crate.rs
71+
$ $cg_clif_dir/build/bin/cg_clif -Cllvm-args=mode=jit -Cprefer-dynamic my_crate.rs
7272
```
7373

7474
### Shell
@@ -77,7 +77,7 @@ These are a few functions that allow you to easily run rust code from the shell
7777

7878
```bash
7979
function jit_naked() {
80-
echo "$@" | $cg_clif_dir/build/bin/cg_clif - --jit
80+
echo "$@" | $cg_clif_dir/build/bin/cg_clif - -Cllvm-args=mode=jit -Cprefer-dynamic
8181
}
8282

8383
function jit() {

scripts/cargo.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ cmd=$1
1010
shift || true
1111

1212
if [[ "$cmd" = "jit" ]]; then
13-
cargo "+${TOOLCHAIN}" rustc "$@" -- --jit
13+
cargo "+${TOOLCHAIN}" rustc "$@" -- -Cllvm-args=mode=jit -Cprefer-dynamic
1414
else
1515
cargo "+${TOOLCHAIN}" "$cmd" "$@"
1616
fi

scripts/filter_profile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
pushd $(dirname "$0")/../
55
source build/config.sh
66
popd
7-
PROFILE=$1 OUTPUT=$2 exec $RUSTC $RUSTFLAGS --jit $0
7+
PROFILE=$1 OUTPUT=$2 exec $RUSTC $RUSTFLAGS -Cllvm-args=mode=jit -Cprefer-dynamic $0
88
#*/
99

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

scripts/tests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function no_sysroot_tests() {
1515

1616
if [[ "$JIT_SUPPORTED" = "1" ]]; then
1717
echo "[JIT] mini_core_hello_world"
18-
CG_CLIF_JIT_ARGS="abc bcd" $MY_RUSTC --jit example/mini_core_hello_world.rs --cfg jit --target "$HOST_TRIPLE"
18+
CG_CLIF_JIT_ARGS="abc bcd" $MY_RUSTC -Cllvm-args=mode=jit -Cprefer-dynamic example/mini_core_hello_world.rs --cfg jit --target "$HOST_TRIPLE"
1919
else
2020
echo "[JIT] mini_core_hello_world (skipped)"
2121
fi
@@ -37,7 +37,7 @@ function base_sysroot_tests() {
3737

3838
if [[ "$JIT_SUPPORTED" = "1" ]]; then
3939
echo "[JIT] std_example"
40-
$MY_RUSTC --jit example/std_example.rs --target "$HOST_TRIPLE"
40+
$MY_RUSTC -Cllvm-args=mode=jit -Cprefer-dynamic example/std_example.rs --target "$HOST_TRIPLE"
4141
else
4242
echo "[JIT] std_example (skipped)"
4343
fi

src/bin/cg_clif.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ fn main() {
4444
let mut callbacks = CraneliftPassesCallbacks::default();
4545
rustc_driver::install_ice_hook();
4646
let exit_code = rustc_driver::catch_with_exit_code(|| {
47-
let mut use_jit = false;
48-
49-
let mut args = std::env::args_os()
47+
let args = std::env::args_os()
5048
.enumerate()
5149
.map(|(i, arg)| {
5250
arg.into_string().unwrap_or_else(|arg| {
@@ -56,23 +54,10 @@ fn main() {
5654
)
5755
})
5856
})
59-
.filter(|arg| {
60-
if arg == "--jit" {
61-
use_jit = true;
62-
false
63-
} else {
64-
true
65-
}
66-
})
6757
.collect::<Vec<_>>();
68-
if use_jit {
69-
args.push("-Cprefer-dynamic".to_string());
70-
}
7158
let mut run_compiler = rustc_driver::RunCompiler::new(&args, &mut callbacks);
7259
run_compiler.set_make_codegen_backend(Some(Box::new(move |_| {
73-
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend {
74-
config: rustc_codegen_cranelift::BackendConfig { use_jit },
75-
})
60+
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { config: None })
7661
})));
7762
run_compiler.run()
7863
});

src/bin/cg_clif_build_sysroot.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ fn main() {
9292
let mut run_compiler = rustc_driver::RunCompiler::new(&args, &mut callbacks);
9393
if use_clif {
9494
run_compiler.set_make_codegen_backend(Some(Box::new(move |_| {
95-
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend {
96-
config: rustc_codegen_cranelift::BackendConfig { use_jit: false },
97-
})
95+
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { config: None })
9896
})));
9997
}
10098
run_compiler.run()

src/driver/jit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>) -> ! {
106106

107107
let finalized_main: *const u8 = jit_module.get_finalized_function(main_func_id);
108108

109-
println!("Rustc codegen cranelift will JIT run the executable, because --jit was passed");
109+
println!("Rustc codegen cranelift will JIT run the executable, because -Cllvm-args=mode=jit was passed");
110110

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

src/driver/mod.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_middle::middle::cstore::EncodedMetadata;
77
use rustc_middle::mir::mono::{Linkage as RLinkage, MonoItem, Visibility};
88

99
use crate::prelude::*;
10+
use crate::CodegenMode;
1011

1112
mod aot;
1213
#[cfg(feature = "jit")]
@@ -20,24 +21,25 @@ pub(crate) fn codegen_crate(
2021
) -> Box<dyn Any> {
2122
tcx.sess.abort_if_errors();
2223

23-
if config.use_jit {
24-
let is_executable = tcx
25-
.sess
26-
.crate_types()
27-
.contains(&rustc_session::config::CrateType::Executable);
28-
if !is_executable {
29-
tcx.sess.fatal("can't jit non-executable crate");
30-
}
24+
match config.codegen_mode {
25+
CodegenMode::Aot => aot::run_aot(tcx, metadata, need_metadata_module),
26+
CodegenMode::Jit => {
27+
let is_executable = tcx
28+
.sess
29+
.crate_types()
30+
.contains(&rustc_session::config::CrateType::Executable);
31+
if !is_executable {
32+
tcx.sess.fatal("can't jit non-executable crate");
33+
}
3134

32-
#[cfg(feature = "jit")]
33-
let _: ! = jit::run_jit(tcx);
35+
#[cfg(feature = "jit")]
36+
let _: ! = jit::run_jit(tcx);
3437

35-
#[cfg(not(feature = "jit"))]
36-
tcx.sess
37-
.fatal("jit support was disabled when compiling rustc_codegen_cranelift");
38+
#[cfg(not(feature = "jit"))]
39+
tcx.sess
40+
.fatal("jit support was disabled when compiling rustc_codegen_cranelift");
41+
}
3842
}
39-
40-
aot::run_aot(tcx, metadata, need_metadata_module)
4143
}
4244

4345
fn predefine_mono_items<'tcx>(

src/lib.rs

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
associated_type_bounds,
66
never_type,
77
try_blocks,
8-
hash_drain_filter
8+
hash_drain_filter,
9+
str_split_once
910
)]
1011
#![warn(rust_2018_idioms)]
1112
#![warn(unused_lifetimes)]
@@ -34,6 +35,7 @@ extern crate rustc_target;
3435
extern crate rustc_driver;
3536

3637
use std::any::Any;
38+
use std::str::FromStr;
3739

3840
use rustc_codegen_ssa::traits::CodegenBackend;
3941
use rustc_codegen_ssa::CodegenResults;
@@ -172,12 +174,53 @@ impl<'tcx, M: Module> CodegenCx<'tcx, M> {
172174
}
173175

174176
#[derive(Copy, Clone, Debug)]
177+
pub enum CodegenMode {
178+
Aot,
179+
Jit,
180+
}
181+
182+
impl Default for CodegenMode {
183+
fn default() -> Self {
184+
CodegenMode::Aot
185+
}
186+
}
187+
188+
impl FromStr for CodegenMode {
189+
type Err = String;
190+
191+
fn from_str(s: &str) -> Result<Self, Self::Err> {
192+
match s {
193+
"aot" => Ok(CodegenMode::Aot),
194+
"jit" => Ok(CodegenMode::Jit),
195+
_ => Err(format!("Unknown codegen mode `{}`", s)),
196+
}
197+
}
198+
}
199+
200+
#[derive(Copy, Clone, Debug, Default)]
175201
pub struct BackendConfig {
176-
pub use_jit: bool,
202+
pub codegen_mode: CodegenMode,
203+
}
204+
205+
impl BackendConfig {
206+
fn from_opts(opts: &[String]) -> Result<Self, String> {
207+
let mut config = BackendConfig::default();
208+
for opt in opts {
209+
if let Some((name, value)) = opt.split_once('=') {
210+
match name {
211+
"mode" => config.codegen_mode = value.parse()?,
212+
_ => return Err(format!("Unknown option `{}`", name)),
213+
}
214+
} else {
215+
return Err(format!("Invalid option `{}`", opt));
216+
}
217+
}
218+
Ok(config)
219+
}
177220
}
178221

179222
pub struct CraneliftCodegenBackend {
180-
pub config: BackendConfig,
223+
pub config: Option<BackendConfig>,
181224
}
182225

183226
impl CodegenBackend for CraneliftCodegenBackend {
@@ -204,7 +247,13 @@ impl CodegenBackend for CraneliftCodegenBackend {
204247
metadata: EncodedMetadata,
205248
need_metadata_module: bool,
206249
) -> Box<dyn Any> {
207-
let res = driver::codegen_crate(tcx, metadata, need_metadata_module, self.config);
250+
let config = if let Some(config) = self.config {
251+
config
252+
} else {
253+
BackendConfig::from_opts(&tcx.sess.opts.cg.llvm_args)
254+
.unwrap_or_else(|err| tcx.sess.fatal(&err))
255+
};
256+
let res = driver::codegen_crate(tcx, metadata, need_metadata_module, config);
208257

209258
rustc_symbol_mangling::test::report_symbol_names(tcx);
210259

@@ -305,7 +354,5 @@ fn build_isa(sess: &Session) -> Box<dyn isa::TargetIsa + 'static> {
305354
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
306355
#[no_mangle]
307356
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
308-
Box::new(CraneliftCodegenBackend {
309-
config: BackendConfig { use_jit: false },
310-
})
357+
Box::new(CraneliftCodegenBackend { config: None })
311358
}

0 commit comments

Comments
 (0)