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

mir-opt: Merge all branch BBs into a single copy statement #129931

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

DianQK
Copy link
Member

@DianQK DianQK commented Sep 3, 2024

#128299 simplified

match a {
    Foo::A(x) => Foo::A(*x),
    Foo::B => Foo::B
}

to

match a {
    Foo::A(x) => a, // copy a
    Foo::B => Foo::B
}

The switch branch can be simplified into a single copy statement. This PR implements a relatively general simplification.

@rustbot
Copy link
Collaborator

rustbot commented Sep 3, 2024

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 3, 2024
@DianQK
Copy link
Member Author

DianQK commented Sep 3, 2024

r? ghost

@rustbot
Copy link
Collaborator

rustbot commented Sep 3, 2024

Failed to set assignee to ghost: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@DianQK
Copy link
Member Author

DianQK commented Sep 3, 2024

r? cjgillot

@rustbot rustbot assigned cjgillot and unassigned nnethercote Sep 3, 2024
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Sep 14, 2024

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

@DianQK
Copy link
Member Author

DianQK commented Sep 14, 2024

@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 14, 2024
@bors
Copy link
Contributor

bors commented Sep 14, 2024

⌛ Trying commit 622247a with merge c324112...

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 14, 2024
Merge these copy statements that simplified the canonical enum clone method by GVN

This is blocked by rust-lang#128299.
@bors
Copy link
Contributor

bors commented Sep 14, 2024

☀️ Try build successful - checks-actions
Build commit: c324112 (c3241126e94d7a53a1805ad854686e0bd061b5df)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (c324112): comparison URL.

