Skip to content

Allow MIR pass UnreachableProp when coverage is enabled #116938

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

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 2 additions & 3 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
pub mod query;

mod counters;
mod graph;
pub mod query;
pub(crate) mod repair;
mod spans;

#[cfg(test)]
mod tests;

Expand Down
58 changes: 58 additions & 0 deletions compiler/rustc_mir_transform/src/coverage/repair.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use rustc_middle::mir::coverage::{CounterId, CoverageKind};
use rustc_middle::mir::{
self, Coverage, MirPass, SourceInfo, Statement, StatementKind, START_BLOCK,
};
use rustc_middle::ty::TyCtxt;
use rustc_span::DUMMY_SP;

/// If a function has been [instrumented for coverage](super::InstrumentCoverage),
/// but MIR optimizations subsequently remove all of its [`CoverageKind::CounterIncrement`]
/// statements (e.g. because bb0 is unreachable), then we won't generate any
/// `llvm.instrprof.increment` intrinsics. LLVM will think the function is not
/// instrumented, and it will disappear from coverage mappings and coverage reports.
///
/// This pass detects when that has happened, and re-inserts a dummy counter-increment
/// statement so that LLVM knows to treat the function as instrumented.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should be done by codegen directly. When starting codegen for a mono-item, check if function_coverage_info is set, and add this logic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that sounds reasonable. I'll have to track down the right place to add a hook, but once I do the result should be more self-contained in codegen.

pub struct RepairCoverage;

impl<'tcx> MirPass<'tcx> for RepairCoverage {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.instrument_coverage()
}

fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) {
// If a function wasn't instrumented for coverage in the first place,
// then there's no need to repair anything.
if body.function_coverage_info.is_none() {
return;
}

// If the body still contains one or more counter-increment statements,
// there's no need to repair anything.
let has_counter_increment = body
.basic_blocks
.iter()
.flat_map(|bb_data| &bb_data.statements)
.filter_map(|statement| match statement.kind {
StatementKind::Coverage(box ref coverage) => Some(coverage),
_ => None,
})
.any(|coverage| matches!(coverage.kind, CoverageKind::CounterIncrement { .. }));
if has_counter_increment {
return;
}

debug!(
"all counter-increments were removed after instrumentation; restoring one counter in {:?}",
body.source.def_id(),
);

let statement = Statement {
source_info: SourceInfo::outermost(DUMMY_SP),
kind: StatementKind::Coverage(Box::new(Coverage {
kind: CoverageKind::CounterIncrement { id: CounterId::START },
})),
};
body[START_BLOCK].statements.insert(0, statement);
}
}
3 changes: 3 additions & 0 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&large_enums::EnumSizeOpt { discrepancy: 128 },
// Some cleanup necessary at least for LLVM and potentially other codegen backends.
&add_call_guards::CriticalCallEdges,
// If MIR optimizations removed all coverage-increment statements
// from an instrumented function, add another one to avoid problems.
&coverage::repair::RepairCoverage,
// Cleanup for human readability, off by default.
&prettify::ReorderBasicBlocks,
&prettify::ReorderLocals,
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_mir_transform/src/unreachable_prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ pub struct UnreachablePropagation;
impl MirPass<'_> for UnreachablePropagation {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
// Enable only under -Zmir-opt-level=2 as this can make programs less debuggable.

// FIXME(#116171) Coverage gets confused by MIR passes that can remove all
// coverage statements from an instrumented function. This pass can be
// re-enabled when coverage codegen is robust against that happening.
sess.mir_opt_level() >= 2 && !sess.instrument_coverage()
sess.mir_opt_level() >= 2
}

fn run_pass<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
Expand Down
2 changes: 1 addition & 1 deletion tests/coverage-map/unreachable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(core_intrinsics)]
#![feature(coverage_attribute)]
// compile-flags: --edition=2021
// compile-flags: --edition=2021 -Copt-level=2

// <https://github.com/rust-lang/rust/issues/116171>
// If we instrument a function for coverage, but all of its counter-increment
Expand Down
2 changes: 1 addition & 1 deletion tests/run-coverage/unreachable.coverage
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
LL| |#![feature(core_intrinsics)]
LL| |#![feature(coverage_attribute)]
LL| |// compile-flags: --edition=2021
LL| |// compile-flags: --edition=2021 -Copt-level=2
LL| |
LL| |// <https://github.com/rust-lang/rust/issues/116171>
LL| |// If we instrument a function for coverage, but all of its counter-increment
Expand Down
2 changes: 1 addition & 1 deletion tests/run-coverage/unreachable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(core_intrinsics)]
#![feature(coverage_attribute)]
// compile-flags: --edition=2021
// compile-flags: --edition=2021 -Copt-level=2

// <https://github.com/rust-lang/rust/issues/116171>
// If we instrument a function for coverage, but all of its counter-increment
Expand Down