rustc_mir_transform/
deduce_param_attrs.rs

1//! Deduces supplementary parameter attributes from MIR.
2//!
3//! Deduced parameter attributes are those that can only be soundly determined by examining the
4//! body of the function instead of just the signature. These can be useful for optimization
5//! purposes on a best-effort basis. We compute them here and store them into the crate metadata so
6//! dependent crates can use them.
7//!
8//! Note that this *crucially* relies on codegen *not* doing any more MIR-level transformations
9//! after `optimized_mir`! We check for things that are *not* guaranteed to be preserved by MIR
10//! transforms, such as which local variables happen to be mutated.
11
12use rustc_hir::def_id::LocalDefId;
13use rustc_index::IndexVec;
14use rustc_middle::middle::deduced_param_attrs::{DeducedParamAttrs, UsageSummary};
15use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
16use rustc_middle::mir::*;
17use rustc_middle::ty::{self, Ty, TyCtxt};
18use rustc_session::config::OptLevel;
19
20/// A visitor that determines how a return place and arguments are used inside MIR body.
21/// To determine whether a local is mutated we can't use the mutability field on LocalDecl
22/// because it has no meaning post-optimization.
23struct DeduceParamAttrs {
24    /// Summarizes how a return place and arguments are used inside MIR body.
25    usage: IndexVec<Local, UsageSummary>,
26}
27
28impl DeduceParamAttrs {
29    /// Returns a new DeduceParamAttrs instance.
30    fn new(body: &Body<'_>) -> Self {
31        let mut this =
32            Self { usage: IndexVec::from_elem_n(UsageSummary::empty(), body.arg_count + 1) };
33        // Code generation indicates that a return place is writable. To avoid setting both
34        // `readonly` and `writable` attributes, when return place is never written to, mark it as
35        // mutated.
36        this.usage[RETURN_PLACE] |= UsageSummary::MUTATE;
37        this
38    }
39
40    /// Returns whether a local is the return place or an argument and returns its index.
41    fn as_param(&self, local: Local) -> Option<Local> {
42        if local.index() < self.usage.len() { Some(local) } else { None }
43    }
44}
45
46impl<'tcx> Visitor<'tcx> for DeduceParamAttrs {
47    fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
48        // We're only interested in the return place or an argument.
49        let Some(i) = self.as_param(place.local) else { return };
50
51        match context {
52            // Not actually using the local.
53            PlaceContext::NonUse(..) => {}
54            // Neither mutated nor captured.
55            _ if place.is_indirect_first_projection() => {}
56            // This is a `Drop`. It could disappear at monomorphization, so mark it specially.
57            PlaceContext::MutatingUse(MutatingUseContext::Drop)
58                // Projection changes the place's type, so `needs_drop(local.ty)` is not
59                // `needs_drop(place.ty)`.
60                if place.projection.is_empty() => {
61                    self.usage[i] |= UsageSummary::DROP;
62            }
63            PlaceContext::MutatingUse(
64                  MutatingUseContext::Call
65                | MutatingUseContext::Yield
66                | MutatingUseContext::Drop
67                | MutatingUseContext::Borrow
68                | MutatingUseContext::RawBorrow) => {
69                self.usage[i] |= UsageSummary::MUTATE;
70                self.usage[i] |= UsageSummary::CAPTURE;
71            }
72            PlaceContext::MutatingUse(
73                  MutatingUseContext::Store
74                | MutatingUseContext::SetDiscriminant
75                | MutatingUseContext::AsmOutput
76                | MutatingUseContext::Projection
77                | MutatingUseContext::Retag) => {
78                self.usage[i] |= UsageSummary::MUTATE;
79            }
80            | PlaceContext::NonMutatingUse(NonMutatingUseContext::RawBorrow) => {
81                // Whether mutating though a `&raw const` is allowed is still undecided, so we
82                // disable any sketchy `readonly` optimizations for now.
83                self.usage[i] |= UsageSummary::MUTATE;
84                self.usage[i] |= UsageSummary::CAPTURE;
85            }
86            PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow) => {
87                // Not mutating if the parameter is `Freeze`.
88                self.usage[i] |= UsageSummary::SHARED_BORROW;
89                self.usage[i] |= UsageSummary::CAPTURE;
90            }
91            // Not mutating, so it's fine.
92            PlaceContext::NonMutatingUse(
93                  NonMutatingUseContext::Inspect
94                | NonMutatingUseContext::Copy
95                | NonMutatingUseContext::Move
96                | NonMutatingUseContext::FakeBorrow
97                | NonMutatingUseContext::PlaceMention
98                | NonMutatingUseContext::Projection) => {}
99        }
100    }
101
102    fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
103        // OK, this is subtle. Suppose that we're trying to deduce whether `x` in `f` is read-only
104        // and we have the following:
105        //
106        //     fn f(x: BigStruct) { g(x) }
107        //     fn g(mut y: BigStruct) { y.foo = 1 }
108        //
109        // If, at the generated MIR level, `f` turned into something like:
110        //
111        //      fn f(_1: BigStruct) -> () {
112        //          let mut _0: ();
113        //          bb0: {
114        //              _0 = g(move _1) -> bb1;
115        //          }
116        //          ...
117        //      }
118        //
119        // then it would be incorrect to mark `x` (i.e. `_1`) as `readonly`, because `g`'s write to
120        // its copy of the indirect parameter would actually be a write directly to the pointer that
121        // `f` passes. Note that function arguments are the only situation in which this problem can
122        // arise: every other use of `move` in MIR doesn't actually write to the value it moves
123        // from.
124        if let TerminatorKind::Call { ref args, .. } = terminator.kind {
125            for arg in args {
126                if let Operand::Move(place) = arg.node
127                    && !place.is_indirect_first_projection()
128                    && let Some(i) = self.as_param(place.local)
129                {
130                    self.usage[i] |= UsageSummary::MUTATE;
131                    self.usage[i] |= UsageSummary::CAPTURE;
132                }
133            }
134        };
135
136        self.super_terminator(terminator, location);
137    }
138}
139
140/// Returns true if values of a given type will never be passed indirectly, regardless of ABI.
141fn type_will_always_be_passed_directly(ty: Ty<'_>) -> bool {
142    matches!(
143        ty.kind(),
144        ty::Bool
145            | ty::Char
146            | ty::Float(..)
147            | ty::Int(..)
148            | ty::RawPtr(..)
149            | ty::Ref(..)
150            | ty::Slice(..)
151            | ty::Uint(..)
152    )
153}
154
155/// Returns the deduced parameter attributes for a function.
156///
157/// Deduced parameter attributes are those that can only be soundly determined by examining the
158/// body of the function instead of just the signature. These can be useful for optimization
159/// purposes on a best-effort basis. We compute them here and store them into the crate metadata so
160/// dependent crates can use them.
161#[tracing::instrument(level = "trace", skip(tcx), ret)]
162pub(super) fn deduced_param_attrs<'tcx>(
163    tcx: TyCtxt<'tcx>,
164    def_id: LocalDefId,
165) -> &'tcx [DeducedParamAttrs] {
166    // This computation is unfortunately rather expensive, so don't do it unless we're optimizing.
167    // Also skip it in incremental mode.
168    if tcx.sess.opts.optimize == OptLevel::No || tcx.sess.opts.incremental.is_some() {
169        return &[];
170    }
171
172    // If the Freeze lang item isn't present, then don't bother.
173    if tcx.lang_items().freeze_trait().is_none() {
174        return &[];
175    }
176
177    // Codegen won't use this information for anything if all the function parameters are passed
178    // directly. Detect that and bail, for compilation speed.
179    let fn_ty = tcx.type_of(def_id).instantiate_identity();
180    if matches!(fn_ty.kind(), ty::FnDef(..))
181        && fn_ty
182            .fn_sig(tcx)
183            .inputs_and_output()
184            .skip_binder()
185            .iter()
186            .all(type_will_always_be_passed_directly)
187    {
188        return &[];
189    }
190
191    // Don't deduce any attributes for functions that have no MIR.
192    if !tcx.is_mir_available(def_id) {
193        return &[];
194    }
195
196    // Grab the optimized MIR. Analyze it to determine which arguments have been mutated.
197    let body: &Body<'tcx> = tcx.optimized_mir(def_id);
198    // Arguments spread at ABI level are currently unsupported.
199    if body.spread_arg.is_some() {
200        return &[];
201    }
202
203    let mut deduce = DeduceParamAttrs::new(body);
204    deduce.visit_body(body);
205    tracing::trace!(?deduce.usage);
206
207    let mut deduced_param_attrs: &[_] = tcx
208        .arena
209        .alloc_from_iter(deduce.usage.into_iter().map(|usage| DeducedParamAttrs { usage }));
210
211    // Trailing parameters past the size of the `deduced_param_attrs` array are assumed to have the
212    // default set of attributes, so we don't have to store them explicitly. Pop them off to save a
213    // few bytes in metadata.
214    while let Some((last, rest)) = deduced_param_attrs.split_last()
215        && last.is_default()
216    {
217        deduced_param_attrs = rest;
218    }
219
220    deduced_param_attrs
221}