Skip to content

Commit 439dbfa

Browse files
zhuyunxingLambdaris
zhuyunxing
authored andcommitted
coverage. Lowering MC/DC statements to llvm-ir
1 parent cf6b6cb commit 439dbfa

File tree

6 files changed

+240
-3
lines changed

6 files changed

+240
-3
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+125-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_data_structures::small_c_str::SmallCStr;
1717
use rustc_hir::def_id::DefId;
1818
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
1919
use rustc_middle::ty::layout::{
20-
FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, TyAndLayout,
20+
FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTyCtxt, LayoutError, LayoutOfHelpers, TyAndLayout,
2121
};
2222
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
2323
use rustc_sanitizers::{cfi, kcfi};
@@ -1702,4 +1702,128 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17021702
};
17031703
kcfi_bundle
17041704
}
1705+
1706+
pub(crate) fn mcdc_parameters(
1707+
&mut self,
1708+
fn_name: &'ll Value,
1709+
hash: &'ll Value,
1710+
bitmap_bytes: &'ll Value,
1711+
) -> &'ll Value {
1712+
debug!("mcdc_parameters() with args ({:?}, {:?}, {:?})", fn_name, hash, bitmap_bytes);
1713+
1714+
assert!(llvm_util::get_version() >= (18, 0, 0), "MCDC intrinsics require LLVM 18 or later");
1715+
1716+
let llfn = unsafe { llvm::LLVMRustGetInstrProfMCDCParametersIntrinsic(self.cx().llmod) };
1717+
let llty = self.cx.type_func(
1718+
&[self.cx.type_ptr(), self.cx.type_i64(), self.cx.type_i32()],
1719+
self.cx.type_void(),
1720+
);
1721+
let args = &[fn_name, hash, bitmap_bytes];
1722+
let args = self.check_call("call", llty, llfn, args);
1723+
1724+
unsafe {
1725+
let _ = llvm::LLVMRustBuildCall(
1726+
self.llbuilder,
1727+
llty,
1728+
llfn,
1729+
args.as_ptr() as *const &llvm::Value,
1730+
args.len() as c_uint,
1731+
[].as_ptr(),
1732+
0 as c_uint,
1733+
);
1734+
// Create condition bitmap named `mcdc.addr`.
1735+
let mut bx = Builder::with_cx(self.cx);
1736+
bx.position_at_start(llvm::LLVMGetFirstBasicBlock(self.llfn()));
1737+
let cond_bitmap = {
1738+
let alloca =
1739+
llvm::LLVMBuildAlloca(bx.llbuilder, bx.cx.type_i32(), c"mcdc.addr".as_ptr());
1740+
llvm::LLVMSetAlignment(alloca, 4);
1741+
alloca
1742+
};
1743+
bx.store(self.const_i32(0), cond_bitmap, self.tcx().data_layout.i32_align.abi);
1744+
cond_bitmap
1745+
}
1746+
}
1747+
1748+
pub(crate) fn mcdc_tvbitmap_update(
1749+
&mut self,
1750+
fn_name: &'ll Value,
1751+
hash: &'ll Value,
1752+
bitmap_bytes: &'ll Value,
1753+
bitmap_index: &'ll Value,
1754+
mcdc_temp: &'ll Value,
1755+
) {
1756+
debug!(
1757+
"mcdc_tvbitmap_update() with args ({:?}, {:?}, {:?}, {:?}, {:?})",
1758+
fn_name, hash, bitmap_bytes, bitmap_index, mcdc_temp
1759+
);
1760+
assert!(llvm_util::get_version() >= (18, 0, 0), "MCDC intrinsics require LLVM 18 or later");
1761+
1762+
let llfn =
1763+
unsafe { llvm::LLVMRustGetInstrProfMCDCTVBitmapUpdateIntrinsic(self.cx().llmod) };
1764+
let llty = self.cx.type_func(
1765+
&[
1766+
self.cx.type_ptr(),
1767+
self.cx.type_i64(),
1768+
self.cx.type_i32(),
1769+
self.cx.type_i32(),
1770+
self.cx.type_ptr(),
1771+
],
1772+
self.cx.type_void(),
1773+
);
1774+
let args = &[fn_name, hash, bitmap_bytes, bitmap_index, mcdc_temp];
1775+
let args = self.check_call("call", llty, llfn, args);
1776+
unsafe {
1777+
let _ = llvm::LLVMRustBuildCall(
1778+
self.llbuilder,
1779+
llty,
1780+
llfn,
1781+
args.as_ptr() as *const &llvm::Value,
1782+
args.len() as c_uint,
1783+
[].as_ptr(),
1784+
0 as c_uint,
1785+
);
1786+
}
1787+
let i32_align = self.tcx().data_layout.i32_align.abi;
1788+
self.store(self.const_i32(0), mcdc_temp, i32_align);
1789+
}
1790+
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+
);
1803+
assert!(llvm_util::get_version() >= (18, 0, 0), "MCDC intrinsics require LLVM 18 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+
}
1828+
}
17051829
}

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+69-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_codegen_ssa::traits::{
1313
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
1414
use rustc_llvm::RustString;
1515
use rustc_middle::bug;
16-
use rustc_middle::mir::coverage::CoverageKind;
16+
use rustc_middle::mir::coverage::{CoverageKind, FunctionCoverageInfo};
1717
use rustc_middle::ty::layout::HasTyCtxt;
1818
use rustc_middle::ty::Instance;
1919
use rustc_target::abi::Align;
@@ -30,13 +30,15 @@ pub struct CrateCoverageContext<'ll, 'tcx> {
3030
pub(crate) function_coverage_map:
3131
RefCell<FxIndexMap<Instance<'tcx>, FunctionCoverageCollector<'tcx>>>,
3232
pub(crate) pgo_func_name_var_map: RefCell<FxHashMap<Instance<'tcx>, &'ll llvm::Value>>,
33+
pub(crate) mcdc_condition_bitmap_map: RefCell<FxHashMap<Instance<'tcx>, &'ll llvm::Value>>,
3334
}
3435

3536
impl<'ll, 'tcx> CrateCoverageContext<'ll, 'tcx> {
3637
pub fn new() -> Self {
3738
Self {
3839
function_coverage_map: Default::default(),
3940
pgo_func_name_var_map: Default::default(),
41+
mcdc_condition_bitmap_map: Default::default(),
4042
}
4143
}
4244

@@ -45,6 +47,12 @@ impl<'ll, 'tcx> CrateCoverageContext<'ll, 'tcx> {
4547
) -> FxIndexMap<Instance<'tcx>, FunctionCoverageCollector<'tcx>> {
4648
self.function_coverage_map.replace(FxIndexMap::default())
4749
}
50+
51+
/// LLVM use a temp value to record evaluated mcdc test vector of each decision, which is called condition bitmap.
52+
/// This value is named `mcdc.addr` (same as clang) and is a 32-bit integer.
53+
fn try_get_mcdc_condition_bitmap(&self, instance: &Instance<'tcx>) -> Option<&'ll llvm::Value> {
54+
self.mcdc_condition_bitmap_map.borrow().get(instance).copied()
55+
}
4856
}
4957

5058
// These methods used to be part of trait `CoverageInfoMethods`, which no longer
@@ -90,6 +98,10 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
9098
return;
9199
};
92100

101+
if function_coverage_info.mcdc_bitmap_bytes > 0 {
102+
ensure_mcdc_parameters(bx, instance, function_coverage_info);
103+
}
104+
93105
let Some(coverage_context) = bx.coverage_context() else { return };
94106
let mut coverage_map = coverage_context.function_coverage_map.borrow_mut();
95107
let func_coverage = coverage_map
@@ -131,10 +143,66 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
131143
CoverageKind::ExpressionUsed { id } => {
132144
func_coverage.mark_expression_id_seen(id);
133145
}
146+
CoverageKind::CondBitmapUpdate { id, value, .. } => {
147+
drop(coverage_map);
148+
assert_ne!(
149+
id.as_u32(),
150+
0,
151+
"ConditionId of evaluated conditions should never be zero"
152+
);
153+
let cond_bitmap = coverage_context
154+
.try_get_mcdc_condition_bitmap(&instance)
155+
.expect("mcdc cond bitmap should have been allocated for updating");
156+
let cond_loc = bx.const_i32(id.as_u32() as i32 - 1);
157+
let bool_value = bx.const_bool(value);
158+
let fn_name = bx.get_pgo_func_name_var(instance);
159+
let hash = bx.const_u64(function_coverage_info.function_source_hash);
160+
bx.mcdc_condbitmap_update(fn_name, hash, cond_loc, cond_bitmap, bool_value);
161+
}
162+
CoverageKind::TestVectorBitmapUpdate { bitmap_idx } => {
163+
drop(coverage_map);
164+
let cond_bitmap = coverage_context
165+
.try_get_mcdc_condition_bitmap(&instance)
166+
.expect("mcdc cond bitmap should have been allocated for merging into the global bitmap");
167+
let bitmap_bytes = bx.tcx().coverage_ids_info(instance.def).mcdc_bitmap_bytes;
168+
assert!(bitmap_idx < bitmap_bytes, "bitmap index of the decision out of range");
169+
assert!(
170+
bitmap_bytes <= function_coverage_info.mcdc_bitmap_bytes,
171+
"bitmap length disagreement: query says {bitmap_bytes} but function info only has {}",
172+
function_coverage_info.mcdc_bitmap_bytes
173+
);
174+
175+
let fn_name = bx.get_pgo_func_name_var(instance);
176+
let hash = bx.const_u64(function_coverage_info.function_source_hash);
177+
let bitmap_bytes = bx.const_u32(bitmap_bytes);
178+
let bitmap_index = bx.const_u32(bitmap_idx);
179+
bx.mcdc_tvbitmap_update(fn_name, hash, bitmap_bytes, bitmap_index, cond_bitmap);
180+
}
134181
}
135182
}
136183
}
137184

