Skip to content

Commit

Permalink
coverage. Fix panic when generating mcdc code for inlined functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuyunxing committed Jul 2, 2024
1 parent 722aea5 commit 49e8e2d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
8 changes: 8 additions & 0 deletions compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ impl<'ll, 'tcx> CrateCoverageContext<'ll, 'tcx> {
self.function_coverage_map.replace(FxIndexMap::default())
}

fn is_initialized_for(&self, instance: &Instance<'tcx>) -> bool {
self.mcdc_condition_bitmap_map.borrow().contains_key(instance)
}

/// LLVM use a temp value to record evaluated mcdc test vector of each decision, which is called condition bitmap.
/// In order to handle nested decisions, several condition bitmaps can be
/// allocated for a function body.
Expand Down Expand Up @@ -93,6 +97,10 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {

impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
fn init_coverage(&mut self, instance: Instance<'tcx>) {
if !self.coverage_context().is_some_and(|ctx| !ctx.is_initialized_for(&instance)) {
return;
}

let Some(function_coverage_info) =
self.tcx.instance_mir(instance.def).function_coverage_info.as_deref()
else {
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
pub fn codegen_coverage(&self, bx: &mut Bx, kind: &CoverageKind, scope: SourceScope) {
// Determine the instance that coverage data was originally generated for.
let instance = if let Some(inlined) = scope.inlined_instance(&self.mir.source_scopes) {
self.monomorphize(inlined)
let primary_instance = self.monomorphize(inlined);
// Coverage context of the inlined instance may not be initialized before.
bx.init_coverage(primary_instance);
primary_instance
} else {
self.instance
};
Expand Down
21 changes: 21 additions & 0 deletions tests/coverage/mcdc/inlined_expressions.cov-map
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Function name: inlined_expressions::inlined_instance
Raw bytes (52): 0x[01, 01, 03, 01, 05, 0b, 02, 09, 0d, 06, 01, 08, 01, 01, 06, 28, 00, 02, 01, 05, 00, 0b, 30, 05, 02, 01, 02, 00, 00, 05, 00, 06, 05, 00, 0a, 00, 0b, 30, 09, 0d, 02, 00, 00, 00, 0a, 00, 0b, 07, 01, 01, 00, 02]
Number of files: 1
- file 0 => global file 1
Number of expressions: 3
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
- expression 1 operands: lhs = Expression(2, Add), rhs = Expression(0, Sub)
- expression 2 operands: lhs = Counter(2), rhs = Counter(3)
Number of file 0 mappings: 6
- Code(Counter(0)) at (prev + 8, 1) to (start + 1, 6)
- MCDCDecision { bitmap_idx: 0, conditions_num: 2 } at (prev + 1, 5) to (start + 0, 11)
- MCDCBranch { true: Counter(1), false: Expression(0, Sub), condition_id: 1, true_next_id: 2, false_next_id: 0 } at (prev + 0, 5) to (start + 0, 6)
true = c1
false = (c0 - c1)
- Code(Counter(1)) at (prev + 0, 10) to (start + 0, 11)
- MCDCBranch { true: Counter(2), false: Counter(3), condition_id: 2, true_next_id: 0, false_next_id: 0 } at (prev + 0, 10) to (start + 0, 11)
true = c2
false = c3
- Code(Expression(1, Add)) at (prev + 1, 1) to (start + 0, 2)
= ((c2 + c3) + (c0 - c1))

41 changes: 41 additions & 0 deletions tests/coverage/mcdc/inlined_expressions.coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
LL| |#![feature(coverage_attribute)]
LL| |//@ edition: 2021
LL| |//@ min-llvm-version: 18
LL| |//@ compile-flags: -Zcoverage-options=mcdc -Cinline-threshold=0
LL| |//@ llvm-cov-flags: --show-branches=count --show-mcdc
LL| |
LL| |#[inline(always)]
LL| 3|fn inlined_instance(a: bool, b: bool) -> bool {
LL| 3| a && b
^2
------------------
| Branch (LL:5): [True: 2, False: 1]
| Branch (LL:10): [True: 1, False: 1]
------------------
|---> MC/DC Decision Region (LL:5) to (LL:11)
|
| Number of Conditions: 2
| Condition C1 --> (LL:5)
| Condition C2 --> (LL:10)
|
| Executed MC/DC Test Vectors:
|
| C1, C2 Result
| 1 { F, - = F }
| 2 { T, F = F }
| 3 { T, T = T }
|
| C1-Pair: covered: (1,3)
| C2-Pair: covered: (2,3)
| MC/DC Coverage for Decision: 100.00%
|
------------------
LL| 3|}
LL| |
LL| |#[coverage(off)]
LL| |fn main() {
LL| | let _ = inlined_instance(true, false);
LL| | let _ = inlined_instance(false, true);
LL| | let _ = inlined_instance(true, true);
LL| |}

17 changes: 17 additions & 0 deletions tests/coverage/mcdc/inlined_expressions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![feature(coverage_attribute)]
//@ edition: 2021
//@ min-llvm-version: 18
//@ compile-flags: -Zcoverage-options=mcdc -Cinline-threshold=0
//@ llvm-cov-flags: --show-branches=count --show-mcdc

#[inline(always)]
fn inlined_instance(a: bool, b: bool) -> bool {
a && b
}

#[coverage(off)]
fn main() {
let _ = inlined_instance(true, false);
let _ = inlined_instance(false, true);
let _ = inlined_instance(true, true);
}

0 comments on commit 49e8e2d

Please sign in to comment.