Skip to content

Commit 1d8c381

Browse files
committed
Upgrades the coverage map to Version 4
Changes the coverage map injected into binaries compiled with `-Zinstrument-coverage` to LLVM Coverage Mapping Format, Version 4 (from Version 3). Note, binaries compiled with this version will require LLVM tools from at least LLVM Version 11.
1 parent 40624dd commit 1d8c381

File tree

8 files changed

+203
-132
lines changed

8 files changed

+203
-132
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+97-104
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::llvm;
44

55
use llvm::coverageinfo::CounterMappingRegion;
66
use rustc_codegen_ssa::coverageinfo::map::{Counter, CounterExpression};
7-
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods};
7+
use rustc_codegen_ssa::traits::ConstMethods;
88
use rustc_data_structures::fx::FxIndexSet;
99
use rustc_llvm::RustString;
1010
use rustc_middle::mir::coverage::CodeRegion;
@@ -38,46 +38,50 @@ pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) {
3838
let mut mapgen = CoverageMapGenerator::new();
3939

4040
// Encode coverage mappings and generate function records
41-
let mut function_records = Vec::<&'ll llvm::Value>::new();
42-
let coverage_mappings_buffer = llvm::build_byte_buffer(|coverage_mappings_buffer| {
43-
for (instance, function_coverage) in function_coverage_map.into_iter() {
44-
debug!("Generate coverage map for: {:?}", instance);
45-
46-
let mangled_function_name = cx.tcx.symbol_name(instance).to_string();
47-
let function_source_hash = function_coverage.source_hash();
48-
let (expressions, counter_regions) =
49-
function_coverage.get_expressions_and_counter_regions();
50-
51-
let old_len = coverage_mappings_buffer.len();
52-
mapgen.write_coverage_mappings(expressions, counter_regions, coverage_mappings_buffer);
53-
let mapping_data_size = coverage_mappings_buffer.len() - old_len;
54-
debug_assert!(
55-
mapping_data_size > 0,
56-
"Every `FunctionCoverage` should have at least one counter"
57-
);
58-
59-
let function_record = mapgen.make_function_record(
60-
cx,
61-
mangled_function_name,
62-
function_source_hash,
63-
mapping_data_size,
64-
);
65-
function_records.push(function_record);
66-
}
67-
});
41+
let mut function_data = Vec::new();
42+
for (instance, function_coverage) in function_coverage_map.into_iter() {
43+
debug!("Generate coverage map for: {:?}", instance);
44+
45+
let mangled_function_name = cx.tcx.symbol_name(instance).to_string();
46+
let function_source_hash = function_coverage.source_hash();
47+
let (expressions, counter_regions) =
48+
function_coverage.get_expressions_and_counter_regions();
49+
50+
let coverage_mapping_buffer = llvm::build_byte_buffer(|coverage_mapping_buffer| {
51+
mapgen.write_coverage_mapping(expressions, counter_regions, coverage_mapping_buffer);
52+
});
53+
debug_assert!(
54+
coverage_mapping_buffer.len() > 0,
55+
"Every `FunctionCoverage` should have at least one counter"
56+
);
57+
58+
function_data.push((mangled_function_name, function_source_hash, coverage_mapping_buffer));
59+
}
6860

6961
// Encode all filenames referenced by counters/expressions in this module
7062
let filenames_buffer = llvm::build_byte_buffer(|filenames_buffer| {
7163
coverageinfo::write_filenames_section_to_buffer(&mapgen.filenames, filenames_buffer);
7264
});
7365

66+
let filenames_size = filenames_buffer.len();
67+
let filenames_val = cx.const_bytes(&filenames_buffer[..]);
68+
let filenames_ref = coverageinfo::hash_bytes(filenames_buffer);
69+
7470
// Generate the LLVM IR representation of the coverage map and store it in a well-known global
75-
mapgen.save_generated_coverage_map(
76-
cx,
77-
function_records,
78-
filenames_buffer,
79-
coverage_mappings_buffer,
80-
);
71+
let cov_data_val = mapgen.generate_coverage_map(cx, filenames_size, filenames_val);
72+
73+
for (mangled_function_name, function_source_hash, coverage_mapping_buffer) in function_data {
74+
save_function_record(
75+
cx,
76+
mangled_function_name,
77+
function_source_hash,
78+
filenames_ref,
79+
coverage_mapping_buffer,
80+
);
81+
}
82+
83+
// Save the coverage data value to LLVM IR
84+
coverageinfo::save_cov_data_to_mod(cx, cov_data_val);
8185
}
8286

