Skip to content

Commit 2393a06

Browse files
committed
coverage: Only generate a CGU's covmap record if it has covfun records
1 parent 6a8c016 commit 2393a06

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
8080
let filenames_val = cx.const_bytes(&filenames_buffer);
8181
let filenames_ref = llvm_cov::hash_bytes(&filenames_buffer);
8282

83-
// Generate the coverage map header, which contains the filenames used by
84-
// this CGU's coverage mappings, and store it in a well-known global.
85-
generate_covmap_record(cx, covmap_version, filenames_size, filenames_val);
86-
8783
let mut unused_function_names = Vec::new();
8884

8985
let covfun_records = function_coverage_map
@@ -93,6 +89,12 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
9389
})
9490
.collect::<Vec<_>>();
9591

92+
// If there are no covfun records for this CGU, don't generate a covmap record.
93+
// This should prevent a repeat of <https://github.com/rust-lang/rust/issues/133606>.
94+
if covfun_records.is_empty() {
95+
return;
96+
}
97+
9698
for covfun in &covfun_records {
9799
unused_function_names.extend(covfun.mangled_function_name_if_unused());
98100

@@ -117,6 +119,10 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
117119
llvm::set_linkage(array, llvm::Linkage::InternalLinkage);
118120
llvm::set_initializer(array, initializer);
119121
}
122+
123+
// Generate the coverage map header, which contains the filenames used by
124+
// this CGU's coverage mappings, and store it in a well-known global.
125+
generate_covmap_record(cx, covmap_version, filenames_size, filenames_val);
120126
}
121127

122128
/// Maps "global" (per-CGU) file ID numbers to their underlying filenames.

tests/codegen/instrument-coverage/testprog.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ fn main() {
7373

7474
// WIN: $__llvm_profile_runtime_user = comdat any
7575

76-
// CHECK: @__llvm_coverage_mapping = private constant
77-
// CHECK-SAME: section "[[INSTR_PROF_COVMAP]]", align 8
78-
7976
// CHECK: @__covrec_{{[A-F0-9]+}}u = linkonce_odr hidden constant
8077
// CHECK-SAME: section "[[INSTR_PROF_COVFUN]]"[[COMDAT_IF_SUPPORTED]], align 8
8178

79+
// CHECK: @__llvm_coverage_mapping = private constant
80+
// CHECK-SAME: section "[[INSTR_PROF_COVMAP]]", align 8
81+
8282
// WIN: @__llvm_profile_runtime = external{{.*}}global i32
8383

8484
// CHECK: @__profc__R{{[a-zA-Z0-9_]+}}testprog14will_be_called = {{private|internal}} global

0 commit comments

Comments
 (0)