Skip to content

Commit 7d080e1

Browse files
committed
coverage: Correctly report and check LLVM's coverage mapping version
1 parent ee03c28 commit 7d080e1

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ use rustc_span::Symbol;
1818

1919
/// Generates and exports the Coverage Map.
2020
///
21-
/// Rust Coverage Map generation supports LLVM Coverage Mapping Format version
22-
/// 6 (zero-based encoded as 5), as defined at
23-
/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
21+
/// Rust Coverage Map generation supports LLVM Coverage Mapping Format versions
22+
/// 6 and 7 (encoded as 5 and 6 respectively), as described at
23+
/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/18.0-2024-02-13/llvm/docs/CoverageMappingFormat.rst).
2424
/// These versions are supported by the LLVM coverage tools (`llvm-profdata` and `llvm-cov`)
25-
/// bundled with Rust's fork of LLVM.
25+
/// distributed in the `llvm-tools-preview` rustup component.
2626
///
2727
/// Consequently, Rust's bundled version of Clang also generates Coverage Maps compliant with
2828
/// the same version. Clang's implementation of Coverage Map generation was referenced when
@@ -32,10 +32,21 @@ use rustc_span::Symbol;
3232
pub fn finalize(cx: &CodegenCx<'_, '_>) {
3333
let tcx = cx.tcx;
3434

35-
// Ensure the installed version of LLVM supports Coverage Map Version 6
36-
// (encoded as a zero-based value: 5), which was introduced with LLVM 13.
37-
let version = coverageinfo::mapping_version();
38-
assert_eq!(version, 5, "The `CoverageMappingVersion` exposed by `llvm-wrapper` is out of sync");
35+
// Ensure that LLVM is using a version of the coverage mapping format that
36+
// agrees with our Rust-side code. Supported versions (encoded as n-1) are:
37+
// - `CovMapVersion::Version6` (5) used by LLVM 13-17
38+
// - `CovMapVersion::Version7` (6) used by LLVM 18
39+
let covmap_version = {
40+
let llvm_covmap_version = coverageinfo::mapping_version();
41+
let expected_versions = 5..=6;
42+
assert!(
43+
expected_versions.contains(&llvm_covmap_version),
44+
"Coverage mapping version exposed by `llvm-wrapper` is out of sync; \
45+
expected {expected_versions:?} but was {llvm_covmap_version}"
46+
);
47+
// This is the version number that we will embed in the covmap section:
48+
llvm_covmap_version
49+
};
3950

4051
debug!("Generating coverage map for CodegenUnit: `{}`", cx.codegen_unit.name());
4152

@@ -75,7 +86,7 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
7586

7687
// Generate the coverage map header, which contains the filenames used by
7788
// this CGU's coverage mappings, and store it in a well-known global.
78-
let cov_data_val = generate_coverage_map(cx, version, filenames_size, filenames_val);
89+
let cov_data_val = generate_coverage_map(cx, covmap_version, filenames_size, filenames_val);
7990
coverageinfo::save_cov_data_to_mod(cx, cov_data_val);
8091

8192
let mut unused_function_names = Vec::new();

compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,7 @@ extern "C" void LLVMRustCoverageWriteMappingVarNameToString(RustStringRef Str) {
205205
}
206206

207207
extern "C" uint32_t LLVMRustCoverageMappingVersion() {
208-
return coverage::CovMapVersion::Version6;
208+
// This should always be `CurrentVersion`, because that's the version LLVM
209+
// will use when encoding the data we give it.
210+
return coverage::CovMapVersion::CurrentVersion;
209211
}

0 commit comments

Comments
 (0)