Skip to content

Commit 2250b84

Browse files
author
zhuyunxing
committed
coverage. Adapt to llvm optimization for conditions limits
1 parent 3d8ea11 commit 2250b84

File tree

19 files changed

+289
-490
lines changed

19 files changed

+289
-490
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+13-50
Original file line numberDiff line numberDiff line change
@@ -1719,9 +1719,9 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17191719
&mut self,
17201720
fn_name: &'ll Value,
17211721
hash: &'ll Value,
1722-
bitmap_bytes: &'ll Value,
1722+
bitmap_bits: &'ll Value,
17231723
) {
1724-
debug!("mcdc_parameters() with args ({:?}, {:?}, {:?})", fn_name, hash, bitmap_bytes);
1724+
debug!("mcdc_parameters() with args ({:?}, {:?}, {:?})", fn_name, hash, bitmap_bits);
17251725

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

@@ -1730,7 +1730,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17301730
&[self.cx.type_ptr(), self.cx.type_i64(), self.cx.type_i32()],
17311731
self.cx.type_void(),
17321732
);
1733-
let args = &[fn_name, hash, bitmap_bytes];
1733+
let args = &[fn_name, hash, bitmap_bits];
17341734
let args = self.check_call("call", llty, llfn, args);
17351735

17361736
unsafe {
@@ -1750,29 +1750,22 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17501750
&mut self,
17511751
fn_name: &'ll Value,
17521752
hash: &'ll Value,
1753-
bitmap_bytes: &'ll Value,
17541753
bitmap_index: &'ll Value,
17551754
mcdc_temp: &'ll Value,
17561755
) {
17571756
debug!(
1758-
"mcdc_tvbitmap_update() with args ({:?}, {:?}, {:?}, {:?}, {:?})",
1759-
fn_name, hash, bitmap_bytes, bitmap_index, mcdc_temp
1757+
"mcdc_tvbitmap_update() with args ({:?}, {:?}, {:?}, {:?})",
1758+
fn_name, hash, bitmap_index, mcdc_temp
17601759
);
17611760
assert!(llvm_util::get_version() >= (19, 0, 0), "MCDC intrinsics require LLVM 19 or later");
17621761

17631762
let llfn =
17641763
unsafe { llvm::LLVMRustGetInstrProfMCDCTVBitmapUpdateIntrinsic(self.cx().llmod) };
17651764
let llty = self.cx.type_func(
1766-
&[
1767-
self.cx.type_ptr(),
1768-
self.cx.type_i64(),
1769-
self.cx.type_i32(),
1770-
self.cx.type_i32(),
1771-
self.cx.type_ptr(),
1772-
],
1765+
&[self.cx.type_ptr(), self.cx.type_i64(), self.cx.type_i32(), self.cx.type_ptr()],
17731766
self.cx.type_void(),
17741767
);
1775-
let args = &[fn_name, hash, bitmap_bytes, bitmap_index, mcdc_temp];
1768+
let args = &[fn_name, hash, bitmap_index, mcdc_temp];
17761769
let args = self.check_call("call", llty, llfn, args);
17771770
unsafe {
17781771
let _ = llvm::LLVMRustBuildCall(
@@ -1788,42 +1781,12 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17881781
self.store(self.const_i32(0), mcdc_temp, self.tcx.data_layout.i32_align.abi);
17891782
}
17901783

1791-
pub(crate) fn mcdc_condbitmap_update(
1792-
&mut self,
1793-
fn_name: &'ll Value,
1794-
hash: &'ll Value,
1795-
cond_loc: &'ll Value,
1796-
mcdc_temp: &'ll Value,
1797-
bool_value: &'ll Value,
1798-
) {
1799-
debug!(
1800-
"mcdc_condbitmap_update() with args ({:?}, {:?}, {:?}, {:?}, {:?})",
1801-
fn_name, hash, cond_loc, mcdc_temp, bool_value
1802-
);
1784+
pub(crate) fn mcdc_condbitmap_update(&mut self, cond_index: &'ll Value, mcdc_temp: &'ll Value) {
1785+
debug!("mcdc_condbitmap_update() with args ({:?}, {:?})", cond_index, mcdc_temp);
18031786
assert!(llvm_util::get_version() >= (19, 0, 0), "MCDC intrinsics require LLVM 19 or later");
1804-
let llfn = unsafe { llvm::LLVMRustGetInstrProfMCDCCondBitmapIntrinsic(self.cx().llmod) };
1805-
let llty = self.cx.type_func(
1806-
&[
1807-
self.cx.type_ptr(),
1808-
self.cx.type_i64(),
1809-
self.cx.type_i32(),
1810-
self.cx.type_ptr(),
1811-
self.cx.type_i1(),
1812-
],
1813-
self.cx.type_void(),
1814-
);
1815-
let args = &[fn_name, hash, cond_loc, mcdc_temp, bool_value];
1816-
self.check_call("call", llty, llfn, args);
1817-
unsafe {
1818-
let _ = llvm::LLVMRustBuildCall(
1819-
self.llbuilder,
1820-
llty,
1821-
llfn,
1822-
args.as_ptr() as *const &llvm::Value,
1823-
args.len() as c_uint,
1824-
[].as_ptr(),
1825-
0 as c_uint,
1826-
);
1827-
}
1787+
let align = self.tcx.data_layout.i32_align.abi;
1788+
let current_tv_index = self.load(self.cx.type_i32(), mcdc_temp, align);
1789+
let new_tv_index = self.add(current_tv_index, cond_index);
1790+
self.store(new_tv_index, mcdc_temp, align);
18281791
}
18291792
}

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

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

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

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

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

214208
let fn_name = bx.get_pgo_func_name_var(instance);
215209
let hash = bx.const_u64(function_coverage_info.function_source_hash);
216-
let bitmap_bytes = bx.const_u32(bitmap_bytes);
217210
let bitmap_index = bx.const_u32(bitmap_idx);
218-
bx.mcdc_tvbitmap_update(fn_name, hash, bitmap_bytes, bitmap_index, cond_bitmap);
211+
bx.mcdc_tvbitmap_update(fn_name, hash, bitmap_index, cond_bitmap);
219212
}
220213
}
221214
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,6 @@ extern "C" {
16351635
pub fn LLVMRustGetInstrProfIncrementIntrinsic(M: &Module) -> &Value;
16361636
pub fn LLVMRustGetInstrProfMCDCParametersIntrinsic(M: &Module) -> &Value;
16371637
pub fn LLVMRustGetInstrProfMCDCTVBitmapUpdateIntrinsic(M: &Module) -> &Value;
1638-
pub fn LLVMRustGetInstrProfMCDCCondBitmapIntrinsic(M: &Module) -> &Value;
16391638

16401639
pub fn LLVMRustBuildCall<'a>(
16411640
B: &Builder<'a>,

compiler/rustc_middle/src/mir/coverage.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ pub enum CoverageKind {
137137

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

143143
/// Marks the point in MIR control flow represented by a evaluated decision.
144144
///
@@ -154,14 +154,8 @@ impl Debug for CoverageKind {
154154
BlockMarker { id } => write!(fmt, "BlockMarker({:?})", id.index()),
155155
CounterIncrement { id } => write!(fmt, "CounterIncrement({:?})", id.index()),
156156
ExpressionUsed { id } => write!(fmt, "ExpressionUsed({:?})", id.index()),
157-
CondBitmapUpdate { id, value, decision_depth } => {
158-
write!(
159-
fmt,
160-
"CondBitmapUpdate({:?}, {:?}, depth={:?})",
161-
id.index(),
162-
value,
163-
decision_depth
164-
)
157+
CondBitmapUpdate { index, decision_depth } => {
158+
write!(fmt, "CondBitmapUpdate(index={:?}, depth={:?})", index, decision_depth)
165159
}
166160
TestVectorBitmapUpdate { bitmap_idx, decision_depth } => {
167161
write!(fmt, "TestVectorUpdate({:?}, depth={:?})", bitmap_idx, decision_depth)
@@ -275,7 +269,7 @@ pub struct Mapping {
275269
pub struct FunctionCoverageInfo {
276270
pub function_source_hash: u64,
277271
pub num_counters: usize,
278-
pub mcdc_bitmap_bytes: u32,
272+
pub mcdc_bitmap_bits: usize,
279273
pub expressions: IndexVec<ExpressionId, Expression>,
280274
pub mappings: Vec<Mapping>,
281275
/// 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
@@ -15,10 +15,9 @@ use rustc_span::Span;
1515
use crate::build::Builder;
1616
use crate::errors::{MCDCExceedsConditionLimit, MCDCExceedsDecisionDepth};
1717

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

2322
/// MCDC allocates an i32 variable on stack for each depth. Ignore decisions nested too much to prevent it
2423
/// consuming excessive memory.

0 commit comments

Comments
 (0)