Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of orc2 based jit engine. #477

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ pub mod intrinsics;
pub mod memory_buffer;
#[deny(missing_docs)]
pub mod module;
#[deny(missing_docs)]
pub mod object_file;
#[cfg(not(any(feature = "llvm4-0", feature = "llvm5-0", feature = "llvm6-0",feature = "llvm7-0", feature = "llvm8-0", feature = "llvm9-0",feature = "llvm10-0")))]
pub mod orc2;
pub mod passes;
pub mod targets;
pub mod types;
Expand Down
34 changes: 34 additions & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use crate::data_layout::DataLayout;
use crate::debug_info::{DICompileUnit, DWARFEmissionKind, DWARFSourceLanguage, DebugInfoBuilder};
use crate::execution_engine::ExecutionEngine;
use crate::memory_buffer::MemoryBuffer;
use crate::orc2::LLJITExecutionEngine;
#[llvm_versions(13.0..=latest)]
use crate::passes::PassBuilderOptions;
use crate::support::{to_c_str, LLVMString};
Expand Down Expand Up @@ -169,6 +170,7 @@ pub struct Module<'ctx> {
data_layout: RefCell<Option<DataLayout>>,
pub(crate) module: Cell<LLVMModuleRef>,
pub(crate) owned_by_ee: RefCell<Option<ExecutionEngine<'ctx>>>,
pub(crate) owned_by_lljit : RefCell<Option<LLJITExecutionEngine<'ctx>>>,
_marker: PhantomData<&'ctx Context>,
}

Expand All @@ -184,6 +186,7 @@ impl<'ctx> Module<'ctx> {
Module {
module: Cell::new(module),
owned_by_ee: RefCell::new(None),
owned_by_lljit : RefCell::new(None),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should just have one owned by field with an enum in the option?

data_layout: RefCell::new(Some(Module::get_borrowed_data_layout(module))),
_marker: PhantomData,
}
Expand Down Expand Up @@ -440,6 +443,33 @@ impl<'ctx> Module<'ctx> {
unsafe { TargetTriple::new(LLVMString::create_from_c_str(CStr::from_ptr(target_str))) }
}

/// Crates a [crate::orc2::LLJitExecutionEngine] from this `Module`
/// # Example
/// ```no_run
/// use inkwell::context::Context;
/// use inkwell::module::Module;
/// use inkwell::targets::{InitializationConfig, Target};
///
/// Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");
///
/// let context = Context::create();
/// let module = context.create_module("my_module");
/// let lljit_engine = module.create_lljit_engine().unwrap();
///
/// ```
#[llvm_versions(11.0..=latest)]
pub fn create_lljit_engine(self) -> Result<crate::orc2::LLJITExecutionEngine<'ctx>, LLVMString>{
Target::initialize_native(&InitializationConfig::default()).map_err(|mut err_string| {
err_string.push('\0');

LLVMString::create_from_str(&err_string)
})?;

let engine = crate::orc2::LLJITBuilder::new().create()?;
engine.add_module(self)?;
Ok(engine)
}

/// Creates an `ExecutionEngine` from this `Module`.
///
/// # Example
Expand Down Expand Up @@ -468,6 +498,10 @@ impl<'ctx> Module<'ctx> {
let string = "This module is already owned by an ExecutionEngine.\0";
return Err(LLVMString::create_from_str(string));
}
if self.owned_by_lljit.borrow().is_some() {
let string = "This module is already owned by an LLJITExecutionEngine\0";
return Err(LLVMString::create_from_str(string));
}

let mut execution_engine = MaybeUninit::uninit();
let mut err_string = MaybeUninit::uninit();
Expand Down
Loading