From 1a3edecbf21eef7f39bbf3a7bf38fb72fdc6af61 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 13 Jun 2013 21:25:12 -0700 Subject: Revert "Revert "Remove all usage of the global LLVMContextRef"" This reverts commit 541c657a738006d78171aa261125a6a46f283b35. --- src/rustllvm/RustWrapper.cpp | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'src/rustllvm/RustWrapper.cpp') diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index 4ee5df28d24..17eb0f50b9b 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -447,9 +447,10 @@ LLVMRustWriteOutputFile(LLVMPassManagerRef PMR, return true; } -extern "C" LLVMModuleRef LLVMRustParseAssemblyFile(const char *Filename) { +extern "C" LLVMModuleRef LLVMRustParseAssemblyFile(LLVMContextRef C, + const char *Filename) { SMDiagnostic d; - Module *m = ParseAssemblyFile(Filename, d, getGlobalContext()); + Module *m = ParseAssemblyFile(Filename, d, *unwrap(C)); if (m) { return wrap(m); } else { @@ -499,9 +500,6 @@ extern "C" LLVMValueRef LLVMGetOrInsertFunction(LLVMModuleRef M, extern "C" LLVMTypeRef LLVMMetadataTypeInContext(LLVMContextRef C) { return wrap(Type::getMetadataTy(*unwrap(C))); } -extern "C" LLVMTypeRef LLVMMetadataType(void) { - return LLVMMetadataTypeInContext(LLVMGetGlobalContext()); -} extern "C" LLVMValueRef LLVMBuildAtomicLoad(LLVMBuilderRef B, LLVMValueRef source, @@ -561,3 +559,24 @@ extern "C" LLVMValueRef LLVMInlineAsm(LLVMTypeRef Ty, Constraints, HasSideEffects, IsAlignStack, (InlineAsm::AsmDialect) Dialect)); } + +/** + * This function is intended to be a threadsafe interface into enabling a + * multithreaded LLVM. This is invoked at the start of the translation phase of + * compilation to ensure that LLVM is ready. + * + * All of trans properly isolates LLVM with the use of a different + * LLVMContextRef per task, thus allowing parallel compilation of different + * crates in the same process. At the time of this writing, the use case for + * this is unit tests for rusti, but there are possible other applications. + */ +extern "C" bool LLVMRustStartMultithreading() { + static Mutex lock; + bool ret = true; + assert(lock.acquire()); + if (!LLVMIsMultithreaded()) { + ret = LLVMStartMultithreaded(); + } + assert(lock.release()); + return ret; +} -- cgit 1.4.1-3-g733a5 From a90fffe3671cb70c37d493efe8cebafab2a4705d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 13 Jun 2013 21:25:18 -0700 Subject: Revert "Revert "Have JIT execution take ownership of the LLVMContextRef"" This reverts commit 19adece68b00bd1873499cca6f1537750608d769. --- src/librustc/back/link.rs | 62 ++++++++++++++++++++++++++------------- src/librustc/driver/driver.rs | 10 +++---- src/librustc/lib/llvm.rs | 14 +++++++-- src/librustc/middle/trans/base.rs | 23 +++++++++------ src/librusti/rusti.rc | 4 --- src/rustllvm/RustWrapper.cpp | 30 ++++++++----------- src/rustllvm/rustllvm.def.in | 4 ++- src/rustllvm/rustllvm.h | 1 + 8 files changed, 88 insertions(+), 60 deletions(-) (limited to 'src/rustllvm/RustWrapper.cpp') diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs index 3c8705b4ee9..f37ef83e770 100644 --- a/src/librustc/back/link.rs +++ b/src/librustc/back/link.rs @@ -102,7 +102,7 @@ pub mod jit { use back::link::llvm_err; use driver::session::Session; use lib::llvm::llvm; - use lib::llvm::{ModuleRef, PassManagerRef}; + use lib::llvm::{ModuleRef, PassManagerRef, ContextRef}; use metadata::cstore; use core::cast; @@ -125,6 +125,7 @@ pub mod jit { pub fn exec(sess: Session, pm: PassManagerRef, + c: ContextRef, m: ModuleRef, opt: c_int, stacks: bool) { @@ -153,26 +154,43 @@ pub mod jit { }); } - // The execute function will return a void pointer - // to the _rust_main function. We can do closure - // magic here to turn it straight into a callable rust - // closure. It will also cleanup the memory manager - // for us. - - let entry = llvm::LLVMRustExecuteJIT(manager, - pm, m, opt, stacks); - - if ptr::is_null(entry) { - llvm_err(sess, ~"Could not JIT"); - } else { - let closure = Closure { - code: entry, - env: ptr::null() - }; - let func: &fn() = cast::transmute(closure); + // We custom-build a JIT execution engine via some rust wrappers + // first. This wrappers takes ownership of the module passed in. + let ee = llvm::LLVMRustBuildJIT(manager, pm, m, opt, stacks); + if ee.is_null() { + llvm::LLVMContextDispose(c); + llvm_err(sess, ~"Could not create the JIT"); + } - func(); + // Next, we need to get a handle on the _rust_main function by + // looking up it's corresponding ValueRef and then requesting that + // the execution engine compiles the function. + let fun = do str::as_c_str("_rust_main") |entry| { + llvm::LLVMGetNamedFunction(m, entry) + }; + if fun.is_null() { + llvm::LLVMDisposeExecutionEngine(ee); + llvm::LLVMContextDispose(c); + llvm_err(sess, ~"Could not find _rust_main in the JIT"); } + + // Finally, once we have the pointer to the code, we can do some + // closure magic here to turn it straight into a callable rust + // closure + let code = llvm::LLVMGetPointerToGlobal(ee, fun); + assert!(!code.is_null()); + let closure = Closure { + code: code, + env: ptr::null() + }; + let func: &fn() = cast::transmute(closure); + func(); + + // Sadly, there currently is no interface to re-use this execution + // engine, so it's disposed of here along with the context to + // prevent leaks. + llvm::LLVMDisposeExecutionEngine(ee); + llvm::LLVMContextDispose(c); } } } @@ -189,6 +207,7 @@ pub mod write { use driver::session; use lib::llvm::llvm; use lib::llvm::{ModuleRef, mk_pass_manager, mk_target_data}; + use lib::llvm::{ContextRef}; use lib; use back::passes; @@ -207,6 +226,7 @@ pub mod write { } pub fn run_passes(sess: Session, + llcx: ContextRef, llmod: ModuleRef, output_type: output_type, output: &Path) { @@ -281,7 +301,7 @@ pub mod write { // JIT execution takes ownership of the module, // so don't dispose and return. - jit::exec(sess, pm.llpm, llmod, CodeGenOptLevel, true); + jit::exec(sess, pm.llpm, llcx, llmod, CodeGenOptLevel, true); if sess.time_llvm_passes() { llvm::LLVMRustPrintPassTimings(); @@ -349,6 +369,7 @@ pub mod write { // Clean up and return llvm::LLVMDisposeModule(llmod); + llvm::LLVMContextDispose(llcx); if sess.time_llvm_passes() { llvm::LLVMRustPrintPassTimings(); } @@ -367,6 +388,7 @@ pub mod write { } llvm::LLVMDisposeModule(llmod); + llvm::LLVMContextDispose(llcx); if sess.time_llvm_passes() { llvm::LLVMRustPrintPassTimings(); } } } diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index b72cdd40ecd..0447481596a 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -216,7 +216,7 @@ pub fn compile_rest(sess: Session, let mut crate = crate_opt.unwrap(); - let (llmod, link_meta) = { + let (llcx, llmod, link_meta) = { crate = time(time_passes, ~"intrinsic injection", || front::intrinsic_inject::inject_intrinsic(sess, crate)); @@ -339,14 +339,14 @@ pub fn compile_rest(sess: Session, let obj_filename = outputs.obj_filename.with_filetype("s"); time(time_passes, ~"LLVM passes", || - link::write::run_passes(sess, llmod, output_type, - &obj_filename)); + link::write::run_passes(sess, llcx, llmod, output_type, + &obj_filename)); link::write::run_ndk(sess, &obj_filename, &outputs.obj_filename); } else { time(time_passes, ~"LLVM passes", || - link::write::run_passes(sess, llmod, sess.opts.output_type, - &outputs.obj_filename)); + link::write::run_passes(sess, llcx, llmod, sess.opts.output_type, + &outputs.obj_filename)); } let stop_after_codegen = diff --git a/src/librustc/lib/llvm.rs b/src/librustc/lib/llvm.rs index 0e9ea982d9f..b18c9e9b4c2 100644 --- a/src/librustc/lib/llvm.rs +++ b/src/librustc/lib/llvm.rs @@ -205,6 +205,8 @@ pub enum BasicBlock_opaque {} pub type BasicBlockRef = *BasicBlock_opaque; pub enum Builder_opaque {} pub type BuilderRef = *Builder_opaque; +pub enum ExecutionEngine_opaque {} +pub type ExecutionEngineRef = *ExecutionEngine_opaque; pub enum MemoryBuffer_opaque {} pub type MemoryBufferRef = *MemoryBuffer_opaque; pub enum PassManager_opaque {} @@ -223,7 +225,7 @@ pub enum Pass_opaque {} pub type PassRef = *Pass_opaque; pub mod llvm { - use super::{AtomicBinOp, AtomicOrdering, BasicBlockRef}; + use super::{AtomicBinOp, AtomicOrdering, BasicBlockRef, ExecutionEngineRef}; use super::{Bool, BuilderRef, ContextRef, MemoryBufferRef, ModuleRef}; use super::{ObjectFileRef, Opcode, PassManagerRef, PassManagerBuilderRef}; use super::{SectionIteratorRef, TargetDataRef, TypeKind, TypeRef, UseRef}; @@ -363,6 +365,10 @@ pub mod llvm { pub unsafe fn LLVMGetPointerAddressSpace(PointerTy: TypeRef) -> c_uint; #[fast_ffi] + pub unsafe fn LLVMGetPointerToGlobal(EE: ExecutionEngineRef, + V: ValueRef) + -> *(); + #[fast_ffi] pub unsafe fn LLVMGetVectorSize(VectorTy: TypeRef) -> c_uint; /* Operations on other types */ @@ -1003,6 +1009,8 @@ pub mod llvm { Name: *c_char); #[fast_ffi] pub unsafe fn LLVMDisposeBuilder(Builder: BuilderRef); + #[fast_ffi] + pub unsafe fn LLVMDisposeExecutionEngine(EE: ExecutionEngineRef); /* Metadata */ #[fast_ffi] @@ -1819,11 +1827,11 @@ pub mod llvm { /** Execute the JIT engine. */ #[fast_ffi] - pub unsafe fn LLVMRustExecuteJIT(MM: *(), + pub unsafe fn LLVMRustBuildJIT(MM: *(), PM: PassManagerRef, M: ModuleRef, OptLevel: c_int, - EnableSegmentedStacks: bool) -> *(); + EnableSegmentedStacks: bool) -> ExecutionEngineRef; /** Parses the bitcode in the given memory buffer. */ #[fast_ffi] diff --git a/src/librustc/middle/trans/base.rs b/src/librustc/middle/trans/base.rs index 3f6a37bf864..f2cf35f9fc7 100644 --- a/src/librustc/middle/trans/base.rs +++ b/src/librustc/middle/trans/base.rs @@ -3018,7 +3018,7 @@ pub fn trans_crate(sess: session::Session, tcx: ty::ctxt, output: &Path, emap2: resolve::ExportMap2, - maps: astencode::Maps) -> (ModuleRef, LinkMeta) { + maps: astencode::Maps) -> (ContextRef, ModuleRef, LinkMeta) { let symbol_hasher = @mut hash::default_state(); let link_meta = link::build_link_meta(sess, crate, output, symbol_hasher); @@ -3040,9 +3040,11 @@ pub fn trans_crate(sess: session::Session, let llmod_id = link_meta.name.to_owned() + ".rc"; unsafe { - if !llvm::LLVMRustStartMultithreading() { - sess.bug("couldn't enable multi-threaded LLVM"); - } + // FIXME(#6511): get LLVM building with --enable-threads so this + // function can be called + // if !llvm::LLVMRustStartMultithreading() { + // sess.bug("couldn't enable multi-threaded LLVM"); + // } let llcx = llvm::LLVMContextCreate(); set_task_llcx(llcx); let llmod = str::as_c_str(llmod_id, |buf| { @@ -3178,7 +3180,8 @@ pub fn trans_crate(sess: session::Session, io::println(fmt!("%-7u %s", v, k)); } } - return (llmod, link_meta); + unset_task_llcx(); + return (llcx, llmod, link_meta); } } @@ -3189,8 +3192,10 @@ pub fn task_llcx() -> ContextRef { *opt.expect("task-local LLVMContextRef wasn't ever set!") } -fn set_task_llcx(c: ContextRef) { - unsafe { - local_data::local_data_set(task_local_llcx_key, @c); - } +unsafe fn set_task_llcx(c: ContextRef) { + local_data::local_data_set(task_local_llcx_key, @c); +} + +unsafe fn unset_task_llcx() { + local_data::local_data_pop(task_local_llcx_key); } diff --git a/src/librusti/rusti.rc b/src/librusti/rusti.rc index 1a97c806027..90a5a350b7f 100644 --- a/src/librusti/rusti.rc +++ b/src/librusti/rusti.rc @@ -648,9 +648,5 @@ mod tests { fn f() {} f() "); - - debug!("regression test for #5803"); - run_cmds(["spawn( || println(\"Please don't segfault\") );", - "do spawn { println(\"Please?\"); }"]); } } diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index 17eb0f50b9b..30e01b53ab7 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -329,12 +329,12 @@ LLVMRustLoadCrate(void* mem, const char* crate) { return true; } -extern "C" void* -LLVMRustExecuteJIT(void* mem, - LLVMPassManagerRef PMR, - LLVMModuleRef M, - CodeGenOpt::Level OptLevel, - bool EnableSegmentedStacks) { +extern "C" LLVMExecutionEngineRef +LLVMRustBuildJIT(void* mem, + LLVMPassManagerRef PMR, + LLVMModuleRef M, + CodeGenOpt::Level OptLevel, + bool EnableSegmentedStacks) { InitializeNativeTarget(); InitializeNativeTargetAsmPrinter(); @@ -371,21 +371,15 @@ LLVMRustExecuteJIT(void* mem, if(!EE || Err != "") { LLVMRustError = Err.c_str(); - return 0; + // The EngineBuilder only takes ownership of these two structures if the + // create() call is successful, but here it wasn't successful. + LLVMDisposeModule(M); + delete MM; + return NULL; } MM->invalidateInstructionCache(); - Function* func = EE->FindFunctionNamed("_rust_main"); - - if(!func || Err != "") { - LLVMRustError = Err.c_str(); - return 0; - } - - void* entry = EE->getPointerToFunction(func); - assert(entry); - - return entry; + return wrap(EE); } extern "C" bool diff --git a/src/rustllvm/rustllvm.def.in b/src/rustllvm/rustllvm.def.in index f8c68d798b9..f5397165781 100644 --- a/src/rustllvm/rustllvm.def.in +++ b/src/rustllvm/rustllvm.def.in @@ -6,13 +6,14 @@ LLVMRustConstSmallInt LLVMRustConstInt LLVMRustLoadCrate LLVMRustPrepareJIT -LLVMRustExecuteJIT +LLVMRustBuildJIT LLVMRustParseBitcode LLVMRustParseAssemblyFile LLVMRustPrintPassTimings LLVMRustStartMultithreading LLVMCreateObjectFile LLVMDisposeObjectFile +LLVMDisposeExecutionEngine LLVMGetSections LLVMDisposeSectionIterator LLVMIsSectionIteratorAtEnd @@ -356,6 +357,7 @@ LLVMGetParamParent LLVMGetParamTypes LLVMGetParams LLVMGetPointerAddressSpace +LLVMGetPointerToGlobal LLVMGetPreviousBasicBlock LLVMGetPreviousFunction LLVMGetPreviousGlobal diff --git a/src/rustllvm/rustllvm.h b/src/rustllvm/rustllvm.h index 1c8842f7b4a..394146eea20 100644 --- a/src/rustllvm/rustllvm.h +++ b/src/rustllvm/rustllvm.h @@ -45,6 +45,7 @@ #include "llvm/Transforms/Vectorize.h" #include "llvm-c/Core.h" #include "llvm-c/BitReader.h" +#include "llvm-c/ExecutionEngine.h" #include "llvm-c/Object.h" // Used by RustMCJITMemoryManager::getPointerToNamedFunction() -- cgit 1.4.1-3-g733a5 From dc18321ef589711ee0a0e5adc5b7ed412641e73e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 13 Jun 2013 21:46:59 -0700 Subject: Don't run passes again on JIT code These passes are already run beforehand, no need to do them twice. --- src/librustc/back/link.rs | 52 +++++++++++++++----------------------------- src/librustc/lib/llvm.rs | 2 -- src/rustllvm/RustWrapper.cpp | 14 ------------ 3 files changed, 17 insertions(+), 51 deletions(-) (limited to 'src/rustllvm/RustWrapper.cpp') diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs index f37ef83e770..32ad07bde93 100644 --- a/src/librustc/back/link.rs +++ b/src/librustc/back/link.rs @@ -102,35 +102,21 @@ pub mod jit { use back::link::llvm_err; use driver::session::Session; use lib::llvm::llvm; - use lib::llvm::{ModuleRef, PassManagerRef, ContextRef}; + use lib::llvm::{ModuleRef, ContextRef}; use metadata::cstore; use core::cast; - use core::libc::c_int; use core::ptr; use core::str; - - pub mod rusti { - #[nolink] - #[abi = "rust-intrinsic"] - pub extern "rust-intrinsic" { - pub fn morestack_addr() -> *(); - } - } - - pub struct Closure { - code: *(), - env: *(), - } + use core::sys; + use core::unstable::intrinsics; pub fn exec(sess: Session, - pm: PassManagerRef, c: ContextRef, m: ModuleRef, - opt: c_int, stacks: bool) { unsafe { - let manager = llvm::LLVMRustPrepareJIT(rusti::morestack_addr()); + let manager = llvm::LLVMRustPrepareJIT(intrinsics::morestack_addr()); // We need to tell JIT where to resolve all linked // symbols from. The equivalent of -lstd, -lcore, etc. @@ -156,7 +142,7 @@ pub mod jit { // We custom-build a JIT execution engine via some rust wrappers // first. This wrappers takes ownership of the module passed in. - let ee = llvm::LLVMRustBuildJIT(manager, pm, m, opt, stacks); + let ee = llvm::LLVMRustBuildJIT(manager, m, stacks); if ee.is_null() { llvm::LLVMContextDispose(c); llvm_err(sess, ~"Could not create the JIT"); @@ -179,7 +165,7 @@ pub mod jit { // closure let code = llvm::LLVMGetPointerToGlobal(ee, fun); assert!(!code.is_null()); - let closure = Closure { + let closure = sys::Closure { code: code, env: ptr::null() }; @@ -282,7 +268,17 @@ pub mod write { debug!("Running Module Optimization Pass"); mpm.run(llmod); - if is_object_or_assembly_or_exe(output_type) || opts.jit { + if opts.jit { + // If we are using JIT, go ahead and create and execute the + // engine now. JIT execution takes ownership of the module and + // context, so don't dispose and return. + jit::exec(sess, llcx, llmod, true); + + if sess.time_llvm_passes() { + llvm::LLVMRustPrintPassTimings(); + } + return; + } else if is_object_or_assembly_or_exe(output_type) { let LLVMOptNone = 0 as c_int; // -O0 let LLVMOptLess = 1 as c_int; // -O1 let LLVMOptDefault = 2 as c_int; // -O2, -Os @@ -295,20 +291,6 @@ pub mod write { session::Aggressive => LLVMOptAggressive }; - if opts.jit { - // If we are using JIT, go ahead and create and - // execute the engine now. - // JIT execution takes ownership of the module, - // so don't dispose and return. - - jit::exec(sess, pm.llpm, llcx, llmod, CodeGenOptLevel, true); - - if sess.time_llvm_passes() { - llvm::LLVMRustPrintPassTimings(); - } - return; - } - let FileType; if output_type == output_type_object || output_type == output_type_exe { diff --git a/src/librustc/lib/llvm.rs b/src/librustc/lib/llvm.rs index b18c9e9b4c2..289bb4f63f5 100644 --- a/src/librustc/lib/llvm.rs +++ b/src/librustc/lib/llvm.rs @@ -1828,9 +1828,7 @@ pub mod llvm { /** Execute the JIT engine. */ #[fast_ffi] pub unsafe fn LLVMRustBuildJIT(MM: *(), - PM: PassManagerRef, M: ModuleRef, - OptLevel: c_int, EnableSegmentedStacks: bool) -> ExecutionEngineRef; /** Parses the bitcode in the given memory buffer. */ diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp index 30e01b53ab7..ba87624e2dd 100644 --- a/src/rustllvm/RustWrapper.cpp +++ b/src/rustllvm/RustWrapper.cpp @@ -331,9 +331,7 @@ LLVMRustLoadCrate(void* mem, const char* crate) { extern "C" LLVMExecutionEngineRef LLVMRustBuildJIT(void* mem, - LLVMPassManagerRef PMR, LLVMModuleRef M, - CodeGenOpt::Level OptLevel, bool EnableSegmentedStacks) { InitializeNativeTarget(); @@ -346,25 +344,13 @@ LLVMRustBuildJIT(void* mem, Options.JITEmitDebugInfo = true; Options.NoFramePointerElim = true; Options.EnableSegmentedStacks = EnableSegmentedStacks; - PassManager *PM = unwrap(PMR); RustMCJITMemoryManager* MM = (RustMCJITMemoryManager*) mem; - assert(MM); - PM->add(createBasicAliasAnalysisPass()); - PM->add(createInstructionCombiningPass()); - PM->add(createReassociatePass()); - PM->add(createGVNPass()); - PM->add(createCFGSimplificationPass()); - PM->add(createFunctionInliningPass()); - PM->add(createPromoteMemoryToRegisterPass()); - PM->run(*unwrap(M)); - ExecutionEngine* EE = EngineBuilder(unwrap(M)) .setErrorStr(&Err) .setTargetOptions(Options) .setJITMemoryManager(MM) - .setOptLevel(OptLevel) .setUseMCJIT(true) .setAllocateGVsWithCode(false) .create(); -- cgit 1.4.1-3-g733a5