Skip to content

Commit 156bb78

Browse files
committed
mcdc-coverage: allocate a condition bitmap on the function stackframe upon MCDCBitmapRequire
1 parent 441839d commit 156bb78

File tree

1 file changed

+39
-14
lines changed
  • compiler/rustc_codegen_llvm/src/coverageinfo

1 file changed

+39
-14
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_middle::bug;
1616
use rustc_middle::mir::coverage::CoverageKind;
1717
use rustc_middle::ty::layout::HasTyCtxt;
1818
use rustc_middle::ty::Instance;
19+
use rustc_target::abi::Align;
1920

2021
use std::cell::RefCell;
2122

@@ -31,13 +32,18 @@ pub struct CrateCoverageContext<'ll, 'tcx> {
3132
pub(crate) function_coverage_map:
3233
RefCell<FxIndexMap<Instance<'tcx>, FunctionCoverageCollector<'tcx>>>,
3334
pub(crate) pgo_func_name_var_map: RefCell<FxHashMap<Instance<'tcx>, &'ll llvm::Value>>,
35+
36+
/// When MCDC is enabled, holds references to the stack-allocated function-wise
37+
/// condition bitmaps.
38+
pub(crate) mcdc_condbitmap_map: RefCell<FxHashMap<Instance<'tcx>, &'ll llvm::Value>>,
3439
}
3540

3641
impl<'ll, 'tcx> CrateCoverageContext<'ll, 'tcx> {
3742
pub fn new() -> Self {
3843
Self {
3944
function_coverage_map: Default::default(),
4045
pgo_func_name_var_map: Default::default(),
46+
mcdc_condbitmap_map: Default::default(),
4147
}
4248
}
4349

@@ -104,20 +110,6 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
104110
| CoverageKind::MCDCDecisionMarker { .. } => unreachable!(
105111
"marker statement {kind:?} should have been removed by CleanupPostBorrowck"
106112
),
107-
CoverageKind::MCDCBitmapRequire { needed_bytes } => {
108-
// We need to explicitly drop the `RefMut` before calling into `instrprof_mcdc_parameters`,
109-
// as that needs an exclusive borrow.
110-
drop(coverage_map);
111-
112-
let fn_name = bx.get_pgo_func_name_var(instance);
113-
let hash = bx.const_u64(function_coverage_info.function_source_hash);
114-
let num_bitmap_bytes = bx.const_u32(needed_bytes);
115-
debug!(
116-
"codegen intrinsic instrprof.mcdc.parameters(fn_name={:?}, hash={:?}, bitmap_bytes={:?})",
117-
fn_name, hash, num_bitmap_bytes,
118-
);
119-
bx.instrprof_mcdc_parameters(fn_name, hash, num_bitmap_bytes);
120-
}
121113
CoverageKind::CounterIncrement { id } => {
122114
func_coverage.mark_counter_id_seen(id);
123115
// We need to explicitly drop the `RefMut` before calling into `instrprof_increment`,
@@ -149,6 +141,39 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
149141
CoverageKind::ExpressionUsed { id } => {
150142
func_coverage.mark_expression_id_seen(id);
151143
}
144+
CoverageKind::MCDCBitmapRequire { needed_bytes } => {
145+
// We need to explicitly drop the `RefMut` before calling into `instrprof_mcdc_parameters`,
146+
// as that needs an exclusive borrow.
147+
drop(coverage_map);
148+
149+
let fn_name = bx.get_pgo_func_name_var(instance);
150+
let hash = bx.const_u64(function_coverage_info.function_source_hash);
151+
let num_bitmap_bytes = bx.const_u32(needed_bytes);
152+
debug!(
153+
"codegen intrinsic instrprof.mcdc.parameters(fn_name={:?}, hash={:?}, bitmap_bytes={:?})",
154+
fn_name, hash, num_bitmap_bytes,
155+
);
156+
// Call the intrinsic to ask LLVM to allocate a global variable for the
157+
// test vector bitmaps in the function body.
158+
bx.instrprof_mcdc_parameters(fn_name, hash, num_bitmap_bytes);
159+
160+
// Allocates an integer in the function stackframe that will be
161+
// used for the condition bitmaps of the decisions.
162+
let cond_bitmap_addr = bx.alloca(
163+
bx.type_uint_from_ty(rustc_middle::ty::UintTy::U32),
164+
Align::from_bytes(4).expect("4 bytes alignment failed"),
165+
);
166+
167+
// Acquire a new handle to coverage_context.
168+
let coverage_context = bx.coverage_context().expect("Presence checked");
169+
coverage_context
170+
.mcdc_condbitmap_map
171+
.borrow_mut()
172+
.insert(instance, cond_bitmap_addr);
173+
}
174+
CoverageKind::MCDCCondBitmapReset => todo!(),
175+
CoverageKind::MCDCCondBitmapUpdate { condition_id: _, bool_value: _ } => todo!(),
176+
CoverageKind::MCDCTestBitmapUpdate { needed_bytes: _, decision_index: _ } => todo!(),
152177
}
153178
}
154179
}

0 commit comments

Comments
 (0)