Replace CG_CLIF_JIT with --jit
This commit is contained in:
parent
838dd17a67
commit
426e55709c
9 changed files with 69 additions and 23 deletions
21
Readme.md
21
Readme.md
|
@ -36,7 +36,24 @@ If you compiled cg_clif in debug mode (aka you didn't pass `--release` to `./tes
|
||||||
> You should prefer using the Cargo method.
|
> You should prefer using the Cargo method.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ 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
|
$ $cg_clif_dir/target/release/cg_clif my_crate.rs
|
||||||
|
```
|
||||||
|
|
||||||
|
### Jit mode
|
||||||
|
|
||||||
|
In jit mode cg_clif will immediately execute your code without creating an executable file.
|
||||||
|
This requires all dependencies to be available as dynamic library.
|
||||||
|
The easiest way to achieve this is by creating a new dylib crate that has all your dependencies as dependencies of itself.
|
||||||
|
Rustc will then link all your rlib dependencies into the dylib.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ $cg_clif_dir/cargo.sh jit
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ $cg_clif_dir/target/release/cg_clif --jit my_crate.rs
|
||||||
```
|
```
|
||||||
|
|
||||||
### Shell
|
### Shell
|
||||||
|
@ -45,7 +62,7 @@ These are a few functions that allow you to easily run rust code from the shell
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
function jit_naked() {
|
function jit_naked() {
|
||||||
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
|
echo "$@" | $cg_clif_dir/target/release/cg_clif - --jit
|
||||||
}
|
}
|
||||||
|
|
||||||
function jit() {
|
function jit() {
|
||||||
|
|
9
cargo.sh
9
cargo.sh
|
@ -12,12 +12,11 @@ TOOLCHAIN=$(cat rust-toolchain)
|
||||||
|
|
||||||
popd >/dev/null
|
popd >/dev/null
|
||||||
|
|
||||||
if [[ $(rustc -V) != $(rustc +${TOOLCHAIN} -V) ]]; then
|
|
||||||
echo "rustc_codegen_cranelift is build for $(rustc +${TOOLCHAIN} -V) but the default rustc version is $(rustc -V)."
|
|
||||||
echo "Using $(rustc +${TOOLCHAIN} -V)."
|
|
||||||
fi
|
|
||||||
|
|
||||||
cmd=$1
|
cmd=$1
|
||||||
shift
|
shift
|
||||||
|
|
||||||
|
if [[ "$cmd" = "jit" ]]; then
|
||||||
|
cargo +${TOOLCHAIN} rustc $@ -- --jit
|
||||||
|
else
|
||||||
cargo +${TOOLCHAIN} $cmd $@
|
cargo +${TOOLCHAIN} $cmd $@
|
||||||
|
fi
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
# List of env vars recognized by cg_clif
|
# List of env vars recognized by cg_clif
|
||||||
|
|
||||||
<dl>
|
<dl>
|
||||||
<dt>CG_CLIF_JIT</dt>
|
|
||||||
<dd>Enable JIT mode to immediately run a program instead of writing an executable file.</dd>
|
|
||||||
<dt>CG_CLIF_JIT_ARGS</dt>
|
<dt>CG_CLIF_JIT_ARGS</dt>
|
||||||
<dd>When JIT mode is enable pass these arguments to the program.</dd>
|
<dd>When JIT mode is enable pass these arguments to the program.</dd>
|
||||||
<dt>CG_CLIF_INCR_CACHE_DISABLED</dt>
|
<dt>CG_CLIF_INCR_CACHE_DISABLED</dt>
|
||||||
|
|
|
@ -5,7 +5,7 @@ CHANNEL="release"
|
||||||
pushd $(dirname "$0")/../
|
pushd $(dirname "$0")/../
|
||||||
source scripts/config.sh
|
source scripts/config.sh
|
||||||
popd
|
popd
|
||||||
CG_CLIF_JIT=1 PROFILE=$1 OUTPUT=$2 exec rustc $RUSTFLAGS $0 --crate-type bin -Cprefer-dynamic
|
PROFILE=$1 OUTPUT=$2 exec $RUSTC $RUSTFLAGS --jit $0
|
||||||
#*/
|
#*/
|
||||||
|
|
||||||
//! This program filters away uninteresting samples and trims uninteresting frames for stackcollapse
|
//! This program filters away uninteresting samples and trims uninteresting frames for stackcollapse
|
||||||
|
|
|
@ -50,7 +50,9 @@ fn main() {
|
||||||
let mut callbacks = TimePassesCallbacks::default();
|
let mut callbacks = TimePassesCallbacks::default();
|
||||||
rustc_driver::install_ice_hook();
|
rustc_driver::install_ice_hook();
|
||||||
let exit_code = rustc_driver::catch_with_exit_code(|| {
|
let exit_code = rustc_driver::catch_with_exit_code(|| {
|
||||||
let args = std::env::args_os()
|
let mut use_jit = false;
|
||||||
|
|
||||||
|
let mut args = std::env::args_os()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, arg)| {
|
.map(|(i, arg)| {
|
||||||
arg.into_string().unwrap_or_else(|arg| {
|
arg.into_string().unwrap_or_else(|arg| {
|
||||||
|
@ -60,14 +62,29 @@ fn main() {
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
.filter(|arg| {
|
||||||
|
if arg == "--jit" {
|
||||||
|
use_jit = true;
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
if use_jit {
|
||||||
|
args.push("-Cprefer-dynamic".to_string());
|
||||||
|
}
|
||||||
rustc_driver::run_compiler(
|
rustc_driver::run_compiler(
|
||||||
&args,
|
&args,
|
||||||
&mut callbacks,
|
&mut callbacks,
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
Some(Box::new(|_| {
|
Some(Box::new(move |_| {
|
||||||
rustc_codegen_cranelift::__rustc_codegen_backend()
|
Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend {
|
||||||
|
config: rustc_codegen_cranelift::BackendConfig {
|
||||||
|
use_jit,
|
||||||
|
}
|
||||||
|
})
|
||||||
})),
|
})),
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
|
@ -87,7 +87,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>) -> ! {
|
||||||
|
|
||||||
let finalized_main: *const u8 = jit_module.get_finalized_function(main_func_id);
|
let finalized_main: *const u8 = jit_module.get_finalized_function(main_func_id);
|
||||||
|
|
||||||
println!("Rustc codegen cranelift will JIT run the executable, because the CG_CLIF_JIT env var is set");
|
println!("Rustc codegen cranelift will JIT run the executable, because --jit was passed");
|
||||||
|
|
||||||
let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
|
let f: extern "C" fn(c_int, *const *const c_char) -> c_int =
|
||||||
unsafe { ::std::mem::transmute(finalized_main) };
|
unsafe { ::std::mem::transmute(finalized_main) };
|
||||||
|
|
|
@ -16,15 +16,19 @@ pub(crate) fn codegen_crate(
|
||||||
tcx: TyCtxt<'_>,
|
tcx: TyCtxt<'_>,
|
||||||
metadata: EncodedMetadata,
|
metadata: EncodedMetadata,
|
||||||
need_metadata_module: bool,
|
need_metadata_module: bool,
|
||||||
|
config: crate::BackendConfig,
|
||||||
) -> Box<dyn Any> {
|
) -> Box<dyn Any> {
|
||||||
tcx.sess.abort_if_errors();
|
tcx.sess.abort_if_errors();
|
||||||
|
|
||||||
if std::env::var("CG_CLIF_JIT").is_ok()
|
if config.use_jit {
|
||||||
&& tcx
|
let is_executable = tcx
|
||||||
.sess
|
.sess
|
||||||
.crate_types()
|
.crate_types()
|
||||||
.contains(&rustc_session::config::CrateType::Executable)
|
.contains(&rustc_session::config::CrateType::Executable);
|
||||||
{
|
if !is_executable {
|
||||||
|
tcx.sess.fatal("can't jit non-executable crate");
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "jit")]
|
#[cfg(feature = "jit")]
|
||||||
let _: ! = jit::run_jit(tcx);
|
let _: ! = jit::run_jit(tcx);
|
||||||
|
|
||||||
|
|
17
src/lib.rs
17
src/lib.rs
|
@ -181,7 +181,14 @@ impl<'tcx, B: Backend + 'static> CodegenCx<'tcx, B> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CraneliftCodegenBackend;
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub struct BackendConfig {
|
||||||
|
pub use_jit: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CraneliftCodegenBackend {
|
||||||
|
pub config: BackendConfig,
|
||||||
|
}
|
||||||
|
|
||||||
impl CodegenBackend for CraneliftCodegenBackend {
|
impl CodegenBackend for CraneliftCodegenBackend {
|
||||||
fn init(&self, sess: &Session) {
|
fn init(&self, sess: &Session) {
|
||||||
|
@ -223,7 +230,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
|
||||||
metadata: EncodedMetadata,
|
metadata: EncodedMetadata,
|
||||||
need_metadata_module: bool,
|
need_metadata_module: bool,
|
||||||
) -> Box<dyn Any> {
|
) -> Box<dyn Any> {
|
||||||
let res = driver::codegen_crate(tcx, metadata, need_metadata_module);
|
let res = driver::codegen_crate(tcx, metadata, need_metadata_module, self.config);
|
||||||
|
|
||||||
rustc_symbol_mangling::test::report_symbol_names(tcx);
|
rustc_symbol_mangling::test::report_symbol_names(tcx);
|
||||||
|
|
||||||
|
@ -345,5 +352,9 @@ fn build_isa(sess: &Session, enable_pic: bool) -> Box<dyn isa::TargetIsa + 'stat
|
||||||
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
|
/// This is the entrypoint for a hot plugged rustc_codegen_cranelift
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
|
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
|
||||||
Box::new(CraneliftCodegenBackend)
|
Box::new(CraneliftCodegenBackend {
|
||||||
|
config: BackendConfig {
|
||||||
|
use_jit: false,
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
4
test.sh
4
test.sh
|
@ -29,7 +29,7 @@ $RUSTC example/example.rs --crate-type lib --target $TARGET_TRIPLE
|
||||||
|
|
||||||
if [[ "$JIT_SUPPORTED" = "1" ]]; then
|
if [[ "$JIT_SUPPORTED" = "1" ]]; then
|
||||||
echo "[JIT] mini_core_hello_world"
|
echo "[JIT] mini_core_hello_world"
|
||||||
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
|
CG_CLIF_JIT_ARGS="abc bcd" $RUSTC --jit example/mini_core_hello_world.rs --cfg jit --target $HOST_TRIPLE
|
||||||
else
|
else
|
||||||
echo "[JIT] mini_core_hello_world (skipped)"
|
echo "[JIT] mini_core_hello_world (skipped)"
|
||||||
fi
|
fi
|
||||||
|
@ -52,7 +52,7 @@ $RUN_WRAPPER ./target/out/alloc_example
|
||||||
|
|
||||||
if [[ "$JIT_SUPPORTED" = "1" ]]; then
|
if [[ "$JIT_SUPPORTED" = "1" ]]; then
|
||||||
echo "[JIT] std_example"
|
echo "[JIT] std_example"
|
||||||
CG_CLIF_JIT=1 $RUSTC --crate-type bin -Cprefer-dynamic example/std_example.rs --target $HOST_TRIPLE
|
$RUSTC --jit example/std_example.rs --target $HOST_TRIPLE
|
||||||
else
|
else
|
||||||
echo "[JIT] std_example (skipped)"
|
echo "[JIT] std_example (skipped)"
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue