Skip to content

Commit f4a7938

Browse files
committed
implemented query for coverage data
This commit adds a query that allows the CoverageData to be pulled from a call on tcx, avoiding the need to change the `codegen_intrinsic_call()` signature (no need to pass in the FunctionCx or any additional arguments. The commit does not change where/when the CoverageData is computed. It's still done in the `pass`, and saved in the MIR `Body`. See discussion (in progress) here: rust-lang#73488 (comment)
1 parent 933fe80 commit f4a7938

File tree

6 files changed

+25
-13
lines changed

6 files changed

+25
-13
lines changed

src/librustc_codegen_llvm/intrinsic.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_codegen_ssa::common::TypeKind;
1616
use rustc_codegen_ssa::glue;
1717
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
1818
use rustc_codegen_ssa::mir::place::PlaceRef;
19-
use rustc_codegen_ssa::mir::FunctionCx;
2019
use rustc_codegen_ssa::traits::*;
2120
use rustc_codegen_ssa::MemFlags;
2221
use rustc_hir as hir;
@@ -82,14 +81,14 @@ fn get_simple_intrinsic(cx: &CodegenCx<'ll, '_>, name: &str) -> Option<&'ll Valu
8281
}
8382

8483
impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
85-
fn codegen_intrinsic_call<'b, Bx: BuilderMethods<'b, 'tcx>>(
84+
fn codegen_intrinsic_call(
8685
&mut self,
87-
fx: &FunctionCx<'b, 'tcx, Bx>,
8886
instance: ty::Instance<'tcx>,
8987
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
9088
args: &[OperandRef<'tcx, &'ll Value>],
9189
llresult: &'ll Value,
9290
span: Span,
91+
caller_instance: ty::Instance<'tcx>,
9392
) {
9493
let tcx = self.tcx;
9594
let callee_ty = instance.monomorphic_ty(tcx);
@@ -141,8 +140,11 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
141140
self.call(llfn, &[], None)
142141
}
143142
"count_code_region" => {
144-
let coverage_data = fx.mir.coverage_data.as_ref().unwrap();
145-
let mangled_fn = tcx.symbol_name(fx.instance);
143+
let coverage_data = tcx
144+
.coverage_data(caller_instance.def_id())
145+
.as_ref()
146+
.expect("LLVM intrinsic count_code_region call has associated coverage_data");
147+
let mangled_fn = tcx.symbol_name(caller_instance);
146148
let (mangled_fn_name, _len_val) = self.const_str(mangled_fn.name);
147149
let hash = self.const_u64(coverage_data.hash);
148150
let index = args[0].immediate();

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,12 +688,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
688688
.collect();
689689

690690
bx.codegen_intrinsic_call(
691-
self,
692691
*instance.as_ref().unwrap(),
693692
&fn_abi,
694693
&args,
695694
dest,
696695
terminator.source_info.span,
696+
self.instance,
697697
);
698698

699699
if let ReturnDest::IndirectOperand(dst, _) = ret_dest {

src/librustc_codegen_ssa/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use self::operand::{OperandRef, OperandValue};
2121

2222
/// Master context for codegenning from MIR.
2323
pub struct FunctionCx<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
24-
pub instance: Instance<'tcx>,
24+
instance: Instance<'tcx>,
2525

26-
pub mir: &'tcx mir::Body<'tcx>,
26+
mir: &'tcx mir::Body<'tcx>,
2727

2828
debug_context: Option<FunctionDebugContext<Bx::DIScope>>,
2929

src/librustc_codegen_ssa/traits/intrinsic.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use super::BackendTypes;
22
use crate::mir::operand::OperandRef;
3-
use crate::mir::FunctionCx;
4-
use crate::traits::BuilderMethods;
53
use rustc_middle::ty::{self, Ty};
64
use rustc_span::Span;
75
use rustc_target::abi::call::FnAbi;
@@ -10,14 +8,14 @@ pub trait IntrinsicCallMethods<'tcx>: BackendTypes {
108
/// Remember to add all intrinsics here, in librustc_typeck/check/mod.rs,
119
/// and in libcore/intrinsics.rs; if you need access to any llvm intrinsics,
1210
/// add them to librustc_codegen_llvm/context.rs
13-
fn codegen_intrinsic_call<'a, Bx: BuilderMethods<'a, 'tcx>>(
11+
fn codegen_intrinsic_call(
1412
&mut self,
15-
fx: &FunctionCx<'a, 'tcx, Bx>,
1613
instance: ty::Instance<'tcx>,
1714
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
1815
args: &[OperandRef<'tcx, Self::Value>],
1916
llresult: Self::Value,
2017
span: Span,
18+
caller_instance: ty::Instance<'tcx>,
2119
);
2220

2321
fn abort(&mut self);

src/librustc_middle/query/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ rustc_queries! {
214214
cache_on_disk_if { key.is_local() }
215215
}
216216

217+
query coverage_data(key: DefId) -> Option<mir::CoverageData> {
218+
desc { |tcx| "retrieving coverage data, if computed from MIR for `{}`", tcx.def_path_str(key) }
219+
storage(ArenaCacheSelector<'tcx>)
220+
cache_on_disk_if { key.is_local() }
221+
}
222+
217223
query promoted_mir(key: DefId) -> IndexVec<mir::Promoted, mir::Body<'tcx>> {
218224
desc { |tcx| "optimizing promoted MIR for `{}`", tcx.def_path_str(key) }
219225
storage(ArenaCacheSelector<'tcx>)

src/librustc_mir/transform/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
66
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
77
use rustc_index::vec::IndexVec;
88
use rustc_middle::mir::visit::Visitor as _;
9-
use rustc_middle::mir::{traversal, Body, ConstQualifs, MirPhase, Promoted};
9+
use rustc_middle::mir::{traversal, Body, ConstQualifs, CoverageData, MirPhase, Promoted};
1010
use rustc_middle::ty::query::Providers;
1111
use rustc_middle::ty::steal::Steal;
1212
use rustc_middle::ty::{InstanceDef, TyCtxt, TypeFoldable};
@@ -53,6 +53,7 @@ pub(crate) fn provide(providers: &mut Providers<'_>) {
5353
mir_drops_elaborated_and_const_checked,
5454
optimized_mir,
5555
is_mir_available,
56+
coverage_data,
5657
promoted_mir,
5758
..*providers
5859
};
@@ -422,6 +423,11 @@ fn run_optimization_passes<'tcx>(
422423
);
423424
}
424425

426+
fn coverage_data(tcx: TyCtxt<'_>, def_id: DefId) -> Option<CoverageData> {
427+
let body = tcx.optimized_mir(def_id);
428+
body.coverage_data.clone()
429+
}
430+
425431
fn optimized_mir(tcx: TyCtxt<'_>, def_id: DefId) -> Body<'_> {
426432
if tcx.is_constructor(def_id) {
427433
// There's no reason to run all of the MIR passes on constructors when

0 commit comments

Comments
 (0)