@@ -29,7 +29,7 @@ use back::link::{mangle_exported_name};
2929use back:: { link, abi, upcall} ;
3030use driver:: session;
3131use driver:: session:: Session ;
32- use lib:: llvm:: { ModuleRef , ValueRef , TypeRef , BasicBlockRef } ;
32+ use lib:: llvm:: { ContextRef , ModuleRef , ValueRef , TypeRef , BasicBlockRef } ;
3333use lib:: llvm:: { True , False } ;
3434use lib:: llvm:: { llvm, mk_target_data, mk_type_names} ;
3535use lib;
@@ -73,6 +73,7 @@ use core::libc::c_uint;
7373use core:: str;
7474use core:: uint;
7575use core:: vec;
76+ use core:: local_data;
7677use extra:: time;
7778use syntax:: ast:: ident;
7879use syntax:: ast_map:: { path, path_elt_to_str, path_name} ;
@@ -1187,7 +1188,7 @@ pub fn new_block(cx: fn_ctxt, parent: Option<block>, kind: block_kind,
11871188 } ;
11881189 unsafe {
11891190 let llbb = str:: as_c_str ( cx. ccx . sess . str_of ( s) , |buf| {
1190- llvm:: LLVMAppendBasicBlock ( cx. llfn , buf)
1191+ llvm:: LLVMAppendBasicBlockInContext ( cx . ccx . llcx , cx. llfn , buf)
11911192 } ) ;
11921193 let bcx = mk_block ( llbb,
11931194 parent,
@@ -1554,11 +1555,12 @@ pub struct BasicBlocks {
15541555// Creates the standard set of basic blocks for a function
15551556pub fn mk_standard_basic_blocks( llfn: ValueRef ) -> BasicBlocks {
15561557 unsafe {
1558+ let cx = task_llcx( ) ;
15571559 BasicBlocks {
15581560 sa : str:: as_c_str( "static_allocas" ,
1559- |buf| llvm:: LLVMAppendBasicBlock ( llfn, buf) ) ,
1561+ |buf| llvm:: LLVMAppendBasicBlockInContext ( cx , llfn, buf) ) ,
15601562 rt : str:: as_c_str( "return" ,
1561- |buf| llvm:: LLVMAppendBasicBlock ( llfn, buf) )
1563+ |buf| llvm:: LLVMAppendBasicBlockInContext ( cx , llfn, buf) )
15621564 }
15631565 }
15641566}
@@ -2341,7 +2343,7 @@ pub fn create_entry_wrapper(ccx: @CrateContext,
23412343 };
23422344 let llbb = str::as_c_str(" top", |buf| {
23432345 unsafe {
2344- llvm::LLVMAppendBasicBlock( llfn, buf)
2346+ llvm::LLVMAppendBasicBlockInContext(ccx.llcx, llfn, buf)
23452347 }
23462348 });
23472349 let bld = ccx.builder.B;
@@ -2659,10 +2661,10 @@ pub fn declare_intrinsics(llmod: ModuleRef) -> HashMap<&'static str, ValueRef> {
26592661 T_void()));
26602662 let memcpy32 =
26612663 decl_cdecl_fn(llmod, " llvm. memcpy. p0i8. p0i8. i32 ",
2662- T_fn(copy T_memcpy32_args, T_void()));
2664+ T_fn(T_memcpy32_args, T_void()));
26632665 let memcpy64 =
26642666 decl_cdecl_fn(llmod, " llvm. memcpy. p0i8. p0i8. i64 ",
2665- T_fn(copy T_memcpy64_args, T_void()));
2667+ T_fn(T_memcpy64_args, T_void()));
26662668 let memmove32 =
26672669 decl_cdecl_fn(llmod, " llvm. memmove. p0i8. p0i8. i32 ",
26682670 T_fn(T_memcpy32_args, T_void()));
@@ -3038,9 +3040,13 @@ pub fn trans_crate(sess: session::Session,
30383040 let llmod_id = link_meta.name.to_owned() + " . rc";
30393041
30403042 unsafe {
3043+ if !llvm::LLVMRustStartMultithreading() {
3044+ sess.bug(" couldn' t enable multi-threaded LLVM ");
3045+ }
3046+ let llcx = llvm::LLVMContextCreate();
3047+ set_task_llcx(llcx);
30413048 let llmod = str::as_c_str(llmod_id, |buf| {
3042- llvm::LLVMModuleCreateWithNameInContext
3043- (buf, llvm::LLVMGetGlobalContext())
3049+ llvm::LLVMModuleCreateWithNameInContext(buf, llcx)
30443050 });
30453051 let data_layout: &str = sess.targ_cfg.target_strs.data_layout;
30463052 let targ_triple: &str = sess.targ_cfg.target_strs.target_triple;
@@ -3071,6 +3077,7 @@ pub fn trans_crate(sess: session::Session,
30713077 let ccx = @CrateContext {
30723078 sess: sess,
30733079 llmod: llmod,
3080+ llcx: llcx,
30743081 td: td,
30753082 tn: tn,
30763083 externs: @mut HashMap::new(),
@@ -3124,7 +3131,9 @@ pub fn trans_crate(sess: session::Session,
31243131 int_type: int_type,
31253132 float_type: float_type,
31263133 opaque_vec_type: T_opaque_vec(targ_cfg),
3127- builder: BuilderRef_res(unsafe { llvm::LLVMCreateBuilder() }),
3134+ builder: BuilderRef_res(unsafe {
3135+ llvm::LLVMCreateBuilderInContext(llcx)
3136+ }),
31283137 shape_cx: mk_ctxt(llmod),
31293138 crate_map: crate_map,
31303139 uses_gc: @mut false,
@@ -3172,3 +3181,16 @@ pub fn trans_crate(sess: session::Session,
31723181 return (llmod, link_meta);
31733182 }
31743183}
3184+
3185+ fn task_local_llcx_key(_v: @ContextRef) {}
3186+
3187+ pub fn task_llcx() -> ContextRef {
3188+ let opt = unsafe { local_data::local_data_get(task_local_llcx_key) };
3189+ *opt.expect(" task-local LLVMContextRef wasn' t ever set!" )
3190+ }
3191+
3192+ fn set_task_llcx ( c : ContextRef ) {
3193+ unsafe {
3194+ local_data:: local_data_set ( task_local_llcx_key, @c) ;
3195+ }
3196+ }
0 commit comments