Commit Graph

619 Commits

Author SHA1 Message Date
Esteban Küber 146711fc24 Use let...else instead of match foo { ... _ => return }; and if let ... else return 2025-12-12 17:52:39 +00:00
bors 4ad239f415 Auto merge of #142821 - cjgillot:jump-threading-single, r=saethlin
Compute jump threading opportunities in a single pass

The current implementation of jump threading walks MIR CFG backwards from each `SwitchInt` terminator. This PR replaces this by a single postorder traversal of MIR. In theory, we could do a full fixpoint dataflow analysis, but this has low returns as we forbid threading through a loop header.

The second commit in this PR modifies the carried state to a lighter data structure. The current implementation uses some kind of `IndexVec<ValueIndex, &[Condition]>`. This is needlessly heavy, as the state rarely ever carries more than a few `Condition`s. The first commit replaces this state with a simpler `&[Condition]`, and puts the corresponding `ValueIndex` inside `Condition`.

The three later commits are perf tweaks.

The sixth commit is the main change. Instead of carrying the goto target inside the condition, we maintain a set of conditions associated with each block, and their consequences in following blocks. Think: if this condition is fulfilled in this block, then that condition is fulfilled in that block. This makes the threading algorithm much easier to implement, without the extra bookkeeping of `ThreadingOpportunity` we had.

Later commits modify that algorithm to shrink the set of duplicated blocks. By propagating fulfilled conditions down the CFG, and trimming costly threads.
2025-12-01 23:44:49 +00:00
Camille Gillot 72444372ae Replace OffsetOf by an actual sum. 2025-11-18 00:10:03 +00:00
Camille GILLOT acf3b6a6a6 Skip process_constant if state has no matching value. 2025-11-16 01:38:16 +00:00
Camille Gillot 4f24d70395 Extend value_analysis API. 2025-11-16 01:37:48 +00:00
Stuart Cook d3475140ee Rollup merge of #128666 - pitaj:intrinsic-overflow_checks, r=BoxyUwU
Add `overflow_checks` intrinsic

This adds an intrinsic which allows code in a pre-built library to inherit the overflow checks option from a crate depending on it. This enables code in the standard library to explicitly change behavior based on whether `overflow_checks` are enabled, regardless of the setting used when standard library was compiled.

This is very similar to the `ub_checks` intrinsic, and refactors the two to use a common mechanism.

