Skip to content

Commit

Permalink
Merge pull request #2403 from bjorn3/simplejit_hot_swapping
Browse files Browse the repository at this point in the history
SimpleJIT hot code swapping
  • Loading branch information
Pat Hickey authored Dec 3, 2020
2 parents 09662fa + 937a3fd commit 0f1dc9a
Show file tree
Hide file tree
Showing 8 changed files with 533 additions and 33 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions cranelift/codegen/src/ir/libcall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub enum LibCall {

/// Elf __tls_get_addr
ElfTlsGetAddr,
// When adding a new variant make sure to add it to `all_libcalls` too.
}

impl fmt::Display for LibCall {
Expand Down Expand Up @@ -136,6 +137,33 @@ impl LibCall {
_ => return None,
})
}

/// Get a list of all known `LibCall`'s.
pub fn all_libcalls() -> &'static [LibCall] {
use LibCall::*;
&[
Probestack,
UdivI64,
SdivI64,
UremI64,
SremI64,
IshlI64,
UshrI64,
SshrI64,
CeilF32,
CeilF64,
FloorF32,
FloorF64,
TruncF32,
TruncF64,
NearestF32,
NearestF64,
Memcpy,
Memset,
Memmove,
ElfTlsGetAddr,
]
}
}

/// Get a function reference for `libcall` in `func`, following the signature
Expand Down
94 changes: 94 additions & 0 deletions cranelift/module/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,97 @@ pub trait Module {
/// Define a data object, producing the data contents from the given `DataContext`.
fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()>;
}

impl<M: Module> Module for &mut M {
fn isa(&self) -> &dyn isa::TargetIsa {
(**self).isa()
}

fn declarations(&self) -> &ModuleDeclarations {
(**self).declarations()
}

fn get_name(&self, name: &str) -> Option<FuncOrDataId> {
(**self).get_name(name)
}

fn target_config(&self) -> isa::TargetFrontendConfig {
(**self).target_config()
}

fn make_context(&self) -> Context {
(**self).make_context()
}

fn clear_context(&self, ctx: &mut Context) {
(**self).clear_context(ctx)
}

fn make_signature(&self) -> ir::Signature {
(**self).make_signature()
}

fn clear_signature(&self, sig: &mut ir::Signature) {
(**self).clear_signature(sig)
}

fn declare_function(
&mut self,
name: &str,
linkage: Linkage,
signature: &ir::Signature,
) -> ModuleResult<FuncId> {
(**self).declare_function(name, linkage, signature)
}

fn declare_data(
&mut self,
name: &str,
linkage: Linkage,
writable: bool,
tls: bool,
) -> ModuleResult<DataId> {
(**self).declare_data(name, linkage, writable, tls)
}

fn declare_func_in_func(&self, func: FuncId, in_func: &mut ir::Function) -> ir::FuncRef {
(**self).declare_func_in_func(func, in_func)
}

fn declare_data_in_func(&self, data: DataId, func: &mut ir::Function) -> ir::GlobalValue {
(**self).declare_data_in_func(data, func)
}

fn declare_func_in_data(&self, func: FuncId, ctx: &mut DataContext) -> ir::FuncRef {
(**self).declare_func_in_data(func, ctx)
}

fn declare_data_in_data(&self, data: DataId, ctx: &mut DataContext) -> ir::GlobalValue {
(**self).declare_data_in_data(data, ctx)
}

fn define_function<TS>(
&mut self,
func: FuncId,
ctx: &mut Context,
trap_sink: &mut TS,
) -> ModuleResult<ModuleCompiledFunction>
where
TS: binemit::TrapSink,
{
(**self).define_function(func, ctx, trap_sink)
}

fn define_function_bytes(
&mut self,
func: FuncId,
bytes: &[u8],
relocs: &[RelocRecord],
) -> ModuleResult<ModuleCompiledFunction> {
(**self).define_function_bytes(func, bytes, relocs)
}

fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()> {
(**self).define_data(data, data_ctx)
}
}
1 change: 1 addition & 0 deletions cranelift/simplejit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cranelift-module = { path = "../module", version = "0.68.0" }
cranelift-native = { path = "../native", version = "0.68.0" }
cranelift-codegen = { path = "../codegen", version = "0.68.0", default-features = false, features = ["std"] }
cranelift-entity = { path = "../entity", version = "0.68.0" }
anyhow = "1.0"
region = "2.2.0"
libc = { version = "0.2.42" }
errno = "0.2.4"
Expand Down
12 changes: 11 additions & 1 deletion cranelift/simplejit/examples/simplejit-minimal.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
use cranelift::prelude::*;
use cranelift_codegen::binemit::NullTrapSink;
use cranelift_codegen::settings::{self, Configurable};
use cranelift_module::{default_libcall_names, Linkage, Module};
use cranelift_simplejit::{SimpleJITBuilder, SimpleJITModule};
use std::mem;

fn main() {
let mut flag_builder = settings::builder();
flag_builder.set("use_colocated_libcalls", "false").unwrap();
// FIXME set back to true once the x64 backend supports it.
flag_builder.set("is_pic", "false").unwrap();
let isa_builder = cranelift_native::builder().unwrap_or_else(|msg| {
panic!("host machine is not supported: {}", msg);
});
let isa = isa_builder.finish(settings::Flags::new(flag_builder));
let mut module: SimpleJITModule =
SimpleJITModule::new(SimpleJITBuilder::new(default_libcall_names()));
SimpleJITModule::new(SimpleJITBuilder::with_isa(isa, default_libcall_names()));

let mut ctx = module.make_context();
let mut func_ctx = FunctionBuilderContext::new();

Expand Down
Loading

0 comments on commit 0f1dc9a

Please sign in to comment.