From 80bcc74ef6a21fd549c8c2e3fd0f9269e75f9f34 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sat, 1 Jun 2024 11:14:14 +0000 Subject: [PATCH 01/17] use rustc-dep-of-std in panic_unwind --- library/panic_unwind/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/panic_unwind/Cargo.toml b/library/panic_unwind/Cargo.toml index dce2da3164440..f830808d19648 100644 --- a/library/panic_unwind/Cargo.toml +++ b/library/panic_unwind/Cargo.toml @@ -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 } From 1c2d0f5eccf45254205dd61b73ee9d67ded23780 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sat, 1 Jun 2024 11:18:21 +0000 Subject: [PATCH 02/17] prefer tracing::instrument over debug strings --- .../rustc_mir_transform/src/promote_consts.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_mir_transform/src/promote_consts.rs b/compiler/rustc_mir_transform/src/promote_consts.rs index 7ec59cc983f53..b66f084ed95ce 100644 --- a/compiler/rustc_mir_transform/src/promote_consts.rs +++ b/compiler/rustc_mir_transform/src/promote_consts.rs @@ -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 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> { // 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 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> { | 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 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> { } TempState::Unpromotable | TempState::PromotedOut => TempState::Unpromotable, }; + debug!(?temp); } fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) { @@ -972,7 +968,7 @@ fn promote_candidates<'tcx>( candidates: Vec, ) -> IndexVec> { // Visit candidates in reverse, in case they're nested. - debug!("promote_candidates({:?})", candidates); + debug!(promote_candidates = ?candidates); let mut promotions = IndexVec::new(); From c7ac773070051000e1c6cb836e8304faa8aebd29 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sat, 1 Jun 2024 11:18:34 +0000 Subject: [PATCH 03/17] promote_consts: fail fast if there are no candidates --- compiler/rustc_mir_transform/src/promote_consts.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/rustc_mir_transform/src/promote_consts.rs b/compiler/rustc_mir_transform/src/promote_consts.rs index b66f084ed95ce..c623890419c4b 100644 --- a/compiler/rustc_mir_transform/src/promote_consts.rs +++ b/compiler/rustc_mir_transform/src/promote_consts.rs @@ -970,6 +970,11 @@ fn promote_candidates<'tcx>( // Visit candidates in reverse, in case they're nested. debug!(promote_candidates = ?candidates); + // eagerly fail fast + if candidates.is_empty() { + return IndexVec::new(); + } + let mut promotions = IndexVec::new(); let mut extra_statements = vec![]; From 3facd92b58c29dad3a6669d04fbf7db7cc544bad Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Wed, 29 May 2024 04:22:08 +0000 Subject: [PATCH 04/17] mvp --- compiler/rustc_mir_transform/src/lib.rs | 13 +- .../rustc_mir_transform/src/promote_consts.rs | 10 +- .../src/promote_consts_local_arrays.rs | 1125 +++++++++++++++++ ...st_array_locals.main.PromoteArraysOpt.diff | 56 + tests/mir-opt/const_array_locals.rs | 30 + 5 files changed, 1231 insertions(+), 3 deletions(-) create mode 100644 compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs create mode 100644 tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff create mode 100644 tests/mir-opt/const_array_locals.rs diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index e4670633914e4..7e436b3c4ca3c 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -91,6 +91,7 @@ mod normalize_array_len; mod nrvo; mod prettify; mod promote_consts; +mod promote_consts_local_arrays; mod ref_prop; mod remove_noop_landing_pads; mod remove_storage_markers; @@ -342,14 +343,22 @@ fn mir_promoted( // What we need to run borrowck etc. let promote_pass = promote_consts::PromoteTemps::default(); + let promote_array = promote_consts_local_arrays::PromoteArraysOpt::default(); pm::run_passes( tcx, &mut body, - &[&promote_pass, &simplify::SimplifyCfg::PromoteConsts, &coverage::InstrumentCoverage], + &[ + &promote_pass, + &promote_array, + &simplify::SimplifyCfg::PromoteConsts, + &coverage::InstrumentCoverage, + ], Some(MirPhase::Analysis(AnalysisPhase::Initial)), ); - let promoted = promote_pass.promoted_fragments.into_inner(); + let mut promoted = promote_pass.promoted_fragments.into_inner(); + let array_promoted = promote_array.promoted_fragments.into_inner(); + promoted.extend(array_promoted); (tcx.alloc_steal_mir(body), tcx.alloc_steal_promoted(promoted)) } diff --git a/compiler/rustc_mir_transform/src/promote_consts.rs b/compiler/rustc_mir_transform/src/promote_consts.rs index c623890419c4b..849618b4517ea 100644 --- a/compiler/rustc_mir_transform/src/promote_consts.rs +++ b/compiler/rustc_mir_transform/src/promote_consts.rs @@ -103,7 +103,15 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> { // We're only interested in temporaries and the return place match self.ccx.body.local_kind(index) { LocalKind::Arg => return, - LocalKind::Temp if self.ccx.body.local_decls[index].is_user_variable() => return, + LocalKind::Temp + if { + let is_user_variable = self.ccx.body.local_decls[index].is_user_variable(); + debug!(?is_user_variable); + is_user_variable + } => + { + return; + } LocalKind::ReturnPointer | LocalKind::Temp => {} } diff --git a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs new file mode 100644 index 0000000000000..9f06941a07669 --- /dev/null +++ b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs @@ -0,0 +1,1125 @@ +//! A pass that promotes const local arrays to rodata. +//! +//! The rvalues considered constant are trees of temps, +//! each with exactly one initialization, and holding +//! a constant value with no interior mutability. +//! They are placed into a new MIR constant body in +//! `promoted` and the borrow rvalue is replaced with +//! a `Literal::Promoted` using the index into `promoted` +//! of that constant MIR. +//! +//! This pass assumes that every use is dominated by an +//! initialization and can otherwise silence errors, if +//! move analysis runs after promotion on broken MIR. + +use either::{Left, Right}; +use rustc_data_structures::fx::FxHashSet; +use rustc_hir as hir; +use rustc_middle::mir; +use rustc_middle::mir::visit::{ + MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor, +}; +use rustc_middle::mir::*; +use rustc_middle::ty::GenericArgs; +use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt}; +use rustc_middle::{bug, span_bug}; +use rustc_span::Span; + +use rustc_index::{Idx, IndexSlice, IndexVec}; +use rustc_span::source_map::Spanned; + +use std::assert_matches::assert_matches; +use std::cell::Cell; +use std::{cmp, mem}; + +use rustc_const_eval::check_consts::{qualifs, ConstCx}; + +/// A `MirPass` for promotion. +/// +/// Promotion is the extraction of promotable temps into separate MIR bodies so they can have +/// `'static` lifetime. +/// +/// After this pass is run, `promoted_fragments` will hold the MIR body corresponding to each +/// newly created `Constant`. +// FIXME: consider merging to `PromoteTemps` pass. +#[derive(Default)] +pub struct PromoteArraysOpt<'tcx> { + pub promoted_fragments: Cell>>, +} + +// LLVM optimizes the load of 16 byte as a single `mov`. +// Bigger values make more `mov` instructions generated. +// While changing code as this lint suggests, it becomes +// a single load (`lea`) of an address in `.rodata`. +const STACK_THRESHOLD: u64 = 16; + +impl<'tcx> MirPass<'tcx> for PromoteArraysOpt<'tcx> { + fn is_enabled(&self, sess: &rustc_session::Session) -> bool { + sess.mir_opt_level() > 0 + } + + #[instrument(level = "trace", skip(self, tcx, body))] + fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { + debug!("running on body: {:?}", body.source.def_id()); + // There's not really any point in promoting errorful MIR. + // + // This does not include MIR that failed const-checking, which we still try to promote. + if let Err(_) = body.return_ty().error_reported() { + debug!("MIR had errors"); + return; + } + if body.source.promoted.is_some() { + return; + } + + let ccx = ConstCx::new(tcx, body); + let (mut temps, all_candidates, already_promoted) = collect_temps_and_candidates(&ccx); + + let promotable_candidates = validate_candidates(&ccx, &mut temps, &all_candidates); + debug!(candidates = ?promotable_candidates); + + let promoted = + promote_candidates(body, tcx, temps, promotable_candidates, already_promoted); + self.promoted_fragments.set(promoted); + } +} + +/// State of a temporary during collection and promotion. +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +enum TempState { + /// No references to this temp. + Undefined, + /// One direct assignment and any number of direct uses. + /// A borrow of this temp is promotable if the assigned + /// value is qualified as constant. + Defined { location: Location, uses: usize, valid: Result<(), ()> }, + /// Any other combination of assignments/uses. + Unpromotable, + /// This temp was part of an rvalue which got extracted + /// during promotion and needs cleanup. + PromotedOut, +} + +/// A "root candidate" for promotion, which will become the +/// returned value in a promoted MIR, unless it's a subset +/// of a larger candidate. +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +struct Candidate { + location: Location, +} + +struct Collector<'a, 'tcx> { + ccx: &'a ConstCx<'a, 'tcx>, + temps: IndexVec, + candidates: Vec, + already_promoted: usize, +} + +impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> { + #[instrument(level = "debug", skip(self))] + fn visit_local(&mut self, index: Local, context: PlaceContext, location: Location) { + // We're only interested in temporaries + match self.ccx.body.local_kind(index) { + LocalKind::Arg | LocalKind::ReturnPointer => return, + LocalKind::Temp => {} + } + + { + let is_user_variable = self.ccx.body.local_decls[index].is_user_variable(); + debug!(?is_user_variable); + } + + // Ignore drops, if the temp gets promoted, + // then it's constant and thus drop is noop. + // Non-uses are also irrelevant. + if 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!(?temp); + *temp = match *temp { + TempState::Undefined => match context { + PlaceContext::MutatingUse(MutatingUseContext::Store | MutatingUseContext::Call) => { + TempState::Defined { location, uses: 0, valid: Err(()) } + } + _ => TempState::Unpromotable, + }, + TempState::Defined { ref mut uses, .. } => { + use NonMutatingUseContext as Ctxt; + // We only allow non-mutating use of arrays. + let allowed_use = match context { + // this may already be promoted by PromoteTemps + PlaceContext::NonMutatingUse(Ctxt::Projection) => false, + PlaceContext::NonMutatingUse(_) => true, + PlaceContext::MutatingUse(_) | PlaceContext::NonUse(_) => false, + }; + debug!(?allowed_use); + if allowed_use { + *uses += 1; + return; + } + TempState::Unpromotable + } + TempState::Unpromotable | TempState::PromotedOut => TempState::Unpromotable, + }; + debug!(?temp); + } + + #[instrument(level = "debug", skip(self, rvalue))] + fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) { + if !rvalue.ty(&self.ccx.body.local_decls, self.ccx.tcx).is_array() { + debug!(?rvalue); + } + + self.super_rvalue(rvalue, location); + + let Rvalue::Aggregate(kind, _ops) = rvalue else { + return; + }; + + debug!("pushing a candidate of type {:?} @ {:?}", kind, location); + self.candidates.push(Candidate { location }); + } + + // #[instrument(level = "debug", skip(self, constant))] + fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, _location: Location) { + if let Const::Unevaluated(c, _ty) = constant.const_ + && c.promoted.is_some() + { + self.already_promoted += 1; + } + + // Skipping `super_constant` as the visitor is otherwise only looking for locals. + } +} + +fn collect_temps_and_candidates<'tcx>( + ccx: &ConstCx<'_, 'tcx>, +) -> (IndexVec, Vec, usize) { + let mut collector = Collector { + temps: IndexVec::from_elem(TempState::Undefined, &ccx.body.local_decls), + candidates: vec![], + ccx, + already_promoted: 0, + }; + for (bb, data) in traversal::reverse_postorder(ccx.body) { + collector.visit_basic_block_data(bb, data); + } + + // debug!(collector.already_promoted); + (collector.temps, collector.candidates, collector.already_promoted) +} + +/// Checks whether locals that appear in a promotion context (`Candidate`) are actually promotable. +/// +/// This wraps an `Item`, and has access to all fields of that `Item` via `Deref` coercion. +struct Validator<'a, 'tcx> { + ccx: &'a ConstCx<'a, 'tcx>, + temps: &'a mut IndexSlice, + /// For backwards compatibility, we are promoting function calls in `const`/`static` + /// initializers. But we want to avoid evaluating code that might panic and that otherwise would + /// not have been evaluated, so we only promote such calls in basic blocks that are guaranteed + /// to execute. In other words, we only promote such calls in basic blocks that are definitely + /// not dead code. Here we cache the result of computing that set of basic blocks. + promotion_safe_blocks: Option>, +} + +impl<'a, 'tcx> std::ops::Deref for Validator<'a, 'tcx> { + type Target = ConstCx<'a, 'tcx>; + + fn deref(&self) -> &Self::Target { + self.ccx + } +} + +struct Unpromotable; + +impl<'tcx> Validator<'_, 'tcx> { + fn validate_candidate(&mut self, candidate: Candidate) -> Result<(), Unpromotable> { + let Left(statement) = self.body.stmt_at(candidate.location) else { bug!() }; + let Some((place, rvalue @ Rvalue::Aggregate(box kind, operands))) = + statement.kind.as_assign() + else { + bug!() + }; + + let TempState::Defined { .. } = self.temps[place.local] else { + return Err(Unpromotable); + }; + + let AggregateKind::Array(arr_ty) = kind else { + return Err(Unpromotable); + }; + + // lint only `if size_of(init) > STACK_THRESHOLD` + let tcx = self.tcx; + let rvalue_ty = rvalue.ty(self.body, tcx); + if let Ok(layout) = tcx.layout_of(self.param_env.and(rvalue_ty)) + && let size = layout.layout.size() + && size.bytes() <= STACK_THRESHOLD + { + debug!("size of array is too small: {:?}", size); + return Err(Unpromotable); + } + + if !arr_ty.is_trivially_pure_clone_copy() { + return Err(Unpromotable); + } + + for o in operands { + self.validate_operand(o)?; + } + + // No projections at all + if !place.projection.is_empty() { + return Err(Unpromotable); + } + + Ok(()) + } + + // FIXME(eddyb) maybe cache this? + fn qualif_local(&mut self, local: Local) -> bool { + let TempState::Defined { location: loc, .. } = self.temps[local] else { + return false; + }; + + let stmt_or_term = self.body.stmt_at(loc); + match stmt_or_term { + Left(statement) => { + let Some((_, rhs)) = statement.kind.as_assign() else { + span_bug!(statement.source_info.span, "{:?} is not an assignment", statement) + }; + qualifs::in_rvalue::(self.ccx, &mut |l| self.qualif_local::(l), rhs) + } + Right(terminator) => { + assert_matches!(terminator.kind, TerminatorKind::Call { .. }); + let return_ty = self.body.local_decls[local].ty; + Q::in_any_value_of_ty(self.ccx, return_ty) + } + } + } + + // We can only promote interior borrows of promotable temps (non-temps + // don't get promoted anyway). + fn validate_local(&mut self, local: Local) -> Result<(), Unpromotable> { + let TempState::Defined { location: loc, uses, valid } = self.temps[local] else { + return Err(Unpromotable); + }; + + // We cannot promote things that need dropping, since the promoted value would not get + // dropped. + if self.qualif_local::(local) { + return Err(Unpromotable); + } + + if self.qualif_local::(local) { + return Err(Unpromotable); + } + + if valid.is_ok() { + return Ok(()); + } + + let ok = { + let stmt_or_term = self.body.stmt_at(loc); + match stmt_or_term { + Left(statement) => { + let Some((_, rhs)) = statement.kind.as_assign() else { + span_bug!( + statement.source_info.span, + "{:?} is not an assignment", + statement + ) + }; + self.validate_rvalue(rhs) + } + Right(terminator) => match &terminator.kind { + TerminatorKind::Call { func, args, .. } => { + self.validate_call(func, args, loc.block) + } + TerminatorKind::Yield { .. } => Err(Unpromotable), + kind => { + span_bug!(terminator.source_info.span, "{:?} not promotable", kind); + } + }, + } + }; + + self.temps[local] = match ok { + Ok(()) => TempState::Defined { location: loc, uses, valid: Ok(()) }, + Err(_) => TempState::Unpromotable, + }; + + ok + } + + fn validate_place(&mut self, place: PlaceRef<'tcx>) -> Result<(), Unpromotable> { + let Some((place_base, elem)) = place.last_projection() else { + return self.validate_local(place.local); + }; + + // Validate topmost projection, then recurse. + match elem { + // Recurse directly. + ProjectionElem::ConstantIndex { .. } + | ProjectionElem::Subtype(_) + | ProjectionElem::Subslice { .. } => {} + + // Never recurse. + ProjectionElem::OpaqueCast(..) | ProjectionElem::Downcast(..) => { + return Err(Unpromotable); + } + + ProjectionElem::Deref => { + // When a static is used by-value, that gets desugared to `*STATIC_ADDR`, + // and we need to be able to promote this. So check if this deref matches + // that specific pattern. + + // We need to make sure this is a `Deref` of a local with no further projections. + // Discussion can be found at + // https://github.com/rust-lang/rust/pull/74945#discussion_r463063247 + if let Some(local) = place_base.as_local() + && let TempState::Defined { location, .. } = self.temps[local] + && let Left(def_stmt) = self.body.stmt_at(location) + && let Some((_, Rvalue::Use(Operand::Constant(c)))) = def_stmt.kind.as_assign() + && let Some(did) = c.check_static_ptr(self.tcx) + // Evaluating a promoted may not read statics except if it got + // promoted from a static (this is a CTFE check). So we + // can only promote static accesses inside statics. + && let Some(hir::ConstContext::Static(..)) = self.const_kind + && !self.tcx.is_thread_local_static(did) + { + // Recurse. + } else { + return Err(Unpromotable); + } + } + ProjectionElem::Index(local) => { + // Only accept if we can predict the index and are indexing an array. + if let TempState::Defined { location: loc, .. } = self.temps[local] + && let Left(statement) = self.body.stmt_at(loc) + && let Some((_, Rvalue::Use(Operand::Constant(c)))) = statement.kind.as_assign() + && let Some(idx) = c.const_.try_eval_target_usize(self.tcx, self.param_env) + // Determine the type of the thing we are indexing. + && let ty::Array(_, len) = place_base.ty(self.body, self.tcx).ty.kind() + // It's an array; determine its length. + && let Some(len) = len.try_eval_target_usize(self.tcx, self.param_env) + // If the index is in-bounds, go ahead. + && idx < len + { + self.validate_local(local)?; + // Recurse. + } else { + return Err(Unpromotable); + } + } + + ProjectionElem::Field(..) => { + let base_ty = place_base.ty(self.body, self.tcx).ty; + if base_ty.is_union() { + // No promotion of union field accesses. + return Err(Unpromotable); + } + } + } + + self.validate_place(place_base) + } + + fn validate_operand(&mut self, operand: &Operand<'tcx>) -> Result<(), Unpromotable> { + match operand { + Operand::Copy(place) | Operand::Move(place) => self.validate_place(place.as_ref()), + + // The qualifs for a constant (e.g. `HasMutInterior`) are checked in + // `validate_rvalue` upon access. + Operand::Constant(c) => { + if let Some(def_id) = c.check_static_ptr(self.tcx) { + // Only allow statics (not consts) to refer to other statics. + // FIXME(eddyb) does this matter at all for promotion? + // FIXME(RalfJung) it makes little sense to not promote this in `fn`/`const fn`, + // and in `const` this cannot occur anyway. The only concern is that we might + // promote even `let x = &STATIC` which would be useless, but this applies to + // promotion inside statics as well. + let is_static = matches!(self.const_kind, Some(hir::ConstContext::Static(_))); + if !is_static { + return Err(Unpromotable); + } + + let is_thread_local = self.tcx.is_thread_local_static(def_id); + if is_thread_local { + return Err(Unpromotable); + } + } + + Ok(()) + } + } + } + + // The reference operation itself must be promotable. + // (Needs to come after `validate_local` to avoid ICEs.) + fn validate_ref(&mut self, kind: BorrowKind, place: &Place<'tcx>) -> Result<(), Unpromotable> { + match kind { + // Reject these borrow types just to be safe. + // FIXME(RalfJung): could we allow them? Should we? No point in it until we have a usecase. + BorrowKind::Fake(_) | BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture } => { + return Err(Unpromotable); + } + + BorrowKind::Shared => { + let has_mut_interior = self.qualif_local::(place.local); + if has_mut_interior { + return Err(Unpromotable); + } + } + + // FIXME: consider changing this to only promote &mut [] for default borrows, + // also forbidding two phase borrows + BorrowKind::Mut { kind: MutBorrowKind::Default | MutBorrowKind::TwoPhaseBorrow } => { + let ty = place.ty(self.body, self.tcx).ty; + + // In theory, any zero-sized value could be borrowed + // mutably without consequences. However, only &mut [] + // is allowed right now. + if let ty::Array(_, len) = ty.kind() { + match len.try_eval_target_usize(self.tcx, self.param_env) { + Some(0) => {} + _ => return Err(Unpromotable), + } + } else { + return Err(Unpromotable); + } + } + } + + Ok(()) + } + + fn validate_rvalue(&mut self, rvalue: &Rvalue<'tcx>) -> Result<(), Unpromotable> { + match rvalue { + Rvalue::Use(operand) | Rvalue::Repeat(operand, _) => { + self.validate_operand(operand)?; + } + Rvalue::CopyForDeref(place) => { + let op = &Operand::Copy(*place); + self.validate_operand(op)? + } + + Rvalue::Discriminant(place) | Rvalue::Len(place) => { + self.validate_place(place.as_ref())? + } + + Rvalue::ThreadLocalRef(_) => return Err(Unpromotable), + + // ptr-to-int casts are not possible in consts and thus not promotable + Rvalue::Cast(CastKind::PointerExposeProvenance, _, _) => return Err(Unpromotable), + + // all other casts including int-to-ptr casts are fine, they just use the integer value + // at pointer type. + Rvalue::Cast(_, operand, _) => { + self.validate_operand(operand)?; + } + + Rvalue::NullaryOp(op, _) => match op { + NullOp::SizeOf => {} + NullOp::AlignOf => {} + NullOp::OffsetOf(_) => {} + NullOp::UbChecks => {} + }, + + Rvalue::ShallowInitBox(_, _) => return Err(Unpromotable), + + Rvalue::UnaryOp(op, operand) => { + match op { + // These operations can never fail. + UnOp::Neg | UnOp::Not | UnOp::PtrMetadata => {} + } + + self.validate_operand(operand)?; + } + + Rvalue::BinaryOp(op, box (lhs, rhs)) => { + let op = *op; + let lhs_ty = lhs.ty(self.body, self.tcx); + + if let ty::RawPtr(_, _) | ty::FnPtr(..) = lhs_ty.kind() { + // Raw and fn pointer operations are not allowed inside consts and thus not promotable. + assert!(matches!( + op, + BinOp::Eq + | BinOp::Ne + | BinOp::Le + | BinOp::Lt + | BinOp::Ge + | BinOp::Gt + | BinOp::Offset + )); + return Err(Unpromotable); + } + + match op { + BinOp::Div | BinOp::Rem => { + if lhs_ty.is_integral() { + let sz = lhs_ty.primitive_size(self.tcx); + // Integer division: the RHS must be a non-zero const. + let rhs_val = match rhs { + Operand::Constant(c) => { + c.const_.try_eval_scalar_int(self.tcx, self.param_env) + } + _ => None, + }; + match rhs_val.map(|x| x.assert_uint(sz)) { + // for the zero test, int vs uint does not matter + Some(x) if x != 0 => {} // okay + _ => return Err(Unpromotable), // value not known or 0 -- not okay + } + // Furthermore, for signed divison, we also have to exclude `int::MIN / -1`. + if lhs_ty.is_signed() { + match rhs_val.map(|x| x.assert_int(sz)) { + Some(-1) | None => { + // The RHS is -1 or unknown, so we have to be careful. + // But is the LHS int::MIN? + let lhs_val = match lhs { + Operand::Constant(c) => c + .const_ + .try_eval_scalar_int(self.tcx, self.param_env), + _ => None, + }; + let lhs_min = sz.signed_int_min(); + match lhs_val.map(|x| x.assert_int(sz)) { + Some(x) if x != lhs_min => {} // okay + _ => return Err(Unpromotable), // value not known or int::MIN -- not okay + } + } + _ => {} + } + } + } + } + // The remaining operations can never fail. + BinOp::Eq + | BinOp::Ne + | BinOp::Le + | BinOp::Lt + | BinOp::Ge + | BinOp::Gt + | BinOp::Cmp + | BinOp::Offset + | BinOp::Add + | BinOp::AddUnchecked + | BinOp::AddWithOverflow + | BinOp::Sub + | BinOp::SubUnchecked + | BinOp::SubWithOverflow + | BinOp::Mul + | BinOp::MulUnchecked + | BinOp::MulWithOverflow + | BinOp::BitXor + | BinOp::BitAnd + | BinOp::BitOr + | BinOp::Shl + | BinOp::ShlUnchecked + | BinOp::Shr + | BinOp::ShrUnchecked => {} + } + + self.validate_operand(lhs)?; + self.validate_operand(rhs)?; + } + + Rvalue::AddressOf(_, place) => { + // We accept `&raw *`, i.e., raw reborrows -- creating a raw pointer is + // no problem, only using it is. + if let Some((place_base, ProjectionElem::Deref)) = place.as_ref().last_projection() + { + let base_ty = place_base.ty(self.body, self.tcx).ty; + if let ty::Ref(..) = base_ty.kind() { + return self.validate_place(place_base); + } + } + return Err(Unpromotable); + } + + Rvalue::Ref(_, kind, place) => { + // Special-case reborrows to be more like a copy of the reference. + let mut place_simplified = place.as_ref(); + if let Some((place_base, ProjectionElem::Deref)) = + place_simplified.last_projection() + { + let base_ty = place_base.ty(self.body, self.tcx).ty; + if let ty::Ref(..) = base_ty.kind() { + place_simplified = place_base; + } + } + + self.validate_place(place_simplified)?; + + // Check that the reference is fine (using the original place!). + // (Needs to come after `validate_place` to avoid ICEs.) + self.validate_ref(*kind, place)?; + } + + Rvalue::Aggregate(_, operands) => { + for o in operands { + self.validate_operand(o)?; + } + } + } + + Ok(()) + } + + /// Computes the sets of blocks of this MIR that are definitely going to be executed + /// if the function returns successfully. That makes it safe to promote calls in them + /// that might fail. + fn promotion_safe_blocks(body: &mir::Body<'tcx>) -> FxHashSet { + let mut safe_blocks = FxHashSet::default(); + let mut safe_block = START_BLOCK; + loop { + safe_blocks.insert(safe_block); + // Let's see if we can find another safe block. + safe_block = match body.basic_blocks[safe_block].terminator().kind { + TerminatorKind::Goto { target } => target, + TerminatorKind::Call { target: Some(target), .. } + | TerminatorKind::Drop { target, .. } => { + // This calls a function or the destructor. `target` does not get executed if + // the callee loops or panics. But in both cases the const already fails to + // evaluate, so we are fine considering `target` a safe block for promotion. + target + } + TerminatorKind::Assert { target, .. } => { + // Similar to above, we only consider successful execution. + target + } + _ => { + // No next safe block. + break; + } + }; + } + safe_blocks + } + + /// Returns whether the block is "safe" for promotion, which means it cannot be dead code. + /// We use this to avoid promoting operations that can fail in dead code. + fn is_promotion_safe_block(&mut self, block: BasicBlock) -> bool { + let body = self.body; + let safe_blocks = + self.promotion_safe_blocks.get_or_insert_with(|| Self::promotion_safe_blocks(body)); + safe_blocks.contains(&block) + } + + fn validate_call( + &mut self, + callee: &Operand<'tcx>, + args: &[Spanned>], + block: BasicBlock, + ) -> Result<(), Unpromotable> { + // Validate the operands. If they fail, there's no question -- we cannot promote. + self.validate_operand(callee)?; + for arg in args { + self.validate_operand(&arg.node)?; + } + + // Functions marked `#[rustc_promotable]` are explicitly allowed to be promoted, so we can + // accept them at this point. + let fn_ty = callee.ty(self.body, self.tcx); + if let ty::FnDef(def_id, _) = *fn_ty.kind() { + if self.tcx.is_promotable_const_fn(def_id) { + return Ok(()); + } + } + + // Ideally, we'd stop here and reject the rest. + // But for backward compatibility, we have to accept some promotion in const/static + // initializers. Inline consts are explicitly excluded, they are more recent so we have no + // backwards compatibility reason to allow more promotion inside of them. + let promote_all_fn = matches!( + self.const_kind, + Some(hir::ConstContext::Static(_) | hir::ConstContext::Const { inline: false }) + ); + if !promote_all_fn { + return Err(Unpromotable); + } + // Make sure the callee is a `const fn`. + let is_const_fn = match *fn_ty.kind() { + ty::FnDef(def_id, _) => self.tcx.is_const_fn_raw(def_id), + _ => false, + }; + if !is_const_fn { + return Err(Unpromotable); + } + // The problem is, this may promote calls to functions that panic. + // We don't want to introduce compilation errors if there's a panic in a call in dead code. + // So we ensure that this is not dead code. + if !self.is_promotion_safe_block(block) { + return Err(Unpromotable); + } + // This passed all checks, so let's accept. + Ok(()) + } +} + +fn validate_candidates( + ccx: &ConstCx<'_, '_>, + temps: &mut IndexSlice, + candidates: &[Candidate], +) -> Vec { + let mut validator = Validator { ccx, temps, promotion_safe_blocks: None }; + + candidates + .iter() + .copied() + .filter(|&candidate| validator.validate_candidate(candidate).is_ok()) + .collect() +} + +struct Promoter<'a, 'tcx> { + tcx: TyCtxt<'tcx>, + source: &'a mut Body<'tcx>, + promoted: Body<'tcx>, + temps: &'a mut IndexVec, + extra_statements: &'a mut Vec<(Location, Statement<'tcx>)>, + + /// If true, all nested temps are also kept in the + /// source MIR, not moved to the promoted MIR. + keep_original: bool, + + /// If true, add the new const (the promoted) to the required_consts of the parent MIR. + /// This is initially false and then set by the visitor when it encounters a `Call` terminator. + add_to_required: bool, +} + +impl<'a, 'tcx> Promoter<'a, 'tcx> { + fn new_block(&mut self) -> BasicBlock { + let span = self.promoted.span; + self.promoted.basic_blocks_mut().push(BasicBlockData { + statements: vec![], + terminator: Some(Terminator { + source_info: SourceInfo::outermost(span), + kind: TerminatorKind::Return, + }), + is_cleanup: false, + }) + } + + fn assign(&mut self, dest: Local, rvalue: Rvalue<'tcx>, span: Span) { + let last = self.promoted.basic_blocks.last_index().unwrap(); + let data = &mut self.promoted[last]; + data.statements.push(Statement { + source_info: SourceInfo::outermost(span), + kind: StatementKind::Assign(Box::new((Place::from(dest), rvalue))), + }); + } + + fn is_temp_kind(&self, local: Local) -> bool { + self.source.local_kind(local) == LocalKind::Temp + } + + /// Copies the initialization of this temp to the + /// promoted MIR, recursing through temps. + fn promote_temp(&mut self, temp: Local) -> Local { + let old_keep_original = self.keep_original; + let loc = match self.temps[temp] { + TempState::Defined { location, uses, .. } if uses > 0 => { + if uses > 1 { + self.keep_original = true; + } + location + } + state => { + span_bug!(self.promoted.span, "{:?} not promotable: {:?}", temp, state); + } + }; + if !self.keep_original { + self.temps[temp] = TempState::PromotedOut; + } + + let num_stmts = self.source[loc.block].statements.len(); + let new_temp = self.promoted.local_decls.push(LocalDecl::new( + self.source.local_decls[temp].ty, + self.source.local_decls[temp].source_info.span, + )); + + debug!("promote({:?} @ {:?}/{:?}, {:?})", temp, loc, num_stmts, self.keep_original); + + // First, take the Rvalue or Call out of the source MIR, + // or duplicate it, depending on keep_original. + if loc.statement_index < num_stmts { + let (mut rvalue, source_info) = { + let statement = &mut self.source[loc.block].statements[loc.statement_index]; + let StatementKind::Assign(box (_place, rhs)) = &mut statement.kind else { + span_bug!(statement.source_info.span, "{:?} is not an assignment", statement); + }; + + ( + if self.keep_original { + rhs.clone() + } else { + let unit = Rvalue::Use(Operand::Constant(Box::new(ConstOperand { + span: statement.source_info.span, + user_ty: None, + const_: Const::zero_sized(self.tcx.types.unit), + }))); + mem::replace(rhs, unit) + }, + statement.source_info, + ) + }; + + self.visit_rvalue(&mut rvalue, loc); + self.assign(new_temp, rvalue, source_info.span); + } else { + let terminator = if self.keep_original { + self.source[loc.block].terminator().clone() + } else { + let terminator = self.source[loc.block].terminator_mut(); + let target = match &terminator.kind { + TerminatorKind::Call { target: Some(target), .. } => *target, + kind => { + span_bug!(terminator.source_info.span, "{:?} not promotable", kind); + } + }; + Terminator { + source_info: terminator.source_info, + kind: mem::replace(&mut terminator.kind, TerminatorKind::Goto { target }), + } + }; + + match terminator.kind { + TerminatorKind::Call { + mut func, mut args, call_source: desugar, fn_span, .. + } => { + // This promoted involves a function call, so it may fail to evaluate. + // Let's make sure it is added to `required_consts` so that that failure cannot get lost. + self.add_to_required = true; + + self.visit_operand(&mut func, loc); + for arg in &mut args { + self.visit_operand(&mut arg.node, loc); + } + + let last = self.promoted.basic_blocks.last_index().unwrap(); + let new_target = self.new_block(); + + *self.promoted[last].terminator_mut() = Terminator { + kind: TerminatorKind::Call { + func, + args, + unwind: UnwindAction::Continue, + destination: Place::from(new_temp), + target: Some(new_target), + call_source: desugar, + fn_span, + }, + source_info: SourceInfo::outermost(terminator.source_info.span), + ..terminator + }; + } + kind => { + span_bug!(terminator.source_info.span, "{:?} not promotable", kind); + } + }; + }; + + self.keep_original = old_keep_original; + new_temp + } + + fn promote_candidate(mut self, candidate: Candidate, next_promoted_id: usize) -> Body<'tcx> { + let def = self.source.source.def_id(); + let promoted_id = Promoted::new(next_promoted_id); + let (mut rvalue, promoted_op, promoted_ref_rvalue) = { + let promoted = &mut self.promoted; + let tcx = self.tcx; + let mut promoted_operand = |ty, span| { + promoted.span = span; + promoted.local_decls[RETURN_PLACE] = LocalDecl::new(ty, span); + let args = tcx.erase_regions(GenericArgs::identity_for_item(tcx, def)); + let uneval = mir::UnevaluatedConst { def, args, promoted: Some(promoted_id) }; + + ConstOperand { span, user_ty: None, const_: Const::Unevaluated(uneval, ty) } + }; + + let blocks = self.source.basic_blocks.as_mut(); + let local_decls = &mut self.source.local_decls; + let loc = candidate.location; + let statement = &mut blocks[loc.block].statements[loc.statement_index]; + let StatementKind::Assign(box (place, Rvalue::Aggregate(_kind, _operands))) = + &mut statement.kind + else { + bug!() + }; + + let ty = local_decls[place.local].ty; + let span = statement.source_info.span; + + let ref_ty = Ty::new_ref(tcx, tcx.lifetimes.re_erased, ty, hir::Mutability::Not); + + // Create a temp to hold the promoted reference. + // This is because `*r` requires `r` to be a local, + // otherwise we would use the `promoted` directly. + let mut promoted_ref = LocalDecl::new(ref_ty, span); + promoted_ref.source_info = statement.source_info; + let promoted_ref: Local = local_decls.push(promoted_ref); + // let new_state = TempState::Defined { location: loc, uses: 1, valid: Err(()) }; + let new_state = TempState::Unpromotable; + assert_eq!(self.temps.push(new_state), promoted_ref); + + let promoted_operand = promoted_operand(ref_ty, span); + let promoted_ref_statement = Statement { + source_info: statement.source_info, + kind: StatementKind::Assign(Box::new(( + Place::from(promoted_ref), + Rvalue::Use(Operand::Constant(Box::new(promoted_operand))), + ))), + }; + self.extra_statements.push((loc, promoted_ref_statement)); + + let promoted_ref_place = + Place { local: promoted_ref, projection: tcx.mk_place_elems(&[PlaceElem::Deref]) }; + + // let promoted_rvalue = Rvalue::Use(Operand::Copy(place.clone())); + let promoted_rvalue = + Rvalue::Ref(tcx.lifetimes.re_erased, BorrowKind::Shared, Place::from(place.local)); + let promoted_ref_rvalue = Rvalue::Use(Operand::Copy(promoted_ref_place)); + + (promoted_rvalue, promoted_operand, promoted_ref_rvalue) + }; + + let promoted_rvalue = promoted_ref_rvalue; + + assert_eq!(self.new_block(), START_BLOCK); + self.visit_rvalue( + &mut rvalue, + Location { block: START_BLOCK, statement_index: usize::MAX }, + ); + + { + let blocks = self.source.basic_blocks_mut(); + let loc = candidate.location; + let statement = &mut blocks[loc.block].statements[loc.statement_index]; + if let StatementKind::Assign(box (_place, ref mut rvalue)) = &mut statement.kind { + *rvalue = promoted_rvalue; + } + } + + let span = self.promoted.span; + self.assign(RETURN_PLACE, rvalue, span); + + // Now that we did promotion, we know whether we'll want to add this to `required_consts`. + if self.add_to_required { + self.source.required_consts.push(promoted_op); + } + + self.promoted.source.promoted = Some(promoted_id); + self.promoted + } +} + +/// Replaces all temporaries with their promoted counterparts. +impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> { + fn tcx(&self) -> TyCtxt<'tcx> { + self.tcx + } + + #[instrument(level = "trace", skip(self))] + fn visit_local(&mut self, local: &mut Local, _ctx: PlaceContext, _loc: Location) { + if self.is_temp_kind(*local) { + *local = self.promote_temp(*local); + } + } + + // #[instrument(level = "trace", skip(self, rvalue))] + fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) { + self.super_rvalue(rvalue, location); + } + + fn visit_constant(&mut self, constant: &mut ConstOperand<'tcx>, _location: Location) { + if constant.const_.is_required_const() { + self.promoted.required_consts.push(*constant); + } + + // Skipping `super_constant` as the visitor is otherwise only looking for locals. + } +} + +fn promote_candidates<'tcx>( + body: &mut Body<'tcx>, + tcx: TyCtxt<'tcx>, + mut temps: IndexVec, + candidates: Vec, + already_promoted: usize, +) -> IndexVec> { + // eagerly fail fast + if candidates.is_empty() { + return IndexVec::new(); + } + + let mut promotions = IndexVec::new(); + // FIXME: buggy things, there shouldn't be more than 42 promoted items by PromoteTemps. + // cannot check body.source.promoted,maybe stealed + // let already = 0; + + let mut extra_statements = vec![]; + // Visit candidates in reverse, in case they're nested. + for candidate in candidates.into_iter().rev() { + let Location { block, statement_index } = candidate.location; + if let StatementKind::Assign(box (place, _)) = &body[block].statements[statement_index].kind + { + if let Some(local) = place.as_local() { + if temps[local] == TempState::PromotedOut { + // Already promoted. + continue; + } + } + } + + // Declare return place local so that `mir::Body::new` doesn't complain. + let initial_locals = IndexVec::from([LocalDecl::new(tcx.types.never, body.span)]); + + let mut scope = body.source_scopes[body.source_info(candidate.location).scope].clone(); + scope.parent_scope = None; + + let mut promoted = Body::new( + /* source */ body.source, // `promoted` gets filled in below + /* basic_blocks */ IndexVec::new(), + /* source_scopes */ IndexVec::from([scope]), + /* local_decls */ initial_locals, + /* user_type_annotations */ IndexVec::new(), + /* arg_count */ 0, + /* var_debug_info */ vec![], + /* span */ body.span, + /* coroutine */ None, + /* tainted_by_errors */ body.tainted_by_errors, + ); + promoted.phase = MirPhase::Analysis(AnalysisPhase::Initial); + + let promoter = Promoter { + promoted, + tcx, + source: body, + temps: &mut temps, + extra_statements: &mut extra_statements, + keep_original: false, + add_to_required: false, + }; + + // `required_consts` of the promoted itself gets filled while building the MIR body. + let promoted = promoter.promote_candidate(candidate, promotions.len() + already_promoted); + // debug!(?promoted); + promotions.push(promoted); + } + + // Insert each of `extra_statements` before its indicated location, which + // has to be done in reverse location order, to not invalidate the rest. + extra_statements.sort_by_key(|&(loc, _)| cmp::Reverse(loc)); + for (loc, statement) in extra_statements { + body[loc.block].statements.insert(loc.statement_index, statement); + } + + promotions +} diff --git a/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff b/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff new file mode 100644 index 0000000000000..547975d6c76a7 --- /dev/null +++ b/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff @@ -0,0 +1,56 @@ +- // MIR for `main` before PromoteArraysOpt ++ // MIR for `main` after PromoteArraysOpt + + fn main() -> () { + let mut _0: (); + let _1: [i32; 32]; + let mut _3: &[i32; 32]; + let _4: [i32; 32]; + let _5: (); + let mut _6: [u16; 32]; ++ let mut _9: &[i32; 32]; + scope 1 { + debug _arr => _1; + let _2: [i32; 32]; + let mut _7: &[i32; 32]; + scope 2 { + debug _darr => _2; ++ let mut _8: &[u16; 32]; + } + } + + bb0: { + StorageLive(_1); +- _1 = [const 255_i32, const 105_i32, const 15_i32, const 39_i32, const 62_i32, const 251_i32, const 191_i32, const 178_i32, const 9_i32, const 4_i32, const 56_i32, const 221_i32, const 193_i32, const 164_i32, const 194_i32, const 197_i32, const 6_i32, const 243_i32, const 218_i32, const 171_i32, const 87_i32, const 247_i32, const 104_i32, const 159_i32, const 22_i32, const 157_i32, const 105_i32, const 31_i32, const 96_i32, const 173_i32, const 50_i32, const 1_i32]; ++ _9 = const main::promoted[2]; ++ _1 = (*_9); + FakeRead(ForLet(None), _1); + StorageLive(_2); + StorageLive(_3); + _7 = const main::promoted[0]; + _3 = &(*_7); + _2 = (*_3); + FakeRead(ForLet(None), _2); + StorageDead(_3); + StorageLive(_5); + StorageLive(_6); +- _6 = [const 255_u16, const 105_u16, const 15_u16, const 39_u16, const 62_u16, const 251_u16, const 191_u16, const 178_u16, const 9_u16, const 4_u16, const 56_u16, const 221_u16, const 193_u16, const 164_u16, const 194_u16, const 197_u16, const 6_u16, const 243_u16, const 218_u16, const 171_u16, const 87_u16, const 247_u16, const 104_u16, const 159_u16, const 22_u16, const 157_u16, const 105_u16, const 31_u16, const 96_u16, const 173_u16, const 50_u16, const 1_u16]; ++ _8 = const main::promoted[1]; ++ _6 = (*_8); + _5 = consume(move _6) -> [return: bb1, unwind: bb2]; + } + + bb1: { + StorageDead(_6); + StorageDead(_5); + _0 = const (); + StorageDead(_2); + StorageDead(_1); + return; + } + + bb2 (cleanup): { + resume; + } + } + diff --git a/tests/mir-opt/const_array_locals.rs b/tests/mir-opt/const_array_locals.rs new file mode 100644 index 0000000000000..bfebbf35f151d --- /dev/null +++ b/tests/mir-opt/const_array_locals.rs @@ -0,0 +1,30 @@ +//@ test-mir-pass: PromoteArraysOpt + +// EMIT_MIR const_array_locals.main.PromoteArraysOpt.diff +// CHECK-LABEL: fn main() -> () { +pub fn main() { + // CHECK: let [[array_lit:_.*]]: [i32; 32]; + // CHECK: let mut [[moved_array:_.*]]: [u16; 32]; + // CHECK: debug _arr => [[arr:_.*]]; + + // CHECK: [[array_lit]] = (*{{_[0-9]*}}) + let _arr = [ + 255, 105, 15, 39, 62, 251, 191, 178, 9, 4, 56, 221, + 193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104, + 159, 22, 157, 105, 31, 96, 173, 50, 1, + ]; + let _darr = *&[ + 255, 105, 15, 39, 62, 251, 191, 178, 9, 4, 56, 221, + 193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104, + 159, 22, 157, 105, 31, 96, 173, 50, 1, + ]; + + // CHECK: [[moved_array]] = (*{{_[0-9]*}}) + consume([255, 105, 15, 39, 62, 251, 191, 178, 9, 4, 56, 221, + 193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104, + 159, 22, 157, 105, 31, 96, 173, 50, 1,]); +} + +fn consume(_arr: [u16; 32]) { + unimplemented!() +} From 7a97b1c3b1611115ffb78d69983f32c34fbcd1df Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 3 Jun 2024 06:46:33 +0000 Subject: [PATCH 05/17] nested array --- tests/mir-opt/const_array_locals.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/mir-opt/const_array_locals.rs b/tests/mir-opt/const_array_locals.rs index bfebbf35f151d..fb6644b3492d0 100644 --- a/tests/mir-opt/const_array_locals.rs +++ b/tests/mir-opt/const_array_locals.rs @@ -13,6 +13,11 @@ pub fn main() { 193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104, 159, 22, 157, 105, 31, 96, 173, 50, 1, ]; + let _foo = [ + [255, 105, 15, 39, 62, 251, 191, 178, 9, 4, 56, 221], + [193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104], + [159, 22, 157, 105, 31, 96, 173, 50, 1], + ]; let _darr = *&[ 255, 105, 15, 39, 62, 251, 191, 178, 9, 4, 56, 221, 193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104, From 3e21e5f68c669e6de505911d0680a7ccdc106f68 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 3 Jun 2024 06:46:33 +0000 Subject: [PATCH 06/17] nested array --- tests/mir-opt/const_array_locals.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/mir-opt/const_array_locals.rs b/tests/mir-opt/const_array_locals.rs index fb6644b3492d0..439f18a3394ce 100644 --- a/tests/mir-opt/const_array_locals.rs +++ b/tests/mir-opt/const_array_locals.rs @@ -15,8 +15,7 @@ pub fn main() { ]; let _foo = [ [255, 105, 15, 39, 62, 251, 191, 178, 9, 4, 56, 221], - [193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104], - [159, 22, 157, 105, 31, 96, 173, 50, 1], + [193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104, 42], ]; let _darr = *&[ 255, 105, 15, 39, 62, 251, 191, 178, 9, 4, 56, 221, From b6bf4e4cc74cf9c34f7dd247cd1666c589611757 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Tue, 4 Jun 2024 14:00:54 +0000 Subject: [PATCH 07/17] eliminate promoted temp --- .../src/promote_consts_local_arrays.rs | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs index 9f06941a07669..5a34dc2d43213 100644 --- a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs +++ b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs @@ -1060,9 +1060,6 @@ fn promote_candidates<'tcx>( } let mut promotions = IndexVec::new(); - // FIXME: buggy things, there shouldn't be more than 42 promoted items by PromoteTemps. - // cannot check body.source.promoted,maybe stealed - // let already = 0; let mut extra_statements = vec![]; // Visit candidates in reverse, in case they're nested. @@ -1121,5 +1118,30 @@ fn promote_candidates<'tcx>( body[loc.block].statements.insert(loc.statement_index, statement); } + // Eliminate assignments to, and drops of promoted temps. + let is_promoted_out = |index: Local| temps[index] == TempState::PromotedOut; + for block in body.basic_blocks_mut() { + block.statements.retain(|statement| match &statement.kind { + StatementKind::Assign(box ( + place, + Rvalue::Use(Operand::Constant(box ConstOperand { + const_: Const::Val(_, ty), .. + })), + )) => { + if ty.is_unit() + && let Some(index) = place.as_local() + { + !is_promoted_out(index) + } else { + true + } + } + StatementKind::StorageLive(index) | StatementKind::StorageDead(index) => { + !is_promoted_out(*index) + } + _ => true, + }); + } + promotions } From 6b6bc183253013269a8116679a036fd843b3127d Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Tue, 4 Jun 2024 14:06:01 +0000 Subject: [PATCH 08/17] bless --- ...st_array_locals.main.PromoteArraysOpt.diff | 71 ++++++++++++------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff b/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff index 547975d6c76a7..4504e9fa3bda5 100644 --- a/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff +++ b/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff @@ -4,48 +4,67 @@ fn main() -> () { let mut _0: (); let _1: [i32; 32]; - let mut _3: &[i32; 32]; - let _4: [i32; 32]; - let _5: (); - let mut _6: [u16; 32]; -+ let mut _9: &[i32; 32]; + let mut _3: [i32; 12]; + let mut _4: [i32; 12]; + let mut _6: &[i32; 32]; + let _7: [i32; 32]; + let _8: (); + let mut _9: [u16; 32]; ++ let mut _13: &[i32; 32]; scope 1 { debug _arr => _1; - let _2: [i32; 32]; - let mut _7: &[i32; 32]; + let _2: [[i32; 12]; 2]; ++ let mut _12: &[[i32; 12]; 2]; scope 2 { - debug _darr => _2; -+ let mut _8: &[u16; 32]; + debug _foo => _2; + let _5: [i32; 32]; + let mut _10: &[i32; 32]; + scope 3 { + debug _darr => _5; ++ let mut _11: &[u16; 32]; + } } } bb0: { - StorageLive(_1); +- StorageLive(_1); - _1 = [const 255_i32, const 105_i32, const 15_i32, const 39_i32, const 62_i32, const 251_i32, const 191_i32, const 178_i32, const 9_i32, const 4_i32, const 56_i32, const 221_i32, const 193_i32, const 164_i32, const 194_i32, const 197_i32, const 6_i32, const 243_i32, const 218_i32, const 171_i32, const 87_i32, const 247_i32, const 104_i32, const 159_i32, const 22_i32, const 157_i32, const 105_i32, const 31_i32, const 96_i32, const 173_i32, const 50_i32, const 1_i32]; -+ _9 = const main::promoted[2]; -+ _1 = (*_9); ++ _13 = const main::promoted[3]; ++ _1 = (*_13); FakeRead(ForLet(None), _1); - StorageLive(_2); - StorageLive(_3); - _7 = const main::promoted[0]; - _3 = &(*_7); - _2 = (*_3); +- StorageLive(_2); +- StorageLive(_3); +- _3 = [const 255_i32, const 105_i32, const 15_i32, const 39_i32, const 62_i32, const 251_i32, const 191_i32, const 178_i32, const 9_i32, const 4_i32, const 56_i32, const 221_i32]; +- StorageLive(_4); +- _4 = [const 193_i32, const 164_i32, const 194_i32, const 197_i32, const 6_i32, const 243_i32, const 218_i32, const 171_i32, const 87_i32, const 247_i32, const 104_i32, const 42_i32]; +- _2 = [move _3, move _4]; +- StorageDead(_4); +- StorageDead(_3); ++ _12 = const main::promoted[2]; ++ _2 = (*_12); FakeRead(ForLet(None), _2); - StorageDead(_3); StorageLive(_5); StorageLive(_6); -- _6 = [const 255_u16, const 105_u16, const 15_u16, const 39_u16, const 62_u16, const 251_u16, const 191_u16, const 178_u16, const 9_u16, const 4_u16, const 56_u16, const 221_u16, const 193_u16, const 164_u16, const 194_u16, const 197_u16, const 6_u16, const 243_u16, const 218_u16, const 171_u16, const 87_u16, const 247_u16, const 104_u16, const 159_u16, const 22_u16, const 157_u16, const 105_u16, const 31_u16, const 96_u16, const 173_u16, const 50_u16, const 1_u16]; -+ _8 = const main::promoted[1]; -+ _6 = (*_8); - _5 = consume(move _6) -> [return: bb1, unwind: bb2]; + _10 = const main::promoted[0]; + _6 = &(*_10); + _5 = (*_6); + FakeRead(ForLet(None), _5); + StorageDead(_6); + StorageLive(_8); +- StorageLive(_9); +- _9 = [const 255_u16, const 105_u16, const 15_u16, const 39_u16, const 62_u16, const 251_u16, const 191_u16, const 178_u16, const 9_u16, const 4_u16, const 56_u16, const 221_u16, const 193_u16, const 164_u16, const 194_u16, const 197_u16, const 6_u16, const 243_u16, const 218_u16, const 171_u16, const 87_u16, const 247_u16, const 104_u16, const 159_u16, const 22_u16, const 157_u16, const 105_u16, const 31_u16, const 96_u16, const 173_u16, const 50_u16, const 1_u16]; ++ _11 = const main::promoted[1]; ++ _9 = (*_11); + _8 = consume(move _9) -> [return: bb1, unwind: bb2]; } bb1: { - StorageDead(_6); - StorageDead(_5); +- StorageDead(_9); + StorageDead(_8); _0 = const (); - StorageDead(_2); - StorageDead(_1); + StorageDead(_5); +- StorageDead(_2); +- StorageDead(_1); return; } From 19a01fdd75cbe7ba1899e71cb6fda23f1cd5a02d Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Tue, 4 Jun 2024 14:12:53 +0000 Subject: [PATCH 09/17] fmt --- tests/mir-opt/const_array_locals.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/mir-opt/const_array_locals.rs b/tests/mir-opt/const_array_locals.rs index 439f18a3394ce..af769bd76e69d 100644 --- a/tests/mir-opt/const_array_locals.rs +++ b/tests/mir-opt/const_array_locals.rs @@ -2,6 +2,7 @@ // EMIT_MIR const_array_locals.main.PromoteArraysOpt.diff // CHECK-LABEL: fn main() -> () { +#[rustfmt::skip] pub fn main() { // CHECK: let [[array_lit:_.*]]: [i32; 32]; // CHECK: let mut [[moved_array:_.*]]: [u16; 32]; From dfcae48d0bde4676afee2e7493a949c7e44aa986 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Wed, 5 Jun 2024 15:19:09 +0000 Subject: [PATCH 10/17] ignore const/static items --- .../src/promote_consts_local_arrays.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs index 5a34dc2d43213..afc898a6d525d 100644 --- a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs +++ b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs @@ -72,6 +72,15 @@ impl<'tcx> MirPass<'tcx> for PromoteArraysOpt<'tcx> { return; } + // Ignore static/const items. Already processed by PromoteTemps + if let Some(ctx) = tcx.hir().body_const_context(body.source.def_id()) { + use hir::ConstContext::*; + match ctx { + Static(_) | Const {inline: _ } => return, + _ => {} + } + } + let ccx = ConstCx::new(tcx, body); let (mut temps, all_candidates, already_promoted) = collect_temps_and_candidates(&ccx); From e53c2318995c8a3bf001e83bc83c51d22df41211 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 6 Jun 2024 07:24:16 +0000 Subject: [PATCH 11/17] ignore simd temps --- .../src/promote_consts_local_arrays.rs | 23 +++++++++++++++---- tests/mir-opt/const_array_locals.rs | 6 +++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs index afc898a6d525d..c94f13d062153 100644 --- a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs +++ b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs @@ -76,7 +76,7 @@ impl<'tcx> MirPass<'tcx> for PromoteArraysOpt<'tcx> { if let Some(ctx) = tcx.hir().body_const_context(body.source.def_id()) { use hir::ConstContext::*; match ctx { - Static(_) | Const {inline: _ } => return, + Static(_) | Const { inline: _ } => return, _ => {} } } @@ -178,16 +178,29 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> { #[instrument(level = "debug", skip(self, rvalue))] fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) { - if !rvalue.ty(&self.ccx.body.local_decls, self.ccx.tcx).is_array() { + self.super_rvalue(rvalue, location); + + let rvalue_ty = rvalue.ty(&self.ccx.body.local_decls, self.ccx.tcx); + if !rvalue_ty.is_array() { debug!(?rvalue); } - self.super_rvalue(rvalue, location); - - let Rvalue::Aggregate(kind, _ops) = rvalue else { + let Rvalue::Aggregate(kind, operands) = rvalue else { return; }; + if let ty::Adt(adt, _) = rvalue_ty.kind() + && adt.repr().simd() + { + debug!("ignore #[repr(simd)]"); + if let Some(first) = operands.iter().next() + && let Operand::Copy(place) | Operand::Move(place) = first + { + self.temps[place.local] = TempState::Unpromotable; + } + return; + } + debug!("pushing a candidate of type {:?} @ {:?}", kind, location); self.candidates.push(Candidate { location }); } diff --git a/tests/mir-opt/const_array_locals.rs b/tests/mir-opt/const_array_locals.rs index af769bd76e69d..f0fbe24f0738a 100644 --- a/tests/mir-opt/const_array_locals.rs +++ b/tests/mir-opt/const_array_locals.rs @@ -1,4 +1,8 @@ //@ test-mir-pass: PromoteArraysOpt +#![feature(repr_simd)] + +#[repr(simd)] +struct F32x8([f32; 8]); // EMIT_MIR const_array_locals.main.PromoteArraysOpt.diff // CHECK-LABEL: fn main() -> () { @@ -28,6 +32,8 @@ pub fn main() { consume([255, 105, 15, 39, 62, 251, 191, 178, 9, 4, 56, 221, 193, 164, 194, 197, 6, 243, 218, 171, 87, 247, 104, 159, 22, 157, 105, 31, 96, 173, 50, 1,]); + + let _f = F32x8([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]); } fn consume(_arr: [u16; 32]) { From d11c9e2cc68a3c3f8eb44aa8d7895a56ca3a4766 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 6 Jun 2024 07:29:33 +0000 Subject: [PATCH 12/17] bless --- ...st_array_locals.main.PromoteArraysOpt.diff | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff b/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff index 4504e9fa3bda5..488f0c06e0828 100644 --- a/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff +++ b/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff @@ -10,18 +10,23 @@ let _7: [i32; 32]; let _8: (); let mut _9: [u16; 32]; -+ let mut _13: &[i32; 32]; + let mut _11: [f32; 8]; ++ let mut _15: &[i32; 32]; scope 1 { debug _arr => _1; let _2: [[i32; 12]; 2]; -+ let mut _12: &[[i32; 12]; 2]; ++ let mut _14: &[[i32; 12]; 2]; scope 2 { debug _foo => _2; let _5: [i32; 32]; - let mut _10: &[i32; 32]; + let mut _12: &[i32; 32]; scope 3 { debug _darr => _5; -+ let mut _11: &[u16; 32]; + let _10: F32x8; ++ let mut _13: &[u16; 32]; + scope 4 { + debug _f => _10; + } } } } @@ -29,8 +34,8 @@ bb0: { - StorageLive(_1); - _1 = [const 255_i32, const 105_i32, const 15_i32, const 39_i32, const 62_i32, const 251_i32, const 191_i32, const 178_i32, const 9_i32, const 4_i32, const 56_i32, const 221_i32, const 193_i32, const 164_i32, const 194_i32, const 197_i32, const 6_i32, const 243_i32, const 218_i32, const 171_i32, const 87_i32, const 247_i32, const 104_i32, const 159_i32, const 22_i32, const 157_i32, const 105_i32, const 31_i32, const 96_i32, const 173_i32, const 50_i32, const 1_i32]; -+ _13 = const main::promoted[3]; -+ _1 = (*_13); ++ _15 = const main::promoted[3]; ++ _1 = (*_15); FakeRead(ForLet(None), _1); - StorageLive(_2); - StorageLive(_3); @@ -40,28 +45,35 @@ - _2 = [move _3, move _4]; - StorageDead(_4); - StorageDead(_3); -+ _12 = const main::promoted[2]; -+ _2 = (*_12); ++ _14 = const main::promoted[2]; ++ _2 = (*_14); FakeRead(ForLet(None), _2); StorageLive(_5); StorageLive(_6); - _10 = const main::promoted[0]; - _6 = &(*_10); + _12 = const main::promoted[0]; + _6 = &(*_12); _5 = (*_6); FakeRead(ForLet(None), _5); StorageDead(_6); StorageLive(_8); - StorageLive(_9); - _9 = [const 255_u16, const 105_u16, const 15_u16, const 39_u16, const 62_u16, const 251_u16, const 191_u16, const 178_u16, const 9_u16, const 4_u16, const 56_u16, const 221_u16, const 193_u16, const 164_u16, const 194_u16, const 197_u16, const 6_u16, const 243_u16, const 218_u16, const 171_u16, const 87_u16, const 247_u16, const 104_u16, const 159_u16, const 22_u16, const 157_u16, const 105_u16, const 31_u16, const 96_u16, const 173_u16, const 50_u16, const 1_u16]; -+ _11 = const main::promoted[1]; -+ _9 = (*_11); ++ _13 = const main::promoted[1]; ++ _9 = (*_13); _8 = consume(move _9) -> [return: bb1, unwind: bb2]; } bb1: { - StorageDead(_9); StorageDead(_8); + StorageLive(_10); + StorageLive(_11); + _11 = [const 1f32, const 1f32, const 1f32, const 1f32, const 1f32, const 1f32, const 1f32, const 1f32]; + _10 = F32x8(move _11); + StorageDead(_11); + FakeRead(ForLet(None), _10); _0 = const (); + StorageDead(_10); StorageDead(_5); - StorageDead(_2); - StorageDead(_1); From 1f40fa4589f3ad4075d8a6cf550b78aa53d30bb1 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 6 Jun 2024 07:50:38 +0000 Subject: [PATCH 13/17] ignore rvalue not array --- .../src/promote_consts_local_arrays.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs index c94f13d062153..b6d3e41df0896 100644 --- a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs +++ b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs @@ -180,19 +180,14 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> { fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) { self.super_rvalue(rvalue, location); - let rvalue_ty = rvalue.ty(&self.ccx.body.local_decls, self.ccx.tcx); - if !rvalue_ty.is_array() { - debug!(?rvalue); - } - let Rvalue::Aggregate(kind, operands) = rvalue else { + debug!("ignoring rvalue=`{rvalue:?}`"); return; }; - if let ty::Adt(adt, _) = rvalue_ty.kind() - && adt.repr().simd() - { - debug!("ignore #[repr(simd)]"); + let rvalue_ty = rvalue.ty(&self.ccx.body.local_decls, self.ccx.tcx); + if !rvalue_ty.is_array() { + debug!("ignoring rvalue=`{rvalue:?}`"); if let Some(first) = operands.iter().next() && let Operand::Copy(place) | Operand::Move(place) = first { From 7c4dccaf8cca7c37d1bc33b2547ccc3f108219f6 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 6 Jun 2024 08:12:58 +0000 Subject: [PATCH 14/17] aos => soa --- .../src/promote_consts_local_arrays.rs | 55 +++++++++---------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs index b6d3e41df0896..4b116f0b7e222 100644 --- a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs +++ b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs @@ -82,9 +82,9 @@ impl<'tcx> MirPass<'tcx> for PromoteArraysOpt<'tcx> { } let ccx = ConstCx::new(tcx, body); - let (mut temps, all_candidates, already_promoted) = collect_temps_and_candidates(&ccx); + let (mut temps, mut all_candidates, already_promoted) = collect_temps_and_candidates(&ccx); - let promotable_candidates = validate_candidates(&ccx, &mut temps, &all_candidates); + let promotable_candidates = validate_candidates(&ccx, &mut temps, &mut all_candidates); debug!(candidates = ?promotable_candidates); let promoted = @@ -109,18 +109,16 @@ enum TempState { PromotedOut, } -/// A "root candidate" for promotion, which will become the -/// returned value in a promoted MIR, unless it's a subset -/// of a larger candidate. -#[derive(Copy, Clone, PartialEq, Eq, Debug)] -struct Candidate { - location: Location, -} +#[derive(Clone, Debug)] +struct Candidates(Vec); struct Collector<'a, 'tcx> { ccx: &'a ConstCx<'a, 'tcx>, temps: IndexVec, - candidates: Vec, + /// A "root candidate" for promotion, which will become the + /// returned value in a promoted MIR, unless it's a subset + /// of a larger candidate. + candidates: Candidates, already_promoted: usize, } @@ -197,7 +195,7 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> { } debug!("pushing a candidate of type {:?} @ {:?}", kind, location); - self.candidates.push(Candidate { location }); + self.candidates.0.push(location); } // #[instrument(level = "debug", skip(self, constant))] @@ -214,10 +212,10 @@ impl<'tcx> Visitor<'tcx> for Collector<'_, 'tcx> { fn collect_temps_and_candidates<'tcx>( ccx: &ConstCx<'_, 'tcx>, -) -> (IndexVec, Vec, usize) { +) -> (IndexVec, Candidates, usize) { let mut collector = Collector { temps: IndexVec::from_elem(TempState::Undefined, &ccx.body.local_decls), - candidates: vec![], + candidates: Candidates(vec![]), ccx, already_promoted: 0, }; @@ -254,8 +252,8 @@ impl<'a, 'tcx> std::ops::Deref for Validator<'a, 'tcx> { struct Unpromotable; impl<'tcx> Validator<'_, 'tcx> { - fn validate_candidate(&mut self, candidate: Candidate) -> Result<(), Unpromotable> { - let Left(statement) = self.body.stmt_at(candidate.location) else { bug!() }; + fn validate_candidate(&mut self, candidate: Location) -> Result<(), Unpromotable> { + let Left(statement) = self.body.stmt_at(candidate) else { bug!() }; let Some((place, rvalue @ Rvalue::Aggregate(box kind, operands))) = statement.kind.as_assign() else { @@ -780,18 +778,15 @@ impl<'tcx> Validator<'_, 'tcx> { } } -fn validate_candidates( +fn validate_candidates<'a>( ccx: &ConstCx<'_, '_>, temps: &mut IndexSlice, - candidates: &[Candidate], -) -> Vec { + candidates: &'a mut Candidates, +) -> &'a mut Candidates { let mut validator = Validator { ccx, temps, promotion_safe_blocks: None }; + candidates.0.retain(|&candidate| validator.validate_candidate(candidate).is_ok()); candidates - .iter() - .copied() - .filter(|&candidate| validator.validate_candidate(candidate).is_ok()) - .collect() } struct Promoter<'a, 'tcx> { @@ -946,7 +941,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> { new_temp } - fn promote_candidate(mut self, candidate: Candidate, next_promoted_id: usize) -> Body<'tcx> { + fn promote_candidate(mut self, candidate: Location, next_promoted_id: usize) -> Body<'tcx> { let def = self.source.source.def_id(); let promoted_id = Promoted::new(next_promoted_id); let (mut rvalue, promoted_op, promoted_ref_rvalue) = { @@ -963,7 +958,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> { let blocks = self.source.basic_blocks.as_mut(); let local_decls = &mut self.source.local_decls; - let loc = candidate.location; + let loc = candidate; let statement = &mut blocks[loc.block].statements[loc.statement_index]; let StatementKind::Assign(box (place, Rvalue::Aggregate(_kind, _operands))) = &mut statement.kind @@ -1017,7 +1012,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> { { let blocks = self.source.basic_blocks_mut(); - let loc = candidate.location; + let loc = candidate; let statement = &mut blocks[loc.block].statements[loc.statement_index]; if let StatementKind::Assign(box (_place, ref mut rvalue)) = &mut statement.kind { *rvalue = promoted_rvalue; @@ -1068,11 +1063,11 @@ fn promote_candidates<'tcx>( body: &mut Body<'tcx>, tcx: TyCtxt<'tcx>, mut temps: IndexVec, - candidates: Vec, + candidates: &Candidates, already_promoted: usize, ) -> IndexVec> { // eagerly fail fast - if candidates.is_empty() { + if candidates.0.is_empty() { return IndexVec::new(); } @@ -1080,8 +1075,8 @@ fn promote_candidates<'tcx>( let mut extra_statements = vec![]; // Visit candidates in reverse, in case they're nested. - for candidate in candidates.into_iter().rev() { - let Location { block, statement_index } = candidate.location; + for candidate in candidates.0.iter().copied().rev() { + let Location { block, statement_index } = candidate; if let StatementKind::Assign(box (place, _)) = &body[block].statements[statement_index].kind { if let Some(local) = place.as_local() { @@ -1095,7 +1090,7 @@ fn promote_candidates<'tcx>( // Declare return place local so that `mir::Body::new` doesn't complain. let initial_locals = IndexVec::from([LocalDecl::new(tcx.types.never, body.span)]); - let mut scope = body.source_scopes[body.source_info(candidate.location).scope].clone(); + let mut scope = body.source_scopes[body.source_info(candidate).scope].clone(); scope.parent_scope = None; let mut promoted = Body::new( From ea443af8be3a30a75437ad47fb83038ba9f89938 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 6 Jun 2024 08:48:43 +0000 Subject: [PATCH 15/17] fix ice nested array --- .../rustc_mir_transform/src/promote_consts_local_arrays.rs | 3 +++ tests/mir-opt/const_array_locals.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs index 4b116f0b7e222..f5afc47079b57 100644 --- a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs +++ b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs @@ -252,6 +252,7 @@ impl<'a, 'tcx> std::ops::Deref for Validator<'a, 'tcx> { struct Unpromotable; impl<'tcx> Validator<'_, 'tcx> { + #[instrument(level = "trace", skip(self))] fn validate_candidate(&mut self, candidate: Location) -> Result<(), Unpromotable> { let Left(statement) = self.body.stmt_at(candidate) else { bug!() }; let Some((place, rvalue @ Rvalue::Aggregate(box kind, operands))) = @@ -276,6 +277,8 @@ impl<'tcx> Validator<'_, 'tcx> { && size.bytes() <= STACK_THRESHOLD { debug!("size of array is too small: {:?}", size); + // for nested arrays + self.temps[place.local] = TempState::Unpromotable; return Err(Unpromotable); } diff --git a/tests/mir-opt/const_array_locals.rs b/tests/mir-opt/const_array_locals.rs index f0fbe24f0738a..8aef101e0616c 100644 --- a/tests/mir-opt/const_array_locals.rs +++ b/tests/mir-opt/const_array_locals.rs @@ -34,6 +34,9 @@ pub fn main() { 159, 22, 157, 105, 31, 96, 173, 50, 1,]); let _f = F32x8([1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]); + + // ice + [[1, 0, 0], [0, 1, 0], [0, 0, 1]]; // 2D array } fn consume(_arr: [u16; 32]) { From 514e4a9fb2537d74d6a66eda1fef96fcef3bb52c Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 6 Jun 2024 12:37:57 +0000 Subject: [PATCH 16/17] bless --- ...st_array_locals.main.PromoteArraysOpt.diff | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff b/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff index 488f0c06e0828..5873d814ff6a1 100644 --- a/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff +++ b/tests/mir-opt/const_array_locals.main.PromoteArraysOpt.diff @@ -11,19 +11,23 @@ let _8: (); let mut _9: [u16; 32]; let mut _11: [f32; 8]; -+ let mut _15: &[i32; 32]; + let _12: [[i32; 3]; 3]; + let mut _13: [i32; 3]; + let mut _14: [i32; 3]; + let mut _15: [i32; 3]; ++ let mut _19: &[i32; 32]; scope 1 { debug _arr => _1; let _2: [[i32; 12]; 2]; -+ let mut _14: &[[i32; 12]; 2]; ++ let mut _18: &[[i32; 12]; 2]; scope 2 { debug _foo => _2; let _5: [i32; 32]; - let mut _12: &[i32; 32]; + let mut _16: &[i32; 32]; scope 3 { debug _darr => _5; let _10: F32x8; -+ let mut _13: &[u16; 32]; ++ let mut _17: &[u16; 32]; scope 4 { debug _f => _10; } @@ -34,8 +38,8 @@ bb0: { - StorageLive(_1); - _1 = [const 255_i32, const 105_i32, const 15_i32, const 39_i32, const 62_i32, const 251_i32, const 191_i32, const 178_i32, const 9_i32, const 4_i32, const 56_i32, const 221_i32, const 193_i32, const 164_i32, const 194_i32, const 197_i32, const 6_i32, const 243_i32, const 218_i32, const 171_i32, const 87_i32, const 247_i32, const 104_i32, const 159_i32, const 22_i32, const 157_i32, const 105_i32, const 31_i32, const 96_i32, const 173_i32, const 50_i32, const 1_i32]; -+ _15 = const main::promoted[3]; -+ _1 = (*_15); ++ _19 = const main::promoted[3]; ++ _1 = (*_19); FakeRead(ForLet(None), _1); - StorageLive(_2); - StorageLive(_3); @@ -45,21 +49,21 @@ - _2 = [move _3, move _4]; - StorageDead(_4); - StorageDead(_3); -+ _14 = const main::promoted[2]; -+ _2 = (*_14); ++ _18 = const main::promoted[2]; ++ _2 = (*_18); FakeRead(ForLet(None), _2); StorageLive(_5); StorageLive(_6); - _12 = const main::promoted[0]; - _6 = &(*_12); + _16 = const main::promoted[0]; + _6 = &(*_16); _5 = (*_6); FakeRead(ForLet(None), _5); StorageDead(_6); StorageLive(_8); - StorageLive(_9); - _9 = [const 255_u16, const 105_u16, const 15_u16, const 39_u16, const 62_u16, const 251_u16, const 191_u16, const 178_u16, const 9_u16, const 4_u16, const 56_u16, const 221_u16, const 193_u16, const 164_u16, const 194_u16, const 197_u16, const 6_u16, const 243_u16, const 218_u16, const 171_u16, const 87_u16, const 247_u16, const 104_u16, const 159_u16, const 22_u16, const 157_u16, const 105_u16, const 31_u16, const 96_u16, const 173_u16, const 50_u16, const 1_u16]; -+ _13 = const main::promoted[1]; -+ _9 = (*_13); ++ _17 = const main::promoted[1]; ++ _9 = (*_17); _8 = consume(move _9) -> [return: bb1, unwind: bb2]; } @@ -72,6 +76,18 @@ _10 = F32x8(move _11); StorageDead(_11); FakeRead(ForLet(None), _10); + StorageLive(_12); + StorageLive(_13); + _13 = [const 1_i32, const 0_i32, const 0_i32]; + StorageLive(_14); + _14 = [const 0_i32, const 1_i32, const 0_i32]; + StorageLive(_15); + _15 = [const 0_i32, const 0_i32, const 1_i32]; + _12 = [move _13, move _14, move _15]; + StorageDead(_15); + StorageDead(_14); + StorageDead(_13); + StorageDead(_12); _0 = const (); StorageDead(_10); StorageDead(_5); From dca720706d6091d51ca7f69ace440e7c0778be1f Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sat, 8 Jun 2024 16:05:02 +0000 Subject: [PATCH 17/17] use ccx --- .../src/promote_consts_local_arrays.rs | 11 ++++------- tests/mir-opt/const_array_locals.rs | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs index f5afc47079b57..ba9b18add2ac1 100644 --- a/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs +++ b/compiler/rustc_mir_transform/src/promote_consts_local_arrays.rs @@ -60,6 +60,7 @@ impl<'tcx> MirPass<'tcx> for PromoteArraysOpt<'tcx> { #[instrument(level = "trace", skip(self, tcx, body))] fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { + use hir::ConstContext::{Const, Static}; debug!("running on body: {:?}", body.source.def_id()); // There's not really any point in promoting errorful MIR. // @@ -72,16 +73,12 @@ impl<'tcx> MirPass<'tcx> for PromoteArraysOpt<'tcx> { return; } + let ccx = ConstCx::new(tcx, body); // Ignore static/const items. Already processed by PromoteTemps - if let Some(ctx) = tcx.hir().body_const_context(body.source.def_id()) { - use hir::ConstContext::*; - match ctx { - Static(_) | Const { inline: _ } => return, - _ => {} - } + if let Some(Static(_) | Const { inline: _ }) = ccx.const_kind { + return; } - let ccx = ConstCx::new(tcx, body); let (mut temps, mut all_candidates, already_promoted) = collect_temps_and_candidates(&ccx); let promotable_candidates = validate_candidates(&ccx, &mut temps, &mut all_candidates); diff --git a/tests/mir-opt/const_array_locals.rs b/tests/mir-opt/const_array_locals.rs index 8aef101e0616c..614033c17925c 100644 --- a/tests/mir-opt/const_array_locals.rs +++ b/tests/mir-opt/const_array_locals.rs @@ -5,7 +5,7 @@ struct F32x8([f32; 8]); // EMIT_MIR const_array_locals.main.PromoteArraysOpt.diff -// CHECK-LABEL: fn main() -> () { +// CHECK-LABEL: fn main( #[rustfmt::skip] pub fn main() { // CHECK: let [[array_lit:_.*]]: [i32; 32];