Skip to content

Commit 75184f1

Browse files
committed
[DebugInfo] Fix handling '# line "file"' for DWARFv5
`CppHashInfo.Filename` is a `StringRef` that references a part of the source file and it is not null-terminated at the end of the file name. `AsmParser::parseAndMatchAndEmitTargetInstruction()` passes it to `getStreamer().emitDwarfFileDirective()`, and it eventually comes to `isRootFile()`. The comparison fails because `FileName.data()` is not properly terminated. In addition, the old code might cause a significant speed degradation for long source files. The `operator!=()` for `std::string` and `const char *` can be implemented in a way that it finds the length of the second argument first, which slows the comparison for long data. `parseAndMatchAndEmitTargetInstruction()` calls `emitDwarfFileDirective()` every time if `CppHashInfo.Filename` is not empty. As a result, the longer the source file is, the slower the compilation wend, and for a very long file, it might take hours instead of a couple of seconds normally. Differential Revision: https://reviews.llvm.org/D117785
1 parent 8def89b commit 75184f1

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

llvm/lib/MC/MCDwarf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ Expected<unsigned> MCDwarfLineTable::tryGetFile(StringRef &Directory,
561561

562562
static bool isRootFile(const MCDwarfFile &RootFile, StringRef &Directory,
563563
StringRef &FileName, Optional<MD5::MD5Result> Checksum) {
564-
if (RootFile.Name.empty() || RootFile.Name != FileName.data())
564+
if (RootFile.Name.empty() || StringRef(RootFile.Name) != FileName)
565565
return false;
566566
return RootFile.Checksum == Checksum;
567567
}

llvm/test/MC/ELF/debug-hash-file.s

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// RUN: llvm-mc -triple x86_64-unknown-linux-gnu -filetype obj -g -dwarf-version 4 -o %t %s
2-
// RUN: llvm-dwarfdump -debug-info -debug-line %t | FileCheck %s
2+
// RUN: llvm-dwarfdump -debug-info -debug-line %t | FileCheck %s --check-prefixes=CHECK,DWARF4
3+
4+
// RUN: llvm-mc -triple x86_64-unknown-linux-gnu -filetype obj -g -dwarf-version 5 -o %t %s
5+
// RUN: llvm-dwarfdump -debug-info -debug-line %t | FileCheck %s --check-prefixes=CHECK,DWARF5
36

47
// CHECK: DW_TAG_compile_unit
58
// CHECK-NOT: DW_TAG_
@@ -8,10 +11,17 @@
811
// CHECK-NOT: DW_TAG_
912
// CHECK: DW_AT_decl_file ("/MyTest/Inputs{{(/|\\)+}}other.S")
1013

11-
// CHECK: include_directories[ 1] = "/MyTest/Inputs"
12-
// CHECK: file_names[ 1]:
13-
// CHECK-NEXT: name: "other.S"
14-
// CHECK-NEXT: dir_index: 1
14+
// DWARF4: include_directories[ 1] = "/MyTest/Inputs"
15+
// DWARF4: file_names[ 1]:
16+
// DWARF4-NEXT: name: "other.S"
17+
// DWARF4-NEXT: dir_index: 1
18+
19+
// DWARF5: include_directories[ 0] =
20+
// DWARF5-NOT: include_directories[ 1] =
21+
// DWARF5: file_names[ 0]:
22+
// DWARF5-NEXT: name: "/MyTest/Inputs/other.S"
23+
// DWARF5-NEXT: dir_index: 0
24+
// DWARF5-NOT: file_names[ 1]:
1525

1626
# 1 "/MyTest/Inputs/other.S"
1727

0 commit comments

Comments
 (0)