Index: lib/CodeGen/AsmPrinter/CodeViewDebug.h =================================================================== --- lib/CodeGen/AsmPrinter/CodeViewDebug.h +++ lib/CodeGen/AsmPrinter/CodeViewDebug.h @@ -191,6 +191,8 @@ void emitDebugInfoForGlobals(); + void emitDebugInfoForRetainedTypes(); + void emitDebugInfoForUDTs( ArrayRef> UDTs); Index: lib/CodeGen/AsmPrinter/CodeViewDebug.cpp =================================================================== --- lib/CodeGen/AsmPrinter/CodeViewDebug.cpp +++ lib/CodeGen/AsmPrinter/CodeViewDebug.cpp @@ -344,6 +344,9 @@ setCurrentSubprogram(nullptr); emitDebugInfoForGlobals(); + // Emit retained types. + emitDebugInfoForRetainedTypes(); + // Switch back to the generic .debug$S section after potentially processing // comdat symbol sections. switchToDebugSectionForSymbol(nullptr); @@ -1802,6 +1805,18 @@ } } +void CodeViewDebug::emitDebugInfoForRetainedTypes() { + NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); + for (const MDNode *Node : CUs->operands()) { + for (auto *Ty : cast(Node)->getRetainedTypes()) { + if (DIType *RT = dyn_cast(Ty)) { + getTypeIndex(RT); + // FIXME: Add to global/local DTU list. + } + } + } +} + void CodeViewDebug::emitDebugInfoForGlobal(const DIGlobalVariable *DIGV, MCSymbol *GVSym) { // DataSym record, see SymbolRecord.h for more info. Index: test/DebugInfo/COFF/retained-types.ll =================================================================== --- /dev/null +++ test/DebugInfo/COFF/retained-types.ll @@ -0,0 +1,153 @@ +; RUN: llc < %s -filetype=obj | llvm-readobj - -codeview | FileCheck %s + +; This test checks that types which are used in expressions, but for which +; there are no variables, known as retained types, get emitted. + +; C++ source to regenerate: +; $ cat /tmp/a.cc +; struct S { int x; }; +; int f(void *p) { +; return static_cast(p)->x; +; } +; $ clang /tmp/a.cc -S -emit-llvm -g -gcodeview -target x86_64-windows-msvc -o t.ll + +; CHECK: CodeViewTypes [ +; CHECK: Section: .debug$T (6) +; CHECK: Magic: 0x4 +; CHECK: ArgList (0x1000) { +; CHECK: TypeLeafKind: LF_ARGLIST (0x1201) +; CHECK: NumArgs: 1 +; CHECK: Arguments [ +; CHECK: ArgType: void* (0x603) +; CHECK: ] +; CHECK: } +; CHECK: Procedure (0x1001) { +; CHECK: TypeLeafKind: LF_PROCEDURE (0x1008) +; CHECK: ReturnType: int (0x74) +; CHECK: CallingConvention: NearC (0x0) +; CHECK: FunctionOptions [ (0x0) +; CHECK: ] +; CHECK: NumParameters: 1 +; CHECK: ArgListType: (void*) (0x1000) +; CHECK: } +; CHECK: FuncId (0x1002) { +; CHECK: TypeLeafKind: LF_FUNC_ID (0x1601) +; CHECK: ParentScope: 0x0 +; CHECK: FunctionType: int (void*) (0x1001) +; CHECK: Name: f +; CHECK: } +; CHECK: Struct (0x1003) { +; CHECK: TypeLeafKind: LF_STRUCTURE (0x1505) +; CHECK: MemberCount: 0 +; CHECK: Properties [ (0x280) +; CHECK: ForwardReference (0x80) +; CHECK: HasUniqueName (0x200) +; CHECK: ] +; CHECK: FieldList: 0x0 +; CHECK: DerivedFrom: 0x0 +; CHECK: VShape: 0x0 +; CHECK: SizeOf: 0 +; CHECK: Name: S +; CHECK: LinkageName: .?AUS@@ +; CHECK: } +; CHECK: Pointer (0x1004) { +; CHECK: TypeLeafKind: LF_POINTER (0x1002) +; CHECK: PointeeType: S (0x1003) +; CHECK: PointerAttributes: 0x1000C +; CHECK: PtrType: Near64 (0xC) +; CHECK: PtrMode: Pointer (0x0) +; CHECK: IsFlat: 0 +; CHECK: IsConst: 0 +; CHECK: IsVolatile: 0 +; CHECK: IsUnaligned: 0 +; CHECK: SizeOf: 8 +; CHECK: } +; CHECK: FieldList (0x1005) { +; CHECK: TypeLeafKind: LF_FIELDLIST (0x1203) +; CHECK: DataMember { +; CHECK: AccessSpecifier: Public (0x3) +; CHECK: Type: int (0x74) +; CHECK: FieldOffset: 0x0 +; CHECK: Name: x +; CHECK: } +; CHECK: } +; CHECK: Struct (0x1006) { +; CHECK: TypeLeafKind: LF_STRUCTURE (0x1505) +; CHECK: MemberCount: 1 +; CHECK: Properties [ (0x200) +; CHECK: HasUniqueName (0x200) +; CHECK: ] +; CHECK: FieldList: (0x1005) +; CHECK: DerivedFrom: 0x0 +; CHECK: VShape: 0x0 +; CHECK: SizeOf: 4 +; CHECK: Name: S +; CHECK: LinkageName: .?AUS@@ +; CHECK: } +; CHECK: StringId (0x1007) { +; CHECK: TypeLeafKind: LF_STRING_ID (0x1605) +; CHECK: Id: 0x0 +; CHECK: StringData: \usr\local\google\work\llvm\build.release\tmp\a.cc +; CHECK: } +; CHECK: UdtSourceLine (0x1008) { +; CHECK: TypeLeafKind: LF_UDT_SRC_LINE (0x1606) +; CHECK: UDT: S (0x1006) +; CHECK: SourceFile: \usr\local\google\work\llvm\build.release\tmp\a.cc (0x1007) +; CHECK: LineNumber: 1 +; CHECK: } +; CHECK: ] + +; ModuleID = '/tmp/a.cc' +source_filename = "/tmp/a.cc" +target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64--windows-msvc18.0.0" + +%struct.S = type { i32 } + +; Function Attrs: nounwind uwtable +define i32 @"\01?f@@YAHPEAX@Z"(i8* %p) #0 !dbg !13 { +entry: + %p.addr = alloca i8*, align 8 + store i8* %p, i8** %p.addr, align 8 + call void @llvm.dbg.declare(metadata i8** %p.addr, metadata !17, metadata !18), !dbg !19 + %0 = load i8*, i8** %p.addr, align 8, !dbg !20 + %1 = bitcast i8* %0 to %struct.S*, !dbg !21 + %x = getelementptr inbounds %struct.S, %struct.S* %1, i32 0, i32 0, !dbg !22 + %2 = load i32, i32* %x, align 4, !dbg !22 + ret i32 %2, !dbg !23 +} + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind readnone } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!9, !10, !11} +!llvm.ident = !{!12} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.9.0 (trunk 273441) (llvm/trunk 273449)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3) +!1 = !DIFile(filename: "/tmp/a.cc", directory: "/usr/local/google/work/llvm/build.release") +!2 = !{} +!3 = !{!4} +!4 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !5, size: 64, align: 64) +!5 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "S", file: !1, line: 1, size: 32, align: 32, elements: !6, identifier: ".?AUS@@") +!6 = !{!7} +!7 = !DIDerivedType(tag: DW_TAG_member, name: "x", scope: !5, file: !1, line: 1, baseType: !8, size: 32, align: 32) +!8 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!9 = !{i32 2, !"CodeView", i32 1} +!10 = !{i32 2, !"Debug Info Version", i32 3} +!11 = !{i32 1, !"PIC Level", i32 2} +!12 = !{!"clang version 3.9.0 (trunk 273441) (llvm/trunk 273449)"} +!13 = distinct !DISubprogram(name: "f", linkageName: "\01?f@@YAHPEAX@Z", scope: !1, file: !1, line: 2, type: !14, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2) +!14 = !DISubroutineType(types: !15) +!15 = !{!8, !16} +!16 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64, align: 64) +!17 = !DILocalVariable(name: "p", arg: 1, scope: !13, file: !1, line: 2, type: !16) +!18 = !DIExpression() +!19 = !DILocation(line: 2, column: 13, scope: !13) +!20 = !DILocation(line: 3, column: 26, scope: !13) +!21 = !DILocation(line: 3, column: 10, scope: !13) +!22 = !DILocation(line: 3, column: 30, scope: !13) +!23 = !DILocation(line: 3, column: 3, scope: !13)