Auto merge of #125853 - tesuji:promote-fail-fast, r=cjgillot

promote_consts: some clean-up after experimenting

This is some clean-up after experimenting in #125916,
Prefer to review commit-by-commit.
This commit is contained in:
bors 2024-06-21 16:00:14 +00:00
commit 5ced3dad57
3 changed files with 22 additions and 24 deletions

View File

@ -441,7 +441,7 @@ pub fn mplace_to_ref(
/// Take an operand, representing a pointer, and dereference it to a place.
/// Corresponds to the `*` operator in Rust.
#[instrument(skip(self), level = "debug")]
#[instrument(skip(self), level = "trace")]
pub fn deref_pointer(
&self,
src: &impl Readable<'tcx, M::Provenance>,
@ -533,7 +533,7 @@ pub fn local_to_place(
/// Computes a place. You should only use this if you intend to write into this
/// place; for reading, a more efficient alternative is `eval_place_to_op`.
#[instrument(skip(self), level = "debug")]
#[instrument(skip(self), level = "trace")]
pub fn eval_place(
&self,
mir_place: mir::Place<'tcx>,
@ -570,7 +570,7 @@ pub fn eval_place(
/// Write an immediate to a place
#[inline(always)]
#[instrument(skip(self), level = "debug")]
#[instrument(skip(self), level = "trace")]
pub fn write_immediate(
&mut self,
src: Immediate<M::Provenance>,
@ -808,7 +808,7 @@ pub fn copy_op(
/// Copies the data from an operand to a place.
/// `allow_transmute` indicates whether the layouts may disagree.
#[inline(always)]
#[instrument(skip(self), level = "debug")]
#[instrument(skip(self), level = "trace")]
fn copy_op_inner(
&mut self,
src: &impl Readable<'tcx, M::Provenance>,
@ -837,7 +837,7 @@ fn copy_op_inner(
/// `allow_transmute` indicates whether the layouts may disagree.
/// Also, if you use this you are responsible for validating that things get copied at the
/// right type.
#[instrument(skip(self), level = "debug")]
#[instrument(skip(self), level = "trace")]
fn copy_op_no_validate(
&mut self,
src: &impl Readable<'tcx, M::Provenance>,
@ -914,7 +914,7 @@ fn copy_op_no_validate(
/// If the place currently refers to a local that doesn't yet have a matching allocation,
/// create such an allocation.
/// This is essentially `force_to_memplace`.
#[instrument(skip(self), level = "debug")]
#[instrument(skip(self), level = "trace")]
pub fn force_allocation(
&mut self,
place: &PlaceTy<'tcx, M::Provenance>,

View File

@ -60,7 +60,7 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let ccx = ConstCx::new(tcx, body);
let (mut temps, all_candidates) = collect_temps_and_candidates(&ccx);
let promotable_candidates = validate_candidates(&ccx, &mut temps, &all_candidates);
let promotable_candidates = validate_candidates(&ccx, &mut temps, all_candidates);
let promoted = promote_candidates(body, tcx, temps, promotable_candidates);
self.promoted_fragments.set(promoted);
@ -98,8 +98,8 @@ struct Collector<'a, 'tcx> {
}
impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> {
#[instrument(level = "debug", skip(self))]
fn visit_local(&mut self, index: Local, context: PlaceContext, location: Location) {
debug!("visit_local: index={:?} context={:?} location={:?}", index, context, location);
// We're only interested in temporaries and the return place
match self.ccx.body.local_kind(index) {
LocalKind::Arg => return,
@ -111,20 +111,15 @@ fn visit_local(&mut self, index: Local, context: PlaceContext, location: Locatio
// then it's constant and thus drop is noop.
// Non-uses are also irrelevant.
if context.is_drop() || !context.is_use() {
debug!(
"visit_local: context.is_drop={:?} context.is_use={:?}",
context.is_drop(),
context.is_use(),
);
debug!(is_drop = context.is_drop(), is_use = context.is_use());
return;
}
let temp = &mut self.temps[index];
debug!("visit_local: temp={:?}", temp);
debug!(?temp);
*temp = match *temp {
TempState::Undefined => match context {
PlaceContext::MutatingUse(MutatingUseContext::Store)
| PlaceContext::MutatingUse(MutatingUseContext::Call) => {
PlaceContext::MutatingUse(MutatingUseContext::Store | MutatingUseContext::Call) => {
TempState::Defined { location, uses: 0, valid: Err(()) }
}
_ => TempState::Unpromotable,
@ -137,7 +132,7 @@ fn visit_local(&mut self, index: Local, context: PlaceContext, location: Locatio
| PlaceContext::NonMutatingUse(_) => true,
PlaceContext::MutatingUse(_) | PlaceContext::NonUse(_) => false,
};
debug!("visit_local: allowed_use={:?}", allowed_use);
debug!(?allowed_use);
if allowed_use {
*uses += 1;
return;
@ -146,6 +141,7 @@ fn visit_local(&mut self, index: Local, context: PlaceContext, location: Locatio
}
TempState::Unpromotable | TempState::PromotedOut => TempState::Unpromotable,
};
debug!(?temp);
}
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
@ -695,15 +691,12 @@ fn validate_call(
fn validate_candidates(
ccx: &ConstCx<'_, '_>,
temps: &mut IndexSlice<Local, TempState>,
candidates: &[Candidate],
mut candidates: Vec<Candidate>,
) -> Vec<Candidate> {
let mut validator = Validator { ccx, temps, promotion_safe_blocks: None };
candidates.retain(|&candidate| validator.validate_candidate(candidate).is_ok());
candidates
.iter()
.copied()
.filter(|&candidate| validator.validate_candidate(candidate).is_ok())
.collect()
}
struct Promoter<'a, 'tcx> {
@ -972,7 +965,12 @@ fn promote_candidates<'tcx>(
candidates: Vec<Candidate>,
) -> IndexVec<Promoted, Body<'tcx>> {
// Visit candidates in reverse, in case they're nested.
debug!("promote_candidates({:?})", candidates);
debug!(promote_candidates = ?candidates);
// eagerly fail fast
if candidates.is_empty() {
return IndexVec::new();
}
let mut promotions = IndexVec::new();

View File

@ -16,7 +16,7 @@ alloc = { path = "../alloc" }
core = { path = "../core" }
unwind = { path = "../unwind" }
compiler_builtins = "0.1.0"
cfg-if = "1.0"
cfg-if = { version = "1.0", features = ['rustc-dep-of-std'] }
[target.'cfg(not(all(windows, target_env = "msvc")))'.dependencies]
libc = { version = "0.2", default-features = false }