Skip to content

Commit 46bf5d5

Browse files
authored
Globalopt pass produces invalid debug info (#100654)
This patch fixes an issue in the GlobalOpt pass where deleting a global variable fails to update the corresponding dbg.value and it references an empty metadata entry. The SalvageDebugInfo() function has been updated to handle dbg.value intrinsic when globals are deleted.
1 parent 47d831f commit 46bf5d5

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

llvm/lib/IR/Metadata.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ void ReplaceableMetadataImpl::SalvageDebugInfo(const Constant &C) {
346346
MetadataTracking::OwnerTy Owner = Pair.second.first;
347347
if (!Owner)
348348
continue;
349+
// Check for MetadataAsValue.
350+
if (isa<MetadataAsValue *>(Owner)) {
351+
cast<MetadataAsValue *>(Owner)->handleChangedMetadata(
352+
ValueAsMetadata::get(UndefValue::get(C.getType())));
353+
continue;
354+
}
349355
if (!isa<Metadata *>(Owner))
350356
continue;
351357
auto *OwnerMD = dyn_cast_if_present<MDNode>(cast<Metadata *>(Owner));

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,7 @@ deleteIfDead(GlobalValue &GV,
13381338
if (DeleteFnCallback)
13391339
DeleteFnCallback(*F);
13401340
}
1341+
ReplaceableMetadataImpl::SalvageDebugInfo(GV);
13411342
GV.eraseFromParent();
13421343
++NumDeleted;
13431344
return true;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
; RUN: opt -S -passes=globalopt --experimental-debuginfo-iterators=false <%s | FileCheck %s
2+
; CHECK: #dbg_value(ptr undef,
3+
; CHECK-SAME: [[VAR:![0-9]+]],
4+
; CHECK-SAME: !DIExpression()
5+
; CHECK: [[VAR]] = !DILocalVariable(name: "_format"
6+
7+
8+
; ModuleID = '<stdin>'
9+
source_filename = "test.cpp"
10+
11+
@_ZZZZ4main_format = internal constant [24 x i8] c"Result1: Hello, World!\0A\00", align 16, !dbg !9
12+
13+
define void @foo() align 2 !dbg !5 {
14+
entry:
15+
call void @llvm.dbg.value(metadata ptr @_ZZZZ4main_format, metadata !11, metadata !DIExpression()), !dbg !12
16+
ret void
17+
}
18+
19+
!llvm.dbg.cu = !{!0}
20+
!llvm.module.flags = !{!3, !4}
21+
22+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang", isOptimized: true, emissionKind: FullDebug)
23+
!1 = !DIFile(filename: "test.cpp", directory: "/path/to")
24+
!2 = !{}
25+
!3 = !{i32 7, !"Dwarf Version", i32 4}
26+
!4 = !{i32 2, !"Debug Info Version", i32 3}
27+
!5 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 27, type: !6, scopeLine: 27, spFlags: DISPFlagDefinition, unit: !0)
28+
!6 = !DISubroutineType(types: !7)
29+
!7 = !{null}
30+
!8 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
31+
!9 = !DIGlobalVariableExpression(var: !10, expr: !DIExpression())
32+
!10 = distinct !DIGlobalVariable(name: "_format", scope: !5, file: !1, line: 49, type: !8, isLocal: true, isDefinition: true)
33+
!11 = !DILocalVariable(name: "_format", arg: 1, scope: !5, file: !1, line: 79, type: !8)
34+
!12 = !DILocation(line: 0, scope: !5)

0 commit comments

Comments
 (0)