Skip to content

Commit 391ad51

Browse files
author
zhuyunxing
committed
coverage. Adopt nicer code style
1 parent 29e0881 commit 391ad51

File tree

7 files changed

+88
-45
lines changed

7 files changed

+88
-45
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs

+38-15
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub mod mcdc {
115115

116116
/// Must match the layout of `LLVMRustMCDCDecisionParameters`.
117117
#[repr(C)]
118-
#[derive(Clone, Copy, Debug)]
118+
#[derive(Clone, Copy, Debug, Default)]
119119
pub struct DecisionParameters {
120120
bitmap_idx: u32,
121121
conditions_num: u16,
@@ -126,19 +126,47 @@ pub mod mcdc {
126126

127127
/// Must match the layout of `LLVMRustMCDCBranchParameters`.
128128
#[repr(C)]
129-
#[derive(Clone, Copy, Debug)]
129+
#[derive(Clone, Copy, Debug, Default)]
130130
pub struct BranchParameters {
131131
condition_id: LLVMConditionId,
132132
condition_ids: [LLVMConditionId; 2],
133133
}
134134

135-
/// Same layout with `LLVMRustMCDCParameters`
136135
#[repr(C, u8)]
137-
#[derive(Clone, Copy, Debug)]
138-
pub enum Parameters {
136+
pub enum ParameterTag {
139137
None,
140-
Decision(DecisionParameters),
141-
Branch(BranchParameters),
138+
Decision,
139+
Branch,
140+
}
141+
/// Same layout with `LLVMRustMCDCParameters`
142+
#[repr(C)]
143+
#[derive(Clone, Copy, Debug)]
144+
pub struct Parameters {
145+
tag: ParameterTag,
146+
decision_params: DecisionParameters,
147+
branch_params: BranchParameters,
148+
}
149+
150+
impl Parameters {
151+
pub fn none() -> Self {
152+
Self {
153+
tag: ParameterTag::None,
154+
decision_params: Default::default(),
155+
branch_params: Default::default(),
156+
}
157+
}
158+
pub fn decision(decision_params: DecisionParameters) -> Self {
159+
Self { tag: ParameterTag::Decision, decision_params, branch_params: Default::default() }
160+
}
161+
pub fn branch(branch_params: BranchParameters) -> Self {
162+
Self { tag: ParameterTag::Branch, decision_params: Default, branch_params }
163+
}
164+
}
165+
166+
impl Default for Parameters {
167+
fn default() -> Self {
168+
Self::none()
169+
}
142170
}
143171

144172
impl From<ConditionInfo> for BranchParameters {
@@ -287,14 +315,9 @@ impl CounterMappingRegion {
287315
end_line: u32,
288316
end_col: u32,
289317
) -> Self {
290-
let mcdc_params = condition_info
291-
.map(mcdc::BranchParameters::from)
292-
.map(mcdc::Parameters::Branch)
293-
.unwrap_or(mcdc::Parameters::None);
294-
let kind = match mcdc_params {
295-
mcdc::Parameters::None => RegionKind::BranchRegion,
296-
mcdc::Parameters::Branch(_) => RegionKind::MCDCBranchRegion,
297-
_ => unreachable!("invalid mcdc params for branch"),
318+
let (kind, mcdc_params) = match condition_info {
319+
Some(info) => (RegionKind::MCDCBranchRegion, mcdc::Parameters::branch(info.into())),
320+
None => (RegionKind::BranchRegion, Default::default()),
298321
};
299322
Self {
300323
counter,

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,20 @@ fn ensure_mcdc_parameters<'ll, 'tcx>(
188188
instance: Instance<'tcx>,
189189
function_coverage_info: &FunctionCoverageInfo,
190190
) {
191-
if bx
192-
.coverage_context()
193-
.is_some_and(|cx| !cx.mcdc_condition_bitmap_map.borrow().contains_key(&instance))
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-
.unwrap()
201-
.mcdc_condition_bitmap_map
202-
.borrow_mut()
203-
.insert(instance, cond_bitmap);
191+
let Some(cx) = bx.coverage_context() else { return };
192+
if cx.mcdc_condition_bitmap_map.borrow().contains_key(&instance) {
193+
return;
204194
}
195+
196+
let fn_name = bx.get_pgo_func_name_var(instance);
197+
let hash = bx.const_u64(function_coverage_info.function_source_hash);
198+
let bitmap_bytes = bx.const_u32(function_coverage_info.mcdc_bitmap_bytes);
199+
let cond_bitmap = bx.mcdc_parameters(fn_name, hash, bitmap_bytes);
200+
bx.coverage_context()
201+
.expect("already checked above")
202+
.mcdc_condition_bitmap_map
203+
.borrow_mut()
204+
.insert(instance, cond_bitmap);
205205
}
206206

207207
/// Calls llvm::createPGOFuncNameVar() with the given function instance's

compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp

+30-7
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,18 @@ struct LLVMRustMCDCBranchParameters {
8282
int16_t ConditionIDs[2];
8383
};
8484

85-
union LLVMRustMCDCParametersPayload {
86-
LLVMRustMCDCDecisionParameters DecisionParameters;
87-
LLVMRustMCDCBranchParameters BranchParameters;
88-
};
89-
9085
struct LLVMRustMCDCParameters {
9186
LLVMRustMCDCParametersTag Tag;
92-
LLVMRustMCDCParametersPayload Payload;
87+
LLVMRustMCDCDecisionParameters DecisionParameters;
88+
LLVMRustMCDCBranchParameters BranchParameters;
9389
};
9490

91+
// LLVM representations for `MCDCParameters` evolved from LLVM 18 to 19.
92+
// Look at representations in 18
93+
// https://github.com/rust-lang/llvm-project/blob/66a2881a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L253-L263
94+
// and representations in 19
95+
// https://github.com/llvm/llvm-project/blob/843cc474faefad1d639f4c44c1cf3ad7dbda76c8/llvm/include/llvm/ProfileData/Coverage/MCDCTypes.h
96+
#if LLVM_VERSION_GE(18, 0) && LLVM_VERSION_LT(19, 0)
9597
static coverage::CounterMappingRegion::MCDCParameters
9698
fromRust(LLVMRustMCDCParameters Params) {
9799
auto parameter = coverage::CounterMappingRegion::MCDCParameters{};
@@ -117,6 +119,27 @@ fromRust(LLVMRustMCDCParameters Params) {
117119
}
118120
report_fatal_error("Bad LLVMRustMCDCParametersTag!");
119121
}
122+
#elif LLVM_VERSION_GE(19, 0)
123+
static coverage::mcdc::MCDCParameters fromRust(LLVMRustMCDCParameters Params) {
124+
switch (Params.Tag) {
125+
case LLVMRustMCDCParametersTag::None:
126+
return coverage::mcdc::MCDCParameters;
127+
case LLVMRustMCDCParametersTag::Decision:
128+
return coverage::mcdc::DecisionParameters(
129+
Params.Payload.DecisionParameters.BitmapIdx,
130+
Params.Payload.DecisionParameters.NumConditions);
131+
case LLVMRustMCDCParametersTag::Branch:
132+
return coverage::mcdc::BranchParameters(
133+
static_cast<coverage::mcdc::ConditionID>(
134+
Params.Payload.BranchParameters.ConditionID),
135+
{static_cast<coverage::CounterMappingRegion::MCDCConditionID>(
136+
Params.Payload.BranchParameters.ConditionIDs[0]),
137+
static_cast<coverage::CounterMappingRegion::MCDCConditionID>(
138+
Params.Payload.BranchParameters.ConditionIDs[1])});
139+
}
140+
report_fatal_error("Bad LLVMRustMCDCParametersTag!");
141+
}
142+
#endif
120143

121144
// FFI equivalent of struct `llvm::coverage::CounterMappingRegion`
122145
// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L211-L304
@@ -191,7 +214,7 @@ extern "C" void LLVMRustCoverageWriteMappingToBuffer(
191214
RustMappingRegions, NumMappingRegions)) {
192215
MappingRegions.emplace_back(
193216
fromRust(Region.Count), fromRust(Region.FalseCount),
194-
#if LLVM_VERSION_GE(18, 0) && LLVM_VERSION_LT(19, 0)
217+
#if LLVM_VERSION_GE(18, 0)
195218
fromRust(Region.MCDCParameters),
196219
#endif
197220
Region.FileID, Region.ExpandedFileID, // File IDs, then region info.

compiler/rustc_middle/src/mir/coverage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl MappingKind {
225225
let two = |a, b| Some(a).into_iter().chain(Some(b));
226226
match *self {
227227
Self::Code(term) => one(term),
228-
Self::Branch { true_term, false_term, .. } => two(true_term, false_term),
228+
Self::Branch { true_term, false_term } => two(true_term, false_term),
229229
Self::MCDCBranch { true_term, false_term, .. } => two(true_term, false_term),
230230
Self::MCDCDecision(_) => zero(),
231231
}

compiler/rustc_mir_transform/src/coverage/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,10 @@ fn inject_mcdc_statements<'tcx>(
233233
return;
234234
}
235235

236-
// Inject test vector update first because inject statement always inject new statement at head.
236+
// Inject test vector update first because `inject_statement` always insert new statement at head.
237237
for (end_bcbs, bitmap_idx) in
238238
coverage_spans.all_bcb_mappings().filter_map(|mapping| match &mapping.kind {
239-
BcbMappingKind::Decision { end_bcbs: end_bcb, bitmap_idx, .. } => {
240-
Some((end_bcb, *bitmap_idx))
241-
}
239+
BcbMappingKind::Decision { end_bcbs, bitmap_idx, .. } => Some((end_bcbs, *bitmap_idx)),
242240
_ => None,
243241
})
244242
{

compiler/rustc_session/src/options.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -947,13 +947,12 @@ mod parse {
947947
match option {
948948
"no-branch" => {
949949
slot.branch = false;
950+
slot.mcdc = false;
950951
}
951-
"branch" => {
952-
slot.branch = true;
953-
}
952+
"branch" => slot.branch = true,
954953
"mcdc" => {
955954
slot.branch = true;
956-
slot.mcdc = true
955+
slot.mcdc = true;
957956
}
958957
_ => return false,
959958
}

src/doc/unstable-book/src/compiler-flags/coverage-options.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ This option controls details of the coverage instrumentation performed by
55

66
Multiple options can be passed, separated by commas. Valid options are:
77

8-
- `no-branch`, `branch` or `mcdc`: `branch` enables branch coverage instrumentation and `mcdc` further enables modified condition/decision coverage instrumentation. `no-branch` disables branch coverage instrumentation, which is same as do not pass `branch` or `mcdc`
8+
- `no-branch`, `branch` or `mcdc`: `branch` enables branch coverage instrumentation and `mcdc` further enables modified condition/decision coverage instrumentation. `no-branch` disables branch coverage instrumentation as well as mcdc instrumentation, which is same as do not pass `branch` or `mcdc`.

0 commit comments

Comments
 (0)