]>
Commit | Line | Data |
---|---|---|
2b03887a FG |
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 | ||
353b0b11 | 8 | use rustc_hir::def_id::LocalDefId; |
2b03887a FG |
9 | use rustc_index::bit_set::BitSet; |
10 | use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor}; | |
49aad941 FG |
11 | use rustc_middle::mir::{Body, Location, Operand, Place, Terminator, TerminatorKind, RETURN_PLACE}; |
12 | use rustc_middle::ty::{self, DeducedParamAttrs, Ty, TyCtxt}; | |
2b03887a FG |
13 | use 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. | |
17 | struct 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: BitSet<usize>, | |
22 | } | |
23 | ||
24 | impl DeduceReadOnly { | |
25 | /// Returns a new DeduceReadOnly instance. | |
26 | fn new(arg_count: usize) -> Self { | |
27 | Self { mutable_args: BitSet::new_empty(arg_count) } | |
28 | } | |
29 | } | |
30 | ||
31 | impl<'tcx> Visitor<'tcx> for DeduceReadOnly { | |
49aad941 | 32 | fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) { |
2b03887a | 33 | // We're only interested in arguments. |
49aad941 | 34 | if place.local == RETURN_PLACE || place.local.index() > self.mutable_args.domain_size() { |
2b03887a FG |
35 | return; |
36 | } | |
37 | ||
49aad941 FG |
38 | let mark_as_mutable = match context { |
39 | PlaceContext::MutatingUse(..) => { | |
2b03887a | 40 | // This is a mutation, so mark it as such. |
49aad941 FG |
41 | true |
42 | } | |
43 | PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf) => { | |
44 | // Whether mutating though a `&raw const` is allowed is still undecided, so we | |
45 | // disable any sketchy `readonly` optimizations for now. | |
46 | // But we only need to do this if the pointer would point into the argument. | |
ed00b5ec | 47 | // IOW: for indirect places, like `&raw (*local).field`, this surely cannot mutate `local`. |
49aad941 | 48 | !place.is_indirect() |
2b03887a FG |
49 | } |
50 | PlaceContext::NonMutatingUse(..) | PlaceContext::NonUse(..) => { | |
51 | // Not mutating, so it's fine. | |
49aad941 | 52 | false |
2b03887a | 53 | } |
49aad941 FG |
54 | }; |
55 | ||
56 | if mark_as_mutable { | |
57 | self.mutable_args.insert(place.local.index() - 1); | |
2b03887a FG |
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 { | |
c0240ec0 | 114 | if let Operand::Move(place) = arg.node { |
487cf647 FG |
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); | |
2b03887a FG |
124 | } |
125 | } | |
126 | }; | |
127 | ||
128 | self.super_terminator(terminator, location); | |
129 | } | |
130 | } | |
131 | ||
2b03887a | 132 | /// Returns true if values of a given type will never be passed indirectly, regardless of ABI. |
9c376795 | 133 | fn type_will_always_be_passed_directly(ty: Ty<'_>) -> bool { |
2b03887a FG |
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. | |
353b0b11 FG |
153 | pub fn deduced_param_attrs<'tcx>( |
154 | tcx: TyCtxt<'tcx>, | |
155 | def_id: LocalDefId, | |
156 | ) -> &'tcx [DeducedParamAttrs] { | |
2b03887a FG |
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 language 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. | |
add651ee | 170 | let fn_ty = tcx.type_of(def_id).instantiate_identity(); |
2b03887a FG |
171 | if matches!(fn_ty.kind(), ty::FnDef(..)) { |
172 | if 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 | ||
184 | // Don't deduce any attributes for functions that have no MIR. | |
185 | if !tcx.is_mir_available(def_id) { | |
186 | return &[]; | |
187 | } | |
188 | ||
2b03887a FG |
189 | // Grab the optimized MIR. Analyze it to determine which arguments have been mutated. |
190 | let body: &Body<'tcx> = tcx.optimized_mir(def_id); | |
191 | let mut deduce_read_only = DeduceReadOnly::new(body.arg_count); | |
192 | deduce_read_only.visit_body(body); | |
193 | ||
194 | // Set the `readonly` attribute for every argument that we concluded is immutable and that | |
195 | // contains no UnsafeCells. | |
196 | // | |
197 | // FIXME: This is overly conservative around generic parameters: `is_freeze()` will always | |
198 | // return false for them. For a description of alternatives that could do a better job here, | |
199 | // see [1]. | |
200 | // | |
201 | // [1]: https://github.com/rust-lang/rust/pull/103172#discussion_r999139997 | |
49aad941 | 202 | let param_env = tcx.param_env_reveal_all_normalized(def_id); |
2b03887a FG |
203 | let mut deduced_param_attrs = tcx.arena.alloc_from_iter( |
204 | body.local_decls.iter().skip(1).take(body.arg_count).enumerate().map( | |
205 | |(arg_index, local_decl)| DeducedParamAttrs { | |
206 | read_only: !deduce_read_only.mutable_args.contains(arg_index) | |
fe692bf9 | 207 | // We must normalize here to reveal opaques and normalize |
c620b35d FG |
208 | // their generic parameters, otherwise we'll see exponential |
209 | // blow-up in compile times: #113372 | |
fe692bf9 FG |
210 | && tcx |
211 | .normalize_erasing_regions(param_env, local_decl.ty) | |
212 | .is_freeze(tcx, param_env), | |
2b03887a FG |
213 | }, |
214 | ), | |
215 | ); | |
216 | ||
217 | // Trailing parameters past the size of the `deduced_param_attrs` array are assumed to have the | |
218 | // default set of attributes, so we don't have to store them explicitly. Pop them off to save a | |
219 | // few bytes in metadata. | |
220 | while deduced_param_attrs.last() == Some(&DeducedParamAttrs::default()) { | |
221 | let last_index = deduced_param_attrs.len() - 1; | |
222 | deduced_param_attrs = &mut deduced_param_attrs[0..last_index]; | |
223 | } | |
224 | ||
225 | deduced_param_attrs | |
226 | } |