Skip to content

Allow for mixing source/no-source DIFiles in one CU #73877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions llvm/include/llvm/MC/MCDwarf.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ struct MCDwarfLineTableHeader {
StringMap<unsigned> SourceIdMap;
std::string CompilationDir;
MCDwarfFile RootFile;
bool HasSource = false;
bool HasAnySource = false;

private:
bool HasAllMD5 = true;
bool HasAnyMD5 = false;
Expand Down Expand Up @@ -305,15 +306,15 @@ struct MCDwarfLineTableHeader {
RootFile.Checksum = Checksum;
RootFile.Source = Source;
trackMD5Usage(Checksum.has_value());
HasSource = Source.has_value();
HasAnySource |= Source.has_value();
}

void resetFileTable() {
MCDwarfDirs.clear();
MCDwarfFiles.clear();
RootFile.Name.clear();
resetMD5Usage();
HasSource = false;
HasAnySource = false;
}

private:
Expand Down Expand Up @@ -385,7 +386,7 @@ class MCDwarfLineTable {
Header.RootFile.Checksum = Checksum;
Header.RootFile.Source = Source;
Header.trackMD5Usage(Checksum.has_value());
Header.HasSource = Source.has_value();
Header.HasAnySource |= Source.has_value();
}

void resetFileTable() { Header.resetFileTable(); }
Expand Down
11 changes: 8 additions & 3 deletions llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,14 @@ void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
if (ContentTypes.HasLength)
OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length);
if (ContentTypes.HasSource) {
OS << " source: ";
FileEntry.Source.dump(OS, DumpOptions);
OS << '\n';
auto Source = FileEntry.Source.getAsCString();
if (!Source)
consumeError(Source.takeError());
else if ((*Source)[0]) {
OS << " source: ";
FileEntry.Source.dump(OS, DumpOptions);
OS << '\n';
}
}
}
}
Expand Down
18 changes: 0 additions & 18 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,6 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport {
/// The current source language.
dwarf::SourceLanguage CurrentSourceLang = dwarf::DW_LANG_lo_user;

/// Whether source was present on the first DIFile encountered in each CU.
DenseMap<const DICompileUnit *, bool> HasSourceDebugInfo;

/// Stores the count of how many objects were passed to llvm.localescape for a
/// given function and the largest index passed to llvm.localrecover.
DenseMap<Function *, std::pair<unsigned, unsigned>> FrameEscapeInfo;
Expand Down Expand Up @@ -620,9 +617,6 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport {
void verifyAttachedCallBundle(const CallBase &Call,
const OperandBundleUse &BU);

/// Verify all-or-nothing property of DIFile source attribute within a CU.
void verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F);

/// Verify the llvm.experimental.noalias.scope.decl declarations
void verifyNoAliasScopeDecl();
};
Expand Down Expand Up @@ -1352,8 +1346,6 @@ void Verifier::visitDICompileUnit(const DICompileUnit &N) {

CurrentSourceLang = (dwarf::SourceLanguage)N.getSourceLanguage();

verifySourceDebugInfo(N, *N.getFile());

CheckDI((N.getEmissionKind() <= DICompileUnit::LastEmissionKind),
"invalid emission kind", &N);

Expand Down Expand Up @@ -1442,8 +1434,6 @@ void Verifier::visitDISubprogram(const DISubprogram &N) {
"definition subprograms cannot be nested within DICompositeType "
"when enabling ODR",
&N);
if (N.getFile())
verifySourceDebugInfo(*N.getUnit(), *N.getFile());
} else {
// Subprogram declarations (part of the type hierarchy).
CheckDI(!Unit, "subprogram declarations must not have a compile unit", &N);
Expand Down Expand Up @@ -6590,14 +6580,6 @@ void Verifier::verifyAttachedCallBundle(const CallBase &Call,
}
}

void Verifier::verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F) {
bool HasSource = F.getSource().has_value();
if (!HasSourceDebugInfo.count(&U))
HasSourceDebugInfo[&U] = HasSource;
CheckDI(HasSource == HasSourceDebugInfo[&U],
"inconsistent use of embedded source");
}

