Skip to content

Commit 7a06915

Browse files
committed
Simplified EE creation by calling initialize native for the user.
Fixes rust-lang#59
1 parent 878cf8a commit 7a06915

File tree

9 files changed

+29
-51
lines changed

9 files changed

+29
-51
lines changed

Diff for: README.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,7 @@ use std::error::Error;
6464
/// do `unsafe` operations internally.
6565
type SumFunc = unsafe extern "C" fn(u64, u64, u64) -> u64;
6666

67-
fn main() {
68-
Target::initialize_native(&InitializationConfig::default()).unwrap();
69-
run().unwrap();
70-
}
71-
72-
fn run() -> Result<(), Box<Error>> {
67+
fn main() -> Result<(), Box<Error>> {
7368
let context = Context::create();
7469
let module = context.create_module("sum");
7570
let builder = context.create_builder();

Diff for: examples/jit.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn jit_compile_sum(
4040
unsafe { execution_engine.get_function("sum").ok() }
4141
}
4242

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

6161
Ok(())
6262
}
63-
64-
fn main() {
65-
Target::initialize_native(&InitializationConfig::default()).unwrap();
66-
run().unwrap();
67-
}

Diff for: examples/kaleidoscope/main.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use self::inkwell::builder::Builder;
2626
use self::inkwell::context::Context;
2727
use self::inkwell::module::Module;
2828
use self::inkwell::passes::PassManager;
29-
use self::inkwell::targets::{InitializationConfig, Target};
3029
use self::inkwell::types::BasicTypeEnum;
3130
use self::inkwell::values::{BasicValueEnum, FloatValue, FunctionValue, PointerValue};
3231
use self::inkwell::{OptimizationLevel, FloatPredicate};
@@ -1218,8 +1217,6 @@ pub fn main() {
12181217
}
12191218
}
12201219

1221-
Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target.");
1222-
12231220
let context = Context::create();
12241221
let module = context.create_module("repl");
12251222
let builder = context.create_builder();
@@ -1255,7 +1252,7 @@ pub fn main() {
12551252
}
12561253

12571254
// Build precedence map
1258-
let mut prec = HashMap::with_capacity(4);
1255+
let mut prec = HashMap::with_capacity(6);
12591256

12601257
prec.insert('=', 2);
12611258
prec.insert('<', 10);

Diff for: src/module.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ use data_layout::DataLayout;
3434
use execution_engine::ExecutionEngine;
3535
use memory_buffer::MemoryBuffer;
3636
use support::LLVMString;
37-
use targets::Target;
37+
use targets::{Target, InitializationConfig};
3838
use types::{AsTypeRef, BasicType, FunctionType, BasicTypeEnum};
39-
use values::{AsValueRef, BasicValue, BasicValueEnum, FunctionValue, GlobalValue, MetadataValue};
39+
use values::{AsValueRef, BasicValue, FunctionValue, GlobalValue, MetadataValue};
4040

