Skip to content

Commit 0e60df9

Browse files
committed
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.
1 parent ef934d9 commit 0e60df9

File tree

5 files changed

+146
-3
lines changed

5 files changed

+146
-3
lines changed

compiler/rustc_interface/src/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_data_structures::fx::FxHashSet;
55
use rustc_errors::{emitter::HumanReadableErrorType, registry, ColorConfig};
66
use rustc_session::config::rustc_optgroups;
77
use rustc_session::config::Input;
8+
use rustc_session::config::InstrumentXRay;
89
use rustc_session::config::TraitSolver;
910
use rustc_session::config::{build_configuration, build_session_options, to_crate_config};
1011
use rustc_session::config::{
@@ -755,6 +756,7 @@ fn test_unstable_options_tracking_hash() {
755756
tracked!(inline_mir_threshold, Some(123));
756757
tracked!(instrument_coverage, Some(InstrumentCoverage::All));
757758
tracked!(instrument_mcount, true);
759+
tracked!(instrument_xray, Some(InstrumentXRay::default()));
758760
tracked!(link_only, true);
759761
tracked!(llvm_plugins, vec![String::from("plugin_name")]);
760762
tracked!(location_detail, LocationDetail { file: true, line: false, column: false });

compiler/rustc_session/src/config.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,25 @@ pub enum InstrumentCoverage {
174174
Off,
175175
}
176176

177+
/// Settings for `-Z instrument-xray` flag.
178+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
179+
pub struct InstrumentXRay {
180+
/// `-Z instrument-xray=always`, force instrumentation
181+
pub always: bool,
182+
/// `-Z instrument-xray=never`, disable instrumentation
183+
pub never: bool,
184+
/// `-Z instrument-xray=ignore-loops`, ignore presence of loops,
185+
/// instrument functions based only on instruction count
186+
pub ignore_loops: bool,
187+
/// `-Z instrument-xray=instruction-threshold=N`, explicitly set instruction threshold
188+
/// for instrumentation, or `None` to use compiler's default
189+
pub instruction_threshold: Option<usize>,
190+
/// `-Z instrument-xray=skip-entry`, do not instrument function entry
191+
pub skip_entry: bool,
192+
/// `-Z instrument-xray=skip-exit`, do not instrument function exit
193+
pub skip_exit: bool,
194+
}
195+
177196
#[derive(Clone, PartialEq, Hash, Debug)]
178197
pub enum LinkerPluginLto {
179198
LinkerPlugin(PathBuf),
@@ -2805,9 +2824,9 @@ impl PpMode {
28052824
pub(crate) mod dep_tracking {
28062825
use super::{
28072826
BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, ErrorOutputType,
2808-
InstrumentCoverage, LdImpl, LinkerPluginLto, LocationDetail, LtoCli, OomStrategy, OptLevel,
2809-
OutputType, OutputTypes, Passes, SourceFileHashAlgorithm, SplitDwarfKind,
2810-
SwitchWithOptPath, SymbolManglingVersion, TraitSolver, TrimmedDefPaths,
2827+
InstrumentCoverage, InstrumentXRay, LdImpl, LinkerPluginLto, LocationDetail, LtoCli,
2828+
OomStrategy, OptLevel, OutputType, OutputTypes, Passes, SourceFileHashAlgorithm,
2829+
SplitDwarfKind, SwitchWithOptPath, SymbolManglingVersion, TraitSolver, TrimmedDefPaths,
28112830
};
28122831
use crate::lint;
28132832
use crate::options::WasiExecModel;
@@ -2876,6 +2895,7 @@ pub(crate) mod dep_tracking {
28762895
CodeModel,
28772896
TlsModel,
28782897
InstrumentCoverage,
2898+
InstrumentXRay,
28792899
CrateType,
28802900
MergeFunctions,
28812901
PanicStrategy,

compiler/rustc_session/src/options.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ mod desc {
380380
pub const parse_dump_mono_stats: &str = "`markdown` (default) or `json`";
381381
pub const parse_instrument_coverage: &str =
382382
"`all` (default), `except-unused-generics`, `except-unused-functions`, or `off`";
383+
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`";
383384
pub const parse_unpretty: &str = "`string` or `string=string`";
384385
pub const parse_treat_err_as_bug: &str = "either no value or a number bigger than 0";
385386
pub const parse_trait_solver: &str =
@@ -869,6 +870,68 @@ mod parse {
869870
true
870871
}
871872

873+
pub(crate) fn parse_instrument_xray(
874+
slot: &mut Option<InstrumentXRay>,
875+
v: Option<&str>,
876+
) -> bool {
877+
if v.is_some() {
878+
let mut bool_arg = None;
879+
if parse_opt_bool(&mut bool_arg, v) {
880+
*slot = if bool_arg.unwrap() { Some(InstrumentXRay::default()) } else { None };
881+
return true;
882+
}
883+
}
884+
885+
let mut options = slot.get_or_insert_default();
886+
let mut seen_always = false;
887+
let mut seen_never = false;
888+
let mut seen_ignore_loops = false;
889+
let mut seen_instruction_threshold = false;
890+
let mut seen_skip_entry = false;
891+
let mut seen_skip_exit = false;
892+
for option in v.into_iter().map(|v| v.split(',')).flatten() {
893+
match option {
894+
"always" if !seen_always && !seen_never => {
895+
options.always = true;
896+
options.never = false;
897+
seen_always = true;
898+
}
899+
"never" if !seen_never && !seen_always => {
900+
options.never = true;
901+
options.always = false;
902+
seen_never = true;
903+
}
904+
"ignore-loops" if !seen_ignore_loops => {
905+
options.ignore_loops = true;
906+
seen_ignore_loops = true;
907+
}
908+
option
909+
if option.starts_with("instruction-threshold")
910+
&& !seen_instruction_threshold =>
911+
{
912+
let Some(("instruction-threshold", n)) = option.split_once('=') else {
913+
return false;
914+
};
915+
match n.parse() {
916+
Ok(n) => options.instruction_threshold = Some(n),
917+
Err(_) => return false,
918+
}
919+
seen_instruction_threshold = true;
920+
}
921+
"skip-entry" if !seen_skip_entry => {
922+
options.skip_entry = true;
923+
seen_skip_entry = true;
924+
}
925+
"skip-exit" if !seen_skip_exit => {
926+
options.skip_exit = true;
927+
seen_skip_exit = true;
928+
}
929+
_ => return false,
930+
}
931+
}
932+
true
933+
}
934+
872935
pub(crate) fn parse_treat_err_as_bug(slot: &mut Option<NonZeroUsize>, v: Option<&str>) -> bool {
873936
match v {
874937
Some(s) => {
@@ -1397,6 +1460,16 @@ options! {
13971460
`=off` (default)"),
13981461
instrument_mcount: bool = (false, parse_bool, [TRACKED],
13991462
"insert function instrument code for mcount-based tracing (default: no)"),
1463+
instrument_xray: Option<InstrumentXRay> = (None, parse_instrument_xray, [TRACKED],
1464+
"insert function instrument code for XRay-based tracing (default: no)
1465+
Optional extra settings:
1466+
`=always`
1467+
`=never`
1468+
`=ignore-loops`
1469+
`=instruction-threshold=N`
1470+
`=skip-entry`
1471+
`=skip-exit`
1472+
Multiple options can be combined with commas."),
14001473
keep_hygiene_data: bool = (false, parse_bool, [UNTRACKED],
14011474
"keep hygiene data after analysis (default: no)"),
14021475
layout_seed: Option<u64> = (None, parse_opt_number, [TRACKED],
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# `instrument-xray`
2+
3+
The tracking issue for this feature is: [#102921](https://github.com/rust-lang/rust/issues/102921).
4+
5+
------------------------
6+
7+
Enable generation of NOP sleds for XRay function tracing instrumentation.
8+
For more information on XRay,
9+
read [LLVM documentation](https://llvm.org/docs/XRay.html),
10+
and/or the [XRay whitepaper](http://research.google.com/pubs/pub45287.html).
11+
12+
Set the `-Z instrument-xray` compiler flag in order to enable XRay instrumentation.
13+
14+
- `-Z instrument-xray` – use the default settings
15+
- `-Z instrument-xray=skip-exit` – configure a custom setting
16+
- `-Z instrument-xray=ignore-loops,instruction-threshold=300`
17+
multiple settings separated by commas
18+
19+
Supported options:
20+
21+
- `always` – force instrumentation of all functions
22+
- `never` – do no instrument any functions
23+
- `ignore-loops` – ignore presence of loops,
24+
instrument functions based only on instruction count
25+
- `instruction-threshold=10` – set a different instruction threshold for instrumentation
26+
- `skip-entry` – do no instrument function entry
27+
- `skip-exit` – do no instrument function exit
28+
29+
The default settings are:
30+
31+
- instrument both entry & exit from functions
32+
- instrument functions with at least 200 instructions,
33+
or containing a non-trivial loop
34+
35+
Note that `-Z instrument-xray` only enables generation of NOP sleds
36+
which on their own don't do anything useful.
37+
In order to actually trace the functions,
38+
you will need to link a separate runtime library of your choice,
39+
such as Clang's [XRay Runtime Library](https://www.llvm.org/docs/XRay.html#xray-runtime-library).

tests/rustdoc-ui/z-help.stdout

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@
7070
`=except-unused-functions`
7171
`=off` (default)
7272
-Z instrument-mcount=val -- insert function instrument code for mcount-based tracing (default: no)
73+
-Z instrument-xray=val -- insert function instrument code for XRay-based tracing (default: no)
74+
Optional extra settings:
75+
`=always`
76+
`=never`
77+
`=ignore-loops`
78+
`=instruction-threshold=N`
79+
`=skip-entry`
80+
`=skip-exit`
81+
Multiple options can be combined with commas.
7382
-Z keep-hygiene-data=val -- keep hygiene data after analysis (default: no)
7483
-Z layout-seed=val -- seed layout randomization
7584
-Z link-native-libraries=val -- link native libraries in the linker invocation (default: yes)

0 commit comments

Comments
 (0)