Skip to content

Globalopt pass produces invalid debug info #100654

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

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/lib/IR/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@ void ReplaceableMetadataImpl::SalvageDebugInfo(const Constant &C) {
MetadataTracking::OwnerTy Owner = Pair.second.first;
if (!Owner)
continue;
// Check for MetadataAsValue.
if (isa<MetadataAsValue *>(Owner)) {
cast<MetadataAsValue *>(Owner)->handleChangedMetadata(
ValueAsMetadata::get(UndefValue::get(C.getType())));
continue;
}
if (!isa<Metadata *>(Owner))
continue;
auto *OwnerMD = dyn_cast_if_present<MDNode>(cast<Metadata *>(Owner));
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Transforms/IPO/GlobalOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,7 @@ deleteIfDead(GlobalValue &GV,
if (DeleteFnCallback)
DeleteFnCallback(*F);
}
ReplaceableMetadataImpl::SalvageDebugInfo(GV);
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 line necessary? I believe GV.removeDeadConstantUsers(); should already call this if GV is dead, as part of constantIsDead(..., /*RemoveDeadUsers=*/true).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The loop responsible for removing dead users and potentially calling constantIsDead() does not execute in this case. It looks like both the iterators I and E are null indicating it does not count debug intrinsic as users.

Copy link
Contributor

Choose a reason for hiding this comment

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

It might be more appropriate to put this into removeDeadConstantUsers then, after the loop - I think we'd want to perform the salvage step for any constant that dies with debug users regardless of the pass that does it. However, that might end up affecting other tests; if so it can easily be left for another patch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I can investigate more and follow up on a different patch if it's a viable approach.

GV.eraseFromParent();
++NumDeleted;
return true;
Expand Down
34 changes: 34 additions & 0 deletions llvm/test/DebugInfo/X86/undef-dbg-val.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
; RUN: opt -S -passes=globalopt --experimental-debuginfo-iterators=false <%s | FileCheck %s
; CHECK: #dbg_value(ptr undef,
; CHECK-SAME: [[VAR:![0-9]+]],
; CHECK-SAME: !DIExpression()
; CHECK: [[VAR]] = !DILocalVariable(name: "_format"


; ModuleID = '<stdin>'
source_filename = "test.cpp"

@_ZZZZ4main_format = internal constant [24 x i8] c"Result1: Hello, World!\0A\00", align 16, !dbg !9

define void @foo() align 2 !dbg !5 {
entry:
call void @llvm.dbg.value(metadata ptr @_ZZZZ4main_format, metadata !11, metadata !DIExpression()), !dbg !12
ret void
}

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4}

!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, emissionKind: FullDebug)
!1 = !DIFile(filename: "test.cpp", directory: "/path/to")
!2 = !{}
!3 = !{i32 7, !"Dwarf Version", i32 4}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 27, type: !6, scopeLine: 27, spFlags: DISPFlagDefinition, unit: !0)
!6 = !DISubroutineType(types: !7)
!7 = !{null}
!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!9 = !DIGlobalVariableExpression(var: !10, expr: !DIExpression())
!10 = distinct !DIGlobalVariable(name: "_format", scope: !5, file: !1, line: 49, type: !8, isLocal: true, isDefinition: true)
!11 = !DILocalVariable(name: "_format", arg: 1, scope: !5, file: !1, line: 79, type: !8)
!12 = !DILocation(line: 0, scope: !5)
Loading