Skip to content

Commit d07ef5b

Browse files
committed
coverage: Add LLVM plumbing for expansion regions
This is currently unused, but paves the way for future work on expansion regions without having to worry about the FFI parts.
1 parent 2947be7 commit d07ef5b

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ pub(crate) struct CoverageSpan {
146146
#[derive(Clone, Debug, Default)]
147147
pub(crate) struct Regions {
148148
pub(crate) code_regions: Vec<CodeRegion>,
149+
pub(crate) expansion_regions: Vec<ExpansionRegion>,
149150
pub(crate) branch_regions: Vec<BranchRegion>,
150151
pub(crate) mcdc_branch_regions: Vec<MCDCBranchRegion>,
151152
pub(crate) mcdc_decision_regions: Vec<MCDCDecisionRegion>,
@@ -154,10 +155,16 @@ pub(crate) struct Regions {
154155
impl Regions {
155156
/// Returns true if none of this structure's tables contain any regions.
156157
pub(crate) fn has_no_regions(&self) -> bool {
157-
let Self { code_regions, branch_regions, mcdc_branch_regions, mcdc_decision_regions } =
158-
self;
158+
let Self {
159+
code_regions,
160+
expansion_regions,
161+
branch_regions,
162+
mcdc_branch_regions,
163+
mcdc_decision_regions,
164+
} = self;
159165

160166
code_regions.is_empty()
167+
&& expansion_regions.is_empty()
161168
&& branch_regions.is_empty()
162169
&& mcdc_branch_regions.is_empty()
163170
&& mcdc_decision_regions.is_empty()
@@ -172,6 +179,14 @@ pub(crate) struct CodeRegion {
172179
pub(crate) counter: Counter,
173180
}
174181

182+
/// Must match the layout of `LLVMRustCoverageExpansionRegion`.
183+
#[derive(Clone, Debug)]
184+
#[repr(C)]
185+
pub(crate) struct ExpansionRegion {
186+
pub(crate) cov_span: CoverageSpan,
187+
pub(crate) expanded_file_id: u32,
188+
}
189+
175190
/// Must match the layout of `LLVMRustCoverageBranchRegion`.
176191
#[derive(Clone, Debug)]
177192
#[repr(C)]

compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,18 @@ pub(crate) fn write_function_mappings_to_buffer(
6363
expressions: &[ffi::CounterExpression],
6464
regions: &ffi::Regions,
6565
) -> Vec<u8> {
66-
let ffi::Regions { code_regions, branch_regions, mcdc_branch_regions, mcdc_decision_regions } =
67-
regions;
66+
let ffi::Regions {
67+
code_regions,
68+
expansion_regions,
69+
branch_regions,
70+
mcdc_branch_regions,
71+
mcdc_decision_regions,
72+
} = regions;
73+
74+
// SAFETY:
75+
// - All types are FFI-compatible and have matching representations in Rust/C++.
76+
// - For pointer/length pairs, the pointer and length come from the same vector or slice.
77+
// - C++ code does not retain any pointers after the call returns.
6878
llvm::build_byte_buffer(|buffer| unsafe {
6979
llvm::LLVMRustCoverageWriteFunctionMappingsToBuffer(
7080
virtual_file_mapping.as_ptr(),
@@ -73,6 +83,8 @@ pub(crate) fn write_function_mappings_to_buffer(
7383
expressions.len(),
7484
code_regions.as_ptr(),
7585
code_regions.len(),
86+
expansion_regions.as_ptr(),
87+
expansion_regions.len(),
7688
branch_regions.as_ptr(),
7789
branch_regions.len(),
7890
mcdc_branch_regions.as_ptr(),

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,18 @@ fn fill_region_tables<'tcx>(
120120
// Associate that global file ID with a local file ID for this function.
121121
let local_file_id = covfun.virtual_file_mapping.local_id_for_global(global_file_id);
122122

123-
let ffi::Regions { code_regions, branch_regions, mcdc_branch_regions, mcdc_decision_regions } =
124-
&mut covfun.regions;
125-
126123
let make_cov_span =
127124
|span: Span| spans::make_coverage_span(local_file_id, source_map, &source_file, span);
128125
let discard_all = tcx.sess.coverage_discard_all_spans_in_codegen();
129126

127+
let ffi::Regions {
128+
code_regions,
129+
expansion_regions: _, // FIXME(Zalathar): Fill out support for expansion regions
130+
branch_regions,
131+
mcdc_branch_regions,
132+
mcdc_decision_regions,
133+
} = &mut covfun.regions;
134+
130135
// For each counter/region pair in this function+file, convert it to a
131136
// form suitable for FFI.
132137
for &Mapping { ref kind, span } in &fn_cov_info.mappings {

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,8 @@ unsafe extern "C" {
20192019
NumExpressions: size_t,
20202020
CodeRegions: *const crate::coverageinfo::ffi::CodeRegion,
20212021
NumCodeRegions: size_t,
2022+
ExpansionRegions: *const crate::coverageinfo::ffi::ExpansionRegion,
2023+
NumExpansionRegions: size_t,
20222024
BranchRegions: *const crate::coverageinfo::ffi::BranchRegion,
20232025
NumBranchRegions: size_t,
20242026
MCDCBranchRegions: *const crate::coverageinfo::ffi::MCDCBranchRegion,

compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ struct LLVMRustCoverageCodeRegion {
7777
LLVMRustCounter Count;
7878
};
7979

80+
// Must match the layout of
81+
// `rustc_codegen_llvm::coverageinfo::ffi::ExpansionRegion`.
82+
struct LLVMRustCoverageExpansionRegion {
83+
LLVMRustCoverageSpan Span;
84+
uint32_t ExpandedFileID;
85+
};
86+
8087
// Must match the layout of
8188
// `rustc_codegen_llvm::coverageinfo::ffi::BranchRegion`.
8289
struct LLVMRustCoverageBranchRegion {
@@ -151,6 +158,8 @@ extern "C" void LLVMRustCoverageWriteFunctionMappingsToBuffer(
151158
const unsigned *VirtualFileMappingIDs, size_t NumVirtualFileMappingIDs,
152159
const LLVMRustCounterExpression *RustExpressions, size_t NumExpressions,
153160
const LLVMRustCoverageCodeRegion *CodeRegions, size_t NumCodeRegions,
161+
const LLVMRustCoverageExpansionRegion *ExpansionRegions,
162+
size_t NumExpansionRegions,
154163
const LLVMRustCoverageBranchRegion *BranchRegions, size_t NumBranchRegions,
155164
const LLVMRustCoverageMCDCBranchRegion *MCDCBranchRegions,
156165
size_t NumMCDCBranchRegions,
@@ -179,6 +188,13 @@ extern "C" void LLVMRustCoverageWriteFunctionMappingsToBuffer(
179188
Region.Span.ColumnStart, Region.Span.LineEnd, Region.Span.ColumnEnd));
180189
}
181190

191+
// Expansion regions:
192+
for (const auto &Region : ArrayRef(ExpansionRegions, NumExpansionRegions)) {
193+
MappingRegions.push_back(coverage::CounterMappingRegion::makeExpansion(
194+
Region.Span.FileID, Region.ExpandedFileID, Region.Span.LineStart,
195+
Region.Span.ColumnStart, Region.Span.LineEnd, Region.Span.ColumnEnd));
196+
}
197+
182198
// Branch regions:
183199
for (const auto &Region : ArrayRef(BranchRegions, NumBranchRegions)) {
184200
MappingRegions.push_back(coverage::CounterMappingRegion::makeBranchRegion(

0 commit comments

Comments
 (0)