Skip to content

Commit 327723b

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 5ca2f6b commit 327723b

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
@@ -247,6 +247,15 @@ pub enum AnnotateMoves {
247247
Enabled(Option<u64>),
248248
}
249249

250+
/// Settings for `-Z instrument-mcount-opts` flag.
251+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
252+
pub struct InstrumentMcountOpts {
253+
// Insert a nop which could be replaced by an mcount call.
254+
pub no_call: bool,
255+
// Record the location of the call instrument in a special linker section.
256+
pub record: bool,
257+
}
258+
250259
/// Settings for `-Z instrument-xray` flag.
251260
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
252261
pub struct InstrumentXRay {
@@ -3057,10 +3066,11 @@ pub(crate) mod dep_tracking {
30573066
use super::{
30583067
AnnotateMoves, AutoDiff, BranchProtection, CFGuard, CFProtection, CoverageOptions,
30593068
CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FmtDebug, FunctionReturn,
3060-
InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail,
3061-
LtoCli, MirStripDebugInfo, NextSolverConfig, Offload, OptLevel, OutFileName, OutputType,
3062-
OutputTypes, PatchableFunctionEntry, Polonius, ResolveDocLinks, SourceFileHashAlgorithm,
3063-
SplitDwarfKind, SwitchWithOptPath, SymbolManglingVersion, WasiExecModel,
3069+
InliningThreshold, InstrumentCoverage, InstrumentMcountOpts, InstrumentXRay,
3070+
LinkerPluginLto, LocationDetail, LtoCli, MirStripDebugInfo, NextSolverConfig, Offload,
3071+
OptLevel, OutFileName, OutputType, OutputTypes, PatchableFunctionEntry, Polonius,
3072+
ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath,
3073+
SymbolManglingVersion, WasiExecModel,
30643074
};
30653075
use crate::lint;
30663076
use crate::utils::NativeLib;
@@ -3122,6 +3132,7 @@ pub(crate) mod dep_tracking {
31223132
TlsModel,
31233133
InstrumentCoverage,
31243134
CoverageOptions,
3135+
InstrumentMcountOpts,
31253136
InstrumentXRay,
31263137
CrateType,
31273138
MergeFunctions,

compiler/rustc_session/src/options.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,8 @@ mod desc {
832832
pub(crate) const parse_dump_mono_stats: &str = "`markdown` (default) or `json`";
833833
pub(crate) const parse_instrument_coverage: &str = parse_bool;
834834
pub(crate) const parse_coverage_options: &str = "`block` | `branch` | `condition`";
835+
pub(crate) const parse_instrument_mcount_opts: &str =
836+
"a comma separated list of options: `call`,`no-call`,`record`,`no-record`.";
835837
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`";
836838
pub(crate) const parse_unpretty: &str = "`string` or `string=string`";
837839
pub(crate) const parse_treat_err_as_bug: &str = "either no value or a non-negative number";
@@ -1582,6 +1584,32 @@ pub mod parse {
15821584
true
15831585
}
15841586

1587+
pub(crate) fn parse_instrument_mcount_opts(
1588+
slot: &mut InstrumentMcountOpts,
1589+
v: Option<&str>,
1590+
) -> bool {
1591+
for option in v.into_iter().flat_map(|v| v.split(',')) {
1592+
match option {
1593+
"no-call" => {
1594+
slot.no_call = true;
1595+
}
1596+
"call" => {
1597+
slot.no_call = false;
1598+
}
1599+
"no-record" => {
1600+
slot.record = false;
1601+
}
1602+
"record" => {
1603+
slot.record = true;
1604+
}
1605+
_ => {
1606+
return false;
1607+
}
1608+
}
1609+
}
1610+
v.is_some()
1611+
}
1612+
15851613
pub(crate) fn parse_instrument_xray(
15861614
slot: &mut Option<InstrumentXRay>,
15871615
v: Option<&str>,
@@ -2434,6 +2462,12 @@ options! {
24342462
"insert function instrument code for fentry-based tracing (default: no)"),
24352463
instrument_mcount: bool = (false, parse_bool, [TRACKED],
24362464
"insert function instrument code for mcount-based tracing (default: no)"),
2465+
instrument_mcount_opts: InstrumentMcountOpts = (InstrumentMcountOpts::default(), parse_instrument_mcount_opts, [TRACKED],
2466+
"mcount instrumentation configuration options (default: `call,no-record`).
2467+
Optional extra settings:
2468+
`=call` or `=no-call`
2469+
`=no-record` or `=record`
2470+
Multiple options can be combined with commas."),
24372471
instrument_xray: Option<InstrumentXRay> = (None, parse_instrument_xray, [TRACKED],
24382472
"insert function instrument code for XRay-based tracing (default: no)
24392473
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)