void Verifier::verifyNoAliasScopeDecl() {
if (NoAliasScopeDecls.empty())
return;
Expand Down
23 changes: 9 additions & 14 deletions llvm/lib/MC/MCDwarf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ void MCDwarfLineTableHeader::emitV2FileDirTables(MCStreamer *MCOS) const {
}

static void emitOneV5FileEntry(MCStreamer *MCOS, const MCDwarfFile &DwarfFile,
bool EmitMD5, bool HasSource,
bool EmitMD5, bool HasAnySource,
std::optional<MCDwarfLineStr> &LineStr) {
assert(!DwarfFile.Name.empty());
if (LineStr)
Expand All @@ -401,7 +401,7 @@ static void emitOneV5FileEntry(MCStreamer *MCOS, const MCDwarfFile &DwarfFile,
MCOS->emitBinaryData(
StringRef(reinterpret_cast<const char *>(Cksum.data()), Cksum.size()));
}
if (HasSource) {
if (HasAnySource) {
if (LineStr)
LineStr->emitRef(MCOS, DwarfFile.Source.value_or(StringRef()));
else {
Expand Down Expand Up @@ -452,7 +452,7 @@ void MCDwarfLineTableHeader::emitV5FileDirTables(
uint64_t Entries = 2;
if (HasAllMD5)
Entries += 1;
if (HasSource)
if (HasAnySource)
Entries += 1;
MCOS->emitInt8(Entries);
MCOS->emitULEB128IntValue(dwarf::DW_LNCT_path);
Expand All @@ -464,7 +464,7 @@ void MCDwarfLineTableHeader::emitV5FileDirTables(
MCOS->emitULEB128IntValue(dwarf::DW_LNCT_MD5);
MCOS->emitULEB128IntValue(dwarf::DW_FORM_data16);
}
if (HasSource) {
if (HasAnySource) {
MCOS->emitULEB128IntValue(dwarf::DW_LNCT_LLVM_source);
MCOS->emitULEB128IntValue(LineStr ? dwarf::DW_FORM_line_strp
: dwarf::DW_FORM_string);
Expand All @@ -479,9 +479,9 @@ void MCDwarfLineTableHeader::emitV5FileDirTables(
assert((!RootFile.Name.empty() || MCDwarfFiles.size() >= 1) &&
"No root file and no .file directives");
emitOneV5FileEntry(MCOS, RootFile.Name.empty() ? MCDwarfFiles[1] : RootFile,
HasAllMD5, HasSource, LineStr);
HasAllMD5, HasAnySource, LineStr);
for (unsigned i = 1; i < MCDwarfFiles.size(); ++i)
emitOneV5FileEntry(MCOS, MCDwarfFiles[i], HasAllMD5, HasSource, LineStr);
emitOneV5FileEntry(MCOS, MCDwarfFiles[i], HasAllMD5, HasAnySource, LineStr);
}

std::pair<MCSymbol *, MCSymbol *>
Expand Down Expand Up @@ -598,7 +598,7 @@ MCDwarfLineTableHeader::tryGetFile(StringRef &Directory, StringRef &FileName,
// If any files have embedded source, they all must.
if (MCDwarfFiles.empty()) {
trackMD5Usage(Checksum.has_value());
HasSource = (Source != std::nullopt);
HasAnySource |= Source.has_value();
}
if (DwarfVersion >= 5 && isRootFile(RootFile, Directory, FileName, Checksum))
return 0;
Expand All @@ -625,11 +625,6 @@ MCDwarfLineTableHeader::tryGetFile(StringRef &Directory, StringRef &FileName,
return make_error<StringError>("file number already allocated",
inconvertibleErrorCode());

// If any files have embedded source, they all must.
if (HasSource != (Source != std::nullopt))
return make_error<StringError>("inconsistent use of embedded source",
inconvertibleErrorCode());

if (Directory.empty()) {
// Separate the directory part from the basename of the FileName.
StringRef tFileName = sys::path::filename(FileName);
Expand Down Expand Up @@ -662,8 +657,8 @@ MCDwarfLineTableHeader::tryGetFile(StringRef &Directory, StringRef &FileName,
File.Checksum = Checksum;
trackMD5Usage(Checksum.has_value());
File.Source = Source;
if (Source)
HasSource = true;
if (Source.has_value())
HasAnySource = true;

// return the allocated FileNumber.
return FileNumber;
Expand Down
27 changes: 0 additions & 27 deletions llvm/test/Assembler/debug-info-source-invalid.ll

This file was deleted.

41 changes: 0 additions & 41 deletions llvm/test/Assembler/debug-info-source.ll

This file was deleted.

37 changes: 37 additions & 0 deletions llvm/test/DebugInfo/Generic/mixed-source.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
; RUN: %llc_dwarf -O0 -filetype=obj -o - < %s | llvm-dwarfdump -debug-line - | FileCheck %s

; CHECK: include_directories[ 0] = "dir"
; CHECK-NEXT: file_names[ 0]:
; CHECK-NEXT: name: "foo.c"
; CHECK-NEXT: dir_index: 0
; CHECK-NEXT: source: "void foo() { }\n"
; CHECK-NEXT: file_names[ 1]:
; CHECK-NEXT: name: "bar.h"
; CHECK-NEXT: dir_index: 0
; CHECK-NOT: source:

; Test that DIFiles mixing source and no-source within a DICompileUnit works.

define dso_local void @foo() !dbg !5 {
ret void, !dbg !7
}

define dso_local void @bar() !dbg !6 {
ret void, !dbg !8
}

!llvm.dbg.cu = !{!4}
!llvm.module.flags = !{!0, !1}

!0 = !{i32 2, !"Dwarf Version", i32 5}
!1 = !{i32 2, !"Debug Info Version", i32 3}

!2 = !DIFile(filename: "foo.c", directory: "dir", source: "void foo() { }\0A")
!3 = !DIFile(filename: "bar.h", directory: "dir")

!4 = distinct !DICompileUnit(language: DW_LANG_C99, emissionKind: FullDebug, file: !2)
!5 = distinct !DISubprogram(name: "foo", file: !2, line: 1, type: !9, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !4)
!6 = distinct !DISubprogram(name: "bar", file: !3, line: 1, type: !9, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !4)
!7 = !DILocation(line: 1, scope: !5)
!8 = !DILocation(line: 1, scope: !6)
!9 = !DISubroutineType(types: !{})