Skip to content

Commit

Permalink
Auto merge of #59950 - Centril:rollup-hpmr62i, r=Centril
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Apr 14, 2019
2 parents 0085672 + a6b8097 commit ee621f4
Show file tree
Hide file tree
Showing 22 changed files with 880 additions and 198 deletions.
27 changes: 0 additions & 27 deletions src/bootstrap/bin/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,33 +187,6 @@ fn main() {
cmd.arg("-C").arg(format!("debug-assertions={}", debug_assertions));
}

// Build all crates in the `std` facade with `-Z emit-stack-sizes` to add stack usage
// information.
//
// When you use this `-Z` flag with Cargo you get stack usage information on all crates
// compiled from source, and when you are using LTO you also get information on pre-compiled
// crates like `core` and `std`, even if they were not compiled with `-Z emit-stack-sizes`.
// However, there's an exception: `compiler_builtins`. This crate is special and doesn't
// participate in LTO because it's always linked as a separate object file. For this reason
// it's impossible to get stack usage information about `compiler-builtins` using
// `RUSTFLAGS` + Cargo, or `cargo rustc`.
//
// To make the stack usage information of all crates under the `std` facade available to
// Cargo based stack usage analysis tools, in both LTO and non-LTO mode, we compile them
// with the `-Z emit-stack-sizes` flag. The `RUSTC_EMIT_STACK_SIZES` var helps us apply this
// flag only to the crates in the `std` facade. The `-Z` flag is known to currently work
// with targets that produce ELF files so we limit its use flag to those targets.
//
// NOTE(japaric) if this ever causes problem with an LLVM upgrade or any PR feel free to
// remove it or comment it out
if env::var_os("RUSTC_EMIT_STACK_SIZES").is_some()
&& (target.contains("-linux-")
|| target.contains("-none-eabi")
|| target.ends_with("-none-elf"))
{
cmd.arg("-Zemit-stack-sizes");
}

