Skip to content

Commit b4668ec

Browse files
committed
Improved version check
1 parent b5fef37 commit b4668ec

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ use tracing::debug;
2626
/// undocumented details in Clang's implementation (that may or may not be important) were also
2727
/// replicated for Rust's Coverage Map.
2828
pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) {
29+
// Ensure LLVM supports Coverage Map Version 4 (encoded as a zero-based value: 3).
30+
// If not, the LLVM Version must be less than 11.
31+
let version = coverageinfo::mapping_version();
32+
assert_eq!(version, 3, "rustc option `-Z instrument-coverage` requires LLVM 11 or higher.");
33+
2934
let function_coverage_map = match cx.coverage_context() {
3035
Some(ctx) => ctx.take_function_coverage_map(),
3136
None => return,
@@ -68,7 +73,7 @@ pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) {
6873
let filenames_ref = coverageinfo::hash_bytes(filenames_buffer);
6974

7075
// Generate the LLVM IR representation of the coverage map and store it in a well-known global
71-
let cov_data_val = mapgen.generate_coverage_map(cx, filenames_size, filenames_val);
76+
let cov_data_val = mapgen.generate_coverage_map(cx, version, filenames_size, filenames_val);
7277

7378
for (mangled_function_name, function_source_hash, coverage_mapping_buffer) in function_data {
7479
save_function_record(
@@ -159,21 +164,18 @@ impl CoverageMapGenerator {
159164
fn generate_coverage_map(
160165
self,
161166
cx: &CodegenCx<'ll, 'tcx>,
167+
version: u32,
162168
filenames_size: usize,
163169
filenames_val: &'ll llvm::Value,
164170
) -> &'ll llvm::Value {
165-
debug!(
166-
"cov map: filenames_size = {}, 0-based version = {}",
167-
filenames_size,
168-
coverageinfo::mapping_version()
169-
);
171+
debug!("cov map: filenames_size = {}, 0-based version = {}", filenames_size, version);
170172

171173
// Create the coverage data header (Note, fields 0 and 2 are now always zero,
172174
// as of `llvm::coverage::CovMapVersion::Version4`.)
173175
let zero_was_n_records_val = cx.const_u32(0);
174176
let filenames_size_val = cx.const_u32(filenames_size as u32);
175177
let zero_was_coverage_size_val = cx.const_u32(0);
176-
let version_val = cx.const_u32(coverageinfo::mapping_version());
178+
let version_val = cx.const_u32(version);
177179
let cov_data_header_val = cx.const_struct(
178180
&[zero_was_n_records_val, filenames_size_val, zero_was_coverage_size_val, version_val],
179181
/*packed=*/ false,

compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ extern "C" void LLVMRustCoverageWriteFuncSectionNameToString(LLVMModuleRef M,
7373
RustStringRef Str) {
7474
#if LLVM_VERSION_GE(11, 0)
7575
WriteSectionNameToString(M, IPSK_covfun, Str);
76-
#else
77-
report_fatal_error("rustc option `-Z instrument-coverage` requires LLVM 11 or higher.");
76+
// else do nothing; the `Version` check will abort codegen on the Rust side
7877
#endif
7978
}
8079

@@ -88,6 +87,6 @@ extern "C" uint32_t LLVMRustCoverageMappingVersion() {
8887
#if LLVM_VERSION_GE(11, 0)
8988
return coverage::CovMapVersion::Version4;
9089
#else
91-
report_fatal_error("rustc option `-Z instrument-coverage` requires LLVM 11 or higher.");
90+
return coverage::CovMapVersion::Version3;
9291
#endif
9392
}

0 commit comments

Comments
 (0)