The primary use case for this is to allow the new `RangeFrom` iterator to yield the maximum element before overflowing, as requested [here](https://github.com/rust-lang/rust/issues/125687#issuecomment-2151118208). This PR includes a working `IterRangeFrom` implementation based on this new intrinsic that exhibits the desired behavior.

[Prior discussion on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Ability.20to.20select.20code.20based.20on.20.60overflow_checks.60.3F)
2025-11-09 13:22:23 +11:00
Peter Jaszkowiak cc8b95cc54 add overflow_checks intrinsic 2025-11-08 10:57:35 -07:00
Nicholas Nethercote 8793239702 Put Analysis back into Results.
`Results` used to contain an `Analysis`, but it was removed in #140234.
That change made sense because the analysis was mutable but the entry
states were immutable and it was good to separate them so the mutability
of the different pieces was clear.

Now that analyses are immutable there is no need for the separation,
lots of analysis+results pairs can be combined, and the names are going
back to what they were before:
- `Results` -> `EntryStates`
- `AnalysisAndResults` -> `Results`
2025-10-28 10:26:50 +11:00
Nicholas Nethercote a97cd3ba57 Make Analysis immutable in many more places.
The `state: A::Domain` value is the primary things that's modified when
performing an analysis. The `Analysis` impl is immutable in every case
but one (`MaybeRequiredStorage`) and it now uses interior mutability.

As well as changing many `&mut A` arguments to `&A`, this also:
- lets `CowMut` be replaced with the simpler `SimpleCow` in `cursor.rs`;
- removes the need for the `RefCell` in `Formatter`;
- removes the need for `MaybeBorrowedLocals` to impl `Clone`, because
  it's a unit type and it's now clear that its constructor can be used
  directly instead of being put into a local variable and cloned.
2025-10-28 08:23:27 +11:00
Nicholas Nethercote 8afbd9fe02 Use a RefCell in MaybeRequiresStorage.
This will let us make `Analysis` arguments in many other places
immutable, in the next commit.
2025-10-27 18:37:10 +11:00
Nicholas Nethercote 517b767fa1 Reorder args for visit_results_in_block.
Put `analysis` first, to match `apply_effects_in_range`.
2025-10-27 18:37:10 +11:00
Nicholas Nethercote 958a2e4a3d Make Analysis immutable in ResultsVisitor::visit_* methods.
This makes sense -- you wouldn't expect that visiting the results of an
analysis would change the analysis itself.
2025-10-27 18:37:06 +11:00
Peter Jaszkowiak 2e5d395f2b refactor ub_checks and contract_checks to share logic 2025-10-25 14:30:04 -06:00
Camille Gillot 5dfbf67f94 Replace NullOp::SizeOf and NullOp::AlignOf by lang items. 2025-10-23 00:38:28 +00:00
beepster4096 bce7f71589 make move unwrap_binder! a move subpath 2025-10-11 22:51:40 -07:00
beepster4096 b151a5e6a2 update movepath docs 2025-10-11 22:51:40 -07:00
beepster4096 fb1c90a0ae use abstraction over projectionelem in move analysis 2025-10-11 22:51:40 -07:00
Camille Gillot b7c2b3dc80 Remove StatementKind::Deinit. 2025-10-10 12:57:24 +00:00
dianqk 8da04285cf mir-opt: Eliminate dead statements even if they are used by debuginfos 2025-10-02 14:58:59 +08:00
dianqk 85b2f70693 mir-opt: Eliminate trivial unnecessary storage annotations 2025-10-02 14:55:51 +08:00
dianqk 571412f819 mir-opt: Eliminate dead ref statements 2025-10-02 14:55:50 +08:00
beepster4096 aa5a21450a ProjectionElem::Subtype -> CastKind::Subtype 2025-09-26 01:25:26 -07:00
Stuart Cook 540fd20ba6 Rollup merge of #146664 - fmease:clean-up-dyn, r=jdonszelmann
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-18 11:48:51 +10:00
León Orell Valerian Liehr 26f3337d4e Remove DynKind 2025-09-17 04:46:46 +02:00
Camille Gillot 53b91ea87f Remove Rvalue::Len. 2025-09-16 22:23:19 +00:00
Boxy e379c77586 erase_regions to erase_and_anonymize_regions 2025-09-09 14:49:16 +02:00
bors 2f3f27bf79 Auto merge of #145541 - cjgillot:dest-prop-live-range, r=Amanieu
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-07 23:36:21 +00:00
Camille GILLOT 9b8a719ae4 Reimplement DestinationPropagation according to live ranges. 2025-09-07 16:24:46 +00:00
Camille GILLOT 2ff92e83af Introduce fast insertion at extremities to IntervalSet. 2025-09-07 16:06:40 +00:00
Camille GILLOT 4e7a068c9a Introduce PlaceContext::may_observe_address. 2025-09-07 13:51:53 +00:00
Nicholas Nethercote 301655eafe Revert introduction of [workspace.dependencies].
This was done in #145740 and #145947. It is causing problems for people
using r-a on anything that uses the rustc-dev rustup package, e.g. Miri,
clippy.

This repository has lots of submodules and subtrees and various
different projects are carved out of pieces of it. It seems like
`[workspace.dependencies]` will just be more trouble than it's worth.
2025-09-02 19:12:54 +10:00
Nicholas Nethercote 5ce3797073 Introduce MirDumper and MirWriter.
MIR dumping is a mess. There are lots of functions and entry points,
e.g. `dump_mir`, `dump_mir_with_options`, `dump_polonius_mir`,
`dump_mir_to_writer`. Also, it's crucial that `create_dump_file` is
never called without `dump_enabled` first being checked, but there is no
mechanism for ensuring this and it's hard to tell if it is satisfied on
all paths. (`dump_enabled` is checked twice on some paths, however!)

This commit introduces `MirWriter`, which controls the MIR writing, and
encapsulates the `extra_data` closure and `options`. Two existing
functions are now methods of this type. It sets reasonable defaults,
allowing the removal of many `|_, _| Ok(())` closures.

The commit also introduces `MirDumper`, which is layered on top of
`MirWriter`, and which manages the creation of the dump files,
encapsulating pass names, disambiguators, etc. Four existing functions
are now methods of this type.
- `MirDumper::new` will only succeed if dumps are enabled, and will
  return `None` otherwise, which makes it impossible to dump when you
  shouldn't.
- It also sets reasonable defaults for various things like
  disambiguators, which means you no longer need to specify them in many
  cases. When they do need to be specified, it's now done via setter
  methods.
- It avoids some repetition. E.g. `dump_nll_mir` previously specifed the
  pass name `"nll"` four times and the disambiguator `&0` three times;
  now it specifies them just once, to put them in the `MirDumper`.
- For Polonius, the `extra_data` closure can now be specified earlier,
  which avoids having to pass some arguments through some functions.
2025-09-01 09:19:03 +10:00
Nicholas Nethercote b16c66ee83 Add polonius-engine to [workspace.dependencies]. 2025-08-28 20:12:16 +10:00
Nicholas Nethercote c50d2cc807 Add tracing to [workspace.dependencies]. 2025-08-27 14:21:19 +10:00
Jacob Pratt c80e77fa21 Rollup merge of #145695 - cjgillot:place-elem-map, r=oli-obk,lcnr
Introduce ProjectionElem::try_map.

Small utility function useful to manipulate MIR place projections.
2025-08-22 22:00:54 -04:00
Camille Gillot e5bd01b533 Correct comments. 2025-08-21 23:59:20 +00:00
Camille GILLOT 37e7f52876 Introduce ProjectionElem::try_map. 2025-08-21 02:06:21 +00:00
Josh Triplett bad4f5c13f Eliminate unnecessary dependency from rustc_mir_dataflow to rustc_hir
`rustc_mir_dataflow` only uses `DefId`, which is a re-export from
`rustc_span`.
2025-08-20 15:04:00 -07:00
Amogh Shivaram 997c6a8821 Escape diff strings in graphviz 2025-08-08 00:20:55 -05:00
Samuel Tardieu d082ff4c04 Rollup merge of #144478 - joshtriplett:doc-code-formatting-prep, r=Amanieu
Improve formatting of doc code blocks

We don't currently apply automatic formatting to doc comment code blocks. As a
result, it has built up various idiosyncracies, which make such automatic
formatting difficult. Some of those idiosyncracies also make things harder for
human readers or other tools.

This PR makes a few improvements to doc code formatting, in the hopes of making
future automatic formatting easier, as well as in many cases providing net
readability improvements.

I would suggest reading each commit separately, as each commit contains one
class of changes.
2025-08-02 11:24:24 +02:00
Kivooeo bae38bad78 use let chains in hir, lint, mir 2025-07-28 06:10:14 +05:00
Josh Triplett 3352dfc4a2 Skip formatting for some compiler documentation code
These examples feature Rust code that's presented primarily to
illustrate how its compilation would be handled, and these examples are
formatted to highlight those aspects in ways that rustfmt wouldn't
preserve. Turn formatting off in those examples.

(Doc code isn't formatted yet, but this will make it easier to enable
doc code formatting in the future.)
2025-07-25 22:01:49 -07:00
Tomasz Miąsko 5b6d661039 Remove support for SwitchInt edge effects in backward dataflow analyses
Those effects are untested and unused. Remove them along with
the implementation of `BasicBlocks::switch_sources`.
2025-07-11 10:56:50 +02:00
Tomasz Miąsko 011d4aa81f Call get_switch_int_data on a block with SwitchInt terminator
Fix a mix-up of a block with its predecessors in handling of SwitchInt
edge effects for backward analysis. Note that this functionality is
currently unused, so change has no practical impact.
2025-07-11 08:49:11 +02:00
bors d350797b7e Auto merge of #142707 - ashivaram23:drop_wildcard, r=dianqk
Apply effects to `otherwise` edge in dataflow analysis

This allows `ElaborateDrops` to remove drops when a `match` wildcard arm covers multiple no-Drop enum variants. It modifies dataflow analysis to update the `MaybeUninitializedPlaces` and `MaybeInitializedPlaces` data for a block reached through an `otherwise` edge.

Fixes rust-lang/rust#142705.
2025-07-09 03:42:01 +00:00
Amogh Shivaram c7ef03aeb7 Apply effects to otherwise edge in dataflow analysis 2025-07-08 18:15:05 -05:00
dianqk 24e553e6bc mir: Use the new method for BasicBlockData 2025-06-29 20:39:13 +08:00
dianqk 9f9cd5e283 mir: Add a new method to statement
Avoid introducing a large number of changes when adding optional initialization fields.
2025-06-29 20:13:36 +08:00
Pietro Albini 2ce08ca5d6 update cfg(bootstrap) 2025-05-12 15:33:37 +02:00
Guillaume Gomez 82c99c41c0 Rollup merge of #140234 - nnethercote:separate-Analysis-and-Results, r=davidtwco
Separate dataflow analysis and results

`Analysis` gets put into `Results` with `EntryStates`, by `iterate_to_fixpoint`. This has two problems:
- `Results` is passed various places where only `Analysis` is needed.
- `EntryStates` is passed around mutably everywhere even though it is immutable.

This commit mostly separates `Analysis` from `Results` and fixes these two problems.

r? `@davidtwco`
2025-05-07 18:19:04 +02:00