Skip to content

Commit 863927c

Browse files
committed
rewrite post-processing routines not to require a CrateContext
These do some low-level munging on the LLVM data structures. Unclear that they need to operate as a "second pass" but leave it for now.
1 parent bc79f01 commit 863927c

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

src/librustc_trans/base.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,8 @@ fn write_metadata(cx: &SharedCrateContext,
799799
/// Find any symbols that are defined in one compilation unit, but not declared
800800
/// in any other compilation unit. Give these symbols internal linkage.
801801
fn internalize_symbols<'a, 'tcx>(sess: &Session,
802-
ccxs: &CrateContextList<'a, 'tcx>,
802+
scx: &SharedCrateContext<'a, 'tcx>,
803+
llvm_modules: &[ModuleLlvm],
803804
symbol_map: &SymbolMap<'tcx>,
804805
exported_symbols: &ExportedSymbols) {
805806
let export_threshold =
@@ -814,7 +815,6 @@ fn internalize_symbols<'a, 'tcx>(sess: &Session,
814815
.map(|&(ref name, _)| &name[..])
815816
.collect::<FxHashSet<&str>>();
816817

817-
let scx = ccxs.shared();
818818
let tcx = scx.tcx();
819819

820820
let incr_comp = sess.opts.debugging_opts.incremental.is_some();
@@ -829,8 +829,8 @@ fn internalize_symbols<'a, 'tcx>(sess: &Session,
829829
// incremental compilation, we don't need to collect. See below for more
830830
// information.
831831
if !incr_comp {
832-
for ccx in ccxs.iter_need_trans() {
833-
for val in iter_globals(ccx.llmod()).chain(iter_functions(ccx.llmod())) {
832+
for ll in llvm_modules {
833+
for val in iter_globals(ll.llmod).chain(iter_functions(ll.llmod)) {
834834
let linkage = llvm::LLVMRustGetLinkage(val);
835835
// We only care about external declarations (not definitions)
836836
// and available_externally definitions.
@@ -866,8 +866,8 @@ fn internalize_symbols<'a, 'tcx>(sess: &Session,
866866
// Examine each external definition. If the definition is not used in
867867
// any other compilation unit, and is not reachable from other crates,
868868
// then give it internal linkage.
869-
for ccx in ccxs.iter_need_trans() {
870-
for val in iter_globals(ccx.llmod()).chain(iter_functions(ccx.llmod())) {
869+
for ll in llvm_modules {
870+
for val in iter_globals(ll.llmod).chain(iter_functions(ll.llmod)) {
871871
let linkage = llvm::LLVMRustGetLinkage(val);
872872

873873
let is_externally_visible = (linkage == llvm::Linkage::ExternalLinkage) ||
@@ -926,33 +926,34 @@ fn internalize_symbols<'a, 'tcx>(sess: &Session,
926926
// when using MSVC linker. We do this only for data, as linker can fix up
927927
// code references on its own.
928928
// See #26591, #27438
929-
fn create_imps(cx: &CrateContextList) {
929+
fn create_imps(sess: &Session,
930+
llvm_modules: &[ModuleLlvm]) {
930931
// The x86 ABI seems to require that leading underscores are added to symbol
931932
// names, so we need an extra underscore on 32-bit. There's also a leading
932933
// '\x01' here which disables LLVM's symbol mangling (e.g. no extra
933934
// underscores added in front).
934-
let prefix = if cx.shared().sess().target.target.target_pointer_width == "32" {
935+
let prefix = if sess.target.target.target_pointer_width == "32" {
935936
"\x01__imp__"
936937
} else {
937938
"\x01__imp_"
938939
};
939940
unsafe {
940-
for ccx in cx.iter_need_trans() {
941-
let exported: Vec<_> = iter_globals(ccx.llmod())
941+
for ll in llvm_modules {
942+
let exported: Vec<_> = iter_globals(ll.llmod)
942943
.filter(|&val| {
943944
llvm::LLVMRustGetLinkage(val) ==
944945
llvm::Linkage::ExternalLinkage &&
945946
llvm::LLVMIsDeclaration(val) == 0
946947
})
947948
.collect();
948949

949-
let i8p_ty = Type::i8p(&ccx);
950+
let i8p_ty = Type::i8p_llcx(ll.llcx);
950951
for val in exported {
951952
let name = CStr::from_ptr(llvm::LLVMGetValueName(val));
952953
let mut imp_name = prefix.as_bytes().to_vec();
953954
imp_name.extend(name.to_bytes());
954955
let imp_name = CString::new(imp_name).unwrap();
955-
let imp = llvm::LLVMAddGlobal(ccx.llmod(),
956+
let imp = llvm::LLVMAddGlobal(ll.llmod,
956957
i8p_ty.to_ref(),
957958
imp_name.as_ptr() as *const _);
958959
let init = llvm::LLVMConstBitCast(val, i8p_ty.to_ref());
@@ -1244,11 +1245,23 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12441245
let exported_symbols = ExportedSymbols::compute_from(&shared_ccx,
12451246
&symbol_map);
12461247

1248+
// Get the list of llvm modules we created. We'll do a few wacky
1249+
// transforms on them now.
1250+
1251+
let llvm_modules: Vec<_> =
1252+
modules.iter()
1253+
.filter_map(|module| match module.source {
1254+
ModuleSource::Translated(llvm) => Some(llvm),
1255+
_ => None,
1256+
})
1257+
.collect();
1258+
12471259
// Now that we have all symbols that are exported from the CGUs of this
12481260
// crate, we can run the `internalize_symbols` pass.
12491261
time(shared_ccx.sess().time_passes(), "internalize symbols", || {
12501262
internalize_symbols(sess,
1251-
&crate_context_list,
1263+
&shared_ccx,
1264+
&llvm_modules,
12521265
&symbol_map,
12531266
&exported_symbols);
12541267
});
@@ -1259,7 +1272,7 @@ pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12591272

12601273
if sess.target.target.options.is_like_msvc &&
12611274
sess.crate_types.borrow().iter().any(|ct| *ct == config::CrateTypeRlib) {
1262-
create_imps(&crate_context_list);
1275+
create_imps(sess, &llvm_modules);
12631276
}
12641277

12651278
let linker_info = LinkerInfo::new(&shared_ccx, &exported_symbols);

src/librustc_trans/context.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -244,21 +244,6 @@ impl<'a, 'tcx: 'a> CrateContextList<'a, 'tcx> {
244244
filter_to_previous_work_product_unavail: false,
245245
}
246246
}
247-
248-
/// Iterator over all CCX that need translation (cannot reuse results from
249-
/// previous incr. comp.).
250-
pub fn iter_need_trans<'b>(&'b self) -> CrateContextIterator<'b, 'tcx> {
251-
CrateContextIterator {
252-
shared: self.shared,
253-
index: 0,
254-
local_ccxs: &self.local_ccxs[..],
255-
filter_to_previous_work_product_unavail: true,
256-
}
257-
}
258-
259-
pub fn shared(&self) -> &'a SharedCrateContext<'a, 'tcx> {
260-
self.shared
261-
}
262247
}
263248

264249
/// A CrateContext value binds together one LocalCrateContext with the

src/librustc_trans/type_.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![allow(non_upper_case_globals)]
1212

1313
use llvm;
14-
use llvm::{TypeRef, Bool, False, True, TypeKind};
14+
use llvm::{ContextRef, TypeRef, Bool, False, True, TypeKind};
1515
use llvm::{Float, Double, X86_FP80, PPC_FP128, FP128};
1616

1717
use context::CrateContext;
@@ -82,6 +82,10 @@ impl Type {
8282
ty!(llvm::LLVMInt8TypeInContext(ccx.llcx()))
8383
}
8484

85+
pub fn i8_llcx(llcx: ContextRef) -> Type {
86+
ty!(llvm::LLVMInt8TypeInContext(llcx))
87+
}
88+
8589
pub fn i16(ccx: &CrateContext) -> Type {
8690
ty!(llvm::LLVMInt16TypeInContext(ccx.llcx()))
8791
}
@@ -123,6 +127,10 @@ impl Type {
123127
Type::i8(ccx).ptr_to()
124128
}
125129

130+
pub fn i8p_llcx(llcx: ContextRef) -> Type {
131+
Type::i8_llcx(llcx).ptr_to()
132+
}
133+
126134
pub fn int(ccx: &CrateContext) -> Type {
127135
match &ccx.tcx().sess.target.target.target_pointer_width[..] {
128136
"16" => Type::i16(ccx),

0 commit comments

Comments
 (0)