185+
fn ensure_mcdc_parameters<'ll, 'tcx>(
186+
bx: &mut Builder<'_, 'll, 'tcx>,
187+
instance: Instance<'tcx>,
188+
function_coverage_info: &FunctionCoverageInfo,
189+
) {
190+
let Some(cx) = bx.coverage_context() else { return };
191+
if cx.mcdc_condition_bitmap_map.borrow().contains_key(&instance) {
192+
return;
193+
}
194+
195+
let fn_name = bx.get_pgo_func_name_var(instance);
196+
let hash = bx.const_u64(function_coverage_info.function_source_hash);
197+
let bitmap_bytes = bx.const_u32(function_coverage_info.mcdc_bitmap_bytes);
198+
let cond_bitmap = bx.mcdc_parameters(fn_name, hash, bitmap_bytes);
199+
bx.coverage_context()
200+
.expect("already checked above")
201+
.mcdc_condition_bitmap_map
202+
.borrow_mut()
203+
.insert(instance, cond_bitmap);
204+
}
205+
138206
/// Calls llvm::createPGOFuncNameVar() with the given function instance's
139207
/// mangled function name. The LLVM API returns an llvm::GlobalVariable
140208
/// containing the function name, with the specific variable name and linkage

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,10 @@ extern "C" {
16311631

16321632
// Miscellaneous instructions
16331633
pub fn LLVMRustGetInstrProfIncrementIntrinsic(M: &Module) -> &Value;
1634+
pub fn LLVMRustGetInstrProfMCDCParametersIntrinsic(M: &Module) -> &Value;
1635+
pub fn LLVMRustGetInstrProfMCDCTVBitmapUpdateIntrinsic(M: &Module) -> &Value;
1636+
pub fn LLVMRustGetInstrProfMCDCCondBitmapIntrinsic(M: &Module) -> &Value;
1637+
16341638
pub fn LLVMRustBuildCall<'a>(
16351639
B: &Builder<'a>,
16361640
Ty: &'a Type,

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,33 @@ extern "C" LLVMValueRef LLVMRustGetInstrProfIncrementIntrinsic(LLVMModuleRef M)
15461546
unwrap(M), llvm::Intrinsic::instrprof_increment));
15471547
}
15481548

