Skip to content

Commit 125945d

Browse files
author
zhuyunxing
committed
coverage. Adapt to llvm optimization for conditions limits
1 parent fff32c4 commit 125945d

File tree

19 files changed

+348
-385
lines changed

19 files changed

+348
-385
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+13-50
Original file line numberDiff line numberDiff line change
@@ -1763,9 +1763,9 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17631763
&mut self,
17641764
fn_name: &'ll Value,
17651765
hash: &'ll Value,
1766-
bitmap_bytes: &'ll Value,
1766+
bitmap_bits: &'ll Value,
17671767
) {
1768-
debug!("mcdc_parameters() with args ({:?}, {:?}, {:?})", fn_name, hash, bitmap_bytes);
1768+
debug!("mcdc_parameters() with args ({:?}, {:?}, {:?})", fn_name, hash, bitmap_bits);
17691769

17701770
assert!(llvm_util::get_version() >= (19, 0, 0), "MCDC intrinsics require LLVM 19 or later");
17711771

@@ -1774,7 +1774,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17741774
&[self.cx.type_ptr(), self.cx.type_i64(), self.cx.type_i32()],
17751775
self.cx.type_void(),
17761776
);
1777-
let args = &[fn_name, hash, bitmap_bytes];
1777+
let args = &[fn_name, hash, bitmap_bits];
17781778
let args = self.check_call("call", llty, llfn, args);
17791779

