Skip to content

Commit c67b99f

Browse files
committed
Reinstate bonus for unused UbChecks.
1 parent a02dc34 commit c67b99f

File tree

4 files changed

+854
-52
lines changed

4 files changed

+854
-52
lines changed

compiler/rustc_mir_transform/src/cost_checker.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,27 @@ impl<'b, 'tcx> CostChecker<'b, 'tcx> {
6060
}
6161

6262
impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
63-
fn visit_statement(&mut self, statement: &Statement<'tcx>, _: Location) {
63+
fn visit_operand(&mut self, operand: &Operand<'tcx>, _: Location) {
64+
match operand {
65+
Operand::RuntimeChecks(RuntimeChecks::UbChecks) => {
66+
if !self
67+
.tcx
68+
.sess
69+
.opts
70+
.unstable_opts
71+
.inline_mir_preserve_debug
72+
.unwrap_or(self.tcx.sess.ub_checks())
73+
{
74+
// If this is in optimized MIR it's because it's used later, so if we don't need UB
75+
// checks this session, give a bonus here to offset the cost of the call later.
76+
self.bonus += CALL_PENALTY;
77+
}
78+
}
79+
_ => {}
80+
}
81+
}
82+
83+
fn visit_statement(&mut self, statement: &Statement<'tcx>, loc: Location) {
6484
// Most costs are in rvalues and terminators, not in statements.
6585
match statement.kind {
6686
StatementKind::Intrinsic(ref ndi) => {
@@ -72,9 +92,10 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
7292
StatementKind::Assign(..) => self.penalty += INSTR_COST,
7393
_ => {}
7494
}
95+
self.super_statement(statement, loc)
7596
}
7697

77-
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, _: Location) {
98+
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, loc: Location) {
7899
match &terminator.kind {
79100
TerminatorKind::Drop { place, unwind, .. } => {
80101
// If the place doesn't actually need dropping, treat it like a regular goto.
@@ -151,6 +172,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
151172
bug!("{kind:?} should not be in runtime MIR");
152173
}
153174
}
175+
self.super_terminator(terminator, loc)
154176
}
155177
}
156178

tests/codegen-llvm/slice-iter-len-eq-zero.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type Demo = [u8; 3];
88
#[no_mangle]
99
pub fn slice_iter_len_eq_zero(y: std::slice::Iter<'_, Demo>) -> bool {
1010
// CHECK-NOT: sub
11-
// CHECK: %[[RET:.+]] = icmp eq ptr {{%0, %1|%1, %0}}
11+
// CHECK: %[[RET:.+]] = icmp eq ptr {{%y.0, %y.1|%y.1, %y.0}}
1212
// CHECK: ret i1 %[[RET]]
1313
y.len() == 0
1414
}
@@ -31,7 +31,7 @@ struct MyZST;
3131
// CHECK-LABEL: @slice_zst_iter_len_eq_zero
3232
#[no_mangle]
3333
pub fn slice_zst_iter_len_eq_zero(y: std::slice::Iter<'_, MyZST>) -> bool {
34-
// CHECK: %[[RET:.+]] = icmp eq ptr %1, null
34+
// CHECK: %[[RET:.+]] = icmp eq ptr %y.1, null
3535
// CHECK: ret i1 %[[RET]]
3636
y.len() == 0
3737
}

0 commit comments

Comments
 (0)