Skip to content

Commit

Permalink
coverage: Reify CovfunRecord as an intermediate step
Browse files Browse the repository at this point in the history
  • Loading branch information
Zalathar committed Dec 11, 2024
1 parent 7c4ac71 commit 6a8c016
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
22 changes: 12 additions & 10 deletions compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use tracing::debug;
use crate::common::CodegenCx;
use crate::coverageinfo::llvm_cov;
use crate::coverageinfo::map_data::FunctionCoverage;
use crate::coverageinfo::mapgen::covfun::prepare_covfun_record;
use crate::llvm;

mod covfun;
Expand Down Expand Up @@ -85,16 +86,17 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {

let mut unused_function_names = Vec::new();

// Encode coverage mappings and generate function records
for (instance, function_coverage) in function_coverage_map {
covfun::prepare_and_generate_covfun_record(
cx,
&global_file_table,
filenames_ref,
&mut unused_function_names,
instance,
&function_coverage,
);
let covfun_records = function_coverage_map
.into_iter()
.filter_map(|(instance, function_coverage)| {
prepare_covfun_record(tcx, &global_file_table, instance, &function_coverage)
})
.collect::<Vec<_>>();

for covfun in &covfun_records {
unused_function_names.extend(covfun.mangled_function_name_if_unused());

covfun::generate_covfun_record(cx, filenames_ref, covfun)
}

// For unused functions, we need to take their mangled names and store them
Expand Down
59 changes: 33 additions & 26 deletions compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,30 @@ use crate::coverageinfo::mapgen::{GlobalFileTable, VirtualFileMapping, span_file
use crate::coverageinfo::{ffi, llvm_cov};
use crate::llvm;

pub(crate) fn prepare_and_generate_covfun_record<'ll, 'tcx>(
cx: &CodegenCx<'ll, 'tcx>,
/// Intermediate coverage metadata for a single function, used to help build
/// the final record that will be embedded in the `__llvm_covfun` section.
#[derive(Debug)]
pub(crate) struct CovfunRecord<'tcx> {
mangled_function_name: &'tcx str,
source_hash: u64,
is_used: bool,
coverage_mapping_buffer: Vec<u8>,
}

impl<'tcx> CovfunRecord<'tcx> {
/// FIXME(Zalathar): Make this the responsibility of the code that determines
/// which functions are unused.
pub(crate) fn mangled_function_name_if_unused(&self) -> Option<&'tcx str> {
(!self.is_used).then_some(self.mangled_function_name)
}
}

pub(crate) fn prepare_covfun_record<'tcx>(
tcx: TyCtxt<'tcx>,
global_file_table: &GlobalFileTable,
filenames_ref: u64,
unused_function_names: &mut Vec<&'tcx str>,
instance: Instance<'tcx>,
function_coverage: &FunctionCoverage<'tcx>,
) {
let tcx = cx.tcx;

) -> Option<CovfunRecord<'tcx>> {
let mangled_function_name = tcx.symbol_name(instance).name;
let source_hash = function_coverage.source_hash();
let is_used = function_coverage.is_used();
Expand All @@ -47,22 +61,11 @@ pub(crate) fn prepare_and_generate_covfun_record<'ll, 'tcx>(
);
} else {
debug!("unused function had no coverage mapping data: {}", mangled_function_name);
return;
return None;
}
}

if !is_used {
unused_function_names.push(mangled_function_name);
}

generate_covfun_record(
cx,
mangled_function_name,
source_hash,
filenames_ref,
coverage_mapping_buffer,
is_used,
);
Some(CovfunRecord { mangled_function_name, source_hash, is_used, coverage_mapping_buffer })
}

/// Using the expressions and counter regions collected for a single function,
Expand Down Expand Up @@ -145,14 +148,18 @@ fn encode_mappings_for_function(
/// Generates the contents of the covfun record for this function, which
/// contains the function's coverage mapping data. The record is then stored
/// as a global variable in the `__llvm_covfun` section.
fn generate_covfun_record(
cx: &CodegenCx<'_, '_>,
mangled_function_name: &str,
source_hash: u64,
pub(crate) fn generate_covfun_record<'tcx>(
cx: &CodegenCx<'_, 'tcx>,
filenames_ref: u64,
coverage_mapping_buffer: Vec<u8>,
is_used: bool,
covfun: &CovfunRecord<'tcx>,
) {
let &CovfunRecord {
mangled_function_name,
source_hash,
is_used,
ref coverage_mapping_buffer, // Previously-encoded coverage mappings
} = covfun;

// Concatenate the encoded coverage mappings
let coverage_mapping_size = coverage_mapping_buffer.len();
let coverage_mapping_val = cx.const_bytes(&coverage_mapping_buffer);
Expand Down

0 comments on commit 6a8c016

Please sign in to comment.