Skip to content

Commit aed8cc9

Browse files
committed
mcdc-coverage: Add FFI MCDC Params for coverage mappings encoding
1 parent 4493f11 commit aed8cc9

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub enum RegionKind {
102102

103103
/// A DecisionRegion represents a top-level boolean expression and is
104104
/// associated with a variable length bitmap index and condition number.
105+
/// FIXME(dprn): Remove unused variables.
105106
#[allow(dead_code)]
106107
MCDCDecisionRegion = 5,
107108

@@ -110,6 +111,28 @@ pub enum RegionKind {
110111
MCDCBranchRegion = 6,
111112
}
112113

114+
/// This struct provides LLVM's representation of "MCDCParameters" that may be defined for a
115+
/// Coverage Mapping Region.
116+
///
117+
/// Correspond to struct `llvm::coverage::CounterMappingRegion::MCDCParameters`
118+
///
119+
/// Must match The layout of `LLVMRustMCDCParameters`
120+
#[derive(Copy, Clone, Debug, Default)]
121+
#[repr(C)]
122+
pub struct MCDCParameters {
123+
/// Byte Index of Bitmap Coverage Object for a Decision Region.
124+
bitmap_idx: u32,
125+
126+
/// Number of Conditions used for a Decision Region.
127+
num_conditions: u32,
128+
129+
/// IDs used to represent a branch region and other branch regions
130+
/// evaluated based on True and False branches.
131+
id: u32,
132+
true_id: u32,
133+
false_id: u32,
134+
}
135+
113136
/// This struct provides LLVM's representation of a "CoverageMappingRegion", encoded into the
114137
/// coverage map, in accordance with the
115138
/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
@@ -155,6 +178,8 @@ pub struct CounterMappingRegion {
155178
end_col: u32,
156179

157180
kind: RegionKind,
181+
182+
mcdc_params: MCDCParameters,
158183
}
159184

160185
impl CounterMappingRegion {
@@ -181,6 +206,7 @@ impl CounterMappingRegion {
181206
start_col,
182207
end_line,
183208
end_col,
209+
None,
184210
),
185211
}
186212
}
@@ -203,9 +229,11 @@ impl CounterMappingRegion {
203229
end_line,
204230
end_col,
205231
kind: RegionKind::CodeRegion,
232+
mcdc_params: Default::default(),
206233
}
207234
}
208235

236+
/// - `mcdc_params` should be None when MCDC is disabled.
209237
pub(crate) fn branch_region(
210238
counter: Counter,
211239
false_counter: Counter,
@@ -214,7 +242,13 @@ impl CounterMappingRegion {
214242
start_col: u32,
215243
end_line: u32,
216244
end_col: u32,
245+
mcdc_params: Option<MCDCParameters>,
217246
) -> Self {
247+
let (kind, mcdc_params) = match mcdc_params {
248+
None => (RegionKind::BranchRegion, Default::default()),
249+
Some(params) => (RegionKind::MCDCBranchRegion, params),
250+
};
251+
218252
Self {
219253
counter,
220254
false_counter,
@@ -224,7 +258,36 @@ impl CounterMappingRegion {
224258
start_col,
225259
end_line,
226260
end_col,
227-
kind: RegionKind::BranchRegion,
261+
kind,
262+
mcdc_params,
263+
}
264+
}
265+
266+
#[allow(dead_code)]
267+
pub(crate) fn decision_region(
268+
bitmap_idx: u32,
269+
num_conditions: u32,
270+
file_id: u32,
271+
start_line: u32,
272+
start_col: u32,
273+
end_line: u32,
274+
end_col: u32,
275+
) -> Self {
276+
Self {
277+
counter: Counter::ZERO,
278+
false_counter: Counter::ZERO,
279+
file_id,
280+
expanded_file_id: 0,
281+
start_line,
282+
start_col,
283+
end_line,
284+
end_col,
285+
kind: RegionKind::MCDCDecisionRegion,
286+
mcdc_params: MCDCParameters {
287+
bitmap_idx,
288+
num_conditions,
289+
.. Default::default()
290+
},
228291
}
229292
}
230293

@@ -249,6 +312,7 @@ impl CounterMappingRegion {
249312
end_line,
250313
end_col,
251314
kind: RegionKind::ExpansionRegion,
315+
mcdc_params: Default::default(),
252316
}
253317
}
254318

@@ -272,6 +336,7 @@ impl CounterMappingRegion {
272336
end_line,
273337
end_col,
274338
kind: RegionKind::SkippedRegion,
339+
mcdc_params: Default::default(),
275340
}
276341
}
277342

@@ -296,6 +361,7 @@ impl CounterMappingRegion {
296361
end_line,
297362
end_col: (1_u32 << 31) | end_col,
298363
kind: RegionKind::GapRegion,
364+
mcdc_params: Default::default(),
299365
}
300366
}
301367
}

compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ fromRust(LLVMRustCounterMappingRegionKind Kind) {
6868
report_fatal_error("Bad LLVMRustCounterMappingRegionKind!");
6969
}
7070

71+
// FFI equivalent of struct `llvm::coverage::CounterMappingRegion::MCDCParameters`
72+
// https://github.com/rust-lang/llvm-project/blob/66a2881a/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L253-L263
73+
struct LLVMRustMCDCParameters {
74+
/// Byte Index of Bitmap Coverage Object for a Decision Region.
75+
uint32_t BitmapIdx = 0;
76+
77+
/// Number of Conditions used for a Decision Region.
78+
uint32_t NumConditions = 0;
79+
80+
/// IDs used to represent a branch region and other branch regions
81+
/// evaluated based on True and False branches.
82+
uint32_t ID = 0;
83+
uint32_t TrueID = 0;
84+
uint32_t FalseID = 0;
85+
};
86+
87+
static coverage::CounterMappingRegion::MCDCParameters
88+
fromRust(LLVMRustMCDCParameters MCDCParams) {
89+
return coverage::CounterMappingRegion::MCDCParameters{
90+
MCDCParams.BitmapIdx, MCDCParams.NumConditions,
91+
MCDCParams.ID, MCDCParams.TrueID, MCDCParams.FalseID
92+
};
93+
}
94+
7195
// FFI equivalent of struct `llvm::coverage::CounterMappingRegion`
7296
// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L211-L304
7397
struct LLVMRustCounterMappingRegion {
@@ -80,6 +104,7 @@ struct LLVMRustCounterMappingRegion {
80104
uint32_t LineEnd;
81105
uint32_t ColumnEnd;
82106
LLVMRustCounterMappingRegionKind Kind;
107+
LLVMRustMCDCParameters MCDCParams;
83108
};
84109

85110
// FFI equivalent of enum `llvm::coverage::CounterExpression::ExprKind`
@@ -146,7 +171,7 @@ extern "C" void LLVMRustCoverageWriteMappingToBuffer(
146171
MappingRegions.emplace_back(
147172
fromRust(Region.Count), fromRust(Region.FalseCount),
148173
#if LLVM_VERSION_GE(18, 0) && LLVM_VERSION_LT(19, 0)
149-
coverage::CounterMappingRegion::MCDCParameters{},
174+
fromRust(Region.MCDCParams),
150175
#endif
151176
Region.FileID, Region.ExpandedFileID,
152177
Region.LineStart, Region.ColumnStart, Region.LineEnd, Region.ColumnEnd,

0 commit comments

Comments
 (0)