if let Ok(s) = env::var("RUSTC_CODEGEN_UNITS") {
cmd.arg("-C").arg(format!("codegen-units={}", s));
}
Expand Down
4 changes: 0 additions & 4 deletions src/bootstrap/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ impl Step for Std {
let _folder = builder.fold_output(|| format!("stage{}-std", compiler.stage));
builder.info(&format!("Building stage{} std artifacts ({} -> {})", compiler.stage,
&compiler.host, target));
// compile with `-Z emit-stack-sizes`; see bootstrap/src/rustc.rs for more details
cargo.env("RUSTC_EMIT_STACK_SIZES", "1");
run_cargo(builder,
&mut cargo,
&libstd_stamp(builder, compiler, target),
Expand Down Expand Up @@ -397,8 +395,6 @@ impl Step for Test {
let _folder = builder.fold_output(|| format!("stage{}-test", compiler.stage));
builder.info(&format!("Building stage{} test artifacts ({} -> {})", compiler.stage,
&compiler.host, target));
// compile with `-Z emit-stack-sizes`; see bootstrap/src/rustc.rs for more details
cargo.env("RUSTC_EMIT_STACK_SIZES", "1");
run_cargo(builder,
&mut cargo,
&libtest_stamp(builder, compiler, target),
Expand Down
38 changes: 32 additions & 6 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ impl LinkerPluginLto {
}
}

#[derive(Clone, PartialEq, Hash)]
pub enum PgoGenerate {
Enabled(Option<PathBuf>),
Disabled,
}

impl PgoGenerate {
pub fn enabled(&self) -> bool {
match *self {
PgoGenerate::Enabled(_) => true,
PgoGenerate::Disabled => false,
}
}
}

#[derive(Clone, Copy, PartialEq, Hash)]
pub enum DebugInfo {
None,
Expand Down Expand Up @@ -826,13 +841,15 @@ macro_rules! options {
pub const parse_linker_plugin_lto: Option<&str> =
Some("either a boolean (`yes`, `no`, `on`, `off`, etc), \
or the path to the linker plugin");
pub const parse_pgo_generate: Option<&str> =
Some("an optional path to the profiling data output directory");
pub const parse_merge_functions: Option<&str> =
Some("one of: `disabled`, `trampolines`, or `aliases`");
}

#[allow(dead_code)]
mod $mod_set {
use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto};
use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto, PgoGenerate};
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -1087,6 +1104,14 @@ macro_rules! options {
true
}

fn parse_pgo_generate(slot: &mut PgoGenerate, v: Option<&str>) -> bool {
*slot = match v {
None => PgoGenerate::Enabled(None),
Some(path) => PgoGenerate::Enabled(Some(PathBuf::from(path))),
};
true
}

fn parse_merge_functions(slot: &mut Option<MergeFunctions>, v: Option<&str>) -> bool {
match v.and_then(|s| MergeFunctions::from_str(s).ok()) {
Some(mergefunc) => *slot = Some(mergefunc),
Expand Down Expand Up @@ -1363,7 +1388,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"extra arguments to prepend to the linker invocation (space separated)"),
profile: bool = (false, parse_bool, [TRACKED],
"insert profiling code"),
pgo_gen: Option<String> = (None, parse_opt_string, [TRACKED],
pgo_gen: PgoGenerate = (PgoGenerate::Disabled, parse_pgo_generate, [TRACKED],
"Generate PGO profile data, to a given file, or to the default location if it's empty."),
pgo_use: String = (String::new(), parse_string, [TRACKED],
"Use PGO profile data from the given profile file."),
Expand Down Expand Up @@ -1980,7 +2005,7 @@ pub fn build_session_options_and_crate_config(
);
}

if debugging_opts.pgo_gen.is_some() && !debugging_opts.pgo_use.is_empty() {
if debugging_opts.pgo_gen.enabled() && !debugging_opts.pgo_use.is_empty() {
early_error(
error_format,
"options `-Z pgo-gen` and `-Z pgo-use` are exclusive",
Expand Down Expand Up @@ -2490,7 +2515,7 @@ mod dep_tracking {
use std::path::PathBuf;
use std::collections::hash_map::DefaultHasher;
use super::{CrateType, DebugInfo, ErrorOutputType, OptLevel, OutputTypes,
Passes, Sanitizer, LtoCli, LinkerPluginLto};
Passes, Sanitizer, LtoCli, LinkerPluginLto, PgoGenerate};
use syntax::feature_gate::UnstableFeatures;
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel, TargetTriple};
use syntax::edition::Edition;
Expand Down Expand Up @@ -2558,6 +2583,7 @@ mod dep_tracking {
impl_dep_tracking_hash_via_hash!(TargetTriple);
impl_dep_tracking_hash_via_hash!(Edition);
impl_dep_tracking_hash_via_hash!(LinkerPluginLto);
impl_dep_tracking_hash_via_hash!(PgoGenerate);

impl_dep_tracking_hash_for_sortable_vec_of!(String);
impl_dep_tracking_hash_for_sortable_vec_of!(PathBuf);
Expand Down Expand Up @@ -2625,7 +2651,7 @@ mod tests {
build_session_options_and_crate_config,
to_crate_config
};
use crate::session::config::{LtoCli, LinkerPluginLto};
use crate::session::config::{LtoCli, LinkerPluginLto, PgoGenerate};
use crate::session::build_session;
use crate::session::search_paths::SearchPath;
use std::collections::{BTreeMap, BTreeSet};
Expand Down Expand Up @@ -3124,7 +3150,7 @@ mod tests {
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());

opts = reference.clone();
opts.debugging_opts.pgo_gen = Some(String::from("abc"));
opts.debugging_opts.pgo_gen = PgoGenerate::Enabled(None);
assert_ne!(reference.dep_tracking_hash(), opts.dep_tracking_hash());

opts = reference.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn set_probestack(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ fn link_args(cmd: &mut dyn Linker,
cmd.build_static_executable();
}

if sess.opts.debugging_opts.pgo_gen.is_some() {
if sess.opts.debugging_opts.pgo_gen.enabled() {
cmd.pgo_gen();
}

Expand Down
40 changes: 33 additions & 7 deletions src/librustc_codegen_llvm/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::LlvmCodegenBackend;
use rustc::hir::def_id::LOCAL_CRATE;
use rustc_codegen_ssa::back::write::{CodegenContext, ModuleConfig, run_assembler};
use rustc_codegen_ssa::traits::*;
use rustc::session::config::{self, OutputType, Passes, Lto};
use rustc::session::config::{self, OutputType, Passes, Lto, PgoGenerate};
use rustc::session::Session;
use rustc::ty::TyCtxt;
use rustc_codegen_ssa::{ModuleCodegen, CompiledModule};
Expand All @@ -25,7 +25,7 @@ use errors::{Handler, FatalError};
use std::ffi::{CString, CStr};
use std::fs;
use std::io::{self, Write};
use std::path::Path;
use std::path::{Path, PathBuf};
use std::str;
use std::sync::Arc;
use std::slice;
Expand Down Expand Up @@ -706,10 +706,20 @@ pub unsafe fn with_llvm_pmb(llmod: &llvm::Module,
.unwrap_or(llvm::CodeGenOptSizeNone);
let inline_threshold = config.inline_threshold;

let pgo_gen_path = config.pgo_gen.as_ref().map(|s| {
let s = if s.is_empty() { "default_%m.profraw" } else { s };
CString::new(s.as_bytes()).unwrap()
});
let pgo_gen_path = match config.pgo_gen {
PgoGenerate::Enabled(ref opt_dir_path) => {
let path = if let Some(dir_path) = opt_dir_path {
dir_path.join("default_%m.profraw")
} else {
PathBuf::from("default_%m.profraw")
};

Some(CString::new(format!("{}", path.display())).unwrap())
}
PgoGenerate::Disabled => {
None
}
};

let pgo_use_path = if config.pgo_use.is_empty() {
None
Expand Down Expand Up @@ -793,21 +803,31 @@ fn create_msvc_imps(
} else {
"\x01__imp_"
};

unsafe {
let i8p_ty = Type::i8p_llcx(llcx);
let globals = base::iter_globals(llmod)
.filter(|&val| {
llvm::LLVMRustGetLinkage(val) == llvm::Linkage::ExternalLinkage &&
llvm::LLVMIsDeclaration(val) == 0
})
.map(move |val| {
.filter_map(|val| {
// Exclude some symbols that we know are not Rust symbols.
let name = CStr::from_ptr(llvm::LLVMGetValueName(val));
if ignored(name.to_bytes()) {
None
} else {
Some((val, name))
}
})
.map(move |(val, name)| {
let mut imp_name = prefix.as_bytes().to_vec();
imp_name.extend(name.to_bytes());
let imp_name = CString::new(imp_name).unwrap();
(imp_name, val)
})
.collect::<Vec<_>>();

for (imp_name, val) in globals {
let imp = llvm::LLVMAddGlobal(llmod,
i8p_ty,
Expand All @@ -816,4 +836,10 @@ fn create_msvc_imps(
llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage);
}
}

// Use this function to exclude certain symbols from `__imp` generation.
fn ignored(symbol_name: &[u8]) -> bool {
// These are symbols generated by LLVM's profiling instrumentation
symbol_name.starts_with(b"__llvm_profile_")
}
}
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn exported_symbols_provider_local<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}
}

if tcx.sess.opts.debugging_opts.pgo_gen.is_some() {
if tcx.sess.opts.debugging_opts.pgo_gen.enabled() {
// These are weak symbols that point to the profile version and the
// profile name, which need to be treated as exported so LTO doesn't nix
// them.
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_codegen_ssa/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use rustc_incremental::{copy_cgu_workproducts_to_incr_comp_cache_dir,
use rustc::dep_graph::{WorkProduct, WorkProductId, WorkProductFileKind};
use rustc::dep_graph::cgu_reuse_tracker::CguReuseTracker;
use rustc::middle::cstore::EncodedMetadata;
use rustc::session::config::{self, OutputFilenames, OutputType, Passes, Sanitizer, Lto};
use rustc::session::config::{self, OutputFilenames, OutputType, Passes, Lto,
Sanitizer, PgoGenerate};
use rustc::session::Session;
use rustc::util::nodemap::FxHashMap;
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
Expand Down Expand Up @@ -55,7 +56,7 @@ pub struct ModuleConfig {
/// Some(level) to optimize binary size, or None to not affect program size.
pub opt_size: Option<config::OptLevel>,

pub pgo_gen: Option<String>,
pub pgo_gen: PgoGenerate,
pub pgo_use: String,

// Flags indicating which outputs to produce.
Expand Down Expand Up @@ -93,7 +94,7 @@ impl ModuleConfig {
opt_level: None,
opt_size: None,

pgo_gen: None,
pgo_gen: PgoGenerate::Disabled,
pgo_use: String::new(),

emit_no_opt_bc: false,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ impl<'a> CrateLoader<'a> {

fn inject_profiler_runtime(&mut self) {
if self.sess.opts.debugging_opts.profile ||
self.sess.opts.debugging_opts.pgo_gen.is_some()
self.sess.opts.debugging_opts.pgo_gen.enabled()
{
info!("loading profiler");

Expand Down
Loading

0 comments on commit ee621f4

Please sign in to comment.