Skip to content

Commit 0fcd068

Browse files
committed
Refactor the cli of cg_clif
1 parent 0f9c09f commit 0fcd068

File tree

6 files changed

+35
-53
lines changed

6 files changed

+35
-53
lines changed

docs/usage.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ $ $cg_clif_dir/dist/cargo-clif jit
3838
or
3939

4040
```bash
41-
$ $cg_clif_dir/dist/rustc-clif -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic my_crate.rs
41+
$ $cg_clif_dir/dist/rustc-clif -Cllvm-args=jit-mode -Cprefer-dynamic my_crate.rs
4242
```
4343

4444
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
5454

5555
```bash
5656
function jit_naked() {
57-
echo "$@" | $cg_clif_dir/dist/rustc-clif - -Zunstable-options -Cllvm-args=mode=jit-lazy -Cprefer-dynamic
57+
echo "$@" | $cg_clif_dir/dist/rustc-clif - -Zunstable-options -Cllvm-args=jit-mode -Cllvm-args=jit-lazy -Cprefer-dynamic
5858
}
5959

6060
function jit() {

scripts/cargo-clif.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn main() {
5050
.chain([
5151
"--".to_string(),
5252
"-Zunstable-options".to_string(),
53-
"-Cllvm-args=mode=jit".to_string(),
53+
"-Cllvm-args=jit-mode".to_string(),
5454
])
5555
.collect()
5656
}
@@ -62,7 +62,8 @@ fn main() {
6262
.chain([
6363
"--".to_string(),
6464
"-Zunstable-options".to_string(),
65-
"-Cllvm-args=mode=jit-lazy".to_string(),
65+
"-Cllvm-args=jit-mode".to_string(),
66+
"-Cllvm-args=jit-lazy".to_string(),
6667
])
6768
.collect()
6869
}

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
RUSTC="$(pwd)/dist/rustc-clif"
66
popd
7-
PROFILE=$1 OUTPUT=$2 exec $RUSTC -Zunstable-options -Cllvm-args=mode=jit -Cprefer-dynamic $0
7+
PROFILE=$1 OUTPUT=$2 exec $RUSTC -Zunstable-options -Cllvm-args=jit-mode -Cprefer-dynamic $0
88
#*/
99

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

src/config.rs

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
/// The mode to use for compilation.
2-
#[derive(Copy, Clone, Debug)]
3-
pub enum CodegenMode {
4-
/// AOT compile the crate. This is the default.
5-
Aot,
6-
/// JIT compile and execute the crate.
7-
Jit,
8-
/// JIT compile and execute the crate, but only compile functions the first time they are used.
9-
JitLazy,
10-
}
11-
121
/// Configuration of cg_clif as passed in through `-Cllvm-args` and various env vars.
132
#[derive(Clone, Debug)]
143
pub struct BackendConfig {
154
/// Should the crate be AOT compiled or JIT executed.
165
///
17-
/// Defaults to AOT compilation. Can be set using `-Cllvm-args=mode=...`.
18-
pub codegen_mode: CodegenMode,
6+
/// Defaults to AOT compilation. Can be set using `-Cllvm-args=jit-mode`.
7+
pub jit_mode: bool,
8+
9+
/// When JIT executing should the lazy JIT mode be used.
10+
///
11+
/// Defaults to false. Can be set using `-Cllvm-args=jit-lazy`.
12+
pub lazy_jit: bool,
1913

2014
/// When JIT mode is enable pass these arguments to the program.
2115
///
@@ -27,7 +21,8 @@ impl BackendConfig {
2721
/// Parse the configuration passed in using `-Cllvm-args`.
2822
pub fn from_opts(opts: &[String]) -> Result<Self, String> {
2923
let mut config = BackendConfig {
30-
codegen_mode: CodegenMode::Aot,
24+
jit_mode: false,
25+
lazy_jit: false,
3126
jit_args: match std::env::var("CG_CLIF_JIT_ARGS") {
3227
Ok(args) => args.split(' ').map(|arg| arg.to_string()).collect(),
3328
Err(std::env::VarError::NotPresent) => vec![],
@@ -43,20 +38,10 @@ impl BackendConfig {
4338
// testing cg_clif.
4439
continue;
4540
}
46-
if let Some((name, value)) = opt.split_once('=') {
47-
match name {
48-
"mode" => {
49-
config.codegen_mode = match value {
50-
"aot" => CodegenMode::Aot,
51-
"jit" => CodegenMode::Jit,
52-
"jit-lazy" => CodegenMode::JitLazy,
53-
_ => return Err(format!("Unknown codegen mode `{}`", value)),
54-
};
55-
}
56-
_ => return Err(format!("Unknown option `{}`", name)),
57-
}
58-
} else {
59-
return Err(format!("Invalid option `{}`", opt));
41+
match &**opt {
42+
"jit-mode" => config.jit_mode = true,
43+
"jit-lazy" => config.lazy_jit = true,
44+
_ => return Err(format!("Unknown option `{}`", opt)),
6045
}
6146
}
6247

src/driver/jit.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use rustc_middle::mir::mono::MonoItem;
1313
use rustc_session::Session;
1414
use rustc_span::sym;
1515

16+
use crate::CodegenCx;
1617
use crate::debuginfo::TypeDebugContext;
1718
use crate::prelude::*;
1819
use crate::unwind_module::UnwindModule;
19-
use crate::{CodegenCx, CodegenMode};
2020

2121
struct JitState {
2222
jit_module: UnwindModule<JITModule>,
@@ -79,7 +79,7 @@ fn create_jit_module(tcx: TyCtxt<'_>, hotswap: bool) -> (UnwindModule<JITModule>
7979
(jit_module, cx)
8080
}
8181

82-
pub(crate) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode, jit_args: Vec<String>) -> ! {
82+
pub(crate) fn run_jit(tcx: TyCtxt<'_>, jit_lazy: bool, jit_args: Vec<String>) -> ! {
8383
if !tcx.sess.opts.output_types.should_codegen() {
8484
tcx.dcx().fatal("JIT mode doesn't work with `cargo check`");
8585
}
@@ -88,8 +88,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode, jit_args: Vec<
8888
tcx.dcx().fatal("can't jit non-executable crate");
8989
}
9090

91-
let (mut jit_module, mut cx) =
92-
create_jit_module(tcx, matches!(codegen_mode, CodegenMode::JitLazy));
91+
let (mut jit_module, mut cx) = create_jit_module(tcx, jit_lazy);
9392
let mut cached_context = Context::new();
9493

9594
let cgus = tcx.collect_and_partition_mono_items(()).codegen_units;
@@ -105,9 +104,10 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode, jit_args: Vec<
105104
super::predefine_mono_items(tcx, &mut jit_module, &mono_items);
106105
for (mono_item, _) in mono_items {
107106
match mono_item {
108-
MonoItem::Fn(inst) => match codegen_mode {
109-
CodegenMode::Aot => unreachable!(),
110-
CodegenMode::Jit => {
107+
MonoItem::Fn(inst) => {
108+
if jit_lazy {
109+
codegen_shim(tcx, &mut cached_context, &mut jit_module, inst)
110+
} else {
111111
codegen_and_compile_fn(
112112
tcx,
113113
&mut cx,
@@ -116,10 +116,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode, jit_args: Vec<
116116
inst,
117117
);
118118
}
119-
CodegenMode::JitLazy => {
120-
codegen_shim(tcx, &mut cached_context, &mut jit_module, inst)
121-
}
122-
},
119+
}
123120
MonoItem::Static(def_id) => {
124121
crate::constant::codegen_static(tcx, &mut jit_module, def_id);
125122
}

src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,14 @@ impl CodegenBackend for CraneliftCodegenBackend {
213213
BackendConfig::from_opts(&tcx.sess.opts.cg.llvm_args)
214214
.unwrap_or_else(|err| tcx.sess.dcx().fatal(err))
215215
});
216-
match config.codegen_mode {
217-
CodegenMode::Aot => driver::aot::run_aot(tcx, metadata, need_metadata_module),
218-
CodegenMode::Jit | CodegenMode::JitLazy => {
219-
#[cfg(feature = "jit")]
220-
driver::jit::run_jit(tcx, config.codegen_mode, config.jit_args);
221-
222-
#[cfg(not(feature = "jit"))]
223-
tcx.dcx().fatal("jit support was disabled when compiling rustc_codegen_cranelift");
224-
}
216+
if config.jit_mode {
217+
#[cfg(feature = "jit")]
218+
driver::jit::run_jit(tcx, config.lazy_jit, config.jit_args);
219+
220+
#[cfg(not(feature = "jit"))]
221+
tcx.dcx().fatal("jit support was disabled when compiling rustc_codegen_cranelift");
222+
} else {
223+
driver::aot::run_aot(tcx, metadata, need_metadata_module)
225224
}
226225
}
227226

0 commit comments

Comments
 (0)