Skip to content

Commit 431e0ab

Browse files
committed
Auto merge of #55871 - ljedrz:llvm_back_allocations, r=nagisa
codegen_llvm_back: improve allocations This commit was split out from #54864. Last time it was causing an LLVM OOM, which was most probably caused by not collecting the globals. - preallocate vectors of known length - `extend` instead of `append` where the argument is consumable - turn 2 `push` loops into `extend`s - create a vector from a function producing one instead of using `extend_from_slice` on it - consume `modules` when no longer needed - ~~return an `impl Iterator` from `generate_lto_work`~~ - ~~don't `collect` `globals`, as they are iterated over and consumed right afterwards~~ While I'm hoping it won't cause an OOM anymore, I would still consider this a "high-risk" PR and not roll it up.
2 parents 58e9832 + 2043d30 commit 431e0ab

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

Diff for: src/librustc_codegen_llvm/back/link.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ pub(crate) fn link_binary(sess: &Session,
7272
bug!("invalid output type `{:?}` for target os `{}`",
7373
crate_type, sess.opts.target_triple);
7474
}
75-
let mut out_files = link_binary_output(sess,
76-
codegen_results,
77-
crate_type,
78-
outputs,
79-
crate_name);
80-
out_filenames.append(&mut out_files);
75+
let out_files = link_binary_output(sess,
76+
codegen_results,
77+
crate_type,
78+
outputs,
79+
crate_name);
80+
out_filenames.extend(out_files);
8181
}
8282

8383
// Remove the temporary object file and metadata if we aren't saving temps

Diff for: src/librustc_codegen_llvm/back/lto.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,12 @@ fn fat_lto(cgcx: &CodegenContext<LlvmCodegenBackend>,
225225
// and we want to move everything to the same LLVM context. Currently the
226226
// way we know of to do that is to serialize them to a string and them parse
227227
// them later. Not great but hey, that's why it's "fat" LTO, right?
228-
for module in modules {
228+
serialized_modules.extend(modules.into_iter().map(|module| {
229229
let buffer = ModuleBuffer::new(module.module_llvm.llmod());
230230
let llmod_id = CString::new(&module.name[..]).unwrap();
231-
serialized_modules.push((SerializedModule::Local(buffer), llmod_id));
232-
}
231+
232+
(SerializedModule::Local(buffer), llmod_id)
233+
}));
233234

234235
// For all serialized bitcode files we parse them and link them in as we did
235236
// above, this is all mostly handled in C++. Like above, though, we don't
@@ -349,9 +350,10 @@ fn thin_lto(cgcx: &CodegenContext<LlvmCodegenBackend>,
349350
.map(|&(_, ref wp)| (wp.cgu_name.clone(), wp.clone()))
350351
.collect();
351352

352-
let mut thin_buffers = Vec::new();
353-
let mut module_names = Vec::new();
354-
let mut thin_modules = Vec::new();
353+
let full_scope_len = modules.len() + serialized_modules.len() + cached_modules.len();
354+
let mut thin_buffers = Vec::with_capacity(modules.len());
355+
let mut module_names = Vec::with_capacity(full_scope_len);
356+
let mut thin_modules = Vec::with_capacity(full_scope_len);
355357

356358
// FIXME: right now, like with fat LTO, we serialize all in-memory
357359
// modules before working with them and ThinLTO. We really
@@ -360,7 +362,7 @@ fn thin_lto(cgcx: &CodegenContext<LlvmCodegenBackend>,
360362
// into the global index. It turns out that this loop is by far
361363
// the most expensive portion of this small bit of global
362364
// analysis!
363-
for (i, module) in modules.iter().enumerate() {
365+
for (i, module) in modules.into_iter().enumerate() {
364366
info!("local module: {} - {}", i, module.name);
365367
let name = CString::new(module.name.clone()).unwrap();
366368
let buffer = ThinBuffer::new(module.module_llvm.llmod());
@@ -406,7 +408,7 @@ fn thin_lto(cgcx: &CodegenContext<LlvmCodegenBackend>,
406408
// incremental ThinLTO first where we could actually avoid
407409
// looking at upstream modules entirely sometimes (the contents,
408410
// we must always unconditionally look at the index).
409-
let mut serialized = Vec::new();
411+
let mut serialized = Vec::with_capacity(serialized_modules.len() + cached_modules.len());
410412

411413
let cached_modules = cached_modules.into_iter().map(|(sm, wp)| {
412414
(sm, CString::new(wp.cgu_name).unwrap())

Diff for: src/librustc_codegen_llvm/back/rpath.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@ pub fn get_rpath_flags(config: &mut RPathConfig) -> Vec<String> {
3131
return Vec::new();
3232
}
3333

34-
let mut flags = Vec::new();
35-
3634
debug!("preparing the RPATH!");
3735

3836
let libs = config.used_crates.clone();
3937
let libs = libs.iter().filter_map(|&(_, ref l)| l.option()).collect::<Vec<_>>();
4038
let rpaths = get_rpaths(config, &libs);
41-
flags.extend_from_slice(&rpaths_to_flags(&rpaths));
39+
let mut flags = rpaths_to_flags(&rpaths);
4240

4341
// Use DT_RUNPATH instead of DT_RPATH if available
4442
if config.linker_is_gnu {
@@ -49,7 +47,8 @@ pub fn get_rpath_flags(config: &mut RPathConfig) -> Vec<String> {
4947
}
5048

5149
fn rpaths_to_flags(rpaths: &[String]) -> Vec<String> {
52-
let mut ret = Vec::new();
50+
let mut ret = Vec::with_capacity(rpaths.len()); // the minimum needed capacity
51+
5352
for rpath in rpaths {
5453
if rpath.contains(',') {
5554
ret.push("-Wl,-rpath".into());

Diff for: src/librustc_codegen_ssa/back/symbol_export.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,11 @@ fn exported_symbols_provider_local<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
229229
"__llvm_profile_raw_version",
230230
"__llvm_profile_filename",
231231
];
232-
for sym in &PROFILER_WEAK_SYMBOLS {
232+
233+
symbols.extend(PROFILER_WEAK_SYMBOLS.iter().map(|sym| {
233234
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(sym));
234-
symbols.push((exported_symbol, SymbolExportLevel::C));
235-
}
235+
(exported_symbol, SymbolExportLevel::C)
236+
}));
236237
}
237238

238239
if tcx.sess.crate_types.borrow().contains(&config::CrateType::Dylib) {

0 commit comments

Comments
 (0)