1//===-- SPIRVNonSemanticDebugHandler.cpp - NSDI AsmPrinter handler -*- C++
2//-*-===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#include "SPIRVNonSemanticDebugHandler.h"
11#include "MCTargetDesc/SPIRVMCTargetDesc.h"
12#include "SPIRVSubtarget.h"
13#include "SPIRVTargetMachine.h"
14#include "SPIRVUtils.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/SmallVectorExtras.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/BinaryFormat/Dwarf.h"
21#include "llvm/CodeGen/AsmPrinter.h"
22#include "llvm/CodeGen/MachineDominators.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstr.h"
25#include "llvm/IR/DebugInfo.h"
26#include "llvm/IR/DebugInfoMetadata.h"
27#include "llvm/IR/DebugProgramInstruction.h"
28#include "llvm/IR/GlobalVariable.h"
29#include "llvm/IR/InstIterator.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/Module.h"
32#include "llvm/MC/MCInst.h"
33#include "llvm/MC/MCStreamer.h"
34#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/MathExtras.h"
36#include "llvm/Support/Path.h"
37#include <cassert>
38
39using namespace llvm;
40
41namespace {
42
43/// Look up \p Key in a register map and return its value, or std::nullopt when
44/// the key is absent.
45template <typename MapT>
46static std::optional<MCRegister> lookupOptReg(const MapT &Map,
47 typename MapT::key_type Key) {
48 auto It = Map.find(Key);
49 if (It == Map.end())
50 return std::nullopt;
51 assert(It->second.isValid() && "invalid register stored in map");
52 return It->second;
53}
54
55enum : uint32_t {
56 NSDIFlagIsProtected = 1u << 0,
57 NSDIFlagIsPrivate = 1u << 1,
58 NSDIFlagIsPublic = NSDIFlagIsPrivate | NSDIFlagIsProtected,
59 NSDIFlagIsLocal = 1u << 2,
60 NSDIFlagIsDefinition = 1u << 3,
61 NSDIFlagFwdDecl = 1u << 4,
62 NSDIFlagArtificial = 1u << 5,
63 NSDIFlagExplicit = 1u << 6,
64 NSDIFlagPrototyped = 1u << 7,
65 NSDIFlagObjectPointer = 1u << 8,
66 NSDIFlagStaticMember = 1u << 9,
67 NSDIFlagIndirectVariable = 1u << 10,
68 NSDIFlagLValueReference = 1u << 11,
69 NSDIFlagRValueReference = 1u << 12,
70 NSDIFlagIsOptimized = 1u << 13,
71 NSDIFlagIsEnumClass = 1u << 14,
72 NSDIFlagTypePassByValue = 1u << 15,
73 NSDIFlagTypePassByReference = 1u << 16,
74 NSDIFlagUnknownPhysicalLayout = 1u << 17,
75};
76
77static uint32_t mapDIFlagsToNonSemantic(DINode::DIFlags DFlags) {
78 uint32_t Flags = 0;
79 if ((DFlags & DINode::FlagAccessibility) == DINode::FlagPublic)
80 Flags |= NSDIFlagIsPublic;
81 if ((DFlags & DINode::FlagAccessibility) == DINode::FlagProtected)
82 Flags |= NSDIFlagIsProtected;
83 if ((DFlags & DINode::FlagAccessibility) == DINode::FlagPrivate)
84 Flags |= NSDIFlagIsPrivate;
85 if (DFlags & DINode::FlagFwdDecl)
86 Flags |= NSDIFlagFwdDecl;
87 if (DFlags & DINode::FlagArtificial)
88 Flags |= NSDIFlagArtificial;
89 if (DFlags & DINode::FlagExplicit)
90 Flags |= NSDIFlagExplicit;
91 if (DFlags & DINode::FlagPrototyped)
92 Flags |= NSDIFlagPrototyped;
93 if (DFlags & DINode::FlagObjectPointer)
94 Flags |= NSDIFlagObjectPointer;
95 if (DFlags & DINode::FlagStaticMember)
96 Flags |= NSDIFlagStaticMember;
97 if (DFlags & DINode::FlagLValueReference)
98 Flags |= NSDIFlagLValueReference;
99 if (DFlags & DINode::FlagRValueReference)
100 Flags |= NSDIFlagRValueReference;
101 if (DFlags & DINode::FlagTypePassByValue)
102 Flags |= NSDIFlagTypePassByValue;
103 if (DFlags & DINode::FlagTypePassByReference)
104 Flags |= NSDIFlagTypePassByReference;
105 if (DFlags & DINode::FlagEnumClass)
106 Flags |= NSDIFlagIsEnumClass;
107 return Flags;
108}
109
110static uint32_t transDebugFlags(const DINode *DN) {
111 uint32_t Flags = 0;
112 if (const auto *GV = dyn_cast<DIGlobalVariable>(Val: DN)) {
113 if (GV->isLocalToUnit())
114 Flags |= NSDIFlagIsLocal;
115 if (GV->isDefinition())
116 Flags |= NSDIFlagIsDefinition;
117 }
118 if (const auto *SP = dyn_cast<DISubprogram>(Val: DN)) {
119 if (SP->isLocalToUnit())
120 Flags |= NSDIFlagIsLocal;
121 if (SP->isOptimized())
122 Flags |= NSDIFlagIsOptimized;
123 if (SP->isDefinition())
124 Flags |= NSDIFlagIsDefinition;
125 Flags |= mapDIFlagsToNonSemantic(DFlags: SP->getFlags());
126 }
127 if (DN->getTag() == dwarf::DW_TAG_reference_type)
128 Flags |= NSDIFlagLValueReference;
129 if (DN->getTag() == dwarf::DW_TAG_rvalue_reference_type)
130 Flags |= NSDIFlagRValueReference;
131 if (const auto *Ty = dyn_cast<DIType>(Val: DN))
132 Flags |= mapDIFlagsToNonSemantic(DFlags: Ty->getFlags());
133 if (const auto *LV = dyn_cast<DILocalVariable>(Val: DN))
134 Flags |= mapDIFlagsToNonSemantic(DFlags: LV->getFlags());
135 return Flags;
136}
137
138// Map a DWARF composite tag to a NonSemantic.Shader.DebugInfo Composite Type
139// value: Class 0, Structure 1, Union 2.
140static uint32_t mapCompositeTypeTag(unsigned Tag) {
141 switch (Tag) {
142 case dwarf::DW_TAG_class_type:
143 return 0;
144 case dwarf::DW_TAG_structure_type:
145 return 1;
146 case dwarf::DW_TAG_union_type:
147 return 2;
148 default:
149 reportFatalInternalError(reason: "unexpected DWARF composite tag " + Twine(Tag) +
150 ". Expecting 0, 1 or 2");
151 }
152}
153
154// Map a DWARF qualifier tag to a NonSemantic.Shader.DebugInfo Type Qualifier
155// value: Const 0, Volatile 1, Restrict 2, Atomic 3.
156static std::optional<uint32_t> mapTypeQualifierTag(unsigned Tag) {
157 switch (Tag) {
158 case dwarf::DW_TAG_const_type:
159 return 0;
160 case dwarf::DW_TAG_volatile_type:
161 return 1;
162 case dwarf::DW_TAG_restrict_type:
163 return 2;
164 case dwarf::DW_TAG_atomic_type:
165 return 3;
166 default:
167 return std::nullopt;
168 }
169}
170
171static const MachineInstr *
172findLastFunctionOpVariableDeclaration(const MachineFunction &MF,
173 SPIRV::ModuleAnalysisInfo &MAI) {
174
175 // We iterate over the instructions to find the last OpVariable instruction if
176 // any. The following SPIRV rule is used to terminate the traversal earlier:
177 // SPIR-V 2.16.1, Function Structure: "All OpVariable instructions in a
178 // function must be in the first block in the function. These instructions,
179 // together with any intermixed OpLine and OpNoLine instructions, must be the
180 // first instructions in that block."
181 const MachineInstr *LastOpVariable = nullptr;
182 bool SeenOpVariable = false;
183 for (const MachineInstr &MI : MF.front()) {
184 if (MI.getOpcode() == SPIRV::OpVariable) {
185 SeenOpVariable = true;
186 if (!MAI.getSkipEmission(MI: &MI))
187 LastOpVariable = &MI;
188 continue;
189 }
190
191 bool CanInterleaveWithOpVariable =
192 MI.getOpcode() == SPIRV::OpLine || MI.getOpcode() == SPIRV::OpNoLine;
193 if (SeenOpVariable && !CanInterleaveWithOpVariable &&
194 !MAI.getSkipEmission(MI: &MI))
195 break;
196 }
197 return LastOpVariable;
198}
199
200} // namespace
201
202SPIRVNonSemanticDebugHandler::SPIRVNonSemanticDebugHandler(AsmPrinter &AP)
203 : DebugHandlerBase(&AP),
204 CLOpts(static_cast<const SPIRVTargetMachine &>(AP.TM).getCLOpts()) {}
205
206// Map DWARF source language codes to NonSemantic.Shader.DebugInfo.100 source
207// language codes. Values are from the SourceLanguage enum in the
208// NonSemantic.Shader.DebugInfo.100 specification, section 4.3.
209unsigned SPIRVNonSemanticDebugHandler::toNSDISrcLang(unsigned DwarfSrcLang) {
210 switch (DwarfSrcLang) {
211 case dwarf::DW_LANG_OpenCL:
212 return 3; // OpenCL_C
213 case dwarf::DW_LANG_OpenCL_CPP:
214 return 4; // OpenCL_CPP
215 case dwarf::DW_LANG_CPP_for_OpenCL:
216 return 6; // CPP_for_OpenCL
217 case dwarf::DW_LANG_GLSL:
218 return 2; // GLSL
219 case dwarf::DW_LANG_HLSL:
220 return 5; // HLSL
221 case dwarf::DW_LANG_SYCL:
222 return 7; // SYCL
223 case dwarf::DW_LANG_Zig:
224 return 12; // Zig
225 default:
226 return 0; // Unknown
227 }
228}
229
230static bool isBooleanType(const DIBasicType *BT) {
231 return BT->getEncoding() == dwarf::DW_ATE_boolean;
232}
233
234// \p Value truncated to the width of \p BT, or 0 or 1 for a boolean.
235static uint64_t constantBits(uint64_t Value, const DIBasicType *BT) {
236 // A boolean is one of two values and a DIBasicType may give it no size, so
237 // masking to its width would turn every true into a false.
238 if (isBooleanType(BT))
239 return Value != 0;
240 unsigned Width = BT->getSizeInBits();
241 return Width >= 64 ? Value : Value & ((uint64_t(1) << Width) - 1);
242}
243
244// \p Value truncated to the width of \p BT. APInt::getZExtValue() asserts
245// above 64 bits, so the truncation comes first.
246static uint64_t constantBits(const APInt &Value, const DIBasicType *BT) {
247 unsigned Width = isBooleanType(BT) ? 1 : BT->getSizeInBits();
248 return constantBits(Value: Value.getActiveBits() > 64
249 ? Value.trunc(width: std::min(a: Width, b: 64u)).getZExtValue()
250 : Value.getZExtValue(),
251 BT);
252}
253
254// The DIBasicType that \p Ty resolves to through typedefs and qualifiers, or
255// null.
256static const DIBasicType *stripToScalarType(const DIType *Ty) {
257 // visitDIDerivedType accepts a cyclic chain of typedefs and qualifiers, so
258 // this stops once it reaches an already seen type.
259 SmallPtrSet<const DIDerivedType *, 4> Seen;
260 while (const auto *DT = dyn_cast_or_null<DIDerivedType>(Val: Ty)) {
261 unsigned Tag = DT->getTag();
262 if ((Tag != dwarf::DW_TAG_typedef && !mapTypeQualifierTag(Tag)) ||
263 !Seen.insert(Ptr: DT).second)
264 return nullptr;
265 Ty = DT->getBaseType();
266 }
267
268 const auto *BT = dyn_cast_or_null<DIBasicType>(Val: Ty);
269 if (!BT)
270 return nullptr;
271
272 // OpTypeBool carries no width, so a boolean of any size is accepted.
273 if (isBooleanType(BT))
274 return BT;
275
276 unsigned Size = BT->getSizeInBits();
277 switch (BT->getEncoding()) {
278 case dwarf::DW_ATE_signed:
279 case dwarf::DW_ATE_unsigned:
280 case dwarf::DW_ATE_signed_char:
281 case dwarf::DW_ATE_unsigned_char:
282 return Size == 8 || Size == 16 || Size == 32 || Size == 64 ? BT : nullptr;
283 case dwarf::DW_ATE_float:
284 // SPIR-V has no 8-bit float.
285 return Size == 16 || Size == 32 || Size == 64 ? BT : nullptr;
286 default:
287 return nullptr;
288 }
289}
290
291// Collect distinct DILocations and DILocalVariables from LLVM IR.
292//
293// DILocations come from instruction debug locations and from the debug records
294// attached to them. DebugLine pre-emission and MIR lookups assume every
295// machine-instruction debug location already appeared here; a codegen-only
296// location would not be collected and emission will be skipped.
297//
298// DILocalVariables come from the DbgVariableRecords attached to instructions
299// and from the retained nodes of each DISubprogram. Retained nodes are needed
300// because a variable with no remaining debug record (e.g. optimized away) must
301// still get a DebugLocalVariable.
302static void collectDebugLocationsAndLocalVariables(
303 const Module &M, SetVector<const DILocation *> &Locations,
304 SetVector<const DILocalVariable *> &LVs) {
305 for (const Function &F : M) {
306 const DISubprogram *SP = F.getSubprogram();
307 if (!SP)
308 continue;
309 for (const MDNode *N : SP->getRetainedNodes())
310 if (const auto *LV = dyn_cast_or_null<DILocalVariable>(Val: N))
311 LVs.insert(X: LV);
312 for (const Instruction &I : instructions(F)) {
313 if (const DILocation *DL = I.getDebugLoc().get())
314 Locations.insert(X: DL);
315 for (DbgRecord &DR : I.getDbgRecordRange()) {
316 if (const DILocation *DL = DR.getDebugLoc().get())
317 Locations.insert(X: DL);
318 if (const auto *DVR = dyn_cast<DbgVariableRecord>(Val: &DR))
319 if (const DILocalVariable *LV = DVR->getVariable())
320 LVs.insert(X: LV);
321 }
322 }
323 }
324}
325
326// Insert \p S and its enclosing DILexicalBlock/DINamespace/DILexicalBlockFile
327// chain into \p Out, parent before child, so single-pass emission never needs
328// a forward reference for the Parent operand.
329static void collectLexicalBlockChain(const DIScope *S,
330 SetVector<const DIScope *> &Out) {
331 // Walk up child-first, then insert in reverse to get parents in first.
332 SmallVector<const DIScope *, 8> Chain;
333 while (S && !Out.contains(key: S) &&
334 isa<DILexicalBlock, DINamespace, DILexicalBlockFile>(Val: S)) {
335 Chain.push_back(Elt: S);
336 S = S->getScope();
337 }
338 Out.insert(Start: Chain.rbegin(), End: Chain.rend());
339}
340
341void SPIRVNonSemanticDebugHandler::beginModule(Module *M) {
342 // The base class sets Asm = nullptr when the module has no compile units,
343 // and initializes lexical scope tracking otherwise.
344 DebugHandlerBase::beginModule(M);
345
346 if (!Asm)
347 return;
348
349 CompileUnits.clear();
350 DebugTypes.clear();
351 EnumCompileUnits.clear();
352 Subprograms.clear();
353 UniqueDebugLocations.clear();
354 GlobalVariableDebugInfoMap.clear();
355 LocalVariables.clear();
356 DebugLocalVariableRegs.clear();
357 DebugExpressionRegs.clear();
358 LexicalBlocks.clear();
359 DebugScopeRegs.clear();
360 ScopesInProgress.clear();
361 HasRelaxedExtInst = false;
362 FailedScopes.clear();
363 DebugInlinedAtRegs.clear();
364 ScopeToPathOpStringReg.clear();
365 DebugSourceRegByFileStr.clear();
366 OpStringContentCache.clear();
367 ScalarTypeCache.clear();
368 ScalarConstantCache.clear();
369 ConstantValueRegs.clear();
370 DebugOperationCache.clear();
371 DebugExpressionCache.clear();
372 ModuleScopeIds.clear();
373 GlobalDIEmitted = false;
374 GlobalNSDIEnabled = false;
375 CurrentMAI = nullptr;
376#ifndef NDEBUG
377 NonSemanticOpStringsSectionEmitted = false;
378#endif
379 CachedDebugInfoNoneReg = MCRegister();
380 CachedEmptyStringReg = MCRegister();
381 CachedOpTypeVoidReg = MCRegister();
382 CachedOpTypeInt32Reg = MCRegister();
383
384 // Collect compile-unit info: file paths and source languages.
385 for (const DICompileUnit *CU : M->debug_compile_units()) {
386 const DIFile *File = CU->getFile();
387 CompileUnitInfo Info;
388 Info.TheCU = CU;
389 if (sys::path::is_absolute(path: File->getFilename()))
390 Info.FilePath = File->getFilename();
391 else
392 sys::path::append(path&: Info.FilePath, a: File->getDirectory(),
393 b: File->getFilename());
394 // getName() returns the language code regardless of whether the name is
395 // versioned. getUnversionedName() would assert on versioned names.
396 Info.SpirvSourceLanguage = toNSDISrcLang(DwarfSrcLang: CU->getSourceLanguage().getName());
397 CompileUnits.push_back(Elt: std::move(Info));
398 for (const DICompositeType *ET : CU->getEnumTypes())
399 EnumCompileUnits.try_emplace(Key: ET, Args&: CU);
400 }
401
402 // Collect DWARF version from module flags. For CodeView modules there is no
403 // "Dwarf Version" flag; DwarfVersion remains 0, which is the correct value
404 // for the DebugCompilationUnit DWARF Version operand in that case.
405 if (const NamedMDNode *Flags = M->getNamedMetadata(Name: "llvm.module.flags")) {
406 for (const auto *Op : Flags->operands()) {
407 const MDOperand &NameOp = Op->getOperand(I: 1);
408 if (NameOp.equalsStr(Str: "Dwarf Version"))
409 DwarfVersion =
410 cast<ConstantInt>(
411 Val: cast<ConstantAsMetadata>(Val: Op->getOperand(I: 2))->getValue())
412 ->getSExtValue();
413 }
414 }
415
416 // Find all debug info types that may be referenced by NSDI instructions.
417 // Member types are not roots: each one is emitted only from its composite.
418 DebugInfoFinder Finder;
419 Finder.processModule(M: *M);
420 copy_if(Range: Finder.types(), Out: std::back_inserter(x&: DebugTypes), P: [](const DIType *Ty) {
421 return Ty->getTag() != dwarf::DW_TAG_member;
422 });
423
424 Subprograms.assign(in_start: Finder.subprograms().begin(), in_end: Finder.subprograms().end());
425
426 // Walk LLVM globals to map each DIGlobalVariable to its llvm::GlobalVariable.
427 DenseMap<const DIGlobalVariable *, const GlobalVariable *> DIGVToLLVMGV;
428 for (const GlobalVariable &G : M->globals()) {
429 SmallVector<DIGlobalVariableExpression *> GVEs;
430 G.getDebugInfo(GVs&: GVEs);
431 for (DIGlobalVariableExpression *GVE : GVEs) {
432 if (const DIGlobalVariable *GV = GVE->getVariable()) {
433 DIGVToLLVMGV.try_emplace(Key: GV, Args: &G);
434 }
435 }
436 }
437
438 for (const DIGlobalVariableExpression *GVE : Finder.global_variables()) {
439 const DIGlobalVariable *GV = GVE->getVariable();
440 const DIExpression *Expr = GVE->getExpression();
441 GlobalVariableDebugInfoMap.try_emplace(
442 Key: GV, Args: GlobalVariableDebugInfo{.Expr: Expr, .LLVMGV: DIGVToLLVMGV.lookup(Val: GV)});
443 }
444
445 collectDebugLocationsAndLocalVariables(M: *M, Locations&: UniqueDebugLocations,
446 LVs&: LocalVariables);
447
448 // DILexicalBlock and DINamespace scopes are lowered to DebugLexicalBlock.
449 // DILexicalBlockFile is lowered to DebugLexicalBlockDiscriminator.
450 // Collect them in parent-before-child order so they can be emitted in a
451 // single pass.
452 for (const DIScope *S : Finder.scopes())
453 collectLexicalBlockChain(S, Out&: LexicalBlocks);
454}
455
456// The emitted SPIR-V id of \p MI, or an invalid register when \p MI defines
457// no result.
458static MCRegister getResultId(const MachineInstr &MI,
459 SPIRV::ModuleAnalysisInfo &MAI) {
460 if (MI.getNumOperands() == 0 || !MI.getOperand(i: 0).isReg() ||
461 !MI.getOperand(i: 0).isDef())
462 return MCRegister();
463 return MAI.getRegisterAlias(MF: MI.getMF(), Reg: MI.getOperand(i: 0).getReg());
464}
465
466void SPIRVNonSemanticDebugHandler::prepareModuleOutput(
467 const SPIRVSubtarget &ST, SPIRV::ModuleAnalysisInfo &MAI) {
468 if (CompileUnits.empty())
469 return;
470 if (!ST.canUseExtension(E: SPIRV::Extension::SPV_KHR_non_semantic_info))
471 return;
472
473 // Add the extension to requirements so OpExtension is output.
474 MAI.Reqs.addExtension(ToAdd: SPIRV::Extension::SPV_KHR_non_semantic_info);
475
476 HasRelaxedExtInst =
477 CLOpts.debug_scope_forward_refs &&
478 ST.canUseExtension(
479 E: SPIRV::Extension::SPV_KHR_relaxed_extended_instruction);
480 if (HasRelaxedExtInst)
481 MAI.Reqs.addExtension(
482 ToAdd: SPIRV::Extension::SPV_KHR_relaxed_extended_instruction);
483
484 // Add the NonSemantic.Shader.DebugInfo.100 entry to ExtInstSetMap so that
485 // outputOpExtInstImports() emits the OpExtInstImport instruction. Allocate a
486 // fresh result ID for it now; the same ID is used in emitExtInst() operands.
487 if (!MAI.ExtInstSetMap.count(Val: NSSet))
488 MAI.ExtInstSetMap[NSSet] = MAI.getNextIDRegister();
489
490 for (const MachineInstr *MI : MAI.getMSInstrs(MSType: SPIRV::MB_TypeConstVars)) {
491 if (MCRegister Id = getResultId(MI: *MI, MAI))
492 ModuleScopeIds.insert(V: Id);
493 }
494}
495
496void SPIRVNonSemanticDebugHandler::emitMCInst(MCInst &Inst) {
497 Asm->OutStreamer->emitInstruction(Inst, STI: Asm->getSubtargetInfo());
498}
499
500MCRegister
501SPIRVNonSemanticDebugHandler::emitOpString(StringRef S,
502 SPIRV::ModuleAnalysisInfo &MAI) {
503 MCRegister Reg = MAI.getNextIDRegister();
504 MCInst Inst;
505 Inst.setOpcode(SPIRV::OpString);
506 Inst.addOperand(Op: MCOperand::createReg(Reg));
507 addStringImm(Str: S, Inst);
508 emitMCInst(Inst);
509 return Reg;
510}
511
512MCRegister SPIRVNonSemanticDebugHandler::emitOpStringIfNew(
513 StringRef S, SPIRV::ModuleAnalysisInfo &MAI) {
514#ifndef NDEBUG
515 assert(!NonSemanticOpStringsSectionEmitted &&
516 "emitOpStringIfNew is only valid while emitting SPIR-V section 7");
517#endif
518 auto [It, Inserted] = OpStringContentCache.try_emplace(Key: S, Args: MCRegister());
519 if (Inserted)
520 It->second = emitOpString(S, MAI);
521
522 return It->second;
523}
524
525MCRegister SPIRVNonSemanticDebugHandler::getCachedOpStringReg(StringRef S) {
526#ifndef NDEBUG
527 assert(NonSemanticOpStringsSectionEmitted &&
528 "getCachedOpStringReg requires emitNonSemanticDebugStrings() first");
529#endif
530 auto It = OpStringContentCache.find(Key: S);
531 assert(It != OpStringContentCache.end() &&
532 "NSDI OpString missing from cache; emitNonSemanticDebugStrings must "
533 "cache every string used in section 10");
534 return It->second;
535}
536
537MCRegister SPIRVNonSemanticDebugHandler::emitAndCacheScopePathOpStringReg(
538 const DIScope *Scope, SPIRV::ModuleAnalysisInfo &MAI) {
539 auto [It, Inserted] = ScopeToPathOpStringReg.try_emplace(Key: Scope, Args: MCRegister());
540 if (Inserted)
541 It->second = emitOpStringIfNew(S: getDebugFullPath(Scope), MAI);
542 return It->second;
543}
544
545MCRegister SPIRVNonSemanticDebugHandler::getCachedScopePathOpStringReg(
546 const DIScope *Scope, bool UseEmptyPathIfNullScope) {
547 if (!Scope) {
548 assert(UseEmptyPathIfNullScope &&
549 "null scope path lookup requires UseEmptyPathIfNullScope");
550 assert(CachedEmptyStringReg.isValid() &&
551 "empty path OpString must be cached in emitNonSemanticDebugStrings");
552 return CachedEmptyStringReg;
553 }
554 auto It = ScopeToPathOpStringReg.find(Val: Scope);
555 assert(It != ScopeToPathOpStringReg.end() &&
556 "path OpString must be cached in emitNonSemanticDebugStrings");
557 MCRegister FileStrReg = It->second;
558 assert(FileStrReg.isValid() && "path OpString id must be valid once cached");
559 return FileStrReg;
560}
561
562MCRegister SPIRVNonSemanticDebugHandler::emitOpConstantI32(
563 uint32_t Value, MCRegister I32TypeReg, SPIRV::ModuleAnalysisInfo &MAI) {
564 return findOrEmitConstant(Opcode: SPIRV::OpConstantI, TypeReg: I32TypeReg, Value,
565 /*Width=*/32, MAI);
566}
567
568bool SPIRVNonSemanticDebugHandler::markForwardReferencedOperands(
569 ArrayRef<MCRegister> Operands) {
570 bool HasForwardRef = false;
571 for (auto &Entry : ScopesInProgress) {
572 InProgressScope &InProgress = Entry.second;
573 assert(InProgress.Id.isValid() &&
574 "in-progress scope must have a reserved id");
575 if (!is_contained(Range&: Operands, Element: InProgress.Id))
576 continue;
577 InProgress.ForwardReferenced = true;
578 HasForwardRef = true;
579 }
580 return HasForwardRef;
581}
582
583MCRegister SPIRVNonSemanticDebugHandler::emitExtInst(
584 SPIRV::NonSemanticExtInst::NonSemanticExtInst Opcode,
585 MCRegister VoidTypeReg, MCRegister ExtInstSetReg,
586 ArrayRef<MCRegister> Operands, SPIRV::ModuleAnalysisInfo &MAI,
587 std::optional<MCRegister> ResultReg) {
588 assert((!ResultReg || ResultReg->isValid()) &&
589 "ResultReg must be a valid id when provided");
590 MCRegister Reg = ResultReg ? *ResultReg : MAI.getNextIDRegister();
591
592 // An operand that is still an in-progress scope id is a forward reference.
593 // Tag the instruction with OpExtInstWithForwardRefsKHR and remember the id:
594 // if that scope later fails, the id still needs a definition.
595 bool HasForwardRef =
596 HasRelaxedExtInst && markForwardReferencedOperands(Operands);
597
598 MCInst Inst;
599 Inst.setOpcode(HasForwardRef ? SPIRV::OpExtInstWithForwardRefsKHR
600 : SPIRV::OpExtInst);
601 Inst.addOperand(Op: MCOperand::createReg(Reg));
602 Inst.addOperand(Op: MCOperand::createReg(Reg: VoidTypeReg));
603 Inst.addOperand(Op: MCOperand::createReg(Reg: ExtInstSetReg));
604 Inst.addOperand(Op: MCOperand::createImm(Val: static_cast<int64_t>(Opcode)));
605 for (MCRegister R : Operands)
606 Inst.addOperand(Op: MCOperand::createReg(Reg: R));
607 emitMCInst(Inst);
608 return Reg;
609}
610
611MCRegister SPIRVNonSemanticDebugHandler::getOrEmitOpTypeVoidReg(
612 SPIRV::ModuleAnalysisInfo &MAI) {
613 if (!CachedOpTypeVoidReg.isValid())
614 CachedOpTypeVoidReg = findOrEmitOpTypeVoid(MAI);
615 return CachedOpTypeVoidReg;
616}
617
618MCRegister SPIRVNonSemanticDebugHandler::getOrEmitOpTypeInt32Reg(
619 SPIRV::ModuleAnalysisInfo &MAI) {
620 if (!CachedOpTypeInt32Reg.isValid())
621 CachedOpTypeInt32Reg = findOrEmitOpTypeInt32(MAI);
622 return CachedOpTypeInt32Reg;
623}
624
625MCRegister SPIRVNonSemanticDebugHandler::findOrEmitOpTypeVoid(
626 SPIRV::ModuleAnalysisInfo &MAI) {
627 for (const MachineInstr *MI : MAI.getMSInstrs(MSType: SPIRV::MB_TypeConstVars)) {
628 if (MI->getOpcode() == SPIRV::OpTypeVoid)
629 return MAI.getRegisterAlias(MF: MI->getMF(), Reg: MI->getOperand(i: 0).getReg());
630 }
631 MCRegister Reg = MAI.getNextIDRegister();
632 MCInst Inst;
633 Inst.setOpcode(SPIRV::OpTypeVoid);
634 Inst.addOperand(Op: MCOperand::createReg(Reg));
635 emitMCInst(Inst);
636 return Reg;
637}
638
639// Whether declaring \p BT's SPIR-V type would require a capability. The type
640// width determines the requirement: Int8, Int16, Int64, Float16 and Float64 in
641// SPIRVModuleAnalysis::collectReqs(). A boolean and a 32-bit scalar are free.
642static bool scalarTypeNeedsCapability(const DIBasicType *BT) {
643 if (isBooleanType(BT))
644 return false;
645 unsigned Width = BT->getSizeInBits();
646 return Width != 32;
647}
648
649MCRegister SPIRVNonSemanticDebugHandler::findOrEmitScalarType(
650 const DIBasicType *BT, SPIRV::ModuleAnalysisInfo &MAI) {
651 bool IsBool = isBooleanType(BT);
652 bool IsFloat = BT->getEncoding() == dwarf::DW_ATE_float;
653 unsigned Opcode = IsBool ? SPIRV::OpTypeBool
654 : IsFloat ? SPIRV::OpTypeFloat
655 : SPIRV::OpTypeInt;
656 // Two DIBasicTypes can describe one SPIR-V type, and OpTypeBool has no
657 // width, so the key is the opcode and the width rather than the node.
658 int64_t Width = IsBool ? 0 : BT->getSizeInBits();
659
660 // OpTypeInt 32 0 is already owned by getOrEmitOpTypeInt32Reg(), which every
661 // line and column constant needs. Declaring a second one is a duplicate type
662 // declaration, which the validator rejects.
663 if (Opcode == SPIRV::OpTypeInt && Width == 32)
664 return getOrEmitOpTypeInt32Reg(MAI);
665
666 auto [CacheIt, Inserted] =
667 ScalarTypeCache.try_emplace(Key: {Opcode, Width}, Args: MCRegister());
668 if (!Inserted)
669 return CacheIt->second;
670
671 // The backend writes every integer type with signedness 0, so a signed and
672 // an unsigned DIBasicType of the same width share one OpTypeInt.
673 for (const MachineInstr *MI : MAI.getMSInstrs(MSType: SPIRV::MB_TypeConstVars)) {
674 if (MI->getOpcode() != Opcode)
675 continue;
676 if (IsBool || (MI->getOperand(i: 1).getImm() == Width &&
677 (IsFloat || MI->getOperand(i: 2).getImm() == 0))) {
678 CacheIt->second =
679 MAI.getRegisterAlias(MF: MI->getMF(), Reg: MI->getOperand(i: 0).getReg());
680 return CacheIt->second;
681 }
682 }
683
684 // A non-semantic instruction "has no semantic impact, and can be safely
685 // removed from the module", so debug info must leave the module's
686 // requirements alone. Declaring OpTypeInt 64 forces Int64, which Vulkan ties
687 // to a device feature, so this returns nothing if the type requires a new
688 // capability.
689 if (scalarTypeNeedsCapability(BT))
690 return MCRegister();
691
692 MCRegister Reg = MAI.getNextIDRegister();
693 MCInst Inst;
694 Inst.setOpcode(Opcode);
695 Inst.addOperand(Op: MCOperand::createReg(Reg));
696 if (!IsBool) {
697 Inst.addOperand(Op: MCOperand::createImm(Val: Width));
698 if (!IsFloat)
699 Inst.addOperand(Op: MCOperand::createImm(Val: 0));
700 }
701 emitMCInst(Inst);
702 CacheIt->second = Reg;
703 return Reg;
704}
705
706// Whether the constant \p MI holds \p Value, read as two words when \p IsWide.
707static bool constantHasValue(const MachineInstr &MI, uint64_t Value,
708 bool IsWide) {
709 unsigned Words = IsWide ? 2 : 1;
710 if (MI.getNumOperands() < 2 + Words)
711 return false;
712 uint64_t Found = 0;
713 for (unsigned I = 0; I < Words; ++I) {
714 if (!MI.getOperand(i: 2 + I).isImm())
715 return false;
716 Found |= static_cast<uint64_t>(
717 static_cast<uint32_t>(MI.getOperand(i: 2 + I).getImm()))
718 << (32 * I);
719 }
720 return Found == Value;
721}
722
723MCRegister SPIRVNonSemanticDebugHandler::findModuleConstant(
724 unsigned Opcode, MCRegister TypeReg, uint64_t Value, bool IsBool,
725 bool IsWide, SPIRV::ModuleAnalysisInfo &MAI) {
726 for (const MachineInstr *MI : MAI.getMSInstrs(MSType: SPIRV::MB_TypeConstVars)) {
727 if (MI->getOpcode() != Opcode || MI->getNumOperands() < 2)
728 continue;
729 if (MAI.getRegisterAlias(MF: MI->getMF(), Reg: MI->getOperand(i: 1).getReg()) !=
730 TypeReg)
731 continue;
732 // A boolean holds its value in its opcode, so there is nothing to compare.
733 if (!IsBool && !constantHasValue(MI: *MI, Value, IsWide))
734 continue;
735 return MAI.getRegisterAlias(MF: MI->getMF(), Reg: MI->getOperand(i: 0).getReg());
736 }
737 return MCRegister();
738}
739
740// \p Width is 0 for a boolean, whose value lives in \p Opcode.
741MCRegister SPIRVNonSemanticDebugHandler::findOrEmitConstant(
742 unsigned Opcode, MCRegister TypeReg, uint64_t Value, unsigned Width,
743 SPIRV::ModuleAnalysisInfo &MAI) {
744 bool IsBool = Width == 0;
745 bool IsWide = Width == 64;
746
747 auto [CacheIt, Inserted] =
748 ScalarConstantCache.try_emplace(Key: {TypeReg.id(), Value}, Args: MCRegister());
749 if (!Inserted)
750 return CacheIt->second;
751
752 MCRegister Found =
753 findModuleConstant(Opcode, TypeReg, Value, IsBool, IsWide, MAI);
754 if (Found.isValid()) {
755 CacheIt->second = Found;
756 return Found;
757 }
758
759 MCRegister Reg = MAI.getNextIDRegister();
760 MCInst Inst;
761 Inst.setOpcode(Opcode);
762 Inst.addOperand(Op: MCOperand::createReg(Reg));
763 Inst.addOperand(Op: MCOperand::createReg(Reg: TypeReg));
764 if (!IsBool) {
765 // OpConstant's Value is "the bit pattern for the constant. Types 32 bits
766 // wide or smaller take one word. Larger types take multiple words, with
767 // low-order words appearing first" (SPIR-V specification, OpConstant).
768 Inst.addOperand(Op: MCOperand::createImm(Val: static_cast<int64_t>(Lo_32(Value))));
769 if (IsWide) {
770 Inst.addOperand(Op: MCOperand::createImm(Val: static_cast<int64_t>(Hi_32(Value))));
771 } else if (Width == 16) {
772 // A 16-bit float takes one word, the same as a 32-bit one, so the text
773 // printer needs the width to pick the right APFloat semantics.
774 Inst.setFlags(SPIRV::INST_PRINTER_WIDTH16);
775 }
776 }
777 emitMCInst(Inst);
778 CacheIt->second = Reg;
779 return Reg;
780}
781
782MCRegister SPIRVNonSemanticDebugHandler::findOrEmitScalarConstant(
783 const DIBasicType *BT, uint64_t Value, SPIRV::ModuleAnalysisInfo &MAI) {
784 MCRegister TypeReg = findOrEmitScalarType(BT, MAI);
785 if (!TypeReg.isValid())
786 return MCRegister();
787
788 bool IsBool = isBooleanType(BT);
789 bool IsFloat = BT->getEncoding() == dwarf::DW_ATE_float;
790 unsigned Opcode =
791 IsBool ? (Value ? SPIRV::OpConstantTrue : SPIRV::OpConstantFalse)
792 : IsFloat ? SPIRV::OpConstantF
793 : SPIRV::OpConstantI;
794 return findOrEmitConstant(Opcode, TypeReg, Value,
795 Width: IsBool ? 0 : BT->getSizeInBits(), MAI);
796}
797
798MCRegister SPIRVNonSemanticDebugHandler::findOrEmitOpTypeInt32(
799 SPIRV::ModuleAnalysisInfo &MAI) {
800 for (const MachineInstr *MI : MAI.getMSInstrs(MSType: SPIRV::MB_TypeConstVars)) {
801 if (MI->getOpcode() == SPIRV::OpTypeInt &&
802 MI->getOperand(i: 1).getImm() == 32 && MI->getOperand(i: 2).getImm() == 0)
803 return MAI.getRegisterAlias(MF: MI->getMF(), Reg: MI->getOperand(i: 0).getReg());
804 }
805 MCRegister Reg = MAI.getNextIDRegister();
806 MCInst Inst;
807 Inst.setOpcode(SPIRV::OpTypeInt);
808 Inst.addOperand(Op: MCOperand::createReg(Reg));
809 Inst.addOperand(Op: MCOperand::createImm(Val: 32)); // width
810 Inst.addOperand(Op: MCOperand::createImm(Val: 0)); // signedness (unsigned)
811 emitMCInst(Inst);
812 return Reg;
813}
814
815// Each node emits its dependencies first. E.g. for `void f() { struct L; }`,
816// node L emits f, then L. Entering a scope reserves its result id. For
817// `struct S { S *p; }`, S* is a back edge. When forward references are
818// enabled, that id is used with OpExtInstWithForwardRefsKHR, and a later
819// failure defines it as DebugInfoNone only when an emitted instruction already
820// named it. Otherwise the edge is dropped and the reserved id stays unused.
821auto SPIRVNonSemanticDebugHandler::getOrCreateDebugScope(const DIScope *S)
822 -> EmitResult<MCRegister> {
823 if (!S || FailedScopes.contains(Ptr: S))
824 return EmitResult<MCRegister>::unsupported();
825 if (auto Reg = lookupOptReg(Map: DebugScopeRegs, Key: S))
826 return EmitResult<MCRegister>::emitted(V: *Reg);
827
828 // Mark the scope node as in progress while its dependencies are
829 // resolved/emitted.
830 auto [InProgressIt, FirstVisit] = ScopesInProgress.try_emplace(Key: S);
831 if (!FirstVisit) { // We hit a back edge, it is not the first insertion.
832 if (!HasRelaxedExtInst)
833 return EmitResult<MCRegister>::inProgress(); // No support for forward
834 // refs.
835 // Use the reserved id for the back edge when forward references are
836 // supported.
837 MCRegister ReservedId = InProgressIt->second.Id;
838 assert(ReservedId.isValid() && "in-progress scope must have a reserved id");
839 return EmitResult<MCRegister>::emitted(V: ReservedId);
840 }
841
842 // Reserve the result id first: a dependency may refer back to this scope.
843 assert(CurrentMAI && "debug scope id allocation requires CurrentMAI");
844 MCRegister Id = CurrentMAI->getNextIDRegister();
845 assert(Id.isValid() && "debug scope id must be a valid register");
846 InProgressIt->second.Id = Id;
847
848 EmitResult<PreparedScope> P = emitDebugScope(S);
849 assert((!HasRelaxedExtInst || P.Status != EmitStatus::InProgress) &&
850 "InProgress is only returned when forward references are unsupported");
851 if (!P) {
852 // Emission during recursion updates the container. Look the scope up again.
853 InProgressIt = ScopesInProgress.find(Val: S);
854 assert(InProgressIt != ScopesInProgress.end() &&
855 "scope must stay in progress until its instruction is emitted");
856 bool ForwardReferenced = InProgressIt->second.ForwardReferenced;
857 ScopesInProgress.erase(I: InProgressIt);
858 // P here is either unsupported or in progress. When forward references are
859 // supported, P can only be unsupported. Define the id only when an
860 // already-emitted instruction named it.
861 if (ForwardReferenced) {
862 assert(HasRelaxedExtInst &&
863 "a forward reference requires relaxed extended instructions");
864 assert(P.Status == EmitStatus::Unsupported &&
865 "a forward-referenced scope fails as unsupported");
866 SPIRV::ModuleAnalysisInfo &MAI = *CurrentMAI;
867 emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugInfoNone,
868 VoidTypeReg: getOrEmitOpTypeVoidReg(MAI), ExtInstSetReg: MAI.getExtInstSetReg(SetNum: NSSet), Operands: {},
869 MAI, ResultReg: Id);
870 }
871
872 // InProgress is a dropped back edge (when forward references are not
873 // supported). Leave it out of FailedScopes so a later walk can retry the
874 // scope. Regardless of forward references, cache unsupported scopes to
875 // avoid retrying them.
876 if (P.Status == EmitStatus::Unsupported)
877 FailedScopes.insert(Ptr: S);
878 return P.Status == EmitStatus::InProgress
879 ? EmitResult<MCRegister>::inProgress()
880 : EmitResult<MCRegister>::unsupported();
881 }
882
883 // The scope was successfully emitted.
884 SPIRV::ModuleAnalysisInfo &MAI = *CurrentMAI;
885 MCRegister Reg =
886 emitExtInst(Opcode: P.Value->Opcode, VoidTypeReg: getOrEmitOpTypeVoidReg(MAI),
887 ExtInstSetReg: MAI.getExtInstSetReg(SetNum: NSSet), Operands: P.Value->Operands, MAI, ResultReg: Id);
888 assert(Reg == Id &&
889 "debug scope result id must reuse the id reserved at entry");
890 ScopesInProgress.erase(Val: S);
891 assert(!GlobalNSDIEnabled &&
892 "debug type or scope created after module-scope NSDI emission");
893 DebugScopeRegs[S] = Reg;
894 return EmitResult<MCRegister>::emitted(V: Reg);
895}
896
897auto SPIRVNonSemanticDebugHandler::getOrCreateDebugTypeOrNone(const DIType *Ty)
898 -> EmitResult<MCRegister> {
899 if (Ty)
900 return getOrCreateDebugScope(S: Ty);
901 assert(CachedDebugInfoNoneReg.isValid() &&
902 "DebugInfoNone must be emitted before debug types");
903 return EmitResult<MCRegister>::emitted(V: CachedDebugInfoNoneReg);
904}
905
906auto SPIRVNonSemanticDebugHandler::emitDebugTypeBasic(
907 const DIBasicType *BT, MCRegister I32TypeReg,
908 SPIRV::ModuleAnalysisInfo &MAI) -> EmitResult<PreparedScope> {
909 if (!isUInt<32>(x: BT->getSizeInBits()))
910 return EmitResult<PreparedScope>::unsupported();
911
912 MCRegister NameReg = getCachedOpStringReg(S: BT->getName());
913 MCRegister SizeReg = emitOpConstantI32(
914 Value: static_cast<uint32_t>(BT->getSizeInBits()), I32TypeReg, MAI);
915
916 // Map DWARF base type encodings to NSDI encoding codes per
917 // NonSemantic.Shader.DebugInfo.100 specification, section 4.5.
918 unsigned Encoding = 0; // Unspecified
919 switch (BT->getEncoding()) {
920 case dwarf::DW_ATE_address:
921 Encoding = 1;
922 break;
923 case dwarf::DW_ATE_boolean:
924 Encoding = 2;
925 break;
926 case dwarf::DW_ATE_float:
927 Encoding = 3;
928 break;
929 case dwarf::DW_ATE_signed:
930 Encoding = 4;
931 break;
932 case dwarf::DW_ATE_signed_char:
933 Encoding = 5;
934 break;
935 case dwarf::DW_ATE_unsigned:
936 Encoding = 6;
937 break;
938 case dwarf::DW_ATE_unsigned_char:
939 Encoding = 7;
940 break;
941 }
942 MCRegister EncodingReg = emitOpConstantI32(Value: Encoding, I32TypeReg, MAI);
943 MCRegister FlagsReg = emitOpConstantI32(Value: 0, I32TypeReg, MAI);
944 return EmitResult<PreparedScope>::emitted(
945 V: PreparedScope{SPIRV::NonSemanticExtInst::DebugTypeBasic,
946 {NameReg, SizeReg, EncodingReg, FlagsReg}});
947}
948
949auto SPIRVNonSemanticDebugHandler::emitDebugScope(const DIScope *S)
950 -> EmitResult<PreparedScope> {
951 assert(CurrentMAI && "emitDebugScope requires CurrentMAI");
952 SPIRV::ModuleAnalysisInfo &MAI = *CurrentMAI;
953 MCRegister ExtInstSetReg = MAI.getExtInstSetReg(SetNum: NSSet);
954 MCRegister VoidTypeReg = getOrEmitOpTypeVoidReg(MAI);
955 MCRegister I32TypeReg = getOrEmitOpTypeInt32Reg(MAI);
956
957 if (const auto *BT = dyn_cast<DIBasicType>(Val: S))
958 return emitDebugTypeBasic(BT, I32TypeReg, MAI);
959 if (const auto *ST = dyn_cast<DISubroutineType>(Val: S))
960 return emitDebugTypeFunctionForSubroutineType(ST, MAI);
961 if (const auto *CT = dyn_cast<DICompositeType>(Val: S))
962 return emitDebugTypeForCompositeType(CT, VoidTypeReg, I32TypeReg,
963 ExtInstSetReg, MAI);
964 if (const auto *DT = dyn_cast<DIDerivedType>(Val: S))
965 return emitDebugTypeForDerivedType(DT, VoidTypeReg, I32TypeReg,
966 ExtInstSetReg, MAI);
967 if (isa<DILexicalBlock, DINamespace>(Val: S))
968 return emitDebugLexicalBlock(S, VoidTypeReg, I32TypeReg, ExtInstSetReg,
969 MAI);
970 if (const auto *LBF = dyn_cast<DILexicalBlockFile>(Val: S))
971 return emitDebugLexicalBlockDiscriminator(LBF, VoidTypeReg, I32TypeReg,
972 ExtInstSetReg, MAI);
973 if (const auto *SP = dyn_cast<DISubprogram>(Val: S)) {
974 if (SP->isDefinition())
975 return emitDebugFunction(SP, VoidTypeReg, I32TypeReg, ExtInstSetReg, MAI);
976 return emitDebugFunctionDeclaration(SP, VoidTypeReg, I32TypeReg,
977 ExtInstSetReg, MAI);
978 }
979 return EmitResult<PreparedScope>::unsupported();
980}
981
982auto SPIRVNonSemanticDebugHandler::emitDebugTypeForCompositeType(
983 const DICompositeType *CT, MCRegister VoidTypeReg, MCRegister I32TypeReg,
984 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI)
985 -> EmitResult<PreparedScope> {
986 switch (CT->getTag()) {
987 case dwarf::DW_TAG_array_type:
988 // A vector is an array with DINode::FlagVector. A plain array is the
989 // same tag without it. A matrix is also lowered to a DW_TAG_array_type
990 // (two subranges), so it is indistinguishable from a 2D array here and
991 // is emitted as a DebugTypeArray.
992 //
993 // FIXME: Emitting a matrix as a DebugTypeArray is valid but loses the
994 // matrix shape. DWARF has no matrix tag, so distinguishing a matrix needs
995 // a new DINode flag analogous to FlagVector, set on the array, plus a way
996 // to carry column-major vs row-major traits. Array-of-vectors alone would
997 // not disambiguate a matrix from a genuine array of vectors. Once the
998 // frontend marks matrices, route them to a DebugTypeMatrix path here.
999 if (CT->isVector())
1000 return emitDebugTypeVector(VT: CT, MAI);
1001 return emitDebugTypeArray(AT: CT, MAI);
1002 case dwarf::DW_TAG_structure_type:
1003 case dwarf::DW_TAG_class_type:
1004 case dwarf::DW_TAG_union_type:
1005 return emitDebugTypeComposite(CT, VoidTypeReg, I32TypeReg, ExtInstSetReg,
1006 MAI);
1007 case dwarf::DW_TAG_enumeration_type:
1008 return emitDebugTypeEnum(ET: CT, VoidTypeReg, I32TypeReg, ExtInstSetReg, MAI);
1009 default:
1010 return EmitResult<PreparedScope>::unsupported();
1011 }
1012}
1013
1014auto SPIRVNonSemanticDebugHandler::emitDebugTypeForDerivedType(
1015 const DIDerivedType *DT, MCRegister VoidTypeReg, MCRegister I32TypeReg,
1016 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI)
1017 -> EmitResult<PreparedScope> {
1018 switch (DT->getTag()) {
1019 case dwarf::DW_TAG_pointer_type:
1020 return emitDebugTypePointer(PT: DT, MAI);
1021 case dwarf::DW_TAG_typedef:
1022 return emitDebugTypedef(TD: DT, VoidTypeReg, I32TypeReg, ExtInstSetReg, MAI);
1023 case dwarf::DW_TAG_member:
1024 return emitDebugTypeMember(M: DT, VoidTypeReg, I32TypeReg, ExtInstSetReg, MAI);
1025 default:
1026 if (std::optional<uint32_t> Qualifier = mapTypeQualifierTag(Tag: DT->getTag()))
1027 return emitDebugTypeQualifier(QT: DT, Qualifier: *Qualifier, MAI);
1028 return EmitResult<PreparedScope>::unsupported();
1029 }
1030}
1031
1032auto SPIRVNonSemanticDebugHandler::emitDebugTypePointer(
1033 const DIDerivedType *PT, SPIRV::ModuleAnalysisInfo &MAI)
1034 -> EmitResult<PreparedScope> {
1035 // A DWARF address space is required to determine the SPIR-V storage class.
1036 // Skip pointer types that do not carry one.
1037 if (!PT->getDWARFAddressSpace().has_value())
1038 return EmitResult<PreparedScope>::unsupported();
1039
1040 EmitResult<MCRegister> Base = getOrCreateDebugTypeOrNone(Ty: PT->getBaseType());
1041 if (!Base)
1042 return EmitResult<PreparedScope>::failure(Other: Base);
1043
1044 MCRegister I32TypeReg = getOrEmitOpTypeInt32Reg(MAI);
1045 MCRegister DebugTypePointerFlagsReg =
1046 emitOpConstantI32(Value: transDebugFlags(DN: PT), I32TypeReg, MAI);
1047
1048 // For SPIR-V targets, Clang sets DwarfAddressSpace to the LLVM IR address
1049 // space, which addressSpaceToStorageClass expects.
1050 const auto &ST = static_cast<const SPIRVSubtarget &>(Asm->getSubtargetInfo());
1051 MCRegister StorageClassReg = emitOpConstantI32(
1052 Value: addressSpaceToStorageClass(AddrSpace: PT->getDWARFAddressSpace().value(), STI: ST),
1053 I32TypeReg, MAI);
1054
1055 return EmitResult<PreparedScope>::emitted(
1056 V: PreparedScope{SPIRV::NonSemanticExtInst::DebugTypePointer,
1057 {*Base.Value, StorageClassReg, DebugTypePointerFlagsReg}});
1058}
1059
1060auto SPIRVNonSemanticDebugHandler::emitDebugTypeQualifier(
1061 const DIDerivedType *QT, uint32_t Qualifier, SPIRV::ModuleAnalysisInfo &MAI)
1062 -> EmitResult<PreparedScope> {
1063 EmitResult<MCRegister> Base = getOrCreateDebugTypeOrNone(Ty: QT->getBaseType());
1064 if (!Base)
1065 return EmitResult<PreparedScope>::failure(Other: Base);
1066
1067 MCRegister QualifierReg =
1068 emitOpConstantI32(Value: Qualifier, I32TypeReg: getOrEmitOpTypeInt32Reg(MAI), MAI);
1069 return EmitResult<PreparedScope>::emitted(
1070 V: PreparedScope{SPIRV::NonSemanticExtInst::DebugTypeQualifier,
1071 {*Base.Value, QualifierReg}});
1072}
1073
1074auto SPIRVNonSemanticDebugHandler::emitDebugTypeFunctionForSubroutineType(
1075 const DISubroutineType *ST, SPIRV::ModuleAnalysisInfo &MAI)
1076 -> EmitResult<PreparedScope> {
1077 MCRegister VoidTypeReg = getOrEmitOpTypeVoidReg(MAI);
1078 MCRegister I32TypeReg = getOrEmitOpTypeInt32Reg(MAI);
1079 MCRegister DebugTypeFunctionFlagsReg =
1080 emitOpConstantI32(Value: transDebugFlags(DN: ST), I32TypeReg, MAI);
1081 DITypeArray TA = ST->getTypeArray();
1082 SmallVector<MCRegister, 8> Ops;
1083 Ops.push_back(Elt: DebugTypeFunctionFlagsReg);
1084 // Empty DI type tuple: no explicit return or parameter slots (hand-written IR
1085 // may use !{}). Emit void-only prototype. Same as SPIRV-LLVM-Translator when
1086 // DISubroutineType::getTypeArray() has zero elements.
1087 if (TA.empty()) {
1088 Ops.push_back(Elt: VoidTypeReg);
1089 } else {
1090 for (unsigned I = 0, E = TA.size(); I != E; ++I) {
1091 bool IsReturnType = (I == 0);
1092 EmitResult<MCRegister> Slot =
1093 mapDISignatureTypeToReg(Ty: TA[I], VoidTypeReg, ReturnType: IsReturnType);
1094 // No emitted DebugType* id for this slot (e.g., pointer that
1095 // was skipped due missing address space, etc.).
1096 if (!Slot)
1097 return EmitResult<PreparedScope>::failure(Other: Slot);
1098 Ops.push_back(Elt: *Slot.Value);
1099 }
1100 }
1101 return EmitResult<PreparedScope>::emitted(
1102 V: PreparedScope{SPIRV::NonSemanticExtInst::DebugTypeFunction, Ops});
1103}
1104
1105// Match SPIRV-LLVM-Translator's selection logic for the Parent operand.
1106auto SPIRVNonSemanticDebugHandler::resolveScope(const DIScope *Scope,
1107 const DICompileUnit *FallbackCU)
1108 -> EmitResult<MCRegister> {
1109 if (!CLOpts.debug_lexical_block_discriminator_scope)
1110 while (const auto *LBF = dyn_cast_or_null<DILexicalBlockFile>(Val: Scope))
1111 Scope = LBF->getScope();
1112
1113 // These scopes have their own debug instruction.
1114 if (isa_and_nonnull<DIType, DILexicalBlock, DINamespace, DISubprogram,
1115 DILexicalBlockFile>(Val: Scope))
1116 return getOrCreateDebugScope(S: Scope);
1117
1118 // For a file, compile-unit, or absent scope, fall back to a compile unit.
1119 auto CUResult = [](std::optional<MCRegister> Reg) {
1120 return Reg ? EmitResult<MCRegister>::emitted(V: *Reg)
1121 : EmitResult<MCRegister>::unsupported();
1122 };
1123 if (FallbackCU)
1124 return CUResult(lookupOptReg(Map: DebugScopeRegs, Key: FallbackCU));
1125
1126 if (CompileUnits.empty())
1127 return EmitResult<MCRegister>::unsupported();
1128
1129 return CUResult(lookupOptReg(Map: DebugScopeRegs, Key: CompileUnits[0].TheCU));
1130}
1131
1132auto SPIRVNonSemanticDebugHandler::emitDebugLexicalBlock(
1133 const DIScope *S, MCRegister VoidTypeReg, MCRegister I32TypeReg,
1134 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI)
1135 -> EmitResult<PreparedScope> {
1136 assert((isa<DILexicalBlock, DINamespace>(S)) &&
1137 "S must be a DILexicalBlock or DINamespace in emitDebugLexicalBlock");
1138 EmitResult<MCRegister> Parent = resolveScope(Scope: S->getScope());
1139 if (!Parent)
1140 return EmitResult<PreparedScope>::failure(Other: Parent);
1141
1142 MCRegister FileStrReg = getCachedScopePathOpStringReg(
1143 Scope: S->getFile(), /*UseEmptyPathIfNullScope=*/true);
1144 MCRegister SrcReg = getOrEmitDebugSourceForFileStrReg(FileStrReg, VoidTypeReg,
1145 ExtInstSetReg, MAI);
1146
1147 SmallVector<MCRegister, 5> Ops;
1148 if (const auto *LB = dyn_cast<DILexicalBlock>(Val: S)) {
1149 MCRegister LineReg = emitOpConstantI32(Value: static_cast<uint32_t>(LB->getLine()),
1150 I32TypeReg, MAI);
1151 MCRegister ColReg = emitOpConstantI32(
1152 Value: static_cast<uint32_t>(LB->getColumn()), I32TypeReg, MAI);
1153 Ops = {SrcReg, LineReg, ColReg, *Parent.Value};
1154 } else {
1155 const auto *NS = cast<DINamespace>(Val: S);
1156 // DINamespace carries no line/column info.
1157 MCRegister LineReg = emitOpConstantI32(Value: 0, I32TypeReg, MAI);
1158 MCRegister ColReg = emitOpConstantI32(Value: 0, I32TypeReg, MAI);
1159 MCRegister NameReg = getCachedOpStringReg(S: NS->getName());
1160 Ops = {SrcReg, LineReg, ColReg, *Parent.Value, NameReg};
1161 }
1162
1163 return EmitResult<PreparedScope>::emitted(
1164 V: PreparedScope{SPIRV::NonSemanticExtInst::DebugLexicalBlock, Ops});
1165}
1166
1167auto SPIRVNonSemanticDebugHandler::emitDebugLexicalBlockDiscriminator(
1168 const DILexicalBlockFile *LBF, MCRegister VoidTypeReg,
1169 MCRegister I32TypeReg, MCRegister ExtInstSetReg,
1170 SPIRV::ModuleAnalysisInfo &MAI) -> EmitResult<PreparedScope> {
1171 assert(LBF && "LBF must not be null");
1172 EmitResult<MCRegister> Parent = resolveScope(Scope: LBF->getScope());
1173 if (!Parent)
1174 return EmitResult<PreparedScope>::failure(Other: Parent);
1175
1176 MCRegister FileStrReg = getCachedScopePathOpStringReg(
1177 Scope: LBF->getFile(), /*UseEmptyPathIfNullScope=*/true);
1178 MCRegister SrcReg = getOrEmitDebugSourceForFileStrReg(FileStrReg, VoidTypeReg,
1179 ExtInstSetReg, MAI);
1180 MCRegister DiscReg = emitOpConstantI32(
1181 Value: static_cast<uint32_t>(LBF->getDiscriminator()), I32TypeReg, MAI);
1182 return EmitResult<PreparedScope>::emitted(
1183 V: PreparedScope{SPIRV::NonSemanticExtInst::DebugLexicalBlockDiscriminator,
1184 {SrcReg, DiscReg, *Parent.Value}});
1185}
1186
1187MCRegister SPIRVNonSemanticDebugHandler::getOrEmitDebugInlinedAt(
1188 const DILocation *IA, MCRegister VoidTypeReg, MCRegister I32TypeReg,
1189 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI) {
1190 assert(IA && "IA must not be null in getOrEmitDebugInlinedAt");
1191
1192 if (MCRegister Cached = DebugInlinedAtRegs.lookup(Val: IA))
1193 return Cached;
1194
1195 EmitResult<MCRegister> Scope = resolveScope(Scope: IA->getScope());
1196 if (!Scope)
1197 return MCRegister();
1198
1199 MCRegister LineReg =
1200 emitOpConstantI32(Value: static_cast<uint32_t>(IA->getLine()), I32TypeReg, MAI);
1201
1202 SmallVector<MCRegister, 3> Ops{LineReg, *Scope.Value};
1203 // Recurse before building this instruction's operands so an outer
1204 // inlined-at link is always available.
1205 if (const DILocation *Outer = IA->getInlinedAt()) {
1206 MCRegister OuterReg = getOrEmitDebugInlinedAt(
1207 IA: Outer, VoidTypeReg, I32TypeReg, ExtInstSetReg, MAI);
1208 if (!OuterReg.isValid())
1209 return MCRegister();
1210 Ops.push_back(Elt: OuterReg);
1211 }
1212
1213 MCRegister Reg = emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugInlinedAt,
1214 VoidTypeReg, ExtInstSetReg, Operands: Ops, MAI);
1215 DebugInlinedAtRegs[IA] = Reg;
1216 return Reg;
1217}
1218
1219auto SPIRVNonSemanticDebugHandler::emitDebugFunctionDeclaration(
1220 const DISubprogram *SP, MCRegister VoidTypeReg, MCRegister I32TypeReg,
1221 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI)
1222 -> EmitResult<PreparedScope> {
1223 assert(SP && "SP must not be null in emitDebugFunctionDeclaration");
1224 assert(!SP->isDefinition() &&
1225 "SP must not be a definition in emitDebugFunctionDeclaration");
1226
1227 // The IR verifier already enforces that this cannot be null.
1228 EmitResult<MCRegister> FnTy = getOrCreateDebugScope(S: SP->getType());
1229 if (!FnTy)
1230 return EmitResult<PreparedScope>::failure(Other: FnTy);
1231
1232 EmitResult<MCRegister> Parent = resolveScope(Scope: SP->getScope(), FallbackCU: SP->getUnit());
1233 if (!Parent)
1234 return EmitResult<PreparedScope>::failure(Other: Parent);
1235
1236 MCRegister FileStrReg = getCachedScopePathOpStringReg(Scope: SP);
1237
1238 MCRegister NameReg = getCachedOpStringReg(S: SP->getName());
1239 MCRegister LinkageReg = getCachedOpStringReg(S: SP->getLinkageName());
1240 MCRegister SrcReg = getOrEmitDebugSourceForFileStrReg(FileStrReg, VoidTypeReg,
1241 ExtInstSetReg, MAI);
1242
1243 MCRegister LineReg =
1244 emitOpConstantI32(Value: static_cast<uint32_t>(SP->getLine()), I32TypeReg, MAI);
1245 MCRegister ColReg = emitOpConstantI32(Value: 0, I32TypeReg, MAI);
1246
1247 uint32_t FlagsVal = transDebugFlags(DN: SP);
1248 // TODO: When composite scopes are DebugFunctionDeclaration parents (available
1249 // in DebugScopeRegs), sync declaration Flags with SPIRV-LLVM-Translator.
1250 FlagsVal &= ~NSDIFlagIsDefinition;
1251 MCRegister FlagsReg = emitOpConstantI32(Value: FlagsVal, I32TypeReg, MAI);
1252
1253 return EmitResult<PreparedScope>::emitted(
1254 V: PreparedScope{SPIRV::NonSemanticExtInst::DebugFunctionDeclaration,
1255 {NameReg, *FnTy.Value, SrcReg, LineReg, ColReg,
1256 *Parent.Value, LinkageReg, FlagsReg}});
1257}
1258
1259auto SPIRVNonSemanticDebugHandler::emitDebugFunction(
1260 const DISubprogram *SP, MCRegister VoidTypeReg, MCRegister I32TypeReg,
1261 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI)
1262 -> EmitResult<PreparedScope> {
1263 assert(SP && "SP must not be null in emitDebugFunction");
1264 assert(SP->isDefinition() && "SP must be a definition in emitDebugFunction");
1265
1266 EmitResult<MCRegister> FnTy = getOrCreateDebugScope(S: SP->getType());
1267 if (!FnTy)
1268 return EmitResult<PreparedScope>::failure(Other: FnTy);
1269
1270 EmitResult<MCRegister> Parent = resolveScope(Scope: SP->getScope(), FallbackCU: SP->getUnit());
1271 if (!Parent)
1272 return EmitResult<PreparedScope>::failure(Other: Parent);
1273
1274 MCRegister NameReg = getCachedOpStringReg(S: SP->getName());
1275 MCRegister LinkageReg = getCachedOpStringReg(S: SP->getLinkageName());
1276 MCRegister FileStrReg = getCachedScopePathOpStringReg(Scope: SP);
1277 MCRegister SrcReg = getOrEmitDebugSourceForFileStrReg(FileStrReg, VoidTypeReg,
1278 ExtInstSetReg, MAI);
1279
1280 MCRegister LineReg =
1281 emitOpConstantI32(Value: static_cast<uint32_t>(SP->getLine()), I32TypeReg, MAI);
1282 // LLVM's DISubprogram has no column field but SPIR-V expects one in
1283 // DebugFunction.
1284 MCRegister ColReg = emitOpConstantI32(Value: 0, I32TypeReg, MAI);
1285 MCRegister FlagsReg = emitOpConstantI32(Value: transDebugFlags(DN: SP), I32TypeReg, MAI);
1286 MCRegister ScopeLineReg = emitOpConstantI32(
1287 Value: static_cast<uint32_t>(SP->getScopeLine()), I32TypeReg, MAI);
1288
1289 SmallVector<MCRegister, 10> Ops = {NameReg, *FnTy.Value, SrcReg,
1290 LineReg, ColReg, *Parent.Value,
1291 LinkageReg, FlagsReg, ScopeLineReg};
1292
1293 if (const DISubprogram *Decl = SP->getDeclaration()) {
1294 EmitResult<MCRegister> DeclRes = getOrCreateDebugScope(S: Decl);
1295 if (DeclRes)
1296 Ops.push_back(Elt: *DeclRes.Value);
1297 }
1298
1299 return EmitResult<PreparedScope>::emitted(
1300 V: PreparedScope{SPIRV::NonSemanticExtInst::DebugFunction, Ops});
1301}
1302
1303auto SPIRVNonSemanticDebugHandler::mapDISignatureTypeToReg(
1304 const DIType *Ty, MCRegister VoidTypeReg, bool ReturnType)
1305 -> EmitResult<MCRegister> {
1306 if (!Ty && ReturnType)
1307 return EmitResult<MCRegister>::emitted(V: VoidTypeReg);
1308 return getOrCreateDebugTypeOrNone(Ty);
1309}
1310
1311// NonSemantic.Shader.DebugInfo.100 debug operation encodings
1312// (section 4.5, "Debug Operations").
1313enum class NonSemanticDebugOp : uint32_t {
1314 Deref = 0,
1315 Plus = 1,
1316 Minus = 2,
1317 PlusUconst = 3,
1318 BitPiece = 4,
1319 Swap = 5,
1320 Xderef = 6,
1321 StackValue = 7,
1322 Constu = 8,
1323 Fragment = 9
1324};
1325
1326static std::optional<NonSemanticDebugOp>
1327mapDwarfOpToNonSemanticOp(uint64_t DwarfOp) {
1328 switch (DwarfOp) {
1329 case dwarf::DW_OP_deref:
1330 return NonSemanticDebugOp::Deref;
1331 case dwarf::DW_OP_plus:
1332 return NonSemanticDebugOp::Plus;
1333 case dwarf::DW_OP_minus:
1334 return NonSemanticDebugOp::Minus;
1335 case dwarf::DW_OP_plus_uconst:
1336 return NonSemanticDebugOp::PlusUconst;
1337 case dwarf::DW_OP_bit_piece:
1338 return NonSemanticDebugOp::BitPiece;
1339 case dwarf::DW_OP_swap:
1340 return NonSemanticDebugOp::Swap;
1341 case dwarf::DW_OP_xderef:
1342 return NonSemanticDebugOp::Xderef;
1343 case dwarf::DW_OP_stack_value:
1344 return NonSemanticDebugOp::StackValue;
1345 case dwarf::DW_OP_constu:
1346 return NonSemanticDebugOp::Constu;
1347 case dwarf::DW_OP_LLVM_fragment:
1348 return NonSemanticDebugOp::Fragment;
1349 default:
1350 return std::nullopt;
1351 }
1352}
1353
1354// The DebugOperation operand values for \p Op. Each becomes an OpConstant of
1355// OpTypeInt 32 0, so every argument must fit in 32 bits.
1356static std::optional<SmallVector<uint32_t, 3>>
1357mapExprOperand(const DIExpression::ExprOperand &Op) {
1358 std::optional<NonSemanticDebugOp> NSOp =
1359 mapDwarfOpToNonSemanticOp(DwarfOp: Op.getOp());
1360 if (!NSOp)
1361 return std::nullopt;
1362
1363 SmallVector<uint32_t, 3> Operands{static_cast<uint32_t>(*NSOp)};
1364 for (unsigned I = 0, E = Op.getNumArgs(); I != E; ++I) {
1365 uint64_t Arg = Op.getArg(I);
1366 if (!isUInt<32>(x: Arg))
1367 return std::nullopt;
1368 Operands.push_back(Elt: static_cast<uint32_t>(Arg));
1369 }
1370 return Operands;
1371}
1372
1373std::optional<MCRegister> SPIRVNonSemanticDebugHandler::emitDebugOperation(
1374 const DIExpression::ExprOperand &Op, MCRegister VoidTypeReg,
1375 MCRegister I32TypeReg, MCRegister ExtInstSetReg,
1376 SPIRV::ModuleAnalysisInfo &MAI) {
1377 std::optional<SmallVector<uint32_t, 3>> Key = mapExprOperand(Op);
1378 if (!Key)
1379 return std::nullopt;
1380
1381 auto [It, Inserted] = DebugOperationCache.try_emplace(Key: std::move(*Key));
1382 if (!Inserted)
1383 return It->second;
1384
1385 SmallVector<MCRegister, 3> Operands;
1386 for (uint32_t V : It->first)
1387 Operands.push_back(Elt: emitOpConstantI32(Value: V, I32TypeReg, MAI));
1388 MCRegister Reg = emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugOperation,
1389 VoidTypeReg, ExtInstSetReg, Operands, MAI);
1390 It->second = Reg;
1391 return Reg;
1392}
1393
1394std::optional<MCRegister> SPIRVNonSemanticDebugHandler::emitDebugExpression(
1395 const DIExpression *Expr, MCRegister VoidTypeReg, MCRegister I32TypeReg,
1396 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI) {
1397 assert(Expr && "Expr must not be null in emitDebugExpression");
1398
1399 SmallVector<MCRegister> OperationRegs;
1400 for (const DIExpression::ExprOperand &Op : Expr->expr_ops()) {
1401 std::optional<MCRegister> OpReg =
1402 emitDebugOperation(Op, VoidTypeReg, I32TypeReg, ExtInstSetReg, MAI);
1403 if (!OpReg)
1404 return std::nullopt;
1405 OperationRegs.push_back(Elt: *OpReg);
1406 }
1407
1408 auto [It, Inserted] =
1409 DebugExpressionCache.try_emplace(Key: std::move(OperationRegs));
1410 if (!Inserted)
1411 return It->second;
1412
1413 MCRegister Reg = emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugExpression,
1414 VoidTypeReg, ExtInstSetReg, Operands: It->first, MAI);
1415 It->second = Reg;
1416 return Reg;
1417}
1418
1419std::optional<MCRegister> SPIRVNonSemanticDebugHandler::emitDebugGlobalVariable(
1420 const DIGlobalVariable *GV, const GlobalVariableDebugInfo &Info,
1421 MCRegister VoidTypeReg, MCRegister I32TypeReg, MCRegister ExtInstSetReg,
1422 SPIRV::ModuleAnalysisInfo &MAI) {
1423 assert(GV && "GV must not be null in emitDebugGlobalVariable");
1424
1425 EmitResult<MCRegister> Parent = resolveScope(Scope: GV->getScope());
1426 if (!Parent)
1427 return std::nullopt;
1428
1429 MCRegister ParentReg = *Parent.Value;
1430
1431 // TyReg: DebugInfoNone when GV has no DI type (as done in
1432 // SPIRV-LLVM-Translator). Declarations (isDefinition: false) can have null
1433 // getType() while definitions must have a non-null one (enforced by the IR
1434 // verifier).
1435 MCRegister TyReg = CachedDebugInfoNoneReg;
1436 if (const DIType *Ty = GV->getType()) {
1437 auto TyRegOpt = lookupOptReg(Map: DebugScopeRegs, Key: Ty);
1438 if (!TyRegOpt)
1439 return std::nullopt;
1440 TyReg = *TyRegOpt;
1441 }
1442
1443 std::optional<MCRegister> StaticMemberRegOpt;
1444 if (const DIDerivedType *SM = GV->getStaticDataMemberDeclaration()) {
1445 // Do not create the member here, just look it up to avoid orphaning it.
1446 StaticMemberRegOpt = lookupOptReg(Map: DebugScopeRegs, Key: SM);
1447 if (!StaticMemberRegOpt)
1448 return std::nullopt;
1449 }
1450
1451 MCRegister NameReg = getCachedOpStringReg(S: GV->getName());
1452 MCRegister LinkageReg = getCachedOpStringReg(S: GV->getLinkageName());
1453 MCRegister FileStrReg = getCachedScopePathOpStringReg(
1454 Scope: GV->getFile(), /*UseEmptyPathIfNullScope=*/true);
1455 MCRegister SrcReg = getOrEmitDebugSourceForFileStrReg(FileStrReg, VoidTypeReg,
1456 ExtInstSetReg, MAI);
1457
1458 MCRegister LineReg =
1459 emitOpConstantI32(Value: static_cast<uint32_t>(GV->getLine()), I32TypeReg, MAI);
1460 // DIGlobalVariable or DIGlobalVariableExpression metadata carry no column
1461 // field. Column is hardcoded to 0 (because it can't be determined), matching
1462 // SPIRV-LLVM-Translator.
1463 MCRegister ColReg = emitOpConstantI32(Value: 0, I32TypeReg, MAI);
1464
1465 // Variable: @g OpVariable id when !dbg matches; else a DebugExpression for
1466 // the GVE init value when no @g exists and the expression is non-empty; else
1467 // DebugInfoNone. As per spec, the DebugExpression must contain the constant
1468 // value of the variable that was optimized out. An empty expression contains
1469 // no value, so we emit DebugInfoNone instead.
1470 MCRegister VariableReg = CachedDebugInfoNoneReg;
1471 if (const GlobalVariable *LLVMGV = Info.LLVMGV) {
1472 MCRegister GVReg = MAI.getGlobalObjReg(GO: LLVMGV);
1473 if (GVReg.isValid())
1474 VariableReg = GVReg;
1475 } else if (Info.Expr && Info.Expr->getNumElements() != 0) {
1476 if (auto ExprReg = emitDebugExpression(Expr: Info.Expr, VoidTypeReg, I32TypeReg,
1477 ExtInstSetReg, MAI))
1478 VariableReg = *ExprReg;
1479 }
1480
1481 MCRegister FlagsReg = emitOpConstantI32(Value: transDebugFlags(DN: GV), I32TypeReg, MAI);
1482
1483 SmallVector<MCRegister, 10> Ops = {NameReg, TyReg, SrcReg,
1484 LineReg, ColReg, ParentReg,
1485 LinkageReg, VariableReg, FlagsReg};
1486
1487 if (StaticMemberRegOpt)
1488 Ops.push_back(Elt: *StaticMemberRegOpt);
1489
1490 return emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugGlobalVariable,
1491 VoidTypeReg, ExtInstSetReg, Operands: Ops, MAI);
1492}
1493
1494std::optional<MCRegister> SPIRVNonSemanticDebugHandler::emitDebugLocalVariable(
1495 const DILocalVariable *LV, MCRegister VoidTypeReg, MCRegister I32TypeReg,
1496 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI) {
1497 assert(LV && "LV must not be null in emitDebugLocalVariable");
1498
1499 EmitResult<MCRegister> Parent = resolveScope(Scope: LV->getScope());
1500 if (!Parent)
1501 return std::nullopt;
1502
1503 MCRegister TyReg = CachedDebugInfoNoneReg;
1504 if (const DIType *Ty = LV->getType()) {
1505 auto TyRegOpt = lookupOptReg(Map: DebugScopeRegs, Key: Ty);
1506 if (!TyRegOpt)
1507 return std::nullopt;
1508 TyReg = *TyRegOpt;
1509 }
1510
1511 MCRegister NameReg = getCachedOpStringReg(S: LV->getName());
1512 MCRegister FileStrReg = getCachedScopePathOpStringReg(
1513 Scope: LV->getFile(), /*UseEmptyPathIfNullScope=*/true);
1514 MCRegister SrcReg = getOrEmitDebugSourceForFileStrReg(FileStrReg, VoidTypeReg,
1515 ExtInstSetReg, MAI);
1516 MCRegister LineReg =
1517 emitOpConstantI32(Value: static_cast<uint32_t>(LV->getLine()), I32TypeReg, MAI);
1518 // DILocalVariable has no column field. Column is hardcoded to 0.
1519 MCRegister ColReg = emitOpConstantI32(Value: 0, I32TypeReg, MAI);
1520 MCRegister FlagsReg = emitOpConstantI32(Value: transDebugFlags(DN: LV), I32TypeReg, MAI);
1521
1522 SmallVector<MCRegister, 8> Ops = {NameReg, TyReg, SrcReg, LineReg,
1523 ColReg, *Parent.Value, FlagsReg};
1524 if (unsigned Arg = LV->getArg())
1525 Ops.push_back(Elt: emitOpConstantI32(Value: Arg, I32TypeReg, MAI));
1526
1527 return emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugLocalVariable, VoidTypeReg,
1528 ExtInstSetReg, Operands: Ops, MAI);
1529}
1530
1531auto SPIRVNonSemanticDebugHandler::emitDebugTypeVector(
1532 const DICompositeType *VT, SPIRV::ModuleAnalysisInfo &MAI)
1533 -> EmitResult<PreparedScope> {
1534 const auto *BaseTy = dyn_cast_or_null<DIBasicType>(Val: VT->getBaseType());
1535 if (!BaseTy)
1536 return EmitResult<PreparedScope>::unsupported();
1537
1538 // DebugTypeVector models only 1D vectors (multi-subrange types cannot be
1539 // encoded).
1540 DINodeArray Elements = VT->getElements();
1541 if (Elements.size() != 1)
1542 return EmitResult<PreparedScope>::unsupported();
1543 const auto *SR = cast<DISubrange>(Val: Elements[0]);
1544 const auto *CI = dyn_cast_if_present<ConstantInt *>(Val: SR->getCount());
1545 if (!CI)
1546 return EmitResult<PreparedScope>::unsupported();
1547
1548 EmitResult<MCRegister> Base = getOrCreateDebugScope(S: BaseTy);
1549 if (!Base)
1550 return EmitResult<PreparedScope>::failure(Other: Base);
1551
1552 MCRegister I32TypeReg = getOrEmitOpTypeInt32Reg(MAI);
1553 MCRegister CountReg = emitOpConstantI32(
1554 Value: static_cast<uint32_t>(CI->getZExtValue()), I32TypeReg, MAI);
1555 return EmitResult<PreparedScope>::emitted(V: PreparedScope{
1556 SPIRV::NonSemanticExtInst::DebugTypeVector, {*Base.Value, CountReg}});
1557}
1558
1559auto SPIRVNonSemanticDebugHandler::emitDebugTypeArray(
1560 const DICompositeType *AT, SPIRV::ModuleAnalysisInfo &MAI)
1561 -> EmitResult<PreparedScope> {
1562 // Unlike DebugTypeVector, the element may be any debug type, not only a
1563 // basic type. Create it first so this array names an id that already exists.
1564 EmitResult<MCRegister> Base = getOrCreateDebugScope(S: AT->getBaseType());
1565 if (!Base)
1566 return EmitResult<PreparedScope>::failure(Other: Base);
1567
1568 MCRegister I32TypeReg = getOrEmitOpTypeInt32Reg(MAI);
1569
1570 SmallVector<MCRegister> Ops;
1571 Ops.push_back(Elt: *Base.Value);
1572
1573 // One component count per DISubrange, in DWARF subrange order. Emit 0 for
1574 // counts that are not a compile-time constant (dynamic arrays). This matches
1575 // OpTypeRuntimeArray.
1576 for (const DINode *Element : AT->getElements()) {
1577 const auto *SR = dyn_cast<DISubrange>(Val: Element);
1578 if (!SR)
1579 continue;
1580 // A DIVariable count (a variable-length array) is not a ConstantInt, so it
1581 // maps to 0 here. DebugTypeArray also allows a DebugLocalVariable or
1582 // DebugGlobalVariable id for it, but no frontend we target emits one. A
1583 // constant wider than 32 bits maps to 0 too, since the count operand is a
1584 // 32-bit OpConstant and such an array cannot occur in a shader.
1585 uint32_t Count = 0;
1586 if (const auto *CI = dyn_cast_if_present<ConstantInt *>(Val: SR->getCount())) {
1587 const APInt &Value = CI->getValue();
1588 if (Value.getActiveBits() <= 32)
1589 Count = static_cast<uint32_t>(Value.getZExtValue());
1590 }
1591 Ops.push_back(Elt: emitOpConstantI32(Value: Count, I32TypeReg, MAI));
1592 }
1593
1594 return EmitResult<PreparedScope>::emitted(
1595 V: PreparedScope{SPIRV::NonSemanticExtInst::DebugTypeArray, Ops});
1596}
1597
1598auto SPIRVNonSemanticDebugHandler::emitDebugTypeMember(
1599 const DIDerivedType *M, MCRegister VoidTypeReg, MCRegister I32TypeReg,
1600 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI)
1601 -> EmitResult<PreparedScope> {
1602 EmitResult<MCRegister> Ty = getOrCreateDebugScope(S: M->getBaseType());
1603 if (!Ty)
1604 return EmitResult<PreparedScope>::failure(Other: Ty);
1605
1606 if (!isUInt<32>(x: M->getOffsetInBits()) || !isUInt<32>(x: M->getSizeInBits()))
1607 return EmitResult<PreparedScope>::unsupported();
1608
1609 MCRegister NameReg = getCachedOpStringReg(S: M->getName());
1610 MCRegister FileStrReg = getCachedScopePathOpStringReg(
1611 Scope: M->getFile(), /*UseEmptyPathIfNullScope=*/true);
1612 MCRegister SrcReg = getOrEmitDebugSourceForFileStrReg(FileStrReg, VoidTypeReg,
1613 ExtInstSetReg, MAI);
1614 MCRegister LineReg =
1615 emitOpConstantI32(Value: static_cast<uint32_t>(M->getLine()), I32TypeReg, MAI);
1616
1617 // DIDerivedType members carry no column, so emit 0.
1618 MCRegister ColReg = emitOpConstantI32(Value: 0, I32TypeReg, MAI);
1619 MCRegister OffsetReg = emitOpConstantI32(
1620 Value: static_cast<uint32_t>(M->getOffsetInBits()), I32TypeReg, MAI);
1621 MCRegister SizeReg = emitOpConstantI32(
1622 Value: static_cast<uint32_t>(M->getSizeInBits()), I32TypeReg, MAI);
1623 MCRegister FlagsReg = emitOpConstantI32(Value: transDebugFlags(DN: M), I32TypeReg, MAI);
1624
1625 // In NonSemantic.Shader.DebugInfo a DebugTypeMember has no Parent operand:
1626 // only the composite references its members. This is by design, it drops the
1627 // Parent that OpenCL.DebugInfo.100 had, and it avoids a composite/member
1628 // reference cycle.
1629 //
1630 // FIXME: Static members are not handled yet: their constant initializer is
1631 // available but is not emitted as the optional Value operand, and under DWARF
1632 // 5 a static member is tagged DW_TAG_variable, which the caller's member loop
1633 // skips.
1634 return EmitResult<PreparedScope>::emitted(
1635 V: PreparedScope{SPIRV::NonSemanticExtInst::DebugTypeMember,
1636 {NameReg, *Ty.Value, SrcReg, LineReg, ColReg, OffsetReg,
1637 SizeReg, FlagsReg}});
1638}
1639
1640auto SPIRVNonSemanticDebugHandler::emitDebugTypeComposite(
1641 const DICompositeType *CT, MCRegister VoidTypeReg, MCRegister I32TypeReg,
1642 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI)
1643 -> EmitResult<PreparedScope> {
1644 // Parent and size before any member: a failure must stream nothing, or a
1645 // retry would emit the members again.
1646 EmitResult<MCRegister> Parent = resolveScope(Scope: CT->getScope());
1647 if (!Parent)
1648 return EmitResult<PreparedScope>::failure(Other: Parent);
1649
1650 if (!isUInt<32>(x: CT->getSizeInBits()))
1651 return EmitResult<PreparedScope>::unsupported();
1652
1653 SmallVector<MCRegister, 8> MemberRegs;
1654 for (const DINode *Element : CT->getElements()) {
1655 const auto *M = dyn_cast<DIDerivedType>(Val: Element);
1656 if (!M || M->getTag() != dwarf::DW_TAG_member)
1657 continue;
1658 EmitResult<MCRegister> Member = getOrCreateDebugScope(S: M);
1659 if (Member)
1660 MemberRegs.push_back(Elt: *Member.Value);
1661 }
1662
1663 MCRegister NameReg = getCachedOpStringReg(S: CT->getName());
1664 MCRegister LinkageReg = getCachedOpStringReg(S: CT->getIdentifier());
1665 MCRegister FileStrReg = getCachedScopePathOpStringReg(
1666 Scope: CT->getFile(), /*UseEmptyPathIfNullScope=*/true);
1667 MCRegister SrcReg = getOrEmitDebugSourceForFileStrReg(FileStrReg, VoidTypeReg,
1668 ExtInstSetReg, MAI);
1669
1670 MCRegister TagReg =
1671 emitOpConstantI32(Value: mapCompositeTypeTag(Tag: CT->getTag()), I32TypeReg, MAI);
1672 MCRegister LineReg =
1673 emitOpConstantI32(Value: static_cast<uint32_t>(CT->getLine()), I32TypeReg, MAI);
1674 MCRegister ColReg = emitOpConstantI32(Value: 0, I32TypeReg, MAI);
1675
1676 // A forward declaration has no known size or members: Size is DebugInfoNone.
1677 MCRegister SizeReg = CachedDebugInfoNoneReg;
1678 if (!CT->isForwardDecl())
1679 SizeReg = emitOpConstantI32(Value: static_cast<uint32_t>(CT->getSizeInBits()),
1680 I32TypeReg, MAI);
1681
1682 MCRegister FlagsReg = emitOpConstantI32(Value: transDebugFlags(DN: CT), I32TypeReg, MAI);
1683
1684 SmallVector<MCRegister> Ops = {NameReg, TagReg, SrcReg,
1685 LineReg, ColReg, *Parent.Value,
1686 LinkageReg, SizeReg, FlagsReg};
1687 Ops.append(in_start: MemberRegs.begin(), in_end: MemberRegs.end());
1688 return EmitResult<PreparedScope>::emitted(
1689 V: PreparedScope{SPIRV::NonSemanticExtInst::DebugTypeComposite, Ops});
1690}
1691
1692auto SPIRVNonSemanticDebugHandler::emitDebugTypeEnum(
1693 const DICompositeType *ET, MCRegister VoidTypeReg, MCRegister I32TypeReg,
1694 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI)
1695 -> EmitResult<PreparedScope> {
1696 // Skip incomplete enums.
1697 uint64_t Size = ET->getSizeInBits();
1698 if (Size == 0 || !isUInt<32>(x: Size))
1699 return EmitResult<PreparedScope>::unsupported();
1700
1701 EmitResult<MCRegister> Underlying =
1702 getOrCreateDebugTypeOrNone(Ty: ET->getBaseType());
1703 if (!Underlying)
1704 return EmitResult<PreparedScope>::failure(Other: Underlying);
1705
1706 EmitResult<MCRegister> Parent =
1707 resolveScope(Scope: ET->getScope(), FallbackCU: EnumCompileUnits.lookup(Val: ET));
1708 if (!Parent)
1709 return EmitResult<PreparedScope>::failure(Other: Parent);
1710
1711 MCRegister NameReg = getCachedOpStringReg(S: ET->getName());
1712 MCRegister FileStrReg = getCachedScopePathOpStringReg(
1713 Scope: ET->getFile(), /*UseEmptyPathIfNullScope=*/true);
1714 MCRegister SrcReg = getOrEmitDebugSourceForFileStrReg(FileStrReg, VoidTypeReg,
1715 ExtInstSetReg, MAI);
1716 MCRegister LineReg =
1717 emitOpConstantI32(Value: static_cast<uint32_t>(ET->getLine()), I32TypeReg, MAI);
1718 // DICompositeType carries no column, so emit 0.
1719 MCRegister ColReg = emitOpConstantI32(Value: 0, I32TypeReg, MAI);
1720 MCRegister SizeReg =
1721 emitOpConstantI32(Value: static_cast<uint32_t>(Size), I32TypeReg, MAI);
1722 MCRegister FlagsReg = emitOpConstantI32(Value: transDebugFlags(DN: ET), I32TypeReg, MAI);
1723
1724 SmallVector<MCRegister> Ops = {NameReg, *Underlying.Value, SrcReg, LineReg,
1725 ColReg, *Parent.Value, SizeReg, FlagsReg};
1726
1727 // Each Value is a 32-bit integer constant. Skip the Value and Name pair of an
1728 // enumerator whose value needs more bits. Otherwise, the truncation would
1729 // show a different value.
1730 for (const DINode *Element : ET->getElements()) {
1731 const auto *E = dyn_cast<DIEnumerator>(Val: Element);
1732 if (!E)
1733 continue;
1734 const APInt &Value = E->getValue();
1735 if (E->isUnsigned() ? !Value.isIntN(N: 32) : !Value.isSignedIntN(N: 32))
1736 continue;
1737 uint32_t Bits = E->isUnsigned()
1738 ? static_cast<uint32_t>(Value.getZExtValue())
1739 : static_cast<uint32_t>(Value.getSExtValue());
1740 Ops.push_back(Elt: emitOpConstantI32(Value: Bits, I32TypeReg, MAI));
1741 Ops.push_back(Elt: getCachedOpStringReg(S: E->getName()));
1742 }
1743
1744 return EmitResult<PreparedScope>::emitted(
1745 V: PreparedScope{SPIRV::NonSemanticExtInst::DebugTypeEnum, Ops});
1746}
1747
1748auto SPIRVNonSemanticDebugHandler::emitDebugTypedef(
1749 const DIDerivedType *TD, MCRegister VoidTypeReg, MCRegister I32TypeReg,
1750 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI)
1751 -> EmitResult<PreparedScope> {
1752 EmitResult<MCRegister> Base = getOrCreateDebugScope(S: TD->getBaseType());
1753 if (!Base)
1754 return EmitResult<PreparedScope>::failure(Other: Base);
1755
1756 MCRegister NameReg = getCachedOpStringReg(S: TD->getName());
1757 MCRegister FileStrReg = getCachedScopePathOpStringReg(
1758 Scope: TD->getFile(), /*UseEmptyPathIfNullScope=*/true);
1759 MCRegister SrcReg = getOrEmitDebugSourceForFileStrReg(FileStrReg, VoidTypeReg,
1760 ExtInstSetReg, MAI);
1761 MCRegister LineReg =
1762 emitOpConstantI32(Value: static_cast<uint32_t>(TD->getLine()), I32TypeReg, MAI);
1763 // DIDerivedType typedefs carry no column, so emit 0.
1764 MCRegister ColReg = emitOpConstantI32(Value: 0, I32TypeReg, MAI);
1765
1766 // Parent must be a lexical scope. Valid NSDI lexical scopes are
1767 // DebugCompilationUnit, DebugFunction, DebugLexicalBlock, or
1768 // DebugTypeComposite.
1769 EmitResult<MCRegister> Parent = resolveScope(Scope: TD->getScope());
1770 if (!Parent)
1771 return EmitResult<PreparedScope>::failure(Other: Parent);
1772
1773 return EmitResult<PreparedScope>::emitted(V: PreparedScope{
1774 SPIRV::NonSemanticExtInst::DebugTypedef,
1775 {NameReg, *Base.Value, SrcReg, LineReg, ColReg, *Parent.Value}});
1776}
1777
1778void SPIRVNonSemanticDebugHandler::emitNonSemanticDebugStrings(
1779 SPIRV::ModuleAnalysisInfo &MAI) {
1780 if (CompileUnits.empty())
1781 return;
1782 // Check that prepareModuleOutput() registered the extended instruction set.
1783 // If the subtarget does not support the extension, neither strings nor ext
1784 // insts are emitted.
1785 if (!MAI.getExtInstSetReg(SetNum: NSSet).isValid())
1786 return;
1787
1788 for (const CompileUnitInfo &Info : CompileUnits) {
1789 if (Info.TheCU) {
1790 MCRegister PathReg = emitOpStringIfNew(S: Info.FilePath, MAI);
1791 ScopeToPathOpStringReg[Info.TheCU] = PathReg;
1792 if (const DIFile *F = Info.TheCU->getFile())
1793 ScopeToPathOpStringReg[F] = PathReg;
1794 }
1795 }
1796
1797 for (const DISubprogram *SP : Subprograms) {
1798 emitOpStringIfNew(S: SP->getName(), MAI);
1799 emitOpStringIfNew(S: SP->getLinkageName(), MAI);
1800 emitAndCacheScopePathOpStringReg(Scope: SP, MAI);
1801 }
1802
1803 for (const DIType *Ty : DebugTypes) {
1804 if (const auto *BT = dyn_cast<DIBasicType>(Val: Ty)) {
1805 emitOpStringIfNew(S: BT->getName(), MAI);
1806 continue;
1807 }
1808 if (const auto *CT = dyn_cast<DICompositeType>(Val: Ty)) {
1809 // Cache the enum's name, file path and enumerator names as OpStrings for
1810 // DebugTypeEnum.
1811 if (CT->getTag() == dwarf::DW_TAG_enumeration_type) {
1812 emitOpStringIfNew(S: CT->getName(), MAI);
1813 emitAndCacheScopePathOpStringReg(Scope: CT->getFile(), MAI);
1814 for (const DINode *Element : CT->getElements())
1815 if (const auto *E = dyn_cast<DIEnumerator>(Val: Element))
1816 emitOpStringIfNew(S: E->getName(), MAI);
1817 continue;
1818 }
1819 // Cache the OpStrings each DebugTypeComposite and its DebugTypeMembers
1820 // use: the composite name, identifier (linkage name), and path, plus
1821 // each member name and path. Arrays and vectors have no such strings.
1822 if (CT->getTag() != dwarf::DW_TAG_structure_type &&
1823 CT->getTag() != dwarf::DW_TAG_class_type &&
1824 CT->getTag() != dwarf::DW_TAG_union_type)
1825 continue;
1826 emitOpStringIfNew(S: CT->getName(), MAI);
1827 emitOpStringIfNew(S: CT->getIdentifier(), MAI);
1828 emitAndCacheScopePathOpStringReg(Scope: CT->getFile(), MAI);
1829 for (const DINode *Element : CT->getElements()) {
1830 const auto *M = dyn_cast<DIDerivedType>(Val: Element);
1831 if (!M || M->getTag() != dwarf::DW_TAG_member)
1832 continue;
1833 emitOpStringIfNew(S: M->getName(), MAI);
1834 emitAndCacheScopePathOpStringReg(Scope: M->getFile(), MAI);
1835 }
1836 continue;
1837 }
1838 // Cache the name and path OpStrings each DebugTypedef uses.
1839 const auto *TD = dyn_cast<DIDerivedType>(Val: Ty);
1840 if (TD && TD->getTag() == dwarf::DW_TAG_typedef) {
1841 emitOpStringIfNew(S: TD->getName(), MAI);
1842 emitAndCacheScopePathOpStringReg(Scope: TD->getFile(), MAI);
1843 }
1844 }
1845
1846 for (const auto &[GV, _] : GlobalVariableDebugInfoMap) {
1847 emitOpStringIfNew(S: GV->getName(), MAI);
1848 emitOpStringIfNew(S: GV->getLinkageName(), MAI);
1849 emitAndCacheScopePathOpStringReg(Scope: GV->getFile(), MAI);
1850 }
1851
1852 for (const DILocalVariable *LV : LocalVariables) {
1853 emitOpStringIfNew(S: LV->getName(), MAI);
1854 emitAndCacheScopePathOpStringReg(Scope: LV->getFile(), MAI);
1855 }
1856
1857 for (const DIScope *S : LexicalBlocks) {
1858 emitAndCacheScopePathOpStringReg(Scope: S->getFile(), MAI);
1859 if (const auto *NS = dyn_cast<DINamespace>(Val: S))
1860 emitOpStringIfNew(S: NS->getName(), MAI);
1861 }
1862
1863 for (const DILocation *DL : UniqueDebugLocations)
1864 emitAndCacheScopePathOpStringReg(Scope: DL->getScope(), MAI);
1865
1866 CachedEmptyStringReg = emitOpStringIfNew(S: "", MAI);
1867
1868#ifndef NDEBUG
1869 NonSemanticOpStringsSectionEmitted = true;
1870#endif
1871}
1872
1873void SPIRVNonSemanticDebugHandler::emitDebugFunctionDefinition(
1874 MCRegister DebugFunctionReg, MCRegister OpFunctionReg,
1875 SPIRV::ModuleAnalysisInfo &MAI) {
1876 assert(DebugFunctionReg.isValid() && OpFunctionReg.isValid() &&
1877 "DebugFunctionDefinition operands must be valid");
1878 MCRegister VoidTypeReg = getOrEmitOpTypeVoidReg(MAI);
1879 MCRegister ExtInstSetReg = MAI.getExtInstSetReg(SetNum: NSSet);
1880 emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugFunctionDefinition, VoidTypeReg,
1881 ExtInstSetReg, Operands: {DebugFunctionReg, OpFunctionReg}, MAI);
1882}
1883
1884void SPIRVNonSemanticDebugHandler::resetPerFunctionDebugState() {
1885 CurrentMF = nullptr;
1886 LastFunctionOpVariable = nullptr;
1887 DebugFunctionDefinitionEmitted = false;
1888 LastLineMI = nullptr;
1889 LastScopeMI = nullptr;
1890 Records.clear();
1891}
1892
1893void SPIRVNonSemanticDebugHandler::preparePerFunctionDebug(
1894 const MachineFunction *MF) {
1895 resetPerFunctionDebugState();
1896 if (!GlobalNSDIEnabled || !CurrentMAI)
1897 return;
1898
1899 CurrentMF = MF;
1900
1901 if (MF->getFunction()
1902 .getFnAttribute(SPIRV_BACKEND_SERVICE_FUN_NAME)
1903 .isValid())
1904 return;
1905
1906 const DISubprogram *SP = MF->getFunction().getSubprogram();
1907 if (!SP || !SP->isDefinition())
1908 return;
1909
1910 // DebugFunctionDefinition is emitted after the last function-level
1911 // OpVariable. If there are none, it is emitted after the entry OpLabel.
1912 LastFunctionOpVariable =
1913 findLastFunctionOpVariableDeclaration(MF: *MF, MAI&: *CurrentMAI);
1914
1915 analyzeDebugRecords(MF: *MF);
1916}
1917
1918void SPIRVNonSemanticDebugHandler::tryEmitDebugFunctionDefinition(
1919 SPIRV::ModuleAnalysisInfo &MAI) {
1920 if (DebugFunctionDefinitionEmitted || !GlobalNSDIEnabled)
1921 return;
1922
1923 assert(CurrentMF && "no current MachineFunction");
1924 const Function &F = CurrentMF->getFunction();
1925 const DISubprogram *SP = F.getSubprogram();
1926 if (!SP || !SP->isDefinition())
1927 return;
1928
1929 auto DFIt = DebugScopeRegs.find(Val: SP);
1930 if (DFIt == DebugScopeRegs.end())
1931 return;
1932
1933 MCRegister OpFunctionReg = MAI.getGlobalObjReg(GO: &F);
1934 if (!OpFunctionReg.isValid())
1935 return;
1936
1937 emitDebugFunctionDefinition(DebugFunctionReg: DFIt->second, OpFunctionReg, MAI);
1938 DebugFunctionDefinitionEmitted = true;
1939}
1940
1941void SPIRVNonSemanticDebugHandler::beginFunctionImpl(
1942 const MachineFunction *MF) {
1943 preparePerFunctionDebug(MF);
1944}
1945
1946void SPIRVNonSemanticDebugHandler::endFunctionImpl(const MachineFunction *MF) {
1947 (void)MF;
1948 resetPerFunctionDebugState();
1949}
1950
1951void SPIRVNonSemanticDebugHandler::beginInstruction(const MachineInstr *MI) {
1952 assert(CurMI == nullptr && "CurMI must be null");
1953 CurMI = MI;
1954
1955 if (!DebugFunctionDefinitionEmitted)
1956 return;
1957
1958 emitAnalyzedRecords(Anchor: MI);
1959
1960 std::optional<const MachineInstr *> Target = resolveDebugLocTarget(MI);
1961 if (!Target)
1962 return;
1963
1964 (void)emitDebugScopeForInstruction(MI: *Target);
1965 emitDebugLineForInstruction(MI: *Target);
1966}
1967
1968// The register that holds the variable's address in \p MI, or std::nullopt
1969// when \p MI is not a declare this backend can describe.
1970//
1971// The spec requires DebugDeclare's Variable operand to be "the <id> of an
1972// OpVariable instruction that defines the local variable". MIR has no
1973// DBG_DECLARE, so what this looks for is an indirect DBG_VALUE whose location
1974// register an OpVariable defines.
1975static std::optional<Register>
1976getDebugDeclareStorageReg(const MachineInstr &MI) {
1977 // #dbg_declare is an indirect DBG_VALUE in MIR; #dbg_value is normally a
1978 // direct one except for the variadic case.
1979 if (!MI.isIndirectDebugValue())
1980 return std::nullopt;
1981
1982 // A variadic #dbg_value becomes DBG_VALUE $noreg, 0, ... which is indirect
1983 // too, and $noreg is not virtual.
1984 Register LocReg = MI.getDebugOperand(Index: 0).getReg();
1985 if (!LocReg.isVirtual())
1986 return std::nullopt;
1987
1988 // DebugDeclare can only encode the address of an OpVariable.
1989 // Other legitimate #dbg_declare cannot be encoded.
1990 // Examples: an access chain for a field, an OpFunctionParameter for a byval
1991 // argument, or a module-scope constant for a null or fixed address.
1992
1993 // LocReg may also have no def at all: erasing dead storage leaves the
1994 // DBG_VALUE pointing at an undefined register. MachineVerifier permits that
1995 // because LiveDebugVariables normally clears it, but this pipeline has no
1996 // register allocation, so LiveDebugVariables never runs.
1997 const MachineInstr *Def = MI.getMF()->getRegInfo().getUniqueVRegDef(Reg: LocReg);
1998 if (!Def || Def->getOpcode() != SPIRV::OpVariable)
1999 return std::nullopt;
2000
2001 return LocReg;
2002}
2003
2004void SPIRVNonSemanticDebugHandler::emitDebugBinding(
2005 SPIRV::NonSemanticExtInst::NonSemanticExtInst Opcode,
2006 const MachineInstr *MI, MCRegister LocationReg) {
2007 assert(DebugFunctionDefinitionEmitted &&
2008 "DebugFunctionDefinition must be emitted");
2009 assert(CurrentMAI && "CurrentMAI must be set");
2010
2011 auto VarRegOpt = lookupOptReg(Map: DebugLocalVariableRegs, Key: MI->getDebugVariable());
2012 if (!VarRegOpt)
2013 return;
2014
2015 auto ExprRegOpt = lookupOptReg(Map: DebugExpressionRegs, Key: MI->getDebugExpression());
2016 if (!ExprRegOpt)
2017 return;
2018
2019 if (!emitDebugScopeForInstruction(MI))
2020 return;
2021 emitDebugLineForInstruction(MI);
2022
2023 SPIRV::ModuleAnalysisInfo &MAI = *CurrentMAI;
2024 MCRegister VoidTypeReg = getOrEmitOpTypeVoidReg(MAI);
2025 MCRegister ExtInstSetReg = MAI.getExtInstSetReg(SetNum: NSSet);
2026 emitExtInst(Opcode, VoidTypeReg, ExtInstSetReg,
2027 Operands: {*VarRegOpt, LocationReg, *ExprRegOpt}, MAI);
2028}
2029
2030// The scalar type and value of the constant \p MI assigns, or nullopt when
2031// this backend cannot name it.
2032static std::optional<std::pair<const DIBasicType *, uint64_t>>
2033getConstantDbgValueBits(const MachineInstr &MI) {
2034 if (!MI.isNonListDebugValue() || MI.isIndirectDebugValue())
2035 return std::nullopt;
2036
2037 const MachineOperand &Value = MI.getDebugOperand(Index: 0);
2038 if (!Value.isImm() && !Value.isCImm() && !Value.isFPImm())
2039 return std::nullopt;
2040
2041 const DILocalVariable *LV = MI.getDebugVariable();
2042 const DIBasicType *BT = LV ? stripToScalarType(Ty: LV->getType()) : nullptr;
2043 if (!BT)
2044 return std::nullopt;
2045
2046 uint64_t Bits;
2047 if (Value.isImm())
2048 Bits = constantBits(Value: static_cast<uint64_t>(Value.getImm()), BT);
2049 else if (Value.isCImm())
2050 Bits = constantBits(Value: Value.getCImm()->getValue(), BT);
2051 else
2052 Bits = constantBits(Value: Value.getFPImm()->getValueAPF().bitcastToAPInt(), BT);
2053 return std::make_pair(x&: BT, y&: Bits);
2054}
2055
2056std::optional<MCRegister> SPIRVNonSemanticDebugHandler::getConstantValueReg(
2057 const MachineInstr &MI) const {
2058 std::optional<std::pair<const DIBasicType *, uint64_t>> Const =
2059 getConstantDbgValueBits(MI);
2060 if (!Const)
2061 return std::nullopt;
2062
2063 auto It = ConstantValueRegs.find(Val: *Const);
2064 if (It == ConstantValueRegs.end() || !It->second.isValid())
2065 return std::nullopt;
2066 return It->second;
2067}
2068
2069static std::optional<Register> getDebugValueReg(const MachineInstr &MI) {
2070 if (!MI.isNonListDebugValue() || MI.isIndirectDebugValue())
2071 return std::nullopt;
2072
2073 const MachineOperand &Value = MI.getDebugOperand(Index: 0);
2074 if (!Value.isReg())
2075 return std::nullopt;
2076
2077 Register ValueReg = Value.getReg();
2078 if (!ValueReg.isVirtual())
2079 return std::nullopt;
2080
2081 const MachineInstr *Def = MI.getMF()->getRegInfo().getUniqueVRegDef(Reg: ValueReg);
2082 if (!Def || (Def->isPseudo() && !Def->isPHI()) || Def->isMetaInstruction() ||
2083 Def->getNumOperands() == 0 || !Def->getOperand(i: 0).isReg() ||
2084 !Def->getOperand(i: 0).isDef() || Def->getOperand(i: 0).getReg() != ValueReg)
2085 return std::nullopt;
2086
2087 return ValueReg;
2088}
2089
2090static bool isMergeInstruction(unsigned Opcode) {
2091 return Opcode == SPIRV::OpSelectionMerge || Opcode == SPIRV::OpLoopMerge ||
2092 Opcode == SPIRV::OpLoopControlINTEL;
2093}
2094
2095// Whether \p MI becomes a SPIR-V instruction in the output.
2096static bool isEmitted(const MachineInstr *MI, SPIRV::ModuleAnalysisInfo &MAI) {
2097 return !MI->isMetaInstruction() && !MAI.getSkipEmission(MI);
2098}
2099
2100static bool isDebugLocTarget(const MachineInstr *MI,
2101 SPIRV::ModuleAnalysisInfo &MAI) {
2102 if (!isEmitted(MI, MAI))
2103 return false;
2104 switch (MI->getOpcode()) {
2105 case SPIRV::OpFunction:
2106 case SPIRV::OpFunctionParameter:
2107 case SPIRV::OpFunctionEnd:
2108 case SPIRV::OpLabel:
2109 case TargetOpcode::PHI:
2110 return false;
2111 default:
2112 return true;
2113 }
2114}
2115
2116static const MachineInstr *
2117findAdjacentEmittedInstruction(const MachineInstr *MI,
2118 SPIRV::ModuleAnalysisInfo &MAI, bool Forward) {
2119 for (const MachineInstr *Adj = Forward ? MI->getNextNode()
2120 : MI->getPrevNode();
2121 Adj; Adj = Forward ? Adj->getNextNode() : Adj->getPrevNode()) {
2122 if (!isEmitted(MI: Adj, MAI))
2123 continue;
2124 return Adj;
2125 }
2126 return nullptr;
2127}
2128
2129std::optional<const MachineInstr *>
2130SPIRVNonSemanticDebugHandler::resolveDebugLocTarget(const MachineInstr *MI) {
2131 assert(CurrentMAI && "CurrentMAI must be set");
2132 SPIRV::ModuleAnalysisInfo &MAI = *CurrentMAI;
2133
2134 // Structural opcodes don't require a DebugLine/DebugScope, other opcodes
2135 // might have already been emitted in the module scope.
2136 if (!isDebugLocTarget(MI, MAI))
2137 return std::nullopt;
2138
2139 // DebugLine/DebugScope can be emitted before a merge instruction, but not
2140 // after it (nothing may sit between the merge and its terminator). We can
2141 // use either the merge's or the terminator's debug info; we emit the
2142 // terminator's one.
2143 const MachineInstr *Prev = findAdjacentEmittedInstruction(MI, MAI, Forward: false);
2144 if (Prev && isMergeInstruction(Opcode: Prev->getOpcode()))
2145 return std::nullopt;
2146
2147 if (isMergeInstruction(Opcode: MI->getOpcode())) {
2148 // Use the terminator's debug info; when we reach it later, the check
2149 // above skips it.
2150 MI = findAdjacentEmittedInstruction(MI, MAI, Forward: true);
2151 assert(MI && "Merge instruction must be followed by a terminator");
2152 // The assert is compiled out in a release build, and returning MI there
2153 // would give an engaged optional holding null.
2154 if (!MI)
2155 return std::nullopt;
2156 }
2157
2158 return MI;
2159}
2160
2161void SPIRVNonSemanticDebugHandler::resolveDebugRecord(
2162 const MachineInstr &MI, const MachineInstr *LastEmitted,
2163 const MachineDominatorTree &DomTree) {
2164 SPIRV::ModuleAnalysisInfo &MAI = *CurrentMAI;
2165 const MachineFunction &MF = *MI.getMF();
2166
2167 // A record sits at its own position, except in a merge region: nothing may
2168 // be emitted between a merge and its terminator, so it belongs ahead of the
2169 // merge.
2170 const MachineInstr *Anchor =
2171 LastEmitted && isMergeInstruction(Opcode: LastEmitted->getOpcode()) ? LastEmitted
2172 : &MI;
2173
2174 // A #dbg_declare reaches Machine IR as an indirect DBG_VALUE. Its storage is
2175 // an OpVariable in the entry block, so it is in the output by the time any
2176 // record naming it is reached.
2177 if (std::optional<Register> LocReg = getDebugDeclareStorageReg(MI)) {
2178 MCRegister StorageReg = MAI.getRegisterAlias(MF: &MF, Reg: *LocReg);
2179 if (StorageReg.isValid())
2180 placeRecord(Opcode: SPIRV::NonSemanticExtInst::DebugDeclare, MI, LocationReg: StorageReg,
2181 Anchor);
2182 return;
2183 }
2184
2185 if (std::optional<MCRegister> ConstReg = getConstantValueReg(MI)) {
2186 placeRecord(Opcode: SPIRV::NonSemanticExtInst::DebugValue, MI, LocationReg: *ConstReg, Anchor);
2187 return;
2188 }
2189
2190 std::optional<Register> ValReg = getDebugValueReg(MI);
2191 if (!ValReg)
2192 return;
2193 MCRegister ValueReg = MAI.getRegisterAlias(MF: &MF, Reg: *ValReg);
2194 if (!ValueReg.isValid())
2195 return;
2196
2197 // NonSemantic.Shader.DebugInfo states that "Forward references are not
2198 // allowed, to be compliant with SPV_KHR_non_semantic_info", so the id has to
2199 // be in the output already. A module section precedes every function body.
2200 //
2201 // For anything else, SPIR-V requires a definition in a function block to
2202 // dominate a non-phi use, and requires a block to precede every block it
2203 // dominates, so a dominating definition is also one already printed. A
2204 // record naming a definition that does not dominate it is dropped.
2205 const MachineInstr *Def = MF.getRegInfo().getUniqueVRegDef(Reg: *ValReg);
2206 if (ModuleScopeIds.contains(V: ValueReg) ||
2207 (isEmitted(MI: Def, MAI) && DomTree.dominates(A: Def, B: &MI)))
2208 placeRecord(Opcode: SPIRV::NonSemanticExtInst::DebugValue, MI, LocationReg: ValueReg, Anchor);
2209}
2210
2211void SPIRVNonSemanticDebugHandler::analyzeDebugRecords(
2212 const MachineFunction &MF) {
2213 assert(CurrentMAI && "CurrentMAI must be set");
2214
2215 // Built on the first record rather than required as an analysis pass, which
2216 // would build a tree for every SPIR-V compilation.
2217 std::optional<MachineDominatorTree> DomTree;
2218
2219 for (const MachineBasicBlock &MBB : MF) {
2220 const MachineInstr *LastEmitted = nullptr;
2221 for (const MachineInstr &MI : MBB) {
2222 if (MI.isDebugValueLike()) {
2223 if (!DomTree)
2224 DomTree.emplace(args&: const_cast<MachineFunction &>(MF));
2225 resolveDebugRecord(MI, LastEmitted, DomTree: *DomTree);
2226 }
2227
2228 if (isEmitted(MI: &MI, MAI&: *CurrentMAI))
2229 LastEmitted = &MI;
2230 }
2231 }
2232}
2233
2234void SPIRVNonSemanticDebugHandler::placeRecord(
2235 SPIRV::NonSemanticExtInst::NonSemanticExtInst Opcode,
2236 const MachineInstr &MI, MCRegister LocationReg,
2237 const MachineInstr *Anchor) {
2238 Records[Anchor].push_back(Elt: {.MI: &MI, .Opcode: Opcode, .LocationReg: LocationReg});
2239}
2240
2241void SPIRVNonSemanticDebugHandler::emitAnalyzedRecords(
2242 const MachineInstr *Anchor) {
2243 auto It = Records.find(Val: Anchor);
2244 if (It == Records.end())
2245 return;
2246 for (const DebugRecord &R : It->second)
2247 emitDebugBinding(Opcode: R.Opcode, MI: R.MI, LocationReg: R.LocationReg);
2248}
2249
2250bool SPIRVNonSemanticDebugHandler::emitDebugScopeForInstruction(
2251 const MachineInstr *MI) {
2252 assert(DebugFunctionDefinitionEmitted &&
2253 "DebugFunctionDefinition must be emitted");
2254 assert(CurrentMAI && "CurrentMAI must be set");
2255
2256 // The region is implicitly closed at each basic block boundary, so a
2257 // LastScopeMI from another block must be dropped before it is read below:
2258 // the new block needs its own DebugScope, and has no region left to close.
2259 if (LastScopeMI && MI->getParent() != LastScopeMI->getParent())
2260 LastScopeMI = nullptr;
2261
2262 SPIRV::ModuleAnalysisInfo &MAI = *CurrentMAI;
2263 MCRegister VoidTypeReg = getOrEmitOpTypeVoidReg(MAI);
2264 MCRegister ExtInstSetReg = MAI.getExtInstSetReg(SetNum: NSSet);
2265
2266 const DILocation *CurDL = MI->getDebugLoc().get();
2267 if (!CurDL) {
2268 // No location for the current instruction.
2269 if (LastScopeMI) {
2270 // Close the current DebugScope region.
2271 emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugNoScope, VoidTypeReg,
2272 ExtInstSetReg, Operands: {}, MAI);
2273 LastScopeMI = nullptr;
2274 }
2275 return true;
2276 }
2277
2278 const DIScope *CurScope = CurDL->getScope();
2279 const DILocation *CurInlinedAt = CurDL->getInlinedAt();
2280
2281 if (LastScopeMI) {
2282 const DILocation *LastDL = LastScopeMI->getDebugLoc().get();
2283 if (LastDL->getScope() == CurScope &&
2284 LastDL->getInlinedAt() == CurInlinedAt)
2285 return true;
2286 }
2287
2288 EmitResult<MCRegister> CurScopeRes = resolveScope(Scope: CurScope);
2289 if (!CurScopeRes)
2290 return false;
2291
2292 SmallVector<MCRegister, 2> Ops{*CurScopeRes.Value};
2293 if (CurInlinedAt) {
2294 // If the global emission did not include this inlined-at case, we skip it.
2295 MCRegister InlinedReg = DebugInlinedAtRegs.lookup(Val: CurInlinedAt);
2296 if (!InlinedReg.isValid())
2297 return false;
2298 Ops.push_back(Elt: InlinedReg);
2299 }
2300
2301 // A new DebugScope region is needed.
2302 emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugScope, VoidTypeReg, ExtInstSetReg,
2303 Operands: Ops, MAI);
2304
2305 LastScopeMI = MI;
2306 return true;
2307}
2308
2309void SPIRVNonSemanticDebugHandler::emitDebugLineForInstruction(
2310 const MachineInstr *MI) {
2311 assert(DebugFunctionDefinitionEmitted &&
2312 "DebugFunctionDefinition must be emitted");
2313 assert(CurrentMAI && "CurrentMAI must be set");
2314
2315 // The region is implicitly closed at each basic block boundary, so a
2316 // LastLineMI from another block must be dropped before it is read below:
2317 // the new block needs its own DebugLine, and has no region left to close.
2318 if (LastLineMI && MI->getParent() != LastLineMI->getParent())
2319 LastLineMI = nullptr;
2320
2321 SPIRV::ModuleAnalysisInfo &MAI = *CurrentMAI;
2322 MCRegister VoidTypeReg = getOrEmitOpTypeVoidReg(MAI);
2323 MCRegister ExtInstSetReg = MAI.getExtInstSetReg(SetNum: NSSet);
2324
2325 const DILocation *DL = MI->getDebugLoc().get();
2326 if (!DL) {
2327 // No location for the current instruction
2328 if (LastLineMI) {
2329 // Close the current DebugLine region.
2330 emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugNoLine, VoidTypeReg,
2331 ExtInstSetReg, Operands: {}, MAI);
2332 LastLineMI = nullptr;
2333 }
2334 // No DebugLine region to close.
2335 return;
2336 }
2337
2338 // At this point, there is a location for the current instruction.
2339 // If it matches the last emitted DebugLine, no new DebugLine region is
2340 // needed. Otherwise, emit a new DebugLine region and update LastLineMI.
2341
2342 MCRegister FileStrReg = getCachedScopePathOpStringReg(
2343 Scope: DL->getScope(), /*UseEmptyPathIfNullScope=*/true);
2344 unsigned Line = DL->getLine();
2345 unsigned Col = DL->getColumn();
2346
2347 unsigned I32TypeId = CachedOpTypeInt32Reg.id();
2348 MCRegister SrcReg = DebugSourceRegByFileStr.lookup(Val: FileStrReg.id());
2349 MCRegister LineReg = ScalarConstantCache.lookup(Val: {I32TypeId, Line});
2350 MCRegister ColStartReg = ScalarConstantCache.lookup(Val: {I32TypeId, Col});
2351 MCRegister ColEndReg = ScalarConstantCache.lookup(Val: {I32TypeId, Col + 1});
2352
2353 // The elements of each collected DILocation (DebugSource, line/column
2354 // constants) are pre-emitted from LLVM-IR instruction !dbg attachments and
2355 // debug-program records; MIR is expected to reuse those same locations (or
2356 // carry none). A lookup miss means codegen attached a source position whose
2357 // elements were never pre-emitted, and debug-line emission is skipped.
2358 if (!SrcReg.isValid() || !LineReg.isValid() || !ColStartReg.isValid() ||
2359 !ColEndReg.isValid())
2360 return;
2361
2362 // Current location matches the last emitted DebugLine region.
2363 if (LastLineMI && MI->getDebugLoc() == LastLineMI->getDebugLoc())
2364 return;
2365
2366 // A new DebugLine region is needed.
2367 emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugLine, VoidTypeReg, ExtInstSetReg,
2368 Operands: {SrcReg, LineReg, LineReg, ColStartReg, ColEndReg}, MAI);
2369
2370 LastLineMI = MI;
2371}
2372
2373void SPIRVNonSemanticDebugHandler::endInstruction() {
2374 const MachineInstr *MI = CurMI;
2375 CurMI = nullptr;
2376
2377 if (!MI || !GlobalNSDIEnabled || !CurrentMF)
2378 return;
2379
2380 if (DebugFunctionDefinitionEmitted || MI != LastFunctionOpVariable)
2381 return;
2382
2383 // If this is the last function-level OpVariable, emit the
2384 // DebugFunctionDefinition. Otherwise, we had already done it before right
2385 // after the OpLabel (see notifyEntryLabelEmitted).
2386 assert(CurrentMAI && "CurrentMAI must be set");
2387 tryEmitDebugFunctionDefinition(MAI&: *CurrentMAI);
2388}
2389
2390void SPIRVNonSemanticDebugHandler::notifyEntryLabelEmitted(
2391 const MachineFunction &MF) {
2392 if (!GlobalNSDIEnabled || DebugFunctionDefinitionEmitted || !CurrentMF)
2393 return;
2394
2395 assert(CurrentMF == &MF &&
2396 "notification does not match the current MachineFunction");
2397
2398 if (LastFunctionOpVariable)
2399 return;
2400
2401 // If there are no function-level OpVariables, emit the
2402 // DebugFunctionDefinition. Otherwise, DebugFunctionDefinition is emitted
2403 // after the last OpVariable (see endInstruction).
2404 tryEmitDebugFunctionDefinition(MAI&: *CurrentMAI);
2405}
2406
2407void SPIRVNonSemanticDebugHandler::collectDebugExpressions(
2408 const SPIRV::ModuleAnalysisInfo &MAI,
2409 SetVector<const DIExpression *> &Out) const {
2410 for (const MachineInstr *MI : MAI.DebugValueMIs)
2411 Out.insert(X: MI->getDebugExpression());
2412}
2413
2414void SPIRVNonSemanticDebugHandler::emitNonSemanticGlobalDebugInfo(
2415 SPIRV::ModuleAnalysisInfo &MAI) {
2416 if (GlobalDIEmitted)
2417 return;
2418
2419 GlobalDIEmitted = true;
2420
2421 if (CompileUnits.empty()) {
2422 GlobalNSDIEnabled = false;
2423 return;
2424 }
2425
2426 // Retrieve the ext inst set register allocated by prepareModuleOutput().
2427 MCRegister ExtInstSetReg = MAI.getExtInstSetReg(SetNum: NSSet);
2428 if (!ExtInstSetReg.isValid()) {
2429 GlobalNSDIEnabled = false;
2430 return;
2431 }
2432
2433#ifndef NDEBUG
2434 assert(NonSemanticOpStringsSectionEmitted &&
2435 "emitNonSemanticDebugStrings() must run before "
2436 "emitNonSemanticGlobalDebugInfo()");
2437#endif
2438
2439 CurrentMAI = &MAI;
2440
2441 MCRegister VoidTypeReg = getOrEmitOpTypeVoidReg(MAI);
2442 MCRegister I32TypeReg = getOrEmitOpTypeInt32Reg(MAI);
2443
2444 CachedDebugInfoNoneReg = emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugInfoNone,
2445 VoidTypeReg, ExtInstSetReg, Operands: {}, MAI);
2446
2447 // Emit integer constants shared across all NSDI instructions. The constant
2448 // cache ensures each value is emitted at most once even when referenced from
2449 // multiple instructions. All constants are pre-emitted before any DebugSource
2450 // so that the output order is: constants, then
2451 // DebugSource+DebugCompilationUnit pairs. This keeps OpConstant instructions
2452 // grouped before the OpExtInst instructions.
2453
2454 // The Version operand of DebugCompilationUnit is the version of the
2455 // NonSemantic.Shader.DebugInfo instruction set, which is 100 for
2456 // "NonSemantic.Shader.DebugInfo.100" (NonSemanticShaderDebugInfo100Version).
2457 MCRegister DebugInfoVersionReg = emitOpConstantI32(Value: 100, I32TypeReg, MAI);
2458 MCRegister DwarfVersionReg =
2459 emitOpConstantI32(Value: static_cast<uint32_t>(DwarfVersion), I32TypeReg, MAI);
2460
2461 // Pre-emit source language constants for all compile units before entering
2462 // the DebugSource loop.
2463 SmallVector<MCRegister> SrcLangRegs =
2464 map_to_vector(C&: CompileUnits, F: [&](const CompileUnitInfo &Info) {
2465 return emitOpConstantI32(Value: Info.SpirvSourceLanguage, I32TypeReg, MAI);
2466 });
2467
2468 // Emit DebugSource and DebugCompilationUnit for each compile unit.
2469 for (auto [Info, SrcLangReg] : llvm::zip(t&: CompileUnits, u&: SrcLangRegs)) {
2470 MCRegister FileStrReg = ScopeToPathOpStringReg.lookup(Val: Info.TheCU);
2471 assert(FileStrReg.isValid() &&
2472 "CU path OpString must be emitted in emitNonSemanticDebugStrings");
2473 MCRegister DebugSourceReg = getOrEmitDebugSourceForFileStrReg(
2474 FileStrReg, VoidTypeReg, ExtInstSetReg, MAI);
2475 MCRegister CUDbgReg = emitExtInst(
2476 Opcode: SPIRV::NonSemanticExtInst::DebugCompilationUnit, VoidTypeReg,
2477 ExtInstSetReg,
2478 Operands: {DebugInfoVersionReg, DwarfVersionReg, DebugSourceReg, SrcLangReg},
2479 MAI);
2480 if (Info.TheCU)
2481 DebugScopeRegs[Info.TheCU] = CUDbgReg;
2482 }
2483
2484 for (const DIScope *S :
2485 concat<const DIScope *const>(Ranges&: LexicalBlocks, Ranges&: DebugTypes, Ranges&: Subprograms))
2486 getOrCreateDebugScope(S);
2487
2488 // Emit DebugLocalVariable after DebugFunction and their lexical blocks so the
2489 // Parent operand can resolve. Record the ids for DebugDeclare.
2490 for (const DILocalVariable *LV : LocalVariables)
2491 if (auto LVReg = emitDebugLocalVariable(LV, VoidTypeReg, I32TypeReg,
2492 ExtInstSetReg, MAI))
2493 DebugLocalVariableRegs[LV] = *LVReg;
2494
2495 // Opcodes like DebugDeclare are part of the function body, but
2496 // DebugExpression is not. For such opcodes, we collect the expressions
2497 // directly from the MIR to avoid inconsistencies with those in the LLVM IR
2498 // module.
2499 SetVector<const DIExpression *> Expressions;
2500 collectDebugExpressions(MAI, Out&: Expressions);
2501 for (const DIExpression *Expr : Expressions)
2502 if (auto ExprReg = emitDebugExpression(Expr, VoidTypeReg, I32TypeReg,
2503 ExtInstSetReg, MAI))
2504 DebugExpressionRegs[Expr] = *ExprReg;
2505
2506 // Emit DebugGlobalVariable for each collected DIGlobalVariable.
2507 for (const auto &[GV, Info] : GlobalVariableDebugInfoMap)
2508 emitDebugGlobalVariable(GV, Info, VoidTypeReg, I32TypeReg, ExtInstSetReg,
2509 MAI);
2510
2511 // Emit DebugInlinedAt allowing recursive inlining.
2512 for (const DILocation *DL : UniqueDebugLocations)
2513 if (const DILocation *IA = DL->getInlinedAt())
2514 getOrEmitDebugInlinedAt(IA, VoidTypeReg, I32TypeReg, ExtInstSetReg, MAI);
2515
2516 // Repeating the lookups emitDebugBinding() performs emits a type and a
2517 // constant only for an assignment that will reach a DebugValue.
2518 for (const MachineInstr *MI : MAI.DebugValueMIs) {
2519 std::optional<std::pair<const DIBasicType *, uint64_t>> Const =
2520 getConstantDbgValueBits(MI: *MI);
2521 if (!Const)
2522 continue;
2523 if (!DebugLocalVariableRegs.contains(Val: MI->getDebugVariable()) ||
2524 !DebugExpressionRegs.contains(Val: MI->getDebugExpression()))
2525 continue;
2526 auto [It, Inserted] = ConstantValueRegs.try_emplace(Key: *Const, Args: MCRegister());
2527 if (Inserted)
2528 It->second = findOrEmitScalarConstant(BT: Const->first, Value: Const->second, MAI);
2529 if (!It->second.isValid())
2530 ConstantValueRegs.erase(I: It);
2531 }
2532
2533 for (const DILocation *DL : UniqueDebugLocations) {
2534 emitOpConstantI32(Value: DL->getLine(), I32TypeReg, MAI);
2535 emitOpConstantI32(Value: DL->getColumn(), I32TypeReg, MAI);
2536 emitOpConstantI32(Value: DL->getColumn() + 1, I32TypeReg, MAI);
2537 MCRegister FileStrReg =
2538 getCachedScopePathOpStringReg(Scope: DL->getScope(),
2539 /*UseEmptyPathIfNullScope=*/true);
2540 getOrEmitDebugSourceForFileStrReg(FileStrReg, VoidTypeReg, ExtInstSetReg,
2541 MAI);
2542 }
2543
2544 GlobalNSDIEnabled = true;
2545}
2546
2547SmallString<128>
2548SPIRVNonSemanticDebugHandler::getDebugFullPath(const DIScope *Scope) const {
2549 SmallString<128> Out;
2550 if (!Scope)
2551 return Out;
2552 StringRef Filename = Scope->getFilename();
2553 const auto Style = sys::path::Style::native;
2554 if (sys::path::is_absolute(path: Filename, style: Style))
2555 Out.assign(in_start: Filename.begin(), in_end: Filename.end());
2556 else {
2557 StringRef Dir = Scope->getDirectory();
2558 Out.assign(in_start: Dir.begin(), in_end: Dir.end());
2559 sys::path::append(path&: Out, style: Style, a: Filename);
2560 }
2561 return Out;
2562}
2563
2564MCRegister SPIRVNonSemanticDebugHandler::getOrEmitDebugSourceForFileStrReg(
2565 MCRegister FileStrReg, MCRegister VoidTypeReg, MCRegister ExtInstSetReg,
2566 SPIRV::ModuleAnalysisInfo &MAI) {
2567 const unsigned Key = FileStrReg.id();
2568 auto It = DebugSourceRegByFileStr.find(Val: Key);
2569 if (It != DebugSourceRegByFileStr.end())
2570 return It->second;
2571
2572 MCRegister DS = emitExtInst(Opcode: SPIRV::NonSemanticExtInst::DebugSource,
2573 VoidTypeReg, ExtInstSetReg, Operands: {FileStrReg}, MAI);
2574 DebugSourceRegByFileStr[Key] = DS;
2575 return DS;
2576}
2577