Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions diffsl/src/execution/cranelift/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ pub struct CraneliftModule<M: Module> {
/// The module, with the object backend.
module: Mutex<M>,

emitted_object: Mutex<Option<Vec<u8>>>,

layout: DataLayout,

indices_id: DataId,
constants_id: DataId,
model_index_id: DataId,
thread_counter: Option<DataId>,

//triple: Triple,
triple: Triple,
int_type: types::Type,
real_type: types::Type,
real_ptr_type: types::Type,
Expand Down Expand Up @@ -575,9 +577,11 @@ impl<M: Module> CraneliftModule<M> {
builder_context: FunctionBuilderContext::new(),
ctx: module.make_context(),
module: Mutex::new(module),
emitted_object: Mutex::new(None),
indices_id,
constants_id,
model_index_id,
triple,
int_type,
real_type: real_type_cranelift,
real_ptr_type: ptr_type,
Expand Down Expand Up @@ -1094,6 +1098,20 @@ impl<M: Module> CraneliftModule<M> {
}
}

impl CraneliftModule<ObjectModule> {
fn new_object_backend(triple: Triple) -> Result<ObjectModule> {
let mut flag_builder = settings::builder();
flag_builder.set("use_colocated_libcalls", "false").unwrap();
flag_builder.set("is_pic", "false").unwrap();
flag_builder.set("opt_level", "speed").unwrap();
let flags = settings::Flags::new(flag_builder);
let isa = isa::lookup(triple)?.finish(flags)?;
let builder =
ObjectBuilder::new(isa, "diffsol", cranelift_module::default_libcall_names())?;
Ok(ObjectModule::new(builder))
}
}

impl<M: Module + Send + 'static> CodegenModule for CraneliftModule<M> {}

impl CodegenModuleCompile for CraneliftModule<ObjectModule> {
Expand All @@ -1108,16 +1126,7 @@ impl CodegenModuleCompile for CraneliftModule<ObjectModule> {
let threaded = thread_dim > 1;

let triple = triple.unwrap_or(Triple::host());
let mut flag_builder = settings::builder();
flag_builder.set("use_colocated_libcalls", "false").unwrap();
flag_builder.set("is_pic", "false").unwrap();
flag_builder.set("opt_level", "speed").unwrap();
let flags = settings::Flags::new(flag_builder);
let isa = isa::lookup(triple.clone())?.finish(flags)?;
let builder =
ObjectBuilder::new(isa, "diffsol", cranelift_module::default_libcall_names())?;

let module = ObjectModule::new(builder);
let module = Self::new_object_backend(triple.clone())?;

Self::new(triple, model, threaded, module, real_type)
}
Expand Down Expand Up @@ -1192,9 +1201,18 @@ impl CodegenModuleCompile for CraneliftModule<JITModule> {
}

impl CodegenModuleEmit for CraneliftModule<ObjectModule> {
fn to_object(self) -> Result<Vec<u8>> {
let module = Mutex::into_inner(self.module).unwrap();
module.finish().emit().map_err(|e| anyhow!(e))
fn to_object(&self) -> Result<Vec<u8>> {
let mut emitted_object = self.emitted_object.lock().unwrap();
if let Some(buffer) = emitted_object.as_ref() {
return Ok(buffer.clone());
}

let mut module = self.module.lock().unwrap();
let module_to_emit =
std::mem::replace(&mut *module, Self::new_object_backend(self.triple.clone())?);
let buffer = module_to_emit.finish().emit().map_err(|e| anyhow!(e))?;
*emitted_object = Some(buffer.clone());
Ok(buffer)
}
}

Expand Down
2 changes: 1 addition & 1 deletion diffsl/src/execution/llvm/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ impl CodegenModuleCompile for LlvmModule {
}

impl CodegenModuleEmit for LlvmModule {
fn to_object(self) -> Result<Vec<u8>> {
fn to_object(&self) -> Result<Vec<u8>> {
let module = self.codegen().module();
//module.print_to_stderr();
let buffer = self
Expand Down
2 changes: 1 addition & 1 deletion diffsl/src/execution/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ pub trait CodegenModuleJit: CodegenModule {
}

pub trait CodegenModuleEmit: CodegenModule {
fn to_object(self) -> Result<Vec<u8>>;
fn to_object(&self) -> Result<Vec<u8>>;
}
47 changes: 47 additions & 0 deletions diffsl/tests/roundtrips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,50 @@ fn cranelift_module_object_file_roundtrip() {
assert_rhs_works(&compiler);
let _ = std::fs::remove_file(object_path);
}

#[cfg(all(
feature = "cranelift",
not(target_arch = "wasm32"),
not(target_os = "macos")
))]
#[test]
fn cranelift_multiple_to_object_calls_roundtrip() {
use diffsl::discretise::DiscreteModel;
use diffsl::execution::compiler::{CompilerMode, CompilerOptions};
use diffsl::execution::module::{CodegenModuleCompile, CodegenModuleEmit};
use diffsl::execution::scalar::RealType;
use diffsl::parser::parse_ds_string;
use diffsl::{Compiler, CraneliftObjectModule, ObjectModule};

let code = model_code();
let ast = parse_ds_string(code).expect("dsl should parse");
let model = DiscreteModel::build("cranelift_multiple_object_roundtrip", &ast)
.expect("discrete model should build");

let cranelift_module = <CraneliftObjectModule as CodegenModuleCompile>::from_discrete_model(
&model,
CompilerOptions::default(),
None,
RealType::F64,
Some(code),
)
.expect("cranelift module should compile");

let object_buffer_1 = cranelift_module
.to_object()
.expect("first object emission should succeed");
let object_buffer_2 = cranelift_module
.to_object()
.expect("second object emission should succeed");

assert!(!object_buffer_1.is_empty());
assert_eq!(object_buffer_1, object_buffer_2);

let compiler = Compiler::<ObjectModule, f64>::from_object_file(
object_buffer_2,
CompilerMode::SingleThreaded,
)
.expect("compiler should build from object file");

assert_rhs_works(&compiler);
}
Loading