about summary refs log tree commit diff
path: root/compiler/rustc_mir_transform/src
AgeCommit message (Collapse)AuthorLines
2023-01-30Allow more deriving on packed structs.Nicholas Nethercote-40/+6
Currently, deriving on packed structs has some non-trivial limitations, related to the fact that taking references on unaligned fields is UB. The current approach to field accesses in derived code: - Normal case: `&self.0` - In a packed struct that derives `Copy`: `&{self.0}` - In a packed struct that doesn't derive `Copy`: `&self.0` Plus, we disallow deriving any builtin traits other than `Default` for any packed generic type, because it's possible that there might be misaligned fields. This is a fairly broad restriction. Plus, we disallow deriving any builtin traits other than `Default` for most packed types that don't derive `Copy`. (The exceptions are those where the alignments inherently satisfy the packing, e.g. in a type with `repr(packed(N))` where all the fields have alignments of `N` or less anyway. Such types are pretty strange, because the `packed` attribute is not having any effect.) This commit introduces a new, simpler approach to field accesses: - Normal case: `&self.0` - In a packed struct: `&{self.0}` In the latter case, this requires that all fields impl `Copy`, which is a new restriction. This means that the following example compiles under the old approach and doesn't compile under the new approach. ``` #[derive(Debug)] struct NonCopy(u8); #[derive(Debug) #[repr(packed)] struct MyType(NonCopy); ``` (Note that the old approach's support for cases like this was brittle. Changing the `u8` to a `u16` would be enough to stop it working. So not much capability is lost here.) However, the other constraints from the old rules are removed. We can now derive builtin traits for packed generic structs like this: ``` trait Trait { type A; } #[derive(Hash)] #[repr(packed)] pub struct Foo<T: Trait>(T, T::A); ``` To allow this, we add a `T: Copy` bound in the derived impl and a `T::A: Copy` bound in where clauses. So `T` and `T::A` must impl `Copy`. We can now also derive builtin traits for packed structs that don't derive `Copy`, so long as the fields impl `Copy`: ``` #[derive(Hash)] #[repr(packed)] pub struct Foo(u32); ``` This includes types that hand-impl `Copy` rather than deriving it, such as the following, that show up in winapi-0.2: ``` #[derive(Clone)] #[repr(packed)] struct MyType(i32); impl Copy for MyType {} ``` The new approach is simpler to understand and implement, and it avoids the need for the `unsafe_derive_on_repr_packed` check. One exception is required for backwards-compatibility: we allow `[u8]` fields for now. There is a new lint for this, `byte_slice_in_packed_struct_with_derive`.
2023-01-30Treat Drop as a rmw operationGiacomo Pasini-0/+29
Previously, a Drop terminator was considered a move in MIR. This commit changes the behavior to only treat Drop as a mutable access to the dropped place. In order for this change to be correct, we need to guarantee that a) A dropped value won't be used again b) Places that appear in a drop won't be used again before a subsequent initialization. We can ensure this to be correct at MIR construction because Drop will only be emitted when a variable goes out of scope, thus having: (a) as there is no way of reaching the old value. drop-elaboration will also remove any uninitialized drop. (b) as the place can't be named following the end of the scope. However, the initialization status, previously tracked by moves, should also be tied to the execution of a Drop, hence the additional logic in the dataflow analyses.
2023-01-29Remove obsolete comment.Camille GILLOT-1/+0
2023-01-29Reimplement NormalizeArrayLen.Camille GILLOT-253/+70
2023-01-29Test drop_tracking_mir before querying generator.Camille GILLOT-1/+4
2023-01-29Auto merge of #106908 - cjgillot:copyprop-ssa, r=oli-obkbors-4/+420
Implement simple CopyPropagation based on SSA analysis This PR extracts the "copy propagation" logic from https://github.com/rust-lang/rust/pull/106285. MIR may produce chains of assignment between locals, like `_x = move? _y`. This PR attempts to remove such chains by unifying locals. The current implementation is a bit overzealous in turning moves into copies, and in removing storage statements.
2023-01-29Auto merge of #106227 - bryangarza:ctfe-limit, r=oli-obkbors-1/+72
Use stable metric for const eval limit instead of current terminator-based logic This patch adds a `MirPass` that inserts a new MIR instruction `ConstEvalCounter` to any loops and function calls in the CFG. This instruction is used during Const Eval to count against the `const_eval_limit`, and emit the `StepLimitReached` error, replacing the current logic which uses Terminators only. The new method of counting loops and function calls should be more stable across compiler versions (i.e., not cause crates that compiled successfully before, to no longer compile when changes to the MIR generation/optimization are made). Also see: #103877
2023-01-29Auto merge of #107406 - cjgillot:eliminate-witnesses, r=compiler-errorsbors-17/+18
Only compute mir_generator_witnesses query in drop_tracking_mir mode. Attempt to fix the perf regression in https://github.com/rust-lang/rust/pull/101692 r? `@ghost`
2023-01-28Remove `HirId -> LocalDefId` map from HIR.Camille GILLOT-1/+2
2023-01-28Take a LocalDefId in hir::Visitor::visit_fn.Camille GILLOT-1/+1
2023-01-28Only compute mir_generator_witnesses query in drop_tracking_mir mode.Camille GILLOT-17/+18
2023-01-27Restrict amount of ignored locals.Camille GILLOT-9/+28
2023-01-27Separate witness type computation from the generator transform.Camille GILLOT-28/+286
2023-01-27Remember where a type was kept in MIR.Camille GILLOT-4/+13
2023-01-27Use successor location for dominator check.Camille GILLOT-1/+1
The assignment is complete only after the statement. This marks self-assignments `x = x + 1` as non-sSA.
2023-01-27Do not merge locals that have their address taken.Camille GILLOT-17/+65
2023-01-27Extract SsaLocals abstraction.Camille GILLOT-175/+240
2023-01-27Pacify tidy.Camille GILLOT-3/+3
2023-01-27Discard raw pointers from SSA locals.Camille GILLOT-2/+4
2023-01-27Only consider a local to be SSA if assignment dominates all uses.Camille GILLOT-2/+21
2023-01-27Implement SSA CopyProp pass.Camille GILLOT-0/+269
2023-01-27Allow to remove unused definitions without renumbering locals.Camille GILLOT-2/+14
2023-01-27Consider `CopyForDeref` for DestProp.Camille GILLOT-2/+3
2023-01-26fix up subst_identity vs skip_binder; add some FIXMEs as identified in reviewKyle Matsuda-1/+2
2023-01-26change fn_sig query to use EarlyBinder; remove bound_fn_sig query; add ↵Kyle Matsuda-8/+7
EarlyBinder to fn_sig in metadata
2023-01-26replace usages of fn_sig query with bound_fn_sigKyle Matsuda-3/+8
2023-01-26Disable ConstGoto opt in cleanup blocksJakob Degen-0/+9
2023-01-26Auto merge of #105582 - saethlin:instcombine-assert-inhabited, r=cjgillotbors-2/+84
InstCombine away intrinsic validity assertions This optimization (currently) fires 246 times on the standard library. It seems to fire hardly at all on the big crates in the benchmark suite. Interesting.
2023-01-24Delete `SimplifyArmIdentity` and `SimplifyBranchSame` mir optsJakob Degen-825/+0
2023-01-23Thread a ParamEnv down to might_permit_raw_initBen Kimock-8/+15
2023-01-24Improve efficiency of has_back_edge(...)Bryan Garza-10/+11
2023-01-23Add comments and remove unnecessary codeBryan Garza-0/+2
2023-01-23Move CtfeLimit MirPass to inner_mir_for_ctfeBryan Garza-1/+2
2023-01-23Revert "Move CtfeLimit to mir_const's set of passes"Bryan Garza-1/+1
This reverts commit 332542a92223b2800ed372d2d461921147f29477.
2023-01-23Move CtfeLimit to mir_const's set of passesBryan Garza-1/+1
2023-01-23Abstract out has_back_edge fnBryan Garza-23/+28
2023-01-23Change code to use map insead of for-loopBryan Garza-23/+25
2023-01-23Remove debugging-related codeBryan Garza-7/+2
2023-01-23Clean up CtfeLimit MirPassBryan Garza-74/+31
2023-01-23Create stable metric to measure long computation in Const EvalBryan Garza-3/+111
This patch adds a `MirPass` that tracks the number of back-edges and function calls in the CFG, adds a new MIR instruction to increment a counter every time they are encountered during Const Eval, and emit a warning if a configured limit is breached.
2023-01-21Consistently use dominates instead of is_dominated_byTomasz Miąsko-16/+16
There is a number of APIs that answer dominance queries. Previously they were named either "dominates" or "is_dominated_by". Consistently use the "dominates" form. No functional changes.
2023-01-19Rollup merge of #107037 - tmiasko:rank, r=oli-obkGuillaume Gomez-5/+5
Fix Dominators::rank_partial_cmp to match documentation The only use site is also updated accordingly and there is no change in end-to-end behaviour.
2023-01-19Transform async ResumeTy in generator transformArpad Borsos-7/+111
- Eliminates all the `get_context` calls that async lowering created. - Replace all `Local` `ResumeTy` types with `&mut Context<'_>`. The `Local`s that have their types replaced are: - The `resume` argument itself. - The argument to `get_context`. - The yielded value of a `yield`. The `ResumeTy` hides a `&mut Context<'_>` behind an unsafe raw pointer, and the `get_context` function is being used to convert that back to a `&mut Context<'_>`. Ideally the async lowering would not use the `ResumeTy`/`get_context` indirection, but rather directly use `&mut Context<'_>`, however that would currently lead to higher-kinded lifetime errors. See <https://github.com/rust-lang/rust/issues/105501>. The async lowering step and the type / lifetime inference / checking are still using the `ResumeTy` indirection for the time being, and that indirection is removed here. After this transform, the generator body only knows about `&mut Context<'_>`.
2023-01-18Fix Dominators::rank_partial_cmp to match documentationTomasz Miąsko-5/+5
The only use site is also updated accordingly and there is no change in end-to-end behaviour.
2023-01-17Rollup merge of #104505 - WaffleLapkin:no-double-spaces-in-comments, r=jackh726Matthias Krüger-2/+2
Remove double spaces after dots in comments Most of the comments do not have double spaces, so I assume these are typos.
2023-01-17Remove double spaces after dots in commentsMaybe Waffle-2/+2
2023-01-16Remove ineffective run of SimplifyConstConditionTomasz Miąsko-1/+0
There are no constant conditions at this stage.
2023-01-15InstCombine away intrinsic validity assertionsBen Kimock-2/+77
2023-01-14Remove visit_place.Camille GILLOT-6/+0
2023-01-14Make the inlining destination a `Local`.Camille GILLOT-18/+28