Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move code into rustc_mir_transform #137008

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ pub mod generic_graphviz;
pub mod graphviz;
pub mod interpret;
pub mod mono;
pub mod patch;
pub mod pretty;
mod query;
mod statement;
Expand Down
21 changes: 20 additions & 1 deletion compiler/rustc_mir_dataflow/src/drop_flag_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,26 @@ use rustc_middle::mir::{self, Body, Location, Terminator, TerminatorKind};
use tracing::debug;

use super::move_paths::{InitKind, LookupResult, MoveData, MovePathIndex};
use crate::elaborate_drops::DropFlagState;

/// The value of an inserted drop flag.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum DropFlagState {
/// The tracked value is initialized and needs to be dropped when leaving its scope.
Present,

/// The tracked value is uninitialized or was moved out of and does not need to be dropped when
/// leaving its scope.
Absent,
}

impl DropFlagState {
pub fn value(self) -> bool {
match self {
DropFlagState::Present => true,
DropFlagState::Absent => false,
}
}
}

pub fn move_path_children_matching<'tcx, F>(
move_data: &MoveData<'tcx>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_dataflow/src/impls/initialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_middle::ty::util::Discr;
use rustc_middle::ty::{self, TyCtxt};
use tracing::{debug, instrument};

use crate::elaborate_drops::DropFlagState;
use crate::drop_flag_effects::DropFlagState;
use crate::framework::SwitchIntTarget;
use crate::move_paths::{HasMoveData, InitIndex, InitKind, LookupResult, MoveData, MovePathIndex};
use crate::{
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_mir_dataflow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_middle::ty;
// Please change the public `use` directives cautiously, as they might be used by external tools.
// See issue #120130.
pub use self::drop_flag_effects::{
drop_flag_effects_for_function_entry, drop_flag_effects_for_location,
DropFlagState, drop_flag_effects_for_function_entry, drop_flag_effects_for_location,
move_path_children_matching, on_all_children_bits, on_lookup_result_bits,
};
pub use self::framework::{
Expand All @@ -26,7 +26,6 @@ use self::move_paths::MoveData;

pub mod debuginfo;
mod drop_flag_effects;
pub mod elaborate_drops;
mod errors;
mod framework;
pub mod impls;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rustc_middle::mir::patch::MirPatch;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt};
use tracing::debug;

use crate::patch::MirPatch;
use crate::util;

/// This pass moves values being dropped that are within a packed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use rustc_middle::mir::patch::MirPatch;
use rustc_middle::mir::visit::MutVisitor;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;

use crate::patch::MirPatch;

pub(super) struct Subtyper;

struct SubTypeChecker<'a, 'tcx> {
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_mir_transform/src/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,9 +1061,8 @@ fn insert_switch<'tcx>(
}

fn elaborate_coroutine_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
use rustc_middle::mir::patch::MirPatch;
use rustc_mir_dataflow::elaborate_drops::{Unwind, elaborate_drop};

use crate::elaborate_drop::{Unwind, elaborate_drop};
use crate::patch::MirPatch;
use crate::shim::DropShimElaborator;

// Note that `elaborate_drops` only drops the upvars of a coroutine, and
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_mir_transform/src/deref_separator.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use rustc_middle::mir::patch::MirPatch;
use rustc_middle::mir::visit::NonUseContext::VarDebugInfo;
use rustc_middle::mir::visit::{MutVisitor, PlaceContext};
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;

use crate::patch::MirPatch;

pub(super) struct Derefer;

struct DerefChecker<'a, 'tcx> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/early_otherwise_branch.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::fmt::Debug;

use rustc_middle::mir::patch::MirPatch;
use rustc_middle::mir::*;
use rustc_middle::ty::{Ty, TyCtxt};
use tracing::trace;

use super::simplify::simplify_cfg;
use crate::patch::MirPatch;

/// This pass optimizes something like
/// ```ignore (syntax-highlighting-only)
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_mir_transform/src/elaborate_box_derefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use rustc_abi::FieldIdx;
use rustc_hir::def_id::DefId;
use rustc_middle::mir::patch::MirPatch;
use rustc_middle::mir::visit::MutVisitor;
use rustc_middle::mir::*;
use rustc_middle::span_bug;
use rustc_middle::ty::{Ty, TyCtxt};

use crate::patch::MirPatch;

/// Constructs the types used when accessing a Box's pointer
fn build_ptr_tys<'tcx>(
tcx: TyCtxt<'tcx>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{fmt, iter, mem};
use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx};
use rustc_hir::lang_items::LangItem;
use rustc_index::Idx;
use rustc_middle::mir::patch::MirPatch;
use rustc_middle::mir::*;
use rustc_middle::span_bug;
use rustc_middle::ty::adjustment::PointerCoercion;
Expand All @@ -13,29 +12,11 @@ use rustc_span::DUMMY_SP;
use rustc_span::source_map::Spanned;
use tracing::{debug, instrument};

