Skip to content

Commit ee621f4

Browse files
committed
Auto merge of #59950 - Centril:rollup-hpmr62i, r=Centril
Rollup of 6 pull requests Successful merges: - #59776 (Apply resource-suffix to search-index and source-files scripts as well) - #59784 (Suggest importing macros from the crate root) - #59812 (Exclude profiler-generated symbols from MSVC __imp_-symbol workaround.) - #59874 (Clean up handling of `-Z pgo-gen` commandline option.) - #59890 (Don't generate empty json variables) - #59911 (Revert "compile crates under test w/ -Zemit-stack-sizes") Failed merges: r? @ghost
2 parents 0085672 + a6b8097 commit ee621f4

File tree

22 files changed

+880
-198
lines changed

22 files changed

+880
-198
lines changed

src/bootstrap/bin/rustc.rs

-27
Original file line numberDiff line numberDiff line change
@@ -187,33 +187,6 @@ fn main() {
187187
cmd.arg("-C").arg(format!("debug-assertions={}", debug_assertions));
188188
}
189189

190-
// Build all crates in the `std` facade with `-Z emit-stack-sizes` to add stack usage
191-
// information.
192-
//
193-
// When you use this `-Z` flag with Cargo you get stack usage information on all crates
194-
// compiled from source, and when you are using LTO you also get information on pre-compiled
195-
// crates like `core` and `std`, even if they were not compiled with `-Z emit-stack-sizes`.
196-
// However, there's an exception: `compiler_builtins`. This crate is special and doesn't
197-
// participate in LTO because it's always linked as a separate object file. For this reason
198-
// it's impossible to get stack usage information about `compiler-builtins` using
199-
// `RUSTFLAGS` + Cargo, or `cargo rustc`.
200-
//
201-
// To make the stack usage information of all crates under the `std` facade available to
202-
// Cargo based stack usage analysis tools, in both LTO and non-LTO mode, we compile them
203-
// with the `-Z emit-stack-sizes` flag. The `RUSTC_EMIT_STACK_SIZES` var helps us apply this
204-
// flag only to the crates in the `std` facade. The `-Z` flag is known to currently work
205-
// with targets that produce ELF files so we limit its use flag to those targets.
206-
//
207-
// NOTE(japaric) if this ever causes problem with an LLVM upgrade or any PR feel free to
208-
// remove it or comment it out
209-
if env::var_os("RUSTC_EMIT_STACK_SIZES").is_some()
210-
&& (target.contains("-linux-")
211-
|| target.contains("-none-eabi")
212-
|| target.ends_with("-none-elf"))
213-
{
214-
cmd.arg("-Zemit-stack-sizes");
215-
}
216-
217190
if let Ok(s) = env::var("RUSTC_CODEGEN_UNITS") {
218191
cmd.arg("-C").arg(format!("codegen-units={}", s));
219192
}

src/bootstrap/compile.rs