8387
struct CoverageMapGenerator {
@@ -92,12 +96,12 @@ impl CoverageMapGenerator {
9296
/// Using the `expressions` and `counter_regions` collected for the current function, generate
9397
/// the `mapping_regions` and `virtual_file_mapping`, and capture any new filenames. Then use
9498
/// LLVM APIs to encode the `virtual_file_mapping`, `expressions`, and `mapping_regions` into
95-
/// the given `coverage_mappings` byte buffer, compliant with the LLVM Coverage Mapping format.
96-
fn write_coverage_mappings(
99+
/// the given `coverage_mapping` byte buffer, compliant with the LLVM Coverage Mapping format.
100+
fn write_coverage_mapping(
97101
&mut self,
98102
expressions: Vec<CounterExpression>,
99103
counter_regions: impl Iterator<Item = (Counter, &'a CodeRegion)>,
100-
coverage_mappings_buffer: &RustString,
104+
coverage_mapping_buffer: &RustString,
101105
) {
102106
let mut counter_regions = counter_regions.collect::<Vec<_>>();
103107
if counter_regions.is_empty() {
@@ -145,89 +149,78 @@ impl CoverageMapGenerator {
145149
virtual_file_mapping,
146150
expressions,
147151
mapping_regions,
148-
coverage_mappings_buffer,
152+
coverage_mapping_buffer,
149153
);
150154
}
151155

152-
/// Generate and return the function record `Value`
153-
fn make_function_record(
154-
&mut self,
155-
cx: &CodegenCx<'ll, 'tcx>,
156-
mangled_function_name: String,
157-
function_source_hash: u64,
158-
mapping_data_size: usize,
159-
) -> &'ll llvm::Value {
160-
let name_ref = coverageinfo::compute_hash(&mangled_function_name);
161-
let name_ref_val = cx.const_u64(name_ref);
162-
let mapping_data_size_val = cx.const_u32(mapping_data_size as u32);
163-
let func_hash_val = cx.const_u64(function_source_hash);
164-
cx.const_struct(
165-
&[name_ref_val, mapping_data_size_val, func_hash_val],
166-
/*packed=*/ true,
167-
)
168-
}
169-
170-
/// Combine the filenames and coverage mappings buffers, construct coverage map header and the
171-
/// array of function records, and combine everything into the complete coverage map. Save the
172-
/// coverage map data into the LLVM IR as a static global using a specific, well-known section
173-
/// and name.
174-
fn save_generated_coverage_map(
156+
/// Construct coverage map header and the array of function records, and combine them into the
157+
/// coverage map. Save the coverage map data into the LLVM IR as a static global using a
158+
/// specific, well-known section and name.
159+
fn generate_coverage_map(
175160
self,
176161
cx: &CodegenCx<'ll, 'tcx>,
177-
function_records: Vec<&'ll llvm::Value>,
178-
filenames_buffer: Vec<u8>,
179-
mut coverage_mappings_buffer: Vec<u8>,
180-
) {
181-
// Concatenate the encoded filenames and encoded coverage mappings, and add additional zero
182-
// bytes as-needed to ensure 8-byte alignment.
183-
let mut coverage_size = coverage_mappings_buffer.len();
184-
let filenames_size = filenames_buffer.len();
185-
let remaining_bytes =
186-
(filenames_size + coverage_size) % coverageinfo::COVMAP_VAR_ALIGN_BYTES;
187-
if remaining_bytes > 0 {
188-
let pad = coverageinfo::COVMAP_VAR_ALIGN_BYTES - remaining_bytes;
189-
coverage_mappings_buffer.append(&mut [0].repeat(pad));
190-
coverage_size += pad;
191-
}
192-
let filenames_and_coverage_mappings = [filenames_buffer, coverage_mappings_buffer].concat();
193-
let filenames_and_coverage_mappings_val =
194-
cx.const_bytes(&filenames_and_coverage_mappings[..]);
195-
162+
filenames_size: usize,
163+
filenames_val: &'ll llvm::Value,
164+
) -> &'ll llvm::Value {
196165
debug!(
197-
"cov map: n_records = {}, filenames_size = {}, coverage_size = {}, 0-based version = {}",
198-
function_records.len(),
166+
"cov map: filenames_size = {}, 0-based version = {}",
199167
filenames_size,
200-
coverage_size,
201168
coverageinfo::mapping_version()
202169
);
203170

204-
// Create the coverage data header
205-
let n_records_val = cx.const_u32(function_records.len() as u32);
171+
// Create the coverage data header (Note, fields 0 and 2 are now always zero,
172+
// as of `llvm::coverage::CovMapVersion::Version4`.
173+
let zero_was_n_records_val = cx.const_u32(0);
206174
let filenames_size_val = cx.const_u32(filenames_size as u32);
207-
let coverage_size_val = cx.const_u32(coverage_size as u32);
175+
let zero_was_coverage_size_val = cx.const_u32(0 as u32);
208176
let version_val = cx.const_u32(coverageinfo::mapping_version());
209177
let cov_data_header_val = cx.const_struct(
210-
&[n_records_val, filenames_size_val, coverage_size_val, version_val],
178+
&[zero_was_n_records_val, filenames_size_val, zero_was_coverage_size_val, version_val],
211179
/*packed=*/ false,
212180
);
213181

214-
// Create the function records array
215-
let name_ref_from_u64 = cx.type_i64();
216-
let mapping_data_size_from_u32 = cx.type_i32();
217-
let func_hash_from_u64 = cx.type_i64();
218-
let function_record_ty = cx.type_struct(
219-
&[name_ref_from_u64, mapping_data_size_from_u32, func_hash_from_u64],
220-
/*packed=*/ true,
221-
);
222-
let function_records_val = cx.const_array(function_record_ty, &function_records[..]);
223-
224182
// Create the complete LLVM coverage data value to add to the LLVM IR
225-
let cov_data_val = cx.const_struct(
226-
&[cov_data_header_val, function_records_val, filenames_and_coverage_mappings_val],
227-
/*packed=*/ false,
228-
);
229-
230-
// Save the coverage data value to LLVM IR
231-
coverageinfo::save_map_to_mod(cx, cov_data_val);
183+
cx.const_struct(&[cov_data_header_val, filenames_val], /*packed=*/ false)
232184
}
233185
}
186+
187+
/// Construct a function record and combine it with the function's coverage mapping data.
188+
/// Save the function record into the LLVM IR as a static global using a
189+
/// specific, well-known section and name.
190+
fn save_function_record(
191+
cx: &CodegenCx<'ll, 'tcx>,
192+
mangled_function_name: String,
193+
function_source_hash: u64,
194+
filenames_ref: u64,
195+
coverage_mapping_buffer: Vec<u8>,
196+
) {
197+
// Concatenate the encoded coverage mappings
198+
let coverage_mapping_size = coverage_mapping_buffer.len();
199+
let coverage_mapping_val = cx.const_bytes(&coverage_mapping_buffer[..]);
200+
201+
let func_name_hash = coverageinfo::hash_str(&mangled_function_name);
202+
let func_name_hash_val = cx.const_u64(func_name_hash);
203+
let coverage_mapping_size_val = cx.const_u32(coverage_mapping_size as u32);
204+
let func_hash_val = cx.const_u64(function_source_hash);
205+
let filenames_ref_val = cx.const_u64(filenames_ref);
206+
let func_record_val = cx.const_struct(
207+
&[
208+
func_name_hash_val,
209+
coverage_mapping_size_val,
210+
func_hash_val,
211+
filenames_ref_val,
212+
coverage_mapping_val,
213+
],
214+
/*packed=*/ true,
215+
);
216+
217+
// At the present time, the coverage map for Rust assumes every instrumented function `is_used`.
218+
// Note that Clang marks functions as "unused" in `CodeGenPGO::emitEmptyCounterMapping`. (See:
219+
// https://github.com/rust-lang/llvm-project/blob/de02a75e398415bad4df27b4547c25b896c8bf3b/clang%2Flib%2FCodeGen%2FCodeGenPGO.cpp#L877-L878
220+
// for example.)
221+
//
222+
// It's not yet clear if or how this may be applied to Rust in the future, but the `is_used`
223+
// argument is available and handled similarly.
224+
let is_used = true;
225+
coverageinfo::save_func_record_to_mod(cx, func_name_hash, func_record_val, is_used);
226+
}

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+46-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use tracing::debug;
2323

2424
pub mod mapgen;
2525

26-
const COVMAP_VAR_ALIGN_BYTES: usize = 8;
26+
const VAR_ALIGN_BYTES: usize = 8;
2727

2828
/// A context object for maintaining all state needed by the coverageinfo module.
2929
pub struct CrateCoverageContext<'tcx> {
@@ -177,17 +177,20 @@ pub(crate) fn write_mapping_to_buffer(
177177
);
178178
}
179179
}
180+
pub(crate) fn hash_str(strval: &str) -> u64 {
181+
let strval = CString::new(strval).expect("null error converting hashable str to C string");
182+
unsafe { llvm::LLVMRustCoverageHashCString(strval.as_ptr()) }
183+
}
180184

181-
pub(crate) fn compute_hash(name: &str) -> u64 {
182-
let name = CString::new(name).expect("null error converting hashable name to C string");
183-
unsafe { llvm::LLVMRustCoverageComputeHash(name.as_ptr()) }
185+
pub(crate) fn hash_bytes(bytes: Vec<u8>) -> u64 {
186+
unsafe { llvm::LLVMRustCoverageHashByteArray(bytes.as_ptr().cast(), bytes.len()) }
184187
}
185188

186189
pub(crate) fn mapping_version() -> u32 {
187190
unsafe { llvm::LLVMRustCoverageMappingVersion() }
188191
}
189192

190-
pub(crate) fn save_map_to_mod<'ll, 'tcx>(
193+
pub(crate) fn save_cov_data_to_mod<'ll, 'tcx>(
191194
cx: &CodegenCx<'ll, 'tcx>,
192195
cov_data_val: &'ll llvm::Value,
193196
) {
@@ -198,16 +201,51 @@ pub(crate) fn save_map_to_mod<'ll, 'tcx>(
198201
debug!("covmap var name: {:?}", covmap_var_name);
199202

200203
let covmap_section_name = llvm::build_string(|s| unsafe {
201-
llvm::LLVMRustCoverageWriteSectionNameToString(cx.llmod, s);
204+
llvm::LLVMRustCoverageWriteMapSectionNameToString(cx.llmod, s);
202205
})
203206
.expect("Rust Coverage section name failed UTF-8 conversion");
204207
debug!("covmap section name: {:?}", covmap_section_name);
205208

206209
let llglobal = llvm::add_global(cx.llmod, cx.val_ty(cov_data_val), &covmap_var_name);
207210
llvm::set_initializer(llglobal, cov_data_val);
208211
llvm::set_global_constant(llglobal, true);
209-
llvm::set_linkage(llglobal, llvm::Linkage::InternalLinkage);
212+
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
210213
llvm::set_section(llglobal, &covmap_section_name);
211-
llvm::set_alignment(llglobal, COVMAP_VAR_ALIGN_BYTES);
214+
llvm::set_alignment(llglobal, VAR_ALIGN_BYTES);
215+
cx.add_used_global(llglobal);
216+
}
217+
218+
pub(crate) fn save_func_record_to_mod<'ll, 'tcx>(
219+
cx: &CodegenCx<'ll, 'tcx>,
220+
func_name_hash: u64,
221+
func_record_val: &'ll llvm::Value,
222+
is_used: bool,
223+
) {
224+
// Assign a name to the function record. This is used to merge duplicates.
225+
//
226+
// In LLVM, a "translation unit" (effectively, a `Crate` in Rust) can describe functions that
227+
// are included-but-not-used. If (or when) Rust generates functions that are
228+
// included-but-not-used, note that a dummy description for a function included-but-not-used
229+
// in a Crate can be replaced by full description provided by a different Crate. The two kinds
230+
// of descriptions play distinct roles in LLVM IR; therefore, assign them different names (by
231+
// appending "u" to the end of the function record var name, to prevent `linkonce_odr` merging.
232+
let func_record_var_name =
233+
format!("__covrec_{:X}{}", func_name_hash, if is_used { "u" } else { "" });
234+
debug!("function record var name: {:?}", func_record_var_name);
235+
236+
let func_record_section_name = llvm::build_string(|s| unsafe {
237+
llvm::LLVMRustCoverageWriteFuncSectionNameToString(cx.llmod, s);
238+
})
239+
.expect("Rust Coverage function record section name failed UTF-8 conversion");
240+
debug!("function record section name: {:?}", func_record_section_name);
241+
242+
let llglobal = llvm::add_global(cx.llmod, cx.val_ty(func_record_val), &func_record_var_name);
243+
llvm::set_initializer(llglobal, func_record_val);
244+
llvm::set_global_constant(llglobal, true);
245+
llvm::set_linkage(llglobal, llvm::Linkage::LinkOnceODRLinkage);
246+
llvm::set_visibility(llglobal, llvm::Visibility::Hidden);
247+
llvm::set_section(llglobal, &func_record_section_name);
248+
llvm::set_alignment(llglobal, VAR_ALIGN_BYTES);
249+
llvm::set_comdat(cx.llmod, llglobal, &func_record_var_name);
212250
cx.add_used_global(llglobal);
213251
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1791,10 +1791,14 @@ extern "C" {
17911791

17921792
pub fn LLVMRustCoverageCreatePGOFuncNameVar(F: &'a Value, FuncName: *const c_char)
17931793
-> &'a Value;
1794-
pub fn LLVMRustCoverageComputeHash(Name: *const c_char) -> u64;
1794+
pub fn LLVMRustCoverageHashCString(StrVal: *const c_char) -> u64;
1795+
pub fn LLVMRustCoverageHashByteArray(Bytes: *const c_char, NumBytes: size_t) -> u64;
17951796

17961797
#[allow(improper_ctypes)]
1797-
pub fn LLVMRustCoverageWriteSectionNameToString(M: &Module, Str: &RustString);
1798+
pub fn LLVMRustCoverageWriteMapSectionNameToString(M: &Module, Str: &RustString);
1799+
1800+
#[allow(improper_ctypes)]
1801+
pub fn LLVMRustCoverageWriteFuncSectionNameToString(M: &Module, Str: &RustString);
17981802

17991803
#[allow(improper_ctypes)]
18001804
pub fn LLVMRustCoverageWriteMappingVarNameToString(Str: &RustString);

compiler/rustc_codegen_llvm/src/llvm/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,24 @@ pub fn set_linkage(llglobal: &Value, linkage: Linkage) {
220220
}
221221
}
222222

223+
pub fn set_visibility(llglobal: &Value, visibility: Visibility) {
224+
unsafe {
225+
LLVMRustSetVisibility(llglobal, visibility);
226+
}
227+
}
228+
223229
pub fn set_alignment(llglobal: &Value, bytes: usize) {
224230
unsafe {
225231
ffi::LLVMSetAlignment(llglobal, bytes as c_uint);
226232
}
227233
}
228234

235+
pub fn set_comdat(llmod: &Module, llglobal: &Value, name: &str) {
236+
unsafe {
237+
LLVMRustSetComdat(llmod, llglobal, name.as_ptr().cast(), name.len());
238+
}
239+
}
240+
229241
/// Safe wrapper around `LLVMGetParam`, because segfaults are no fun.
230242
pub fn get_param(llfn: &Value, index: c_uint) -> &Value {
231243
unsafe {

0 commit comments

Comments
 (0)