/// The value of an inserted drop flag.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum DropFlagState {
/// The tracked value is initialized and needs to be dropped when leaving its scope.
Present,

/// The tracked value is uninitialized or was moved out of and does not need to be dropped when
/// leaving its scope.
Absent,
}

impl DropFlagState {
pub fn value(self) -> bool {
match self {
DropFlagState::Present => true,
DropFlagState::Absent => false,
}
}
}
use crate::patch::MirPatch;

/// Describes how/if a value should be dropped.
#[derive(Debug)]
pub enum DropStyle {
pub(crate) enum DropStyle {
/// The value is already dead at the drop location, no drop will be executed.
Dead,

Expand All @@ -56,7 +37,7 @@ pub enum DropStyle {

/// Which drop flags to affect/check with an operation.
#[derive(Debug)]
pub enum DropFlagMode {
pub(crate) enum DropFlagMode {
/// Only affect the top-level drop flag, not that of any contained fields.
Shallow,
/// Affect all nested drop flags in addition to the top-level one.
Expand All @@ -65,7 +46,7 @@ pub enum DropFlagMode {

/// Describes if unwinding is necessary and where to unwind to if a panic occurs.
#[derive(Copy, Clone, Debug)]
pub enum Unwind {
pub(crate) enum Unwind {
/// Unwind to this block.
To(BasicBlock),
/// Already in an unwind path, any panic will cause an abort.
Expand Down Expand Up @@ -98,7 +79,7 @@ impl Unwind {
}
}

pub trait DropElaborator<'a, 'tcx>: fmt::Debug {
pub(crate) trait DropElaborator<'a, 'tcx>: fmt::Debug {
/// The type representing paths that can be moved out of.
///
/// Users can move out of individual fields of a struct, such as `a.b.c`. This type is used to
Expand Down Expand Up @@ -177,7 +158,7 @@ where
/// value.
///
/// When this returns, the MIR patch in the `elaborator` contains the necessary changes.
pub fn elaborate_drop<'b, 'tcx, D>(
pub(crate) fn elaborate_drop<'b, 'tcx, D>(
elaborator: &mut D,
source_info: SourceInfo,
place: Place<'tcx>,
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_mir_transform/src/elaborate_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ use std::fmt;
use rustc_abi::{FieldIdx, VariantIdx};
use rustc_index::IndexVec;
use rustc_index::bit_set::DenseBitSet;
use rustc_middle::mir::patch::MirPatch;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt};
use rustc_mir_dataflow::elaborate_drops::{
DropElaborator, DropFlagMode, DropFlagState, DropStyle, Unwind, elaborate_drop,
};
use rustc_mir_dataflow::impls::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
use rustc_mir_dataflow::move_paths::{LookupResult, MoveData, MovePathIndex};
use rustc_mir_dataflow::{
Analysis, MoveDataTypingEnv, ResultsCursor, on_all_children_bits, on_lookup_result_bits,
Analysis, DropFlagState, MoveDataTypingEnv, ResultsCursor, on_all_children_bits,
on_lookup_result_bits,
};
use rustc_span::Span;
use tracing::{debug, instrument};

use crate::deref_separator::deref_finder;
use crate::elaborate_drop::{DropElaborator, DropFlagMode, DropStyle, Unwind, elaborate_drop};
use crate::patch::MirPatch;

/// During MIR building, Drop terminators are inserted in every place where a drop may occur.
/// However, in this phase, the presence of these terminators does not guarantee that a destructor
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ mod check_pointers;
mod cost_checker;
mod cross_crate_inline;
mod deduce_param_attrs;
mod elaborate_drop;
mod errors;
mod ffi_unwind_calls;
mod lint;
mod lint_tail_expr_drop_order;
mod patch;
mod shim;
mod ssa;

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/match_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::iter;

use rustc_abi::Integer;
use rustc_index::IndexSlice;
use rustc_middle::mir::patch::MirPatch;
use rustc_middle::mir::*;
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt};
use rustc_type_ir::TyKind::*;
use tracing::instrument;

use super::simplify::simplify_cfg;
use crate::patch::MirPatch;

pub(super) struct MatchBranchSimplification;

Expand Down
Loading
Loading