Skip to content

Commit d9160ff

Browse files
DavidSpicketttstellar
authored andcommitted
[globalopt] Don't emit DWARF fragments for members
of a struct that cover the whole struct This can happen when the rest of the members of are zero length. Following the same pattern applied to the SROA pass in: d7f6f16 Fixes: https://bugs.llvm.org/show_bug.cgi?id=45335 Differential Revision: https://reviews.llvm.org/D78720 (cherry picked from commit 3929429)
1 parent bace7be commit d9160ff

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,14 +450,19 @@ static bool CanDoGlobalSRA(GlobalVariable *GV) {
450450
/// Copy over the debug info for a variable to its SRA replacements.
451451
static void transferSRADebugInfo(GlobalVariable *GV, GlobalVariable *NGV,
452452
uint64_t FragmentOffsetInBits,
453-
uint64_t FragmentSizeInBits,
454-
unsigned NumElements) {
453+
uint64_t FragmentSizeInBits) {
455454
SmallVector<DIGlobalVariableExpression *, 1> GVs;
456455
GV->getDebugInfo(GVs);
457456
for (auto *GVE : GVs) {
458457
DIVariable *Var = GVE->getVariable();
458+
Optional<uint64_t> VarSize = Var->getSizeInBits();
459+
459460
DIExpression *Expr = GVE->getExpression();
460-
if (NumElements > 1) {
461+
// If the FragmentSize is smaller than the variable,
462+
// emit a fragment expression.
463+
// If the variable size is unknown a fragment must be
464+
// emitted to be safe.
465+
if (!VarSize || FragmentSizeInBits < *VarSize) {
461466
if (auto E = DIExpression::createFragmentExpression(
462467
Expr, FragmentOffsetInBits, FragmentSizeInBits))
463468
Expr = *E;
@@ -539,8 +544,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
539544
// Copy over the debug info for the variable.
540545
uint64_t Size = DL.getTypeAllocSizeInBits(NGV->getValueType());
541546
uint64_t FragmentOffsetInBits = Layout.getElementOffsetInBits(ElementIdx);
542-
transferSRADebugInfo(GV, NGV, FragmentOffsetInBits, Size,
543-
STy->getNumElements());
547+
transferSRADebugInfo(GV, NGV, FragmentOffsetInBits, Size);
544548
} else if (SequentialType *STy = dyn_cast<SequentialType>(Ty)) {
545549
uint64_t EltSize = DL.getTypeAllocSize(ElTy);
546550
Align EltAlign(DL.getABITypeAlignment(ElTy));
@@ -553,7 +557,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
553557
if (NewAlign > EltAlign)
554558
NGV->setAlignment(NewAlign);
555559
transferSRADebugInfo(GV, NGV, FragmentSizeInBits * ElementIdx,
556-
FragmentSizeInBits, STy->getNumElements());
560+
FragmentSizeInBits);
557561
}
558562
}
559563

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
; RUN: opt -S -globalopt < %s | FileCheck %s
2+
; Generated at -O2 -g from:
3+
; typedef struct {
4+
; } a;
5+
; static struct {
6+
; long b;
7+
; a c;
8+
; } d;
9+
; e() {
10+
; long f = d.b + 1;
11+
; d.b = f;
12+
; }
13+
; (with some simplification by hand)
14+
15+
; Check that the global variable "d" is not
16+
; emitted as a fragment after the member "c"
17+
; is removed.
18+
; d.b is referenced, but d.c has zero length.
19+
; So a fragment covering d.b would be the same
20+
; size as d itself.
21+
22+
source_filename = "pr45335.c"
23+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
24+
target triple = "x86_64-unknown-linux-gnu"
25+
26+
%struct.anon = type { i64, %struct.a }
27+
%struct.a = type {}
28+
29+
; CHECK: @d.0 = internal unnamed_addr global i64 0, align 8, !dbg ![[GVE:.*]]
30+
@d = internal global %struct.anon zeroinitializer, align 8, !dbg !0
31+
32+
; Function Attrs: noinline nounwind uwtable
33+
define dso_local i32 @e() #0 !dbg !18 {
34+
entry:
35+
%0 = load i64, i64* getelementptr inbounds (%struct.anon, %struct.anon* @d, i32 0, i32 0), align 8
36+
%add = add nsw i64 %0, 1
37+
call void @llvm.dbg.value(metadata i64 %add, metadata !24, metadata !DIExpression()), !dbg !25
38+
store i64 %add, i64* getelementptr inbounds (%struct.anon, %struct.anon* @d, i32 0, i32 0), align 8
39+
ret i32 undef
40+
}
41+
42+
; Function Attrs: nounwind readnone speculatable willreturn
43+
declare void @llvm.dbg.declare(metadata, metadata, metadata)
44+
45+
; Function Attrs: nounwind readnone speculatable willreturn
46+
declare void @llvm.dbg.value(metadata, metadata, metadata)
47+
48+
!llvm.dbg.cu = !{!2}
49+
!llvm.module.flags = !{!14, !15}
50+
51+
; CHECK: ![[GVE]] = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
52+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
53+
!1 = distinct !DIGlobalVariable(name: "d", scope: !2, file: !3, line: 6, type: !7, isLocal: true, isDefinition: true)
54+
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !{}, globals: !{!0}, splitDebugInlining: false, nameTableKind: None)
55+
!3 = !DIFile(filename: "pr45335.c", directory: "/")
56+
!7 = distinct !DICompositeType(tag: DW_TAG_structure_type, file: !3, line: 3, size: 64, elements: !8)
57+
!8 = !{!9, !11}
58+
!9 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !7, file: !3, line: 4, baseType: !10, size: 64)
59+
!10 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed)
60+
!11 = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: !7, file: !3, line: 5, baseType: !12, offset: 64)
61+
!12 = !DIDerivedType(tag: DW_TAG_typedef, name: "a", file: !3, line: 2, baseType: !13)
62+
!13 = distinct !DICompositeType(tag: DW_TAG_structure_type, file: !3, line: 1, elements: !{})
63+
!14 = !{i32 7, !"Dwarf Version", i32 4}
64+
!15 = !{i32 2, !"Debug Info Version", i32 3}
65+
!18 = distinct !DISubprogram(name: "e", scope: !3, file: !3, line: 7, type: !19, scopeLine: 7, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !{})
66+
!19 = !DISubroutineType(types: !{!21})
67+
!21 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
68+
!24 = !DILocalVariable(name: "f", scope: !18, file: !3, line: 8, type: !10)
69+
!25 = !DILocation(line: 0, scope: !18)

0 commit comments

Comments
 (0)