Skip to content

Commit 6e3e19f

Browse files
author
zhuyunxing
committed
coverage. Adapt to mcdc mapping formats introduced by llvm 19
1 parent 99bd601 commit 6e3e19f

File tree

22 files changed

+493
-488
lines changed

22 files changed

+493
-488
lines changed

Diff for: compiler/rustc_codegen_llvm/src/builder.rs

+13-50
Original file line numberDiff line numberDiff line change
@@ -1679,9 +1679,9 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
16791679
&mut self,
16801680
fn_name: &'ll Value,
16811681
hash: &'ll Value,
1682-
bitmap_bytes: &'ll Value,
1682+
bitmap_bits: &'ll Value,
16831683
) {
1684-
debug!("mcdc_parameters() with args ({:?}, {:?}, {:?})", fn_name, hash, bitmap_bytes);
1684+
debug!("mcdc_parameters() with args ({:?}, {:?}, {:?})", fn_name, hash, bitmap_bits);
16851685

16861686
assert!(
16871687
crate::llvm_util::get_version() >= (19, 0, 0),
@@ -1693,7 +1693,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
16931693
&[self.cx.type_ptr(), self.cx.type_i64(), self.cx.type_i32()],
16941694
self.cx.type_void(),
16951695
);
1696-
let args = &[fn_name, hash, bitmap_bytes];
1696+
let args = &[fn_name, hash, bitmap_bits];
16971697
let args = self.check_call("call", llty, llfn, args);
16981698

16991699
unsafe {
@@ -1713,13 +1713,12 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17131713
&mut self,
17141714
fn_name: &'ll Value,
17151715
hash: &'ll Value,
1716-
bitmap_bytes: &'ll Value,
17171716
bitmap_index: &'ll Value,
17181717
mcdc_temp: &'ll Value,
17191718
) {
17201719
debug!(
1721-
"mcdc_tvbitmap_update() with args ({:?}, {:?}, {:?}, {:?}, {:?})",
1722-
fn_name, hash, bitmap_bytes, bitmap_index, mcdc_temp
1720+
"mcdc_tvbitmap_update() with args ({:?}, {:?}, {:?}, {:?})",
1721+
fn_name, hash, bitmap_index, mcdc_temp
17231722
);
17241723
assert!(
17251724
crate::llvm_util::get_version() >= (19, 0, 0),
@@ -1729,16 +1728,10 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17291728
let llfn =
17301729
unsafe { llvm::LLVMRustGetInstrProfMCDCTVBitmapUpdateIntrinsic(self.cx().llmod) };
17311730
let llty = self.cx.type_func(
1732-
&[
1733-
self.cx.type_ptr(),
1734-
self.cx.type_i64(),
1735-
self.cx.type_i32(),
1736-
self.cx.type_i32(),
1737-
self.cx.type_ptr(),
1738-
],
1731+
&[self.cx.type_ptr(), self.cx.type_i64(), self.cx.type_i32(), self.cx.type_ptr()],
17391732
self.cx.type_void(),
17401733
);
1741-
let args = &[fn_name, hash, bitmap_bytes, bitmap_index, mcdc_temp];
1734+
let args = &[fn_name, hash, bitmap_index, mcdc_temp];
17421735
let args = self.check_call("call", llty, llfn, args);
17431736
unsafe {
17441737
let _ = llvm::LLVMRustBuildCall(
@@ -1754,45 +1747,15 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17541747
self.store(self.const_i32(0), mcdc_temp, self.tcx.data_layout.i32_align.abi);
17551748
}
17561749

1757-
pub(crate) fn mcdc_condbitmap_update(
1758-
&mut self,
1759-
fn_name: &'ll Value,
1760-
hash: &'ll Value,
1761-
cond_loc: &'ll Value,
1762-
mcdc_temp: &'ll Value,
1763-
bool_value: &'ll Value,
1764-
) {
1765-
debug!(
1766-
"mcdc_condbitmap_update() with args ({:?}, {:?}, {:?}, {:?}, {:?})",
1767-
fn_name, hash, cond_loc, mcdc_temp, bool_value
1768-
);
1750+
pub(crate) fn mcdc_condbitmap_update(&mut self, cond_index: &'ll Value, mcdc_temp: &'ll Value) {
1751+
debug!("mcdc_condbitmap_update() with args ({:?}, {:?})", cond_index, mcdc_temp);
17691752
assert!(
17701753
crate::llvm_util::get_version() >= (19, 0, 0),
17711754
"MCDC intrinsics require LLVM 19 or later"
17721755
);
1773-
let llfn = unsafe { llvm::LLVMRustGetInstrProfMCDCCondBitmapIntrinsic(self.cx().llmod) };
1774-
let llty = self.cx.type_func(
1775-
&[
1776-
self.cx.type_ptr(),
1777-
self.cx.type_i64(),
1778-
self.cx.type_i32(),
1779-
self.cx.type_ptr(),
1780-
self.cx.type_i1(),
1781-
],
1782-
self.cx.type_void(),
1783-
);
1784-
let args = &[fn_name, hash, cond_loc, mcdc_temp, bool_value];
1785-
self.check_call("call", llty, llfn, args);
1786-
unsafe {
1787-
let _ = llvm::LLVMRustBuildCall(
1788-
self.llbuilder,
1789-
llty,
1790-
llfn,
1791-
args.as_ptr() as *const &llvm::Value,
1792-
args.len() as c_uint,
1793-
[].as_ptr(),
1794-
0 as c_uint,
1795-
);
1796-
}
1756+
let align = self.tcx.data_layout.i32_align.abi;
1757+
let current_tv_index = self.load(self.cx.type_i32(), mcdc_temp, align);
1758+
let new_tv_index = self.add(current_tv_index, cond_index);
1759+
self.store(new_tv_index, mcdc_temp, align);
17971760
}
17981761
}

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
9898
};
9999

100100
// If there are no MC/DC bitmaps to set up, return immediately.
101-
if function_coverage_info.mcdc_bitmap_bytes == 0 {
101+
if function_coverage_info.mcdc_bitmap_bits == 0 {
102102
return;
103103
}
104104

105105
let fn_name = self.get_pgo_func_name_var(instance);
106106
let hash = self.const_u64(function_coverage_info.function_source_hash);
107-
let bitmap_bytes = self.const_u32(function_coverage_info.mcdc_bitmap_bytes);
108-
self.mcdc_parameters(fn_name, hash, bitmap_bytes);
107+
let bitmap_bits = self.const_u32(function_coverage_info.mcdc_bitmap_bits as u32);
108+
self.mcdc_parameters(fn_name, hash, bitmap_bits);
109109

110110
// Create pointers named `mcdc.addr.{i}` to stack-allocated condition bitmaps.
111111
let mut cond_bitmaps = vec![];
@@ -185,35 +185,28 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
185185
CoverageKind::ExpressionUsed { id } => {
186186
func_coverage.mark_expression_id_seen(id);
187187
}
188-
CoverageKind::CondBitmapUpdate { id, value, decision_depth } => {
188+
CoverageKind::CondBitmapUpdate { index, decision_depth } => {
189189
drop(coverage_map);
190-
assert_ne!(
191-
id.as_u32(),
192-
0,
193-
"ConditionId of evaluated conditions should never be zero"
194-
);
195190
let cond_bitmap = coverage_context
196191
.try_get_mcdc_condition_bitmap(&instance, decision_depth)
197192
.expect("mcdc cond bitmap should have been allocated for updating");
198-
let cond_loc = bx.const_i32(id.as_u32() as i32 - 1);
199-
let bool_value = bx.const_bool(value);
200-
let fn_name = bx.get_pgo_func_name_var(instance);
201-
let hash = bx.const_u64(function_coverage_info.function_source_hash);
202-
bx.mcdc_condbitmap_update(fn_name, hash, cond_loc, cond_bitmap, bool_value);
193+
let cond_index = bx.const_i32(index as i32);
194+
bx.mcdc_condbitmap_update(cond_index, cond_bitmap);
203195
}
204196
CoverageKind::TestVectorBitmapUpdate { bitmap_idx, decision_depth } => {
205197
drop(coverage_map);
206198
let cond_bitmap = coverage_context
207199
.try_get_mcdc_condition_bitmap(&instance, decision_depth)
208200
.expect("mcdc cond bitmap should have been allocated for merging into the global bitmap");
209-
let bitmap_bytes = function_coverage_info.mcdc_bitmap_bytes;
210-
assert!(bitmap_idx < bitmap_bytes, "bitmap index of the decision out of range");
201+
assert!(
202+
bitmap_idx as usize <= function_coverage_info.mcdc_bitmap_bits,
203+
"bitmap index of the decision out of range"
204+
);
211205

212206
let fn_name = bx.get_pgo_func_name_var(instance);
213207
let hash = bx.const_u64(function_coverage_info.function_source_hash);
214-
let bitmap_bytes = bx.const_u32(bitmap_bytes);
215208
let bitmap_index = bx.const_u32(bitmap_idx);
216-
bx.mcdc_tvbitmap_update(fn_name, hash, bitmap_bytes, bitmap_index, cond_bitmap);
209+
bx.mcdc_tvbitmap_update(fn_name, hash, bitmap_index, cond_bitmap);
217210
}
218211
}
219212
}

Diff for: compiler/rustc_codegen_llvm/src/llvm/ffi.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,6 @@ unsafe extern "C" {
16141614
pub fn LLVMRustGetInstrProfIncrementIntrinsic(M: &Module) -> &Value;
16151615
pub fn LLVMRustGetInstrProfMCDCParametersIntrinsic(M: &Module) -> &Value;
16161616
pub fn LLVMRustGetInstrProfMCDCTVBitmapUpdateIntrinsic(M: &Module) -> &Value;
1617-
pub fn LLVMRustGetInstrProfMCDCCondBitmapIntrinsic(M: &Module) -> &Value;
16181617

16191618
pub fn LLVMRustBuildCall<'a>(
16201619
B: &Builder<'a>,

Diff for: compiler/rustc_middle/src/mir/coverage.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ pub enum CoverageKind {
128128

129129
/// Marks the point in MIR control flow represented by a evaluated condition.
130130
///
131-
/// This is eventually lowered to `llvm.instrprof.mcdc.condbitmap.update` in LLVM IR.
132-
CondBitmapUpdate { id: ConditionId, value: bool, decision_depth: u16 },
131+
/// This is eventually lowered to instruments updating mcdc temp variables.
132+
CondBitmapUpdate { index: u32, decision_depth: u16 },
133133

134134
/// Marks the point in MIR control flow represented by a evaluated decision.
135135
///
@@ -145,14 +145,8 @@ impl Debug for CoverageKind {
145145
BlockMarker { id } => write!(fmt, "BlockMarker({:?})", id.index()),
146146
CounterIncrement { id } => write!(fmt, "CounterIncrement({:?})", id.index()),
147147
ExpressionUsed { id } => write!(fmt, "ExpressionUsed({:?})", id.index()),
148-
CondBitmapUpdate { id, value, decision_depth } => {
149-
write!(
150-
fmt,
151-
"CondBitmapUpdate({:?}, {:?}, depth={:?})",
152-
id.index(),
153-
value,
154-
decision_depth
155-
)
148+
CondBitmapUpdate { index, decision_depth } => {
149+
write!(fmt, "CondBitmapUpdate(index={:?}, depth={:?})", index, decision_depth)
156150
}
157151
TestVectorBitmapUpdate { bitmap_idx, decision_depth } => {
158152
write!(fmt, "TestVectorUpdate({:?}, depth={:?})", bitmap_idx, decision_depth)
@@ -253,7 +247,7 @@ pub struct Mapping {
253247
pub struct FunctionCoverageInfo {
254248
pub function_source_hash: u64,
255249
pub num_counters: usize,
256-
pub mcdc_bitmap_bytes: u32,
250+
pub mcdc_bitmap_bits: usize,
257251
pub expressions: IndexVec<ExpressionId, Expression>,
258252
pub mappings: Vec<Mapping>,
259253
/// The depth of the deepest decision is used to know how many
@@ -275,8 +269,10 @@ pub struct CoverageInfoHi {
275269
/// data structures without having to scan the entire body first.
276270
pub num_block_markers: usize,
277271
pub branch_spans: Vec<BranchSpan>,
278-
pub mcdc_branch_spans: Vec<MCDCBranchSpan>,
279-
pub mcdc_decision_spans: Vec<MCDCDecisionSpan>,
272+
/// Branch spans generated by mcdc. Because of some limits mcdc builder give up generating
273+
/// decisions including them so that they are handled as normal branch spans.
274+
pub mcdc_degraded_branch_spans: Vec<MCDCBranchSpan>,
275+
pub mcdc_spans: Vec<(MCDCDecisionSpan, Vec<MCDCBranchSpan>)>,
280276
}
281277

282278
#[derive(Clone, Debug)]
@@ -299,12 +295,9 @@ pub struct ConditionInfo {
299295
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
300296
pub struct MCDCBranchSpan {
301297
pub span: Span,
302-
/// If `None`, this actually represents a normal branch span inserted for
303-
/// code that was too complex for MC/DC.
304-
pub condition_info: Option<ConditionInfo>,
298+
pub condition_info: ConditionInfo,
305299
pub true_marker: BlockMarkerId,
306300
pub false_marker: BlockMarkerId,
307-
pub decision_depth: u16,
308301
}
309302

310303
#[derive(Copy, Clone, Debug)]
@@ -318,7 +311,7 @@ pub struct DecisionInfo {
318311
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
319312
pub struct MCDCDecisionSpan {
320313
pub span: Span,
321-
pub num_conditions: usize,
322314
pub end_markers: Vec<BlockMarkerId>,
323315
pub decision_depth: u16,
316+
pub num_conditions: usize,
324317
}

Diff for: compiler/rustc_middle/src/mir/pretty.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,8 @@ fn write_coverage_info_hi(
538538
let coverage::CoverageInfoHi {
539539
num_block_markers: _,
540540
branch_spans,
541-
mcdc_branch_spans,
542-
mcdc_decision_spans,
541+
mcdc_degraded_branch_spans,
542+
mcdc_spans,
543543
} = coverage_info_hi;
544544

545545
// Only add an extra trailing newline if we printed at least one thing.
@@ -553,29 +553,35 @@ fn write_coverage_info_hi(
553553
did_print = true;
554554
}
555555

556-
for coverage::MCDCBranchSpan {
557-
span,
558-
condition_info,
559-
true_marker,
560-
false_marker,
561-
decision_depth,
562-
} in mcdc_branch_spans
556+
for coverage::MCDCBranchSpan { span, true_marker, false_marker, .. } in
557+
mcdc_degraded_branch_spans
563558
{
564559
writeln!(
565560
w,
566-
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_marker:?}, false: {false_marker:?}, depth: {decision_depth:?} }} => {span:?}",
567-
condition_info.map(|info| info.condition_id)
561+
"{INDENT}coverage branch {{ true: {true_marker:?}, false: {false_marker:?} }} => {span:?}",
568562
)?;
569563
did_print = true;
570564
}
571565

572-
for coverage::MCDCDecisionSpan { span, num_conditions, end_markers, decision_depth } in
573-
mcdc_decision_spans
566+
for (
567+
coverage::MCDCDecisionSpan { span, end_markers, decision_depth, num_conditions: _ },
568+
conditions,
569+
) in mcdc_spans
574570
{
571+
let num_conditions = conditions.len();
575572
writeln!(
576573
w,
577574
"{INDENT}coverage mcdc decision {{ num_conditions: {num_conditions:?}, end: {end_markers:?}, depth: {decision_depth:?} }} => {span:?}"
578575
)?;
576+
for coverage::MCDCBranchSpan { span, condition_info, true_marker, false_marker } in
577+
conditions
578+
{
579+
writeln!(
580+
w,
581+
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_marker:?}, false: {false_marker:?} }} => {span:?}",
582+
condition_info.condition_id
583+
)?;
584+
}
579585
did_print = true;
580586
}
581587

Diff for: compiler/rustc_mir_build/src/build/coverageinfo.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,16 @@ impl CoverageInfoBuilder {
175175
let branch_spans =
176176
branch_info.map(|branch_info| branch_info.branch_spans).unwrap_or_default();
177177

178-
let (mcdc_decision_spans, mcdc_branch_spans) =
178+
let (mcdc_spans, mcdc_degraded_branch_spans) =
179179
mcdc_info.map(MCDCInfoBuilder::into_done).unwrap_or_default();
180180

181181
// For simplicity, always return an info struct (without Option), even
182182
// if there's nothing interesting in it.
183183
Box::new(CoverageInfoHi {
184184
num_block_markers,
185185
branch_spans,
186-
mcdc_branch_spans,
187-
mcdc_decision_spans,
186+
mcdc_degraded_branch_spans,
187+
mcdc_spans,
188188
})
189189
}
190190
}

0 commit comments

Comments
 (0)