Skip to content

Commit 5bd50ca

Browse files
committed
Remove all usage of the global LLVMContextRef
This allows parallel usage of the rustc library
1 parent 0252c30 commit 5bd50ca

File tree

12 files changed

+141
-129
lines changed

12 files changed

+141
-129
lines changed

configure

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,8 +818,7 @@ do
818818

819819
# Disable unused LLVM features
820820
LLVM_OPTS="$LLVM_DBG_OPTS --disable-docs \
821-
--enable-bindings=none --disable-threads \
822-
--disable-pthreads"
821+
--enable-bindings=none"
823822

824823
if [ "$CFG_C_COMPILER" = "clang" ]
825824
then

src/librustc/lib/llvm.rs

Lines changed: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -231,23 +231,21 @@ pub mod llvm {
231231
#[fast_ffi]
232232
pub unsafe fn LLVMContextCreate() -> ContextRef;
233233
#[fast_ffi]
234-
pub unsafe fn LLVMGetGlobalContext() -> ContextRef;
235-
#[fast_ffi]
236234
pub unsafe fn LLVMContextDispose(C: ContextRef);
237235
#[fast_ffi]
238236
pub unsafe fn LLVMGetMDKindIDInContext(C: ContextRef,
239237
Name: *c_char,
240238
SLen: c_uint)
241239
-> c_uint;
242-
#[fast_ffi]
243-
pub unsafe fn LLVMGetMDKindID(Name: *c_char, SLen: c_uint) -> c_uint;
244240

245241
/* Create and destroy modules. */
246242
#[fast_ffi]
247243
pub unsafe fn LLVMModuleCreateWithNameInContext(ModuleID: *c_char,
248244
C: ContextRef)
249245
-> ModuleRef;
250246
#[fast_ffi]
247+
pub unsafe fn LLVMGetModuleContext(M: ModuleRef) -> ContextRef;
248+
#[fast_ffi]
251249
pub unsafe fn LLVMDisposeModule(M: ModuleRef);
252250

253251
/** Data layout. See Module::getDataLayout. */
@@ -292,18 +290,6 @@ pub mod llvm {
292290
pub unsafe fn LLVMIntTypeInContext(C: ContextRef,
293291
NumBits: c_uint) -> TypeRef;
294292

295-
#[fast_ffi]
296-
pub unsafe fn LLVMInt1Type() -> TypeRef;
297-
#[fast_ffi]
298-
pub unsafe fn LLVMInt8Type() -> TypeRef;
299-
#[fast_ffi]
300-
pub unsafe fn LLVMInt16Type() -> TypeRef;
301-
#[fast_ffi]
302-
pub unsafe fn LLVMInt32Type() -> TypeRef;
303-
#[fast_ffi]
304-
pub unsafe fn LLVMInt64Type() -> TypeRef;
305-
#[fast_ffi]
306-
pub unsafe fn LLVMIntType(NumBits: c_uint) -> TypeRef;
307293
#[fast_ffi]
308294
pub unsafe fn LLVMGetIntTypeWidth(IntegerTy: TypeRef) -> c_uint;
309295

@@ -319,17 +305,6 @@ pub mod llvm {
319305
#[fast_ffi]
320306
pub unsafe fn LLVMPPCFP128TypeInContext(C: ContextRef) -> TypeRef;
321307

322-
#[fast_ffi]
323-
pub unsafe fn LLVMFloatType() -> TypeRef;
324-
#[fast_ffi]
325-
pub unsafe fn LLVMDoubleType() -> TypeRef;
326-
#[fast_ffi]
327-
pub unsafe fn LLVMX86FP80Type() -> TypeRef;
328-
#[fast_ffi]
329-
pub unsafe fn LLVMFP128Type() -> TypeRef;
330-
#[fast_ffi]
331-
pub unsafe fn LLVMPPCFP128Type() -> TypeRef;
332-
333308
/* Operations on function types */
334309
#[fast_ffi]
335310
pub unsafe fn LLVMFunctionType(ReturnType: TypeRef,
@@ -353,11 +328,6 @@ pub mod llvm {
353328
ElementCount: c_uint,
354329
Packed: Bool) -> TypeRef;
355330
#[fast_ffi]
356-
pub unsafe fn LLVMStructType(ElementTypes: *TypeRef,
357-
ElementCount: c_uint,
358-
Packed: Bool)
359-
-> TypeRef;
360-
#[fast_ffi]
361331
pub unsafe fn LLVMCountStructElementTypes(StructTy: TypeRef)
362332
-> c_uint;
363333
#[fast_ffi]
@@ -395,13 +365,6 @@ pub mod llvm {
395365
#[fast_ffi]
396366
pub unsafe fn LLVMMetadataTypeInContext(C: ContextRef) -> TypeRef;
397367

398-
#[fast_ffi]
399-
pub unsafe fn LLVMVoidType() -> TypeRef;
400-
#[fast_ffi]
401-
pub unsafe fn LLVMLabelType() -> TypeRef;
402-
#[fast_ffi]
403-
pub unsafe fn LLVMMetadataType() -> TypeRef;
404-
405368
/* Operations on all values */
406369
#[fast_ffi]
407370
pub unsafe fn LLVMTypeOf(Val: ValueRef) -> TypeRef;
@@ -474,15 +437,11 @@ pub mod llvm {
474437
SLen: c_uint)
475438
-> ValueRef;
476439
#[fast_ffi]
477-
pub unsafe fn LLVMMDString(Str: *c_char, SLen: c_uint) -> ValueRef;
478-
#[fast_ffi]
479440
pub unsafe fn LLVMMDNodeInContext(C: ContextRef,
480441
Vals: *ValueRef,
481442
Count: c_uint)
482443
-> ValueRef;
483444
#[fast_ffi]
484-
pub unsafe fn LLVMMDNode(Vals: *ValueRef, Count: c_uint) -> ValueRef;
485-
#[fast_ffi]
486445
pub unsafe fn LLVMAddNamedMetadataOperand(M: ModuleRef, Str: *c_char,
487446
Val: ValueRef);
488447

@@ -536,20 +495,11 @@ pub mod llvm {
536495
Packed: Bool) -> ValueRef;
537496

538497
#[fast_ffi]
539-
pub unsafe fn LLVMConstString(Str: *c_char,
540-
Length: c_uint,
541-
DontNullTerminate: Bool)
542-
-> ValueRef;
543-
#[fast_ffi]
544498
pub unsafe fn LLVMConstArray(ElementTy: TypeRef,
545499
ConstantVals: *ValueRef,
546500
Length: c_uint)
547501
-> ValueRef;
548502
#[fast_ffi]
549-
pub unsafe fn LLVMConstStruct(ConstantVals: *ValueRef,
550-
Count: c_uint,
551-
Packed: Bool) -> ValueRef;
552-
#[fast_ffi]
553503
pub unsafe fn LLVMConstVector(ScalarConstantVals: *ValueRef,
554504
Size: c_uint) -> ValueRef;
555505

@@ -962,15 +912,6 @@ pub mod llvm {
962912
BB: BasicBlockRef,
963913
Name: *c_char)
964914
-> BasicBlockRef;
965-
966-
#[fast_ffi]
967-
pub unsafe fn LLVMAppendBasicBlock(Fn: ValueRef,
968-
Name: *c_char)
969-
-> BasicBlockRef;
970-
#[fast_ffi]
971-
pub unsafe fn LLVMInsertBasicBlock(InsertBeforeBB: BasicBlockRef,
972-
Name: *c_char)
973-
-> BasicBlockRef;
974915
#[fast_ffi]
975916
pub unsafe fn LLVMDeleteBasicBlock(BB: BasicBlockRef);
976917

@@ -1031,8 +972,6 @@ pub mod llvm {
1031972
#[fast_ffi]
1032973
pub unsafe fn LLVMCreateBuilderInContext(C: ContextRef) -> BuilderRef;
1033974
#[fast_ffi]
1034-
pub unsafe fn LLVMCreateBuilder() -> BuilderRef;
1035-
#[fast_ffi]
1036975
pub unsafe fn LLVMPositionBuilder(Builder: BuilderRef,
1037976
Block: BasicBlockRef,
1038977
Instr: ValueRef);
@@ -1859,7 +1798,8 @@ pub mod llvm {
18591798

18601799
/** Parses LLVM asm in the given file */
18611800
#[fast_ffi]
1862-
pub unsafe fn LLVMRustParseAssemblyFile(Filename: *c_char)
1801+
pub unsafe fn LLVMRustParseAssemblyFile(Filename: *c_char,
1802+
C: ContextRef)
18631803
-> ModuleRef;
18641804

18651805
#[fast_ffi]
@@ -1875,6 +1815,9 @@ pub mod llvm {
18751815
#[fast_ffi]
18761816
pub unsafe fn LLVMRustPrintPassTimings();
18771817

1818+
#[fast_ffi]
1819+
pub unsafe fn LLVMRustStartMultithreading() -> bool;
1820+
18781821
#[fast_ffi]
18791822
pub unsafe fn LLVMStructCreateNamed(C: ContextRef, Name: *c_char)
18801823
-> TypeRef;

src/librustc/middle/trans/base.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use back::link::{mangle_exported_name};
2727
use back::{link, abi, upcall};
2828
use driver::session;
2929
use driver::session::Session;
30-
use lib::llvm::{ModuleRef, ValueRef, TypeRef, BasicBlockRef};
30+
use lib::llvm::{ContextRef, ModuleRef, ValueRef, TypeRef, BasicBlockRef};
3131
use lib::llvm::{True, False};
3232
use lib::llvm::{llvm, mk_target_data, mk_type_names};
3333
use lib;
@@ -65,6 +65,7 @@ use util::ppaux::{Repr, ty_to_str};
6565
use core::hash;
6666
use core::hashmap::{HashMap, HashSet};
6767
use core::libc::c_uint;
68+
use core::local_data;
6869
use std::time;
6970
use syntax::ast::ident;
7071
use syntax::ast_map::{path, path_elt_to_str, path_name};
@@ -1178,7 +1179,7 @@ pub fn new_block(cx: fn_ctxt, parent: Option<block>, kind: block_kind,
11781179
};
11791180
unsafe {
11801181
let llbb = str::as_c_str(*cx.ccx.sess.str_of(s), |buf| {
1181-
llvm::LLVMAppendBasicBlock(cx.llfn, buf)
1182+
llvm::LLVMAppendBasicBlockInContext(cx.ccx.llcx, cx.llfn, buf)
11821183
});
11831184
let bcx = mk_block(llbb,
11841185
parent,
@@ -1558,11 +1559,12 @@ pub struct BasicBlocks {
15581559
// Creates the standard set of basic blocks for a function
15591560
pub fn mk_standard_basic_blocks(llfn: ValueRef) -> BasicBlocks {
15601561
unsafe {
1562+
let cx = task_llcx();
15611563
BasicBlocks {
15621564
sa: str::as_c_str("static_allocas",
1563-
|buf| llvm::LLVMAppendBasicBlock(llfn, buf)),
1565+
|buf| llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf)),
15641566
rt: str::as_c_str("return",
1565-
|buf| llvm::LLVMAppendBasicBlock(llfn, buf))
1567+
|buf| llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf))
15661568
}
15671569
}
15681570
}
@@ -2300,7 +2302,7 @@ pub fn create_entry_wrapper(ccx: @CrateContext,
23002302
};
23012303
let llbb = str::as_c_str("top", |buf| {
23022304
unsafe {
2303-
llvm::LLVMAppendBasicBlock(llfn, buf)
2305+
llvm::LLVMAppendBasicBlockInContext(ccx.llcx, llfn, buf)
23042306
}
23052307
});
23062308
let bld = ccx.builder.B;
@@ -2625,10 +2627,10 @@ pub fn declare_intrinsics(llmod: ModuleRef) -> HashMap<~str, ValueRef> {
26252627
T_void()));
26262628
let memcpy32 =
26272629
decl_cdecl_fn(llmod, "llvm.memcpy.p0i8.p0i8.i32",
2628-
T_fn(copy T_memcpy32_args, T_void()));
2630+
T_fn(T_memcpy32_args, T_void()));
26292631
let memcpy64 =
26302632
decl_cdecl_fn(llmod, "llvm.memcpy.p0i8.p0i8.i64",
2631-
T_fn(copy T_memcpy64_args, T_void()));
2633+
T_fn(T_memcpy64_args, T_void()));
26322634
let memmove32 =
26332635
decl_cdecl_fn(llmod, "llvm.memmove.p0i8.p0i8.i32",
26342636
T_fn(T_memcpy32_args, T_void()));
@@ -3004,9 +3006,13 @@ pub fn trans_crate(sess: session::Session,
30043006
let llmod_id = link_meta.name.to_owned() + ~".rc";
30053007
30063008
unsafe {
3009+
if !llvm::LLVMRustStartMultithreading() {
3010+
sess.bug("couldn't enable multi-threaded LLVM");
3011+
}
3012+
let llcx = llvm::LLVMContextCreate();
3013+
set_task_llcx(llcx);
30073014
let llmod = str::as_c_str(llmod_id, |buf| {
3008-
llvm::LLVMModuleCreateWithNameInContext
3009-
(buf, llvm::LLVMGetGlobalContext())
3015+
llvm::LLVMModuleCreateWithNameInContext(buf, llcx)
30103016
});
30113017
let data_layout: &str = sess.targ_cfg.target_strs.data_layout;
30123018
let targ_triple: &str = sess.targ_cfg.target_strs.target_triple;
@@ -3040,6 +3046,7 @@ pub fn trans_crate(sess: session::Session,
30403046
let ccx = @CrateContext {
30413047
sess: sess,
30423048
llmod: llmod,
3049+
llcx: llcx,
30433050
td: td,
30443051
tn: tn,
30453052
externs: @mut HashMap::new(),
@@ -3094,7 +3101,9 @@ pub fn trans_crate(sess: session::Session,
30943101
float_type: float_type,
30953102
task_type: task_type,
30963103
opaque_vec_type: T_opaque_vec(targ_cfg),
3097-
builder: BuilderRef_res(unsafe { llvm::LLVMCreateBuilder() }),
3104+
builder: BuilderRef_res(unsafe {
3105+
llvm::LLVMCreateBuilderInContext(llcx)
3106+
}),
30983107
shape_cx: mk_ctxt(llmod),
30993108
crate_map: crate_map,
31003109
uses_gc: @mut false,
@@ -3142,3 +3151,16 @@ pub fn trans_crate(sess: session::Session,
31423151
return (llmod, link_meta);
31433152
}
31443153
}
3154+
3155+
fn task_local_llcx_key(_v: @ContextRef) {}
3156+
3157+
pub fn task_llcx() -> ContextRef {
3158+
let opt = unsafe { local_data::local_data_get(task_local_llcx_key) };
3159+
*opt.expect("task-local LLVMContextRef wasn't ever set!")
3160+
}
3161+
3162+
fn set_task_llcx(c: ContextRef) {
3163+
unsafe {
3164+
local_data::local_data_set(task_local_llcx_key, @c);
3165+
}
3166+
}

src/librustc/middle/trans/build.rs

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

563563
do vec::as_imm_buf([min, max]) |ptr, len| {
564564
llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
565-
llvm::LLVMMDNode(ptr, len as c_uint));
565+
llvm::LLVMMDNodeInContext(cx.fcx.ccx.llcx,
566+
ptr, len as c_uint));
566567
}
567568
}
568569

src/librustc/middle/trans/cabi_mips.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use lib::llvm::{llvm, TypeRef, Integer, Pointer, Float, Double};
1313
use lib::llvm::{Struct, Array, Attribute};
1414
use lib::llvm::{StructRetAttribute};
1515
use lib::llvm::True;
16+
use middle::trans::base::task_llcx;
1617
use middle::trans::common::*;
1718
use middle::trans::cabi::*;
1819

@@ -164,7 +165,7 @@ fn coerce_to_int(size: uint) -> ~[TypeRef] {
164165
let r = size % 32;
165166
if r > 0 {
166167
unsafe {
167-
args.push(llvm::LLVMIntType(r as c_uint))
168+
args.push(llvm::LLVMIntTypeInContext(task_llcx(), r as c_uint))
168169
}
169170
}
170171

src/librustc/middle/trans/closure.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,9 @@ pub fn load_environment(fcx: fn_ctxt,
320320
str::as_c_str("load_env",
321321
|buf|
322322
unsafe {
323-
llvm::LLVMAppendBasicBlock(fcx.llfn, buf)
323+
llvm::LLVMAppendBasicBlockInContext(fcx.ccx.llcx,
324+
fcx.llfn,
325+
buf)
324326
});
325327
fcx.llloadenv = Some(ll);
326328
ll

0 commit comments

Comments
 (0)