Index: llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h =================================================================== --- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h +++ llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h @@ -107,6 +107,15 @@ unsigned SiteFuncId = 0; }; + // Combines information from DILexicalBlock and LexicalScope. + struct LexicalBlock { + SmallVector Locals; + SmallVector Children; + const MCSymbol *Begin; + const MCSymbol *End; + StringRef Name; + }; + // For each function, store a vector of labels to its instructions, as well as // to the end of the function. struct FunctionInfo { @@ -119,6 +128,11 @@ SmallVector Locals; + std::unordered_map LexicalBlocks; + + // Lexical blocks containing local variables. + SmallVector ChildBlocks; + std::vector> Annotations; const MCSymbol *Begin = nullptr; @@ -129,6 +143,12 @@ }; FunctionInfo *CurFn = nullptr; + // Map used to seperate variables according to the lexical scope they belong + // in. This is populated by recordLocalVariable() before + // collectLexicalBlocks() separates the variables between the FunctionInfo + // and LexicalBlocks. + DenseMap> ScopeVariables; + /// The set of comdat .debug$S sections that we've seen so far. Each section /// must start with a magic version number that must only be emitted once. /// This set tracks which sections we've already opened. @@ -253,9 +273,18 @@ void collectVariableInfoFromMFTable(DenseSet &Processed); + // Construct the lexical block tree for a routine, pruning emptpy lexical + // scopes, and populate it with local variables. + void collectLexicalBlockInfo(SmallVectorImpl &Scopes, + SmallVectorImpl &Blocks, + SmallVectorImpl &Locals); + void collectLexicalBlockInfo(LexicalScope &Scope, + SmallVectorImpl &ParentBlocks, + SmallVectorImpl &ParentLocals); + /// Records information about a local variable in the appropriate scope. In /// particular, locals from inlined code live inside the inlining site. - void recordLocalVariable(LocalVariable &&Var, const DILocation *Loc); + void recordLocalVariable(LocalVariable &&Var, LexicalScope *LS); /// Emits local variables in the appropriate order. void emitLocalVariableList(ArrayRef Locals); @@ -263,6 +292,13 @@ /// Emits an S_LOCAL record and its associated defined ranges. void emitLocalVariable(const LocalVariable &Var); + /// Emits a sequence of lexical block scopes and their children. + void emitLexicalBlockList(ArrayRef Blocks, + const FunctionInfo& FI); + + /// Emit a lexical block scope and its children. + void emitLexicalBlock(const LexicalBlock &Block, const FunctionInfo& FI); + /// Translates the DIType to codeview if necessary and returns a type index /// for it. codeview::TypeIndex getTypeIndex(DITypeRef TypeRef, Index: llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp =================================================================== --- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp +++ llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp @@ -358,15 +358,15 @@ } void CodeViewDebug::recordLocalVariable(LocalVariable &&Var, - const DILocation *InlinedAt) { - if (InlinedAt) { + LexicalScope *LS) { + if (const DILocation *InlinedAt = LS->getInlinedAt()) { // This variable was inlined. Associate it with the InlineSite. const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram(); InlineSite &Site = getInlineSite(InlinedAt, Inlinee); Site.InlinedLocals.emplace_back(Var); } else { - // This variable goes in the main ProcSym. - CurFn->Locals.emplace_back(Var); + // This variable goes into the corresponding lexical scope. + ScopeVariables[LS].emplace_back(Var); } } @@ -898,6 +898,7 @@ OS.EmitLabel(ProcRecordEnd); emitLocalVariableList(FI.Locals); + emitLexicalBlockList(FI.ChildBlocks, FI); // Emit inlined call site information. Only emit functions inlined directly // into the parent function. We'll emit the other sites recursively as part @@ -1018,7 +1019,7 @@ LocalVariable Var; Var.DIVar = VI.Var; Var.DefRanges.emplace_back(std::move(DefRange)); - recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt()); + recordLocalVariable(std::move(Var), Scope); } } @@ -1149,7 +1150,7 @@ Var.DIVar = DIVar; calculateRanges(Var, Ranges); - recordLocalVariable(std::move(Var), InlinedAt); + recordLocalVariable(std::move(Var), Scope); } } @@ -2288,6 +2289,118 @@ } } +void CodeViewDebug::emitLexicalBlockList(ArrayRef Blocks, + const FunctionInfo& FI) { + for (LexicalBlock *Block : Blocks) + emitLexicalBlock(*Block, FI); +} + +/// Emit an S_BLOCK32 and S_END record pair delimiting the contents of a +/// lexical block scope. +void CodeViewDebug::emitLexicalBlock(const LexicalBlock &Block, + const FunctionInfo& FI) { + MCSymbol *RecordBegin = MMI->getContext().createTempSymbol(), + *RecordEnd = MMI->getContext().createTempSymbol(); + + // Lexical block symbol record. + OS.AddComment("Record length"); + OS.emitAbsoluteSymbolDiff(RecordEnd, RecordBegin, 2); // Record Length + OS.EmitLabel(RecordBegin); + OS.AddComment("Record kind: S_BLOCK32"); + OS.EmitIntValue(SymbolKind::S_BLOCK32, 2); // Record Kind + OS.AddComment("PtrParent"); + OS.EmitIntValue(0, 4); // PtrParent + OS.AddComment("PtrEnd"); + OS.EmitIntValue(0, 4); // PtrEnd + OS.AddComment("Code size"); + OS.emitAbsoluteSymbolDiff(Block.End, Block.Begin, 4); // Code Size + OS.AddComment("Function section relative address"); + OS.EmitCOFFSecRel32(Block.Begin, /*Offset=*/0); // Func Offset + OS.AddComment("Function section index"); + OS.EmitCOFFSectionIndex(FI.Begin); // Func Symbol + OS.AddComment("Lexical block name"); + emitNullTerminatedSymbolName(OS, Block.Name); // Name + OS.EmitLabel(RecordEnd); + + // Emit variables local to this lexical block. + emitLocalVariableList(Block.Locals); + + // Emit lexical blocks contained within this block. + emitLexicalBlockList(Block.Children, FI); + + // Close the lexical block scope. + OS.AddComment("Record length"); + OS.EmitIntValue(2, 2); // Record Length + OS.AddComment("Record kind: S_END"); + OS.EmitIntValue(SymbolKind::S_END, 2); // Record Kind +} + +/// Convenience routine for collecting lexical block information for a list +/// of lexical scopes. +void CodeViewDebug::collectLexicalBlockInfo( + SmallVectorImpl &Scopes, + SmallVectorImpl &Blocks, + SmallVectorImpl &Locals) { + for (LexicalScope *Scope : Scopes) + collectLexicalBlockInfo(*Scope, Blocks, Locals); +} + +/// Populate the lexical blocks and local variable lists of the parent with +/// information about the specified lexical scope. +void CodeViewDebug::collectLexicalBlockInfo( + LexicalScope &Scope, + SmallVectorImpl &ParentBlocks, + SmallVectorImpl &ParentLocals) { + if (Scope.isAbstractScope()) + return; + + auto LocalsIter = ScopeVariables.find(&Scope); + if (LocalsIter == ScopeVariables.end()) { + // This scope does not contain variables and can be eliminated. + collectLexicalBlockInfo(Scope.getChildren(), ParentBlocks, ParentLocals); + return; + } + SmallVectorImpl &Locals = LocalsIter->second; + + const DILexicalBlock *DILB = dyn_cast(Scope.getScopeNode()); + if (!DILB) { + // This scope is not a lexical block and can be eliminated, but keep any + // local variables it contains. + ParentLocals.append(Locals.begin(), Locals.end()); + collectLexicalBlockInfo(Scope.getChildren(), ParentBlocks, ParentLocals); + return; + } + + const SmallVectorImpl &Ranges = Scope.getRanges(); + if (Ranges.size() != 1 || !getLabelAfterInsn(Ranges.front().second)) { + // This lexical block scope does not have a valid address range, or has + // too many address ranges to represent in the current CodeView format, + // so it gets eliminated and any locals it contained are promoted to the + // parent scope. + ParentLocals.append(Locals.begin(), Locals.end()); + collectLexicalBlockInfo(Scope.getChildren(), ParentBlocks, ParentLocals); + return; + } + + // Create a new CodeView lexical block for this lexical scope. If we've + // seen this DILexicalBlock before then the scope tree is malformed and + // we can handle this gracefully by not processing it a second time. + auto BlockInsertion = CurFn->LexicalBlocks.insert({DILB, LexicalBlock()}); + if (!BlockInsertion.second) + return; + + // Create a lexical block containing the local variables and collect the + // the lexical block information for the children. + const InsnRange &Range = Ranges.front(); + LexicalBlock &Block = BlockInsertion.first->second; + Block.Begin = getLabelBeforeInsn(Range.first); + Block.End = getLabelAfterInsn(Range.second); + Block.Name = DILB->getName(); + Block.Locals = std::move(Locals); + ParentBlocks.push_back(&Block); + collectLexicalBlockInfo(Scope.getChildren(), Block.Children, Block.Locals); +} + void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) { const Function &GV = MF->getFunction(); assert(FnDebugInfo.count(&GV)); @@ -2295,6 +2408,15 @@ collectVariableInfo(GV.getSubprogram()); + // Build the lexical block structure to emit for this routine. + if (LexicalScope *CFS = LScopes.getCurrentFunctionScope()) + collectLexicalBlockInfo(*CFS, CurFn->ChildBlocks, CurFn->Locals); + + // Clear the scope and variable information from the map which will not be + // valid after we have finished processing this routine. This also prepares + // the map for the subsequent routine. + ScopeVariables.clear(); + // Don't emit anything if we don't have any line tables. if (!CurFn->HaveLineInfo) { FnDebugInfo.erase(&GV); Index: llvm/test/DebugInfo/COFF/lexicalblock.ll =================================================================== --- llvm/test/DebugInfo/COFF/lexicalblock.ll +++ llvm/test/DebugInfo/COFF/lexicalblock.ll @@ -0,0 +1,228 @@ +; RUN: llc < %s -filetype=obj | llvm-readobj - -codeview | FileCheck %s + +; -- "lexicalblock.cpp" begin -------------------------------------------------- +; int main(int argc, char *argv[]) { +; int localA = 1; +; +; { // S_BLOCK32 containing 'localB' +; int localB = 2; +; localA = localB; +; } +; +; { // S_BLOCK32 not emitted +; { // S_BLOCK32 containing 'localC' +; int localC = 3; +; localA = localC; +; } +; } +; +; { // S_BLOCK32 containing 'localD' +; int localD = 4; +; localA = localD; +; +; { // S_BLOCK32 containing 'localE' +; int localE = 5; +; localA = localE; +; } +; } +; +; if (localA == 5) { // S_BLOCK32 containing 'localF' +; int localF = 6; +; localA = localF; +; } +; +; return localA != 6 ? -1 : 0; +; } +; -- "lexicalblock.cpp" end ---------------------------------------------------- +; +; To regenerate the IR below: +; $ clang -S -emit-llvm -g -gcodeview lexicalblock.cpp +; +; CHECK: {{.*}}Proc{{.*}}Sym { +; CHECK: DisplayName: main +; CHECK: } +; CHECK: LocalSym { +; CHECK: VarName: argc +; CHECK: } +; CHECK: LocalSym { +; CHECK: VarName: argv +; CHECK: } +; CHECK: LocalSym { +; CHECK: VarName: localA +; CHECK: } +; CHECK: BlockSym { +; CHECK: Kind: S_BLOCK32 {{.*}} +; CHECK: BlockName: +; CHECK: } +; CHECK: LocalSym { +; CHECK: VarName: localB +; CHECK: } +; CHECK: ScopeEndSym { +; CHECK: Kind: S_END {{.*}} +; CHECK: } +; CHECK: BlockSym { +; CHECK: Kind: S_BLOCK32 {{.*}} +; CHECK: BlockName: +; CHECK: } +; CHECK: LocalSym { +; CHECK: VarName: localC +; CHECK: } +; CHECK: ScopeEndSym { +; CHECK: } +; CHECK: BlockSym { +; CHECK: Kind: S_BLOCK32 {{.*}} +; CHECK: BlockName: +; CHECK: } +; CHECK: LocalSym { +; CHECK: VarName: localD +; CHECK: } +; CHECK: BlockSym { +; CHECK: Kind: S_BLOCK32 {{.*}} +; CHECK: BlockName: +; CHECK: } +; CHECK: LocalSym { +; CHECK: VarName: localE +; CHECK: } +; CHECK: ScopeEndSym { +; CHECK: Kind: S_END {{.*}} +; CHECK: } +; CHECK: ScopeEndSym { +; CHECK: Kind: S_END {{.*}} +; CHECK: } +; CHECK: BlockSym { +; CHECK: Kind: S_BLOCK32 {{.*}} +; CHECK: BlockName: +; CHECK: } +; CHECK: LocalSym { +; CHECK: VarName: localF +; CHECK: } +; CHECK: ScopeEndSym { +; CHECK: Kind: S_END {{.*}} +; CHECK: } +; CHECK: ProcEnd { +; CHECK: } + +; ModuleID = 'lexicalblock.cpp' +source_filename = "lexicalblock.cpp" +target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-windows-msvc19.0.24210" + +; Function Attrs: noinline norecurse nounwind uwtable +define i32 @main(i32 %argc, i8** %argv) #0 !dbg !7 { +entry: + %retval = alloca i32, align 4 + %argv.addr = alloca i8**, align 8 + %argc.addr = alloca i32, align 4 + %localA = alloca i32, align 4 + %localB = alloca i32, align 4 + %localC = alloca i32, align 4 + %localD = alloca i32, align 4 + %localE = alloca i32, align 4 + %localF = alloca i32, align 4 + store i32 0, i32* %retval, align 4 + store i8** %argv, i8*** %argv.addr, align 8 + call void @llvm.dbg.declare(metadata i8*** %argv.addr, metadata !14, metadata !15), !dbg !16 + store i32 %argc, i32* %argc.addr, align 4 + call void @llvm.dbg.declare(metadata i32* %argc.addr, metadata !17, metadata !15), !dbg !18 + call void @llvm.dbg.declare(metadata i32* %localA, metadata !19, metadata !15), !dbg !20 + store i32 1, i32* %localA, align 4, !dbg !20 + call void @llvm.dbg.declare(metadata i32* %localB, metadata !21, metadata !15), !dbg !23 + store i32 2, i32* %localB, align 4, !dbg !23 + %0 = load i32, i32* %localB, align 4, !dbg !24 + store i32 %0, i32* %localA, align 4, !dbg !25 + call void @llvm.dbg.declare(metadata i32* %localC, metadata !26, metadata !15), !dbg !29 + store i32 3, i32* %localC, align 4, !dbg !29 + %1 = load i32, i32* %localC, align 4, !dbg !30 + store i32 %1, i32* %localA, align 4, !dbg !31 + call void @llvm.dbg.declare(metadata i32* %localD, metadata !32, metadata !15), !dbg !34 + store i32 4, i32* %localD, align 4, !dbg !34 + %2 = load i32, i32* %localD, align 4, !dbg !35 + store i32 %2, i32* %localA, align 4, !dbg !36 + call void @llvm.dbg.declare(metadata i32* %localE, metadata !37, metadata !15), !dbg !39 + store i32 5, i32* %localE, align 4, !dbg !39 + %3 = load i32, i32* %localE, align 4, !dbg !40 + store i32 %3, i32* %localA, align 4, !dbg !41 + %4 = load i32, i32* %localA, align 4, !dbg !42 + %cmp = icmp eq i32 %4, 5, !dbg !44 + br i1 %cmp, label %if.then, label %if.end, !dbg !45 + +if.then: ; preds = %entry + call void @llvm.dbg.declare(metadata i32* %localF, metadata !46, metadata !15), !dbg !48 + store i32 6, i32* %localF, align 4, !dbg !48 + %5 = load i32, i32* %localF, align 4, !dbg !49 + store i32 %5, i32* %localA, align 4, !dbg !50 + br label %if.end, !dbg !51 + +if.end: ; preds = %if.then, %entry + %6 = load i32, i32* %localA, align 4, !dbg !52 + %cmp1 = icmp ne i32 %6, 6, !dbg !53 + %cond = select i1 %cmp1, i32 -1, i32 0, !dbg !52 + ret i32 %cond, !dbg !54 +} + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.declare(metadata, metadata, metadata) #1 + +attributes #0 = { noinline norecurse nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "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" "no-signed-zeros-fp-math"="false" "no-trapping-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 = !{!3, !4, !5} +!llvm.ident = !{!6} + +!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 5.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2) +!1 = !DIFile(filename: "lexicalblock.cpp", directory: "C:/patch/to/directory", checksumkind: CSK_MD5, checksum: "f066527ddfcde27836407fc187becb40") +!2 = !{} +!3 = !{i32 2, !"CodeView", i32 1} +!4 = !{i32 2, !"Debug Info Version", i32 3} +!5 = !{i32 1, !"PIC Level", i32 2} +!6 = !{!"clang version 5.0.0"} +!7 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 1, type: !8, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: false, unit: !0, variables: !2) +!8 = !DISubroutineType(types: !9) +!9 = !{!10, !10, !11} +!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) +!11 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !12, size: 64) +!12 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !13, size: 64) +!13 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char) +!14 = !DILocalVariable(name: "argv", arg: 2, scope: !7, file: !1, line: 1, type: !11) +!15 = !DIExpression() +!16 = !DILocation(line: 1, column: 26, scope: !7) +!17 = !DILocalVariable(name: "argc", arg: 1, scope: !7, file: !1, line: 1, type: !10) +!18 = !DILocation(line: 1, column: 14, scope: !7) +!19 = !DILocalVariable(name: "localA", scope: !7, file: !1, line: 2, type: !10) +!20 = !DILocation(line: 2, column: 7, scope: !7) +!21 = !DILocalVariable(name: "localB", scope: !22, file: !1, line: 5, type: !10) +!22 = distinct !DILexicalBlock(scope: !7, file: !1, line: 4, column: 3) +!23 = !DILocation(line: 5, column: 9, scope: !22) +!24 = !DILocation(line: 6, column: 14, scope: !22) +!25 = !DILocation(line: 6, column: 12, scope: !22) +!26 = !DILocalVariable(name: "localC", scope: !27, file: !1, line: 11, type: !10) +!27 = distinct !DILexicalBlock(scope: !28, file: !1, line: 10, column: 5) +!28 = distinct !DILexicalBlock(scope: !7, file: !1, line: 9, column: 3) +!29 = !DILocation(line: 11, column: 11, scope: !27) +!30 = !DILocation(line: 12, column: 16, scope: !27) +!31 = !DILocation(line: 12, column: 14, scope: !27) +!32 = !DILocalVariable(name: "localD", scope: !33, file: !1, line: 17, type: !10) +!33 = distinct !DILexicalBlock(scope: !7, file: !1, line: 16, column: 3) +!34 = !DILocation(line: 17, column: 9, scope: !33) +!35 = !DILocation(line: 18, column: 14, scope: !33) +!36 = !DILocation(line: 18, column: 12, scope: !33) +!37 = !DILocalVariable(name: "localE", scope: !38, file: !1, line: 21, type: !10) +!38 = distinct !DILexicalBlock(scope: !33, file: !1, line: 20, column: 5) +!39 = !DILocation(line: 21, column: 11, scope: !38) +!40 = !DILocation(line: 22, column: 16, scope: !38) +!41 = !DILocation(line: 22, column: 14, scope: !38) +!42 = !DILocation(line: 26, column: 7, scope: !43) +!43 = distinct !DILexicalBlock(scope: !7, file: !1, line: 26, column: 7) +!44 = !DILocation(line: 26, column: 14, scope: !43) +!45 = !DILocation(line: 26, column: 7, scope: !7) +!46 = !DILocalVariable(name: "localF", scope: !47, file: !1, line: 27, type: !10) +!47 = distinct !DILexicalBlock(scope: !43, file: !1, line: 26, column: 20) +!48 = !DILocation(line: 27, column: 9, scope: !47) +!49 = !DILocation(line: 28, column: 14, scope: !47) +!50 = !DILocation(line: 28, column: 12, scope: !47) +!51 = !DILocation(line: 29, column: 3, scope: !47) +!52 = !DILocation(line: 31, column: 10, scope: !7) +!53 = !DILocation(line: 31, column: 17, scope: !7) +!54 = !DILocation(line: 31, column: 3, scope: !7) + Index: llvm/test/DebugInfo/COFF/register-variables.ll =================================================================== --- llvm/test/DebugInfo/COFF/register-variables.ll +++ llvm/test/DebugInfo/COFF/register-variables.ll @@ -61,12 +61,12 @@ ; ASM: .cv_def_range .Lfunc_begin0 [[p_ecx_esi]], "A\021\022\000\000\000" ; ASM: .cv_def_range [[p_ecx_esi]] [[func_end]], "A\021\027\000\000\000" ; ASM: .short 4414 # Record kind: S_LOCAL -; ASM: .asciz "a" -; ASM: .cv_def_range [[after_getint]] [[after_inc_eax]], "A\021\021\000\000\000" -; ASM: .short 4414 # Record kind: S_LOCAL ; ASM: .asciz "c" ; ASM: .cv_def_range [[after_getint]] [[after_je]], "A\021\021\000\000\000" ; ASM: .short 4414 # Record kind: S_LOCAL +; ASM: .asciz "a" +; ASM: .cv_def_range [[after_getint]] [[after_inc_eax]], "A\021\021\000\000\000" +; ASM: .short 4414 # Record kind: S_LOCAL ; ASM: .asciz "b" ; ASM: .cv_def_range [[after_inc_eax]] [[after_if]], "A\021\021\000\000\000" @@ -111,28 +111,28 @@ ; OBJ: Type: int (0x74) ; OBJ: Flags [ (0x0) ; OBJ: ] -; OBJ: VarName: a +; OBJ: VarName: c ; OBJ: } ; OBJ: DefRangeRegisterSym { ; OBJ: Register: EAX (0x11) ; OBJ: LocalVariableAddrRange { ; OBJ: OffsetStart: .text+0xC ; OBJ: ISectStart: 0x0 -; OBJ: Range: 0x7 +; OBJ: Range: 0x4 ; OBJ: } ; OBJ: } ; OBJ: LocalSym { ; OBJ: Type: int (0x74) ; OBJ: Flags [ (0x0) ; OBJ: ] -; OBJ: VarName: c +; OBJ: VarName: a ; OBJ: } ; OBJ: DefRangeRegisterSym { ; OBJ: Register: EAX (0x11) ; OBJ: LocalVariableAddrRange { ; OBJ: OffsetStart: .text+0xC ; OBJ: ISectStart: 0x0 -; OBJ: Range: 0x4 +; OBJ: Range: 0x7 ; OBJ: } ; OBJ: } ; OBJ: LocalSym {