Skip to content

Commit 779191c

Browse files
committed
Remove all usage of the global LLVMContextRef
This allows parallel usage of the rustc library
1 parent 1310212 commit 779191c

File tree

10 files changed

+126
-113
lines changed

10 files changed

+126
-113
lines changed

src/librustc/lib/llvm.rs

Lines changed: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -239,23 +239,21 @@ pub mod llvm {
239239
#[fast_ffi]
240240
pub unsafe fn LLVMContextCreate() -> ContextRef;
241241
#[fast_ffi]
242-
pub unsafe fn LLVMGetGlobalContext() -> ContextRef;
243-
#[fast_ffi]
244242
pub unsafe fn LLVMContextDispose(C: ContextRef);
245243
#[fast_ffi]
246244
pub unsafe fn LLVMGetMDKindIDInContext(C: ContextRef,
247245
Name: *c_char,
248246
SLen: c_uint)
249247
-> c_uint;
250-
#[fast_ffi]
251-
pub unsafe fn LLVMGetMDKindID(Name: *c_char, SLen: c_uint) -> c_uint;
252248

253249
/* Create and destroy modules. */
254250
#[fast_ffi]
255251
pub unsafe fn LLVMModuleCreateWithNameInContext(ModuleID: *c_char,
256252
C: ContextRef)
257253
-> ModuleRef;
258254
#[fast_ffi]
255+
pub unsafe fn LLVMGetModuleContext(M: ModuleRef) -> ContextRef;
256+
#[fast_ffi]
259257
pub unsafe fn LLVMDisposeModule(M: ModuleRef);
260258

261259
/** Data layout. See Module::getDataLayout. */
@@ -300,18 +298,6 @@ pub mod llvm {
300298
pub unsafe fn LLVMIntTypeInContext(C: ContextRef,
301299
NumBits: c_uint) -> TypeRef;
302300

303-
#[fast_ffi]
304-
pub unsafe fn LLVMInt1Type() -> TypeRef;
305-
#[fast_ffi]
306-
pub unsafe fn LLVMInt8Type() -> TypeRef;
307-
#[fast_ffi]
308-
pub unsafe fn LLVMInt16Type() -> TypeRef;
309-
#[fast_ffi]
310-
pub unsafe fn LLVMInt32Type() -> TypeRef;
311-
#[fast_ffi]
312-
pub unsafe fn LLVMInt64Type() -> TypeRef;
313-
#[fast_ffi]
314-
pub unsafe fn LLVMIntType(NumBits: c_uint) -> TypeRef;
315301
#[fast_ffi]
316302
pub unsafe fn LLVMGetIntTypeWidth(IntegerTy: TypeRef) -> c_uint;
317303

@@ -327,17 +313,6 @@ pub mod llvm {
327313
#[fast_ffi]
328314
pub unsafe fn LLVMPPCFP128TypeInContext(C: ContextRef) -> TypeRef;
329315

330-
#[fast_ffi]
331-
pub unsafe fn LLVMFloatType() -> TypeRef;
332-
#[fast_ffi]
333-
pub unsafe fn LLVMDoubleType() -> TypeRef;
334-
#[fast_ffi]
335-
pub unsafe fn LLVMX86FP80Type() -> TypeRef;
336-
#[fast_ffi]
337-
pub unsafe fn LLVMFP128Type() -> TypeRef;
338-
#[fast_ffi]
339-
pub unsafe fn LLVMPPCFP128Type() -> TypeRef;
340-
341316
/* Operations on function types */
342317
#[fast_ffi]
343318
pub unsafe fn LLVMFunctionType(ReturnType: TypeRef,
@@ -361,11 +336,6 @@ pub mod llvm {
361336
ElementCount: c_uint,
362337
Packed: Bool) -> TypeRef;
363338
#[fast_ffi]
364-
pub unsafe fn LLVMStructType(ElementTypes: *TypeRef,
365-
ElementCount: c_uint,
366-
Packed: Bool)
367-
-> TypeRef;
368-
#[fast_ffi]
369339
pub unsafe fn LLVMCountStructElementTypes(StructTy: TypeRef)
370340
-> c_uint;
371341
#[fast_ffi]
@@ -403,13 +373,6 @@ pub mod llvm {
403373
#[fast_ffi]
404374
pub unsafe fn LLVMMetadataTypeInContext(C: ContextRef) -> TypeRef;
405375

406-
#[fast_ffi]
407-
pub unsafe fn LLVMVoidType() -> TypeRef;
408-
#[fast_ffi]
409-
pub unsafe fn LLVMLabelType() -> TypeRef;
410-
#[fast_ffi]
411-
pub unsafe fn LLVMMetadataType() -> TypeRef;
412-
413376
/* Operations on all values */
414377
#[fast_ffi]
415378
pub unsafe fn LLVMTypeOf(Val: ValueRef) -> TypeRef;
@@ -482,15 +445,11 @@ pub mod llvm {
482445
SLen: c_uint)
483446
-> ValueRef;
484447
#[fast_ffi]
485-
pub unsafe fn LLVMMDString(Str: *c_char, SLen: c_uint) -> ValueRef;
486-
#[fast_ffi]
487448
pub unsafe fn LLVMMDNodeInContext(C: ContextRef,
488449
Vals: *ValueRef,
489450
Count: c_uint)
490451
-> ValueRef;
491452
#[fast_ffi]
492-
pub unsafe fn LLVMMDNode(Vals: *ValueRef, Count: c_uint) -> ValueRef;
493-
#[fast_ffi]
494453
pub unsafe fn LLVMAddNamedMetadataOperand(M: ModuleRef, Str: *c_char,
495454
Val: ValueRef);
496455

@@ -544,20 +503,11 @@ pub mod llvm {
544503
Packed: Bool) -> ValueRef;
545504

546505
#[fast_ffi]
547-
pub unsafe fn LLVMConstString(Str: *c_char,
548-
Length: c_uint,
549-
DontNullTerminate: Bool)
550-
-> ValueRef;
551-
#[fast_ffi]
552506
pub unsafe fn LLVMConstArray(ElementTy: TypeRef,
553507
ConstantVals: *ValueRef,
554508
Length: c_uint)
555509
-> ValueRef;
556510
#[fast_ffi]
557-
pub unsafe fn LLVMConstStruct(ConstantVals: *ValueRef,
558-
Count: c_uint,
559-
Packed: Bool) -> ValueRef;
560-
#[fast_ffi]
561511
pub unsafe fn LLVMConstVector(ScalarConstantVals: *ValueRef,
562512
Size: c_uint) -> ValueRef;
563513

@@ -970,15 +920,6 @@ pub mod llvm {
970920
BB: BasicBlockRef,
971921
Name: *c_char)
972922
-> BasicBlockRef;
973-
974-
#[fast_ffi]
975-
pub unsafe fn LLVMAppendBasicBlock(Fn: ValueRef,
976-
Name: *c_char)
977-
-> BasicBlockRef;
978-
#[fast_ffi]
979-
pub unsafe fn LLVMInsertBasicBlock(InsertBeforeBB: BasicBlockRef,
980-
Name: *c_char)
981-
-> BasicBlockRef;
982923
#[fast_ffi]
983924
pub unsafe fn LLVMDeleteBasicBlock(BB: BasicBlockRef);
984925

@@ -1039,8 +980,6 @@ pub mod llvm {
1039980
#[fast_ffi]
1040981
pub unsafe fn LLVMCreateBuilderInContext(C: ContextRef) -> BuilderRef;
1041982
#[fast_ffi]
1042-
pub unsafe fn LLVMCreateBuilder() -> BuilderRef;
1043-
#[fast_ffi]
1044983
pub unsafe fn LLVMPositionBuilder(Builder: BuilderRef,
1045984
Block: BasicBlockRef,
1046985
Instr: ValueRef);
@@ -1893,7 +1832,8 @@ pub mod llvm {
18931832

18941833
/** Parses LLVM asm in the given file */
18951834
#[fast_ffi]
1896-
pub unsafe fn LLVMRustParseAssemblyFile(Filename: *c_char)
1835+
pub unsafe fn LLVMRustParseAssemblyFile(Filename: *c_char,
1836+
C: ContextRef)
18971837
-> ModuleRef;
18981838

18991839
#[fast_ffi]
@@ -1909,6 +1849,9 @@ pub mod llvm {
19091849
#[fast_ffi]
19101850
pub unsafe fn LLVMRustPrintPassTimings();
19111851

1852+
#[fast_ffi]
1853+
pub unsafe fn LLVMRustStartMultithreading() -> bool;
1854+
19121855
#[fast_ffi]
19131856
pub unsafe fn LLVMStructCreateNamed(C: ContextRef, Name: *c_char)
19141857
-> TypeRef;

src/librustc/middle/trans/base.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use back::link::{mangle_exported_name};
2929
use back::{link, abi, upcall};
3030
use driver::session;
3131
use driver::session::Session;
32-
use lib::llvm::{ModuleRef, ValueRef, TypeRef, BasicBlockRef};
32+
use lib::llvm::{ContextRef, ModuleRef, ValueRef, TypeRef, BasicBlockRef};
3333
use lib::llvm::{True, False};
3434
use lib::llvm::{llvm, mk_target_data, mk_type_names};
3535
use lib;
@@ -73,6 +73,7 @@ use core::libc::c_uint;
7373
use core::str;
7474
use core::uint;
7575
use core::vec;
76+
use core::local_data;
7677
use extra::time;
7778
use syntax::ast::ident;
7879
use syntax::ast_map::{path, path_elt_to_str, path_name};
@@ -1186,7 +1187,7 @@ pub fn new_block(cx: fn_ctxt, parent: Option<block>, kind: block_kind,
11861187
};
11871188
unsafe {
11881189
let llbb = str::as_c_str(*cx.ccx.sess.str_of(s), |buf| {
1189-
llvm::LLVMAppendBasicBlock(cx.llfn, buf)
1190+
llvm::LLVMAppendBasicBlockInContext(cx.ccx.llcx, cx.llfn, buf)
11901191
});
11911192
let bcx = mk_block(llbb,
11921193
parent,
@@ -1560,11 +1561,12 @@ pub struct BasicBlocks {
15601561
// Creates the standard set of basic blocks for a function
15611562
pub fn mk_standard_basic_blocks(llfn: ValueRef) -> BasicBlocks {
15621563
unsafe {
1564+
let cx = task_llcx();
15631565
BasicBlocks {
15641566
sa: str::as_c_str("static_allocas",
1565-
|buf| llvm::LLVMAppendBasicBlock(llfn, buf)),
1567+
|buf| llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf)),
15661568
rt: str::as_c_str("return",
1567-
|buf| llvm::LLVMAppendBasicBlock(llfn, buf))
1569+
|buf| llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf))
15681570
}
15691571
}
15701572
}
@@ -2347,7 +2349,7 @@ pub fn create_entry_wrapper(ccx: @CrateContext,
23472349
};
23482350
let llbb = str::as_c_str("top", |buf| {
23492351
unsafe {
2350-
llvm::LLVMAppendBasicBlock(llfn, buf)
2352+
llvm::LLVMAppendBasicBlockInContext(ccx.llcx, llfn, buf)
23512353
}
23522354
});
23532355
let bld = ccx.builder.B;
@@ -2665,10 +2667,10 @@ pub fn declare_intrinsics(llmod: ModuleRef) -> HashMap<~str, ValueRef> {
26652667
T_void()));
26662668
let memcpy32 =
26672669
decl_cdecl_fn(llmod, "llvm.memcpy.p0i8.p0i8.i32",
2668-
T_fn(copy T_memcpy32_args, T_void()));
2670+
T_fn(T_memcpy32_args, T_void()));
26692671
let memcpy64 =
26702672
decl_cdecl_fn(llmod, "llvm.memcpy.p0i8.p0i8.i64",
2671-
T_fn(copy T_memcpy64_args, T_void()));
2673+
T_fn(T_memcpy64_args, T_void()));
26722674
let memmove32 =
26732675
decl_cdecl_fn(llmod, "llvm.memmove.p0i8.p0i8.i32",
26742676
T_fn(T_memcpy32_args, T_void()));
@@ -3043,9 +3045,13 @@ pub fn trans_crate(sess: session::Session,
30433045
let llmod_id = link_meta.name.to_owned() + ".rc";
30443046
30453047
unsafe {
3048+
if !llvm::LLVMRustStartMultithreading() {
3049+
sess.bug("couldn't enable multi-threaded LLVM");
3050+
}
3051+
let llcx = llvm::LLVMContextCreate();
3052+
set_task_llcx(llcx);
30463053
let llmod = str::as_c_str(llmod_id, |buf| {
3047-
llvm::LLVMModuleCreateWithNameInContext
3048-
(buf, llvm::LLVMGetGlobalContext())
3054+
llvm::LLVMModuleCreateWithNameInContext(buf, llcx)
30493055
});
30503056
let data_layout: &str = sess.targ_cfg.target_strs.data_layout;
30513057
let targ_triple: &str = sess.targ_cfg.target_strs.target_triple;
@@ -3079,6 +3085,7 @@ pub fn trans_crate(sess: session::Session,
30793085
let ccx = @CrateContext {
30803086
sess: sess,
30813087
llmod: llmod,
3088+
llcx: llcx,
30823089
td: td,
30833090
tn: tn,
30843091
externs: @mut HashMap::new(),
@@ -3133,7 +3140,9 @@ pub fn trans_crate(sess: session::Session,
31333140
float_type: float_type,
31343141
task_type: task_type,
31353142
opaque_vec_type: T_opaque_vec(targ_cfg),
3136-
builder: BuilderRef_res(unsafe { llvm::LLVMCreateBuilder() }),
3143+
builder: BuilderRef_res(unsafe {
3144+
llvm::LLVMCreateBuilderInContext(llcx)
3145+
}),
31373146
shape_cx: mk_ctxt(llmod),
31383147
crate_map: crate_map,
31393148
uses_gc: @mut false,
@@ -3181,3 +3190,16 @@ pub fn trans_crate(sess: session::Session,
31813190
return (llmod, link_meta);
31823191
}
31833192
}
3193+
3194+
fn task_local_llcx_key(_v: @ContextRef) {}
3195+
3196+
pub fn task_llcx() -> ContextRef {
3197+
let opt = unsafe { local_data::local_data_get(task_local_llcx_key) };
3198+
*opt.expect("task-local LLVMContextRef wasn't ever set!")
3199+
}
3200+
3201+
fn set_task_llcx(c: ContextRef) {
3202+
unsafe {
3203+
local_data::local_data_set(task_local_llcx_key, @c);
3204+
}
3205+
}

src/librustc/middle/trans/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ pub fn LoadRangeAssert(cx: block, PointerVal: ValueRef, lo: c_ulonglong,
564564

565565
do vec::as_imm_buf([min, max]) |ptr, len| {
566566
llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
567-
llvm::LLVMMDNode(ptr, len as c_uint));
567+
llvm::LLVMMDNodeInContext(cx.fcx.ccx.llcx,
568+
ptr, len as c_uint));
568569
}
569570
}
570571

src/librustc/middle/trans/cabi_mips.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use lib::llvm::{llvm, TypeRef, Integer, Pointer, Float, Double};
1919
use lib::llvm::{Struct, Array, Attribute};
2020
use lib::llvm::{StructRetAttribute};
2121
use lib::llvm::True;
22+
use middle::trans::base::task_llcx;
2223
use middle::trans::common::*;
2324
use middle::trans::cabi::*;
2425

@@ -166,7 +167,7 @@ fn coerce_to_int(size: uint) -> ~[TypeRef] {
166167
let r = size % 32;
167168
if r > 0 {
168169
unsafe {
169-
args.push(llvm::LLVMIntType(r as c_uint))
170+
args.push(llvm::LLVMIntTypeInContext(task_llcx(), r as c_uint))
170171
}
171172
}
172173

src/librustc/middle/trans/closure.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ pub fn load_environment(fcx: fn_ctxt,
326326
str::as_c_str("load_env",
327327
|buf|
328328
unsafe {
329-
llvm::LLVMAppendBasicBlock(fcx.llfn, buf)
329+
llvm::LLVMAppendBasicBlockInContext(fcx.ccx.llcx,
330+
fcx.llfn,
331+
buf)
330332
});
331333
fcx.llloadenv = Some(ll);
332334
ll

0 commit comments

Comments
 (0)