-4
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ impl Step for Std {
9797
let _folder = builder.fold_output(|| format!("stage{}-std", compiler.stage));
9898
builder.info(&format!("Building stage{} std artifacts ({} -> {})", compiler.stage,
9999
&compiler.host, target));
100-
// compile with `-Z emit-stack-sizes`; see bootstrap/src/rustc.rs for more details
101-
cargo.env("RUSTC_EMIT_STACK_SIZES", "1");
102100
run_cargo(builder,
103101
&mut cargo,
104102
&libstd_stamp(builder, compiler, target),
@@ -397,8 +395,6 @@ impl Step for Test {
397395
let _folder = builder.fold_output(|| format!("stage{}-test", compiler.stage));
398396
builder.info(&format!("Building stage{} test artifacts ({} -> {})", compiler.stage,
399397
&compiler.host, target));
400-
// compile with `-Z emit-stack-sizes`; see bootstrap/src/rustc.rs for more details
401-
cargo.env("RUSTC_EMIT_STACK_SIZES", "1");
402398
run_cargo(builder,
403399
&mut cargo,
404400
&libtest_stamp(builder, compiler, target),

src/librustc/session/config.rs

+32-6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ impl LinkerPluginLto {
113113
}
114114
}
115115

116+
#[derive(Clone, PartialEq, Hash)]
117+
pub enum PgoGenerate {
118+
Enabled(Option<PathBuf>),
119+
Disabled,
120+
}
121+
122+
impl PgoGenerate {
123+
pub fn enabled(&self) -> bool {
124+
match *self {
125+
PgoGenerate::Enabled(_) => true,
126+
PgoGenerate::Disabled => false,
127+
}
128+
}
129+
}
130+
116131
#[derive(Clone, Copy, PartialEq, Hash)]
117132
pub enum DebugInfo {
118133
None,
@@ -826,13 +841,15 @@ macro_rules! options {
826841
pub const parse_linker_plugin_lto: Option<&str> =
827842
Some("either a boolean (`yes`, `no`, `on`, `off`, etc), \
828843
or the path to the linker plugin");
844+
pub const parse_pgo_generate: Option<&str> =
845+
Some("an optional path to the profiling data output directory");
829846
pub const parse_merge_functions: Option<&str> =
830847
Some("one of: `disabled`, `trampolines`, or `aliases`");
831848
}
832849

833850
#[allow(dead_code)]
834851
mod $mod_set {
835-
use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto};
852+
use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto, PgoGenerate};
836853
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
837854
use std::path::PathBuf;
838855
use std::str::FromStr;
@@ -1087,6 +1104,14 @@ macro_rules! options {
10871104
true
10881105
}
10891106

1107+
fn parse_pgo_generate(slot: &mut PgoGenerate, v: Option<&str>) -> bool {
1108+
*slot = match v {
1109+
None => PgoGenerate::Enabled(None),
1110+
Some(path) => PgoGenerate::Enabled(Some(PathBuf::from(path))),
1111+
};
1112+
true
1113+
}
1114+
10901115
fn parse_merge_functions(slot: &mut Option<MergeFunctions>, v: Option<&str>) -> bool {
10911116
match v.and_then(|s| MergeFunctions::from_str(s).ok()) {
10921117
Some(mergefunc) => *slot = Some(mergefunc),
@@ -1363,7 +1388,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
13631388
"extra arguments to prepend to the linker invocation (space separated)"),
13641389
profile: bool = (false, parse_bool, [TRACKED],
13651390
"insert profiling code"),
1366-
pgo_gen: Option<String> = (None, parse_opt_string, [TRACKED],
1391+
pgo_gen: PgoGenerate = (PgoGenerate::Disabled, parse_pgo_generate, [TRACKED],
13671392
"Generate PGO profile data, to a given file, or to the default location if it's empty."),
13681393
pgo_use: String = (String::new(), parse_string, [TRACKED],
13691394
"Use PGO profile data from the given profile file."),
@@ -1980,7 +2005,7 @@ pub fn build_session_options_and_crate_config(
19802005
);
19812006
}
19822007

1983-
if debugging_opts.pgo_gen.is_some() && !debugging_opts.pgo_use.is_empty() {
2008+
if debugging_opts.pgo_gen.enabled() && !debugging_opts.pgo_use.is_empty() {
19842009
early_error(
19852010
error_format,
19862011
"options `-Z pgo-gen` and `-Z pgo-use` are exclusive",
@@ -2490,7 +2515,7 @@ mod dep_tracking {
24902515
use std::path::PathBuf;
24912516
use std::collections::hash_map::DefaultHasher;
24922517
use super::{CrateType, DebugInfo, ErrorOutputType, OptLevel, OutputTypes,
2493-
Passes, Sanitizer, LtoCli, LinkerPluginLto};
2518+
Passes, Sanitizer, LtoCli, LinkerPluginLto, PgoGenerate};
24942519
use syntax::feature_gate::UnstableFeatures;
24952520
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel, TargetTriple};
24962521
use syntax::edition::Edition;
@@ -2558,6 +2583,7 @@ mod dep_tracking {
25582583
impl_dep_tracking_hash_via_hash!(TargetTriple);
25592584
impl_dep_tracking_hash_via_hash!(Edition);
25602585
impl_dep_tracking_hash_via_hash!(LinkerPluginLto);
2586+
impl_dep_tracking_hash_via_hash!(PgoGenerate);
25612587

25622588
impl_dep_tracking_hash_for_sortable_vec_of!(String);
25632589
impl_dep_tracking_hash_for_sortable_vec_of!(PathBuf);
@@ -2625,7 +2651,7 @@ mod tests {
26252651
build_session_options_and_crate_config,
26262652
to_crate_config
26272653
};
2628-
use crate::session::config::{LtoCli, LinkerPluginLto};
2654+
use crate::session::config::{LtoCli, LinkerPluginLto, PgoGenerate};
26292655
use crate::session::build_session;
26302656
use crate::session::search_paths::SearchPath;
26312657
use std::collections::{BTreeMap, BTreeSet};
@@ -3124,7 +3150,7 @@ mod tests {
31243150
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
31253151

31263152
opts = reference.clone();
3127-
opts.debugging_opts.pgo_gen = Some(String::from("abc"));
3153+
opts.debugging_opts.pgo_gen = PgoGenerate::Enabled(None);
31283154
assert_ne!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
31293155

31303156
opts = reference.clone();

src/librustc_codegen_llvm/attributes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub fn set_probestack(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
104104
}
105105

106106
// probestack doesn't play nice either with pgo-gen.
107-
if cx.sess().opts.debugging_opts.pgo_gen.is_some() {
107+
if cx.sess().opts.debugging_opts.pgo_gen.enabled() {
108108
return;
109109
}
110110

src/librustc_codegen_llvm/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ fn link_args(cmd: &mut dyn Linker,
10141014
cmd.build_static_executable();
10151015
}
10161016

1017-
if sess.opts.debugging_opts.pgo_gen.is_some() {
1017+
if sess.opts.debugging_opts.pgo_gen.enabled() {
10181018
cmd.pgo_gen();
10191019
}
10201020

src/librustc_codegen_llvm/back/write.rs

+33-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::LlvmCodegenBackend;
1313
use rustc::hir::def_id::LOCAL_CRATE;
1414
use rustc_codegen_ssa::back::write::{CodegenContext, ModuleConfig, run_assembler};
1515
use rustc_codegen_ssa::traits::*;
16-
use rustc::session::config::{self, OutputType, Passes, Lto};
16+
use rustc::session::config::{self, OutputType, Passes, Lto, PgoGenerate};
1717
use rustc::session::Session;
1818
use rustc::ty::TyCtxt;
1919
use rustc_codegen_ssa::{ModuleCodegen, CompiledModule};
@@ -25,7 +25,7 @@ use errors::{Handler, FatalError};
2525
use std::ffi::{CString, CStr};
2626
use std::fs;
2727
use std::io::{self, Write};
28-
use std::path::Path;
28+
use std::path::{Path, PathBuf};
2929
use std::str;
3030
use std::sync::Arc;
3131
use std::slice;
@@ -706,10 +706,20 @@ pub unsafe fn with_llvm_pmb(llmod: &llvm::Module,
706706
.unwrap_or(llvm::CodeGenOptSizeNone);
707707
let inline_threshold = config.inline_threshold;
708708

709-
let pgo_gen_path = config.pgo_gen.as_ref().map(|s| {
710-
let s = if s.is_empty() { "default_%m.profraw" } else { s };
711-
CString::new(s.as_bytes()).unwrap()
712-
});
709+
let pgo_gen_path = match config.pgo_gen {
710+
PgoGenerate::Enabled(ref opt_dir_path) => {
711+
let path = if let Some(dir_path) = opt_dir_path {
712+
dir_path.join("default_%m.profraw")
713+
} else {
714+
PathBuf::from("default_%m.profraw")
715+
};
716+
717+
Some(CString::new(format!("{}", path.display())).unwrap())
718+
}
719+
PgoGenerate::Disabled => {
720+
None
721+
}
722+
};
713723

714724
let pgo_use_path = if config.pgo_use.is_empty() {
715725
None
@@ -793,21 +803,31 @@ fn create_msvc_imps(
793803
} else {
794804
"\x01__imp_"
795805
};
806+
796807
unsafe {
797808
let i8p_ty = Type::i8p_llcx(llcx);
798809
let globals = base::iter_globals(llmod)
799810
.filter(|&val| {
800811
llvm::LLVMRustGetLinkage(val) == llvm::Linkage::ExternalLinkage &&
801812
llvm::LLVMIsDeclaration(val) == 0
802813
})
803-
.map(move |val| {
814+
.filter_map(|val| {
815+
// Exclude some symbols that we know are not Rust symbols.
804816
let name = CStr::from_ptr(llvm::LLVMGetValueName(val));
817+
if ignored(name.to_bytes()) {
818+
None
819+
} else {
820+
Some((val, name))
821+
}
822+
})
823+
.map(move |(val, name)| {
805824
let mut imp_name = prefix.as_bytes().to_vec();
806825
imp_name.extend(name.to_bytes());
807826
let imp_name = CString::new(imp_name).unwrap();
808827
(imp_name, val)
809828
})
810829
.collect::<Vec<_>>();
830+
811831
for (imp_name, val) in globals {
812832
let imp = llvm::LLVMAddGlobal(llmod,
813833
i8p_ty,
@@ -816,4 +836,10 @@ fn create_msvc_imps(
816836
llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage);
817837
}
818838
}
839+
840+
// Use this function to exclude certain symbols from `__imp` generation.
841+
fn ignored(symbol_name: &[u8]) -> bool {
842+
// These are symbols generated by LLVM's profiling instrumentation
843+
symbol_name.starts_with(b"__llvm_profile_")
844+
}
819845
}

src/librustc_codegen_ssa/back/symbol_export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn exported_symbols_provider_local<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
209209
}
210210
}
211211

212-
if tcx.sess.opts.debugging_opts.pgo_gen.is_some() {
212+
if tcx.sess.opts.debugging_opts.pgo_gen.enabled() {
213213
// These are weak symbols that point to the profile version and the
214214
// profile name, which need to be treated as exported so LTO doesn't nix
215215
// them.

src/librustc_codegen_ssa/back/write.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use rustc_incremental::{copy_cgu_workproducts_to_incr_comp_cache_dir,
1212
use rustc::dep_graph::{WorkProduct, WorkProductId, WorkProductFileKind};
1313
use rustc::dep_graph::cgu_reuse_tracker::CguReuseTracker;
1414
use rustc::middle::cstore::EncodedMetadata;
15-
use rustc::session::config::{self, OutputFilenames, OutputType, Passes, Sanitizer, Lto};
15+
use rustc::session::config::{self, OutputFilenames, OutputType, Passes, Lto,
16+
Sanitizer, PgoGenerate};
1617
use rustc::session::Session;
1718
use rustc::util::nodemap::FxHashMap;
1819
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
@@ -55,7 +56,7 @@ pub struct ModuleConfig {
5556
/// Some(level) to optimize binary size, or None to not affect program size.
5657
pub opt_size: Option<config::OptLevel>,
5758

58-
pub pgo_gen: Option<String>,
59+
pub pgo_gen: PgoGenerate,
5960
pub pgo_use: String,
6061

6162
// Flags indicating which outputs to produce.
@@ -93,7 +94,7 @@ impl ModuleConfig {
9394
opt_level: None,
9495
opt_size: None,
9596

96-
pgo_gen: None,
97+
pgo_gen: PgoGenerate::Disabled,
9798
pgo_use: String::new(),
9899

99100
emit_no_opt_bc: false,

src/librustc_metadata/creader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ impl<'a> CrateLoader<'a> {
862862

863863
fn inject_profiler_runtime(&mut self) {
864864
if self.sess.opts.debugging_opts.profile ||
865-
self.sess.opts.debugging_opts.pgo_gen.is_some()
865+
self.sess.opts.debugging_opts.pgo_gen.enabled()
866866
{
867867
info!("loading profiler");
868868

0 commit comments

Comments
 (0)