-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.