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

Fixed coverage map issues; better aligned with LLVM APIs #74733

Merged
merged 4 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions src/librustc_codegen_llvm/coverageinfo/mapgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::ffi::CString;
/// replicated for Rust's Coverage Map.
pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) {
let function_coverage_map = cx.coverage_context().take_function_coverage_map();
if function_coverage_map.len() == 0 {
if function_coverage_map.is_empty() {
// This module has no functions with coverage instrumentation
return;
}
Expand Down Expand Up @@ -81,7 +81,7 @@ struct CoverageMapGenerator {

impl CoverageMapGenerator {
fn new() -> Self {
Self { filenames: Vec::new(), filename_to_index: FxHashMap::<CString, u32>::default() }
Self { filenames: Vec::new(), filename_to_index: FxHashMap::default() }
}

/// Using the `expressions` and `counter_regions` collected for the current function, generate
Expand All @@ -95,7 +95,7 @@ impl CoverageMapGenerator {
coverage_mappings_buffer: &RustString,
) {
let mut counter_regions = counter_regions.collect::<Vec<_>>();
if counter_regions.len() == 0 {
if counter_regions.is_empty() {
return;
}

Expand All @@ -109,7 +109,7 @@ impl CoverageMapGenerator {
// `file_id` (indexing files referenced by the current function), and construct the
// function-specific `virtual_file_mapping` from `file_id` to its index in the module's
// `filenames` array.
counter_regions.sort_by_key(|(_counter, region)| *region);
counter_regions.sort_unstable_by_key(|(_counter, region)| *region);
for (counter, region) in counter_regions {
let (file_path, start_line, start_col, end_line, end_col) = region.file_start_and_end();
let same_file = current_file_path.as_ref().map_or(false, |p| p == file_path);
Expand Down
25 changes: 13 additions & 12 deletions src/librustc_codegen_llvm/coverageinfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,36 +110,37 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
}
}

/// Aligns to C++ struct llvm::coverage::Counter::CounterKind.
/// The order of discrimiators is important.
/// Aligns with [llvm::coverage::CounterMappingRegion::RegionKind](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L205-L221)
#[derive(Copy, Clone, Debug)]
#[repr(C)]
enum RegionKind {
/// A CodeRegion associates some code with a counter
CodeRegion,
CodeRegion = 0,

/// An ExpansionRegion represents a file expansion region that associates
/// a source range with the expansion of a virtual source file, such as
/// for a macro instantiation or #include file.
ExpansionRegion,
ExpansionRegion = 1,

/// A SkippedRegion represents a source range with code that was skipped
/// by a preprocessor or similar means.
SkippedRegion,
SkippedRegion = 2,

/// A GapRegion is like a CodeRegion, but its count is only set as the
/// line execution count when its the only region in the line.
GapRegion,
GapRegion = 3,
}

/// This struct provides LLVM's representation of a "CoverageMappingRegion", encoded into the
/// coverage map in accordance with LLVM's "Coverage Mapping Format". The struct composes fields
/// representing the `Counter` type and value(s) (injected counter ID, or expression type and
/// operands), the source file (an indirect index into a "filenames array", encoded separately),
/// and source location (start and end positions of the represented code region).
/// coverage map, in accordance with the
/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/llvmorg-8.0.0/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
/// The struct composes fields representing the `Counter` type and value(s) (injected counter ID,
/// or expression type and operands), the source file (an indirect index into a "filenames array",
/// encoded separately), and source location (start and end positions of the represented code
/// region).
///
/// Aligns to C++ struct llvm::coverage::CounterMappingRegion.
/// The order of fields is important.
/// Aligns with [llvm::coverage::CounterMappingRegion](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L223-L226)
/// Important: The Rust struct layout (order and types of fields) must match its C++ counterpart.
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct CounterMappingRegion {
Expand Down
32 changes: 16 additions & 16 deletions src/librustc_codegen_ssa/coverageinfo/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@ use std::cmp::{Ord, Ordering};
use std::fmt;
use std::path::PathBuf;

/// Aligns to C++ struct llvm::coverage::Counter::CounterKind.
/// The order of discriminators is important.
/// Aligns with [llvm::coverage::Counter::CounterKind](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L91)
#[derive(Copy, Clone, Debug)]
#[repr(C)]
enum CounterKind {
Zero,
CounterValueReference,
Expression,
Zero = 0,
CounterValueReference = 1,
Expression = 2,
}

/// Aligns to C++ struct llvm::coverage::Counter. Note that `id` has
/// different interpretations, depending on the `kind`:
/// A reference to an instance of an abstract "counter" that will yield a value in a coverage
/// report. Note that `id` has different interpretations, depending on the `kind`:
/// * For `CounterKind::Zero`, `id` is assumed to be `0`
/// * For `CounterKind::CounterValueReference`, `id` matches the `counter_id` of the injected
/// instrumentation counter (the `index` argument to the LLVM intrinsic `instrprof.increment()`)
/// * For `CounterKind::Expression`, `id` is the index into the array of counter expressions.
/// The order of fields is important.
/// * For `CounterKind::Expression`, `id` is the index into the coverage map's array of counter
/// expressions.
/// Aligns with [llvm::coverage::Counter](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L98-L99)
/// Important: The Rust struct layout (order and types of fields) must match its C++ counterpart.
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct Counter {
// Important: The layout (order and types of fields) must match its C++ counterpart.
kind: CounterKind,
id: u32,
}
Expand All @@ -45,21 +47,19 @@ impl Counter {
}
}

/// Aligns to C++ struct llvm::coverage::CounterExpression::ExprKind.
/// The order of discriminators is important.
/// Aligns with [llvm::coverage::CounterExpression::ExprKind](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L146)
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub enum ExprKind {
Subtract,
Add,
Subtract = 0,
Add = 1,
}

/// Aligns to C++ struct llvm::coverage::CounterExpression.
/// The order of fields is important.
/// Aligns with [llvm::coverage::CounterExpression](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L147-L148)
/// Important: The Rust struct layout (order and types of fields) must match its C++ counterpart.
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct CounterExpression {
// Note the field order is important.
kind: ExprKind,
lhs: Counter,
rhs: Counter,
Expand Down
Loading