Skip to content

Commit 93c6362

Browse files
committed
Provide option for specifying the profiler runtime
Currently, if `-Zinstrument-coverage` is enabled, the target is linked against the `library/profiler_builtins` crate (which pulls in LLVM's compiler-rt runtime). This option enables backends to specify an alternative runtime crate for handling injected instrumentation calls.
1 parent 69b352e commit 93c6362

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

compiler/rustc_interface/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,6 @@ fn test_debugging_options_tracking_hash() {
713713
tracked!(no_codegen, true);
714714
tracked!(no_generate_arange_section, true);
715715
tracked!(no_link, true);
716-
tracked!(no_profiler_runtime, true);
717716
tracked!(osx_rpath_install_name, true);
718717
tracked!(panic_abort_tests, true);
719718
tracked!(plt, Some(true));
@@ -722,6 +721,7 @@ fn test_debugging_options_tracking_hash() {
722721
tracked!(print_fuel, Some("abc".to_string()));
723722
tracked!(profile, true);
724723
tracked!(profile_emit, Some(PathBuf::from("abc")));
724+
tracked!(profiler_runtime, None);
725725
tracked!(relax_elf_relocations, Some(true));
726726
tracked!(relro_level, Some(RelroLevel::Full));
727727
tracked!(simulate_remapped_rust_src_base, Some(PathBuf::from("/rustc/abc")));

compiler/rustc_metadata/src/creader.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -769,27 +769,31 @@ impl<'a> CrateLoader<'a> {
769769
}
770770

771771
fn inject_profiler_runtime(&mut self, krate: &ast::Crate) {
772-
if (self.sess.instrument_coverage()
772+
if self.sess.instrument_coverage()
773773
|| self.sess.opts.debugging_opts.profile
774-
|| self.sess.opts.cg.profile_generate.enabled())
775-
&& !self.sess.opts.debugging_opts.no_profiler_runtime
774+
|| self.sess.opts.cg.profile_generate.enabled()
776775
{
777-
info!("loading profiler");
778-
779-
if self.sess.contains_name(&krate.attrs, sym::no_core) {
780-
self.sess.err(
781-
"`profiler_builtins` crate (required by compiler options) \
782-
is not compatible with crate attribute `#![no_core]`",
783-
);
784-
}
776+
if let Some(name) =
777+
self.sess.opts.debugging_opts.profiler_runtime.as_deref().map(Symbol::intern)
778+
{
779+
info!("loading profiler");
780+
781+
if name == sym::profiler_builtins
782+
&& self.sess.contains_name(&krate.attrs, sym::no_core)
783+
{
784+
self.sess.err(
785+
"`profiler_builtins` crate (required by compiler options) \
786+
is not compatible with crate attribute `#![no_core]`",
787+
);
788+
}
785789

786-
let name = sym::profiler_builtins;
787-
let cnum = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit, None);
788-
let data = self.cstore.get_crate_data(cnum);
790+
let cnum = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit, None);
791+
let data = self.cstore.get_crate_data(cnum);
789792

790-
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
791-
if !data.is_profiler_runtime() {
792-
self.sess.err("the crate `profiler_builtins` is not a profiler runtime");
793+
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
794+
if !data.is_profiler_runtime() {
795+
self.sess.err(&format!("the crate `{}` is not a profiler runtime", name));
796+
}
793797
}
794798
}
795799
}

compiler/rustc_metadata/src/locator.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,9 @@ impl CrateError {
11001100
if sess.is_nightly_build() && std::env::var("CARGO").is_ok() {
11011101
err.help("consider building the standard library from source with `cargo build -Zbuild-std`");
11021102
}
1103-
} else if crate_name == sym::profiler_builtins {
1103+
} else if Some(crate_name)
1104+
== sess.opts.debugging_opts.profiler_runtime.as_deref().map(Symbol::intern)
1105+
{
11041106
err.note(&"the compiler may have been built without the profiler runtime");
11051107
}
11061108
err.span_label(span, "can't find crate");

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,8 +1148,6 @@ options! {
11481148
"compile without linking"),
11491149
no_parallel_llvm: bool = (false, parse_no_flag, [UNTRACKED],
11501150
"run LLVM in non-parallel mode (while keeping codegen-units and ThinLTO)"),
1151-
no_profiler_runtime: bool = (false, parse_no_flag, [TRACKED],
1152-
"prevent automatic injection of the profiler_builtins crate"),
11531151
normalize_docs: bool = (false, parse_bool, [TRACKED],
11541152
"normalize associated items in rustdoc when generating documentation"),
11551153
osx_rpath_install_name: bool = (false, parse_bool, [TRACKED],
@@ -1193,6 +1191,8 @@ options! {
11931191
profile_emit: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
11941192
"file path to emit profiling data at runtime when using 'profile' \
11951193
(default based on relative source path)"),
1194+
profiler_runtime: Option<String> = (Some(String::from("profiler_builtins")), parse_opt_string, [TRACKED],
1195+
"name of the profiler runtime crate to automatically inject, or None to disable"),
11961196
query_dep_graph: bool = (false, parse_bool, [UNTRACKED],
11971197
"enable queries of the dependency graph for regression testing (default: no)"),
11981198
query_stats: bool = (false, parse_bool, [UNTRACKED],

0 commit comments

Comments
 (0)