Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow injecting a profiler runtime into #![no_core] crates #133369

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions compiler/rustc_metadata/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
14 changes: 5 additions & 9 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,20 +799,16 @@ 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) {
if self.sess.opts.unstable_opts.no_profiler_runtime
|| !(self.sess.instrument_coverage() || self.sess.opts.cg.profile_generate.enabled())
{
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 {
return;
}

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;
};
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_metadata/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions library/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,6 @@ name = "profiler_builtins"
version = "0.0.0"
dependencies = [
"cc",
"compiler_builtins",
"core",
]

[[package]]
Expand Down
2 changes: 0 additions & 2 deletions library/profiler_builtins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 8 additions & 4 deletions library/profiler_builtins/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#![no_std]
// tidy-alphabetical-start
#![allow(internal_features)]
#![feature(no_core)]
#![feature(profiler_runtime)]
#![feature(staged_api)]
// tidy-alphabetical-end

// Other attributes:
#![no_core]
#![profiler_runtime]
Zalathar marked this conversation as resolved.
Show resolved Hide resolved
#![unstable(
feature = "profiler_runtime_lib",
reason = "internal implementation detail of rustc right now",
issue = "none"
)]
#![allow(unused_features)]
#![allow(internal_features)]
#![feature(staged_api)]
9 changes: 9 additions & 0 deletions tests/coverage/no-core.cov-map
Original file line number Diff line number Diff line change
@@ -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

13 changes: 13 additions & 0 deletions tests/coverage/no-core.coverage
Original file line number Diff line number Diff line change
@@ -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() {}

12 changes: 12 additions & 0 deletions tests/coverage/no-core.rs
Original file line number Diff line number Diff line change
@@ -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() {}
Loading