Skip to content

Commit 24e34e2

Browse files
authored
Rollup merge of #114008 - Zalathar:covfun-section-name, r=cjgillot
coverage: Obtain the `__llvm_covfun` section name outside a per-function loop This section name is always constant for a given target, but obtaining it from LLVM requires a few intermediate allocations. There's no need to do so repeatedly from inside a per-function loop.
2 parents 4fc6b33 + 01f3cc1 commit 24e34e2

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
100100
// Generate the LLVM IR representation of the coverage map and store it in a well-known global
101101
let cov_data_val = mapgen.generate_coverage_map(cx, version, filenames_size, filenames_val);
102102

103+
let covfun_section_name = coverageinfo::covfun_section_name(cx);
103104
for (mangled_function_name, source_hash, is_used, coverage_mapping_buffer) in function_data {
104105
save_function_record(
105106
cx,
107+
&covfun_section_name,
106108
mangled_function_name,
107109
source_hash,
108110
filenames_ref,
@@ -228,6 +230,7 @@ impl CoverageMapGenerator {
228230
/// specific, well-known section and name.
229231
fn save_function_record(
230232
cx: &CodegenCx<'_, '_>,
233+
covfun_section_name: &str,
231234
mangled_function_name: &str,
232235
source_hash: u64,
233236
filenames_ref: u64,
@@ -254,7 +257,13 @@ fn save_function_record(
254257
/*packed=*/ true,
255258
);
256259

257-
coverageinfo::save_func_record_to_mod(cx, func_name_hash, func_record_val, is_used);
260+
coverageinfo::save_func_record_to_mod(
261+
cx,
262+
covfun_section_name,
263+
func_name_hash,
264+
func_record_val,
265+
is_used,
266+
);
258267
}
259268

260269
/// When finalizing the coverage map, `FunctionCoverage` only has the `CodeRegion`s and counters for

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ pub(crate) fn save_cov_data_to_mod<'ll, 'tcx>(
408408

409409
pub(crate) fn save_func_record_to_mod<'ll, 'tcx>(
410410
cx: &CodegenCx<'ll, 'tcx>,
411+
covfun_section_name: &str,
411412
func_name_hash: u64,
412413
func_record_val: &'ll llvm::Value,
413414
is_used: bool,
@@ -423,20 +424,33 @@ pub(crate) fn save_func_record_to_mod<'ll, 'tcx>(
423424
let func_record_var_name =
424425
format!("__covrec_{:X}{}", func_name_hash, if is_used { "u" } else { "" });
425426
debug!("function record var name: {:?}", func_record_var_name);
426-
427-
let func_record_section_name = llvm::build_string(|s| unsafe {
428-
llvm::LLVMRustCoverageWriteFuncSectionNameToString(cx.llmod, s);
429-
})
430-
.expect("Rust Coverage function record section name failed UTF-8 conversion");
431-
debug!("function record section name: {:?}", func_record_section_name);
427+
debug!("function record section name: {:?}", covfun_section_name);
432428

433429
let llglobal = llvm::add_global(cx.llmod, cx.val_ty(func_record_val), &func_record_var_name);
434430
llvm::set_initializer(llglobal, func_record_val);
435431
llvm::set_global_constant(llglobal, true);
436432
llvm::set_linkage(llglobal, llvm::Linkage::LinkOnceODRLinkage);
437433
llvm::set_visibility(llglobal, llvm::Visibility::Hidden);
438-
llvm::set_section(llglobal, &func_record_section_name);
434+
llvm::set_section(llglobal, covfun_section_name);
439435
llvm::set_alignment(llglobal, VAR_ALIGN_BYTES);
440436
llvm::set_comdat(cx.llmod, llglobal, &func_record_var_name);
441437
cx.add_used_global(llglobal);
442438
}
439+
440+
/// Returns the section name string to pass through to the linker when embedding
441+
/// per-function coverage information in the object file, according to the target
442+
/// platform's object file format.
443+
///
444+
/// LLVM's coverage tools read coverage mapping details from this section when
445+
/// producing coverage reports.
446+
///
447+
/// Typical values are:
448+
/// - `__llvm_covfun` on Linux
449+
/// - `__LLVM_COV,__llvm_covfun` on macOS (includes `__LLVM_COV,` segment prefix)
450+
/// - `.lcovfun$M` on Windows (includes `$M` sorting suffix)
451+
pub(crate) fn covfun_section_name(cx: &CodegenCx<'_, '_>) -> String {
452+
llvm::build_string(|s| unsafe {
453+
llvm::LLVMRustCoverageWriteFuncSectionNameToString(cx.llmod, s);
454+
})
455+
.expect("Rust Coverage function record section name failed UTF-8 conversion")
456+
}

0 commit comments

Comments
 (0)