Skip to content

Commit db35232

Browse files
committed
coverage: Consolidate creation of __llvm_covmap records
The code for creating these records was spread across multiple functions and multiple files, so this change moves as much of it as possible into one place.
1 parent 639d00c commit db35232

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+28-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::coverageinfo::map_data::{FunctionCoverage, FunctionCoverageCollector}
55
use crate::llvm;
66

77
use itertools::Itertools as _;
8-
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods};
8+
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods, StaticMethods};
99
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
1010
use rustc_hir::def::DefKind;
1111
use rustc_hir::def_id::DefId;
@@ -70,14 +70,9 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
7070

7171
// Encode all filenames referenced by counters/expressions in this module
7272
let filenames_buffer = global_file_table.filenames_buffer();
73-
let filenames_size = filenames_buffer.len();
74-
let filenames_val = cx.const_bytes(filenames_buffer);
73+
save_covmap_filenames_record(cx, version, filenames_buffer);
7574
let filenames_ref = coverageinfo::hash_bytes(filenames_buffer);
7675

77-
// Generate the LLVM IR representation of the coverage map and store it in a well-known global
78-
let cov_data_val = generate_coverage_map(cx, version, filenames_size, filenames_val);
79-
coverageinfo::save_cov_data_to_mod(cx, cov_data_val);
80-
8176
let mut unused_function_names = Vec::new();
8277
let covfun_section_name = coverageinfo::covfun_section_name(cx);
8378

@@ -288,15 +283,8 @@ fn encode_mappings_for_function(
288283
})
289284
}
290285

291-
/// Construct coverage map header and the array of function records, and combine them into the
292-
/// coverage map. Save the coverage map data into the LLVM IR as a static global using a
293-
/// specific, well-known section and name.
294-
fn generate_coverage_map<'ll>(
295-
cx: &CodegenCx<'ll, '_>,
296-
version: u32,
297-
filenames_size: usize,
298-
filenames_val: &'ll llvm::Value,
299-
) -> &'ll llvm::Value {
286+
fn save_covmap_filenames_record(cx: &CodegenCx<'_, '_>, version: u32, filenames_buffer: &[u8]) {
287+
let filenames_size = filenames_buffer.len();
300288
debug!("cov map: filenames_size = {}, 0-based version = {}", filenames_size, version);
301289

302290
// Create the coverage data header (Note, fields 0 and 2 are now always zero,
@@ -311,7 +299,30 @@ fn generate_coverage_map<'ll>(
311299
);
312300

313301
// Create the complete LLVM coverage data value to add to the LLVM IR
314-
cx.const_struct(&[cov_data_header_val, filenames_val], /*packed=*/ false)
302+
let covmap_val = cx.const_struct(
303+
&[cov_data_header_val, cx.const_bytes(filenames_buffer)],
304+
/*packed=*/ false,
305+
);
306+
307+
let covmap_var_name = llvm::build_string(|s| unsafe {
308+
llvm::LLVMRustCoverageWriteMappingVarNameToString(s);
309+
})
310+
.expect("Rust Coverage Mapping var name failed UTF-8 conversion");
311+
debug!("covmap var name: {:?}", covmap_var_name);
312+
313+
let covmap_section_name = llvm::build_string(|s| unsafe {
314+
llvm::LLVMRustCoverageWriteMapSectionNameToString(cx.llmod, s);
315+
})
316+
.expect("Rust Coverage section name failed UTF-8 conversion");
317+
debug!("covmap section name: {:?}", covmap_section_name);
318+
319+
let llglobal = llvm::add_global(cx.llmod, cx.val_ty(covmap_val), &covmap_var_name);
320+
llvm::set_initializer(llglobal, covmap_val);
321+
llvm::set_global_constant(llglobal, true);
322+
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
323+
llvm::set_section(llglobal, &covmap_section_name);
324+
llvm::set_alignment(llglobal, coverageinfo::VAR_ALIGN_BYTES);
325+
cx.add_used_global(llglobal);
315326
}
316327

317328
/// Construct a function record and combine it with the function's coverage mapping data.

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

-25
Original file line numberDiff line numberDiff line change
@@ -203,31 +203,6 @@ pub(crate) fn mapping_version() -> u32 {
203203
unsafe { llvm::LLVMRustCoverageMappingVersion() }
204204
}
205205

206-
pub(crate) fn save_cov_data_to_mod<'ll, 'tcx>(
207-
cx: &CodegenCx<'ll, 'tcx>,
208-
cov_data_val: &'ll llvm::Value,
209-
) {
210-
let covmap_var_name = llvm::build_string(|s| unsafe {
211-
llvm::LLVMRustCoverageWriteMappingVarNameToString(s);
212-
})
213-
.expect("Rust Coverage Mapping var name failed UTF-8 conversion");
214-
debug!("covmap var name: {:?}", covmap_var_name);
215-
216-
let covmap_section_name = llvm::build_string(|s| unsafe {
217-
llvm::LLVMRustCoverageWriteMapSectionNameToString(cx.llmod, s);
218-
})
219-
.expect("Rust Coverage section name failed UTF-8 conversion");
220-
debug!("covmap section name: {:?}", covmap_section_name);
221-
222-
let llglobal = llvm::add_global(cx.llmod, cx.val_ty(cov_data_val), &covmap_var_name);
223-
llvm::set_initializer(llglobal, cov_data_val);
224-
llvm::set_global_constant(llglobal, true);
225-
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
226-
llvm::set_section(llglobal, &covmap_section_name);
227-
llvm::set_alignment(llglobal, VAR_ALIGN_BYTES);
228-
cx.add_used_global(llglobal);
229-
}
230-
231206
pub(crate) fn save_func_record_to_mod<'ll, 'tcx>(
232207
cx: &CodegenCx<'ll, 'tcx>,
233208
covfun_section_name: &str,

0 commit comments

Comments
 (0)