Skip to content

Commit 0a96176

Browse files
committed
coverage: Make obtaining the codegen coverage context infallible
In all the situations where this context is needed, it should always be available.
1 parent 9f8a6be commit 0a96176

File tree

3 files changed

+16
-33
lines changed

3 files changed

+16
-33
lines changed

compiler/rustc_codegen_llvm/src/context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub(crate) struct CodegenCx<'ll, 'tcx> {
8080

8181
pub isize_ty: &'ll Type,
8282

83+
/// Extra codegen state needed when coverage instrumentation is enabled.
8384
pub coverage_cx: Option<coverageinfo::CrateCoverageContext<'ll, 'tcx>>,
8485
pub dbg_cx: Option<debuginfo::CodegenUnitDebugContext<'ll, 'tcx>>,
8586

@@ -592,11 +593,10 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
592593
&self.statics_to_rauw
593594
}
594595

596+
/// Extra state that is only available when coverage instrumentation is enabled.
595597
#[inline]
596-
pub(crate) fn coverage_context(
597-
&self,
598-
) -> Option<&coverageinfo::CrateCoverageContext<'ll, 'tcx>> {
599-
self.coverage_cx.as_ref()
598+
pub(crate) fn coverage_cx(&self) -> &coverageinfo::CrateCoverageContext<'ll, 'tcx> {
599+
self.coverage_cx.as_ref().expect("only called when coverage instrumentation is enabled")
600600
}
601601

602602
pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
5454
add_unused_functions(cx);
5555
}
5656

57-
let function_coverage_map = match cx.coverage_context() {
58-
Some(ctx) => ctx.take_function_coverage_map(),
59-
None => return,
60-
};
61-
57+
let function_coverage_map = cx.coverage_cx().take_function_coverage_map();
6258
if function_coverage_map.is_empty() {
6359
// This module has no functions with coverage instrumentation
6460
return;
@@ -543,9 +539,5 @@ fn add_unused_function_coverage<'tcx>(
543539
// zero, because none of its counters/expressions are marked as seen.
544540
let function_coverage = FunctionCoverageCollector::unused(instance, function_coverage_info);
545541

546-
if let Some(coverage_context) = cx.coverage_context() {
547-
coverage_context.function_coverage_map.borrow_mut().insert(instance, function_coverage);
548-
} else {
549-
bug!("Could not get the `coverage_context`");
550-
}
542+
cx.coverage_cx().function_coverage_map.borrow_mut().insert(instance, function_coverage);
551543
}

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+10-19
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_codegen_ssa::traits::{
77
};
88
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
99
use rustc_llvm::RustString;
10-
use rustc_middle::bug;
1110
use rustc_middle::mir::coverage::CoverageKind;
1211
use rustc_middle::ty::Instance;
1312
use rustc_middle::ty::layout::HasTyCtxt;
@@ -76,15 +75,11 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
7675
/// `instrprof.increment()`. The `Value` is only created once per instance.
7776
/// Multiple invocations with the same instance return the same `Value`.
7877
fn get_pgo_func_name_var(&self, instance: Instance<'tcx>) -> &'ll llvm::Value {
79-
if let Some(coverage_context) = self.coverage_context() {
80-
debug!("getting pgo_func_name_var for instance={:?}", instance);
81-
let mut pgo_func_name_var_map = coverage_context.pgo_func_name_var_map.borrow_mut();
82-
pgo_func_name_var_map
83-
.entry(instance)
84-
.or_insert_with(|| create_pgo_func_name_var(self, instance))
85-
} else {
86-
bug!("Could not get the `coverage_context`");
87-
}
78+
debug!("getting pgo_func_name_var for instance={:?}", instance);
79+
let mut pgo_func_name_var_map = self.coverage_cx().pgo_func_name_var_map.borrow_mut();
80+
pgo_func_name_var_map
81+
.entry(instance)
82+
.or_insert_with(|| create_pgo_func_name_var(self, instance))
8883
}
8984
}
9085

@@ -118,11 +113,7 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
118113
cond_bitmaps.push(cond_bitmap);
119114
}
120115

121-
self.coverage_context()
122-
.expect("always present when coverage is enabled")
123-
.mcdc_condition_bitmap_map
124-
.borrow_mut()
125-
.insert(instance, cond_bitmaps);
116+
self.coverage_cx().mcdc_condition_bitmap_map.borrow_mut().insert(instance, cond_bitmaps);
126117
}
127118

128119
#[instrument(level = "debug", skip(self))]
@@ -143,8 +134,7 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
143134
return;
144135
};
145136

146-
let Some(coverage_context) = bx.coverage_context() else { return };
147-
let mut coverage_map = coverage_context.function_coverage_map.borrow_mut();
137+
let mut coverage_map = bx.coverage_cx().function_coverage_map.borrow_mut();
148138
let func_coverage = coverage_map
149139
.entry(instance)
150140
.or_insert_with(|| FunctionCoverageCollector::new(instance, function_coverage_info));
@@ -186,15 +176,16 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
186176
}
187177
CoverageKind::CondBitmapUpdate { index, decision_depth } => {
188178
drop(coverage_map);
189-
let cond_bitmap = coverage_context
179+
let cond_bitmap = bx
180+
.coverage_cx()
190181
.try_get_mcdc_condition_bitmap(&instance, decision_depth)
191182
.expect("mcdc cond bitmap should have been allocated for updating");
192183
let cond_index = bx.const_i32(index as i32);
193184
bx.mcdc_condbitmap_update(cond_index, cond_bitmap);
194185
}
195186
CoverageKind::TestVectorBitmapUpdate { bitmap_idx, decision_depth } => {
196187
drop(coverage_map);
197-
let cond_bitmap = coverage_context
188+
let cond_bitmap = bx.coverage_cx()
198189
.try_get_mcdc_condition_bitmap(&instance, decision_depth)
199190
.expect("mcdc cond bitmap should have been allocated for merging into the global bitmap");
200191
assert!(

0 commit comments

Comments
 (0)