Skip to content

Commit 7b1df42

Browse files
Clean up handling of -Zpgo-gen commandline option.
1 parent 3750348 commit 7b1df42

File tree

9 files changed

+60
-27
lines changed

9 files changed

+60
-27
lines changed

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

+16-6
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};
@@ -26,7 +26,7 @@ use errors::{Handler, FatalError};
2626
use std::ffi::{CString, CStr};
2727
use std::fs;
2828
use std::io::{self, Write};
29-
use std::path::Path;
29+
use std::path::{Path, PathBuf};
3030
use std::str;
3131
use std::sync::Arc;
3232
use std::slice;
@@ -708,10 +708,20 @@ pub unsafe fn with_llvm_pmb(llmod: &llvm::Module,
708708
.unwrap_or(llvm::CodeGenOptSizeNone);
709709
let inline_threshold = config.inline_threshold;
710710

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

716726
let pgo_use_path = if config.pgo_use.is_empty() {
717727
None

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};
@@ -56,7 +57,7 @@ pub struct ModuleConfig {
5657
/// Some(level) to optimize binary size, or None to not affect program size.
5758
pub opt_size: Option<config::OptLevel>,
5859

59-
pub pgo_gen: Option<String>,
60+
pub pgo_gen: PgoGenerate,
6061
pub pgo_use: String,
6162

6263
// Flags indicating which outputs to produce.
@@ -94,7 +95,7 @@ impl ModuleConfig {
9495
opt_level: None,
9596
opt_size: None,
9697

97-
pgo_gen: None,
98+
pgo_gen: PgoGenerate::Disabled,
9899
pgo_use: String::new(),
99100

100101
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

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
-include ../tools.mk
22

3-
# ignore-windows
4-
53
all:
64
ifeq ($(PROFILER_SUPPORT),1)
7-
$(RUSTC) -Copt-level=3 -Clto=fat -Z pgo-gen="$(TMPDIR)/test.profraw" test.rs
5+
$(RUSTC) -Copt-level=3 -Clto=fat -Z pgo-gen="$(TMPDIR)" test.rs
86
$(call RUN,test) || exit 1
9-
[ -e "$(TMPDIR)/test.profraw" ] || (echo "No .profraw file"; exit 1)
7+
[ -e "$(TMPDIR)"/default_*.profraw ] || (echo "No .profraw file"; exit 1)
108
endif
+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
-include ../tools.mk
22

3-
# ignore-windows
4-
53
all:
64
ifeq ($(PROFILER_SUPPORT),1)
7-
$(RUSTC) -g -Z pgo-gen="$(TMPDIR)/test.profraw" test.rs
5+
$(RUSTC) -g -Z pgo-gen="$(TMPDIR)" test.rs
86
$(call RUN,test) || exit 1
9-
[ -e "$(TMPDIR)/test.profraw" ] || (echo "No .profraw file"; exit 1)
7+
[ -e "$(TMPDIR)"/default_*.profraw ] || (echo "No .profraw file"; exit 1)
108
endif

0 commit comments

Comments
 (0)