Skip to content

Commit df4199c

Browse files
authored
[DebugInfo] Use correct unit when creating variable across CU boundary (#133282)
When creating a static member DIE, we place it in a potentially pre-existing context DIE, and that DIE might be located in a different CU if we're in an LTO context. When we then add the source-file-ID to the static member DIE, use the correct Unit to do so -- the one that owns the context DIE. Otherwise we might assign a file-ID from one CU to another, and there isn't a guarantee that they'll be the same file, or even exist. Fixes #109227 (I'd normally remove my home directory from these tests, but in this circumstances the same-file-but-with-a-different-name nature of the DIFile is part of the test).
1 parent 8094454 commit df4199c

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1942,13 +1942,14 @@ DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) {
19421942
if (DIE *StaticMemberDIE = getDIE(DT))
19431943
return StaticMemberDIE;
19441944

1945+
DwarfUnit *ContextUnit = static_cast<DwarfUnit *>(ContextDIE->getUnit());
19451946
DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT);
19461947

19471948
const DIType *Ty = DT->getBaseType();
19481949

19491950
addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName());
19501951
addType(StaticMemberDIE, Ty);
1951-
addSourceLine(StaticMemberDIE, DT);
1952+
ContextUnit->addSourceLine(StaticMemberDIE, DT);
19521953
addFlag(StaticMemberDIE, dwarf::DW_AT_external);
19531954
addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
19541955

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
; RUN: llc %s -filetype=obj -o - | llvm-dwarfdump -verify -
2+
; RUN: llc %s -filetype=obj -o - | llvm-dwarfdump -debug-info - | FileCheck %s
3+
4+
;; This test exercises the cross-CU production of type information. Due to
5+
;; unfortunate inputs, when the DW_TAG_variable for the global
6+
;; @a_struct_field8typeDataE is produced it will be done in the CU for 2.cpp.
7+
;; However, the type information for the "a_struct" structure type will have
8+
;; already been produced in 3.cpp's CU. Thus, we'll be creating a DIE in one
9+
;; CU from another CU. Check that when we do so, we get the source file
10+
;; identifier correct -- if the FileID came from the wrong CU then we'd end
11+
;; up with a false declaration file.
12+
;;
13+
;; Due to unrelated reasons there are two copies of the "typeData" static member
14+
;; in this LTO-linked IR -- if that ever gets fitxed, it's fine to drop the
15+
;; duplicate DW_TAG_variable, but we want to keep testing the DW_AT_decl_file
16+
;; that's created across a CU boundary.
17+
;;
18+
;; https://github.com/llvm/llvm-project/issues/109227
19+
20+
; CHECK-LABEL: DW_TAG_structure_type
21+
; CHECK-NOT: DW_TAG
22+
; CHECK: DW_AT_name ("a_struct")
23+
; CHECK-NOT: DW_TAG
24+
; CHECK: DW_TAG_variable
25+
; CHECK-NOT: DW_TAG
26+
; CHECK: DW_AT_decl_file ("C:\Users\gbmorsej\source\bees{{\\|/}}3.cpp")
27+
; CHECK-NOT: DW_TAG
28+
; CHECK: DW_TAG_variable
29+
; CHECK-NOT: DW_TAG
30+
; CHECK: DW_AT_decl_file ("C:\users\gbmorsej\source/bees{{\\|/}}2.cpp")
31+
32+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
33+
target triple = "x86_64-sie-ps5"
34+
35+
%struct.a_struct = type { i8 }
36+
37+
@__trans_tmp_2 = hidden local_unnamed_addr global %struct.a_struct zeroinitializer, align 1, !dbg !0
38+
@a_struct_field8typeDataE = hidden local_unnamed_addr global i64 0, align 8, !dbg !13
39+
40+
!llvm.dbg.cu = !{!2, !15}
41+
!llvm.linker.options = !{}
42+
!llvm.ident = !{!22, !22}
43+
!llvm.module.flags = !{!23, !24, !25, !26, !27, !28, !29, !30, !31, !32}
44+
45+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
46+
!1 = distinct !DIGlobalVariable(name: "__trans_tmp_2", scope: !2, file: !3, line: 9, type: !5, isLocal: false, isDefinition: true)
47+
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_20, file: !3, producer: "clang version 21.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !4, splitDebugInlining: false, debugInfoForProfiling: true, nameTableKind: None)
48+
!3 = !DIFile(filename: "3.cpp", directory: "C:\\Users\\gbmorsej\\source\\bees", checksumkind: CSK_MD5, checksum: "05973c817251e916cc8ba01e728764dc")
49+
!4 = !{!0}
50+
!5 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "a_struct", file: !3, line: 4, size: 8, flags: DIFlagTypePassByValue, elements: !6, identifier: "redacted_struct_name")
51+
!6 = !{!7}
52+
!7 = !DIDerivedType(tag: DW_TAG_variable, name: "typeData", scope: !5, file: !3, line: 6, baseType: !8, flags: DIFlagStaticMember)
53+
!8 = !DIDerivedType(tag: DW_TAG_typedef, name: "TypeData", scope: !10, file: !9, line: 6, baseType: !12)
54+
!9 = !DIFile(filename: ".\\h1.h", directory: "C:\\Users\\gbmorsej\\source\\bees", checksumkind: CSK_MD5, checksum: "0c90de8c8e867df533d869035e11cf8c")
55+
!10 = !DINamespace(name: "b", scope: !11)
56+
!11 = !DINamespace(name: "a", scope: null)
57+
!12 = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed)
58+
!13 = !DIGlobalVariableExpression(var: !14, expr: !DIExpression())
59+
!14 = distinct !DIGlobalVariable(name: "typeData", linkageName: "a_struct_field8typeDataE", scope: !15, file: !18, line: 10, type: !19, isLocal: false, isDefinition: true, declaration: !21)
60+
!15 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_20, file: !16, producer: "clang version 21.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !17, splitDebugInlining: false, debugInfoForProfiling: true, nameTableKind: None)
61+
!16 = !DIFile(filename: "C:\\users\\gbmorsej\\source/bees\\2.cpp", directory: "C:\\Users\\gbmorsej\\source\\bees", checksumkind: CSK_MD5, checksum: "231f25b4978512f0d961a1d7baa9cb01")
62+
!17 = !{!13}
63+
!18 = !DIFile(filename: "C:\\users\\gbmorsej\\source/bees/2.cpp", directory: "", checksumkind: CSK_MD5, checksum: "231f25b4978512f0d961a1d7baa9cb01")
64+
!19 = !DIDerivedType(tag: DW_TAG_typedef, name: "TypeData", scope: !10, file: !20, line: 6, baseType: !12)
65+
!20 = !DIFile(filename: "h1.h", directory: "C:\\Users\\gbmorsej\\source\\bees", checksumkind: CSK_MD5, checksum: "0c90de8c8e867df533d869035e11cf8c")
66+
!21 = !DIDerivedType(tag: DW_TAG_variable, name: "typeData", scope: !5, file: !18, line: 6, baseType: !19, flags: DIFlagStaticMember)
67+
!22 = !{!"clang version 21.0.0"}
68+
!23 = !{i32 7, !"Dwarf Version", i32 5}
69+
!24 = !{i32 2, !"Debug Info Version", i32 3}
70+
!25 = !{i32 1, !"wchar_size", i32 2}
71+
!26 = !{i32 1, !"SIE:somestuff", i32 2}
72+
!27 = !{i32 8, !"PIC Level", i32 2}
73+
!28 = !{i32 7, !"uwtable", i32 2}
74+
!29 = !{i32 7, !"frame-pointer", i32 1}
75+
!30 = !{i32 1, !"MaxTLSAlign", i32 256}
76+
!31 = !{i32 1, !"EnableSplitLTOUnit", i32 1}
77+
!32 = !{i32 1, !"UnifiedLTO", i32 1}
78+

0 commit comments

Comments
 (0)