Skip to content

Commit 0830cc9

Browse files
Revert "Clean up LLVM module naming (just use CodegenUnit names)."
This reverts commit f6894eb.
1 parent 89aa0bc commit 0830cc9

File tree

6 files changed

+69
-39
lines changed

6 files changed

+69
-39
lines changed

src/librustc_codegen_llvm/back/link.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ use std::process::{Output, Stdio};
4545
use std::str;
4646
use syntax::attr;
4747

48+
/// The LLVM module name containing crate-metadata. This includes a `.` on
49+
/// purpose, so it cannot clash with the name of a user-defined module.
50+
pub const METADATA_MODULE_NAME: &'static str = "crate.metadata";
51+
52+
// same as for metadata above, but for allocator shim
53+
pub const ALLOCATOR_MODULE_NAME: &'static str = "crate.allocator";
54+
4855
pub use rustc_codegen_utils::link::{find_crate_name, filename_for_input, default_output_for_target,
4956
invalid_output_for_target, build_link_meta, out_filename,
5057
check_file_is_writeable};

src/librustc_codegen_llvm/back/lto.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ fn fat_lto(cgcx: &CodegenContext,
238238
.expect("must be codegen'ing at least one module");
239239
let module = modules.remove(costliest_module);
240240
let llmod = module.llvm().expect("can't lto pre-codegened modules").llmod;
241-
info!("using {:?} as a base module", module.name);
241+
info!("using {:?} as a base module", module.llmod_id);
242242

243243
// For all other modules we codegened we'll need to link them into our own
244244
// bitcode. All modules were codegened in their own LLVM context, however,
@@ -248,7 +248,7 @@ fn fat_lto(cgcx: &CodegenContext,
248248
for module in modules {
249249
let llvm = module.llvm().expect("can't lto pre-codegened modules");
250250
let buffer = ModuleBuffer::new(llvm.llmod);
251-
let llmod_id = CString::new(&module.name[..]).unwrap();
251+
let llmod_id = CString::new(&module.llmod_id[..]).unwrap();
252252
serialized_modules.push((SerializedModule::Local(buffer), llmod_id));
253253
}
254254

@@ -376,9 +376,9 @@ fn thin_lto(cgcx: &CodegenContext,
376376
// the most expensive portion of this small bit of global
377377
// analysis!
378378
for (i, module) in modules.iter().enumerate() {
379-
info!("local module: {} - {}", i, module.name);
379+
info!("local module: {} - {}", i, module.llmod_id);
380380
let llvm = module.llvm().expect("can't lto precodegened module");
381-
let name = CString::new(module.name.clone()).unwrap();
381+
let name = CString::new(module.llmod_id.clone()).unwrap();
382382
let buffer = ThinBuffer::new(llvm.llmod);
383383
thin_modules.push(llvm::ThinLTOModule {
384384
identifier: name.as_ptr(),
@@ -387,7 +387,7 @@ fn thin_lto(cgcx: &CodegenContext,
387387
});
388388
thin_buffers.push(buffer);
389389
module_names.push(name);
390-
timeline.record(&module.name);
390+
timeline.record(&module.llmod_id);
391391
}
392392

393393
// FIXME: All upstream crates are deserialized internally in the
@@ -676,6 +676,7 @@ impl ThinModule {
676676
llcx,
677677
tm,
678678
}),
679+
llmod_id: self.name().to_string(),
679680
name: self.name().to_string(),
680681
kind: ModuleKind::Regular,
681682
};

src/librustc_codegen_llvm/back/write.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ unsafe fn codegen(cgcx: &CodegenContext,
694694

695695
if config.emit_bc_compressed {
696696
let dst = bc_out.with_extension(RLIB_BYTECODE_EXTENSION);
697-
let data = bytecode::encode(&module.name, data);
697+
let data = bytecode::encode(&module.llmod_id, data);
698698
if let Err(e) = fs::write(&dst, data) {
699699
diag_handler.err(&format!("failed to write bytecode: {}", e));
700700
}
@@ -1306,6 +1306,7 @@ fn execute_work_item(cgcx: &CodegenContext,
13061306
assert_eq!(bytecode_compressed.is_some(), config.emit_bc_compressed);
13071307

13081308
Ok(WorkItemResult::Compiled(CompiledModule {
1309+
llmod_id: module.llmod_id.clone(),
13091310
name: module_name,
13101311
kind: ModuleKind::Regular,
13111312
pre_existing: true,

src/librustc_codegen_llvm/base.rs

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -739,18 +739,15 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
739739
let link_meta = link::build_link_meta(crate_hash);
740740

741741
// Codegen the metadata.
742-
let metadata_cgu_name = CodegenUnit::build_cgu_name(tcx,
743-
LOCAL_CRATE,
744-
&["crate"],
745-
Some("metadata")).as_str()
746-
.to_string();
742+
let llmod_id = "metadata";
747743
let (metadata_llcx, metadata_llmod, metadata) =
748744
time(tcx.sess, "write metadata", || {
749-
write_metadata(tcx, &metadata_cgu_name, &link_meta)
745+
write_metadata(tcx, llmod_id, &link_meta)
750746
});
751747

752748
let metadata_module = ModuleCodegen {
753-
name: metadata_cgu_name,
749+
name: link::METADATA_MODULE_NAME.to_string(),
750+
llmod_id: llmod_id.to_string(),
754751
source: ModuleSource::Codegened(ModuleLlvm {
755752
llcx: metadata_llcx,
756753
llmod: metadata_llmod,
@@ -813,30 +810,26 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
813810

814811
// Codegen an allocator shim, if any
815812
let allocator_module = if let Some(kind) = *tcx.sess.allocator_kind.get() {
816-
let llmod_id = CodegenUnit::build_cgu_name(tcx,
817-
LOCAL_CRATE,
818-
&["crate"],
819-
Some("allocator")).as_str()
820-
.to_string();
821-
let (llcx, llmod) = unsafe {
822-
context::create_context_and_module(tcx.sess, &llmod_id)
823-
};
824-
let modules = ModuleLlvm {
825-
llmod,
826-
llcx,
827-
tm: create_target_machine(tcx.sess, false),
828-
};
829-
time(tcx.sess, "write allocator module", || {
830-
unsafe {
813+
unsafe {
814+
let llmod_id = "allocator";
815+
let (llcx, llmod) =
816+
context::create_context_and_module(tcx.sess, llmod_id);
817+
let modules = ModuleLlvm {
818+
llmod,
819+
llcx,
820+
tm: create_target_machine(tcx.sess, false),
821+
};
822+
time(tcx.sess, "write allocator module", || {
831823
allocator::codegen(tcx, &modules, kind)
832-
}
833-
});
824+
});
834825

835-
Some(ModuleCodegen {
836-
name: llmod_id,
837-
source: ModuleSource::Codegened(modules),
838-
kind: ModuleKind::Allocator,
839-
})
826+
Some(ModuleCodegen {
827+
name: link::ALLOCATOR_MODULE_NAME.to_string(),
828+
llmod_id: llmod_id.to_string(),
829+
source: ModuleSource::Codegened(modules),
830+
kind: ModuleKind::Allocator,
831+
})
832+
}
840833
} else {
841834
None
842835
};
@@ -879,10 +872,21 @@ pub fn codegen_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
879872
// succeed it means that none of the dependencies has changed
880873
// and we can safely re-use.
881874
if let Some(dep_node_index) = tcx.dep_graph.try_mark_green(tcx, dep_node) {
875+
// Append ".rs" to LLVM module identifier.
876+
//
877+
// LLVM code generator emits a ".file filename" directive
878+
// for ELF backends. Value of the "filename" is set as the
879+
// LLVM module identifier. Due to a LLVM MC bug[1], LLVM
880+
// crashes if the module identifier is same as other symbols
881+
// such as a function name in the module.
882+
// 1. http://llvm.org/bugs/show_bug.cgi?id=11479
883+
let llmod_id = format!("{}.rs", cgu.name());
884+
882885
let module = ModuleCodegen {
883886
name: cgu.name().to_string(),
884887
source: ModuleSource::Preexisting(buf),
885888
kind: ModuleKind::Regular,
889+
llmod_id,
886890
};
887891
tcx.dep_graph.mark_loaded_from_cache(dep_node_index, true);
888892
write::submit_codegened_module_to_llvm(tcx, module, 0);
@@ -1191,8 +1195,21 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11911195
{
11921196
let cgu_name = cgu.name().to_string();
11931197

1198+
// Append ".rs" to LLVM module identifier.
1199+
//
1200+
// LLVM code generator emits a ".file filename" directive
1201+
// for ELF backends. Value of the "filename" is set as the
1202+
// LLVM module identifier. Due to a LLVM MC bug[1], LLVM
1203+
// crashes if the module identifier is same as other symbols
1204+
// such as a function name in the module.
1205+
// 1. http://llvm.org/bugs/show_bug.cgi?id=11479
1206+
let llmod_id = format!("{}-{}.rs",
1207+
cgu.name(),
1208+
tcx.crate_disambiguator(LOCAL_CRATE)
1209+
.to_fingerprint().to_hex());
1210+
11941211
// Instantiate monomorphizations without filling out definitions yet...
1195-
let cx = CodegenCx::new(tcx, cgu);
1212+
let cx = CodegenCx::new(tcx, cgu, &llmod_id);
11961213
let module = {
11971214
let mono_items = cx.codegen_unit
11981215
.items_in_deterministic_order(cx.tcx);
@@ -1250,6 +1267,7 @@ fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12501267
name: cgu_name,
12511268
source: ModuleSource::Codegened(llvm_module),
12521269
kind: ModuleKind::Regular,
1270+
llmod_id,
12531271
}
12541272
};
12551273

src/librustc_codegen_llvm/context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ pub unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (Cont
214214

215215
impl<'a, 'tcx> CodegenCx<'a, 'tcx> {
216216
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>,
217-
codegen_unit: Arc<CodegenUnit<'tcx>>)
217+
codegen_unit: Arc<CodegenUnit<'tcx>>,
218+
llmod_id: &str)
218219
-> CodegenCx<'a, 'tcx> {
219220
// An interesting part of Windows which MSVC forces our hand on (and
220221
// apparently MinGW didn't) is the usage of `dllimport` and `dllexport`
@@ -267,7 +268,7 @@ impl<'a, 'tcx> CodegenCx<'a, 'tcx> {
267268

268269
unsafe {
269270
let (llcx, llmod) = create_context_and_module(&tcx.sess,
270-
&codegen_unit.name().as_str());
271+
&llmod_id[..]);
271272

272273
let dbg_cx = if tcx.sess.opts.debuginfo != NoDebugInfo {
273274
let dctx = debuginfo::CrateDebugContext::new(llmod);

src/librustc_codegen_llvm/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ struct ModuleCodegen {
258258
/// unique amongst **all** crates. Therefore, it should contain
259259
/// something unique to this crate (e.g., a module path) as well
260260
/// as the crate name and disambiguator.
261-
/// We currently generate these names via CodegenUnit::build_cgu_name().
262261
name: String,
262+
llmod_id: String,
263263
source: ModuleSource,
264264
kind: ModuleKind,
265265
}
@@ -306,6 +306,7 @@ impl ModuleCodegen {
306306
};
307307

308308
CompiledModule {
309+
llmod_id: self.llmod_id,
309310
name: self.name.clone(),
310311
kind: self.kind,
311312
pre_existing,
@@ -319,6 +320,7 @@ impl ModuleCodegen {
319320
#[derive(Debug)]
320321
struct CompiledModule {
321322
name: String,
323+
llmod_id: String,
322324
kind: ModuleKind,
323325
pre_existing: bool,
324326
object: Option<PathBuf>,

0 commit comments

Comments
 (0)