Skip to content

Commit 803ada7

Browse files
committed
Move LTO symbol export calculation from backends to cg_ssa
1 parent 8b8ffa8 commit 803ada7

File tree

3 files changed

+8
-87
lines changed

3 files changed

+8
-87
lines changed

messages.ftl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,4 @@ codegen_gcc_unwinding_inline_asm =
33
44
codegen_gcc_copy_bitcode = failed to copy bitcode to object file: {$err}
55
6-
codegen_gcc_dynamic_linking_with_lto =
7-
cannot prefer dynamic linking when performing LTO
8-
.note = only 'staticlib', 'bin', and 'cdylib' outputs are supported with LTO
9-
10-
codegen_gcc_lto_disallowed = lto can only be run for executables, cdylibs and static library outputs
11-
12-
codegen_gcc_lto_dylib = lto cannot be used for `dylib` crate type without `-Zdylib-lto`
13-
146
codegen_gcc_lto_bitcode_from_rlib = failed to get bitcode from object file for LTO ({$gcc_err})

src/back/lto.rs

Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,24 @@ use std::sync::Arc;
2424

2525
use gccjit::{Context, OutputKind};
2626
use object::read::archive::ArchiveFile;
27-
use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule, ThinShared};
28-
use rustc_codegen_ssa::back::symbol_export;
27+
use rustc_codegen_ssa::back::lto::{
28+
SerializedModule, ThinModule, ThinShared, exported_symbols_for_lto,
29+
};
2930
use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput};
3031
use rustc_codegen_ssa::traits::*;
3132
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind, looks_like_rust_object_file};
3233
use rustc_data_structures::memmap::Mmap;
3334
use rustc_errors::{DiagCtxtHandle, FatalError};
34-
use rustc_hir::def_id::LOCAL_CRATE;
3535
use rustc_middle::bug;
3636
use rustc_middle::dep_graph::WorkProduct;
37-
use rustc_middle::middle::exported_symbols::{SymbolExportInfo, SymbolExportLevel};
38-
use rustc_session::config::{CrateType, Lto};
37+
use rustc_session::config::Lto;
3938
use rustc_target::spec::RelocModel;
4039
use tempfile::{TempDir, tempdir};
4140

4241
use crate::back::write::save_temp_bitcode;
43-
use crate::errors::{DynamicLinkingWithLTO, LtoBitcodeFromRlib, LtoDisallowed, LtoDylib};
42+
use crate::errors::LtoBitcodeFromRlib;
4443
use crate::{GccCodegenBackend, GccContext, SyncContext, to_gcc_opt_level};
4544

46-
pub fn crate_type_allows_lto(crate_type: CrateType) -> bool {
47-
match crate_type {
48-
CrateType::Executable
49-
| CrateType::Dylib
50-
| CrateType::Staticlib
51-
| CrateType::Cdylib
52-
| CrateType::Sdylib => true,
53-
CrateType::Rlib | CrateType::ProcMacro => false,
54-
}
55-
}
56-
5745
struct LtoData {
5846
// TODO(antoyo): use symbols_below_threshold.
5947
//symbols_below_threshold: Vec<String>,
@@ -65,15 +53,8 @@ fn prepare_lto(
6553
cgcx: &CodegenContext<GccCodegenBackend>,
6654
dcx: DiagCtxtHandle<'_>,
6755
) -> Result<LtoData, FatalError> {
68-
let export_threshold = match cgcx.lto {
69-
// We're just doing LTO for our one crate
70-
Lto::ThinLocal => SymbolExportLevel::Rust,
71-
72-
// We're doing LTO for the entire crate graph
73-
Lto::Fat | Lto::Thin => symbol_export::crates_export_threshold(&cgcx.crate_types),
74-
75-
Lto::No => panic!("didn't request LTO but we're doing LTO"),
76-
};
56+
// FIXME(bjorn3): Limit LTO exports to these symbols
57+
let _symbols_below_threshold = exported_symbols_for_lto(cgcx, dcx)?;
7758

7859
let tmp_path = match tempdir() {
7960
Ok(tmp_path) => tmp_path,
@@ -83,20 +64,6 @@ fn prepare_lto(
8364
}
8465
};
8566

86-
let symbol_filter = &|&(ref name, info): &(String, SymbolExportInfo)| {
87-
if info.level.is_below_threshold(export_threshold) || info.used {
88-
Some(name.clone())
89-
} else {
90-
None
91-
}
92-
};
93-
let exported_symbols = cgcx.exported_symbols.as_ref().expect("needs exported symbols for LTO");
94-
let mut symbols_below_threshold = {
95-
let _timer = cgcx.prof.generic_activity("GCC_lto_generate_symbols_below_threshold");
96-
exported_symbols[&LOCAL_CRATE].iter().filter_map(symbol_filter).collect::<Vec<String>>()
97-
};
98-
info!("{} symbols to preserve in this crate", symbols_below_threshold.len());
99-
10067
// If we're performing LTO for the entire crate graph, then for each of our
10168
// upstream dependencies, find the corresponding rlib and load the bitcode
10269
// from the archive.
@@ -105,32 +72,7 @@ fn prepare_lto(
10572
// with either fat or thin LTO
10673
let mut upstream_modules = Vec::new();
10774
if cgcx.lto != Lto::ThinLocal {
108-
// Make sure we actually can run LTO
109-
for crate_type in cgcx.crate_types.iter() {
110-
if !crate_type_allows_lto(*crate_type) {
111-
dcx.emit_err(LtoDisallowed);
112-
return Err(FatalError);
113-
}
114-
if *crate_type == CrateType::Dylib && !cgcx.opts.unstable_opts.dylib_lto {
115-
dcx.emit_err(LtoDylib);
116-
return Err(FatalError);
117-
}
118-
}
119-
120-
if cgcx.opts.cg.prefer_dynamic && !cgcx.opts.unstable_opts.dylib_lto {
121-
dcx.emit_err(DynamicLinkingWithLTO);
122-
return Err(FatalError);
123-
}
124-
125-
for &(cnum, ref path) in cgcx.each_linked_rlib_for_lto.iter() {
126-
let exported_symbols =
127-
cgcx.exported_symbols.as_ref().expect("needs exported symbols for LTO");
128-
{
129-
let _timer = cgcx.prof.generic_activity("GCC_lto_generate_symbols_below_threshold");
130-
symbols_below_threshold
131-
.extend(exported_symbols[&cnum].iter().filter_map(symbol_filter));
132-
}
133-
75+
for &(_cnum, ref path) in cgcx.each_linked_rlib_for_lto.iter() {
13476
let archive_data = unsafe {
13577
Mmap::map(File::open(path).expect("couldn't open rlib")).expect("couldn't map rlib")
13678
};

src/errors.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,6 @@ pub(crate) struct CopyBitcode {
1414
pub err: std::io::Error,
1515
}
1616

17-
#[derive(Diagnostic)]
18-
#[diag(codegen_gcc_dynamic_linking_with_lto)]
19-
#[note]
20-
pub(crate) struct DynamicLinkingWithLTO;
21-
22-
#[derive(Diagnostic)]
23-
#[diag(codegen_gcc_lto_disallowed)]
24-
pub(crate) struct LtoDisallowed;
25-
26-
#[derive(Diagnostic)]
27-
#[diag(codegen_gcc_lto_dylib)]
28-
pub(crate) struct LtoDylib;
29-
3017
#[derive(Diagnostic)]
3118
#[diag(codegen_gcc_lto_bitcode_from_rlib)]
3219
pub(crate) struct LtoBitcodeFromRlib {

0 commit comments

Comments
 (0)