Skip to content

Commit 6aee422

Browse files
committed
Add -Zinstrument-mcount-opts
This facilitates support for inserting nop's and/or recording the location of each mcount call in a special section named `__mcount_loc`.
1 parent 3be74a5 commit 6aee422

4 files changed

Lines changed: 87 additions & 4 deletions

File tree

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ fn instrument_function_attr<'ll>(
229229
if instrument_entry && sess.opts.unstable_opts.instrument_fentry {
230230
attrs.push(llvm::CreateAttrStringValue(cx.llcx, "fentry-call", "true"));
231231
}
232+
233+
if sess.opts.unstable_opts.instrument_mcount_opts.no_call {
234+
attrs.push(llvm::CreateAttrString(cx.llcx, "mnop-mcount"));
235+
}
236+
if sess.opts.unstable_opts.instrument_mcount_opts.record {
237+
attrs.push(llvm::CreateAttrString(cx.llcx, "mrecord-mcount"));
238+
}
232239
}
233240
if let Some(options) = &sess.opts.unstable_opts.instrument_xray {
234241
// XRay instrumentation is similar to __cyg_profile_func_{enter,exit}.

compiler/rustc_session/src/config.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ pub enum AnnotateMoves {
246246
Enabled(Option<u64>),
247247
}
248248

249+
/// Settings for `-Z instrument-mcount-opts` flag.
250+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
251+
pub struct InstrumentMcountOpts {
252+
// Insert a nop which could be replaced by an mcount call.
253+
pub no_call: bool,
254+
// Record the location of the call instrument in a special linker section.
255+
pub record: bool,
256+
}
257+
249258
/// Settings for `-Z instrument-xray` flag.
250259
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
251260
pub struct InstrumentXRay {
@@ -3050,10 +3059,11 @@ pub(crate) mod dep_tracking {
30503059
use super::{
30513060
AnnotateMoves, AutoDiff, BranchProtection, CFGuard, CFProtection, CoverageOptions,
30523061
CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FmtDebug, FunctionReturn,
3053-
InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail,
3054-
LtoCli, MirStripDebugInfo, NextSolverConfig, Offload, OptLevel, OutFileName, OutputType,
3055-
OutputTypes, PatchableFunctionEntry, Polonius, ResolveDocLinks, SourceFileHashAlgorithm,
3056-
SplitDwarfKind, SwitchWithOptPath, SymbolManglingVersion, WasiExecModel,
3062+
InliningThreshold, InstrumentCoverage, InstrumentMcountOpts, InstrumentXRay,
3063+
LinkerPluginLto, LocationDetail, LtoCli, MirStripDebugInfo, NextSolverConfig, Offload,
3064+
OptLevel, OutFileName, OutputType, OutputTypes, PatchableFunctionEntry, Polonius,
3065+
ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath,
3066+
SymbolManglingVersion, WasiExecModel,
30573067
};
30583068
use crate::lint;
30593069
use crate::utils::NativeLib;
@@ -3115,6 +3125,7 @@ pub(crate) mod dep_tracking {
31153125
TlsModel,
31163126
InstrumentCoverage,
31173127
CoverageOptions,
3128+
InstrumentMcountOpts,
31183129
InstrumentXRay,
31193130
CrateType,
31203131
MergeFunctions,

compiler/rustc_session/src/options.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,8 @@ mod desc {
783783
pub(crate) const parse_dump_mono_stats: &str = "`markdown` (default) or `json`";
784784
pub(crate) const parse_instrument_coverage: &str = parse_bool;
785785
pub(crate) const parse_coverage_options: &str = "`block` | `branch` | `condition`";
786+
pub(crate) const parse_instrument_mcount_opts: &str =
787+
"a comma separated list of options: `call`,`no-call`,`record`,`no-record`.";
786788
pub(crate) 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`";
787789
pub(crate) const parse_unpretty: &str = "`string` or `string=string`";
788790
pub(crate) const parse_treat_err_as_bug: &str = "either no value or a non-negative number";
@@ -1538,6 +1540,32 @@ pub mod parse {
15381540
true
15391541
}
15401542

1543+
pub(crate) fn parse_instrument_mcount_opts(
1544+
slot: &mut InstrumentMcountOpts,
1545+
v: Option<&str>,
1546+
) -> bool {
1547+
for option in v.into_iter().flat_map(|v| v.split(',')) {
1548+
match option {
1549+
"no-call" => {
1550+
slot.no_call = true;
1551+
}
1552+
"call" => {
1553+
slot.no_call = false;
1554+
}
1555+
"no-record" => {
1556+
slot.record = false;
1557+
}
1558+
"record" => {
1559+
slot.record = true;
1560+
}
1561+
_ => {
1562+
return false;
1563+
}
1564+
}
1565+
}
1566+
v.is_some()
1567+
}
1568+
15411569
pub(crate) fn parse_instrument_xray(
15421570
slot: &mut Option<InstrumentXRay>,
15431571
v: Option<&str>,
@@ -2396,6 +2424,12 @@ options! {
23962424
"insert function instrument code for fentry-based tracing (default: no)"),
23972425
instrument_mcount: bool = (false, parse_bool, [TRACKED],
23982426
"insert function instrument code for mcount-based tracing (default: no)"),
2427+
instrument_mcount_opts: InstrumentMcountOpts = (InstrumentMcountOpts::default(), parse_instrument_mcount_opts, [TRACKED],
2428+
"mcount instrumentation configuration options (default: `call,no-record`).
2429+
Optional extra settings:
2430+
`=call` or `=no-call`
2431+
`=no-record` or `=record`
2432+
Multiple options can be combined with commas."),
23992433
instrument_xray: Option<InstrumentXRay> = (None, parse_instrument_xray, [TRACKED],
24002434
"insert function instrument code for XRay-based tracing (default: no)
24012435
Optional extra settings:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@ revisions: dflt ncyr ycnr ncnr ycyr
2+
//@ add-minicore
3+
//@ needs-llvm-components: systemz
4+
//@ compile-flags: -Zinstrument-fentry=y -Copt-level=0 --target=s390x-unknown-linux-gnu
5+
//@[ncyr] compile-flags: -Zinstrument-mcount-opts=no-call,record
6+
//@[ncnr] compile-flags: -Zinstrument-mcount-opts=no-call,no-record
7+
//@[ycnr] compile-flags: -Zinstrument-mcount-opts=call,no-record
8+
//@[ycyr] compile-flags: -Zinstrument-mcount-opts=call,record
9+
#![feature(no_core)]
10+
#![crate_type = "rlib"]
11+
#![no_core]
12+
13+
extern crate minicore;
14+
use minicore::*;
15+
16+
// dflt: attributes #{{.*}} {{.*}} "fentry-call"="true"
17+
// dflt-NOT: attributes #{{.*}} {{.*}} "mnop-mcount"
18+
// dflt-NOT: attributes #{{.*}} {{.*}} "mrecord-mcount"
19+
//
20+
// ncyr: attributes #{{.*}} {{.*}} "fentry-call"="true" "mnop-mcount" "mrecord-mcount"
21+
//
22+
// ncnr: attributes #{{.*}} {{.*}} "fentry-call"="true" "mnop-mcount"
23+
// ncnr-NOT: attributes #{{.*}} {{.*}} "mrecord-mcount"
24+
//
25+
// ycnr: attributes #{{.*}} {{.*}} "fentry-call"="true"
26+
// ycnr-NOT: attributes #{{.*}} {{.*}} "mnop-mcount"
27+
// ycnr-NOT: attributes #{{.*}} {{.*}} "mrecord-mcount"
28+
//
29+
// ycyr: attributes #{{.*}} {{.*}} "fentry-call"="true" "mrecord-mcount"
30+
// ycyr-NOT: attributes #{{.*}} {{.*}} "mnop-mcount"
31+
pub fn foo() {}

0 commit comments

Comments
 (0)