Skip to content

Commit 2ca8daa

Browse files
authored
Merge pull request #1120 from bjorn3/lazy_jit
Lazy compilation in jit mode
2 parents 510616f + 35f4a25 commit 2ca8daa

16 files changed

+260
-88
lines changed

Readme.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ $ $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
72+
```
73+
74+
There is also an experimental lazy jit mode. In this mode functions are only compiled once they are
75+
first called. It currently does not work with multi-threaded programs. When a not yet compiled
76+
function is called from another thread than the main thread, you will get an ICE.
77+
78+
```bash
79+
$ $cg_clif_dir/build/cargo.sh lazy-jit
7280
```
7381

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

7886
```bash
7987
function jit_naked() {
80-
echo "$@" | $cg_clif_dir/build/bin/cg_clif - --jit
88+
echo "$@" | $cg_clif_dir/build/bin/cg_clif - -Cllvm-args=mode=jit -Cprefer-dynamic
8189
}
8290

8391
function jit() {

example/std_example.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ fn main() {
1515
let stderr = ::std::io::stderr();
1616
let mut stderr = stderr.lock();
1717

18+
// FIXME support lazy jit when multi threading
19+
#[cfg(not(lazy_jit))]
1820
std::thread::spawn(move || {
1921
println!("Hello from another thread!");
2022
});

scripts/cargo.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ 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
14+
elif [[ "$cmd" = "lazy-jit" ]]; then
15+
cargo "+${TOOLCHAIN}" rustc "$@" -- -Cllvm-args=mode=jit-lazy -Cprefer-dynamic
1416
else
1517
cargo "+${TOOLCHAIN}" "$cmd" "$@"
1618
fi

scripts/filter_profile.rs

+1-1
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

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ 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"
19+
20+
echo "[JIT-lazy] mini_core_hello_world"
21+
CG_CLIF_JIT_ARGS="abc bcd" $MY_RUSTC -Cllvm-args=mode=jit-lazy -Cprefer-dynamic example/mini_core_hello_world.rs --cfg jit --target "$HOST_TRIPLE"
1922
else
2023
echo "[JIT] mini_core_hello_world (skipped)"
2124
fi
@@ -37,7 +40,10 @@ function base_sysroot_tests() {
3740

3841
if [[ "$JIT_SUPPORTED" = "1" ]]; then
3942
echo "[JIT] std_example"
40-
$MY_RUSTC --jit example/std_example.rs --target "$HOST_TRIPLE"
43+
$MY_RUSTC -Cllvm-args=mode=jit -Cprefer-dynamic example/std_example.rs --target "$HOST_TRIPLE"
44+
45+
echo "[JIT-lazy] std_example"
46+
$MY_RUSTC -Cllvm-args=mode=jit-lazy -Cprefer-dynamic example/std_example.rs --cfg lazy_jit --target "$HOST_TRIPLE"
4147
else
4248
echo "[JIT] std_example (skipped)"
4349
fi

src/backend.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl AddConstructor for ObjectProduct {
162162
}
163163

164164
pub(crate) fn with_object(sess: &Session, name: &str, f: impl FnOnce(&mut Object)) -> Vec<u8> {
165-
let triple = crate::build_isa(sess, true).triple().clone();
165+
let triple = crate::build_isa(sess).triple().clone();
166166

167167
let binary_format = match triple.binary_format {
168168
target_lexicon::BinaryFormat::Elf => object::BinaryFormat::Elf,
@@ -193,7 +193,7 @@ pub(crate) fn with_object(sess: &Session, name: &str, f: impl FnOnce(&mut Object
193193

194194
pub(crate) fn make_module(sess: &Session, name: String) -> ObjectModule {
195195
let mut builder = ObjectBuilder::new(
196-
crate::build_isa(sess, true),
196+
crate::build_isa(sess),
197197
name + ".o",
198198
cranelift_module::default_libcall_names(),
199199
)

src/bin/cg_clif.rs

+2-17
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

+1-3
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/constant.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut impl Module, cx: &mut Constan
447447
data_ctx.write_data_addr(offset.bytes() as u32, global_value, addend as i64);
448448
}
449449

450-
module.define_data(data_id, &data_ctx).unwrap();
450+
// FIXME don't duplicate definitions in lazy jit mode
451+
let _ = module.define_data(data_id, &data_ctx);
451452
cx.done.insert(data_id);
452453
}
453454

src/debuginfo/unwind.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ pub(crate) struct UnwindContext<'tcx> {
1515
}
1616

1717
impl<'tcx> UnwindContext<'tcx> {
18-
pub(crate) fn new(tcx: TyCtxt<'tcx>, isa: &dyn TargetIsa) -> Self {
18+
pub(crate) fn new(tcx: TyCtxt<'tcx>, isa: &dyn TargetIsa, pic_eh_frame: bool) -> Self {
1919
let mut frame_table = FrameTable::default();
2020

2121
let cie_id = if let Some(mut cie) = isa.create_systemv_cie() {
22-
if isa.flags().is_pic() {
22+
if pic_eh_frame {
2323
cie.fde_address_encoding =
2424
gimli::DwEhPe(gimli::DW_EH_PE_pcrel.0 | gimli::DW_EH_PE_sdata4.0);
2525
}

src/driver/aot.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,12 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege
146146
}
147147
}
148148

149-
let mut cx = crate::CodegenCx::new(tcx, module, tcx.sess.opts.debuginfo != DebugInfo::None);
149+
let mut cx = crate::CodegenCx::new(
150+
tcx,
151+
module,
152+
tcx.sess.opts.debuginfo != DebugInfo::None,
153+
true,
154+
);
150155
super::predefine_mono_items(&mut cx, &mono_items);
151156
for (mono_item, (linkage, visibility)) in mono_items {
152157
let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility);
@@ -254,7 +259,7 @@ pub(super) fn run_aot(
254259
tcx.sess.abort_if_errors();
255260

256261
let mut allocator_module = new_module(tcx, "allocator_shim".to_string());
257-
let mut allocator_unwind_context = UnwindContext::new(tcx, allocator_module.isa());
262+
let mut allocator_unwind_context = UnwindContext::new(tcx, allocator_module.isa(), true);
258263
let created_alloc_shim =
259264
crate::allocator::codegen(tcx, &mut allocator_module, &mut allocator_unwind_context);
260265

0 commit comments

Comments
 (0)