about summary refs log tree commit diff
path: root/compiler/rustc_mir_transform/src
AgeCommit message (Collapse)AuthorLines
2025-10-02mir-opt: Eliminate dead statements even if they are used by debuginfosdianqk-0/+33
2025-10-02mir-opt: Eliminate trivial unnecessary storage annotationsdianqk-44/+98
2025-10-02simplifycfg: Preserve debuginfos when merging bbsdianqk-3/+27
2025-10-02mir-opt: Eliminate dead ref statementsdianqk-63/+65
2025-10-02Auto merge of #147055 - beepster4096:subtype_is_not_a_projection, r=lcnrbors-23/+12
Turn ProjectionElem::Subtype into CastKind::Subtype I noticed that drop elaboration can't, in general, handle `ProjectionElem::SubType`. It creates a disjoint move path that overlaps with other move paths. (`Subslice` does too, and I'm working on a different PR to make that special case less fragile.) If its skipped and treated as the same move path as its parent then `MovePath.place` has multiple possible projections. (It would probably make sense to remove all `Subtype` projections for the canonical place but it doesn't make sense to have this special case for a problem that doesn't actually occur in real MIR.) The only reason this doesn't break is that `Subtype` is always the sole projection of the local its applied to. For the same reason, it works fine as a `CastKind` so I figured that makes more sense than documenting and validating this hidden invariant. cc rust-lang/rust#112651, rust-lang/rust#133258 r? Icnr (bc you've been the main person dealing with `Subtype` it looks like)
2025-09-29Rollup merge of #146457 - alexcrichton:wasm-no-exn-instructions, r=bjorn3Jacob Pratt-8/+29
Skip cleanups on unsupported targets This commit is an update to the `AbortUnwindingCalls` MIR pass in the compiler. Specifically a new boolean is added for "can this target possibly unwind" and if that's `false` then terminators are all adjusted to be unreachable/not present. The end result is that this fixes rust-lang/rust#140293 for wasm targets. The motivation for this PR is that currently on WebAssembly targets the usage of the `C-unwind` ABI can lead LLVM to either (a) emit exception-handling instructions or (b) hit a LLVM-ICE-style codegen error. WebAssembly as a base instruction set does not support unwinding at all, and a later proposal to WebAssembly, the exception-handling proposal, was what enabled this. This means that the current intent of WebAssembly targets is that they maintain the baseline of "don't emit exception-handling instructions unless enabled". The commit here is intended to restore this behavior by skipping these instructions even when `C-unwind` is present. Exception-handling is a relatively tricky and also murky topic in WebAssembly, however. There are two sets of instructions LLVM can emit for WebAssembly exceptions, Rust's Emscripten target supports exceptions, WASI targets do not, the LLVM flags to enable this are not always obvious, and additionally this all touches on "changing exception-handling behavior should be a target-level concern, not a feature". Effectively WebAssembly's exception-handling integration into Rust is not finalized at this time. The best idea at this time is that a parallel set of targets will eventually be added which support exceptions, but it's not clear if/when to do this. In the meantime the goal is to keep existing targets working while still enabling experimentation with exception-handling with `-Zbuild-std` and various permutations of LLVM flags. To that extent this commit does not blanket disable these landing pads and cleanup routines for WebAssembly but instead checks to see if panic=unwind is enabled or if `+exception-handling` is enabled. Tests are updated here as well to account for this where, by default, using a `C-unwind` ABI won't affect Rust codegen at all. If `+exception-handling` is enabled, however, then Rust codegen will look like native platforms where exceptions are caught and the program aborts. More-or-less I've done my best to keep exceptions working on wasm where it's possible to have them work, but turned them off where they're not supposed to be emitted. Closes rust-lang/rust#140293
2025-09-29Rollup merge of #147131 - cjgillot:patch-branches, r=davidtwcoMatthias Krüger-12/+37
Use MirPatch in simplify_branches. This allows to avoid clearing the CFG cache if we don't perform any change. r? ``@ghost`` for perf
2025-09-28remove explicit deref of AbiAlign for most methodsJubilee Young-3/+3
Much of the compiler calls functions on Align projected from AbiAlign. AbiAlign impls Deref to its inner Align, so we can simplify these away. Also, it will minimize disruption when AbiAlign is removed. For now, preserve usages that might resolve to PartialOrd or PartialEq, as those have odd inference.
2025-09-28Use MirPatch in simplify_branches.Camille Gillot-12/+37
2025-09-26Handle self-loops too.Camille Gillot-3/+5
2025-09-26ProjectionElem::Subtype -> CastKind::Subtypebeepster4096-23/+12
2025-09-26JumpThreading: Avoid computing dominators to identify loop headers.Camille Gillot-17/+23
2025-09-25Auto merge of #146697 - cjgillot:invalidate-patch, r=lcnrbors-15/+21
Avoid invalidating CFG caches from MirPatch::apply. Small effort to reduce invalidating CFG caches.
2025-09-23Update compiler/rustc_mir_transform/src/patch.rsCamille Gillot-0/+2
Co-authored-by: lcnr <rust@lcnr.de>
2025-09-23Auto merge of #146317 - saethlin:panic=immediate-abort, r=nnethercotebors-7/+8
Add panic=immediate-abort MCP: https://github.com/rust-lang/compiler-team/issues/909 This adds a new panic strategy, `-Cpanic=immediate-abort`. This panic strategy essentially just codifies use of `-Zbuild-std-features=panic_immediate_abort`. This PR is intended to just set up infrastructure, and while it will change how the compiler is invoked for users of the feature, there should be no other impacts. In many parts of the compiler, `PanicStrategy::ImmediateAbort` behaves just like `PanicStrategy::Abort`, because actually most parts of the compiler just mean to ask "can this unwind?" so I've added a helper function so we can say `sess.panic_strategy().unwinds()`. The panic and unwind strategies have some level of compatibility, which mostly means that we can pre-compile the sysroot with unwinding panics then the sysroot can be linked with aborting panics later. The immediate-abort strategy is all-or-nothing, enforced by `compiler/rustc_metadata/src/dependency_format.rs` and this is tested for in `tests/ui/panic-runtime/`. We could _technically_ be more compatible with the other panic strategies, but immediately-aborting panics primarily exist for users who want to eliminate all the code size responsible for the panic runtime. I'm open to other use cases if people want to present them, but not right now. This PR is already large. `-Cpanic=immediate-abort` sets both `cfg(panic = "immediate-abort")` _and_ `cfg(panic = "abort")`. bjorn3 pointed out that people may be checking for the abort cfg to ask if panics will unwind, and also the sysroot feature this is replacing used to require `-Cpanic=abort` so this seems like a good back-compat step. At least for the moment. Unclear if this is a good idea indefinitely. I can imagine this being confusing. The changes to the standard library attributes are purely mechanical. Apart from that, I removed an `unsafe` we haven't needed for a while since the `abort` intrinsic became safe, and I've added a helpful diagnostic for people trying to use the old feature. To test that `-Cpanic=immediate-abort` conflicts with other panic strategies, I've beefed up the core-stubs infrastructure a bit. There is now a separate attribute to set flags on it. I've added a test that this produces the desired codegen, called `tests/run-make-cargo/panic-immediate-abort-codegen/` and also a separate run-make-cargo test that checks that we can build a binary.
2025-09-21Add panic=immediate-abortBen Kimock-7/+8
2025-09-21Auto merge of #146659 - cjgillot:impossible-taint, r=oli-obkbors-13/+20
Consider errors in MIR as impossible predicates to empty the body. The ICEs come from elaborating drops or performing state transform in MIR bodies that fail typeck or borrowck. If the body is tainted, replace it with `unreachable`. Fixes https://github.com/rust-lang/rust/issues/122630 Fixes https://github.com/rust-lang/rust/issues/122904 Fixes https://github.com/rust-lang/rust/issues/125185 Fixes https://github.com/rust-lang/rust/issues/139556
2025-09-21Rollup merge of #146744 - beepster4096:derefs_in_ref_prop, r=cjgillotStuart Cook-20/+13
Deref related cleanups in ref_prop Cherry picked from rust-lang/rust#146710 r? cjgillot
2025-09-21Consider errors in MIR as impossible predicates.Camille Gillot-13/+20
2025-09-20Make term_patch_map sparse.Camille Gillot-14/+18
2025-09-19Auto merge of #145737 - cjgillot:gvn-valueset, r=saethlinbors-44/+178
GVN: stop hashing opaque values GVN generates values that are not meant to be unified with any other. For instance `Opaque` (aka we don't know anything), non-deterministic constants and borrows. The current algorithm generates a unique index, so the generated `Value` will be different from all the existing. This is wasteful, as we should not hash that `Value` at all. This PR proposes to do this. This involves partially reimplementing a `FxIndexSet`, but yields a small but consistent perf improvement (https://github.com/rust-lang/rust/pull/145737#issuecomment-3276951054).
2025-09-18deref related cleanups in ref_propbeepster4096-20/+13
2025-09-18Rollup merge of #146664 - fmease:clean-up-dyn, r=jdonszelmannStuart Cook-1/+1
Clean up `ty::Dynamic` 1. As a follow-up to PR rust-lang/rust#143036, remove `DynKind` entirely. 2. Inside HIR ty lowering, consolidate modules `dyn_compatibility` and `lint` into `dyn_trait` * `dyn_compatibility` wasn't about dyn compatibility itself, it's about lowering trait object types * `lint` contained dyn-Trait-specific diagnostics+lints only
2025-09-18Avoid invalidating from MirPatch::apply.Camille Gillot-3/+3
2025-09-17Lint overlapping assignments in MIR.Camille Gillot-27/+35
2025-09-17Auto merge of #142915 - cjgillot:dest-prop-default, r=saethlinbors-246/+2
Enable DestinationPropagation by default This PR proposes to perform destination propagation on MIR. Most of the pass was fully rewritten by `@JakobDegen` in rust-lang/rust#96451. This pass is quite heavy, as it needs to perform and save the results of a full liveness dataflow analysis. This accounts for ~50% of the pass' runtime. Perf sees a few decent savings in later llvm passes, but also sizeable régressions when there are no savings to balance this pass' runtime.
2025-09-17Rollup merge of #146564 - cjgillot:mir-nolen, r=scottmcmStuart Cook-102/+40
Remove Rvalue::Len again. Now that we have `RawPtrKind::FakeForPtrMetadata`, we can reimplement `Rvalue::Len` using `PtrMetadata(&raw const (fake) place)`. r? ``@scottmcm``
2025-09-17Remove `DynKind`León Orell Valerian Liehr-1/+1
2025-09-16Do not renumber resume local.Camille Gillot-27/+20
2025-09-16Remove Rvalue::Len.Camille Gillot-102/+40
2025-09-16Enable DestinationPropagation by default.Camille GILLOT-246/+2
2025-09-16Restrict simple assignment condition.Camille Gillot-2/+8
2025-09-14Elaborate comment.Camille Gillot-2/+6
2025-09-14Comment.Camille Gillot-0/+9
2025-09-13Mark reads in statements to avoid overlapping assingments.Camille Gillot-10/+19
2025-09-13Stop counting opaques.Camille Gillot-25/+24
2025-09-13Do not hash opaques in GVN.Camille Gillot-39/+90
2025-09-13Introduce ValueSet.Camille Gillot-21/+96
2025-09-11Skip cleanups on unsupported targetsAlex Crichton-8/+29
This commit is an update to the `AbortUnwindingCalls` MIR pass in the compiler. Specifically a new boolean is added for "can this target possibly unwind" and if that's `false` then terminators are all adjusted to be unreachable/not present. The end result is that this fixes 140293 for wasm targets. The motivation for this PR is that currently on WebAssembly targets the usage of the `C-unwind` ABI can lead LLVM to either (a) emit exception-handling instructions or (b) hit a LLVM-ICE-style codegen error. WebAssembly as a base instruction set does not support unwinding at all, and a later proposal to WebAssembly, the exception-handling proposal, was what enabled this. This means that the current intent of WebAssembly targets is that they maintain the baseline of "don't emit exception-handling instructions unless enabled". The commit here is intended to restore this behavior by skipping these instructions even when `C-unwind` is present. Exception-handling is a relatively tricky and also murky topic in WebAssembly, however. There are two sets of instructions LLVM can emit for WebAssembly exceptions, Rust's Emscripten target supports exceptions, WASI targets do not, the LLVM flags to enable this are not always obvious, and additionally this all touches on "changing exception-handling behavior should be a target-level concern, not a feature". Effectively WebAssembly's exception-handling integration into Rust is not finalized at this time. The best idea at this time is that a parallel set of targets will eventually be added which support exceptions, but it's not clear if/when to do this. In the meantime the goal is to keep existing targets working while still enabling experimentation with exception-handling with `-Zbuild-std` and various permutations of LLVM flags. To that extent this commit does not blanket disable these landing pads and cleanup routines for WebAssembly but instead checks to see if panic=unwind is enabled or if `+exception-handling` is enabled. Tests are updated here as well to account for this where, by default, using a `C-unwind` ABI won't affect Rust codegen at all. If `+exception-handling` is enabled, however, then Rust codegen will look like native platforms where exceptions are caught and the program aborts. More-or-less I've done my best to keep exceptions working on wasm where it's possible to have them work, but turned them off where they're not supposed to be emitted.
2025-09-09Auto merge of #145717 - BoxyUwU:erase_regions_rename, r=lcnrbors-3/+4
rename erase_regions to erase_and_anonymize_regions I find it consistently confusing that `erase_regions` does more than replacing regions with `'erased`. it also makes some code look real goofy to be writing manual folders to erase regions with a comment saying "we cant use erase regions" :> or code that re-calls erase_regions on types with regions already erased just to anonymize all the bound regions. r? lcnr idk how i feel about the name being almost twice as long now
2025-09-09erase_regions to erase_and_anonymize_regionsBoxy-3/+4
2025-09-08fixup limit handling codeJana Dönszelmann-1/+1
2025-09-08Rollup merge of #146298 - cjgillot:gvn-derefer, r=nnethercoteMatthias Krüger-0/+5
GVN: Ensure indirect is first projection in try_as_place. I haven't found any report for this bug on existing code, but managed to trigger it with rust-lang/rust#143333
2025-09-08Auto merge of #145910 - saethlin:ignore-intrinsic-calls, r=cjgillotbors-1/+10
Ignore intrinsic calls in cross-crate-inlining cost model I noticed in a side project that a function which just compares to `[u64; 2]` for equality is not cross-crate-inlinable. That was surprising to me because I didn't think that code contained a function call, but of course our array comparisons are lowered to an intrinsic. Intrinsic calls don't make a function no longer a leaf, so it makes sense to add this as an exception to the "only leaves" cross-crate-inline heuristic. This is the useful compare link: https://perf.rust-lang.org/compare.html?start=7cb1a81145a739c4fd858abe3c624ce8e6e5f9cd&end=c3f0a64dbf9fba4722dacf8e39d2fe00069c995e&stat=instructions%3Au because it disables CGU merging in both commits, so effects that cause changes in the sysroot to perturb partitioning downstream are excluded. Perturbations to what is and isn't cross-crate-inlinable in the sysroot has chaotic effects on what items are in which CGUs after merging. It looks like before this PR by sheer luck some of the CGUs dirtied by the patch in eza incr-unchanged happened to be merged together, and with this PR they are not. The perf runs on this PR point to a nice runtime performance improvement.
2025-09-07Auto merge of #145541 - cjgillot:dest-prop-live-range, r=Amanieubors-502/+292
Reimplement DestinationPropagation according to live ranges. This PR reimplements DestinationPropagation as a problem of merging live-ranges of locals. We merge locals that have disjoint live-ranges. This allows merging several locals in the same round by updating live range information. Live ranges are mainly computed using the `MaybeLiveLocals` analysis. The subtlety is that we split each statement and terminator in 2 positions. The first position is the regular statement. The second position is a shadow, which is always more live. It encodes partial writes and dead writes as a local being live for half a statement. This half statement ensures that writes conflict with another local's writes and regular liveness. r? `@Amanieu`
2025-09-07Rollup merge of #146297 - cjgillot:may-observe-address, r=saethlinMatthias Krüger-1/+3
Introduce PlaceContext::may_observe_address. A small utility method to avoid open-coding the logic in several MIR opts.
2025-09-07Use rustc_data_structures::union_find.Camille Gillot-28/+14
2025-09-07Simplify candidate collection.Camille Gillot-55/+43
2025-09-07Unify a source with all possible destinations.Camille Gillot-59/+39
2025-09-07Do not use prepend to avoid quadratic behaviour.Camille Gillot-23/+28