Commit Graph

1582 Commits

Author SHA1 Message Date
Martin Nordholts
6d8fb57d1a rustc_mir_transform: Enforce rustc::potential_query_instability lint
Stop allowing `rustc::potential_query_instability` on all of
rustc_mir_transform and instead allow it on a case-by-case basis if it
is safe to do so. In this particular crate, all instances were safe to
allow.
2024-01-06 19:09:04 +01:00
Matthias Krüger
909f2b63a3
Rollup merge of #119591 - Enselic:DestinationPropagation-stable, r=cjgillot
rustc_mir_transform: Make DestinationPropagation stable for queries

By using `FxIndexMap` instead of `FxHashMap`, so that the order of visiting of locals is deterministic.

We also need to bless
`copy_propagation_arg.foo.DestinationPropagation.panic*.diff`. Do not review the diff of the diff. Instead look at the diff files before and after this commit. Both before and after this commit, 3 statements are replaced with nop. It's just that due to change in ordering, different statements are replaced. But the net result is the same. In other words, compare this diff (before fix):
* 090d5eac72/tests/mir-opt/dest-prop/copy_propagation_arg.foo.DestinationPropagation.panic-unwind.diff

With this diff (after fix):
* f603babd63/tests/mir-opt/dest-prop/copy_propagation_arg.foo.DestinationPropagation.panic-unwind.diff

and you can see that both before and after the fix, we replace 3 statements with `nop`s.

I find it _slightly_ surprising that the test this PR affects did not previously fail spuriously due to the indeterminism of `FxHashMap`, but I guess in can be explained with the predictability of small `FxHashMap`s with `usize` (`Local`) keys, or something along those lines.

