Skip to content

Commit ed33787

Browse files
committed
Auto merge of #85284 - eggyal:custom-profiler-runtime, r=jackh726
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.
2 parents 1a46283 + 872839e commit ed33787

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

compiler/rustc_interface/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,6 @@ fn test_debugging_options_tracking_hash() {
715715
tracked!(no_codegen, true);
716716
tracked!(no_generate_arange_section, true);
717717
tracked!(no_link, true);
718-
tracked!(no_profiler_runtime, true);
719718
tracked!(osx_rpath_install_name, true);
720719
tracked!(panic_abort_tests, true);
721720
tracked!(plt, Some(true));
@@ -724,6 +723,7 @@ fn test_debugging_options_tracking_hash() {
724723
tracked!(print_fuel, Some("abc".to_string()));
725724
tracked!(profile, true);
726725
tracked!(profile_emit, Some(PathBuf::from("abc")));
726+
tracked!(profiler_runtime, None);
727727
tracked!(relax_elf_relocations, Some(true));
728728
tracked!(relro_level, Some(RelroLevel::Full));
729729
tracked!(simulate_remapped_rust_src_base, Some(PathBuf::from("/rustc/abc")));

compiler/rustc_metadata/src/creader.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -769,28 +769,32 @@ impl<'a> CrateLoader<'a> {
769769
}
770770

771771
fn inject_profiler_runtime(&mut self, krate: &ast::Crate) {
772-
if (self.sess.instrument_coverage()
773-
|| self.sess.opts.debugging_opts.profile
774-
|| self.sess.opts.cg.profile_generate.enabled())
775-
&& !self.sess.opts.debugging_opts.no_profiler_runtime
772+
let profiler_runtime = &self.sess.opts.debugging_opts.profiler_runtime;
773+
774+
if !(profiler_runtime.is_some()
775+
&& (self.sess.instrument_coverage()
776+
|| self.sess.opts.debugging_opts.profile
777+
|| self.sess.opts.cg.profile_generate.enabled()))
776778
{
777-
info!("loading profiler");
779+
return;
780+
}
778781

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-
}
782+
info!("loading profiler");
783+
784+
let name = Symbol::intern(profiler_runtime.as_ref().unwrap());
785+
if name == sym::profiler_builtins && self.sess.contains_name(&krate.attrs, sym::no_core) {
786+
self.sess.err(
787+
"`profiler_builtins` crate (required by compiler options) \
788+
is not compatible with crate attribute `#![no_core]`",
789+
);
790+
}
785791

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);
792+
let cnum = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit, None);
793+
let data = self.cstore.get_crate_data(cnum);
789794

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-
}
795+
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
796+
if !data.is_profiler_runtime() {
797+
self.sess.err(&format!("the crate `{}` is not a profiler runtime", name));
794798
}
795799
}
796800

compiler/rustc_metadata/src/locator.rs

+3-1
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1160,8 +1160,6 @@ options! {
11601160
"compile without linking"),
11611161
no_parallel_llvm: bool = (false, parse_no_flag, [UNTRACKED],
11621162
"run LLVM in non-parallel mode (while keeping codegen-units and ThinLTO)"),
1163-
no_profiler_runtime: bool = (false, parse_no_flag, [TRACKED],
1164-
"prevent automatic injection of the profiler_builtins crate"),
11651163
normalize_docs: bool = (false, parse_bool, [TRACKED],
11661164
"normalize associated items in rustdoc when generating documentation"),
11671165
osx_rpath_install_name: bool = (false, parse_bool, [TRACKED],
@@ -1205,6 +1203,8 @@ options! {
12051203
profile_emit: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
12061204
"file path to emit profiling data at runtime when using 'profile' \
12071205
(default based on relative source path)"),
1206+
profiler_runtime: Option<String> = (Some(String::from("profiler_builtins")), parse_opt_string, [TRACKED],
1207+
"name of the profiler runtime crate to automatically inject, or None to disable"),
12081208
query_dep_graph: bool = (false, parse_bool, [UNTRACKED],
12091209
"enable queries of the dependency graph for regression testing (default: no)"),
12101210
query_stats: bool = (false, parse_bool, [UNTRACKED],

0 commit comments

Comments
 (0)