Overall result: no relevant changes - no 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.

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

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (primary 0.9%)

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)
4.0% [3.0%, 5.1%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.8% [-5.9%, -1.7%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.9% [-5.9%, 5.1%] 5

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

Results (primary 0.0%, secondary -0.0%)

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.1% [0.0%, 0.2%] 11
Regressions ❌
(secondary)
0.0% [0.0%, 0.1%] 2
Improvements ✅
(primary)
-0.1% [-0.3%, -0.0%] 6
Improvements ✅
(secondary)
-0.0% [-0.0%, -0.0%] 3
All ❌✅ (primary) 0.0% [-0.3%, 0.2%] 17

Bootstrap: 760.71s -> 756.595s (-0.54%)
Artifact size: 341.13 MiB -> 341.17 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 15, 2024
Copy link
Contributor

@cjgillot cjgillot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My high-level reaction to this PR is that it's both too specific in what it's trying to match (a clone impl) and too general in its implementation (handles storage statements...).

I suggest having two heuristics:

  • for clone impls, use some kind of StructuralClone trait so that we fully replace the derived impls Clone with simpler MIR;
  • for general MIR, lift the restrictions on this pass (bb0 and assignment to _0 namely).

What do you think?

@@ -604,6 +602,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&dead_store_elimination::DeadStoreElimination::Initial,
&gvn::GVN,
&simplify::SimplifyLocals::AfterGVN,
&match_branches::MatchBranchSimplification,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this run on clone shims too? Or do we want a trait-based solution to detect trivial clone impls?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this run on clone shims too?

Could you explain more?

Or do we want a trait-based solution to detect trivial clone impls?

It makes sense to me for clone impls.

compiler/rustc_mir_transform/src/match_branches.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_transform/src/match_branches.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_transform/src/match_branches.rs Outdated Show resolved Hide resolved
@DianQK
Copy link
Member Author

DianQK commented Sep 18, 2024

* for clone impls, use some kind of `StructuralClone` trait so that we fully replace the derived impls `Clone` with simpler MIR;

* for general MIR, lift the restrictions on this pass (bb0 and assignment to _0 namely).

What do you think?

It looks like makes sense.
IIUC, the StructuralClone is exact matching clone shims? I expect this can improve compile-time.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 18, 2024
Copy link
Member Author

@DianQK DianQK left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't submit all the comments through "Submit review"; there are still some pending comments. o

I am currently modifying the code to move it into a separate pass.

compiler/rustc_mir_transform/src/match_branches.rs Outdated Show resolved Hide resolved
@@ -604,6 +602,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&dead_store_elimination::DeadStoreElimination::Initial,
&gvn::GVN,
&simplify::SimplifyLocals::AfterGVN,
&match_branches::MatchBranchSimplification,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this run on clone shims too?

Could you explain more?

Or do we want a trait-based solution to detect trivial clone impls?

It makes sense to me for clone impls.

@DianQK DianQK changed the title Merge these copy statements that simplified the canonical enum clone method by GVN mir-opt: Merge all branch BBs into a single copy statement Oct 5, 2024
@bors
Copy link
Contributor

bors commented Oct 6, 2024

⌛ Trying commit 7846743 with merge 9637884...

@bors
Copy link
Contributor

bors commented Oct 6, 2024

☀️ Try build successful - checks-actions
Build commit: 9637884 (963788449d01d434fa9a69b331e4f5c41f10eab4)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9637884): comparison URL.

Overall result: ❌✅ regressions and improvements - no 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.

@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.5% [0.3%, 0.8%] 2
Regressions ❌
(secondary)
0.5% [0.5%, 0.5%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.5% [-0.5%, -0.5%] 1
All ❌✅ (primary) 0.5% [0.3%, 0.8%] 2

Max RSS (memory usage)

Results (primary 3.2%, secondary 2.5%)

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.2% [2.2%, 4.7%] 4
Regressions ❌
(secondary)
2.5% [2.5%, 2.5%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.2% [2.2%, 4.7%] 4

Cycles

Results (primary 1.9%)

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% [1.0%, 2.8%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.9% [1.0%, 2.8%] 2

Binary size

Results (primary -0.0%, secondary -0.0%)

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.1%, 0.4%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.4%, -0.0%] 8
Improvements ✅
(secondary)
-0.0% [-0.0%, -0.0%] 1
All ❌✅ (primary) -0.0% [-0.4%, 0.4%] 11

Bootstrap: 775.25s -> 775.189s (-0.01%)
Artifact size: 329.52 MiB -> 329.45 MiB (-0.02%)

@rustbot rustbot removed S-waiting-on-perf Status: Waiting on a perf run to be completed. perf-regression Performance regression. labels Oct 6, 2024
@DianQK
Copy link
Member Author

DianQK commented Oct 7, 2024

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 7, 2024
compiler/rustc_mir_transform/src/merge_branches.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_transform/src/merge_branches.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_transform/src/merge_branches.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_transform/src/merge_branches.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_transform/src/merge_branches.rs Outdated Show resolved Hide resolved
compiler/rustc_mir_transform/src/merge_branches.rs Outdated Show resolved Hide resolved
Some(dest_place)
}

fn find_copy_assign<'tcx>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you doc-comment this? I'm still not sure what pattern you are looking for.

// There is only one statement that cannot be ignored
// that can be used as an expected copy statement.
expected_assign_stmt = Some(statement_index);
lived_stmts.remove(statement_index);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we already discussed this, but I'd rather have DSE run multiple times in the pipeline, than having re-coded here. This complicates the code a lot, where we could get rid of the statements once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since debuginfo handles it differently, I'll try to modify the DSE then get rid of these.

compiler/rustc_mir_transform/src/merge_branches.rs Outdated Show resolved Hide resolved
@@ -609,6 +610,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&dead_store_elimination::DeadStoreElimination::Initial,
&gvn::GVN,
&simplify::SimplifyLocals::AfterGVN,
&merge_branches::MergeBranchSimplification,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why here in the pipeline?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to run after GVN. I expect SimplifyLocals::AfterGVN can simplify the analysis in MergeBranchSimplification. Additionally, after MergeBranchSimplification simplifies the CFG, it will benefit subsequent passes that are related to the CFG.

@DianQK
Copy link
Member Author

DianQK commented Oct 16, 2024

I will probably update in two days.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 16, 2024
@DianQK
Copy link
Member Author

DianQK commented Oct 17, 2024

Just a few trivial modifications and rebase.

@DianQK
Copy link
Member Author

DianQK commented Oct 20, 2024

I have tried to update the document and DSE.

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 20, 2024
@bors
Copy link
Contributor

bors commented Oct 30, 2024

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

@DianQK
Copy link
Member Author

DianQK commented Oct 30, 2024

Blocked by #132353.

@rustbot author
@rustbot label +S-blocked

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-blocked Status: Marked as blocked ❌ on something else such as an RFC or other implementation work. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

8 participants