4141
enum_rename!{
4242
/// This enum defines how to link a global variable or function in a module. The variant documenation is
@@ -417,8 +417,15 @@ impl Module {
417417
///
418418
/// assert_eq!(module.get_context(), context);
419419
/// ```
420-
// SubType: ExecutionEngine<?>
420+
// SubType: ExecutionEngine<Basic?>
421421
pub fn create_execution_engine(&self) -> Result<ExecutionEngine, LLVMString> {
422+
Target::initialize_native(&InitializationConfig::default())
423+
.map_err(|mut err_string| {
424+
err_string.push('\0');
425+
426+
LLVMString::create(err_string.as_ptr() as *const i8)
427+
})?;
428+
422429
let mut execution_engine = unsafe { zeroed() };
423430
let mut err_string = unsafe { zeroed() };
424431
let code = unsafe {
@@ -455,6 +462,13 @@ impl Module {
455462
/// ```
456463
// SubType: ExecutionEngine<Interpreter>
457464
pub fn create_interpreter_execution_engine(&self) -> Result<ExecutionEngine, LLVMString> {
465+
Target::initialize_native(&InitializationConfig::default())
466+
.map_err(|mut err_string| {
467+
err_string.push('\0');
468+
469+
LLVMString::create(err_string.as_ptr() as *const i8)
470+
})?;
471+
458472
let mut execution_engine = unsafe { zeroed() };
459473
let mut err_string = unsafe { zeroed() };
460474

@@ -493,6 +507,13 @@ impl Module {
493507
/// ```
494508
// SubType: ExecutionEngine<Jit>
495509
pub fn create_jit_execution_engine(&self, opt_level: OptimizationLevel) -> Result<ExecutionEngine, LLVMString> {
510+
Target::initialize_native(&InitializationConfig::default())
511+
.map_err(|mut err_string| {
512+
err_string.push('\0');
513+
514+
LLVMString::create(err_string.as_ptr() as *const i8)
515+
})?;
516+
496517
let mut execution_engine = unsafe { zeroed() };
497518
let mut err_string = unsafe { zeroed() };
498519

@@ -501,18 +522,6 @@ impl Module {
501522
};
502523

503524
if code == 1 {
504-
// The module still seems "owned" in this error case, despite failing to create an EE. This would normally
505-
// end in a segfault on Module drop, however we're avoiding that by cloning the module and replacing the underlying pointer
506-
// REVIEW: Ensure this doesn't lead to unexpected behavior... If it does, the alternate strategy would be to change the fn
507-
// signature to take ownership of self and return it with good EE: (self, opt_level) -> Result<(Module, EE), LLVMString>
508-
let module = self.clone();
509-
510-
self.module.set(module.module.get());
511-
512-
forget(module);
513-
514-
// REVIEW: Module still seems "owned" in the error case and may segfault on module drop. :/
515-
// Need to figure out if there's a way to prevent this.
516525
return Err(LLVMString::new(err_string));
517526
}
518527

Diff for: src/support/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ impl LLVMString {
3535
/// Don't use this if it's not necessary. You likely need to allocate
3636
/// a CString as input and then LLVM will likely allocate their own string
3737
/// anyway.
38-
#[allow(dead_code)]
39-
fn create(bytes: *const c_char) -> LLVMString {
38+
pub(crate) fn create(bytes: *const c_char) -> LLVMString {
4039
let ptr = unsafe {
4140
LLVMCreateMessage(bytes)
4241
};

Diff for: src/types/enums.rs

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ enum_type_set! {
6464
ArrayType,
6565
/// A floating point type.
6666
FloatType,
67+
// An integer type.
6768
IntType,
6869
/// A pointer type.
6970
PointerType,

Diff for: tests/all/test_builder.rs

-8
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ fn test_build_call() {
4646

4747
#[test]
4848
fn test_null_checked_ptr_ops() {
49-
Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");
50-
5149
let context = Context::create();
5250
let module = context.create_module("unsafe");
5351
let builder = context.create_builder();
@@ -152,8 +150,6 @@ fn test_null_checked_ptr_ops() {
152150

153151
#[test]
154152
fn test_binary_ops() {
155-
Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");
156-
157153
let context = Context::create();
158154
let module = context.create_module("unsafe");
159155
let builder = context.create_builder();
@@ -238,8 +234,6 @@ fn test_binary_ops() {
238234

239235
#[test]
240236
fn test_switch() {
241-
Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");
242-
243237
let context = Context::create();
244238
let module = context.create_module("unsafe");
245239
let builder = context.create_builder();
@@ -298,8 +292,6 @@ fn test_switch() {
298292

299293
#[test]
300294
fn test_bit_shifts() {
301-
Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");
302-
303295
let context = Context::create();
304296
let module = context.create_module("unsafe");
305297
let builder = context.create_builder();

Diff for: tests/all/test_execution_engine.rs

-6
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ fn test_execution_engine() {
123123
let context = Context::create();
124124
let module = context.create_module("main_module");
125125

126-
Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");
127-
128126
assert!(module.create_execution_engine().is_ok());
129127
}
130128

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

136-
Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");
137-
138134
assert!(module.create_interpreter_execution_engine().is_ok());
139135
}
140136

141137

142138
#[test]
143139
fn test_add_remove_module() {
144-
Target::initialize_all(&InitializationConfig::default());
145-
146140
let context = Context::create();
147141
let module = context.create_module("test");
148142
let ee = module.create_jit_execution_engine(OptimizationLevel::default()).unwrap();

Diff for: tests/all/test_module.rs

-4
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ fn test_module_no_double_free() {
185185

186186
#[test]
187187
fn test_owned_module_dropped_ee_and_context() {
188-
Target::initialize_native(&InitializationConfig::default()).unwrap();
189-
190188
let _module = {
191189
let context = Context::create();
192190
let module = context.create_module("my_mod");
@@ -388,8 +386,6 @@ fn test_linking_modules() {
388386
// fn_val2 is no longer the same instance of f2
389387
assert_ne!(module.get_function("f2"), Some(fn_val2));
390388

391-
Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target");
392-
393389
let _execution_engine = module.create_jit_execution_engine(OptimizationLevel::None).expect("Could not create Execution Engine");
394390
let module4 = context.create_module("mod4");
395391

0 commit comments

Comments
 (0)