2024-06-17 07:36:21 +00:00
|
|
|
use rustc_middle::bug;
|
2023-01-17 21:00:07 +00:00
|
|
|
use rustc_middle::mir::visit::*;
|
|
|
|
use rustc_middle::mir::*;
|
2023-02-03 16:47:11 +00:00
|
|
|
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
|
2023-01-17 21:00:07 +00:00
|
|
|
|
|
|
|
const INSTR_COST: usize = 5;
|
|
|
|
const CALL_PENALTY: usize = 25;
|
|
|
|
const LANDINGPAD_PENALTY: usize = 50;
|
|
|
|
const RESUME_PENALTY: usize = 45;
|
2024-06-17 07:36:21 +00:00
|
|
|
const LARGE_SWITCH_PENALTY: usize = 20;
|
|
|
|
const CONST_SWITCH_BONUS: usize = 10;
|
2023-01-17 21:00:07 +00:00
|
|
|
|
|
|
|
/// Verify that the callee body is compatible with the caller.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub(crate) struct CostChecker<'b, 'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ParamEnv<'tcx>,
|
2024-06-17 06:44:29 +00:00
|
|
|
penalty: usize,
|
|
|
|
bonus: usize,
|
2023-01-17 21:00:07 +00:00
|
|
|
callee_body: &'b Body<'tcx>,
|
2023-02-03 16:47:11 +00:00
|
|
|
instance: Option<ty::Instance<'tcx>>,
|
2023-01-17 21:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'b, 'tcx> CostChecker<'b, 'tcx> {
|
|
|
|
pub fn new(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ParamEnv<'tcx>,
|
2023-02-03 16:47:11 +00:00
|
|
|
instance: Option<ty::Instance<'tcx>>,
|
2023-01-17 21:00:07 +00:00
|
|
|
callee_body: &'b Body<'tcx>,
|
|
|
|
) -> CostChecker<'b, 'tcx> {
|
2024-06-17 06:44:29 +00:00
|
|
|
CostChecker { tcx, param_env, callee_body, instance, penalty: 0, bonus: 0 }
|
2023-01-17 21:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cost(&self) -> usize {
|
2024-06-17 06:44:29 +00:00
|
|
|
usize::saturating_sub(self.penalty, self.bonus)
|
2023-01-17 21:00:07 +00:00
|
|
|
}
|
2023-02-03 16:47:11 +00:00
|
|
|
|
|
|
|
fn instantiate_ty(&self, v: Ty<'tcx>) -> Ty<'tcx> {
|
|
|
|
if let Some(instance) = self.instance {
|
|
|
|
instance.instantiate_mir(self.tcx, ty::EarlyBinder::bind(&v))
|
|
|
|
} else {
|
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|
2023-01-17 21:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
|
2024-06-17 07:36:21 +00:00
|
|
|
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
|
|
|
|
// Most costs are in rvalues and terminators, not in statements.
|
2023-01-17 21:00:07 +00:00
|
|
|
match statement.kind {
|
2024-06-17 07:36:21 +00:00
|
|
|
StatementKind::Intrinsic(ref ndi) => {
|
|
|
|
self.penalty += match **ndi {
|
|
|
|
NonDivergingIntrinsic::Assume(..) => INSTR_COST,
|
|
|
|
NonDivergingIntrinsic::CopyNonOverlapping(..) => CALL_PENALTY,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
_ => self.super_statement(statement, location),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, _location: Location) {
|
|
|
|
match rvalue {
|
|
|
|
Rvalue::NullaryOp(NullOp::UbChecks, ..) if !self.tcx.sess.ub_checks() => {
|
|
|
|
// If this is in optimized MIR it's because it's used later,
|
|
|
|
// so if we don't need UB checks this session, give a bonus
|
|
|
|
// here to offset the cost of the call later.
|
|
|
|
self.bonus += CALL_PENALTY;
|
|
|
|
}
|
|
|
|
// These are essentially constants that didn't end up in an Operand,
|
|
|
|
// so treat them as also being free.
|
|
|
|
Rvalue::NullaryOp(..) => {}
|
2024-06-17 06:44:29 +00:00
|
|
|
_ => self.penalty += INSTR_COST,
|
2023-01-17 21:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, _: Location) {
|
2024-06-17 07:36:21 +00:00
|
|
|
match &terminator.kind {
|
|
|
|
TerminatorKind::Drop { place, unwind, .. } => {
|
2023-01-17 21:00:07 +00:00
|
|
|
// If the place doesn't actually need dropping, treat it like a regular goto.
|
2024-06-17 07:36:21 +00:00
|
|
|
let ty = self.instantiate_ty(place.ty(self.callee_body, self.tcx).ty);
|
|
|
|
if ty.needs_drop(self.tcx, self.param_env) {
|
2024-06-17 06:44:29 +00:00
|
|
|
self.penalty += CALL_PENALTY;
|
2023-01-17 21:00:07 +00:00
|
|
|
if let UnwindAction::Cleanup(_) = unwind {
|
2024-06-17 06:44:29 +00:00
|
|
|
self.penalty += LANDINGPAD_PENALTY;
|
2023-01-17 21:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-17 07:36:21 +00:00
|
|
|
TerminatorKind::Call { func, unwind, .. } => {
|
|
|
|
self.penalty += if let Some((def_id, ..)) = func.const_fn_def()
|
|
|
|
&& self.tcx.intrinsic(def_id).is_some()
|
2023-11-13 13:24:55 +00:00
|
|
|
{
|
2023-01-17 21:00:07 +00:00
|
|
|
// Don't give intrinsics the extra penalty for calls
|
|
|
|
INSTR_COST
|
|
|
|
} else {
|
|
|
|
CALL_PENALTY
|
|
|
|
};
|
|
|
|
if let UnwindAction::Cleanup(_) = unwind {
|
2024-06-17 06:44:29 +00:00
|
|
|
self.penalty += LANDINGPAD_PENALTY;
|
2023-01-17 21:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-17 07:36:21 +00:00
|
|
|
TerminatorKind::SwitchInt { discr, targets } => {
|
|
|
|
if discr.constant().is_some() {
|
|
|
|
// Not only will this become a `Goto`, but likely other
|
|
|
|
// things will be removable as unreachable.
|
|
|
|
self.bonus += CONST_SWITCH_BONUS;
|
|
|
|
} else if targets.all_targets().len() > 3 {
|
|
|
|
// More than false/true/unreachable gets extra cost.
|
|
|
|
self.penalty += LARGE_SWITCH_PENALTY;
|
|
|
|
} else {
|
|
|
|
self.penalty += INSTR_COST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Assert { unwind, msg, .. } => {
|
|
|
|
self.penalty +=
|
|
|
|
if msg.is_optional_overflow_check() && !self.tcx.sess.overflow_checks() {
|
|
|
|
INSTR_COST
|
|
|
|
} else {
|
|
|
|
CALL_PENALTY
|
|
|
|
};
|
2023-01-17 21:00:07 +00:00
|
|
|
if let UnwindAction::Cleanup(_) = unwind {
|
2024-06-17 06:44:29 +00:00
|
|
|
self.penalty += LANDINGPAD_PENALTY;
|
2023-01-17 21:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-17 06:44:29 +00:00
|
|
|
TerminatorKind::UnwindResume => self.penalty += RESUME_PENALTY,
|
2023-01-17 21:00:07 +00:00
|
|
|
TerminatorKind::InlineAsm { unwind, .. } => {
|
2024-06-17 06:44:29 +00:00
|
|
|
self.penalty += INSTR_COST;
|
2023-01-17 21:00:07 +00:00
|
|
|
if let UnwindAction::Cleanup(_) = unwind {
|
2024-06-17 06:44:29 +00:00
|
|
|
self.penalty += LANDINGPAD_PENALTY;
|
2023-01-17 21:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-17 07:36:21 +00:00
|
|
|
TerminatorKind::Unreachable => {
|
|
|
|
self.bonus += INSTR_COST;
|
|
|
|
}
|
|
|
|
TerminatorKind::Goto { .. } | TerminatorKind::Return => {}
|
|
|
|
TerminatorKind::UnwindTerminate(..) => {}
|
|
|
|
kind @ (TerminatorKind::FalseUnwind { .. }
|
|
|
|
| TerminatorKind::FalseEdge { .. }
|
|
|
|
| TerminatorKind::Yield { .. }
|
|
|
|
| TerminatorKind::CoroutineDrop) => {
|
|
|
|
bug!("{kind:?} should not be in runtime MIR");
|
|
|
|
}
|
2023-01-17 21:00:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|