-
Notifications
You must be signed in to change notification settings - Fork 13k
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
add spans to injected coverage counters, extract with CoverageData query #73684
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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,126 @@ | ||
use crate::builder::Builder; | ||
use crate::common::CodegenCx; | ||
use log::debug; | ||
use rustc_codegen_ssa::coverageinfo::map::*; | ||
use rustc_codegen_ssa::traits::{CoverageInfoBuilderMethods, CoverageInfoMethods}; | ||
use rustc_data_structures::fx::FxHashMap; | ||
use rustc_middle::ty::Instance; | ||
|
||
use std::cell::RefCell; | ||
|
||
/// A context object for maintaining all state needed by the coverageinfo module. | ||
pub struct CrateCoverageContext<'tcx> { | ||
// Coverage region data for each instrumented function identified by DefId. | ||
pub(crate) coverage_regions: RefCell<FxHashMap<Instance<'tcx>, FunctionCoverageRegions>>, | ||
} | ||
|
||
impl<'tcx> CrateCoverageContext<'tcx> { | ||
pub fn new() -> Self { | ||
Self { coverage_regions: Default::default() } | ||
} | ||
} | ||
|
||
/// Generates and exports the Coverage Map. | ||
// FIXME(richkadel): Actually generate and export the coverage map to LLVM. | ||
// The current implementation is actually just debug messages to show the data is available. | ||
pub fn finalize(cx: &CodegenCx<'_, '_>) { | ||
let coverage_regions = &*cx.coverage_context().coverage_regions.borrow(); | ||
for instance in coverage_regions.keys() { | ||
let coverageinfo = cx.tcx.coverageinfo(instance.def_id()); | ||
debug_assert!(coverageinfo.num_counters > 0); | ||
debug!( | ||
"Generate coverage map for: {:?}, hash: {}, num_counters: {}", | ||
instance, coverageinfo.hash, coverageinfo.num_counters | ||
); | ||
let function_coverage_regions = &coverage_regions[instance]; | ||
for (index, region) in function_coverage_regions.indexed_regions() { | ||
match region.kind { | ||
CoverageKind::Counter => debug!( | ||
" Counter {}, for {}..{}", | ||
index, region.coverage_span.start_byte_pos, region.coverage_span.end_byte_pos | ||
), | ||
CoverageKind::CounterExpression(lhs, op, rhs) => debug!( | ||
" CounterExpression {} = {} {:?} {}, for {}..{}", | ||
index, | ||
lhs, | ||
op, | ||
rhs, | ||
region.coverage_span.start_byte_pos, | ||
region.coverage_span.end_byte_pos | ||
), | ||
} | ||
} | ||
for unreachable in function_coverage_regions.unreachable_regions() { | ||
debug!( | ||
" Unreachable code region: {}..{}", | ||
unreachable.start_byte_pos, unreachable.end_byte_pos | ||
); | ||
} | ||
} | ||
} | ||
|
||
impl CoverageInfoMethods for CodegenCx<'ll, 'tcx> { | ||
fn coverageinfo_finalize(&self) { | ||
finalize(self) | ||
} | ||
} | ||
|
||
impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> { | ||
fn add_counter_region( | ||
&mut self, | ||
instance: Instance<'tcx>, | ||
index: u32, | ||
start_byte_pos: u32, | ||
end_byte_pos: u32, | ||
) { | ||
debug!( | ||
"adding counter to coverage map: instance={:?}, index={}, byte range {}..{}", | ||
instance, index, start_byte_pos, end_byte_pos, | ||
); | ||
let mut coverage_regions = self.coverage_context().coverage_regions.borrow_mut(); | ||
coverage_regions.entry(instance).or_default().add_counter( | ||
index, | ||
start_byte_pos, | ||
end_byte_pos, | ||
); | ||
} | ||
|
||
fn add_counter_expression_region( | ||
&mut self, | ||
instance: Instance<'tcx>, | ||
index: u32, | ||
lhs: u32, | ||
op: CounterOp, | ||
rhs: u32, | ||
start_byte_pos: u32, | ||
end_byte_pos: u32, | ||
) { | ||
debug!( | ||
"adding counter expression to coverage map: instance={:?}, index={}, {} {:?} {}, byte range {}..{}", | ||
instance, index, lhs, op, rhs, start_byte_pos, end_byte_pos, | ||
); | ||
let mut coverage_regions = self.coverage_context().coverage_regions.borrow_mut(); | ||
coverage_regions.entry(instance).or_default().add_counter_expression( | ||
index, | ||
lhs, | ||
op, | ||
rhs, | ||
start_byte_pos, | ||
end_byte_pos, | ||
); | ||
} | ||
|
||
fn add_unreachable_region( | ||
&mut self, | ||
instance: Instance<'tcx>, | ||
start_byte_pos: u32, | ||
end_byte_pos: u32, | ||
) { | ||
debug!( | ||
"adding unreachable code to coverage map: instance={:?}, byte range {}..{}", | ||
instance, start_byte_pos, end_byte_pos, | ||
); | ||
let mut coverage_regions = self.coverage_context().coverage_regions.borrow_mut(); | ||
coverage_regions.entry(instance).or_default().add_unreachable(start_byte_pos, end_byte_pos); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.
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.
Why not add the code to
codegen_intrinsic_call
? You could just codegen nothing there.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 need to extract the values from the const u32 operands, and for the non-codegened intrinsics, I didn't like the unnecessary overhead of converting them to LLVM values (
OperandRef
s) and back again for no reason. Sois_codegen_intrinsic
takes therustc_middle::mir::Operand
s directly, before the args are converted to the backend.Plus, for the intrinsics that are not passed through to
codegen_intrinsic_call
, this also avoids all of the other steps between the call tois_codegen_intrinsic()
andcodegen_intrinsic_call()
inblock.rs
, and all of the prepwork in the implementation ofcodegen_intrinsic_call()
prior tomatch name {
. That's a lot of code that I can avoid calling by implementing it this way.