Skip to content

Commit

Permalink
Simplified EE creation by calling initialize native for the user.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDan64 committed Jan 26, 2019
1 parent 878cf8a commit 7a06915
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 51 deletions.
7 changes: 1 addition & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ use std::error::Error;
/// do `unsafe` operations internally.
type SumFunc = unsafe extern "C" fn(u64, u64, u64) -> u64;

fn main() {
Target::initialize_native(&InitializationConfig::default()).unwrap();
run().unwrap();
}

fn run() -> Result<(), Box<Error>> {
fn main() -> Result<(), Box<Error>> {
let context = Context::create();
let module = context.create_module("sum");
let builder = context.create_builder();
Expand Down
7 changes: 1 addition & 6 deletions examples/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn jit_compile_sum(
unsafe { execution_engine.get_function("sum").ok() }
}

fn run() -> Result<(), Box<Error>> {
fn main() -> Result<(), Box<Error>> {
let context = Context::create();
let module = context.create_module("sum");
let builder = context.create_builder();
Expand All @@ -60,8 +60,3 @@ fn run() -> Result<(), Box<Error>> {

Ok(())
}

fn main() {
Target::initialize_native(&InitializationConfig::default()).unwrap();
run().unwrap();
}
5 changes: 1 addition & 4 deletions examples/kaleidoscope/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use self::inkwell::builder::Builder;
use self::inkwell::context::Context;
use self::inkwell::module::Module;
use self::inkwell::passes::PassManager;
use self::inkwell::targets::{InitializationConfig, Target};
use self::inkwell::types::BasicTypeEnum;
use self::inkwell::values::{BasicValueEnum, FloatValue, FunctionValue, PointerValue};
use self::inkwell::{OptimizationLevel, FloatPredicate};
Expand Down Expand Up @@ -1218,8 +1217,6 @@ pub fn main() {
}
}

Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target.");

let context = Context::create();
let module = context.create_module("repl");
let builder = context.create_builder();
Expand Down Expand Up @@ -1255,7 +1252,7 @@ pub fn main() {
}

// Build precedence map
let mut prec = HashMap::with_capacity(4);
let mut prec = HashMap::with_capacity(6);

prec.insert('=', 2);
prec.insert('<', 10);
Expand Down
39 changes: 24 additions & 15 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ use data_layout::DataLayout;
use execution_engine::ExecutionEngine;
use memory_buffer::MemoryBuffer;
use support::LLVMString;
use targets::Target;
use targets::{Target, InitializationConfig};
use types::{AsTypeRef, BasicType, FunctionType, BasicTypeEnum};
use values::{AsValueRef, BasicValue, BasicValueEnum, FunctionValue, GlobalValue, MetadataValue};
use values::{AsValueRef, BasicValue, FunctionValue, GlobalValue, MetadataValue};

enum_rename!{
/// This enum defines how to link a global variable or function in a module. The variant documenation is
Expand Down Expand Up @@ -417,8 +417,15 @@ impl Module {
///
/// assert_eq!(module.get_context(), context);
/// ```
// SubType: ExecutionEngine<?>
// SubType: ExecutionEngine<Basic?>
pub fn create_execution_engine(&self) -> Result<ExecutionEngine, LLVMString> {
Target::initialize_native(&InitializationConfig::default())
.map_err(|mut err_string| {
err_string.push('\0');

LLVMString::create(err_string.as_ptr() as *const i8)
})?;

let mut execution_engine = unsafe { zeroed() };
let mut err_string = unsafe { zeroed() };
let code = unsafe {
Expand Down Expand Up @@ -455,6 +462,13 @@ impl Module {
/// ```
// SubType: ExecutionEngine<Interpreter>
pub fn create_interpreter_execution_engine(&self) -> Result<ExecutionEngine, LLVMString> {
Target::initialize_native(&InitializationConfig::default())
.map_err(|mut err_string| {
err_string.push('\0');

LLVMString::create(err_string.as_ptr() as *const i8)
})?;

let mut execution_engine = unsafe { zeroed() };
let mut err_string = unsafe { zeroed() };

Expand Down Expand Up @@ -493,6 +507,13 @@ impl Module {
/// ```
// SubType: ExecutionEngine<Jit>
pub fn create_jit_execution_engine(&self, opt_level: OptimizationLevel) -> Result<ExecutionEngine, LLVMString> {
Target::initialize_native(&InitializationConfig::default())
.map_err(|mut err_string| {
err_string.push('\0');

LLVMString::create(err_string.as_ptr() as *const i8)
})?;

let mut execution_engine = unsafe { zeroed() };
let mut err_string = unsafe { zeroed() };

Expand All @@ -501,18 +522,6 @@ impl Module {
};

if code == 1 {
// The module still seems "owned" in this error case, despite failing to create an EE. This would normally
// end in a segfault on Module drop, however we're avoiding that by cloning the module and replacing the underlying pointer
// REVIEW: Ensure this doesn't lead to unexpected behavior... If it does, the alternate strategy would be to change the fn
// signature to take ownership of self and return it with good EE: (self, opt_level) -> Result<(Module, EE), LLVMString>
let module = self.clone();

self.module.set(module.module.get());

forget(module);

// REVIEW: Module still seems "owned" in the error case and may segfault on module drop. :/
// Need to figure out if there's a way to prevent this.
return Err(LLVMString::new(err_string));
}

Expand Down
3 changes: 1 addition & 2 deletions src/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ impl LLVMString {
/// Don't use this if it's not necessary. You likely need to allocate
/// a CString as input and then LLVM will likely allocate their own string
/// anyway.
#[allow(dead_code)]
fn create(bytes: *const c_char) -> LLVMString {
pub(crate) fn create(bytes: *const c_char) -> LLVMString {
let ptr = unsafe {
LLVMCreateMessage(bytes)
};
Expand Down
1 change: 1 addition & 0 deletions src/types/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ enum_type_set! {
ArrayType,
/// A floating point type.
FloatType,
// An integer type.
IntType,
/// A pointer type.
PointerType,
Expand Down
8 changes: 0 additions & 8 deletions tests/all/test_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ fn test_build_call() {

#[test]
fn test_null_checked_ptr_ops() {
Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

let context = Context::create();
let module = context.create_module("unsafe");
let builder = context.create_builder();
Expand Down Expand Up @@ -152,8 +150,6 @@ fn test_null_checked_ptr_ops() {

#[test]
fn test_binary_ops() {
Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

let context = Context::create();
let module = context.create_module("unsafe");
let builder = context.create_builder();
Expand Down Expand Up @@ -238,8 +234,6 @@ fn test_binary_ops() {

#[test]
fn test_switch() {
Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

let context = Context::create();
let module = context.create_module("unsafe");
let builder = context.create_builder();
Expand Down Expand Up @@ -298,8 +292,6 @@ fn test_switch() {

#[test]
fn test_bit_shifts() {
Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

let context = Context::create();
let module = context.create_module("unsafe");
let builder = context.create_builder();
Expand Down
6 changes: 0 additions & 6 deletions tests/all/test_execution_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ fn test_execution_engine() {
let context = Context::create();
let module = context.create_module("main_module");

Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

assert!(module.create_execution_engine().is_ok());
}

Expand All @@ -133,16 +131,12 @@ fn test_interpreter_execution_engine() {
let context = Context::create();
let module = context.create_module("main_module");

Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

assert!(module.create_interpreter_execution_engine().is_ok());
}


#[test]
fn test_add_remove_module() {
Target::initialize_all(&InitializationConfig::default());

let context = Context::create();
let module = context.create_module("test");
let ee = module.create_jit_execution_engine(OptimizationLevel::default()).unwrap();
Expand Down
4 changes: 0 additions & 4 deletions tests/all/test_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ fn test_module_no_double_free() {

#[test]
fn test_owned_module_dropped_ee_and_context() {
Target::initialize_native(&InitializationConfig::default()).unwrap();

let _module = {
let context = Context::create();
let module = context.create_module("my_mod");
Expand Down Expand Up @@ -388,8 +386,6 @@ fn test_linking_modules() {
// fn_val2 is no longer the same instance of f2
assert_ne!(module.get_function("f2"), Some(fn_val2));

Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");

let _execution_engine = module.create_jit_execution_engine(OptimizationLevel::None).expect("Could not create Execution Engine");
let module4 = context.create_module("mod4");

Expand Down

0 comments on commit 7a06915

Please sign in to comment.