Skip to content

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

@wesleywiser

Description

@wesleywiser

It looks like variables are being optimized away at mir-opt-level=2 when they should be retained for debugging purposes. For example, in the following program, a and b are not known to the debugger (on any platform):

pub fn example() {
    let a = 1;
    let b = 16;
    
    foo(a + b); // break here
}

#[inline(never)]
fn foo(x: i32) {
    std::process::exit(x);
}

This appears to be the result of MIR optimizations as going from -Zmir-opt-level=1 to -Zmir-opt-level=2 causes the calls to @llvm.dbg.value to disappear:

define void @_ZN7example7example17h6142df5531d284b7E() unnamed_addr #0 !dbg !6 {
start:
-  call void @llvm.dbg.value(metadata i32 1, metadata !12, metadata !DIExpression()), !dbg !18
-  call void @llvm.dbg.value(metadata i32 16, metadata !15, metadata !DIExpression()), !dbg !19
  tail call fastcc void @_ZN7example3foo17he136a32a31d586dbE(), !dbg !20
  unreachable, !dbg !21
}

godbolt

LLVM is able to perform this optimization while retaining debuginfo so we shouldn't interfere with that unless the user has opted into a higher level of MIR optimization where degrading debuggability is acceptable.

Metadata

Metadata

Assignees

Labels

A-debuginfoArea: Debugging information in compiled programs (DWARF, PDB, etc.)A-mir-optArea: MIR optimizationsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions