Skip to content

Commit eb051c7

Browse files
Adjust "Source Lang Literal" logic to support multiple CompileUnits (#2098)
This commit changes "Source Lang Literal" flag from simple a scalar value to a vector of pairs: (compile unit, source language).
1 parent b0142af commit eb051c7

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

lib/SPIRV/SPIRVToLLVMDbgTran.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,34 @@ DIScope *SPIRVToLLVMDbgTran::getScope(const SPIRVEntry *ScopeInst) {
167167
return transDebugInst<DIScope>(static_cast<const SPIRVExtInst *>(ScopeInst));
168168
}
169169

170+
void SPIRVToLLVMDbgTran::appendToSourceLangLiteral(DICompileUnit *CompileUnit,
171+
SPIRVWord SourceLang) {
172+
if (!M->getModuleFlag("Source Lang Literal")) {
173+
M->addModuleFlag(llvm::Module::Warning, "Source Lang Literal",
174+
MDTuple::get(M->getContext(), {}));
175+
}
176+
auto *SourceLangLiteral =
177+
dyn_cast<MDTuple>(M->getModuleFlag("Source Lang Literal"));
178+
179+
// Copy old content
180+
SmallVector<Metadata *, 4> Nodes;
181+
for (auto &Node : SourceLangLiteral->operands()) {
182+
Nodes.push_back(Node);
183+
}
184+
185+
// Add new entry
186+
Nodes.push_back(MDTuple::get(
187+
M->getContext(), SmallVector<Metadata *, 2>{
188+
CompileUnit,
189+
ConstantAsMetadata::get(ConstantInt::get(
190+
Type::getInt32Ty(M->getContext()), SourceLang)),
191+
}));
192+
193+
// Update
194+
M->setModuleFlag(llvm::Module::Warning, "Source Lang Literal",
195+
MDTuple::get(M->getContext(), Nodes));
196+
}
197+
170198
DICompileUnit *
171199
SPIRVToLLVMDbgTran::transCompilationUnit(const SPIRVExtInst *DebugInst,
172200
const std::string CompilerVersion,
@@ -191,6 +219,8 @@ SPIRVToLLVMDbgTran::transCompilationUnit(const SPIRVExtInst *DebugInst,
191219
}
192220
SPIRVWord SourceLang =
193221
getConstantValueOrLiteral(Ops, LanguageIdx, DebugInst->getExtSetKind());
222+
SPIRVWord OriginalSourceLang = SourceLang;
223+
bool InvalidSourceLang = false;
194224
if (DebugInst->getExtSetKind() == SPIRVEIS_NonSemantic_Shader_DebugInfo_200) {
195225
SourceLang = convertSPIRVSourceLangToDWARFNonSemanticDbgInfo(SourceLang);
196226
} else if (isSPIRVSourceLangValid(SourceLang)) {
@@ -199,8 +229,8 @@ SPIRVToLLVMDbgTran::transCompilationUnit(const SPIRVExtInst *DebugInst,
199229
// Some SPIR-V producers generate invalid source language value. In such
200230
// case the original value should be preserved in "Source Lang Literal"
201231
// module flag for later use by LLVM IR consumers.
202-
M->addModuleFlag(llvm::Module::Warning, "Source Lang Literal", SourceLang);
203232
SourceLang = dwarf::DW_LANG_OpenCL;
233+
InvalidSourceLang = true;
204234
}
205235

206236
BuilderMap[DebugInst->getId()] = std::make_unique<DIBuilder>(*M);
@@ -218,20 +248,28 @@ SPIRVToLLVMDbgTran::transCompilationUnit(const SPIRVExtInst *DebugInst,
218248
DebugInst->getExtSetKind() ==
219249
SPIRVEIS_NonSemantic_Shader_DebugInfo_200);
220250

221-
return BuilderMap[DebugInst->getId()]->createCompileUnit(
251+
auto *CompileUnit = BuilderMap[DebugInst->getId()]->createCompileUnit(
222252
SourceLang, getFile(Ops[SourceIdx]),
223253
DebugInst->getExtSetKind() == SPIRVEIS_NonSemantic_Shader_DebugInfo_100
224254
? CompilerVersion
225255
: getString(Ops[ProducerIdx]),
226256
false, Flags, 0, StoragePath,
227257
DICompileUnit::DebugEmissionKind::FullDebug, BuildIdentifier);
258+
if (InvalidSourceLang) {
259+
appendToSourceLangLiteral(CompileUnit, OriginalSourceLang);
260+
}
261+
return CompileUnit;
228262
}
229263

230264
// TODO: Remove this workaround once we switch to NonSemantic.Shader.* debug
231265
// info by default
232266
auto Producer = findModuleProducer();
233-
return BuilderMap[DebugInst->getId()]->createCompileUnit(
267+
auto *CompileUnit = BuilderMap[DebugInst->getId()]->createCompileUnit(
234268
SourceLang, getFile(Ops[SourceIdx]), Producer, false, Flags, 0);
269+
if (InvalidSourceLang) {
270+
appendToSourceLangLiteral(CompileUnit, OriginalSourceLang);
271+
}
272+
return CompileUnit;
235273
}
236274

237275
DIBasicType *SPIRVToLLVMDbgTran::transTypeBasic(const SPIRVExtInst *DebugInst) {

lib/SPIRV/SPIRVToLLVMDbgTran.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ class SPIRVToLLVMDbgTran {
106106
MDNode *transDebugInlined(const SPIRVExtInst *Inst);
107107
MDNode *transDebugInlinedNonSemanticShader200(const SPIRVExtInst *Inst);
108108

109+
void appendToSourceLangLiteral(DICompileUnit *CompileUnit,
110+
SPIRVWord SourceLang);
111+
109112
DICompileUnit *transCompilationUnit(const SPIRVExtInst *DebugInst,
110113
const std::string CompilerVersion = "",
111114
const std::string Flags = "");

test/DebugInfo/InvalidSourceLanguageSPIRVtoLLVM.spvasm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@
3636
OpReturn
3737
OpFunctionEnd
3838

39-
; CHECK: !{i32 2, !"Source Lang Literal", i32 42}
40-
; CHECK: !DICompileUnit(language: DW_LANG_OpenCL,
39+
; CHECK: {{![0-9]+}} = !{i32 2, !"Source Lang Literal", [[LIST:![0-9]+]]}
40+
; CHECK: [[LIST]] = !{[[ENTRY:![0-9]+]]}
41+
; CHECK: [[ENTRY]] = !{[[CU:![0-9]+]], i32 42}
42+
; CHECK: [[CU]] = distinct !DICompileUnit(language: DW_LANG_OpenCL

0 commit comments

Comments
 (0)