Skip to content

Commit 9b0e9ed

Browse files
amykhuangzmodem
authored andcommitted
[globalopt] Change so that emitting fragments doesn't use the type size of DIVariables
When turning on -debug-info-kind=constructor we ran into a "fragment covers entire variable" error during thinlto. The fragment is currently always emitted if there is no type size, but sometimes the variable has a forward declared struct type which doesn't have a size. This changes the code to get the type size from the GlobalVariable instead. Differential Revision: https://reviews.llvm.org/D85572 (cherry picked from commit 54b6cca)
1 parent 33c13cd commit 9b0e9ed

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -468,19 +468,16 @@ static bool CanDoGlobalSRA(GlobalVariable *GV) {
468468
/// Copy over the debug info for a variable to its SRA replacements.
469469
static void transferSRADebugInfo(GlobalVariable *GV, GlobalVariable *NGV,
470470
uint64_t FragmentOffsetInBits,
471-
uint64_t FragmentSizeInBits) {
471+
uint64_t FragmentSizeInBits,
472+
uint64_t VarSize) {
472473
SmallVector<DIGlobalVariableExpression *, 1> GVs;
473474
GV->getDebugInfo(GVs);
474475
for (auto *GVE : GVs) {
475476
DIVariable *Var = GVE->getVariable();
476-
Optional<uint64_t> VarSize = Var->getSizeInBits();
477-
478477
DIExpression *Expr = GVE->getExpression();
479478
// If the FragmentSize is smaller than the variable,
480479
// emit a fragment expression.
481-
// If the variable size is unknown a fragment must be
482-
// emitted to be safe.
483-
if (!VarSize || FragmentSizeInBits < *VarSize) {
480+
if (FragmentSizeInBits < VarSize) {
484481
if (auto E = DIExpression::createFragmentExpression(
485482
Expr, FragmentOffsetInBits, FragmentSizeInBits))
486483
Expr = *E;
@@ -505,6 +502,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
505502
assert(GV->hasLocalLinkage());
506503
Constant *Init = GV->getInitializer();
507504
Type *Ty = Init->getType();
505+
uint64_t VarSize = DL.getTypeSizeInBits(Ty);
508506

509507
std::map<unsigned, GlobalVariable *> NewGlobals;
510508

@@ -560,7 +558,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
560558
// Copy over the debug info for the variable.
561559
uint64_t Size = DL.getTypeAllocSizeInBits(NGV->getValueType());
562560
uint64_t FragmentOffsetInBits = Layout.getElementOffsetInBits(ElementIdx);
563-
transferSRADebugInfo(GV, NGV, FragmentOffsetInBits, Size);
561+
transferSRADebugInfo(GV, NGV, FragmentOffsetInBits, Size, VarSize);
564562
} else {
565563
uint64_t EltSize = DL.getTypeAllocSize(ElTy);
566564
Align EltAlign = DL.getABITypeAlign(ElTy);
@@ -573,7 +571,7 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const DataLayout &DL) {
573571
if (NewAlign > EltAlign)
574572
NGV->setAlignment(NewAlign);
575573
transferSRADebugInfo(GV, NGV, FragmentSizeInBits * ElementIdx,
576-
FragmentSizeInBits);
574+
FragmentSizeInBits, VarSize);
577575
}
578576
}
579577

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

0 commit comments

Comments
 (0)