Skip to content
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

Implement constant propagation on top of MIR SSA analysis #116012

Merged
merged 6 commits into from
Dec 30, 2023

Conversation

cjgillot
Copy link
Contributor

This implements the idea I proposed in #110719 (comment)

Based on #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 #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."

r? @oli-obk

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 20, 2023
@rustbot
Copy link
Collaborator

rustbot commented Sep 20, 2023

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

The Miri subtree was changed

cc @rust-lang/miri

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Sep 21, 2023

☔ The latest upstream changes (presumably #116027) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot
Copy link
Collaborator

rustbot commented Sep 21, 2023

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

The Miri subtree was changed

cc @rust-lang/miri

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Sep 22, 2023

☔ The latest upstream changes (presumably #115696) made this pull request unmergeable. Please resolve the merge conflicts.

@rust-log-analyzer

This comment has been minimized.

@cjgillot
Copy link
Contributor Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 23, 2023
@bors
Copy link
Contributor

bors commented Sep 23, 2023

⌛ Trying commit 81d5ac4 with merge 7b24347...

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 23, 2023
Implement constant propagation on top of MIR SSA analysis

This implements the idea I proposed in rust-lang#110719 (comment)

Based on rust-lang#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 rust-lang#109597.

From this abstract representation, we can perform more involved simplifications, for example in rust-lang#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.](rust-lang@2767c49)"

r? `@oli-obk`
@bors
Copy link
Contributor

bors commented Sep 23, 2023

☀️ Try build successful - checks-actions
Build commit: 7b24347 (7b2434719a7e59a9df73c635255e0ca739d29a4c)

1 similar comment
@bors
Copy link
Contributor

bors commented Sep 23, 2023

☀️ Try build successful - checks-actions
Build commit: 7b24347 (7b2434719a7e59a9df73c635255e0ca739d29a4c)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (7b24347): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.4% [0.4%, 0.4%] 1
Regressions ❌
(secondary)
2.2% [1.0%, 2.9%] 4
Improvements ✅
(primary)
-0.6% [-1.6%, -0.3%] 13
Improvements ✅
(secondary)
-0.4% [-0.9%, -0.2%] 13
All ❌✅ (primary) -0.6% [-1.6%, 0.4%] 14

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.9% [0.2%, 3.0%] 3
Regressions ❌
(secondary)
5.0% [2.5%, 6.4%] 8
Improvements ✅
(primary)
-3.1% [-3.5%, -2.7%] 3
Improvements ✅
(secondary)
-3.1% [-5.1%, -0.8%] 5
All ❌✅ (primary) -0.6% [-3.5%, 3.0%] 6

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.3% [2.3%, 4.5%] 4
Improvements ✅
(primary)
-1.3% [-1.6%, -1.0%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.3% [-1.6%, -1.0%] 2

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.3% [0.0%, 1.0%] 58
Regressions ❌
(secondary)
0.1% [0.0%, 0.4%] 17
Improvements ✅
(primary)
-0.6% [-4.8%, -0.0%] 32
Improvements ✅
(secondary)
-0.6% [-2.4%, -0.0%] 19
All ❌✅ (primary) -0.0% [-4.8%, 1.0%] 90

Bootstrap: 634.28s -> 634.677s (0.06%)
Artifact size: 317.90 MiB -> 317.85 MiB (-0.02%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Sep 23, 2023
@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Dec 30, 2023
@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@saethlin
Copy link
Member

Looks to me like the timeout was in uploading to s3

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 30, 2023
@bors
Copy link
Contributor

bors commented Dec 30, 2023

⌛ Testing commit 6d7f063 with merge 8d76d07...

@bors
Copy link
Contributor

bors commented Dec 30, 2023

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing 8d76d07 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 30, 2023
@bors bors merged commit 8d76d07 into rust-lang:master Dec 30, 2023
12 checks passed
@rustbot rustbot added this to the 1.77.0 milestone Dec 30, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (8d76d07): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

Next Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please open an issue or create a new PR that fixes the regressions, add a comment linking to the newly created issue or PR, and then add the perf-regression-triaged label to this PR.

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.7% [0.2%, 1.9%] 10
Regressions ❌
(secondary)
0.6% [0.2%, 1.1%] 15
Improvements ✅
(primary)
-0.6% [-2.6%, -0.2%] 26
Improvements ✅
(secondary)
-31.2% [-62.1%, -0.2%] 8
All ❌✅ (primary) -0.2% [-2.6%, 1.9%] 36

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
3.1% [0.4%, 8.5%] 8
Regressions ❌
(secondary)
4.2% [3.3%, 5.9%] 4
Improvements ✅
(primary)
-2.6% [-5.4%, -0.5%] 10
Improvements ✅
(secondary)
-8.9% [-43.4%, -0.7%] 23
All ❌✅ (primary) -0.1% [-5.4%, 8.5%] 18

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.0% [-1.6%, -0.5%] 5
Improvements ✅
(secondary)
-56.6% [-70.9%, -8.8%] 5
All ❌✅ (primary) -1.0% [-1.6%, -0.5%] 5

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.3% [0.0%, 1.6%] 59
Regressions ❌
(secondary)
0.2% [0.0%, 0.4%] 12
Improvements ✅
(primary)
-0.5% [-4.0%, -0.0%] 28
Improvements ✅
(secondary)
-2.3% [-5.3%, -0.0%] 15
All ❌✅ (primary) 0.1% [-4.0%, 1.6%] 87

Bootstrap: 670.291s -> 668.991s (-0.19%)
Artifact size: 312.00 MiB -> 312.11 MiB (0.03%)

@cjgillot cjgillot deleted the gvn-const branch December 30, 2023 11:57
@rylev
Copy link
Member

rylev commented Jan 2, 2024

It seems given the positives for this change and the fact that improvements outweigh regressions, we can call this triaged.

@rustbot label: +perf-regression-triaged

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Jan 2, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 17, 2024
Fold arithmetic identities in GVN

Extracted from rust-lang#111344

This PR implements a few arithmetic folds for unary and binary operations.
This should take care of the missed optimizations introduced by rust-lang#116012.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 17, 2024
Fold arithmetic identities in GVN

Extracted from rust-lang#111344

This PR implements a few arithmetic folds for unary and binary operations.
This should take care of the missed optimizations introduced by rust-lang#116012.
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jan 18, 2024
Fold arithmetic identities in GVN

Extracted from rust-lang/rust#111344

This PR implements a few arithmetic folds for unary and binary operations.
This should take care of the missed optimizations introduced by rust-lang/rust#116012.
@RalfJung
Copy link
Member

I'm only just seeing this... this is amazing, old const-prop is gone. :) Thanks a ton @cjgillot!

@gurry gurry mentioned this pull request Feb 18, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 18, 2024
…i-obk

Remove const_prop.rs

Removed const_prop.rs and moved its contents to const_prop_lint.rs and dataflow_const_prop.rs where they are used.

const_prop.rs does not actually do any const propagation any more. After rust-lang#116012 all it contains is code that is used by const_prop_lint.rs and one macro that is used by dataflow_const_prop.rs. So it made sense to just move it to those two places and remove this file.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 18, 2024
Rollup merge of rust-lang#121260 - gurry:constprop-lint-cleanup, r=oli-obk

Remove const_prop.rs

Removed const_prop.rs and moved its contents to const_prop_lint.rs and dataflow_const_prop.rs where they are used.

const_prop.rs does not actually do any const propagation any more. After rust-lang#116012 all it contains is code that is used by const_prop_lint.rs and one macro that is used by dataflow_const_prop.rs. So it made sense to just move it to those two places and remove this file.
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Apr 7, 2024
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this pull request Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir-opt Area: MIR optimizations merged-by-bors This PR was explicitly merged by bors. perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.