-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save liveness results for DestinationPropagation #115291
Conversation
r? @jackh726 (rustbot has picked a reviewer for you, use r? to override) |
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
I don't exactly buy the proof. The induction on the proof seems to be in reverse graph structure; it's missing the component that reasons across basic blocks - but trying to add this component in won't work nicely, because the induction will fail on backedges. That being said, I don't think the algorithm is actually wrong, and we can make it in a more straightforward manner: Instead of talking about merging locals, let's pretend that we removed the old locals |
a643a95
to
0e56765
Compare
The induction is one the dataflow fixed-point iteration. I don't need to specialize for backedges. For back-edges, consider the exact dataflow equation |
Can we r? @JakobDegen I unfortunately don't have the background to review this properly right now, nor the time atm to learn that background. |
@JakobDegen I'm not sure how to read your review. Is this good to merge, or do you need me to make some changes? |
☔ The latest upstream changes (presumably #113218) made this pull request unmergeable. Please resolve the merge conflicts. |
e57957e
to
c1bebe4
Compare
@JakobDegen could you clarify if you would like to see any additional changes made to this PR? Thanks! 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Factoring out LivenessValues
has tension in that rustc_borrowck
depends on its internals at the very least for MIR dumps.
☔ The latest upstream changes (presumably #118143) made this pull request unmergeable. Please resolve the merge conflicts. |
Btw @cjgillot I'm not sure it'd be super useful to you but if you needed to make some progress here, I'd gladly r+ a PR about the |
c1bebe4
to
e5c64b5
Compare
☔ The latest upstream changes (presumably #117880) made this pull request unmergeable. Please resolve the merge conflicts. |
23fb1ac
to
810c2d1
Compare
Switching to waiting on author to reply to unresolved comments. Feel free to request a review with @rustbot author |
c09c849
to
1d6723a
Compare
r? wg-mir-opt |
I think this should stay assigned to @JakobDegen as I won't be able to give it the kind of detailed review this pass needs r? @JakobDegen |
Discussed with @JakobDegen on zulip. |
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 rust-lang#115105 (comment)) 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 (rust-lang#115105 (comment)). cc `@JakobDegen`
…mpiler-errors Rollup of 9 pull requests Successful merges: - rust-lang#115291 (Save liveness results for DestinationPropagation) - rust-lang#119651 (proc_macro: Add Literal::c_string constructor) - rust-lang#119855 (Enable Static Builds for FreeBSD) - rust-lang#119955 (Modify GenericArg and Term structs to use strict provenance rules) - rust-lang#119975 (Don't ICE if TAIT-defining fn contains a closure with `_` in return type) - rust-lang#119984 (Change return type of unstable `Waker::noop()` from `Waker` to `&Waker`.) - rust-lang#120001 (Consistently unset RUSTC_BOOTSTRAP when compiling bootstrap) - rust-lang#120020 (Gracefully handle missing typeck information if typeck errored) - rust-lang#120032 (Fix `rustc_abi` build on stable) r? `@ghost` `@rustbot` modify labels: rollup
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 rust-lang#115105 (comment)) 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 (rust-lang#115105 (comment)). cc ``@JakobDegen``
…iaskrgr Rollup of 11 pull requests Successful merges: - rust-lang#115291 (Save liveness results for DestinationPropagation) - rust-lang#119855 (Enable Static Builds for FreeBSD) - rust-lang#119975 (Don't ICE if TAIT-defining fn contains a closure with `_` in return type) - rust-lang#119984 (Change return type of unstable `Waker::noop()` from `Waker` to `&Waker`.) - rust-lang#120001 (Consistently unset RUSTC_BOOTSTRAP when compiling bootstrap) - rust-lang#120020 (Gracefully handle missing typeck information if typeck errored) - rust-lang#120031 (Construct closure type eagerly) - rust-lang#120032 (Fix `rustc_abi` build on stable) - rust-lang#120039 (pat_analysis: Don't rely on contiguous `VariantId`s outside of rustc) - rust-lang#120044 (Fix typo in comments (in_place_collect)) - rust-lang#120056 (Use FnOnceOutput instead of FnOnce where expected) r? `@ghost` `@rustbot` modify labels: rollup
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 rust-lang#115105 (comment)) 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 (rust-lang#115105 (comment)). cc ```@JakobDegen```
…iaskrgr Rollup of 10 pull requests Successful merges: - rust-lang#115291 (Save liveness results for DestinationPropagation) - rust-lang#119855 (Enable Static Builds for FreeBSD) - rust-lang#119975 (Don't ICE if TAIT-defining fn contains a closure with `_` in return type) - rust-lang#120001 (Consistently unset RUSTC_BOOTSTRAP when compiling bootstrap) - rust-lang#120020 (Gracefully handle missing typeck information if typeck errored) - rust-lang#120031 (Construct closure type eagerly) - rust-lang#120032 (Fix `rustc_abi` build on stable) - rust-lang#120039 (pat_analysis: Don't rely on contiguous `VariantId`s outside of rustc) - rust-lang#120044 (Fix typo in comments (in_place_collect)) - rust-lang#120056 (Use FnOnceOutput instead of FnOnce where expected) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 10 pull requests Successful merges: - rust-lang#115291 (Save liveness results for DestinationPropagation) - rust-lang#119855 (Enable Static Builds for FreeBSD) - rust-lang#119975 (Don't ICE if TAIT-defining fn contains a closure with `_` in return type) - rust-lang#120001 (Consistently unset RUSTC_BOOTSTRAP when compiling bootstrap) - rust-lang#120020 (Gracefully handle missing typeck information if typeck errored) - rust-lang#120031 (Construct closure type eagerly) - rust-lang#120032 (Fix `rustc_abi` build on stable) - rust-lang#120039 (pat_analysis: Don't rely on contiguous `VariantId`s outside of rustc) - rust-lang#120044 (Fix typo in comments (in_place_collect)) - rust-lang#120056 (Use FnOnceOutput instead of FnOnce where expected) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#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 rust-lang#115105 (comment)) 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 (rust-lang#115105 (comment)). cc ````@JakobDegen````
Enable DestinationPropagation by default. ~~Based on rust-lang#115291 This PR proposes to enable the destination propagation pass by default. This pass is meant to reduce the amount of copies present in MIR. At the same time, this PR removes the `RenameReturnPlace` pass, as it is currently unsound. `DestinationPropagation` is not limited to `_0`, but does not handle borrowed locals.
Enable DestinationPropagation by default. ~~Based on rust-lang#115291 This PR proposes to enable the destination propagation pass by default. This pass is meant to reduce the amount of copies present in MIR. At the same time, this PR removes the `RenameReturnPlace` pass, as it is currently unsound. `DestinationPropagation` is not limited to `_0`, but does not handle borrowed locals.
Enable DestinationPropagation by default. ~~Based on rust-lang/rust#115291 This PR proposes to enable the destination propagation pass by default. This pass is meant to reduce the amount of copies present in MIR. At the same time, this PR removes the `RenameReturnPlace` pass, as it is currently unsound. `DestinationPropagation` is not limited to `_0`, but does not handle borrowed locals.
Enable DestinationPropagation by default. ~~Based on rust-lang/rust#115291 This PR proposes to enable the destination propagation pass by default. This pass is meant to reduce the amount of copies present in MIR. At the same time, this PR removes the `RenameReturnPlace` pass, as it is currently unsound. `DestinationPropagation` is not limited to `_0`, but does not handle borrowed locals.
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 onucd
anddiesel
. (See #115105 (comment))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)
.Proof
We denote by$\varphi$ be a mapping of locals, which maps $out'(statement)$ , and the proposed liveness set is $\varphi(out(statement))$ .
'
all the quantities of the transformed program. Letsource
todestination
, and is identity otherwise. The exact liveness set after a statement isConsider 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.
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.$\varphi(in)$ is disjoint from $\varphi(kill)$ .$\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'$ .$out' \subset \varphi(out) \implies in' \subset \varphi(in)$ .
From soundness requirement 3,
This implies that
We can conclude that
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.
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 (#115105 (comment)).
cc @JakobDegen