Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change the way JIT mode is selected
  • Loading branch information
bjorn3 committed Dec 25, 2020
1 parent c556e4d commit 20ffea6
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 49 deletions.
4 changes: 2 additions & 2 deletions Readme.md
Expand Up @@ -68,7 +68,7 @@ $ $cg_clif_dir/build/cargo.sh jit
or

```bash
$ $cg_clif_dir/build/bin/cg_clif --jit my_crate.rs
$ $cg_clif_dir/build/bin/cg_clif -Cllvm-args=mode=jit -Cprefer-dynamic my_crate.rs
```

### Shell
Expand All @@ -77,7 +77,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/build/bin/cg_clif - --jit
echo "$@" | $cg_clif_dir/build/bin/cg_clif - -Cllvm-args=mode=jit -Cprefer-dynamic
}

function jit() {
Expand Down
2 changes: 1 addition & 1 deletion scripts/cargo.sh
Expand Up @@ -10,7 +10,7 @@ cmd=$1
shift || true

if [[ "$cmd" = "jit" ]]; then
cargo "+${TOOLCHAIN}" rustc "$@" -- --jit
cargo "+${TOOLCHAIN}" rustc "$@" -- -Cllvm-args=mode=jit -Cprefer-dynamic
else
cargo "+${TOOLCHAIN}" "$cmd" "$@"
fi
2 changes: 1 addition & 1 deletion scripts/filter_profile.rs
Expand Up @@ -4,7 +4,7 @@
pushd $(dirname "$0")/../
source build/config.sh
popd
PROFILE=$1 OUTPUT=$2 exec $RUSTC $RUSTFLAGS --jit $0
PROFILE=$1 OUTPUT=$2 exec $RUSTC $RUSTFLAGS -Cllvm-args=mode=jit -Cprefer-dynamic $0
#*/

//! This program filters away uninteresting samples and trims uninteresting frames for stackcollapse
Expand Down
4 changes: 2 additions & 2 deletions scripts/tests.sh
Expand Up @@ -15,7 +15,7 @@ function no_sysroot_tests() {

if [[ "$JIT_SUPPORTED" = "1" ]]; then
echo "[JIT] mini_core_hello_world"
CG_CLIF_JIT_ARGS="abc bcd" $MY_RUSTC --jit example/mini_core_hello_world.rs --cfg jit --target "$HOST_TRIPLE"
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"
else
echo "[JIT] mini_core_hello_world (skipped)"
fi
Expand All @@ -37,7 +37,7 @@ function base_sysroot_tests() {

if [[ "$JIT_SUPPORTED" = "1" ]]; then
echo "[JIT] std_example"
$MY_RUSTC --jit example/std_example.rs --target "$HOST_TRIPLE"
$MY_RUSTC -Cllvm-args=mode=jit -Cprefer-dynamic example/std_example.rs --target "$HOST_TRIPLE"
else
echo "[JIT] std_example (skipped)"
fi
Expand Down
19 changes: 2 additions & 17 deletions src/bin/cg_clif.rs
Expand Up @@ -44,9 +44,7 @@ fn main() {
let mut callbacks = CraneliftPassesCallbacks::default();
rustc_driver::install_ice_hook();
let exit_code = rustc_driver::catch_with_exit_code(|| {
let mut use_jit = false;

let mut args = std::env::args_os()
let args = std::env::args_os()
.enumerate()
.map(|(i, arg)| {
arg.into_string().unwrap_or_else(|arg| {
Expand All @@ -56,23 +54,10 @@ fn main() {
)
})
})
.filter(|arg| {
if arg == "--jit" {
use_jit = true;
false
} else {
true
}
})
.collect::<Vec<_>>();
if use_jit {
args.push("-Cprefer-dynamic".to_string());
}
let mut run_compiler = rustc_driver::RunCompiler::new(&args, &mut callbacks);
run_compiler.set_make_codegen_backend(Some(Box::new(move |_| {
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend {
config: rustc_codegen_cranelift::BackendConfig { use_jit },
})
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { config: None })
})));
run_compiler.run()
});
Expand Down
4 changes: 1 addition & 3 deletions src/bin/cg_clif_build_sysroot.rs
Expand Up @@ -92,9 +92,7 @@ fn main() {
let mut run_compiler = rustc_driver::RunCompiler::new(&args, &mut callbacks);
if use_clif {
run_compiler.set_make_codegen_backend(Some(Box::new(move |_| {
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend {
config: rustc_codegen_cranelift::BackendConfig { use_jit: false },
})
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { config: None })
})));
}
run_compiler.run()
Expand Down
2 changes: 1 addition & 1 deletion src/driver/jit.rs
Expand Up @@ -106,7 +106,7 @@ pub(super) 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 --jit was passed");
println!("Rustc codegen cranelift will JIT run the executable, because -Cllvm-args=mode=jit was passed");

let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
unsafe { ::std::mem::transmute(finalized_main) };
Expand Down
32 changes: 17 additions & 15 deletions src/driver/mod.rs
Expand Up @@ -7,6 +7,7 @@ use rustc_middle::middle::cstore::EncodedMetadata;
use rustc_middle::mir::mono::{Linkage as RLinkage, MonoItem, Visibility};

use crate::prelude::*;
use crate::CodegenMode;

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

if config.use_jit {
let is_executable = tcx
.sess
.crate_types()
.contains(&rustc_session::config::CrateType::Executable);
if !is_executable {
tcx.sess.fatal("can't jit non-executable crate");
}
match config.codegen_mode {
CodegenMode::Aot => aot::run_aot(tcx, metadata, need_metadata_module),
CodegenMode::Jit => {
let is_executable = tcx
.sess
.crate_types()
.contains(&rustc_session::config::CrateType::Executable);
if !is_executable {
tcx.sess.fatal("can't jit non-executable crate");
}

#[cfg(feature = "jit")]
let _: ! = jit::run_jit(tcx);
#[cfg(feature = "jit")]
let _: ! = jit::run_jit(tcx);

#[cfg(not(feature = "jit"))]
tcx.sess
.fatal("jit support was disabled when compiling rustc_codegen_cranelift");
#[cfg(not(feature = "jit"))]
tcx.sess
.fatal("jit support was disabled when compiling rustc_codegen_cranelift");
}
}

aot::run_aot(tcx, metadata, need_metadata_module)
}

fn predefine_mono_items<'tcx>(
Expand Down
61 changes: 54 additions & 7 deletions src/lib.rs
Expand Up @@ -5,7 +5,8 @@
associated_type_bounds,
never_type,
try_blocks,
hash_drain_filter
hash_drain_filter,
str_split_once
)]
#![warn(rust_2018_idioms)]
#![warn(unused_lifetimes)]
Expand Down Expand Up @@ -34,6 +35,7 @@ extern crate rustc_target;
extern crate rustc_driver;

