Skip to content

Commit 19adece

Browse files
committed
Revert "Have JIT execution take ownership of the LLVMContextRef"
This reverts commit 5c5095d. Conflicts: src/librusti/rusti.rc
1 parent 5bff471 commit 19adece

File tree

8 files changed

+60
-88
lines changed

8 files changed

+60
-88
lines changed

src/librustc/back/link.rs

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub mod jit {
102102
use back::link::llvm_err;
103103
use driver::session::Session;
104104
use lib::llvm::llvm;
105-
use lib::llvm::{ModuleRef, PassManagerRef, ContextRef};
105+
use lib::llvm::{ModuleRef, PassManagerRef};
106106
use metadata::cstore;
107107

108108
use core::cast;
@@ -125,7 +125,6 @@ pub mod jit {
125125

126126
pub fn exec(sess: Session,
127127
pm: PassManagerRef,
128-
c: ContextRef,
129128
m: ModuleRef,
130129
opt: c_int,
131130
stacks: bool) {
@@ -154,43 +153,26 @@ pub mod jit {
154153
});
155154
}
156155

157-
// We custom-build a JIT execution engine via some rust wrappers
158-
// first. This wrappers takes ownership of the module passed in.
159-
let ee = llvm::LLVMRustBuildJIT(manager, pm, m, opt, stacks);
160-
if ee.is_null() {
161-
llvm::LLVMContextDispose(c);
162-
llvm_err(sess, ~"Could not create the JIT");
163-
}
156+
// The execute function will return a void pointer
157+
// to the _rust_main function. We can do closure
158+
// magic here to turn it straight into a callable rust
159+
// closure. It will also cleanup the memory manager
160+
// for us.
164161

165-
// Next, we need to get a handle on the _rust_main function by
166-
// looking up it's corresponding ValueRef and then requesting that
167-
// the execution engine compiles the function.
168-
let fun = do str::as_c_str("_rust_main") |entry| {
169-
llvm::LLVMGetNamedFunction(m, entry)
170-
};
171-
if fun.is_null() {
172-
llvm::LLVMDisposeExecutionEngine(ee);
173-
llvm::LLVMContextDispose(c);
174-
llvm_err(sess, ~"Could not find _rust_main in the JIT");
175-
}
162+
let entry = llvm::LLVMRustExecuteJIT(manager,
163+
pm, m, opt, stacks);
176164

177-
// Finally, once we have the pointer to the code, we can do some
178-
// closure magic here to turn it straight into a callable rust
179-
// closure
180-
let code = llvm::LLVMGetPointerToGlobal(ee, fun);
181-
assert!(!code.is_null());
182-
let closure = Closure {
183-
code: code,
184-
env: ptr::null()
185-
};
186-
let func: &fn() = cast::transmute(closure);
187-
func();
188-
189-
// Sadly, there currently is no interface to re-use this execution
190-
// engine, so it's disposed of here along with the context to
191-
// prevent leaks.
192-
llvm::LLVMDisposeExecutionEngine(ee);
193-
llvm::LLVMContextDispose(c);
165+
if ptr::is_null(entry) {
166+
llvm_err(sess, ~"Could not JIT");
167+
} else {
168+
let closure = Closure {
169+
code: entry,
170+
env: ptr::null()
171+
};
172+
let func: &fn() = cast::transmute(closure);
173+
174+
func();
175+
}
194176
}
195177
}
196178
}
@@ -207,7 +189,6 @@ pub mod write {
207189
use driver::session;
208190
use lib::llvm::llvm;
209191
use lib::llvm::{ModuleRef, mk_pass_manager, mk_target_data};
210-
use lib::llvm::{ContextRef};
211192
use lib;
212193

213194
use back::passes;
@@ -226,7 +207,6 @@ pub mod write {
226207
}
227208

228209
pub fn run_passes(sess: Session,
229-
llcx: ContextRef,
230210
llmod: ModuleRef,
231211
output_type: output_type,
232212
output: &Path) {
@@ -301,7 +281,7 @@ pub mod write {
301281
// JIT execution takes ownership of the module,
302282
// so don't dispose and return.
303283

304-
jit::exec(sess, pm.llpm, llcx, llmod, CodeGenOptLevel, true);
284+
jit::exec(sess, pm.llpm, llmod, CodeGenOptLevel, true);
305285

306286
if sess.time_llvm_passes() {
307287
llvm::LLVMRustPrintPassTimings();
@@ -369,7 +349,6 @@ pub mod write {
369349
// Clean up and return
370350

371351
llvm::LLVMDisposeModule(llmod);
372-
llvm::LLVMContextDispose(llcx);
373352
if sess.time_llvm_passes() {
374353
llvm::LLVMRustPrintPassTimings();
375354
}
@@ -388,7 +367,6 @@ pub mod write {
388367
}
389368

390369
llvm::LLVMDisposeModule(llmod);
391-
llvm::LLVMContextDispose(llcx);
392370
if sess.time_llvm_passes() { llvm::LLVMRustPrintPassTimings(); }
393371
}
394372
}

src/librustc/driver/driver.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ pub fn compile_rest(sess: Session,
216216

217217
let mut crate = crate_opt.unwrap();
218218

219-
let (llcx, llmod, link_meta) = {
219+
let (llmod, link_meta) = {
220220
crate = time(time_passes, ~"intrinsic injection", ||
221221
front::intrinsic_inject::inject_intrinsic(sess, crate));
222222

@@ -339,14 +339,14 @@ pub fn compile_rest(sess: Session,
339339
let obj_filename = outputs.obj_filename.with_filetype("s");
340340

341341
time(time_passes, ~"LLVM passes", ||
342-
link::write::run_passes(sess, llcx, llmod, output_type,
343-
&obj_filename));
342+
link::write::run_passes(sess, llmod, output_type,
343+
&obj_filename));
344344

345345
link::write::run_ndk(sess, &obj_filename, &outputs.obj_filename);
346346
} else {
347347
time(time_passes, ~"LLVM passes", ||
348-
link::write::run_passes(sess, llcx, llmod, sess.opts.output_type,
349-
&outputs.obj_filename));
348+
link::write::run_passes(sess, llmod, sess.opts.output_type,
349+
&outputs.obj_filename));
350350
}
351351

352352
let stop_after_codegen =

src/librustc/lib/llvm.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@ pub enum BasicBlock_opaque {}
205205
pub type BasicBlockRef = *BasicBlock_opaque;
206206
pub enum Builder_opaque {}
207207
pub type BuilderRef = *Builder_opaque;
208-
pub enum ExecutionEngine_opaque {}
209-
pub type ExecutionEngineRef = *ExecutionEngine_opaque;
210208
pub enum MemoryBuffer_opaque {}
211209
pub type MemoryBufferRef = *MemoryBuffer_opaque;
212210
pub enum PassManager_opaque {}
@@ -225,7 +223,7 @@ pub enum Pass_opaque {}
225223
pub type PassRef = *Pass_opaque;
226224

227225
pub mod llvm {
228-
use super::{AtomicBinOp, AtomicOrdering, BasicBlockRef, ExecutionEngineRef};
226+
use super::{AtomicBinOp, AtomicOrdering, BasicBlockRef};
229227
use super::{Bool, BuilderRef, ContextRef, MemoryBufferRef, ModuleRef};
230228
use super::{ObjectFileRef, Opcode, PassManagerRef, PassManagerBuilderRef};
231229
use super::{SectionIteratorRef, TargetDataRef, TypeKind, TypeRef, UseRef};
@@ -365,10 +363,6 @@ pub mod llvm {
365363
pub unsafe fn LLVMGetPointerAddressSpace(PointerTy: TypeRef)
366364
-> c_uint;
367365
#[fast_ffi]
368-
pub unsafe fn LLVMGetPointerToGlobal(EE: ExecutionEngineRef,
369-
V: ValueRef)
370-
-> *();
371-
#[fast_ffi]
372366
pub unsafe fn LLVMGetVectorSize(VectorTy: TypeRef) -> c_uint;
373367

374368
/* Operations on other types */
@@ -1009,8 +1003,6 @@ pub mod llvm {
10091003
Name: *c_char);
10101004
#[fast_ffi]
10111005
pub unsafe fn LLVMDisposeBuilder(Builder: BuilderRef);
1012-
#[fast_ffi]
1013-
pub unsafe fn LLVMDisposeExecutionEngine(EE: ExecutionEngineRef);
10141006

10151007
/* Metadata */
10161008
#[fast_ffi]
@@ -1827,11 +1819,11 @@ pub mod llvm {
18271819

18281820
/** Execute the JIT engine. */
18291821
#[fast_ffi]
1830-
pub unsafe fn LLVMRustBuildJIT(MM: *(),
1822+
pub unsafe fn LLVMRustExecuteJIT(MM: *(),
18311823
PM: PassManagerRef,
18321824
M: ModuleRef,
18331825
OptLevel: c_int,
1834-
EnableSegmentedStacks: bool) -> ExecutionEngineRef;
1826+
EnableSegmentedStacks: bool) -> *();
18351827

18361828
/** Parses the bitcode in the given memory buffer. */
18371829
#[fast_ffi]

src/librustc/middle/trans/base.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3018,7 +3018,7 @@ pub fn trans_crate(sess: session::Session,
30183018
tcx: ty::ctxt,
30193019
output: &Path,
30203020
emap2: resolve::ExportMap2,
3021-
maps: astencode::Maps) -> (ContextRef, ModuleRef, LinkMeta) {
3021+
maps: astencode::Maps) -> (ModuleRef, LinkMeta) {
30223022
30233023
let symbol_hasher = @mut hash::default_state();
30243024
let link_meta = link::build_link_meta(sess, crate, output, symbol_hasher);
@@ -3040,11 +3040,9 @@ pub fn trans_crate(sess: session::Session,
30403040
let llmod_id = link_meta.name.to_owned() + ".rc";
30413041
30423042
unsafe {
3043-
// FIXME(#6511): get LLVM building with --enable-threads so this
3044-
// function can be called
3045-
// if !llvm::LLVMRustStartMultithreading() {
3046-
// sess.bug("couldn't enable multi-threaded LLVM");
3047-
// }
3043+
if !llvm::LLVMRustStartMultithreading() {
3044+
sess.bug("couldn't enable multi-threaded LLVM");
3045+
}
30483046
let llcx = llvm::LLVMContextCreate();
30493047
set_task_llcx(llcx);
30503048
let llmod = str::as_c_str(llmod_id, |buf| {
@@ -3180,8 +3178,7 @@ pub fn trans_crate(sess: session::Session,
31803178
io::println(fmt!("%-7u %s", v, k));
31813179
}
31823180
}
3183-
unset_task_llcx();
3184-
return (llcx, llmod, link_meta);
3181+
return (llmod, link_meta);
31853182
}
31863183
}
31873184
@@ -3192,10 +3189,8 @@ pub fn task_llcx() -> ContextRef {
31923189
*opt.expect("task-local LLVMContextRef wasn't ever set!")
31933190
}
31943191

3195-
unsafe fn set_task_llcx(c: ContextRef) {
3196-
local_data::local_data_set(task_local_llcx_key, @c);
3197-
}
3198-
3199-
unsafe fn unset_task_llcx() {
3200-
local_data::local_data_pop(task_local_llcx_key);
3192+
fn set_task_llcx(c: ContextRef) {
3193+
unsafe {
3194+
local_data::local_data_set(task_local_llcx_key, @c);
3195+
}
32013196
}

src/librusti/rusti.rc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,5 +648,9 @@ mod tests {
648648
fn f() {}
649649
f()
650650
");
651+
652+
debug!("regression test for #5803");
653+
run_cmds(["spawn( || println(\"Please don't segfault\") );",
654+
"do spawn { println(\"Please?\"); }"]);
651655
}
652656
}

src/rustllvm/RustWrapper.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,12 @@ LLVMRustLoadCrate(void* mem, const char* crate) {
329329
return true;
330330
}
331331

332-
extern "C" LLVMExecutionEngineRef
333-
LLVMRustBuildJIT(void* mem,
334-
LLVMPassManagerRef PMR,
335-
LLVMModuleRef M,
336-
CodeGenOpt::Level OptLevel,
337-
bool EnableSegmentedStacks) {
332+
extern "C" void*
333+
LLVMRustExecuteJIT(void* mem,
334+
LLVMPassManagerRef PMR,
335+
LLVMModuleRef M,
336+
CodeGenOpt::Level OptLevel,
337+
bool EnableSegmentedStacks) {
338338

339339
InitializeNativeTarget();
340340
InitializeNativeTargetAsmPrinter();
@@ -371,15 +371,21 @@ LLVMRustBuildJIT(void* mem,
371371

372372
if(!EE || Err != "") {
373373
LLVMRustError = Err.c_str();
374-
// The EngineBuilder only takes ownership of these two structures if the
375-
// create() call is successful, but here it wasn't successful.
376-
LLVMDisposeModule(M);
377-
delete MM;
378-
return NULL;
374+
return 0;
379375
}
380376

381377
MM->invalidateInstructionCache();
382-
return wrap(EE);
378+
Function* func = EE->FindFunctionNamed("_rust_main");
379+
380+
if(!func || Err != "") {
381+
LLVMRustError = Err.c_str();
382+
return 0;
383+
}
384+
385+
void* entry = EE->getPointerToFunction(func);
386+
assert(entry);
387+
388+
return entry;
383389
}
384390

385391
extern "C" bool

src/rustllvm/rustllvm.def.in

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ LLVMRustConstSmallInt
66
LLVMRustConstInt
77
LLVMRustLoadCrate
88
LLVMRustPrepareJIT
9-
LLVMRustBuildJIT
9+
LLVMRustExecuteJIT
1010
LLVMRustParseBitcode
1111
LLVMRustParseAssemblyFile
1212
LLVMRustPrintPassTimings
1313
LLVMRustStartMultithreading
1414
LLVMCreateObjectFile
1515
LLVMDisposeObjectFile
16-
LLVMDisposeExecutionEngine
1716
LLVMGetSections
1817
LLVMDisposeSectionIterator
1918
LLVMIsSectionIteratorAtEnd
@@ -357,7 +356,6 @@ LLVMGetParamParent
357356
LLVMGetParamTypes
358357
LLVMGetParams
359358
LLVMGetPointerAddressSpace
360-
LLVMGetPointerToGlobal
361359
LLVMGetPreviousBasicBlock
362360
LLVMGetPreviousFunction
363361
LLVMGetPreviousGlobal

src/rustllvm/rustllvm.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
#include "llvm/Transforms/Vectorize.h"
4646
#include "llvm-c/Core.h"
4747
#include "llvm-c/BitReader.h"
48-
#include "llvm-c/ExecutionEngine.h"
4948
#include "llvm-c/Object.h"
5049

5150
// Used by RustMCJITMemoryManager::getPointerToNamedFunction()

0 commit comments

Comments
 (0)