Skip to content

Commit 979b372

Browse files
committed
[SPIR-V] Support for multiple DebugCompilationUnits
The module in LLVM can have more then one Compilation Unit when e.g. modules are combined by llvm-linker. This property also needs to be handled in DI.
1 parent f8f41bf commit 979b372

File tree

2 files changed

+42
-32
lines changed

2 files changed

+42
-32
lines changed

llvm/lib/Target/SPIRV/SPIRVEmitNonSemanticDI.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
8383

8484
// Required variables to get from metadata search
8585
LLVMContext *Context;
86-
SmallString<128> FilePath;
87-
unsigned SourceLanguage = 0;
86+
SmallVector<SmallString<128>> FilePaths;
87+
SmallVector<int64_t> SourceLanguages;
8888
int64_t DwarfVersion = 0;
8989
int64_t DebugInfoVersion = 0;
9090
SmallPtrSet<DIBasicType *, 12> BasicTypes;
@@ -101,9 +101,10 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
101101
for (const auto *Op : DbgCu->operands()) {
102102
if (const auto *CompileUnit = dyn_cast<DICompileUnit>(Op)) {
103103
DIFile *File = CompileUnit->getFile();
104-
sys::path::append(FilePath, File->getDirectory(), File->getFilename());
105-
SourceLanguage = CompileUnit->getSourceLanguage();
106-
break;
104+
FilePaths.emplace_back();
105+
sys::path::append(FilePaths.back(), File->getDirectory(),
106+
File->getFilename());
107+
SourceLanguages.push_back(CompileUnit->getSourceLanguage());
107108
}
108109
}
109110
const NamedMDNode *ModuleFlags = M->getNamedMetadata("llvm.module.flags");
@@ -160,9 +161,6 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
160161
return StrReg;
161162
};
162163

163-
// Emit OpString with FilePath which is required by DebugSource
164-
const Register FilePathStrReg = EmitOpString(FilePath);
165-
166164
const SPIRVType *VoidTy =
167165
GR->getOrCreateSPIRVType(Type::getVoidTy(*Context), MIRBuilder);
168166

@@ -187,27 +185,29 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
187185
return InstReg;
188186
};
189187

190-
// Emit DebugSource which is required by DebugCompilationUnit
191-
const Register DebugSourceResIdReg = EmitDIInstruction(
192-
SPIRV::NonSemanticExtInst::DebugSource, {FilePathStrReg});
193-
194188
const SPIRVType *I32Ty =
195189
GR->getOrCreateSPIRVType(Type::getInt32Ty(*Context), MIRBuilder);
196190

197-
// Convert DwarfVersion, DebugInfo and SourceLanguage integers to OpConstant
198-
// instructions required by DebugCompilationUnit
199191
const Register DwarfVersionReg =
200192
GR->buildConstantInt(DwarfVersion, MIRBuilder, I32Ty, false);
201193
const Register DebugInfoVersionReg =
202194
GR->buildConstantInt(DebugInfoVersion, MIRBuilder, I32Ty, false);
203-
const Register SourceLanguageReg =
204-
GR->buildConstantInt(SourceLanguage, MIRBuilder, I32Ty, false);
205195

206-
[[maybe_unused]]
207-
const Register DebugCompUnitResIdReg =
208-
EmitDIInstruction(SPIRV::NonSemanticExtInst::DebugCompilationUnit,
209-
{DebugInfoVersionReg, DwarfVersionReg,
210-
DebugSourceResIdReg, SourceLanguageReg});
196+
for (unsigned Idx = 0; Idx < SourceLanguages.size(); ++Idx) {
197+
const Register FilePathStrReg = EmitOpString(FilePaths[Idx]);
198+
199+
const Register DebugSourceResIdReg = EmitDIInstruction(
200+
SPIRV::NonSemanticExtInst::DebugSource, {FilePathStrReg});
201+
202+
const Register SourceLanguageReg =
203+
GR->buildConstantInt(SourceLanguages[Idx], MIRBuilder, I32Ty, false);
204+
205+
[[maybe_unused]]
206+
const Register DebugCompUnitResIdReg =
207+
EmitDIInstruction(SPIRV::NonSemanticExtInst::DebugCompilationUnit,
208+
{DebugInfoVersionReg, DwarfVersionReg,
209+
DebugSourceResIdReg, SourceLanguageReg});
210+
}
211211

