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
8use rustc_hir::def_id::LocalDefId;
9use rustc_index::bit_set::DenseBitSet;
10use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
11use rustc_middle::mir::{Body, Location, Operand, Place, RETURN_PLACE, Terminator, TerminatorKind};
12use rustc_middle::ty::{self, DeducedParamAttrs, Ty, TyCtxt};
13use rustc_session::config::OptLevel;
14
15/// A visitor that determines which arguments have been mutated. We can't use the mutability field
16/// on LocalDecl for this because it has no meaning post-optimization.
17struct DeduceReadOnly {
18    /// Each bit is indexed by argument number, starting at zero (so 0 corresponds to local decl
19    /// 1). The bit is true if the argument may have been mutated or false if we know it hasn't
20    /// been up to the point we're at.
21    mutable_args: DenseBitSet<usize>,
22}
23
24impl DeduceReadOnly {
25    /// Returns a new DeduceReadOnly instance.
26    fn new(arg_count: usize) -> Self {
27        Self { mutable_args: DenseBitSet::new_empty(arg_count) }
28    }
29}
30
31impl<'tcx> Visitor<'tcx> for DeduceReadOnly {
32    fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
33        // We're only interested in arguments.
34        if place.local == RETURN_PLACE || place.local.index() > self.mutable_args.domain_size() {
35            return;
36        }
37
38        let mark_as_mutable = match context {
39            PlaceContext::MutatingUse(..) => {
40                // This is a mutation, so mark it as such.
41                true
42            }
43            PlaceContext::NonMutatingUse(NonMutatingUseContext::RawBorrow) => {
44                // Whether mutating though a `&raw const` is allowed is still undecided, so we
45                // disable any sketchy `readonly` optimizations for now. But we only need to do
46                // this if the pointer would point into the argument. IOW: for indirect places,
47                // like `&raw (*local).field`, this surely cannot mutate `local`.
48                !place.is_indirect()
49            }
50            PlaceContext::NonMutatingUse(..) | PlaceContext::NonUse(..) => {
51                // Not mutating, so it's fine.
52                false
53            }
54        };
55
56        if mark_as_mutable {
57            self.mutable_args.insert(place.local.index() - 1);
58        }
59    }
60
61    fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
62        // OK, this is subtle. Suppose that we're trying to deduce whether `x` in `f` is read-only
63        // and we have the following:
64        //
65        //     fn f(x: BigStruct) { g(x) }
66        //     fn g(mut y: BigStruct) { y.foo = 1 }
67        //
68        // If, at the generated MIR level, `f` turned into something like:
69        //
70        //      fn f(_1: BigStruct) -> () {
71        //          let mut _0: ();
72        //          bb0: {
73        //              _0 = g(move _1) -> bb1;
74        //          }
75        //          ...
76        //      }
77        //
78        // then it would be incorrect to mark `x` (i.e. `_1`) as `readonly`, because `g`'s write to
79        // its copy of the indirect parameter would actually be a write directly to the pointer that
80        // `f` passes. Note that function arguments are the only situation in which this problem can
81        // arise: every other use of `move` in MIR doesn't actually write to the value it moves
82        // from.
83        //
84        // Anyway, right now this situation doesn't actually arise in practice. Instead, the MIR for
85        // that function looks like this:
86        //
87        //      fn f(_1: BigStruct) -> () {
88        //          let mut _0: ();
89        //          let mut _2: BigStruct;
90        //          bb0: {
91        //              _2 = move _1;
92        //              _0 = g(move _2) -> bb1;
93        //          }
94        //          ...
95        //      }
96        //
97        // Because of that extra move that MIR construction inserts, `x` (i.e. `_1`) can *in
98        // practice* safely be marked `readonly`.
99        //
100        // To handle the possibility that other optimizations (for example, destination propagation)
101        // might someday generate MIR like the first example above, we panic upon seeing an argument
102        // to *our* function that is directly moved into *another* function as an argument. Having
103        // eliminated that problematic case, we can safely treat moves as copies in this analysis.
104        //
105        // In the future, if MIR optimizations cause arguments of a caller to be directly moved into
106        // the argument of a callee, we can just add that argument to `mutated_args` instead of
107        // panicking.
108        //
109        // Note that, because the problematic MIR is never actually generated, we can't add a test
110        // case for this.
111
112        if let TerminatorKind::Call { ref args, .. } = terminator.kind {
113            for arg in args {
114                if let Operand::Move(place) = arg.node {
115                    let local = place.local;
116                    if place.is_indirect()
117                        || local == RETURN_PLACE
118                        || local.index() > self.mutable_args.domain_size()
119                    {
120                        continue;
121                    }
122
123                    self.mutable_args.insert(local.index() - 1);
124                }
125            }
126        };
127
128        self.super_terminator(terminator, location);
129    }
130}
131
132/// Returns true if values of a given type will never be passed indirectly, regardless of ABI.
133fn type_will_always_be_passed_directly(ty: Ty<'_>) -> bool {
134    matches!(
135        ty.kind(),
136        ty::Bool
137            | ty::Char
138            | ty::Float(..)
139            | ty::Int(..)
140            | ty::RawPtr(..)
141            | ty::Ref(..)
142            | ty::Slice(..)
143            | ty::Uint(..)
144    )
145}
146
147/// Returns the deduced parameter attributes for a function.
148///
149/// Deduced parameter attributes are those that can only be soundly determined by examining the
150/// body of the function instead of just the signature. These can be useful for optimization
151/// purposes on a best-effort basis. We compute them here and store them into the crate metadata so
152/// dependent crates can use them.
153pub(super) fn deduced_param_attrs<'tcx>(
154    tcx: TyCtxt<'tcx>,
155    def_id: LocalDefId,
156) -> &'tcx [DeducedParamAttrs] {
157    // This computation is unfortunately rather expensive, so don't do it unless we're optimizing.
158    // Also skip it in incremental mode.
159    if tcx.sess.opts.optimize == OptLevel::No || tcx.sess.opts.incremental.is_some() {
160        return &[];
161    }
162
163    // If the Freeze lang item isn't present, then don't bother.
164    if tcx.lang_items().freeze_trait().is_none() {
165        return &[];
166    }
167
168    // Codegen won't use this information for anything if all the function parameters are passed
169    // directly. Detect that and bail, for compilation speed.
170    let fn_ty = tcx.type_of(def_id).instantiate_identity();
171    if matches!(fn_ty.kind(), ty::FnDef(..))
172        && fn_ty
173            .fn_sig(tcx)
174            .inputs()
175            .skip_binder()
176            .iter()
177            .cloned()
178            .all(type_will_always_be_passed_directly)
179    {
180        return &[];
181    }
182
183    // Don't deduce any attributes for functions that have no MIR.
184    if !tcx.is_mir_available(def_id) {
185        return &[];
186    }
187
188    // Grab the optimized MIR. Analyze it to determine which arguments have been mutated.
189    let body: &Body<'tcx> = tcx.optimized_mir(def_id);
190    let mut deduce_read_only = DeduceReadOnly::new(body.arg_count);
191    deduce_read_only.visit_body(body);
192
193    // Set the `readonly` attribute for every argument that we concluded is immutable and that
194    // contains no UnsafeCells.
195    //
196    // FIXME: This is overly conservative around generic parameters: `is_freeze()` will always
197    // return false for them. For a description of alternatives that could do a better job here,
198    // see [1].
199    //
200    // [1]: https://github.com/rust-lang/rust/pull/103172#discussion_r999139997
201    let typing_env = body.typing_env(tcx);
202    let mut deduced_param_attrs = tcx.arena.alloc_from_iter(
203        body.local_decls.iter().skip(1).take(body.arg_count).enumerate().map(
204            |(arg_index, local_decl)| DeducedParamAttrs {
205                read_only: !deduce_read_only.mutable_args.contains(arg_index)
206                    // We must normalize here to reveal opaques and normalize
207                    // their generic parameters, otherwise we'll see exponential
208                    // blow-up in compile times: #113372
209                    && tcx
210                        .normalize_erasing_regions(typing_env, local_decl.ty)
211                        .is_freeze(tcx, typing_env),
212            },
213        ),
214    );
215
216    // Trailing parameters past the size of the `deduced_param_attrs` array are assumed to have the
217    // default set of attributes, so we don't have to store them explicitly. Pop them off to save a
218    // few bytes in metadata.
219    while deduced_param_attrs.last() == Some(&DeducedParamAttrs::default()) {
220        let last_index = deduced_param_attrs.len() - 1;
221        deduced_param_attrs = &mut deduced_param_attrs[0..last_index];
222    }
223
224    deduced_param_attrs
225}