1
Fork 0
rust/compiler/rustc_borrowck/src
Matthias Krüger 3ae7ab698f
Rollup merge of #115291 - cjgillot:dest-prop-save, r=JakobDegen
Save liveness results for DestinationPropagation

`DestinationPropagation` needs to verify that merge candidates do not conflict with each other. This is done by verifying that a local is not live when its counterpart is written to.

To get the liveness information, the pass runs `MaybeLiveLocals` dataflow analysis repeatedly, once for each propagation round. This is quite costly, and the main driver for the perf impact on `ucd` and `diesel`. (See https://github.com/rust-lang/rust/pull/115105#issuecomment-1689205908)

In order to mitigate this cost, this PR proposes to save the result of the analysis into a `SparseIntervalMatrix`, and mirror merges of locals into that matrix: `liveness(destination) := liveness(destination) union liveness(source)`.

<details>
<summary>Proof</summary>

We denote by `'` all the quantities of the transformed program. Let $\varphi$ be a mapping of locals, which maps `source` to `destination`, and is identity otherwise. The exact liveness set after a statement is $out'(statement)$, and the proposed liveness set is $\varphi(out(statement))$.

Consider a statement. Suppose that the output state verifies $out' \subset phi(out)$. We want to prove that $in' \subset \varphi(in)$ where $in = (out - kill) \cup gen$, and conclude by induction.

We have 2 cases: either that statement is kept with locals renumbered by $\varphi$, or it is a tautological assignment and it removed.

1. If the statement is kept: the gen-set and the kill-set of $statement' = \varphi(statement)$ are $gen' = \varphi(gen)$ and $kill' = \varphi(kill)$ exactly.
From soundness requirement 3, $\varphi(in)$ is disjoint from $\varphi(kill)$.
This implies that $\varphi(out - kill)$ is disjoint from $\varphi(kill)$, and so $\varphi(out - kill) = \varphi(out) - \varphi(kill)$. Then $\varphi(in) = (\varphi(out) - \varphi(kill)) \cup \varphi(gen) = (\varphi(out) - kill') \cup gen'$.
We can conclude that $out' \subset \varphi(out) \implies in' \subset \varphi(in)$.

2. If the statement is removed. As $\varphi(statement)$ is a tautological assignment, we know that $\varphi(gen) = \varphi(kill) = \\{ destination \\}$, while $gen' = kill' = \emptyset$. So $\varphi(in) = \varphi(out) \cup \\{ destination \\}$. Then $in' = out' \subset out \subset \varphi(in)$.

By recursion, we can conclude by that $in' \subset \varphi(in)$ everywhere.
</details>

This approximate liveness results is only suboptimal if there are locals that fully disappear from the CFG due to an assignment cycle. These cases are quite unlikely, so we do not bother with them.

This change allows to reduce the perf impact of DestinationPropagation by half on diesel and ucd (https://github.com/rust-lang/rust/pull/115105#issuecomment-1694701904).

cc ````@JakobDegen````
2024-01-17 20:21:19 +01:00
..
constraints Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
diagnostics Auto merge of #116520 - Enselic:large-copy-into-fn, r=oli-obk 2024-01-16 19:33:14 +00:00
polonius compiler: Lower fn call arg spans down to MIR 2024-01-15 19:07:11 +01:00
region_infer Rollup merge of #115291 - cjgillot:dest-prop-save, r=JakobDegen 2024-01-17 20:21:19 +01:00
type_check Rollup merge of #115291 - cjgillot:dest-prop-save, r=JakobDegen 2024-01-17 20:21:19 +01:00
util
borrow_set.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00
borrowck_errors.rs Rename consuming chaining methods on DiagnosticBuilder. 2024-01-10 07:40:00 +11:00
consumers.rs Fix redundant explicit link in rustc_borrowck 2023-08-19 02:22:13 +08:00
dataflow.rs remove redundant imports 2023-12-10 10:56:22 +08:00
def_use.rs rename BorrowKind::Shallow to Fake 2023-11-08 22:55:28 +01:00
facts.rs introduce Polonius enum for -Zpolonius 2023-10-04 16:16:12 +00:00
lib.rs compiler: Lower fn call arg spans down to MIR 2024-01-15 19:07:11 +01:00
location.rs Replace no_ord_impl with orderable. 2023-11-22 18:38:17 +11:00
member_constraints.rs remove unused muts 2023-04-28 20:19:48 +02:00
nll.rs Move PointIndex to mir_dataflow. 2024-01-07 20:07:35 +00:00
path_utils.rs Remove Upvar duplication 2023-11-26 13:19:10 -05:00
place_ext.rs use PlaceRef abstractions more consistently 2023-06-25 20:38:01 -04:00
places_conflict.rs rename BorrowKind::Shallow to Fake 2023-11-08 22:55:28 +01:00
prefixes.rs Add docs, remove code, change subtyper code 2023-10-02 23:39:44 +03:00
renumber.rs add fixme to RegionCtxt 2023-11-13 14:13:54 +00:00
session_diagnostics.rs s/generator/coroutine/ 2023-10-20 21:14:01 +00:00
universal_regions.rs Simplify closure_env_ty and closure_env_param 2024-01-14 19:15:46 +00:00
used_muts.rs Fix clippy::needless_borrow in the compiler 2023-11-21 20:13:40 +01:00