212212
// We aren't extracting any DebugInfoFlags now so we
213213
// emitting zero to use as <id>Flags argument for DebugBasicType

llvm/test/CodeGen/SPIRV/debug-info/debug-compilation-unit.ll

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,29 @@
66
; CHECK-MIR-DAG: [[type_void:%[0-9]+\:type]] = OpTypeVoid
77
; CHECK-MIR-DAG: [[type_i64:%[0-9]+\:type\(s64\)]] = OpTypeInt 32, 0
88
; CHECK-MIR-DAG: [[dwarf_version:%[0-9]+\:iid\(s32\)]] = OpConstantI [[type_i64]], 5
9-
; CHECK-MIR-DAG: [[source_language:%[0-9]+\:iid\(s32\)]] = OpConstantI [[type_i64]], 3
10-
; CHECK-MIR-DAG: [[debug_info_version:%[0-9]+\:iid\(s32\)]] = OpConstantI [[type_i64]], 21
11-
; CHECK-MIR-DAG: [[filename_str:%[0-9]+\:id\(s32\)]] = OpString 1094795567, 1094795585, 792805697, 1111638594, 1111638594, 1128481583, 1128481603, {{1697596227|1700545347}}, 1886216568, 1663985004, 0
12-
; CHECK-MIR-DAG: [[debug_source:%[0-9]+\:id\(s32\)]] = OpExtInst [[type_void]], 3, 35, [[filename_str]]
13-
; CHECK-MIR-DAG: [[debug_compilation_unit:%[0-9]+\:id\(s32\)]] = OpExtInst [[type_void]], 3, 1, [[source_language]], [[dwarf_version]], [[debug_source]], [[debug_info_version]]
9+
; CHECK-MIR-DAG: [[debug_info_version:%[0-9]+\:iid\(s32\)]] = OpConstantI [[type_i64]], 3
10+
; CHECK-MIR-DAG: [[source_language_opencl:%[0-9]+\:iid\(s32\)]] = OpConstantI [[type_i64]], 21
11+
; CHECK-MIR-DAG: [[source_language_cpp:%[0-9]+\:iid\(s32\)]] = OpConstantI [[type_i64]], 4
12+
; CHECK-MIR-DAG: [[filename_str_opencl:%[0-9]+\:id\(s32\)]] = OpString 1094795567, 1094795585, 792805697, 1111638594, 1111638594, 1128481583, 1128481603, {{1697596227|1700545347}}, 1886216568, 1663985004, 0
13+
; CHECK-MIR-DAG: [[filename_str_cpp:%[0-9]+\:id\(s32\)]] = OpString 1145324591, 1145324612, 793003076, 1162167621, 1162167621, 1179010607, 1179010630, 1697596998, 1886216568, 774989164, 7368803
14+
; CHECK-MIR-DAG: [[debug_source_opencl:%[0-9]+\:id\(s32\)]] = OpExtInst [[type_void]], 3, 35, [[filename_str_opencl]]
15+
; CHECK-MIR-DAG: OpExtInst [[type_void]], 3, 1, [[debug_info_version]], [[dwarf_version]], [[debug_source_opencl]], [[source_language_opencl]]
16+
; CHECK-MIR-DAG: [[debug_source_cpp:%[0-9]+\:id\(s32\)]] = OpExtInst [[type_void]], 3, 35, [[filename_str_cpp]]
17+
; CHECK-MIR-DAG: OpExtInst [[type_void]], 3, 1, [[debug_info_version]], [[dwarf_version]], [[debug_source_cpp]], [[source_language_cpp]]
1418

1519
; CHECK-SPIRV: [[ext_inst_non_semantic:%[0-9]+]] = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
16-
; CHECK-SPIRV: [[filename_str:%[0-9]+]] = OpString "/AAAAAAAAAA/BBBBBBBB/CCCCCCCCC{{[/\\]}}example.c"
20+
; CHECK-SPIRV: [[filename_str_opencl:%[0-9]+]] = OpString "/AAAAAAAAAA/BBBBBBBB/CCCCCCCCC{{[/\\]}}example.c"
21+
; CHECK-SPIRV: [[filename_str_cpp:%[0-9]+]] = OpString "/DDDDDDDDDD/EEEEEEEE/FFFFFFFFF{{[/\\]}}example1.cpp"
1722
; CHECK-SPIRV-DAG: [[type_void:%[0-9]+]] = OpTypeVoid
1823
; CHECK-SPIRV-DAG: [[type_i32:%[0-9]+]] = OpTypeInt 32 0
1924
; CHECK-SPIRV-DAG: [[dwarf_version:%[0-9]+]] = OpConstant [[type_i32]] 5
20-
; CHECK-SPIRV-DAG: [[debug_info_version:%[0-9]+]] = OpConstant [[type_i32]] 21
21-
; CHECK-SPIRV-DAG: [[source_language:%[0-9]+]] = OpConstant [[type_i32]] 3
22-
; CHECK-SPIRV: [[debug_source:%[0-9]+]] = OpExtInst [[type_void]] [[ext_inst_non_semantic]] DebugSource [[filename_str]]
23-
; CHECK-SPIRV: [[debug_compiation_unit:%[0-9]+]] = OpExtInst [[type_void]] [[ext_inst_non_semantic]] DebugCompilationUnit [[source_language]] [[dwarf_version]] [[debug_source]] [[debug_info_version]]
25+
; CHECK-SPIRV-DAG: [[source_language_opencl:%[0-9]+]] = OpConstant [[type_i32]] 21
26+
; CHECK-SPIRV-DAG: [[source_language_cpp:%[0-9]+]] = OpConstant [[type_i32]] 4
27+
; CHECK-SPIRV-DAG: [[debug_info_version:%[0-9]+]] = OpConstant [[type_i32]] 3
28+
; CHECK-SPIRV: [[debug_source_opencl:%[0-9]+]] = OpExtInst [[type_void]] [[ext_inst_non_semantic]] DebugSource [[filename_str_opencl]]
29+
; CHECK-SPIRV: OpExtInst [[type_void]] [[ext_inst_non_semantic]] DebugCompilationUnit [[debug_info_version]] [[dwarf_version]] [[debug_source_opencl]] [[source_language_opencl]]
30+
; CHECK-SPIRV: [[debug_source_cpp:%[0-9]+]] = OpExtInst [[type_void]] [[ext_inst_non_semantic]] DebugSource [[filename_str_cpp]]
31+
; CHECK-SPIRV: OpExtInst [[type_void]] [[ext_inst_non_semantic]] DebugCompilationUnit [[debug_info_version]] [[dwarf_version]] [[debug_source_cpp]] [[source_language_cpp]]
2432

2533
; CHECK-OPTION-NOT: OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
2634
; CHECK-OPTION-NOT: OpString "/AAAAAAAAAA/BBBBBBBB/CCCCCCCCC{{[/\\]}}example.c"
@@ -35,7 +43,7 @@ entry:
3543
ret void
3644
}
3745

38-
!llvm.dbg.cu = !{!0}
46+
!llvm.dbg.cu = !{!0, !6}
3947
!llvm.module.flags = !{!2, !3, !4, !5}
4048

4149
!0 = distinct !DICompileUnit(language: DW_LANG_OpenCL, file: !1, producer: "clang version XX.X.XXXX (FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
@@ -44,3 +52,5 @@ entry:
4452
!3 = !{i32 2, !"Debug Info Version", i32 3}
4553
!4 = !{i32 1, !"wchar_size", i32 4}
4654
!5 = !{i32 7, !"frame-pointer", i32 2}
55+
!6 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !7, producer: "clang version XX.X.XXXX (FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
56+
!7 = !DIFile(filename: "example1.cpp", directory: "/DDDDDDDDDD/EEEEEEEE/FFFFFFFFF", checksumkind: CSK_MD5, checksum: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")

0 commit comments

Comments
 (0)