1
Fork 0

Parse "-Z instrument-xray" codegen option

Recognize all bells and whistles that LLVM's XRay pass is capable of.
The always/never settings are a bit dumb without attributes but they're
still there. The default instruction count is chosen by the compiler,
not LLVM pass. We'll do it later.
This commit is contained in:
Oleksii Lozovskyi 2022-09-24 20:02:44 +09:00
parent ef934d9b63
commit 0e60df9ed1
5 changed files with 146 additions and 3 deletions

View file

@ -174,6 +174,25 @@ pub enum InstrumentCoverage {
Off,
}
/// Settings for `-Z instrument-xray` flag.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct InstrumentXRay {
/// `-Z instrument-xray=always`, force instrumentation
pub always: bool,
/// `-Z instrument-xray=never`, disable instrumentation
pub never: bool,
/// `-Z instrument-xray=ignore-loops`, ignore presence of loops,
/// instrument functions based only on instruction count
pub ignore_loops: bool,
/// `-Z instrument-xray=instruction-threshold=N`, explicitly set instruction threshold
/// for instrumentation, or `None` to use compiler's default
pub instruction_threshold: Option<usize>,
/// `-Z instrument-xray=skip-entry`, do not instrument function entry
pub skip_entry: bool,
/// `-Z instrument-xray=skip-exit`, do not instrument function exit
pub skip_exit: bool,
}
#[derive(Clone, PartialEq, Hash, Debug)]
pub enum LinkerPluginLto {
LinkerPlugin(PathBuf),
@ -2805,9 +2824,9 @@ impl PpMode {
pub(crate) mod dep_tracking {
use super::{
BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, ErrorOutputType,
InstrumentCoverage, LdImpl, LinkerPluginLto, LocationDetail, LtoCli, OomStrategy, OptLevel,
OutputType, OutputTypes, Passes, SourceFileHashAlgorithm, SplitDwarfKind,
SwitchWithOptPath, SymbolManglingVersion, TraitSolver, TrimmedDefPaths,
InstrumentCoverage, InstrumentXRay, LdImpl, LinkerPluginLto, LocationDetail, LtoCli,
OomStrategy, OptLevel, OutputType, OutputTypes, Passes, SourceFileHashAlgorithm,
SplitDwarfKind, SwitchWithOptPath, SymbolManglingVersion, TraitSolver, TrimmedDefPaths,
};
use crate::lint;
use crate::options::WasiExecModel;
@ -2876,6 +2895,7 @@ pub(crate) mod dep_tracking {
CodeModel,
TlsModel,
InstrumentCoverage,
InstrumentXRay,
CrateType,
MergeFunctions,
PanicStrategy,

View file

@ -380,6 +380,7 @@ mod desc {
pub const parse_dump_mono_stats: &str = "`markdown` (default) or `json`";
pub const parse_instrument_coverage: &str =
"`all` (default), `except-unused-generics`, `except-unused-functions`, or `off`";
pub const parse_instrument_xray: &str = "either a boolean (`yes`, `no`, `on`, `off`, etc), or a comma separated list of settings: `always` or `never` (mutually exclusive), `ignore-loops`, `instruction-threshold=N`, `skip-entry`, `skip-exit`";
pub const parse_unpretty: &str = "`string` or `string=string`";
pub const parse_treat_err_as_bug: &str = "either no value or a number bigger than 0";
pub const parse_trait_solver: &str =
@ -869,6 +870,68 @@ mod parse {
true
}
pub(crate) fn parse_instrument_xray(
slot: &mut Option<InstrumentXRay>,
v: Option<&str>,
) -> bool {
if v.is_some() {
let mut bool_arg = None;
if parse_opt_bool(&mut bool_arg, v) {
*slot = if bool_arg.unwrap() { Some(InstrumentXRay::default()) } else { None };
return true;
}
}
let mut options = slot.get_or_insert_default();
let mut seen_always = false;
let mut seen_never = false;
let mut seen_ignore_loops = false;
let mut seen_instruction_threshold = false;
let mut seen_skip_entry = false;
let mut seen_skip_exit = false;
for option in v.into_iter().map(|v| v.split(',')).flatten() {
match option {
"always" if !seen_always && !seen_never => {
options.always = true;
options.never = false;
seen_always = true;
}
"never" if !seen_never && !seen_always => {
options.never = true;
options.always = false;
seen_never = true;
}
"ignore-loops" if !seen_ignore_loops => {
options.ignore_loops = true;
seen_ignore_loops = true;
}
option
if option.starts_with("instruction-threshold")
&& !seen_instruction_threshold =>
{
let Some(("instruction-threshold", n)) = option.split_once('=') else {
return false;
};
match n.parse() {
Ok(n) => options.instruction_threshold = Some(n),
Err(_) => return false,
}
seen_instruction_threshold = true;
}
"skip-entry" if !seen_skip_entry => {
options.skip_entry = true;
seen_skip_entry = true;
}
"skip-exit" if !seen_skip_exit => {
options.skip_exit = true;
seen_skip_exit = true;
}
_ => return false,
}
}
true
}
pub(crate) fn parse_treat_err_as_bug(slot: &mut Option<NonZeroUsize>, v: Option<&str>) -> bool {
match v {
Some(s) => {
@ -1397,6 +1460,16 @@ options! {
`=off` (default)"),
instrument_mcount: bool = (false, parse_bool, [TRACKED],
"insert function instrument code for mcount-based tracing (default: no)"),
instrument_xray: Option<InstrumentXRay> = (None, parse_instrument_xray, [TRACKED],
"insert function instrument code for XRay-based tracing (default: no)
Optional extra settings:
`=always`
`=never`
`=ignore-loops`
`=instruction-threshold=N`
`=skip-entry`
`=skip-exit`
Multiple options can be combined with commas."),
keep_hygiene_data: bool = (false, parse_bool, [UNTRACKED],
"keep hygiene data after analysis (default: no)"),
layout_seed: Option<u64> = (None, parse_opt_number, [TRACKED],