diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 11fef3be5d09b..30bbbee36d054 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -282,20 +282,6 @@ provide! { tcx, def_id, other, cdata, .unwrap_or_else(|| panic!("{def_id:?} does not have eval_static_initializer"))) } trait_def => { table } - deduced_param_attrs => { - // FIXME: `deduced_param_attrs` has some sketchy encoding settings, - // where we don't encode unless we're optimizing, doing codegen, - // and not incremental (see `encoder.rs`). I don't think this is right! - cdata - .root - .tables - .deduced_param_attrs - .get(cdata, def_id.index) - .map(|lazy| { - &*tcx.arena.alloc_from_iter(lazy.decode((cdata, tcx))) - }) - .unwrap_or_default() - } opaque_ty_origin => { table } assumed_wf_types_for_rpitit => { table } collect_return_position_impl_trait_in_trait_tys => { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index a7e7e9985f4d4..a80b5a302066c 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -27,7 +27,7 @@ use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::fast_reject::{self, TreatParams}; use rustc_middle::{bug, span_bug}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque}; -use rustc_session::config::{CrateType, OptLevel, TargetModifier}; +use rustc_session::config::{CrateType, TargetModifier}; use rustc_span::hygiene::HygieneEncodeContext; use rustc_span::{ ByteSymbol, ExternalSource, FileName, SourceFile, SpanData, SpanEncoder, StableSourceFileId, @@ -1822,21 +1822,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { record!(self.tables.mir_coroutine_witnesses[def_id.to_def_id()] <- witnesses); } } - - // Encode all the deduced parameter attributes for everything that has MIR, even for items - // that can't be inlined. But don't if we aren't optimizing in non-incremental mode, to - // save the query traffic. - if tcx.sess.opts.output_types.should_codegen() - && tcx.sess.opts.optimize != OptLevel::No - && tcx.sess.opts.incremental.is_none() - { - for &local_def_id in tcx.mir_keys(()) { - if let DefKind::AssocFn | DefKind::Fn = tcx.def_kind(local_def_id) { - record_array!(self.tables.deduced_param_attrs[local_def_id.to_def_id()] <- - self.tcx.deduced_param_attrs(local_def_id.to_def_id())); - } - } - } } #[instrument(level = "debug", skip(self))] diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 1f7d142d33000..05204dc876af6 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -29,7 +29,7 @@ use rustc_middle::middle::lib_features::FeatureStability; use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault; use rustc_middle::mir; use rustc_middle::ty::fast_reject::SimplifiedType; -use rustc_middle::ty::{self, DeducedParamAttrs, Ty, TyCtxt, UnusedGenericParams}; +use rustc_middle::ty::{self, Ty, TyCtxt, UnusedGenericParams}; use rustc_middle::util::Providers; use rustc_serialize::opaque::FileEncoder; use rustc_session::config::{SymbolManglingVersion, TargetModifier}; @@ -462,7 +462,6 @@ define_tables! { assoc_container: Table, macro_definition: Table>, proc_macro: Table, - deduced_param_attrs: Table>, trait_impl_trait_tys: Table>>>>, doc_link_resolutions: Table>, doc_link_traits_in_scope: Table>, diff --git a/compiler/rustc_metadata/src/rmeta/parameterized.rs b/compiler/rustc_metadata/src/rmeta/parameterized.rs index 34180001f8044..f2971e79d55fc 100644 --- a/compiler/rustc_metadata/src/rmeta/parameterized.rs +++ b/compiler/rustc_metadata/src/rmeta/parameterized.rs @@ -105,7 +105,6 @@ trivially_parameterized_over_tcx! { rustc_middle::ty::AssocItemContainer, rustc_middle::ty::AsyncDestructor, rustc_middle::ty::Asyncness, - rustc_middle::ty::DeducedParamAttrs, rustc_middle::ty::Destructor, rustc_middle::ty::Generics, rustc_middle::ty::ImplTraitInTraitData, diff --git a/compiler/rustc_middle/src/query/erase.rs b/compiler/rustc_middle/src/query/erase.rs index bea2191c5601c..da8fdd2007d07 100644 --- a/compiler/rustc_middle/src/query/erase.rs +++ b/compiler/rustc_middle/src/query/erase.rs @@ -323,7 +323,6 @@ trivial! { rustc_middle::ty::AsyncDestructor, rustc_middle::ty::BoundVariableKind, rustc_middle::ty::AnonConstKind, - rustc_middle::ty::DeducedParamAttrs, rustc_middle::ty::Destructor, rustc_middle::ty::fast_reject::SimplifiedType, rustc_middle::ty::ImplPolarity, diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index 8fba218cc71cd..b90aa97735406 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -2656,11 +2656,6 @@ rustc_queries! { return_result_from_ensure_ok } - query deduced_param_attrs(def_id: DefId) -> &'tcx [ty::DeducedParamAttrs] { - desc { |tcx| "deducing parameter attributes for {}", tcx.def_path_str(def_id) } - separate_provide_extern - } - query doc_link_resolutions(def_id: DefId) -> &'tcx DocLinkResMap { eval_always desc { "resolutions for documentation links for a module" } diff --git a/compiler/rustc_middle/src/query/on_disk_cache.rs b/compiler/rustc_middle/src/query/on_disk_cache.rs index 546791135ba61..64848dcdc30e6 100644 --- a/compiler/rustc_middle/src/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/query/on_disk_cache.rs @@ -799,7 +799,6 @@ impl_ref_decoder! {<'tcx> rustc_span::def_id::DefId, rustc_span::def_id::LocalDefId, (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo), - ty::DeducedParamAttrs, } //- ENCODING ------------------------------------------------------------------- diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 3f37595d0eef0..4e8be7e193a7b 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -577,7 +577,6 @@ impl_arena_copy_decoder! {<'tcx> rustc_span::def_id::DefId, rustc_span::def_id::LocalDefId, (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo), - ty::DeducedParamAttrs, } #[macro_export] diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index de6fa4c344e69..5f15e04599250 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -40,7 +40,6 @@ use rustc_hir::intravisit::VisitorExt; use rustc_hir::lang_items::LangItem; use rustc_hir::{self as hir, Attribute, HirId, Node, TraitCandidate, find_attr}; use rustc_index::IndexVec; -use rustc_macros::{HashStable, TyDecodable, TyEncodable}; use rustc_query_system::cache::WithDepNode; use rustc_query_system::dep_graph::DepNodeIndex; use rustc_query_system::ich::StableHashingContext; @@ -3521,21 +3520,6 @@ impl<'tcx> TyCtxt<'tcx> { } } -/// Parameter attributes that can only be determined by examining the body of a function instead -/// of just its signature. -/// -/// These can be useful for optimization purposes when a function is directly called. We compute -/// them and store them into the crate metadata so that downstream crates can make use of them. -/// -/// Right now, we only have `read_only`, but `no_capture` and `no_alias` might be useful in the -/// future. -#[derive(Clone, Copy, PartialEq, Debug, Default, TyDecodable, TyEncodable, HashStable)] -pub struct DeducedParamAttrs { - /// The parameter is marked immutable in the function and contains no `UnsafeCell` (i.e. its - /// type is freeze). - pub read_only: bool, -} - pub fn provide(providers: &mut Providers) { providers.is_panic_runtime = |tcx, LocalCrate| contains_name(tcx.hir_krate_attrs(), sym::panic_runtime); diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index da17ec1f9f379..a4bc7c0d30dfc 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -77,8 +77,7 @@ pub use self::consts::{ ExprKind, ScalarInt, UnevaluatedConst, ValTree, ValTreeKind, Value, }; pub use self::context::{ - CtxtInterners, CurrentGcx, DeducedParamAttrs, Feed, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt, - TyCtxtFeed, tls, + CtxtInterners, CurrentGcx, Feed, FreeRegionInfo, GlobalCtxt, Lift, TyCtxt, TyCtxtFeed, tls, }; pub use self::fold::*; pub use self::instance::{Instance, InstanceKind, ReifyReason, UnusedGenericParams}; diff --git a/compiler/rustc_mir_transform/src/deduce_param_attrs.rs b/compiler/rustc_mir_transform/src/deduce_param_attrs.rs deleted file mode 100644 index a0db8bdb7ed88..0000000000000 --- a/compiler/rustc_mir_transform/src/deduce_param_attrs.rs +++ /dev/null @@ -1,196 +0,0 @@ -//! Deduces supplementary parameter attributes from MIR. -//! -//! Deduced parameter attributes are those that can only be soundly determined by examining the -//! body of the function instead of just the signature. These can be useful for optimization -//! purposes on a best-effort basis. We compute them here and store them into the crate metadata so -//! dependent crates can use them. - -use rustc_hir::def_id::LocalDefId; -use rustc_index::bit_set::DenseBitSet; -use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor}; -use rustc_middle::mir::{Body, Location, Operand, Place, RETURN_PLACE, Terminator, TerminatorKind}; -use rustc_middle::ty::{self, DeducedParamAttrs, Ty, TyCtxt}; -use rustc_session::config::OptLevel; - -/// A visitor that determines which arguments have been mutated. We can't use the mutability field -/// on LocalDecl for this because it has no meaning post-optimization. -struct DeduceReadOnly { - /// Each bit is indexed by argument number, starting at zero (so 0 corresponds to local decl - /// 1). The bit is true if the argument may have been mutated or false if we know it hasn't - /// been up to the point we're at. - mutable_args: DenseBitSet, -} - -impl DeduceReadOnly { - /// Returns a new DeduceReadOnly instance. - fn new(arg_count: usize) -> Self { - Self { mutable_args: DenseBitSet::new_empty(arg_count) } - } -} - -impl<'tcx> Visitor<'tcx> for DeduceReadOnly { - fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) { - // We're only interested in arguments. - if place.local == RETURN_PLACE || place.local.index() > self.mutable_args.domain_size() { - return; - } - - let mark_as_mutable = match context { - PlaceContext::MutatingUse(..) => { - // This is a mutation, so mark it as such. - true - } - PlaceContext::NonMutatingUse(NonMutatingUseContext::RawBorrow) => { - // Whether mutating though a `&raw const` is allowed is still undecided, so we - // disable any sketchy `readonly` optimizations for now. But we only need to do - // this if the pointer would point into the argument. IOW: for indirect places, - // like `&raw (*local).field`, this surely cannot mutate `local`. - !place.is_indirect() - } - PlaceContext::NonMutatingUse(..) | PlaceContext::NonUse(..) => { - // Not mutating, so it's fine. - false - } - }; - - if mark_as_mutable { - self.mutable_args.insert(place.local.index() - 1); - } - } - - fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) { - // OK, this is subtle. Suppose that we're trying to deduce whether `x` in `f` is read-only - // and we have the following: - // - // fn f(x: BigStruct) { g(x) } - // fn g(mut y: BigStruct) { y.foo = 1 } - // - // If, at the generated MIR level, `f` turned into something like: - // - // fn f(_1: BigStruct) -> () { - // let mut _0: (); - // bb0: { - // _0 = g(move _1) -> bb1; - // } - // ... - // } - // - // then it would be incorrect to mark `x` (i.e. `_1`) as `readonly`, because `g`'s write to - // its copy of the indirect parameter would actually be a write directly to the pointer that - // `f` passes. Note that function arguments are the only situation in which this problem can - // arise: every other use of `move` in MIR doesn't actually write to the value it moves - // from. - if let TerminatorKind::Call { ref args, .. } = terminator.kind { - for arg in args { - if let Operand::Move(place) = arg.node { - let local = place.local; - if place.is_indirect() - || local == RETURN_PLACE - || local.index() > self.mutable_args.domain_size() - { - continue; - } - - self.mutable_args.insert(local.index() - 1); - } - } - }; - - self.super_terminator(terminator, location); - } -} - -/// Returns true if values of a given type will never be passed indirectly, regardless of ABI. -fn type_will_always_be_passed_directly(ty: Ty<'_>) -> bool { - matches!( - ty.kind(), - ty::Bool - | ty::Char - | ty::Float(..) - | ty::Int(..) - | ty::RawPtr(..) - | ty::Ref(..) - | ty::Slice(..) - | ty::Uint(..) - ) -} - -/// Returns the deduced parameter attributes for a function. -/// -/// Deduced parameter attributes are those that can only be soundly determined by examining the -/// body of the function instead of just the signature. These can be useful for optimization -/// purposes on a best-effort basis. We compute them here and store them into the crate metadata so -/// dependent crates can use them. -pub(super) fn deduced_param_attrs<'tcx>( - tcx: TyCtxt<'tcx>, - def_id: LocalDefId, -) -> &'tcx [DeducedParamAttrs] { - // This computation is unfortunately rather expensive, so don't do it unless we're optimizing. - // Also skip it in incremental mode. - if tcx.sess.opts.optimize == OptLevel::No || tcx.sess.opts.incremental.is_some() { - return &[]; - } - - // If the Freeze lang item isn't present, then don't bother. - if tcx.lang_items().freeze_trait().is_none() { - return &[]; - } - - // Codegen won't use this information for anything if all the function parameters are passed - // directly. Detect that and bail, for compilation speed. - let fn_ty = tcx.type_of(def_id).instantiate_identity(); - if matches!(fn_ty.kind(), ty::FnDef(..)) - && fn_ty - .fn_sig(tcx) - .inputs() - .skip_binder() - .iter() - .cloned() - .all(type_will_always_be_passed_directly) - { - return &[]; - } - - // Don't deduce any attributes for functions that have no MIR. - if !tcx.is_mir_available(def_id) { - return &[]; - } - - // Grab the optimized MIR. Analyze it to determine which arguments have been mutated. - let body: &Body<'tcx> = tcx.optimized_mir(def_id); - let mut deduce_read_only = DeduceReadOnly::new(body.arg_count); - deduce_read_only.visit_body(body); - - // Set the `readonly` attribute for every argument that we concluded is immutable and that - // contains no UnsafeCells. - // - // FIXME: This is overly conservative around generic parameters: `is_freeze()` will always - // return false for them. For a description of alternatives that could do a better job here, - // see [1]. - // - // [1]: https://github.com/rust-lang/rust/pull/103172#discussion_r999139997 - let typing_env = body.typing_env(tcx); - let mut deduced_param_attrs = tcx.arena.alloc_from_iter( - body.local_decls.iter().skip(1).take(body.arg_count).enumerate().map( - |(arg_index, local_decl)| DeducedParamAttrs { - read_only: !deduce_read_only.mutable_args.contains(arg_index) - // We must normalize here to reveal opaques and normalize - // their generic parameters, otherwise we'll see exponential - // blow-up in compile times: #113372 - && tcx - .normalize_erasing_regions(typing_env, local_decl.ty) - .is_freeze(tcx, typing_env), - }, - ), - ); - - // Trailing parameters past the size of the `deduced_param_attrs` array are assumed to have the - // default set of attributes, so we don't have to store them explicitly. Pop them off to save a - // few bytes in metadata. - while deduced_param_attrs.last() == Some(&DeducedParamAttrs::default()) { - let last_index = deduced_param_attrs.len() - 1; - deduced_param_attrs = &mut deduced_param_attrs[0..last_index]; - } - - deduced_param_attrs -} diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 1663dfa744f42..16ebf5e4c1581 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -45,7 +45,6 @@ use pass_manager::{self as pm, Lint, MirLint, MirPass, WithMinOptLevel}; mod check_pointers; mod cost_checker; mod cross_crate_inline; -mod deduce_param_attrs; mod elaborate_drop; mod errors; mod ffi_unwind_calls; @@ -221,7 +220,6 @@ pub fn provide(providers: &mut Providers) { mir_callgraph_cyclic: inline::cycle::mir_callgraph_cyclic, mir_inliner_callees: inline::cycle::mir_inliner_callees, promoted_mir, - deduced_param_attrs: deduce_param_attrs::deduced_param_attrs, coroutine_by_move_body_def_id: coroutine::coroutine_by_move_body_def_id, ..providers.queries }; diff --git a/compiler/rustc_ty_utils/src/abi.rs b/compiler/rustc_ty_utils/src/abi.rs index e4ed084b073b6..3bad5d91e34e1 100644 --- a/compiler/rustc_ty_utils/src/abi.rs +++ b/compiler/rustc_ty_utils/src/abi.rs @@ -10,7 +10,6 @@ use rustc_middle::ty::layout::{ FnAbiError, HasTyCtxt, HasTypingEnv, LayoutCx, LayoutOf, TyAndLayout, fn_can_unwind, }; use rustc_middle::ty::{self, InstanceKind, Ty, TyCtxt}; -use rustc_session::config::OptLevel; use rustc_span::DUMMY_SP; use rustc_span::def_id::DefId; use rustc_target::callconv::{ @@ -610,43 +609,8 @@ fn fn_abi_adjust_for_abi<'tcx>( return; } - let tcx = cx.tcx(); - if abi.is_rustic_abi() { fn_abi.adjust_for_rust_abi(cx); - - // Look up the deduced parameter attributes for this function, if we have its def ID and - // we're optimizing in non-incremental mode. We'll tag its parameters with those attributes - // as appropriate. - let deduced_param_attrs = - if tcx.sess.opts.optimize != OptLevel::No && tcx.sess.opts.incremental.is_none() { - fn_def_id.map(|fn_def_id| tcx.deduced_param_attrs(fn_def_id)).unwrap_or_default() - } else { - &[] - }; - - for (arg_idx, arg) in fn_abi.args.iter_mut().enumerate() { - if arg.is_ignore() { - continue; - } - - // If we deduced that this parameter was read-only, add that to the attribute list now. - // - // The `readonly` parameter only applies to pointers, so we can only do this if the - // argument was passed indirectly. (If the argument is passed directly, it's an SSA - // value, so it's implicitly immutable.) - if let &mut PassMode::Indirect { ref mut attrs, .. } = &mut arg.mode { - // The `deduced_param_attrs` list could be empty if this is a type of function - // we can't deduce any parameters for, so make sure the argument index is in - // bounds. - if let Some(deduced_param_attrs) = deduced_param_attrs.get(arg_idx) - && deduced_param_attrs.read_only - { - attrs.regular.insert(ArgAttribute::ReadOnly); - debug!("added deduced read-only attribute"); - } - } - } } else { fn_abi.adjust_for_foreign_abi(cx, abi); } diff --git a/tests/codegen-llvm/addr-of-mutate.rs b/tests/codegen-llvm/addr-of-mutate.rs index 36d6bf555d156..484eab778f0fd 100644 --- a/tests/codegen-llvm/addr-of-mutate.rs +++ b/tests/codegen-llvm/addr-of-mutate.rs @@ -24,7 +24,7 @@ pub unsafe fn second(a_ptr_and_b: (*mut (i32, bool), (i64, bool))) -> bool { } // If going through a deref (and there are no other mutating accesses), then `readonly` is fine. -// CHECK: i1 @third(ptr{{( dead_on_return)?}} noalias noundef readonly align {{[0-9]+}}{{( captures\(address\))?}} dereferenceable({{[0-9]+}}) %a_ptr_and_b) +// CHECK: i1 @third(ptr{{( dead_on_return)?}} noalias noundef align {{[0-9]+}}{{( captures\(address\))?}} dereferenceable({{[0-9]+}}) %a_ptr_and_b) #[no_mangle] pub unsafe fn third(a_ptr_and_b: (*mut (i32, bool), (i64, bool))) -> bool { let b_bool_ptr = core::ptr::addr_of!((*a_ptr_and_b.0).1).cast_mut(); diff --git a/tests/codegen-llvm/deduced-param-attrs.rs b/tests/codegen-llvm/deduced-param-attrs.rs index 34504c80fad1b..893d9301d367e 100644 --- a/tests/codegen-llvm/deduced-param-attrs.rs +++ b/tests/codegen-llvm/deduced-param-attrs.rs @@ -1,4 +1,4 @@ -//@ compile-flags: -Copt-level=3 +//@ compile-flags: -Copt-level=3 -Cno-prepopulate-passes #![crate_type = "lib"] #![allow(internal_features)] @@ -20,7 +20,7 @@ pub struct BigCellContainer { // The by-value parameter for this big struct can be marked readonly. // -// CHECK: @use_big_struct_immutably({{.*}} readonly {{.*}} %big_struct) +// CHECK: @use_big_struct_immutably({{.*}} {{.*}} %big_struct) #[no_mangle] pub fn use_big_struct_immutably(big_struct: BigStruct) { hint::black_box(&big_struct); diff --git a/tests/codegen-llvm/function-arguments.rs b/tests/codegen-llvm/function-arguments.rs index a0744e44c61ec..0f0c99e995f5f 100644 --- a/tests/codegen-llvm/function-arguments.rs +++ b/tests/codegen-llvm/function-arguments.rs @@ -134,7 +134,7 @@ pub fn mutable_notunpin_borrow(_: &mut NotUnpin) {} #[no_mangle] pub fn notunpin_borrow(_: &NotUnpin) {} -// CHECK: @indirect_struct(ptr{{( dead_on_return)?}} noalias noundef readonly align 4{{( captures\(address\))?}} dereferenceable(32) %_1) +// CHECK: @indirect_struct(ptr{{( dead_on_return)?}} noalias noundef align 4{{( captures\(address\))?}} dereferenceable(32) %_1) #[no_mangle] pub fn indirect_struct(_: S) {}