Skip to content

Commit dcb4b24

Browse files
committed
Expression optimization is nearly working
I need to change how I determine if a target from a SwitchInt exits the loop or not, but this almost worked (and did successfully compile and create the right coverage). It just didn't select the right targets for the optimized expressions.
1 parent ea64143 commit dcb4b24

File tree

6 files changed

+729
-344
lines changed

6 files changed

+729
-344
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const COVMAP_VAR_ALIGN_BYTES: usize = 8;
2727

2828
/// A context object for maintaining all state needed by the coverageinfo module.
2929
pub struct CrateCoverageContext<'tcx> {
30-
// Coverage region data for each instrumented function identified by DefId.
30+
// Coverage data for each instrumented function identified by DefId.
3131
pub(crate) function_coverage_map: RefCell<FxHashMap<Instance<'tcx>, FunctionCoverage>>,
3232
}
3333

@@ -58,53 +58,50 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
5858
unsafe { llvm::LLVMRustCoverageCreatePGOFuncNameVar(llfn, mangled_fn_name.as_ptr()) }
5959
}
6060

61-
fn add_counter_region(
61+
fn add_coverage_counter(
6262
&mut self,
6363
instance: Instance<'tcx>,
6464
function_source_hash: u64,
6565
id: CounterValueReference,
6666
region: CodeRegion,
6767
) {
6868
debug!(
69-
"adding counter to coverage_regions: instance={:?}, function_source_hash={}, id={:?}, \
69+
"adding counter to coverage_map: instance={:?}, function_source_hash={}, id={:?}, \
7070
at {:?}",
7171
instance, function_source_hash, id, region,
7272
);
73-
let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
74-
coverage_regions
73+
let mut coverage_map = self.coverage_context().function_coverage_map.borrow_mut();
74+
coverage_map
7575
.entry(instance)
7676
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
7777
.add_counter(function_source_hash, id, region);
7878
}
7979

80-
fn add_counter_expression_region(
80+
fn add_coverage_counter_expression(
8181
&mut self,
8282
instance: Instance<'tcx>,
8383
id: InjectedExpressionIndex,
8484
lhs: ExpressionOperandId,
8585
op: Op,
8686
rhs: ExpressionOperandId,
87-
region: CodeRegion,
87+
region: Option<CodeRegion>,
8888
) {
8989
debug!(
90-
"adding counter expression to coverage_regions: instance={:?}, id={:?}, {:?} {:?} {:?}, \
91-
at {:?}",
90+
"adding counter expression to coverage_map: instance={:?}, id={:?}, {:?} {:?} {:?}; \
91+
region: {:?}",
9292
instance, id, lhs, op, rhs, region,
9393
);
94-
let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
95-
coverage_regions
94+
let mut coverage_map = self.coverage_context().function_coverage_map.borrow_mut();
95+
coverage_map
9696
.entry(instance)
9797
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
9898
.add_counter_expression(id, lhs, op, rhs, region);
9999
}
100100

101-
fn add_unreachable_region(&mut self, instance: Instance<'tcx>, region: CodeRegion) {
102-
debug!(
103-
"adding unreachable code to coverage_regions: instance={:?}, at {:?}",
104-
instance, region,
105-
);
106-
let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
107-
coverage_regions
101+
fn add_coverage_unreachable(&mut self, instance: Instance<'tcx>, region: CodeRegion) {
102+
debug!("adding unreachable code to coverage_map: instance={:?}, at {:?}", instance, region,);
103+
let mut coverage_map = self.coverage_context().function_coverage_map.borrow_mut();
104+
coverage_map
108105
.entry(instance)
109106
.or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
110107
.add_unreachable_region(region);

compiler/rustc_codegen_ssa/src/coverageinfo/map.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use rustc_middle::ty::Instance;
99
use rustc_middle::ty::TyCtxt;
1010

1111
#[derive(Clone, Debug)]
12-
pub struct ExpressionRegion {
12+
pub struct Expression {
1313
lhs: ExpressionOperandId,
1414
op: Op,
1515
rhs: ExpressionOperandId,
16-
region: CodeRegion,
16+
region: Option<CodeRegion>,
1717
}
1818

1919
/// Collects all of the coverage regions associated with (a) injected counters, (b) counter
@@ -31,7 +31,7 @@ pub struct ExpressionRegion {
3131
pub struct FunctionCoverage {
3232
source_hash: u64,
3333
counters: IndexVec<CounterValueReference, Option<CodeRegion>>,
34-
expressions: IndexVec<InjectedExpressionIndex, Option<ExpressionRegion>>,
34+
expressions: IndexVec<InjectedExpressionIndex, Option<Expression>>,
3535
unreachable_regions: Vec<CodeRegion>,
3636
}
3737

@@ -78,11 +78,11 @@ impl FunctionCoverage {
7878
lhs: ExpressionOperandId,
7979
op: Op,
8080
rhs: ExpressionOperandId,
81-
region: CodeRegion,
81+
region: Option<CodeRegion>,
8282
) {
8383
let expression_index = self.expression_index(u32::from(expression_id));
8484
self.expressions[expression_index]
85-
.replace(ExpressionRegion { lhs, op, rhs, region })
85+
.replace(Expression { lhs, op, rhs, region })
8686
.expect_none("add_counter_expression called with duplicate `id_descending_from_max`");
8787
}
8888

@@ -135,9 +135,9 @@ impl FunctionCoverage {
135135
// will be set, and after that, `new_indexes` will only be accessed using those same
136136
// indexes.
137137

138-
// Note that an `ExpressionRegion`s at any given index can include other expressions as
138+
// Note that an `Expression`s at any given index can include other expressions as
139139
// operands, but expression operands can only come from the subset of expressions having
140-
// `expression_index`s lower than the referencing `ExpressionRegion`. Therefore, it is
140+
// `expression_index`s lower than the referencing `Expression`. Therefore, it is
141141
// reasonable to look up the new index of an expression operand while the `new_indexes`
142142
// vector is only complete up to the current `ExpressionIndex`.
143143
let id_to_counter =
@@ -162,15 +162,15 @@ impl FunctionCoverage {
162162
}
163163
};
164164

165-
for (original_index, expression_region) in
165+
for (original_index, expression) in
166166
self.expressions.iter_enumerated().filter_map(|(original_index, entry)| {
167167
// Option::map() will return None to filter out missing expressions. This may happen
168168
// if, for example, a MIR-instrumented expression is removed during an optimization.
169-
entry.as_ref().map(|region| (original_index, region))
169+
entry.as_ref().map(|expression| (original_index, expression))
170170
})
171171
{
172-
let region = &expression_region.region;
173-
let ExpressionRegion { lhs, op, rhs, .. } = *expression_region;
172+
let optional_region = &expression.region;
173+
let Expression { lhs, op, rhs, .. } = *expression;
174174

175175
if let Some(Some((lhs_counter, rhs_counter))) =
176176
id_to_counter(&new_indexes, lhs).map(|lhs_counter| {
@@ -190,12 +190,14 @@ impl FunctionCoverage {
190190
rhs_counter,
191191
);
192192
debug!(
193-
"Adding expression {:?} = {:?} at {:?}",
194-
mapped_expression_index, expression, region
193+
"Adding expression {:?} = {:?}, region: {:?}",
194+
mapped_expression_index, expression, optional_region
195195
);
196196
counter_expressions.push(expression);
197197
new_indexes[original_index] = mapped_expression_index;
198-
expression_regions.push((Counter::expression(mapped_expression_index), region));
198+
if let Some(region) = optional_region {
199+
expression_regions.push((Counter::expression(mapped_expression_index), region));
200+
}
199201
}
200202
}
201203
(counter_expressions, expression_regions.into_iter())

compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1010
let Coverage { kind, code_region } = coverage;
1111
match kind {
1212
CoverageKind::Counter { function_source_hash, id } => {
13-
bx.add_counter_region(self.instance, function_source_hash, id, code_region);
13+
bx.add_coverage_counter(
14+
self.instance,
15+
function_source_hash,
16+
id,
17+
code_region.expect("counters always have code regions"),
18+
);
1419

1520
let coverageinfo = bx.tcx().coverageinfo(self.instance.def_id());
1621

@@ -25,10 +30,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2530
bx.instrprof_increment(fn_name, hash, num_counters, id);
2631
}
2732
CoverageKind::Expression { id, lhs, op, rhs } => {
28-
bx.add_counter_expression_region(self.instance, id, lhs, op, rhs, code_region);
33+
bx.add_coverage_counter_expression(self.instance, id, lhs, op, rhs, code_region);
2934
}
3035
CoverageKind::Unreachable => {
31-
bx.add_unreachable_region(self.instance, code_region);
36+
bx.add_coverage_unreachable(
37+
self.instance,
38+
code_region.expect("unreachable regions always have code regions"),
39+
);
3240
}
3341
}
3442
}

compiler/rustc_codegen_ssa/src/traits/coverageinfo.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ pub trait CoverageInfoMethods: BackendTypes {
99
pub trait CoverageInfoBuilderMethods<'tcx>: BackendTypes {
1010
fn create_pgo_func_name_var(&self, instance: Instance<'tcx>) -> Self::Value;
1111

12-
fn add_counter_region(
12+
fn add_coverage_counter(
1313
&mut self,
1414
instance: Instance<'tcx>,
1515
function_source_hash: u64,
1616
id: CounterValueReference,
1717
region: CodeRegion,
1818
);
1919

20-
fn add_counter_expression_region(
20+
fn add_coverage_counter_expression(
2121
&mut self,
2222
instance: Instance<'tcx>,
2323
id: InjectedExpressionIndex,
2424
lhs: ExpressionOperandId,
2525
op: Op,
2626
rhs: ExpressionOperandId,
27-
region: CodeRegion,
27+
region: Option<CodeRegion>,
2828
);
2929

30-
fn add_unreachable_region(&mut self, instance: Instance<'tcx>, region: CodeRegion);
30+
fn add_coverage_unreachable(&mut self, instance: Instance<'tcx>, region: CodeRegion);
3131
}

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ impl Debug for Statement<'_> {
16261626
#[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
16271627
pub struct Coverage {
16281628
pub kind: CoverageKind,
1629-
pub code_region: CodeRegion,
1629+
pub code_region: Option<CodeRegion>,
16301630
}
16311631

16321632
///////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)