Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ab92699

Browse files
committed
Unbox and unwrap the contents of StatementKind::Coverage
The payload of coverage statements was historically a structure with several fields, so it was boxed to avoid bloating `StatementKind`. Now that the payload is a single relatively-small enum, we can replace `Box<Coverage>` with just `CoverageKind`. This patch also adds a size assertion for `StatementKind`, to avoid accidentally bloating it in the future.
1 parent c3b05c6 commit ab92699

File tree

15 files changed

+44
-68
lines changed

15 files changed

+44
-68
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use rustc_codegen_ssa::traits::CoverageInfoBuilderMethods;
2-
use rustc_middle::mir::Coverage;
2+
use rustc_middle::mir::coverage::CoverageKind;
33
use rustc_middle::ty::Instance;
44

55
use crate::builder::Builder;
66

77
impl<'a, 'gcc, 'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
8-
fn add_coverage(&mut self, _instance: Instance<'tcx>, _coverage: &Coverage) {
8+
fn add_coverage(&mut self, _instance: Instance<'tcx>, _kind: &CoverageKind) {
99
// TODO(antoyo)
1010
}
1111
}

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
1414
use rustc_llvm::RustString;
1515
use rustc_middle::bug;
1616
use rustc_middle::mir::coverage::CoverageKind;
17-
use rustc_middle::mir::Coverage;
1817
use rustc_middle::ty::layout::HasTyCtxt;
1918
use rustc_middle::ty::Instance;
2019

@@ -75,7 +74,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
7574

