Skip to content

Commit e0c38c7

Browse files
author
zhuyunxing
committed
coverage. Add BlockMarker for decisions and inject mcdc statements
1 parent eb2f8af commit e0c38c7

File tree

10 files changed

+291
-187
lines changed

10 files changed

+291
-187
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,27 @@ impl CounterMappingRegion {
224224
end_line,
225225
end_col,
226226
),
227-
MappingKind::Branch { true_term, false_term, mcdc_params: condition_info } => {
228-
Self::branch_region(
229-
Counter::from_term(true_term),
230-
Counter::from_term(false_term),
231-
condition_info,
232-
local_file_id,
233-
start_line,
234-
start_col,
235-
end_line,
236-
end_col,
237-
)
238-
}
239-
MappingKind::Decision(decision_info) => Self::decision_region(
227+
MappingKind::Branch { true_term, false_term } => Self::branch_region(
228+
Counter::from_term(true_term),
229+
Counter::from_term(false_term),
230+
None,
231+
local_file_id,
232+
start_line,
233+
start_col,
234+
end_line,
235+
end_col,
236+
),
237+
MappingKind::MCDCBranch { true_term, false_term, mcdc_params } => Self::branch_region(
238+
Counter::from_term(true_term),
239+
Counter::from_term(false_term),
240+
Some(mcdc_params),
241+
local_file_id,
242+
start_line,
243+
start_col,
244+
end_line,
245+
end_col,
246+
),
247+
MappingKind::MCDCDecision(decision_info) => Self::decision_region(
240248
decision_info,
241249
local_file_id,
242250
start_line,
@@ -272,14 +280,17 @@ impl CounterMappingRegion {
272280
pub(crate) fn branch_region(
273281
counter: Counter,
274282
false_counter: Counter,
275-
condition_info: ConditionInfo,
283+
condition_info: Option<ConditionInfo>,
276284
file_id: u32,
277285
start_line: u32,
278286
start_col: u32,
279287
end_line: u32,
280288
end_col: u32,
281289
) -> Self {
282-
let mcdc_params = mcdc::Parameters::Branch(condition_info.into());
290+
let mcdc_params = condition_info
291+
.map(mcdc::BranchParameters::from)
292+
.map(mcdc::Parameters::Branch)
293+
.unwrap_or(mcdc::Parameters::None);
283294
let kind = match mcdc_params {
284295
mcdc::Parameters::None => RegionKind::BranchRegion,
285296
mcdc::Parameters::Branch(_) => RegionKind::MCDCBranchRegion,
@@ -307,10 +318,12 @@ impl CounterMappingRegion {
307318
end_line: u32,
308319
end_col: u32,
309320
) -> Self {
321+
let mcdc_params = mcdc::Parameters::Decision(decision_info.into());
322+
310323
Self {
311324
counter: Counter::ZERO,
312325
false_counter: Counter::ZERO,
313-
mcdc_params: mcdc::Parameters::Decision(decision_info.into()),
326+
mcdc_params,
314327
file_id,
315328
expanded_file_id: 0,
316329
start_line,

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,8 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
9898
return;
9999
};
100100

101-
match *kind {
102-
// Ensure mcdc parameters are properly initialized for these operations.
103-
CoverageKind::UpdateCondBitmap { .. } | CoverageKind::UpdateTestVector { .. } => {
104-
ensure_mcdc_parameters(bx, instance, function_coverage_info)
105-
}
106-
_ => {}
101+
if function_coverage_info.mcdc_bitmap_bytes > 0 {
102+
ensure_mcdc_parameters(bx, instance, function_coverage_info);
107103
}
108104

109105
let Some(coverage_context) = bx.coverage_context() else { return };
@@ -147,23 +143,27 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
147143
CoverageKind::ExpressionUsed { id } => {
148144
func_coverage.mark_expression_id_seen(id);
149145
}
150-
151-
CoverageKind::UpdateCondBitmap { id, value } => {
146+
CoverageKind::CondBitmapUpdate { id, value, .. } => {
152147
drop(coverage_map);
148+
assert_ne!(
149+
id.as_u32(),
150+
0,
151+
"ConditionId of evaluated conditions should never be zero"
152+
);
153153
let cond_bitmap = coverage_context
154154
.try_get_mcdc_condition_bitmap(&instance)
155-
.expect("mcdc cond bitmap must be set before being updated");
155+
.expect("mcdc cond bitmap should have been allocated for updating");
156156
let cond_loc = bx.const_i32(id.as_u32() as i32 - 1);
157157
let bool_value = bx.const_bool(value);
158158
let fn_name = bx.get_pgo_func_name_var(instance);
159159
let hash = bx.const_u64(function_coverage_info.function_source_hash);
160160
bx.mcdc_condbitmap_update(fn_name, hash, cond_loc, cond_bitmap, bool_value);
161161
}
162-
CoverageKind::UpdateTestVector { bitmap_idx } => {
162+
CoverageKind::TestVectorBitmapUpdate { bitmap_idx } => {
163163
drop(coverage_map);
164164
let cond_bitmap = coverage_context
165-
.try_get_mcdc_condition_bitmap(&instance)
166-
.expect("mcdc cond bitmap must be set before being updated");
165+
.try_get_mcdc_condition_bitmap(&instance)
166+
.expect("mcdc cond bitmap should have been allocated for merging into the global bitmap");
167167
let bitmap_bytes = bx.tcx().coverage_ids_info(instance.def).mcdc_bitmap_bytes;
168168
assert!(bitmap_idx < bitmap_bytes, "bitmap index of the decision out of range");
169169
assert!(

compiler/rustc_middle/src/mir/coverage.rs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ pub enum CoverageKind {
112112
///
113113
/// If this statement does not survive MIR optimizations, any mappings that
114114
/// refer to this counter can have those references simplified to zero.
115-
CounterIncrement {
116-
id: CounterId,
117-
},
115+
CounterIncrement { id: CounterId },
118116

119117
/// Marks the point in MIR control-flow represented by a coverage expression.
120118
///
@@ -124,18 +122,23 @@ pub enum CoverageKind {
124122
/// (This is only inserted for expression IDs that are directly used by
125123
/// mappings. Intermediate expressions with no direct mappings are
126124
/// retained/zeroed based on whether they are transitively used.)
127-
ExpressionUsed {
128-
id: ExpressionId,
129-
},
130-
131-
UpdateCondBitmap {
132-
id: ConditionId,
133-
value: bool,
134-
},
135-
136-
UpdateTestVector {
137-
bitmap_idx: u32,
138-
},
125+
ExpressionUsed { id: ExpressionId },
126+
127+
/// Marks the point in MIR control flow represented by a evaluated condition.
128+
///
129+
/// This is eventually lowered to `llvm.instrprof.mcdc.condbitmap.update` in LLVM IR.
130+
///
131+
/// If this statement does not survive MIR optimizations, the condition would never be
132+
/// taken as evaluated.
133+
CondBitmapUpdate { id: ConditionId, value: bool },
134+
135+
/// Marks the point in MIR control flow represented by a evaluated decision.
136+
///
137+
/// This is eventually lowered to `llvm.instrprof.mcdc.tvbitmap.update` in LLVM IR.
138+
///
139+
/// If this statement does not survive MIR optimizations, the decision would never be
140+
/// taken as evaluated.
141+
TestVectorBitmapUpdate { bitmap_idx: u32 },
139142
}
140143

141144
impl Debug for CoverageKind {
@@ -146,10 +149,12 @@ impl Debug for CoverageKind {
146149
BlockMarker { id } => write!(fmt, "BlockMarker({:?})", id.index()),
147150
CounterIncrement { id } => write!(fmt, "CounterIncrement({:?})", id.index()),
148151
ExpressionUsed { id } => write!(fmt, "ExpressionUsed({:?})", id.index()),
149-
UpdateCondBitmap { id, value } => {
150-
write!(fmt, "UpdateCondBitmap({:?}, {value})", id.index())
152+
CondBitmapUpdate { id, value } => {
153+
write!(fmt, "CondBitmapUpdate({:?}, {:?})", id.index(), value)
154+
}
155+
TestVectorBitmapUpdate { bitmap_idx } => {
156+
write!(fmt, "TestVectorUpdate({:?})", bitmap_idx)
151157
}
152-
UpdateTestVector { bitmap_idx } => write!(fmt, "UpdateTestVector({:?})", bitmap_idx),
153158
}
154159
}
155160
}
@@ -205,9 +210,11 @@ pub enum MappingKind {
205210
/// Associates a normal region of code with a counter/expression/zero.
206211
Code(CovTerm),
207212
/// Associates a branch region with separate counters for true and false.
208-
Branch { true_term: CovTerm, false_term: CovTerm, mcdc_params: ConditionInfo },
213+
Branch { true_term: CovTerm, false_term: CovTerm },
214+
/// Associates a branch region with separate counters for true and false.
215+
MCDCBranch { true_term: CovTerm, false_term: CovTerm, mcdc_params: ConditionInfo },
209216
/// Associates a decision region with a bitmap and number of conditions.
210-
Decision(DecisionInfo),
217+
MCDCDecision(DecisionInfo),
211218
}
212219

213220
impl MappingKind {
@@ -219,7 +226,8 @@ impl MappingKind {
219226
match *self {
220227
Self::Code(term) => one(term),
221228
Self::Branch { true_term, false_term, .. } => two(true_term, false_term),
222-
Self::Decision(_) => zero(),
229+
Self::MCDCBranch { true_term, false_term, .. } => two(true_term, false_term),
230+
Self::MCDCDecision(_) => zero(),
223231
}
224232
}
225233

@@ -228,12 +236,15 @@ impl MappingKind {
228236
pub fn map_terms(&self, map_fn: impl Fn(CovTerm) -> CovTerm) -> Self {
229237
match *self {
230238
Self::Code(term) => Self::Code(map_fn(term)),
231-
Self::Branch { true_term, false_term, mcdc_params } => Self::Branch {
239+
Self::Branch { true_term, false_term } => {
240+
Self::Branch { true_term: map_fn(true_term), false_term: map_fn(false_term) }
241+
}
242+
Self::MCDCBranch { true_term, false_term, mcdc_params } => Self::MCDCBranch {
232243
true_term: map_fn(true_term),
233244
false_term: map_fn(false_term),
234245
mcdc_params,
235246
},
236-
Self::Decision(param) => Self::Decision(param),
247+
Self::MCDCDecision(param) => Self::MCDCDecision(param),
237248
}
238249
}
239250
}
@@ -267,7 +278,6 @@ pub struct BranchInfo {
267278
/// data structures without having to scan the entire body first.
268279
pub num_block_markers: usize,
269280
pub branch_spans: Vec<BranchSpan>,
270-
pub mcdc_bitmap_bytes_num: u32,
271281
pub decision_spans: Vec<DecisionSpan>,
272282
}
273283

@@ -309,5 +319,6 @@ pub struct DecisionInfo {
309319
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
310320
pub struct DecisionSpan {
311321
pub span: Span,
312-
pub mcdc_params: DecisionInfo,
322+
pub conditions_num: u16,
323+
pub join_marker: BlockMarkerId,
313324
}

0 commit comments

Comments
 (0)