use std::any::Any;
use std::str::FromStr;

use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_codegen_ssa::CodegenResults;
Expand Down Expand Up @@ -172,12 +174,53 @@ impl<'tcx, M: Module> CodegenCx<'tcx, M> {
}

#[derive(Copy, Clone, Debug)]
pub enum CodegenMode {
Aot,
Jit,
}

impl Default for CodegenMode {
fn default() -> Self {
CodegenMode::Aot
}
}

impl FromStr for CodegenMode {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"aot" => Ok(CodegenMode::Aot),
"jit" => Ok(CodegenMode::Jit),
_ => Err(format!("Unknown codegen mode `{}`", s)),
}
}
}

#[derive(Copy, Clone, Debug, Default)]
pub struct BackendConfig {
pub use_jit: bool,
pub codegen_mode: CodegenMode,
}

impl BackendConfig {
fn from_opts(opts: &[String]) -> Result<Self, String> {
let mut config = BackendConfig::default();
for opt in opts {
if let Some((name, value)) = opt.split_once('=') {
match name {
"mode" => config.codegen_mode = value.parse()?,
_ => return Err(format!("Unknown option `{}`", name)),
}
} else {
return Err(format!("Invalid option `{}`", opt));
}
}
Ok(config)
}
}

pub struct CraneliftCodegenBackend {
pub config: BackendConfig,
pub config: Option<BackendConfig>,
}

impl CodegenBackend for CraneliftCodegenBackend {
Expand All @@ -204,7 +247,13 @@ impl CodegenBackend for CraneliftCodegenBackend {
metadata: EncodedMetadata,
need_metadata_module: bool,
) -> Box<dyn Any> {
let res = driver::codegen_crate(tcx, metadata, need_metadata_module, self.config);
let config = if let Some(config) = self.config {
config
} else {
BackendConfig::from_opts(&tcx.sess.opts.cg.llvm_args)
.unwrap_or_else(|err| tcx.sess.fatal(&err))
};
let res = driver::codegen_crate(tcx, metadata, need_metadata_module, config);

rustc_symbol_mangling::test::report_symbol_names(tcx);

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

0 comments on commit 20ffea6

Please sign in to comment.