From e6f1ca6752be0816de32747696bce429f5647868 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 23 Nov 2024 16:36:24 +1100 Subject: [PATCH 1/6] Clarify logic for whether a profiler runtime is needed --- compiler/rustc_metadata/src/creader.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index a611e8010be36..6c866e72ca3a3 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -800,9 +800,9 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { } fn inject_profiler_runtime(&mut self, krate: &ast::Crate) { - if self.sess.opts.unstable_opts.no_profiler_runtime - || !(self.sess.instrument_coverage() || self.sess.opts.cg.profile_generate.enabled()) - { + let needs_profiler_runtime = + self.sess.instrument_coverage() || self.sess.opts.cg.profile_generate.enabled(); + if !needs_profiler_runtime || self.sess.opts.unstable_opts.no_profiler_runtime { return; } From 7d2d11b59588e5e75c666b3d2ce6187cbfa6ac7e Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 23 Nov 2024 16:11:02 +1100 Subject: [PATCH 2/6] Sort and separate lint/feature attributes in `profiler_builtins` --- library/profiler_builtins/src/lib.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/library/profiler_builtins/src/lib.rs b/library/profiler_builtins/src/lib.rs index ac685b18c2911..b868cdde405a9 100644 --- a/library/profiler_builtins/src/lib.rs +++ b/library/profiler_builtins/src/lib.rs @@ -1,11 +1,15 @@ -#![no_std] +// tidy-alphabetical-start +#![allow(internal_features)] +#![allow(unused_features)] #![feature(profiler_runtime)] +#![feature(staged_api)] +// tidy-alphabetical-end + +// Other attributes: +#![no_std] #![profiler_runtime] #![unstable( feature = "profiler_runtime_lib", reason = "internal implementation detail of rustc right now", issue = "none" )] -#![allow(unused_features)] -#![allow(internal_features)] -#![feature(staged_api)] From 5967cf1a3ae6fde206ae03a6a2ffc389917db3d5 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 23 Nov 2024 16:13:53 +1100 Subject: [PATCH 3/6] Remove unnecessary `#![allow(unused_features)]` --- library/profiler_builtins/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/profiler_builtins/src/lib.rs b/library/profiler_builtins/src/lib.rs index b868cdde405a9..68b6058db978c 100644 --- a/library/profiler_builtins/src/lib.rs +++ b/library/profiler_builtins/src/lib.rs @@ -1,6 +1,5 @@ // tidy-alphabetical-start #![allow(internal_features)] -#![allow(unused_features)] #![feature(profiler_runtime)] #![feature(staged_api)] // tidy-alphabetical-end From bba35673869408839778949efcb223cf77597352 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 23 Nov 2024 16:14:40 +1100 Subject: [PATCH 4/6] Make profiler_builtins `#![no_core]` instead of just `#![no_std]` This crate doesn't contain any actual Rust code; it's just C/C++ code built and packaged in a Rust-friendly way. --- library/Cargo.lock | 2 -- library/profiler_builtins/Cargo.toml | 2 -- library/profiler_builtins/src/lib.rs | 3 ++- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/library/Cargo.lock b/library/Cargo.lock index 97996d5f0b258..2265fbf70d3f2 100644 --- a/library/Cargo.lock +++ b/library/Cargo.lock @@ -235,8 +235,6 @@ name = "profiler_builtins" version = "0.0.0" dependencies = [ "cc", - "compiler_builtins", - "core", ] [[package]] diff --git a/library/profiler_builtins/Cargo.toml b/library/profiler_builtins/Cargo.toml index f94ea9a6cda28..17e3cbdf52b5e 100644 --- a/library/profiler_builtins/Cargo.toml +++ b/library/profiler_builtins/Cargo.toml @@ -9,8 +9,6 @@ bench = false doc = false [dependencies] -core = { path = "../core" } -compiler_builtins = { version = "0.1.0", features = ['rustc-dep-of-std'] } [build-dependencies] # FIXME: Pinned due to build error when bumped (#132556) diff --git a/library/profiler_builtins/src/lib.rs b/library/profiler_builtins/src/lib.rs index 68b6058db978c..a258f7d31a191 100644 --- a/library/profiler_builtins/src/lib.rs +++ b/library/profiler_builtins/src/lib.rs @@ -1,11 +1,12 @@ // tidy-alphabetical-start #![allow(internal_features)] +#![feature(no_core)] #![feature(profiler_runtime)] #![feature(staged_api)] // tidy-alphabetical-end // Other attributes: -#![no_std] +#![no_core] #![profiler_runtime] #![unstable( feature = "profiler_runtime_lib", From 972663d8ec0ee88b8a0abee4054757ad10223450 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 23 Nov 2024 16:24:23 +1100 Subject: [PATCH 5/6] Allow injecting a profiler runtime into `#![no_core]` crates Now that the profiler runtime is itself `#![no_core]`, it can be a dependency of other no_core crates, including core. --- compiler/rustc_metadata/messages.ftl | 3 --- compiler/rustc_metadata/src/creader.rs | 8 ++------ compiler/rustc_metadata/src/errors.rs | 4 ---- 3 files changed, 2 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_metadata/messages.ftl b/compiler/rustc_metadata/messages.ftl index d80aa0cc4f414..6d7d88fa8d7af 100644 --- a/compiler/rustc_metadata/messages.ftl +++ b/compiler/rustc_metadata/messages.ftl @@ -233,9 +233,6 @@ metadata_prev_alloc_error_handler = metadata_prev_global_alloc = previous global allocator defined here -metadata_profiler_builtins_needs_core = - `profiler_builtins` crate (required by compiler options) is not compatible with crate attribute `#![no_core]` - metadata_raw_dylib_no_nul = link name must not contain NUL characters if link kind is `raw-dylib` diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 6c866e72ca3a3..d1e401fbb75e9 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -799,7 +799,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { self.inject_dependency_if(cnum, "a panic runtime", &|data| data.needs_panic_runtime()); } - fn inject_profiler_runtime(&mut self, krate: &ast::Crate) { + fn inject_profiler_runtime(&mut self) { let needs_profiler_runtime = self.sess.instrument_coverage() || self.sess.opts.cg.profile_generate.enabled(); if !needs_profiler_runtime || self.sess.opts.unstable_opts.no_profiler_runtime { @@ -809,10 +809,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { info!("loading profiler"); let name = Symbol::intern(&self.sess.opts.unstable_opts.profiler_runtime); - if name == sym::profiler_builtins && attr::contains_name(&krate.attrs, sym::no_core) { - self.dcx().emit_err(errors::ProfilerBuiltinsNeedsCore); - } - let Some(cnum) = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit) else { return; }; @@ -1046,7 +1042,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { pub fn postprocess(&mut self, krate: &ast::Crate) { self.inject_forced_externs(); - self.inject_profiler_runtime(krate); + self.inject_profiler_runtime(); self.inject_allocator_crate(krate); self.inject_panic_runtime(krate); diff --git a/compiler/rustc_metadata/src/errors.rs b/compiler/rustc_metadata/src/errors.rs index 16684ae6f263a..b6c5619ec18ae 100644 --- a/compiler/rustc_metadata/src/errors.rs +++ b/compiler/rustc_metadata/src/errors.rs @@ -339,10 +339,6 @@ pub struct NoPanicStrategy { pub strategy: PanicStrategy, } -#[derive(Diagnostic)] -#[diag(metadata_profiler_builtins_needs_core)] -pub struct ProfilerBuiltinsNeedsCore; - #[derive(Diagnostic)] #[diag(metadata_not_profiler_runtime)] pub struct NotProfilerRuntime { From 6798ecaf1000c223378151023b8eb37f3464af29 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 23 Nov 2024 18:05:39 +1100 Subject: [PATCH 6/6] Coverage test for allowing coverage in a `#![no_core]` crate --- tests/coverage/no-core.cov-map | 9 +++++++++ tests/coverage/no-core.coverage | 13 +++++++++++++ tests/coverage/no-core.rs | 12 ++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 tests/coverage/no-core.cov-map create mode 100644 tests/coverage/no-core.coverage create mode 100644 tests/coverage/no-core.rs diff --git a/tests/coverage/no-core.cov-map b/tests/coverage/no-core.cov-map new file mode 100644 index 0000000000000..3a1ca4745c73f --- /dev/null +++ b/tests/coverage/no-core.cov-map @@ -0,0 +1,9 @@ +Function name: no_core::main +Raw bytes (9): 0x[01, 01, 00, 01, 01, 0c, 01, 00, 0d] +Number of files: 1 +- file 0 => global file 1 +Number of expressions: 0 +Number of file 0 mappings: 1 +- Code(Counter(0)) at (prev + 12, 1) to (start + 0, 13) +Highest counter ID seen: c0 + diff --git a/tests/coverage/no-core.coverage b/tests/coverage/no-core.coverage new file mode 100644 index 0000000000000..8b89060956879 --- /dev/null +++ b/tests/coverage/no-core.coverage @@ -0,0 +1,13 @@ + LL| |#![feature(no_core)] + LL| |#![no_core] + LL| |//@ edition: 2021 + LL| | + LL| |// Test that coverage instrumentation works for `#![no_core]` crates. + LL| | + LL| |// For this test, we pull in std anyway, to avoid having to set up our own + LL| |// no-core or no-std environment. What's important is that the compiler allows + LL| |// coverage for a crate with the `#![no_core]` annotation. + LL| |extern crate std; + LL| | + LL| 1|fn main() {} + diff --git a/tests/coverage/no-core.rs b/tests/coverage/no-core.rs new file mode 100644 index 0000000000000..206222902fc94 --- /dev/null +++ b/tests/coverage/no-core.rs @@ -0,0 +1,12 @@ +#![feature(no_core)] +#![no_core] +//@ edition: 2021 + +// Test that coverage instrumentation works for `#![no_core]` crates. + +// For this test, we pull in std anyway, to avoid having to set up our own +// no-core or no-std environment. What's important is that the compiler allows +// coverage for a crate with the `#![no_core]` annotation. +extern crate std; + +fn main() {}