This should fix [this](https://github.com/rust-lang/rust/pull/119252#discussion_r1436101791) comment, but I wanted to make a separate PR for this fix for a simpler development and review process.

Part of https://github.com/rust-lang/rust/issues/84447 which is E-help-wanted.

r? `@cjgillot` who is reviewer for the highly related PR https://github.com/rust-lang/rust/pull/119252.
2024-01-06 16:07:47 +01:00
bors
efb3f11087 Auto merge of #119499 - cjgillot:dtm-opt, r=nnethercote
Two small bitset optimisations
2024-01-06 11:54:15 +00:00
bors
aa7e9f21e9 Auto merge of #119648 - compiler-errors:rollup-42inxd8, r=compiler-errors
Rollup of 9 pull requests

Successful merges:

 - #119208 (coverage: Hoist some complex code out of the main span refinement loop)
 - #119216 (Use diagnostic namespace in stdlib)
 - #119414 (bootstrap: Move -Clto= setting from Rustc::run to rustc_cargo)
 - #119420 (Handle ForeignItem as TAIT scope.)
 - #119468 (rustdoc-search: tighter encoding for f index)
 - #119628 (remove duplicate test)
 - #119638 (fix cyle error when suggesting to use associated function instead of constructor)
 - #119640 (library: Fix warnings in rtstartup)
 - #119642 (library: Fix a symlink test failing on Windows)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-01-06 06:00:27 +00:00
Michael Goulet
bacddd3e5d
Rollup merge of #119208 - Zalathar:hoist, r=WaffleLapkin,Swatinem
coverage: Hoist some complex code out of the main span refinement loop

The span refinement loop in `spans.rs` takes the spans that have been extracted from MIR, and modifies them to produce more helpful output in coverage reports.

It is also one of the most complicated pieces of code in the coverage instrumentor. It has an abundance of moving pieces that make it difficult to understand, and most attempts to modify it end up accidentally changing its behaviour in unacceptable ways.

This PR nevertheless tries to make a dent in it by hoisting two pieces of special-case logic out of the main loop, and into separate preprocessing passes. Coverage tests show that the resulting mappings are *almost* identical, with all known differences being unimportant.

This should hopefully unlock further simplifications to the refinement loop, since it now has fewer edge cases to worry about.
2024-01-05 23:41:41 -05:00
bors
d62f05b842 Auto merge of #119459 - cjgillot:inline-mir-utils, r=compiler-errors
Inline a few utility functions around MIR

Most of them are small enough to benefit from inlining.
2024-01-06 04:01:09 +00:00
Martin Nordholts
95eb5bcb67 rustc_mir_transform: Make DestinationPropagation stable for queries
By using FxIndexMap instead of FxHashMap, so that the order of visiting
of locals is deterministic.

We also need to bless
copy_propagation_arg.foo.DestinationPropagation.panic*.diff. Do not
review the diff of the diff. Instead look at the diff file before and
after this commit. Both before and after this commit, 3 statements are
replaced with nop. It's just that due to change in ordering, different
statements are replaced. But the net result is the same.
2024-01-05 20:55:32 +01:00
Matthias Krüger
ad7aabd965
Rollup merge of #119563 - compiler-errors:coroutine-resume, r=oli-obk
Check yield terminator's resume type in borrowck

In borrowck, we didn't check that the lifetimes of the `TerminatorKind::Yield`'s `resume_place` were actually compatible with the coroutine's signature. That means that the lifetimes were totally going unchecked. Whoops!

This PR implements this checking.

Fixes #119564

r? types
2024-01-05 20:39:53 +01:00
Michael Goulet
3a983ad3b0
Rollup merge of #119577 - tmiasko:lint, r=oli-obk
Migrate memory overlap check from validator to lint

The check attempts to identify potential undefined behaviour, rather
than whether MIR is well-formed. It belongs in the lint not validator.

Follow up to changes from #119077.
2024-01-05 10:57:22 -05:00
Michael Goulet
f361b591ef
Rollup merge of #119538 - nnethercote:cleanup-errors-5, r=compiler-errors
Cleanup error handlers: round 5

More rustc_errors cleanups. A sequel to https://github.com/rust-lang/rust/pull/119171.

r? ````@compiler-errors````
2024-01-05 10:57:21 -05:00
Matthew Jasper
26f48b4cba Stabilize THIR unsafeck 2024-01-05 10:00:59 +00:00
Zalathar
514e026853 coverage: Make the remaining fields of CoverageSpan non-public
The struct itself is already non-public, so having public fields doesn't
achieve anything.
2024-01-05 12:53:23 +11:00
Zalathar
cd5084388a coverage: Split out SpanFromMir from CoverageSpan
This draws a clear distinction between the fields/methods that are needed by
initial span extraction and preprocessing, and those that are needed by the
main "refinement" loop.
2024-01-05 12:53:23 +11:00
Zalathar
d4d2f1428c coverage: Hoist the splitting of visible macro invocations 2024-01-05 12:53:23 +11:00
Zalathar
cd3a9760e4 coverage: Hoist the removal of unwanted macro expansion spans 2024-01-05 12:53:23 +11:00
Zalathar
df0df5256b coverage: Overhaul how "visible macros" are determined 2024-01-05 12:53:23 +11:00
Zalathar
506b9f9689 coverage: Avoid early returns from mir_to_initial_sorted_coverage_spans 2024-01-05 12:53:23 +11:00
Tomasz Miąsko
df116ec246 Migrate memory overlap check from validator to lint
The check attempts to identify potential undefined behaviour, rather
than whether MIR is well-formed. It belongs in the lint not validator.
2024-01-04 23:32:22 +01:00
Tomasz Miąsko
a084e063e6 Fix validation and linting of injected MIR
Reevaluate `body.should_skip()` after updating the MIR phase to ensure
that injected MIR is processed correctly.

Update a few custom MIR tests that were ill-formed for the injected
phase.
2024-01-04 23:06:42 +01:00
Tomasz Miąsko
12b92c8a87 Visit only reachable blocks in MIR lint
No functional changes - all checks have been emitted conditionally on
block being rechable already.
2024-01-04 23:04:48 +01:00
Michael Goulet
1d48f69d65 Check yield terminator's resume type in borrowck 2024-01-04 01:47:56 +00:00
León Orell Valerian Liehr
3053ced813
Rollup merge of #119444 - compiler-errors:closure-or-coroutine, r=oli-obk
Rename `TyCtxt::is_closure` to `TyCtxt::is_closure_or_coroutine`

This function has always been used to test whether the def-id was a closure **or** coroutine: https://github.com/rust-lang/rust/pull/118311/files#diff-69ebec59f7d38331dd1be84ede7957977dcaa39e30ed2869b04aa8c99b2079ccR552 -- the name is just confusing because it disagrees with other fns named `is_closure`, like `Ty::is_closure`.

So let's rename it.
2024-01-03 16:08:26 +01:00
Nicholas Nethercote
505c1371d0 Rename some Diagnostic setters.
`Diagnostic` has 40 methods that return `&mut Self` and could be
considered setters. Four of them have a `set_` prefix. This doesn't seem
necessary for a type that implements the builder pattern. This commit
removes the `set_` prefixes on those four methods.
2024-01-03 19:40:20 +11:00
Camille GILLOT
af7819477a Reuse eligible_storage_live memory. 2024-01-02 21:20:04 +00:00
Camille GILLOT
f83468c89b Inline dominator check. 2023-12-31 00:37:45 +00:00
Michael Goulet
07adee7072 is_coroutine -> is_coroutine_or_closure 2023-12-30 15:24:15 +00:00
bors
e45a937a11 Auto merge of #119438 - Zalathar:prepare-mappings, r=cjgillot
coverage: Prepare mappings separately from injecting statements

These two tasks historically needed to be interleaved, but after various recent changes (including #116046 and #116917) they can now be fully separated.

---

`@rustbot` label +A-code-coverage
2023-12-30 13:39:44 +00:00
Zalathar
3f67118ae7 coverage: Make coverage_counters a local variable
This avoids the awkwardness of having to create it in the pass's constructor,
and then mutate it later to actually create the counters.
2023-12-30 22:36:11 +11:00
Zalathar
e1a2babc06 coverage: Prepare mappings separately from injecting statements
These two tasks historically needed to be interleaved, but after various recent
changes (including #116046 and #116917) they can now be fully separated.
2023-12-30 22:34:15 +11:00
bors
c2354aabea Auto merge of #119377 - tmiasko:after, r=cjgillot
Don't validate / lint MIR before each pass

To avoid redundant work and verbose output in case of failures.
2023-12-30 09:42:05 +00:00
bors
8d76d07666 Auto merge of #116012 - cjgillot:gvn-const, r=oli-obk
Implement constant propagation on top of MIR SSA analysis

This implements the idea I proposed in https://github.com/rust-lang/rust/pull/110719#issuecomment-1718324700

Based on https://github.com/rust-lang/rust/pull/109597

The value numbering "GVN" pass formulates each rvalue that appears in MIR with an abstract form (the `Value` enum), and assigns an integer `VnIndex` to each. This abstract form can be used to deduplicate values, reusing an earlier local that holds the same value instead of recomputing. This part is proposed in #109597.

From this abstract representation, we can perform more involved simplifications, for example in https://github.com/rust-lang/rust/pull/111344.

With the abstract representation `Value`, we can also attempt to evaluate each to a constant using the interpreter. This builds a `VnIndex -> OpTy` map. From this map, we can opportunistically replace an operand or a rvalue with a constant if their value has an associated `OpTy`.

The most relevant commit is [Evaluated computed values to constants.](2767c4912e)"

r? `@oli-obk`
2023-12-30 03:45:58 +00:00
Matthias Krüger
b75ba15062
Rollup merge of #119322 - compiler-errors:async-gen-resume-ty, r=cjgillot
Couple of random coroutine pass simplifications

Just aesthetic changes, except for a random `Ty::new_task_context(tcx)` call that was redundant.
2023-12-29 21:40:22 +01:00
Zalathar
8529b63e2b coverage: Avoid a possible query stability hazard in CoverageCounters
The iteration order of this hashmap can potentially affect the relative
creation order of MIR blocks.
2023-12-29 12:33:52 +11:00
Michael Goulet
d71f7be218 Couple of random coroutine pass simplifications 2023-12-29 00:19:33 +00:00
Michael Goulet
e24da8ea19 Movability doesn't need to be a query anymore 2023-12-28 16:35:01 +00:00
Michael Goulet
fcb42b42d6 Remove movability from TyKind::Coroutine 2023-12-28 16:35:01 +00:00
Tomasz Miąsko
8d77c2eab8 Don't validate / lint MIR before each pass
To avoid redundant work and verbose output in case of failures.
2023-12-28 15:32:54 +01:00
cuishuang
1adf0c16ff Fix some comments
Signed-off-by: cuishuang <imcusg@gmail.com>
2023-12-28 12:23:14 +08:00
Zalathar
be6b059169 coverage: Unexpand spans with find_ancestor_inside_same_ctxt 2023-12-27 23:49:31 +11:00
bors
1ab783112a Auto merge of #119258 - compiler-errors:closure-kind, r=eholk
Make closures carry their own ClosureKind

Right now, we use the "`movability`" field of `hir::Closure` to distinguish a closure and a coroutine. This is paired together with the `CoroutineKind`, which is located not in the `hir::Closure`, but the `hir::Body`. This is strange and redundant.

This PR introduces `ClosureKind` with two variants -- `Closure` and `Coroutine`, which is put into `hir::Closure`. The `CoroutineKind` is thus removed from `hir::Body`, and `Option<Movability>` no longer needs to be a stand-in for "is this a closure or a coroutine".

r? eholk
2023-12-26 04:25:53 +00:00
Michael Goulet
3320c09eab Only regular coroutines have movability 2023-12-25 21:13:41 +00:00
Camille GILLOT
2837727471 Replace legacy ConstProp by GVN. 2023-12-24 20:08:57 +00:00
Camille GILLOT
a03c972816 Enable GVN by default. 2023-12-24 20:08:57 +00:00
Nicholas Nethercote
99472c7049 Remove Session methods that duplicate DiagCtxt methods.
Also add some `dcx` methods to types that wrap `TyCtxt`, for easier
access.
2023-12-24 08:05:28 +11:00
Michael Goulet
ae0a6e8537
Rollup merge of #119198 - compiler-errors:desugaring, r=eholk
Split coroutine desugaring kind from source

What a coroutine is desugared from (gen/async gen/async) should be separate from where it comes (fn/block/closure).
2023-12-22 21:41:04 -05:00
Michael Goulet
7dd095598b
Rollup merge of #119077 - tmiasko:lint, r=cjgillot
Separate MIR lints from validation

Add a MIR lint pass, enabled with -Zlint-mir, which identifies undefined or
likely erroneous behaviour.

The initial implementation mostly migrates existing checks of this nature from
MIR validator, where they did not belong (those checks have false positives and
there is nothing inherently invalid about MIR with undefined behaviour).

Fixes #104736
Fixes #104843
Fixes #116079
Fixes #116736
Fixes #118990
2023-12-22 21:41:03 -05:00
Michael Goulet
004450506e Split coroutine desugaring kind from source 2023-12-22 23:58:29 +00:00
bors
cee794ee98 Auto merge of #119097 - nnethercote:fix-EmissionGuarantee, r=compiler-errors
Fix `EmissionGuarantee`

There are some problems with the `DiagCtxt` API related to `EmissionGuarantee`. This PR fixes them.

r? `@compiler-errors`
2023-12-22 00:03:57 +00:00
Tomasz Miąsko
532080cfcc Stricter check for a use of locals without storage 2023-12-21 12:58:39 +01:00
Tomasz Miąsko
b4877753c3 Don't require owned data in MaybeStorageDead 2023-12-21 12:58:39 +01:00