mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
Refactor the cli of cg_clif
This commit is contained in:
+2
-2
@@ -38,7 +38,7 @@ $ $cg_clif_dir/dist/cargo-clif jit
|
||||
or
|
||||
|
||||
```bash
|
||||
$ $cg_clif_dir/dist/rustc-clif -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic my_crate.rs
|
||||
$ $cg_clif_dir/dist/rustc-clif -Cllvm-args=jit-mode -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/dist/rustc-clif - -Zunstable-options -Cllvm-args=mode=jit-lazy -Cprefer-dynamic
|
||||
echo "$@" | $cg_clif_dir/dist/rustc-clif - -Zunstable-options -Cllvm-args=jit-mode -Cllvm-args=jit-lazy -Cprefer-dynamic
|
||||
}
|
||||
|
||||
function jit() {
|
||||
|
||||
@@ -50,7 +50,7 @@ fn main() {
|
||||
.chain([
|
||||
"--".to_string(),
|
||||
"-Zunstable-options".to_string(),
|
||||
"-Cllvm-args=mode=jit".to_string(),
|
||||
"-Cllvm-args=jit-mode".to_string(),
|
||||
])
|
||||
.collect()
|
||||
}
|
||||
@@ -62,7 +62,8 @@ fn main() {
|
||||
.chain([
|
||||
"--".to_string(),
|
||||
"-Zunstable-options".to_string(),
|
||||
"-Cllvm-args=mode=jit-lazy".to_string(),
|
||||
"-Cllvm-args=jit-mode".to_string(),
|
||||
"-Cllvm-args=jit-lazy".to_string(),
|
||||
])
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
pushd $(dirname "$0")/../
|
||||
RUSTC="$(pwd)/dist/rustc-clif"
|
||||
popd
|
||||
PROFILE=$1 OUTPUT=$2 exec $RUSTC -Zunstable-options -Cllvm-args=mode=jit -Cprefer-dynamic $0
|
||||
PROFILE=$1 OUTPUT=$2 exec $RUSTC -Zunstable-options -Cllvm-args=jit-mode -Cprefer-dynamic $0
|
||||
#*/
|
||||
|
||||
//! This program filters away uninteresting samples and trims uninteresting frames for stackcollapse
|
||||
|
||||
+13
-28
@@ -1,21 +1,15 @@
|
||||
/// The mode to use for compilation.
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum CodegenMode {
|
||||
/// AOT compile the crate. This is the default.
|
||||
Aot,
|
||||
/// JIT compile and execute the crate.
|
||||
Jit,
|
||||
/// JIT compile and execute the crate, but only compile functions the first time they are used.
|
||||
JitLazy,
|
||||
}
|
||||
|
||||
/// Configuration of cg_clif as passed in through `-Cllvm-args` and various env vars.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct BackendConfig {
|
||||
/// Should the crate be AOT compiled or JIT executed.
|
||||
///
|
||||
/// Defaults to AOT compilation. Can be set using `-Cllvm-args=mode=...`.
|
||||
pub codegen_mode: CodegenMode,
|
||||
/// Defaults to AOT compilation. Can be set using `-Cllvm-args=jit-mode`.
|
||||
pub jit_mode: bool,
|
||||
|
||||
/// When JIT executing should the lazy JIT mode be used.
|
||||
///
|
||||
/// Defaults to false. Can be set using `-Cllvm-args=jit-lazy`.
|
||||
pub lazy_jit: bool,
|
||||
|
||||
/// When JIT mode is enable pass these arguments to the program.
|
||||
///
|
||||
@@ -27,7 +21,8 @@ impl BackendConfig {
|
||||
/// Parse the configuration passed in using `-Cllvm-args`.
|
||||
pub fn from_opts(opts: &[String]) -> Result<Self, String> {
|
||||
let mut config = BackendConfig {
|
||||
codegen_mode: CodegenMode::Aot,
|
||||
jit_mode: false,
|
||||
lazy_jit: false,
|
||||
jit_args: match std::env::var("CG_CLIF_JIT_ARGS") {
|
||||
Ok(args) => args.split(' ').map(|arg| arg.to_string()).collect(),
|
||||
Err(std::env::VarError::NotPresent) => vec![],
|
||||
@@ -43,20 +38,10 @@ pub fn from_opts(opts: &[String]) -> Result<Self, String> {
|
||||
// testing cg_clif.
|
||||
continue;
|
||||
}
|
||||
if let Some((name, value)) = opt.split_once('=') {
|
||||
match name {
|
||||
"mode" => {
|
||||
config.codegen_mode = match value {
|
||||
"aot" => CodegenMode::Aot,
|
||||
"jit" => CodegenMode::Jit,
|
||||
"jit-lazy" => CodegenMode::JitLazy,
|
||||
_ => return Err(format!("Unknown codegen mode `{}`", value)),
|
||||
};
|
||||
}
|
||||
_ => return Err(format!("Unknown option `{}`", name)),
|
||||
}
|
||||
} else {
|
||||
return Err(format!("Invalid option `{}`", opt));
|
||||
match &**opt {
|
||||
"jit-mode" => config.jit_mode = true,
|
||||
"jit-lazy" => config.lazy_jit = true,
|
||||
_ => return Err(format!("Unknown option `{}`", opt)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-11
@@ -13,10 +13,10 @@
|
||||
use rustc_session::Session;
|
||||
use rustc_span::sym;
|
||||
|
||||
use crate::CodegenCx;
|
||||
use crate::debuginfo::TypeDebugContext;
|
||||
use crate::prelude::*;
|
||||
use crate::unwind_module::UnwindModule;
|
||||
use crate::{CodegenCx, CodegenMode};
|
||||
|
||||
struct JitState {
|
||||
jit_module: UnwindModule<JITModule>,
|
||||
@@ -79,7 +79,7 @@ fn create_jit_module(tcx: TyCtxt<'_>, hotswap: bool) -> (UnwindModule<JITModule>
|
||||
(jit_module, cx)
|
||||
}
|
||||
|
||||
pub(crate) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode, jit_args: Vec<String>) -> ! {
|
||||
pub(crate) fn run_jit(tcx: TyCtxt<'_>, jit_lazy: bool, jit_args: Vec<String>) -> ! {
|
||||
if !tcx.sess.opts.output_types.should_codegen() {
|
||||
tcx.dcx().fatal("JIT mode doesn't work with `cargo check`");
|
||||
}
|
||||
@@ -88,8 +88,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode, jit_args: Vec<
|
||||
tcx.dcx().fatal("can't jit non-executable crate");
|
||||
}
|
||||
|
||||
let (mut jit_module, mut cx) =
|
||||
create_jit_module(tcx, matches!(codegen_mode, CodegenMode::JitLazy));
|
||||
let (mut jit_module, mut cx) = create_jit_module(tcx, jit_lazy);
|
||||
let mut cached_context = Context::new();
|
||||
|
||||
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<
|
||||
super::predefine_mono_items(tcx, &mut jit_module, &mono_items);
|
||||
for (mono_item, _) in mono_items {
|
||||
match mono_item {
|
||||
MonoItem::Fn(inst) => match codegen_mode {
|
||||
CodegenMode::Aot => unreachable!(),
|
||||
CodegenMode::Jit => {
|
||||
MonoItem::Fn(inst) => {
|
||||
if jit_lazy {
|
||||
codegen_shim(tcx, &mut cached_context, &mut jit_module, inst)
|
||||
} else {
|
||||
codegen_and_compile_fn(
|
||||
tcx,
|
||||
&mut cx,
|
||||
@@ -116,10 +116,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode, jit_args: Vec<
|
||||
inst,
|
||||
);
|
||||
}
|
||||
CodegenMode::JitLazy => {
|
||||
codegen_shim(tcx, &mut cached_context, &mut jit_module, inst)
|
||||
}
|
||||
},
|
||||
}
|
||||
MonoItem::Static(def_id) => {
|
||||
crate::constant::codegen_static(tcx, &mut jit_module, def_id);
|
||||
}
|
||||
|
||||
+7
-8
@@ -213,15 +213,14 @@ fn codegen_crate(
|
||||
BackendConfig::from_opts(&tcx.sess.opts.cg.llvm_args)
|
||||
.unwrap_or_else(|err| tcx.sess.dcx().fatal(err))
|
||||
});
|
||||
match config.codegen_mode {
|
||||
CodegenMode::Aot => driver::aot::run_aot(tcx, metadata, need_metadata_module),
|
||||
CodegenMode::Jit | CodegenMode::JitLazy => {
|
||||
#[cfg(feature = "jit")]
|
||||
driver::jit::run_jit(tcx, config.codegen_mode, config.jit_args);
|
||||
if config.jit_mode {
|
||||
#[cfg(feature = "jit")]
|
||||
driver::jit::run_jit(tcx, config.lazy_jit, config.jit_args);
|
||||
|
||||
#[cfg(not(feature = "jit"))]
|
||||
tcx.dcx().fatal("jit support was disabled when compiling rustc_codegen_cranelift");
|
||||
}
|
||||
#[cfg(not(feature = "jit"))]
|
||||
tcx.dcx().fatal("jit support was disabled when compiling rustc_codegen_cranelift");
|
||||
} else {
|
||||
driver::aot::run_aot(tcx, metadata, need_metadata_module)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user