7675
impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
7776
#[instrument(level = "debug", skip(self))]
78-
fn add_coverage(&mut self, instance: Instance<'tcx>, coverage: &Coverage) {
77+
fn add_coverage(&mut self, instance: Instance<'tcx>, kind: &CoverageKind) {
7978
// Our caller should have already taken care of inlining subtleties,
8079
// so we can assume that counter/expression IDs in this coverage
8180
// statement are meaningful for the given instance.
@@ -98,7 +97,6 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
9897
.entry(instance)
9998
.or_insert_with(|| FunctionCoverageCollector::new(instance, function_coverage_info));
10099

101-
let Coverage { kind } = coverage;
102100
match *kind {
103101
CoverageKind::SpanMarker | CoverageKind::BlockMarker { .. } => unreachable!(
104102
"marker statement {kind:?} should have been removed by CleanupPostBorrowck"
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::traits::*;
22

3-
use rustc_middle::mir::Coverage;
3+
use rustc_middle::mir::coverage::CoverageKind;
44
use rustc_middle::mir::SourceScope;
55

66
use super::FunctionCx;
77

88
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
9-
pub fn codegen_coverage(&self, bx: &mut Bx, coverage: &Coverage, scope: SourceScope) {
9+
pub fn codegen_coverage(&self, bx: &mut Bx, kind: &CoverageKind, scope: SourceScope) {
1010
// Determine the instance that coverage data was originally generated for.
1111
let instance = if let Some(inlined) = scope.inlined_instance(&self.mir.source_scopes) {
1212
self.monomorphize(inlined)
@@ -15,6 +15,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1515
};
1616

1717
// Handle the coverage info in a backend-specific way.
18-
bx.add_coverage(instance, coverage);
18+
bx.add_coverage(instance, kind);
1919
}
2020
}

compiler/rustc_codegen_ssa/src/mir/statement.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6464
cg_indirect_place.storage_dead(bx);
6565
}
6666
}
67-
mir::StatementKind::Coverage(box ref coverage) => {
68-
self.codegen_coverage(bx, coverage, statement.source_info.scope);
67+
mir::StatementKind::Coverage(ref kind) => {
68+
self.codegen_coverage(bx, kind, statement.source_info.scope);
6969
}
7070
mir::StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(ref op)) => {
7171
if !matches!(bx.tcx().sess.opts.optimize, OptLevel::No | OptLevel::Less) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use super::BackendTypes;
2-
use rustc_middle::mir::Coverage;
2+
use rustc_middle::mir::coverage::CoverageKind;
33
use rustc_middle::ty::Instance;
44

55
pub trait CoverageInfoBuilderMethods<'tcx>: BackendTypes {
66
/// Handle the MIR coverage info in a backend-specific way.
77
///
88
/// This can potentially be a no-op in backends that don't support
99
/// coverage instrumentation.
10-
fn add_coverage(&mut self, instance: Instance<'tcx>, coverage: &Coverage);
10+
fn add_coverage(&mut self, instance: Instance<'tcx>, kind: &CoverageKind);
1111
}

compiler/rustc_const_eval/src/transform/validate.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
346346
self.fail(location, format!("explicit `{kind:?}` is forbidden"));
347347
}
348348
}
349-
StatementKind::Coverage(coverage) => {
350-
let kind = &coverage.kind;
349+
StatementKind::Coverage(kind) => {
351350
if self.mir_phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup)
352351
&& let CoverageKind::BlockMarker { .. } | CoverageKind::SpanMarker { .. } = kind
353352
{

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::mir::interpret::{
1313
Provenance,
1414
};
1515
use rustc_middle::mir::visit::Visitor;
16-
use rustc_middle::mir::{self, *};
16+
use rustc_middle::mir::*;
1717
use rustc_target::abi::Size;
1818

1919
const INDENT: &str = " ";
@@ -711,7 +711,7 @@ impl Debug for Statement<'_> {
711711
AscribeUserType(box (ref place, ref c_ty), ref variance) => {
712712
write!(fmt, "AscribeUserType({place:?}, {variance:?}, {c_ty:?})")
713713
}
714-
Coverage(box mir::Coverage { ref kind }) => write!(fmt, "Coverage::{kind:?}"),
714+
Coverage(ref kind) => write!(fmt, "Coverage::{kind:?}"),
715715
Intrinsic(box ref intrinsic) => write!(fmt, "{intrinsic}"),
716716
ConstEvalCounter => write!(fmt, "ConstEvalCounter"),
717717
Nop => write!(fmt, "nop"),

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ pub enum StatementKind<'tcx> {
373373
///
374374
/// Interpreters and codegen backends that don't support coverage instrumentation
375375
/// can usually treat this as a no-op.
376-
Coverage(Box<Coverage>),
376+
Coverage(CoverageKind),
377377

378378
/// Denotes a call to an intrinsic that does not require an unwind path and always returns.
379379
/// This avoids adding a new block and a terminator for simple intrinsics.
@@ -517,12 +517,6 @@ pub enum FakeReadCause {
517517
ForIndex,
518518
}
519519

520-
#[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
521-
#[derive(TypeFoldable, TypeVisitable)]
522-
pub struct Coverage {
523-
pub kind: CoverageKind,
524-
}
525-
526520
#[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)]
527521
#[derive(TypeFoldable, TypeVisitable)]
528522
pub struct CopyNonOverlapping<'tcx> {
@@ -1465,5 +1459,6 @@ mod size_asserts {
14651459
static_assert_size!(Place<'_>, 16);
14661460
static_assert_size!(PlaceElem<'_>, 24);
14671461
static_assert_size!(Rvalue<'_>, 40);
1462+
static_assert_size!(StatementKind<'_>, 16);
14681463
// tidy-alphabetical-end
14691464
}

compiler/rustc_middle/src/mir/visit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ macro_rules! make_mir_visitor {
156156

157157
fn visit_coverage(
158158
&mut self,
159-
coverage: & $($mutability)? Coverage,
159+
kind: & $($mutability)? coverage::CoverageKind,
160160
location: Location,
161161
) {
162-
self.super_coverage(coverage, location);
162+
self.super_coverage(kind, location);
163163
}
164164

165165
fn visit_retag(
@@ -803,7 +803,7 @@ macro_rules! make_mir_visitor {
803803
}
804804

805805
fn super_coverage(&mut self,
806-
_coverage: & $($mutability)? Coverage,
806+
_kind: & $($mutability)? coverage::CoverageKind,
807807
_location: Location) {
808808
}
809809

compiler/rustc_mir_build/src/build/cfg.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ impl<'tcx> CFG<'tcx> {
107107
/// This results in more accurate coverage reports for certain kinds of
108108
/// syntax (e.g. `continue` or `if !`) that would otherwise not appear in MIR.
109109
pub(crate) fn push_coverage_span_marker(&mut self, block: BasicBlock, source_info: SourceInfo) {
110-
let kind = StatementKind::Coverage(Box::new(Coverage {
111-
kind: coverage::CoverageKind::SpanMarker,
112-
}));
110+
let kind = StatementKind::Coverage(coverage::CoverageKind::SpanMarker);
113111
let stmt = Statement { source_info, kind };
114112
self.push(block, stmt);
115113
}

0 commit comments

Comments
 (0)