17801780
unsafe {
@@ -1794,29 +1794,22 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17941794
&mut self,
17951795
fn_name: &'ll Value,
17961796
hash: &'ll Value,
1797-
bitmap_bytes: &'ll Value,
17981797
bitmap_index: &'ll Value,
17991798
mcdc_temp: &'ll Value,
18001799
) {
18011800
debug!(
1802-
"mcdc_tvbitmap_update() with args ({:?}, {:?}, {:?}, {:?}, {:?})",
1803-
fn_name, hash, bitmap_bytes, bitmap_index, mcdc_temp
1801+
"mcdc_tvbitmap_update() with args ({:?}, {:?}, {:?}, {:?})",
1802+
fn_name, hash, bitmap_index, mcdc_temp
18041803
);
18051804
assert!(llvm_util::get_version() >= (19, 0, 0), "MCDC intrinsics require LLVM 19 or later");
18061805

18071806
let llfn =
18081807
unsafe { llvm::LLVMRustGetInstrProfMCDCTVBitmapUpdateIntrinsic(self.cx().llmod) };
18091808
let llty = self.cx.type_func(
1810-
&[
1811-
self.cx.type_ptr(),
1812-
self.cx.type_i64(),
1813-
self.cx.type_i32(),
1814-
self.cx.type_i32(),
1815-
self.cx.type_ptr(),
1816-
],
1809+
&[self.cx.type_ptr(), self.cx.type_i64(), self.cx.type_i32(), self.cx.type_ptr()],
18171810
self.cx.type_void(),
18181811
);
1819-
let args = &[fn_name, hash, bitmap_bytes, bitmap_index, mcdc_temp];
1812+
let args = &[fn_name, hash, bitmap_index, mcdc_temp];
18201813
let args = self.check_call("call", llty, llfn, args);
18211814
unsafe {
18221815
let _ = llvm::LLVMRustBuildCall(
@@ -1832,42 +1825,12 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
18321825
self.store(self.const_i32(0), mcdc_temp, self.tcx.data_layout.i32_align.abi);
18331826
}
18341827

1835-
pub(crate) fn mcdc_condbitmap_update(
1836-
&mut self,
1837-
fn_name: &'ll Value,
1838-
hash: &'ll Value,
1839-
cond_loc: &'ll Value,
1840-
mcdc_temp: &'ll Value,
1841-
bool_value: &'ll Value,
1842-
) {
1843-
debug!(
1844-
"mcdc_condbitmap_update() with args ({:?}, {:?}, {:?}, {:?}, {:?})",
1845-
fn_name, hash, cond_loc, mcdc_temp, bool_value
1846-
);
1828+
pub(crate) fn mcdc_condbitmap_update(&mut self, cond_index: &'ll Value, mcdc_temp: &'ll Value) {
1829+
debug!("mcdc_condbitmap_update() with args ({:?}, {:?})", cond_index, mcdc_temp);
18471830
assert!(llvm_util::get_version() >= (19, 0, 0), "MCDC intrinsics require LLVM 19 or later");
1848-
let llfn = unsafe { llvm::LLVMRustGetInstrProfMCDCCondBitmapIntrinsic(self.cx().llmod) };
1849-
let llty = self.cx.type_func(
1850-
&[
1851-
self.cx.type_ptr(),
1852-
self.cx.type_i64(),
1853-
self.cx.type_i32(),
1854-
self.cx.type_ptr(),
1855-
self.cx.type_i1(),
1856-
],
1857-
self.cx.type_void(),
1858-
);
1859-
let args = &[fn_name, hash, cond_loc, mcdc_temp, bool_value];
1860-
self.check_call("call", llty, llfn, args);
1861-
unsafe {
1862-
let _ = llvm::LLVMRustBuildCall(
1863-
self.llbuilder,
1864-
llty,
1865-
llfn,
1866-
args.as_ptr() as *const &llvm::Value,
1867-
args.len() as c_uint,
1868-
[].as_ptr(),
1869-
0 as c_uint,
1870-
);
1871-
}
1831+
let align = self.tcx.data_layout.i32_align.abi;
1832+
let current_tv_index = self.load(self.cx.type_i32(), mcdc_temp, align);
1833+
let new_tv_index = self.add(current_tv_index, cond_index);
1834+
self.store(new_tv_index, mcdc_temp, align);
18721835
}
18731836
}

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

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

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

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

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

213207
let fn_name = bx.get_pgo_func_name_var(instance);
214208
let hash = bx.const_u64(function_coverage_info.function_source_hash);
215-
let bitmap_bytes = bx.const_u32(bitmap_bytes);
216209
let bitmap_index = bx.const_u32(bitmap_idx);
217-
bx.mcdc_tvbitmap_update(fn_name, hash, bitmap_bytes, bitmap_index, cond_bitmap);
210+
bx.mcdc_tvbitmap_update(fn_name, hash, bitmap_index, cond_bitmap);
218211
}
219212
}
220213
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,6 @@ unsafe extern "C" {
16401640
pub fn LLVMRustGetInstrProfIncrementIntrinsic(M: &Module) -> &Value;
16411641
pub fn LLVMRustGetInstrProfMCDCParametersIntrinsic(M: &Module) -> &Value;
16421642
pub fn LLVMRustGetInstrProfMCDCTVBitmapUpdateIntrinsic(M: &Module) -> &Value;
1643-
pub fn LLVMRustGetInstrProfMCDCCondBitmapIntrinsic(M: &Module) -> &Value;
16441643

16451644
pub fn LLVMRustBuildCall<'a>(
16461645
B: &Builder<'a>,

compiler/rustc_middle/src/mir/coverage.rs

+5-11
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

compiler/rustc_mir_build/src/build/coverageinfo/mcdc.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ use rustc_span::Span;
1212
use crate::build::Builder;
1313
use crate::errors::MCDCExceedsConditionLimit;
1414

15-
/// The MCDC bitmap scales exponentially (2^n) based on the number of conditions seen,
16-
/// So llvm sets a maximum value prevents the bitmap footprint from growing too large without the user's knowledge.
17-
/// This limit may be relaxed if the [upstream change](https://github.com/llvm/llvm-project/pull/82448) is merged.
18-
const MAX_CONDITIONS_IN_DECISION: usize = 6;
15+
/// LLVM uses `i16` to represent condition id. Hence `i16::MAX` is the hard limit for number of
16+
/// conditions in a decision.
17+
const MAX_CONDITIONS_IN_DECISION: usize = i16::MAX as usize;
1918

2019
#[derive(Default)]
2120
struct MCDCDecisionCtx {

0 commit comments

Comments
 (0)