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

Only preserve DebugInfo in DeadStoreElimination if requested. #106852

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

Conversation

cjgillot
Copy link
Contributor

@cjgillot cjgillot commented Jan 14, 2023

The current pass may delete assignments to locals that appear in debuginfo, irrespective of the opt-level. This PR attempts to preserve such assignments.

Fixes #103655

@rustbot
Copy link
Collaborator

rustbot commented Jan 14, 2023

Failed to set assignee to JakobDegen: invalid assignee

Note: Only org members, users with write permissions, or people who have commented on the PR may be assigned.

@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 Jan 14, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jan 14, 2023

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@cjgillot
Copy link
Contributor Author

r? wg-mir-opt

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

bors commented Jan 14, 2023

⌛ Trying commit 7e940fd36a93faafc4f402b0423816942a65fc8c with merge 7dc1f7ab3906a64a8ed85774edd3b3805670f534...

@bors
Copy link
Contributor

bors commented Jan 14, 2023

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (7dc1f7ab3906a64a8ed85774edd3b3805670f534): 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.8% [0.4%, 1.3%] 3
Regressions ❌
(secondary)
0.5% [0.3%, 0.7%] 8
Improvements ✅
(primary)
-0.5% [-0.8%, -0.3%] 7
Improvements ✅
(secondary)
-0.9% [-1.4%, -0.4%] 4
All ❌✅ (primary) -0.1% [-0.8%, 1.3%] 10

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.8% [0.2%, 3.5%] 4
Regressions ❌
(secondary)
2.3% [2.3%, 2.3%] 1
Improvements ✅
(primary)
-3.3% [-3.8%, -2.8%] 2
Improvements ✅
(secondary)
-2.3% [-2.3%, -2.3%] 1
All ❌✅ (primary) 0.1% [-3.8%, 3.5%] 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)
2.0% [2.0%, 2.0%] 1
Improvements ✅
(primary)
-0.9% [-0.9%, -0.9%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.9% [-0.9%, -0.9%] 1

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jan 14, 2023
analysis.initialize_start_block(body, &mut entry_sets[mir::START_BLOCK]);
} else {
// For backward analyses, initialize blocks that exit MIR.
analysis.initialize_start_block(body, &mut init_value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Using initialize_start_block for this feels weird. Can you instead treat locals with debuginfo as being read on return/resume terminators?

@JakobDegen
Copy link
Contributor

Treating locals as being live when the function returns feels like a bit of a weird solution. It will still cause the dead write to be removed in an example like this:

x = 5;
x = 10;
dbg!(x);

See also #103657 for a different approach.

As DSE has soundness implications on DestProp:

It does not anymore. If I accidentally left a comment in saying otherwise, feel free to remove it.

@cjgillot
Copy link
Contributor Author

Treating locals as being live when the function returns feels like a bit of a weird solution. It will still cause the dead write to be removed in an example like this:

x = 5;
x = 10;
dbg!(x);

That's a bit of the point. My proposal is to remove all the assignments that are "overwritten" without being read, but stop just before removing all assignments entirely. Effectively, this amounts to only keep the last assignment.

The debugger won't see the dead assignments, but will see the final value, and the variable will still exist. I'm tempted to consider this a middle ground between optimization and debuggability.

After all, debugging an optimizing build, one's bound to see pink elephants 😄

@cjgillot cjgillot force-pushed the debug-dse branch 2 times, most recently from 6951a9b to a17b591 Compare January 15, 2023 10:33
@JakobDegen
Copy link
Contributor

Hmm, ok, makes sense. Tbh, I think we should also block this on the same discussion as on the other PR. Once we have the T-compiler design meeting on this topic, we can write up an updated policy on the opt levels and make a decision at that point. Feels more principled than merging either of these PRs before that

@davidtwco
Copy link
Member

r? @JakobDegen

@bors
Copy link
Contributor

bors commented Feb 6, 2023

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

@wesleywiser wesleywiser added the S-blocked Status: Blocked on something else such as an RFC or other implementation work. label Feb 16, 2023
@rust-log-analyzer

This comment has been minimized.

@saethlin
Copy link
Member

saethlin commented May 26, 2024

(I'm going to see if I can finish up this PR)

I pushed a rebase because it produced a new test failure, which I've fixed. At least the first part of the test suite looks good locally now? Let's see...

@saethlin
Copy link
Member

@cjgillot This now passes CI. But I think you had some lingering concern about some sort of inconsistency? Up above, you said:

There are differences in the blessed output depending on the target. I haven't tracked where this comes from, probably a change in debuginfo flags somewhere.

But all the mir-opt test output looks good to me; is there some other kind of experiment or test you were doing locally?

@bors
Copy link
Contributor

bors commented Jun 3, 2024

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

@bors
Copy link
Contributor

bors commented Jun 11, 2024

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

@bors
Copy link
Contributor

bors commented Jun 17, 2024

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

scope 3 {
debug z => const 42_u32;
debug z => _3;
Copy link
Member

Choose a reason for hiding this comment

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

This seems strange? Feels like either we should remove the debuginfo for this variable or it should have the constant. Having debuginfo pointing to something that's never initialized feels weird at PreCodegen phase.

(It'd be fine as an intermediate thing, to be removed in a subsequent pass, but seems weird like this.)

Copy link
Member

Choose a reason for hiding this comment

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

Should we maybe DeadStoreElem after SingleUseConsts?

Copy link
Member

Choose a reason for hiding this comment

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

Sure, I pushed a commit that changes the pass order. I'm not sure it's always better, but it fixes this one problem.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Jun 29, 2024

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

@rust-log-analyzer

This comment has been minimized.

&gvn::GVN,
&simplify::SimplifyLocals::AfterGVN,
&dataflow_const_prop::DataflowConstProp,
&single_use_consts::SingleUseConsts,
&dead_store_elimination::DeadStoreElimination::Initial,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd rather see SingleUseConsts run multiple times than move DSE.
Having DSE run early is interesting to reduce work and create additional opportunities for GVN.
Furthermore, SingleUseConsts should be cheap enough to run multiple times, doesn't it?

Copy link
Member

Choose a reason for hiding this comment

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

SingleUseConsts is just O(statements), so should be fine to run multiple times.

Come to think of it, doing it after inlining and before InstSimplify probably makes sense, as otherwise I don't think that https://github.com/cjgillot/rust/blob/a299aa5df79dd8e5a1405b97ddd7b367b61eecc6/compiler/rustc_mir_transform/src/instsimplify.rs#L121 will realistically find anything to simplify.

Copy link
Member

Choose a reason for hiding this comment

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

🤔 I've tried to apply this concept, and it seems to have fixed a FIXME in the SingleUseConsts test?

@rust-log-analyzer

This comment has been minimized.

Comment on lines 10 to 11
- _1 = const <T as MyTrait>::ASSOC_BOOL;
- switchInt(move _1) -> [0: bb2, otherwise: bb1];
+ nop;
+ switchInt(const <T as MyTrait>::ASSOC_BOOL) -> [0: bb2, otherwise: bb1];
nop;
switchInt(const <T as MyTrait>::ASSOC_BOOL) -> [0: bb2, otherwise: bb1];
Copy link
Member

Choose a reason for hiding this comment

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

fix: I think if you're going to run it multiple times you need to make it an enum and manually give each run a different name. Otherwise the tests have nothing to see any more, like here where the diff file is the diff from the second run where nothing happens because it's the first run that did it, and we do want the diff to actually show a diff in the MIR here.

- debug my_bool => _1;
+ debug my_bool => const <T as MyTrait>::ASSOC_BOOL;
debug my_bool => const <T as MyTrait>::ASSOC_BOOL;
Copy link
Member

Choose a reason for hiding this comment

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

Ditto here; this test should be showing a change in the debuginfo for the pass.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-17 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
------
 > importing cache manifest from ghcr.io/rust-lang/rust-ci-cache:3aacb9c90579defe09351ac5e8ee504359f8054da6326ff19038f1b7c90e3cb2aafe33685c6d9b76ee8d2ccbd187ca80c46ab5380485abdd8c0ce7d69cd8d8fd:
------
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-17]
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-17', '--enable-llvm-link-shared', '--set', 'rust.thin-lto-import-instr-limit=10', '--set', 'change-id=99999999', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-17/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.thin-lto-import-instr-limit := 10
configure: change-id            := 99999999
---

warning: static `C_STR` is never used
##[warning] --> alloc_input.rs:8:12
  |
8 |     static C_STR: &std::ffi::CStr = c"cstr";
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `check_type_id` is never used

@bors
Copy link
Contributor

bors commented Jul 25, 2024

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

@alex-semenyuk alex-semenyuk 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 17, 2024
@alex-semenyuk alex-semenyuk 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 9, 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 perf-regression Performance regression. 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.

MIR optimizations at mir-opt-level=2 remove some debuginfo