1549+
extern "C" LLVMValueRef LLVMRustGetInstrProfMCDCParametersIntrinsic(LLVMModuleRef M) {
1550+
#if LLVM_VERSION_GE(18, 0)
1551+
return wrap(llvm::Intrinsic::getDeclaration(
1552+
unwrap(M), llvm::Intrinsic::instrprof_mcdc_parameters));
1553+
#else
1554+
report_fatal_error("LLVM 18.0 is required for mcdc intrinsic functions");
1555+
#endif
1556+
}
1557+
1558+
extern "C" LLVMValueRef LLVMRustGetInstrProfMCDCTVBitmapUpdateIntrinsic(LLVMModuleRef M) {
1559+
#if LLVM_VERSION_GE(18, 0)
1560+
return wrap(llvm::Intrinsic::getDeclaration(
1561+
unwrap(M), llvm::Intrinsic::instrprof_mcdc_tvbitmap_update));
1562+
#else
1563+
report_fatal_error("LLVM 18.0 is required for mcdc intrinsic functions");
1564+
#endif
1565+
}
1566+
1567+
extern "C" LLVMValueRef LLVMRustGetInstrProfMCDCCondBitmapIntrinsic(LLVMModuleRef M) {
1568+
#if LLVM_VERSION_GE(18, 0)
1569+
return wrap(llvm::Intrinsic::getDeclaration(
1570+
unwrap(M), llvm::Intrinsic::instrprof_mcdc_condbitmap_update));
1571+
#else
1572+
report_fatal_error("LLVM 18.0 is required for mcdc intrinsic functions");
1573+
#endif
1574+
}
1575+
15491576
extern "C" LLVMValueRef LLVMRustBuildMemCpy(LLVMBuilderRef B,
15501577
LLVMValueRef Dst, unsigned DstAlign,
15511578
LLVMValueRef Src, unsigned SrcAlign,

compiler/rustc_middle/src/mir/query.rs

+4
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,8 @@ pub struct CoverageIdsInfo {
361361
/// InstrumentCoverage MIR pass, if the highest-numbered counter increments
362362
/// were removed by MIR optimizations.
363363
pub max_counter_id: mir::coverage::CounterId,
364+
365+
/// Coverage codegen for mcdc needs to know the size of the global bitmap so that it can
366+
/// set the `bytemap-bytes` argument of the `llvm.instrprof.mcdc.tvbitmap.update` intrinsic.
367+
pub mcdc_bitmap_bytes: u32,
364368
}

compiler/rustc_mir_transform/src/coverage/query.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ fn coverage_ids_info<'tcx>(
6161
.max()
6262
.unwrap_or(CounterId::ZERO);
6363

64-
CoverageIdsInfo { max_counter_id }
64+
let mcdc_bitmap_bytes = mir_body
65+
.coverage_branch_info
66+
.as_deref()
67+
.map(|info| {
68+
info.mcdc_decision_spans
69+
.iter()
70+
.fold(0, |acc, decision| acc + (1_u32 << decision.conditions_num).div_ceil(8))
71+
})
72+
.unwrap_or_default();
73+
74+
CoverageIdsInfo { max_counter_id, mcdc_bitmap_bytes }
6575
}
6676

6777
fn all_coverage_in_mir_body<'a, 'tcx>(

0 commit comments

Comments
 (0)