@@ -167,6 +167,34 @@ DIScope *SPIRVToLLVMDbgTran::getScope(const SPIRVEntry *ScopeInst) {
167
167
return transDebugInst<DIScope>(static_cast <const SPIRVExtInst *>(ScopeInst));
168
168
}
169
169
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
+
170
198
DICompileUnit *
171
199
SPIRVToLLVMDbgTran::transCompilationUnit (const SPIRVExtInst *DebugInst,
172
200
const std::string CompilerVersion,
@@ -191,6 +219,8 @@ SPIRVToLLVMDbgTran::transCompilationUnit(const SPIRVExtInst *DebugInst,
191
219
}
192
220
SPIRVWord SourceLang =
193
221
getConstantValueOrLiteral (Ops, LanguageIdx, DebugInst->getExtSetKind ());
222
+ SPIRVWord OriginalSourceLang = SourceLang;
223
+ bool InvalidSourceLang = false ;
194
224
if (DebugInst->getExtSetKind () == SPIRVEIS_NonSemantic_Shader_DebugInfo_200) {
195
225
SourceLang = convertSPIRVSourceLangToDWARFNonSemanticDbgInfo (SourceLang);
196
226
} else if (isSPIRVSourceLangValid (SourceLang)) {
@@ -199,8 +229,8 @@ SPIRVToLLVMDbgTran::transCompilationUnit(const SPIRVExtInst *DebugInst,
199
229
// Some SPIR-V producers generate invalid source language value. In such
200
230
// case the original value should be preserved in "Source Lang Literal"
201
231
// module flag for later use by LLVM IR consumers.
202
- M->addModuleFlag (llvm::Module::Warning, " Source Lang Literal" , SourceLang);
203
232
SourceLang = dwarf::DW_LANG_OpenCL;
233
+ InvalidSourceLang = true ;
204
234
}
205
235
206
236
BuilderMap[DebugInst->getId ()] = std::make_unique<DIBuilder>(*M);
@@ -218,20 +248,28 @@ SPIRVToLLVMDbgTran::transCompilationUnit(const SPIRVExtInst *DebugInst,
218
248
DebugInst->getExtSetKind () ==
219
249
SPIRVEIS_NonSemantic_Shader_DebugInfo_200);
220
250
221
- return BuilderMap[DebugInst->getId ()]->createCompileUnit (
251
+ auto *CompileUnit = BuilderMap[DebugInst->getId ()]->createCompileUnit (
222
252
SourceLang, getFile (Ops[SourceIdx]),
223
253
DebugInst->getExtSetKind () == SPIRVEIS_NonSemantic_Shader_DebugInfo_100
224
254
? CompilerVersion
225
255
: getString (Ops[ProducerIdx]),
226
256
false , Flags, 0 , StoragePath,
227
257
DICompileUnit::DebugEmissionKind::FullDebug, BuildIdentifier);
258
+ if (InvalidSourceLang) {
259
+ appendToSourceLangLiteral (CompileUnit, OriginalSourceLang);
260
+ }
261
+ return CompileUnit;
228
262
}
229
263
230
264
// TODO: Remove this workaround once we switch to NonSemantic.Shader.* debug
231
265
// info by default
232
266
auto Producer = findModuleProducer ();
233
- return BuilderMap[DebugInst->getId ()]->createCompileUnit (
267
+ auto *CompileUnit = BuilderMap[DebugInst->getId ()]->createCompileUnit (
234
268
SourceLang, getFile (Ops[SourceIdx]), Producer, false , Flags, 0 );
269
+ if (InvalidSourceLang) {
270
+ appendToSourceLangLiteral (CompileUnit, OriginalSourceLang);
271
+ }
272
+ return CompileUnit;
235
273
}
236
274
237
275
DIBasicType *SPIRVToLLVMDbgTran::transTypeBasic (const SPIRVExtInst *DebugInst) {
0 commit comments