Skip to content

Commit 198ba3b

Browse files
committed
Injecting expressions in place of counters where helpful
Implementing the Graph traits for the BasicCoverageBlock graph. optimized replacement of counters with expressions plus new BCB graphviz * Avoid adding coverage to unreachable blocks. * Special case for Goto at the end of the body. Make it non-reportable. Improved debugging and formatting options (from env) Don't automatically add counters to BCBs without CoverageSpans. They may still get counters but only if there are dependencies from other BCBs that have spans, I think. Make CodeRegions optional for Counters too. It is possible to inject counters (`llvm.instrprof.increment` intrinsic calls without corresponding code regions in the coverage map. An expression can still uses these counter values. Refactored instrument_coverage.rs -> instrument_coverage/mod.rs, and then broke up the mod into multiple files. Compiling with coverage, with the expression optimization, works on the json5format crate and its dependencies. Refactored debug features from mod.rs to debug.rs
1 parent 3291d28 commit 198ba3b

File tree

75 files changed

+1984
-603
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1984
-603
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
5858
unsafe { llvm::LLVMRustCoverageCreatePGOFuncNameVar(llfn, mangled_fn_name.as_ptr()) }
5959
}
6060

61-
fn set_function_source_hash(&mut self, instance: Instance<'tcx>, function_source_hash: u64) -> bool {
61+
fn set_function_source_hash(
62+
&mut self,
63+
instance: Instance<'tcx>,
64+
function_source_hash: u64,
65+
) -> bool {
6266
if let Some(coverage_context) = self.coverage_context() {
6367
debug!(
6468
"ensuring function source hash is set for instance={:?}; function_source_hash={}",
@@ -69,6 +73,7 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
6973
.entry(instance)
7074
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
7175
.set_function_source_hash(function_source_hash);
76+
true
7277
} else {
7378
false
7479
}
@@ -92,6 +97,7 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
9297
.entry(instance)
9398
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
9499
.add_counter(function_source_hash, id, region);
100+
true
95101
} else {
96102
false
97103
}
@@ -105,8 +111,8 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
105111
op: Op,
106112
rhs: ExpressionOperandId,
107113
region: Option<CodeRegion>,
108-
) {
109-
if let Some(coverage_context) = self.coverage_context() -> bool {
114+
) -> bool {
115+
if let Some(coverage_context) = self.coverage_context() {
110116
debug!(
111117
"adding counter expression to coverage_map: instance={:?}, id={:?}, {:?} {:?} {:?}; \
112118
region: {:?}",
@@ -117,19 +123,24 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
117123
.entry(instance)
118124
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
119125
.add_counter_expression(id, lhs, op, rhs, region);
126+
true
120127
} else {
121128
false
122129
}
123130
}
124131

125132
fn add_coverage_unreachable(&mut self, instance: Instance<'tcx>, region: CodeRegion) -> bool {
126133
if let Some(coverage_context) = self.coverage_context() {
127-
debug!("adding unreachable code to coverage_map: instance={:?}, at {:?}", instance, region,);
134+
debug!(
135+
"adding unreachable code to coverage_map: instance={:?}, at {:?}",
136+
instance, region,
137+
);
128138
let mut coverage_map = coverage_context.function_coverage_map.borrow_mut();
129139
coverage_map
130140
.entry(instance)
131141
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
132142
.add_unreachable_region(region);
143+
true
133144
} else {
134145
false
135146
}

compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2424
let fn_name = bx.create_pgo_func_name_var(self.instance);
2525
let hash = bx.const_u64(function_source_hash);
2626
let num_counters = bx.const_u32(coverageinfo.num_counters);
27-
let id = bx.const_u32(u32::from(id));
27+
let index = bx.const_u32(u32::from(id));
2828
debug!(
2929
"codegen intrinsic instrprof.increment(fn_name={:?}, hash={:?}, num_counters={:?}, index={:?})",
30-
fn_name, hash, num_counters, id,
30+
fn_name, hash, num_counters, index,
3131
);
3232
bx.instrprof_increment(fn_name, hash, num_counters, index);
3333
}

compiler/rustc_codegen_ssa/src/traits/coverageinfo.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ pub trait CoverageInfoBuilderMethods<'tcx>: BackendTypes {
1111

1212
/// Returns true if the function source hash was added to the coverage map; false if
1313
/// `-Z instrument-coverage` is not enabled (a coverage map is not being generated).
14-
fn set_function_source_hash(&mut self, instance: Instance<'tcx>, function_source_hash: u64) -> bool;
14+
fn set_function_source_hash(
15+
&mut self,
16+
instance: Instance<'tcx>,
17+
function_source_hash: u64,
18+
) -> bool;
1519

1620
/// Returns true if the counter was added to the coverage map; false if `-Z instrument-coverage`
1721
/// is not enabled (a coverage map is not being generated).
1822
fn add_coverage_counter(
1923
&mut self,
2024
instance: Instance<'tcx>,
2125
function_source_hash: u64,
22-
id: CounterValueReference,
26+
index: CounterValueReference,
2327
region: CodeRegion,
2428
) -> bool;
2529

@@ -33,7 +37,7 @@ pub trait CoverageInfoBuilderMethods<'tcx>: BackendTypes {
3337
op: Op,
3438
rhs: ExpressionOperandId,
3539
region: Option<CodeRegion>,
36-
);
40+
) -> bool;
3741

3842
/// Returns true if the region was added to the coverage map; false if `-Z instrument-coverage`
3943
/// is not enabled (a coverage map is not being generated).

0 commit comments

Comments
 (0)