Skip to content

Introduce debuginfo to statements in MIR #142771

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

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

Conversation

dianqk
Copy link
Member

@dianqk dianqk commented Jun 20, 2025

The PR introduces support for debug information within dead statements. Currently, only the reference statement is supported, which is sufficient to fix #128081.

I don't modify Stable MIR, as I don't think we need debug information when using it.

This PR represents the debug information for the dead reference statement via #dbg_value. For example, let _foo_b = &foo.b becomes #dbg_value(ptr %foo, !22, !DIExpression(DW_OP_plus_uconst, 4, DW_OP_stack_value), !26). You can see this here: https://rust.godbolt.org/z/d43js6adv.

The general principle for handling debug information is to never provide less debug information than the optimized LLVM IR.

The current rules for dropping debug information in this PR are:

I haven't drop debuginfos in MatchBranchSimplification, because LLVM also pick one branch for it.

For the perf result:

I expected this to introduce some regressions; however, the results mixed the effects of inlining. Looking at the doc profile, this is a clear optimization. One potential regression I'm investigating is serde-1.0.219-debug-full.

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 20, 2025
@rust-log-analyzer

This comment has been minimized.

@dianqk dianqk force-pushed the mir-stmt-debuginfo branch from 15c968a to 6b013d4 Compare June 20, 2025 08:01
@rust-log-analyzer

This comment has been minimized.

@dianqk dianqk force-pushed the mir-stmt-debuginfo branch from 6b013d4 to 51576e7 Compare June 20, 2025 09:23
@rust-log-analyzer

This comment has been minimized.

@dianqk
Copy link
Member Author

dianqk commented Jun 20, 2025

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jun 20, 2025

⌛ Trying commit 51576e7 with merge eb83156

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jun 20, 2025
Introduce debuginfo to statements in MIR

Not ready for reviewing. Something known:

- [ ] Retain debuginfo when concatenating bbs
- [ ] Document about when to drop debuginfos (don't be worse than the optimized LLVM IR)
- [ ] Missing tests

r? ghost
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 20, 2025
@rust-bors
Copy link

rust-bors bot commented Jun 20, 2025

☀️ Try build successful (CI)
Build commit: eb83156 (eb83156169ae3bbdd1385d498455dbc44283f5ff, parent: 18491d5be00eb3ed2f1ccee2ac5b792694f2a7a0)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (eb83156): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.9% [0.2%, 14.5%] 51
Regressions ❌
(secondary)
0.8% [0.1%, 1.6%] 52
Improvements ✅
(primary)
-0.4% [-2.1%, -0.2%] 35
Improvements ✅
(secondary)
-0.3% [-0.5%, -0.2%] 39
All ❌✅ (primary) 0.4% [-2.1%, 14.5%] 86

Max RSS (memory usage)

Results (primary 2.3%, secondary 3.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.3% [0.8%, 4.7%] 54
Regressions ❌
(secondary)
3.6% [0.7%, 7.4%] 38
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.3% [-3.3%, -3.3%] 1
All ❌✅ (primary) 2.3% [0.8%, 4.7%] 54

Cycles

Results (primary 3.2%, secondary 2.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
3.9% [1.1%, 14.4%] 7
Regressions ❌
(secondary)
2.5% [2.0%, 3.3%] 3
Improvements ✅
(primary)
-2.0% [-2.0%, -2.0%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.2% [-2.0%, 14.4%] 8

Binary size

Results (primary 0.5%, secondary 0.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.7% [0.0%, 2.2%] 123
Regressions ❌
(secondary)
1.1% [0.0%, 3.5%] 59
Improvements ✅
(primary)
-0.9% [-6.5%, -0.1%] 11
Improvements ✅
(secondary)
-0.5% [-13.0%, -0.1%] 55
All ❌✅ (primary) 0.5% [-6.5%, 2.2%] 134

Bootstrap: 691.482s -> 692.445s (0.14%)
Artifact size: 371.94 MiB -> 372.12 MiB (0.05%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jun 20, 2025
@dianqk dianqk force-pushed the mir-stmt-debuginfo branch from 51576e7 to e72c3ae Compare June 21, 2025 02:31
@dianqk
Copy link
Member Author

dianqk commented Jun 21, 2025

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jun 21, 2025

⌛ Trying commit e72c3ae with merge 77d5c6a

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jun 21, 2025
Introduce debuginfo to statements in MIR

Not ready for reviewing. Something known:

- [ ] Retain debuginfo when concatenating bbs
- [ ] Document about when to drop debuginfos (don't be worse than the optimized LLVM IR)
- [ ] Missing tests

r? ghost
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 21, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jun 21, 2025

☀️ Try build successful (CI)
Build commit: 77d5c6a (77d5c6a20a77fabdee3790a412618b82178e9ab4, parent: 15c701fbc995eb6c5b3a86021c18185f8eee020d)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (77d5c6a): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.9% [0.1%, 14.5%] 53
Regressions ❌
(secondary)
0.8% [0.1%, 1.6%] 50
Improvements ✅
(primary)
-0.4% [-3.0%, -0.2%] 46
Improvements ✅
(secondary)
-0.3% [-0.5%, -0.2%] 41
All ❌✅ (primary) 0.3% [-3.0%, 14.5%] 99

Max RSS (memory usage)

Results (primary 2.4%, secondary 3.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.4% [0.5%, 5.7%] 38
Regressions ❌
(secondary)
4.2% [1.3%, 6.2%] 25
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.7% [-3.0%, -1.1%] 4
All ❌✅ (primary) 2.4% [0.5%, 5.7%] 38

Cycles

Results (primary 2.1%, secondary 0.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
3.0% [0.8%, 14.1%] 10
Regressions ❌
(secondary)
3.3% [2.3%, 4.3%] 2
Improvements ✅
(primary)
-2.3% [-2.9%, -1.7%] 2
Improvements ✅
(secondary)
-2.2% [-2.6%, -1.9%] 2
All ❌✅ (primary) 2.1% [-2.9%, 14.1%] 12

Binary size

Results (primary 0.5%, secondary 0.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.7% [0.0%, 3.0%] 122
Regressions ❌
(secondary)
1.1% [0.0%, 3.5%] 59
Improvements ✅
(primary)
-0.7% [-4.9%, -0.0%] 15
Improvements ✅
(secondary)
-0.5% [-12.7%, -0.0%] 55
All ❌✅ (primary) 0.5% [-4.9%, 3.0%] 137

Bootstrap: 690.617s -> 691.47s (0.12%)
Artifact size: 371.83 MiB -> 372.01 MiB (0.05%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 21, 2025
@bors
Copy link
Collaborator

bors commented Jun 25, 2025

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

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (61f71d4): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.5% [0.1%, 2.7%] 27
Regressions ❌
(secondary)
0.5% [0.0%, 0.9%] 37
Improvements ✅
(primary)
-0.3% [-0.8%, -0.1%] 62
Improvements ✅
(secondary)
-0.3% [-1.1%, -0.0%] 74
All ❌✅ (primary) -0.1% [-0.8%, 2.7%] 89

Max RSS (memory usage)

Results (primary 2.0%, secondary 4.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.1% [0.7%, 5.2%] 71
Regressions ❌
(secondary)
4.0% [1.7%, 5.8%] 38
Improvements ✅
(primary)
-3.5% [-4.0%, -3.0%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.0% [-4.0%, 5.2%] 73

Cycles

Results (primary -0.6%, secondary 3.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.7% [2.7%, 2.7%] 1
Regressions ❌
(secondary)
5.0% [2.3%, 7.3%] 8
Improvements ✅
(primary)
-2.3% [-2.3%, -2.3%] 2
Improvements ✅
(secondary)
-5.2% [-5.2%, -5.2%] 1
All ❌✅ (primary) -0.6% [-2.3%, 2.7%] 3

Binary size

Results (primary 0.5%, secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.7% [0.1%, 3.2%] 117
Regressions ❌
(secondary)
1.1% [0.0%, 3.6%] 57
Improvements ✅
(primary)
-0.6% [-6.6%, -0.1%] 19
Improvements ✅
(secondary)
-0.3% [-6.6%, -0.1%] 55
All ❌✅ (primary) 0.5% [-6.6%, 3.2%] 136

Bootstrap: 467.629s -> 469.243s (0.35%)
Artifact size: 376.97 MiB -> 376.97 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 3, 2025
let retain = f(stmt);
if retain {
if !debuginfos.is_empty() {
if !stmt.debuginfos.is_empty() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this check worth it?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. I have removed the second check.

self.statements.retain_mut(|stmt| f(stmt));
return;
}
let mut debuginfos = Vec::new();
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mind adding a comment saying so?

@@ -15,17 +15,33 @@ use crate::ty::CoroutineArgsExt;
pub struct Statement<'tcx> {
pub source_info: SourceInfo,
pub kind: StatementKind<'tcx>,
pub debuginfos: Vec<StmtDebugInfo<'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 add a comment explaining that those debuginfo are executed before the primary statement?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added:

/// Some debuginfos appearing before the primary statement.

location: Location
) {
self.super_statement_debuginfos(stmt_debuginfo, location);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need this method?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure but I removed.

@@ -1342,6 +1342,10 @@ pub struct BasicBlockData<'tcx> {
/// List of statements in this block.
pub statements: Vec<Statement<'tcx>>,

/// All debuginfos happen before the statement.
/// Put debuginfos here when the last statement is eliminated.
pub after_last_stmt_debuginfos: Vec<StmtDebugInfo<'tcx>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it after the last statement, or before the terminator? Why isn't it inside Terminator?

Copy link
Member Author

Choose a reason for hiding this comment

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

We have some passes that change the terminator, which means we also must hand debuginfos. I think this will make things more complicated.
I expect these debuginfos to be handled in simplifycfg as much as possible.

LocalRef::Operand(operand_ref) => match operand_ref.val {
OperandValue::Immediate(v) => {
Some(PlaceRef::new_sized(v, operand_ref.layout))
let assign_ref = if let Some(place) = place {
Copy link
Contributor

Choose a reason for hiding this comment

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

Use place.and_then(|place| to keep the previous logic?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have removed the Option.

@@ -958,5 +969,5 @@ impl RawPtrKind {

#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
pub enum StmtDebugInfo<'tcx> {
AssignRef(Local, Place<'tcx>),
AssignRef(Local, Option<Place<'tcx>>),
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not convinced by the benefit of allowing None here. Do you mind splitting this change from other tweaks to DSE, so we can see what it does in isolation?

Copy link
Member Author

Choose a reason for hiding this comment

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

Is 048f7eb what you're looking for?

self.always_used.contains(local)
);
// To keep things simple, we don't handle debugging information here, these are in DSE.
self.always_used.contains(local) || self.use_count[local] != 0
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather keep handling debuginfo in this general SimplifyLocals. It runs much more often than DSE.

Copy link
Member Author

Choose a reason for hiding this comment

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

Perhaps, but I don't want to make this PR any more complicated, and I'm concerned about the compile time.

@@ -718,6 +717,7 @@ pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
&strip_debuginfo::StripDebugInfo,
&copy_prop::CopyProp,
&dead_store_elimination::DeadStoreElimination::Final,
&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.

Do you mind submitting this change in a separate PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

I will drop this at the next update.

@@ -254,20 +244,20 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
current = target;
}
let last = current;
*changed |= *start != last;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mind submitting this bugfix in a standalone PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

@cjgillot cjgillot self-assigned this Aug 5, 2025
dianqk added 15 commits August 5, 2025 20:13
This may not be the best phase, but we do need a CFG simplification.
A potential improvement would be to implement a trivial simplification.
Add a test case with the same local.

```
// DBG: _1 = &_2
_1 = &_3
```
Add a test case with the same local.
Add a assertion for unsupported debug information.
Add a test case for identical successors.
…buginfos

Add a test case for a invalid place on debuginfos
…buginfos

- duplicate the StmtDebugInfo arm, a None and a Some version
- iter projections for invalid place
@rust-log-analyzer

This comment has been minimized.

@dianqk
Copy link
Member Author

dianqk commented Aug 6, 2025

I'll squash the fixups during the next rebase. 🙏

@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-19-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
---- [debuginfo-gdb] tests/debuginfo/opt/dead_refs.rs stdout ----

error: compilation failed!
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/debuginfo/opt/dead_refs.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "-C" "prefer-dynamic" "-o" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/debuginfo/opt/dead_refs.gdb/a" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-g" "-Copt-level=3"
stdout: none
--- stderr -------------------------------
error[E0557]: feature has been removed
##[error]  --> /checkout/tests/debuginfo/opt/dead_refs.rs:30:12
   |

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 6, 2025
…hain-changed, r=cjgillot

simplifycfg: Mark as changed when start is modified in collapse goto chain

Split from rust-lang#142771.
r? cjgillot
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 6, 2025
mir: Do not modify NonUse in `super_projection_elem`

Split from rust-lang#142771.
r? cjgillot
rust-timer added a commit that referenced this pull request Aug 7, 2025
Rollup merge of #144998 - dianqk:visit-no-use-proj, r=cjgillot

mir: Do not modify NonUse in `super_projection_elem`

Split from #142771.
r? cjgillot
rust-timer added a commit that referenced this pull request Aug 7, 2025
Rollup merge of #144996 - dianqk:simplifycfg-collapse_goto_chain-changed, r=cjgillot

simplifycfg: Mark as changed when start is modified in collapse goto chain

Split from #142771.
r? cjgillot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. perf-regression Performance regression. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bad codegen for non-copy-derived struct with all Copy derived fields
8 participants