Index: llvm/lib/Transforms/IPO/GlobalOpt.cpp =================================================================== --- llvm/lib/Transforms/IPO/GlobalOpt.cpp +++ llvm/lib/Transforms/IPO/GlobalOpt.cpp @@ -467,14 +467,16 @@ /// Copy over the debug info for a variable to its SRA replacements. static void transferSRADebugInfo(GlobalVariable *GV, GlobalVariable *NGV, uint64_t FragmentOffsetInBits, - uint64_t FragmentSizeInBits, - unsigned NumElements) { + uint64_t FragmentSizeInBits) { SmallVector GVs; GV->getDebugInfo(GVs); for (auto *GVE : GVs) { DIVariable *Var = GVE->getVariable(); + Optional VarSize = Var->getSizeInBits(); + DIExpression *Expr = GVE->getExpression(); - if (NumElements > 1) { + // Don't emit a fragment when it would cover the whole variable + if (!VarSize || FragmentSizeInBits < *VarSize) { if (auto E = DIExpression::createFragmentExpression( Expr, FragmentOffsetInBits, FragmentSizeInBits)) Expr = *E; @@ -556,8 +558,7 @@ // Copy over the debug info for the variable. uint64_t Size = DL.getTypeAllocSizeInBits(NGV->getValueType()); uint64_t FragmentOffsetInBits = Layout.getElementOffsetInBits(ElementIdx); - transferSRADebugInfo(GV, NGV, FragmentOffsetInBits, Size, - STy->getNumElements()); + transferSRADebugInfo(GV, NGV, FragmentOffsetInBits, Size); } else { uint64_t EltSize = DL.getTypeAllocSize(ElTy); Align EltAlign(DL.getABITypeAlignment(ElTy)); @@ -570,7 +571,7 @@ if (NewAlign > EltAlign) NGV->setAlignment(NewAlign); transferSRADebugInfo(GV, NGV, FragmentSizeInBits * ElementIdx, - FragmentSizeInBits, GetSRASequentialNumElements(Ty)); + FragmentSizeInBits); } } Index: llvm/test/DebugInfo/Generic/global-sra-struct-zero-length.ll =================================================================== --- /dev/null +++ llvm/test/DebugInfo/Generic/global-sra-struct-zero-length.ll @@ -0,0 +1,75 @@ +; RUN: opt -S -globalopt < %s | FileCheck %s +; Generated at -O2 -g from: +; typedef struct { +; } a; +; static struct { +; long b; +; a c +; } d; +; e() { +; long f = d.b + 1; +; d.b = f; +; } + +source_filename = "pr45335.c" +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +%struct.anon = type { i64, %struct.a } +%struct.a = type {} + +; CHECK: @d.0 = internal unnamed_addr global i64 0, align 8, !dbg ![[GVE:.*]] +@d = internal global %struct.anon zeroinitializer, align 8, !dbg !0 + +; Function Attrs: noinline nounwind uwtable +define dso_local i32 @e() #0 !dbg !18 { +entry: + %0 = load i64, i64* getelementptr inbounds (%struct.anon, %struct.anon* @d, i32 0, i32 0), align 8, !dbg !22 + %add = add nsw i64 %0, 1, !dbg !23 + call void @llvm.dbg.value(metadata i64 %add, metadata !24, metadata !DIExpression()), !dbg !25 + store i64 %add, i64* getelementptr inbounds (%struct.anon, %struct.anon* @d, i32 0, i32 0), align 8, !dbg !26 + ret i32 undef, !dbg !27 +} + +; Function Attrs: nounwind readnone speculatable willreturn +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +; Function Attrs: nounwind readnone speculatable willreturn +declare void @llvm.dbg.value(metadata, metadata, metadata) #1 + +attributes #0 = { noinline nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone speculatable willreturn } + +!llvm.dbg.cu = !{!2} +!llvm.module.flags = !{!14, !15, !16} +!llvm.ident = !{!17} + +; CHECK: ![[GVE]] = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression()) +!1 = distinct !DIGlobalVariable(name: "d", scope: !2, file: !6, line: 6, type: !7, isLocal: true, isDefinition: true) +!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 11.0.0 (https://github.com/llvm/llvm-project.git 41610d665013d716da245175ada1d9c5a8b79558)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5, splitDebugInlining: false, nameTableKind: None) +!3 = !DIFile(filename: "pr45335.c", directory: "/") +!4 = !{} +!5 = !{!0} +!6 = !DIFile(filename: "pr45335.c", directory: "") +!7 = distinct !DICompositeType(tag: DW_TAG_structure_type, file: !6, line: 3, size: 64, elements: !8) +!8 = !{!9, !11} +!9 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !7, file: !6, line: 4, baseType: !10, size: 64) +!10 = !DIBasicType(name: "long int", size: 64, encoding: DW_ATE_signed) +!11 = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: !7, file: !6, line: 5, baseType: !12, offset: 64) +!12 = !DIDerivedType(tag: DW_TAG_typedef, name: "a", file: !6, line: 2, baseType: !13) +!13 = distinct !DICompositeType(tag: DW_TAG_structure_type, file: !6, line: 1, elements: !4) +!14 = !{i32 7, !"Dwarf Version", i32 4} +!15 = !{i32 2, !"Debug Info Version", i32 3} +!16 = !{i32 1, !"wchar_size", i32 4} +!17 = !{!"clang version 11.0.0 (https://github.com/llvm/llvm-project.git 41610d665013d716da245175ada1d9c5a8b79558)"} +!18 = distinct !DISubprogram(name: "e", scope: !6, file: !6, line: 7, type: !19, scopeLine: 7, spFlags: DISPFlagDefinition, unit: !2, retainedNodes: !4) +!19 = !DISubroutineType(types: !20) +!20 = !{!21} +!21 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!22 = !DILocation(line: 8, column: 14, scope: !18) +!23 = !DILocation(line: 8, column: 16, scope: !18) +!24 = !DILocalVariable(name: "f", scope: !18, file: !6, line: 8, type: !10) +!25 = !DILocation(line: 0, scope: !18) +!26 = !DILocation(line: 9, column: 7, scope: !18) +!27 = !DILocation(line: 10, column: 1, scope: !18)