LLVM 24.0.0git
SPIRVNonSemanticDebugHandler.h
Go to the documentation of this file.
1//===-- SPIRVNonSemanticDebugHandler.h - 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// This file declares SPIRVNonSemanticDebugHandler, a DebugHandlerBase subclass
11// that emits NonSemantic.Shader.DebugInfo.100 instructions in the SPIR-V
12// AsmPrinter. It replaces SPIRVEmitNonSemanticDI, which was a
13// MachineFunctionPass, with a handler that controls instruction placement
14// directly instead of routing through SPIRVModuleAnalysis.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVNONSEMANTICDEBUGHANDLER_H
19#define LLVM_LIB_TARGET_SPIRV_SPIRVNONSEMANTICDEBUGHANDLER_H
20
22#include "SPIRVModuleAnalysis.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/SetVector.h"
27#include "llvm/ADT/StringMap.h"
30#include "llvm/MC/MCInst.h"
31#include "llvm/MC/MCRegister.h"
32#include <optional>
33
34namespace llvm {
35
36class GlobalVariable;
37class SPIRVSubtarget;
38
39/// AsmPrinter handler that emits NonSemantic.Shader.DebugInfo.100 (NSDI)
40/// instructions for the SPIR-V backend. Registered with SPIRVAsmPrinter when
41/// the module contains debug info (llvm.dbg.cu).
42///
43/// Call sequence:
44/// - beginModule() collects compile-unit metadata.
45/// - prepareModuleOutput() adds the extension and ext-inst set to MAI.
46/// - emitNonSemanticDebugStrings() emits NSDI OpStrings in section 7.
47/// - emitNonSemanticGlobalDebugInfo() emits module-scope NSDI and sets
48/// GlobalNSDIEnabled.
49/// - beginFunctionImpl() prepares per-function DebugFunctionDefinition state.
50/// - endInstruction() emits DebugFunctionDefinition after the last function-
51/// level OpVariable; SPIRVAsmPrinter calls notifyEntryLabelEmitted() after
52/// the synthesized entry OpLabel when there are no OpVariables.
53/// - endFunctionImpl() resets per-function state.
55 static constexpr unsigned NSSet = static_cast<unsigned>(
56 SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100);
57
58 struct CompileUnitInfo {
59 const DICompileUnit *TheCU = nullptr;
60 SmallString<128> FilePath;
61 unsigned SpirvSourceLanguage = 0; // NonSemantic.Shader.DebugInfo.100 source
62 // language code (section 4.3)
63 };
65 int64_t DwarfVersion = 0;
66
67 // DI types partitioned from DebugInfoFinder.types() in beginModule()
68 // (basics, pointers, vectors, subroutine types NSDI v1 may emit).
72 // DICompositeType nodes with DW_TAG_array_type and DINode::FlagVector,
73 // partitioned from DebugInfoFinder.types() in beginModule().
75 // DICompositeType nodes with DW_TAG_array_type that are not vectors,
76 // partitioned in beginModule().
78 // DICompositeType nodes with DW_TAG_structure_type, DW_TAG_class_type, or
79 // DW_TAG_union_type, partitioned in beginModule() for DebugTypeComposite.
81 // DIDerivedType nodes with DW_TAG_typedef, partitioned in beginModule() for
82 // DebugTypedef emission.
84
85 // NonSemantic debug instruction result id per emitted scope.
87
88 // DISubprogram nodes that are declarations only (!isDefinition()), collected
89 // in beginModule() for DebugFunctionDeclaration emission.
90 SmallVector<const DISubprogram *> SubprogramDeclarations;
91
92 // DISubprogram nodes that are definitions, collected in beginModule() for
93 // DebugFunction emission.
94 SmallVector<const DISubprogram *> SubprogramDefinitions;
95
96 // Distinct DILocations from instruction !dbg attachments and debug program
97 // records (#dbg_declare, #dbg_value, #dbg_assign, #dbg_label).
98 SetVector<const DILocation *> UniqueDebugLocations;
99
100 struct GlobalVariableDebugInfo {
101 const DIExpression *Expr = nullptr;
102 const GlobalVariable *LLVMGV = nullptr;
103 };
105 GlobalVariableDebugInfoMap;
106
107 // Distinct DILocalVariable nodes collected in beginModule() from dbg records
108 // and from DISubprogram retained nodes.
110
111 // DebugLocalVariable result id per variable that module-scope emission
112 // actually emitted. DebugDeclare needs it for its Local Variable operand; a
113 // variable missing here (skipped type or scope) gets no declare.
115
116 // DebugExpression result id per DIExpression that could be lowered. An
117 // expression missing here uses operations with no NonSemantic counterpart,
118 // so declares referencing it are skipped rather than described wrongly.
120
121 // Distinct DILexicalBlock and DINamespace scopes, parent-before-child
122 // order, collected in beginModule() for DebugLexicalBlock emission.
123 SetVector<const DIScope *> LexicalBlocks;
124
125 // DebugInlinedAt result id per DILocation used as an inlined-at chain link.
127
128 // Path \c OpString result id per \c DIScope (CU, \c DIFile, declaration
129 // \c DISubprogram, …). Filled during \c emitNonSemanticDebugStrings() using
130 // \c getDebugFullPath + \c emitOpStringIfNew; section 10 uses it for
131 // \c DebugSource without recomputing path text.
132 DenseMap<const DIScope *, MCRegister> ScopeToPathOpStringReg;
133
134 // DebugSource result id keyed by path \c OpString id (\c MCRegister::id()),
135 // deduplicating when the same file string is reused.
136 DenseMap<unsigned, MCRegister> DebugSourceRegByFileStr;
137
138 // Maps OpString contents to result id. Populated only by emitOpStringIfNew()
139 // during section 7; section 10 uses getCachedOpStringReg() (lookup only).
140 StringMap<MCRegister> OpStringContentCache;
141
142#ifndef NDEBUG // Only declare the variable for debugging purposes.
143 // True after emitNonSemanticDebugStrings() emitted the NSDI OpStrings for
144 // this module. SPIRVAsmPrinter calls that before
145 // emitNonSemanticGlobalDebugInfo().
146 bool NonSemanticOpStringsSectionEmitted = false;
147#endif
148
149 MCRegister CachedEmptyStringReg;
150
151 MCRegister CachedDebugInfoNoneReg;
152
153 MCRegister CachedOpTypeVoidReg;
154
155 MCRegister CachedOpTypeInt32Reg;
156
157 // Cache of already-emitted i32 constants, keyed by value. Prevents
158 // duplicate OpConstant instructions for the same integer value.
159 DenseMap<uint32_t, MCRegister> I32ConstantCache;
160
161 // Cache of already-emitted DebugTypeFunction instructions, keyed by operand
162 // ids (flags, return type, parameters).
163 DenseMap<SmallVector<MCRegister, 8>, MCRegister> DebugTypeFunctionCache;
164
165 // Cache of already-emitted DebugOperation instructions, keyed by NonSemantic
166 // opcode followed by the 32-bit operation arguments. Inline size 3 is the
167 // spec maximum (opcode plus at most two operands: BitPiece, Fragment).
168 DenseMap<SmallVector<uint32_t, 3>, MCRegister> DebugOperationCache;
169
170 // Cache of already-emitted DebugExpression instructions, keyed by the
171 // DebugOperation result ids in operand order. Useful for debug values
172 // and global-variable init expressions.
173 DenseMap<SmallVector<MCRegister>, MCRegister> DebugExpressionCache;
174
175 // True once emitNonSemanticGlobalDebugInfo() has run. Both
176 // SPIRVAsmPrinter::emitFunctionHeader() and emitEndOfAsmFile() may call
177 // outputModuleSections(), each guarded by ModuleSectionsEmitted, so only
178 // one fires. This flag provides a secondary guard in case the call sites
179 // change.
180 bool GlobalDIEmitted = false;
181
182 // True when emitNonSemanticGlobalDebugInfo() completed module-scope NSDI
183 // emission for this module.
184 bool GlobalNSDIEnabled = false;
185
186 SPIRV::ModuleAnalysisInfo *CurrentMAI = nullptr;
187
188 const MachineFunction *CurrentMF = nullptr;
189
190 const MachineInstr *LastFunctionOpVariable = nullptr;
191
192 bool DebugFunctionDefinitionEmitted = false;
193
194 // Instruction that opened the DebugLine / DebugScope region currently in
195 // effect, or nullptr when no region is open. The two are tracked separately
196 // because a DebugScope region usually spans several DebugLine regions, and
197 // either one can skip emission on a cache miss.
198 const MachineInstr *LastLineMI = nullptr;
199 const MachineInstr *LastScopeMI = nullptr;
200
201public:
203
204 /// Collect compile-unit metadata from the module. Called by
205 /// AsmPrinter::doInitialization() via the handler list. No emission.
206 void beginModule(Module *M) override;
207
208 /// Emit OpString instructions for all NSDI file paths and basic type names
209 /// into the debug section (section 7 of the SPIR-V module layout). Must be
210 /// called from SPIRVAsmPrinter::outputDebugSourceAndStrings(), after
211 /// prepareModuleOutput() has registered the ext inst set. Registers are
212 /// stored in \c OpStringContentCache and \c ScopeToPathOpStringReg;
213 /// \c emitNonSemanticGlobalDebugInfo() resolves them via
214 /// \c getCachedOpStringReg() and path maps.
216
217 /// Add SPV_KHR_non_semantic_info extension and
218 /// NonSemantic.Shader.DebugInfo.100 ext inst set entry to MAI. Must be called
219 /// before outputGlobalRequirements() and outputOpExtInstImports() in
220 /// SPIRVAsmPrinter::outputModuleSections().
221 void prepareModuleOutput(const SPIRVSubtarget &ST,
223
224 /// Emit module-scope NSDI instructions (DebugSource, DebugCompilationUnit,
225 /// DebugTypeBasic, DebugTypePointer, DebugTypeFunction,
226 /// DebugFunctionDeclaration, DebugFunction). Called by
227 /// SPIRVAsmPrinter::outputModuleSections() at section 10 in place of
228 /// outputModuleSection(MB_NonSemanticGlobalDI). Requires
229 /// emitNonSemanticDebugStrings() to have run first when NSDI strings apply.
230 /// Sets \c GlobalNSDIEnabled when module-scope NSDI emission completes.
232
233 /// Called after the synthesized entry \c OpLabel has been emitted.
235
236protected:
237 // All module-level output is driven by emitNonSemanticGlobalDebugInfo(),
238 // called explicitly from SPIRVAsmPrinter::outputModuleSections(). Nothing
239 // needs to happen in the AsmPrinterHandler::endModule() callback.
240 void endModule() override {}
241
242 // DebugHandlerBase stores MMI as a pointer copy from Asm->MMI at construction
243 // time (DebugHandlerBase.cpp: `MMI(Asm->MMI)`). The handler is constructed
244 // before AsmPrinter::doInitialization() runs, so Asm->MMI is null at that
245 // point and MMI remains null for this handler's entire lifetime. Do not call
246 // the base-class beginInstruction/endInstruction — they dereference MMI to
247 // create temp symbols for label tracking and would crash.
248 // Future local NSDI that needs MCContext must use
249 // Asm->OutStreamer->getContext() rather than MMI->getContext().
250 void beginInstruction(const MachineInstr *MI) override;
251 void endInstruction() override;
252
253 // Override beginFunctionImpl(), not beginFunction():
254 // DebugHandlerBase::beginFunction() populates LScopes and DbgValues needed
255 // for future DebugLine emission.
256 void beginFunctionImpl(const MachineFunction *MF) override;
257 void endFunctionImpl(const MachineFunction *MF) override;
258
259private:
260 void emitDebugFunctionDefinition(MCRegister DebugFunctionReg,
261 MCRegister OpFunctionReg,
263
264 void resetPerFunctionDebugState();
265
266 /// Resolve the instruction that a per-instruction DebugLine/DebugScope
267 /// update should attach to: \p MI adjusted forward past a merge
268 /// instruction to its terminator, or \c std::nullopt if \p MI is not a
269 /// valid attachment point (skip-emission, or one of the structural opcodes
270 /// that can never carry DebugLine/DebugScope: OpFunction,
271 /// OpFunctionParameter, OpFunctionEnd, OpLabel, OpPhi).
272 std::optional<const MachineInstr *>
273 resolveDebugLocTarget(const MachineInstr *MI);
274
275 void emitDebugScopeForInstruction(const MachineInstr *MI);
276 void emitDebugLineForInstruction(const MachineInstr *MI);
277 void preparePerFunctionDebug(const MachineFunction *MF);
278 void tryEmitDebugFunctionDefinition(SPIRV::ModuleAnalysisInfo &MAI);
279
280 void emitMCInst(MCInst &Inst);
281 MCRegister emitOpString(StringRef S, SPIRV::ModuleAnalysisInfo &MAI);
282
283 /// Section 7 only: emit OpString and cache it if not already present. Must
284 /// not be called after NonSemanticOpStringsSectionEmitted is set. Returns
285 /// the path (or string) \c OpString result id.
286 MCRegister emitOpStringIfNew(StringRef S, SPIRV::ModuleAnalysisInfo &MAI);
287
288 /// Section 10 only: lookup OpString id from cache; asserts if missing or if
289 /// section 7 did not complete.
290 MCRegister getCachedOpStringReg(StringRef S);
291
292 /// Section 7 only: emit the path \c OpString for \p Scope and cache it under
293 /// \p Scope. Returns the \c OpString result id. A \p Scope already seen
294 /// returns the cached id without rebuilding the path. A null \p Scope maps to
295 /// the empty path and is cached like any other, though section 10 reads it
296 /// through \c getCachedScopePathOpStringReg, which handles null separately.
297 MCRegister emitAndCacheScopePathOpStringReg(const DIScope *Scope,
299
300 /// Section 10 only: lookup path \c OpString id for \p Scope from
301 /// \c ScopeToPathOpStringReg; asserts if missing or invalid. When
302 /// \p UseEmptyPathIfNullScope is true and \p Scope is null, returns
303 /// \c CachedEmptyStringReg instead.
305 getCachedScopePathOpStringReg(const DIScope *Scope,
306 bool UseEmptyPathIfNullScope = false);
307 MCRegister emitOpConstantI32(uint32_t Value, MCRegister I32TypeReg,
309 MCRegister emitExtInst(SPIRV::NonSemanticExtInst::NonSemanticExtInst Opcode,
310 MCRegister VoidTypeReg, MCRegister ExtInstSetReg,
313
314 /// Return a cached DebugTypeFunction id when \p Ops matches a prior emission,
315 /// otherwise emit and cache a new instruction.
316 MCRegister getOrEmitDebugTypeFunction(ArrayRef<MCRegister> Ops,
317 MCRegister VoidTypeReg,
318 MCRegister ExtInstSetReg,
320
321 /// Return OpTypeVoid id for this module (lazy lookup / emit, then cache).
322 MCRegister getOrEmitOpTypeVoidReg(SPIRV::ModuleAnalysisInfo &MAI);
323
324 /// Return OpTypeInt 32 0 id for this module (lazy lookup / emit, then cache).
325 MCRegister getOrEmitOpTypeInt32Reg(SPIRV::ModuleAnalysisInfo &MAI);
326
327 /// Find OpTypeVoid in the already-emitted TypeConstVars section, or emit one
328 /// if the module does not contain it (e.g. no void-returning functions).
329 MCRegister findOrEmitOpTypeVoid(SPIRV::ModuleAnalysisInfo &MAI);
330
331 /// Find OpTypeInt 32 0 in the already-emitted TypeConstVars section, or emit
332 /// one if the module does not contain it.
333 MCRegister findOrEmitOpTypeInt32(SPIRV::ModuleAnalysisInfo &MAI);
334
335 /// Emit \c DebugTypePointer for pointer metadata \p PT.
336 ///
337 /// \returns The result id register on success. Returns \c std::nullopt and
338 /// emits nothing if \p PT has no DWARF address space (needed to pick the
339 /// SPIR-V storage class), or if \p PT has a non-null base DI type that is not
340 /// yet in \c DebugScopeRegs (the pointee was not emitted as a debug type).
341 ///
342 /// Base Type operand: the register from \c DebugScopeRegs for \p PT's base
343 /// type when it is set and mapped; \c DebugInfoNone when there is no base
344 /// type (e.g. \c void * in IR), consistent with SPIRV-LLVM-Translator.
345 std::optional<MCRegister>
346 emitDebugTypePointer(const DIDerivedType *PT, MCRegister ExtInstSetReg,
348
349 /// Emit one DebugTypeFunction for ST when every DI operand maps to a debug
350 /// type id; otherwise emit nothing and return std::nullopt.
351 std::optional<MCRegister>
352 emitDebugTypeFunctionForSubroutineType(const DISubroutineType *ST,
353 MCRegister ExtInstSetReg,
355
356 /// Emit \c DebugFunctionDeclaration for a \c DISubprogram that is not a
357 /// definition (\p SP must satisfy \c !isDefinition()).
358 ///
359 /// \returns The result id register on success. Returns \c std::nullopt and
360 /// emits nothing if \p SP is null, is a definition, has no \c
361 /// DISubroutineType type, the signature type was not emitted in \c
362 /// DebugScopeRegs, no path
363 /// \c OpString was recorded for \p SP in section 7, or
364 /// \c resolveScope returns no id for the \c Parent operand.
365 std::optional<MCRegister>
366 emitDebugFunctionDeclaration(const DISubprogram *SP, MCRegister VoidTypeReg,
367 MCRegister I32TypeReg, MCRegister ExtInstSetReg,
369
370 /// Emit \c DebugFunction for a defining \c DISubprogram (\p SP must satisfy
371 /// \c isDefinition()).
372 std::optional<MCRegister> emitDebugFunction(const DISubprogram *SP,
373 MCRegister VoidTypeReg,
374 MCRegister I32TypeReg,
375 MCRegister ExtInstSetReg,
377
378 /// Emit \c DebugLocalVariable for the source local variable \p LV:
379 /// Name, Type, Source, Line, Column, Parent, Flags, and an optional Arg
380 /// Number. Line, Column, Flags, and Arg Number are emitted as \c OpConstant
381 /// ids as required for non-semantic debug info. Column is always 0:
382 /// \c DILocalVariable has no column field.
383 ///
384 /// Arg Number is appended when \p LV is a parameter.
385 ///
386 /// \returns The result id register on success. Returns \c std::nullopt and
387 /// emits nothing if \p LV's scope is not an emitted local scope,
388 /// if a non-null type was not emitted in \c DebugScopeRegs, or if
389 /// \c resolveScope returns no id for the Parent operand.
390 std::optional<MCRegister>
391 emitDebugLocalVariable(const DILocalVariable *LV, MCRegister VoidTypeReg,
392 MCRegister I32TypeReg, MCRegister ExtInstSetReg,
394
395 /// Emit \c DebugGlobalVariable for the source global variable \p GV.
396 ///
397 /// (\c SPIRVDebug::Operand::GlobalVariable): Name, Type, Source, Line,
398 /// Column, Parent, Linkage Name, Variable, Flags, and an optional Static
399 /// Member Declaration. Line, Column, and Flags are emitted as \c OpConstant
400 /// ids as required for non-semantic debug info.
401 ///
402 /// \c DebugInfoNone is used for two operands when LLVM has no value to
403 /// supply:
404 /// \c Type when \p GV is a declaration with no DI type (e.g. \c extern void;
405 /// valid IR, \c isDefinition: false); \c Variable when no \c
406 /// llvm::GlobalVariable in this module carries \p GV in its \c !dbg metadata.
407 ///
408 /// \returns The result id register on success. Returns \c std::nullopt and
409 /// emits nothing if a non-null \p GV type was not emitted in \c
410 /// DebugScopeRegs, or \p GV has a static data member declaration that was not
411 /// emitted in \c DebugScopeRegs.
412 std::optional<MCRegister> emitDebugGlobalVariable(
413 const DIGlobalVariable *GV, const GlobalVariableDebugInfo &Info,
414 MCRegister VoidTypeReg, MCRegister I32TypeReg, MCRegister ExtInstSetReg,
416
417 /// Collect the \c DIExpression of every debug value in the module
418 /// (\c DBG_VALUE, \c DBG_VALUE_LIST, \c DBG_INSTR_REF), in MIR order.
419 ///
420 /// Reads MIR rather than IR because only MIR shows which debug values
421 /// survived codegen and in what form, and because an expression synthesized
422 /// during lowering never appears in the IR at all. Must be called from
423 /// module-scope emission, which is where the resulting \c DebugExpression
424 /// instructions have to be emitted; every \c MachineFunction is still
425 /// reachable at that point through \c MachineModuleInfo.
426 ///
427 /// Deliberately independent of what the consumers can currently emit, so
428 /// that adding an instruction that needs an expression (\c DebugValue) needs
429 /// no change here. The cost is a \c DebugExpression that nothing references
430 /// yet, for a debug value no instruction is emitted for.
431 void collectDebugExpressions(SetVector<const DIExpression *> &Out) const;
432
433 /// Emit one \c DebugOperation for \p Op, reusing a cached result id when the
434 /// same opcode and arguments were already emitted.
435 ///
436 /// \returns The result id register on success. Returns \c std::nullopt and
437 /// emits nothing if \p Op has no NonSemantic counterpart, or carries an
438 /// argument too large for the 32-bit \c OpConstant operands this set
439 /// requires.
440 std::optional<MCRegister>
441 emitDebugOperation(const DIExpression::ExprOperand &Op,
442 MCRegister VoidTypeReg, MCRegister I32TypeReg,
443 MCRegister ExtInstSetReg, SPIRV::ModuleAnalysisInfo &MAI);
444
445 /// Emit one \c DebugOperation per element of \p Expr followed by the
446 /// \c DebugExpression that lists them. Reuses a cached \c DebugExpression
447 /// when that sequence of \c DebugOperation ids was already emitted. An
448 /// empty \p Expr yields a \c DebugExpression with no operands, which is
449 /// what a plain \c !DIExpression() means.
450 ///
451 /// Must be called from module-scope emission only: \c DebugExpression and
452 /// \c DebugOperation are not in the spec's list of instructions allowed
453 /// inside a function, and forward references were removed in Rev 2.
454 ///
455 /// \returns The result id register on success. Returns \c std::nullopt and
456 /// does not emit the \c DebugExpression if any element has no NonSemantic
457 /// counterpart, or carries an argument too large for the 32-bit \c OpConstant
458 /// operands this set requires.
459 std::optional<MCRegister> emitDebugExpression(const DIExpression *Expr,
460 MCRegister VoidTypeReg,
461 MCRegister I32TypeReg,
462 MCRegister ExtInstSetReg,
464
465 /// Emit \c DebugDeclare for \p MI when it is an indirect \c DBG_VALUE whose
466 /// location register is defined by \c OpVariable, which is the shape
467 /// \c IRTranslator gives a \c #dbg_declare on storage the backend kept.
468 ///
469 /// Emits nothing when \p MI is not such a declare, when the variable has no
470 /// \c DebugLocalVariable, when the expression was not lowered, or when the
471 /// storage is anything other than an \c OpVariable (an access chain, a
472 /// constant, a function parameter, or a dead alloca with no def at all).
473 void emitDebugDeclare(const MachineInstr *MI);
474
475 /// Emit \c DebugTypeVector for the vector composite type \p VT.
476 ///
477 /// \returns The result id register on success. Returns \c std::nullopt and
478 /// emits nothing if \p VT has no \c DIBasicType base type, if the base type
479 /// has not been emitted yet, if \p VT has more than one \c DISubrange
480 /// element, or if the component count is not a compile-time constant.
481 std::optional<MCRegister> emitDebugTypeVector(const DICompositeType *VT,
482 MCRegister ExtInstSetReg,
484
485 /// Emit \c DebugTypeArray for the array composite type \p AT.
486 ///
487 /// Emits the element (base) type id followed by one Component Count per
488 /// \c DISubrange, in DWARF subrange order. A count that is not a
489 /// compile-time constant is emitted as 0, matching \c OpTypeRuntimeArray. A
490 /// matrix arrives here as a multi-subrange array and is emitted with one
491 /// count per dimension.
492 ///
493 /// \returns The result id register on success. Returns \c std::nullopt and
494 /// emits nothing if \p AT's element type has not been emitted into
495 /// \c DebugScopeRegs.
496 std::optional<MCRegister> emitDebugTypeArray(const DICompositeType *AT,
497 MCRegister ExtInstSetReg,
499
500 /// Emit \c DebugTypeMember for the data member \p M (a \c DIDerivedType with
501 /// \c DW_TAG_member). Operands: Name, Type, Source, Line, Column, Offset,
502 /// Size, Flags. NonSemantic \c DebugTypeMember carries no Parent operand: the
503 /// enclosing \c DebugTypeComposite references its members, not the reverse.
504 ///
505 /// \returns The result id register on success. Returns \c std::nullopt and
506 /// emits nothing if \p M's type has not been emitted into \c DebugScopeRegs.
507 std::optional<MCRegister> emitDebugTypeMember(const DIDerivedType *M,
508 MCRegister VoidTypeReg,
509 MCRegister I32TypeReg,
510 MCRegister ExtInstSetReg,
512
513 /// Emit \c DebugTypeComposite for the struct, class, or union \p CT, listing
514 /// the already-emitted \p MemberRegs in its Members operand. A forward
515 /// declaration emits \c DebugInfoNone for Size and no members.
516 ///
517 /// \returns The result id register on success. Returns \c std::nullopt and
518 /// emits nothing if the Parent scope cannot be resolved.
519 std::optional<MCRegister> emitDebugTypeComposite(
520 const DICompositeType *CT, ArrayRef<MCRegister> MemberRegs,
521 MCRegister VoidTypeReg, MCRegister I32TypeReg, MCRegister ExtInstSetReg,
523
524 /// Emit \c DebugTypedef for the typedef derived type \p TD (a \c
525 /// DIDerivedType with \c DW_TAG_typedef). Operands: Name, Base Type, Source,
526 /// Line, Column, Parent. Parent is the enclosing type when \c TD->getScope()
527 /// is an emitted \c DIType, otherwise the first module \c
528 /// DebugCompilationUnit.
529 ///
530 /// \returns The result id register on success. Returns \c std::nullopt and
531 /// emits nothing if \p TD's base type has not been emitted into \c
532 /// DebugScopeRegs.
533 std::optional<MCRegister> emitDebugTypedef(const DIDerivedType *TD,
534 MCRegister VoidTypeReg,
535 MCRegister I32TypeReg,
536 MCRegister ExtInstSetReg,
538
539 /// Map a \c DISubroutineType::getTypeArray() element to an operand register
540 /// for
541 /// \c DebugTypeFunction. Non-null \p Ty resolves via \c DebugScopeRegs; if
542 /// the type was never emitted, returns \c std::nullopt.
543 ///
544 /// LLVM encodes a void return as a null first element (and may use null in
545 /// later slots). NonSemantic \c DebugTypeFunction
546 /// requires a concrete return-type operand, so when \p ReturnType is true and
547 /// \p Ty is null, this returns \p VoidTypeReg (\c OpTypeVoid). When
548 /// \p ReturnType is false and \p Ty is null, this returns
549 /// \c CachedDebugInfoNoneReg (\c DebugInfoNone).
550 std::optional<MCRegister> mapDISignatureTypeToReg(const DIType *Ty,
551 MCRegister VoidTypeReg,
552 bool ReturnType);
553
554 /// Map a DWARF source language code to a NonSemantic.Shader.DebugInfo.100
555 /// source language code.
556 static unsigned toNSDISrcLang(unsigned DwarfSrcLang);
557
558 /// Build a full path from debug \p Scope for OpString / DebugSource, matching
559 /// SPIRV-LLVM-Translator \c getFullPath (OCLUtil.h): \c DIScope::getFilename,
560 /// \c getDirectory, and \c sys::path::Style::native. Works for any \c DIScope
561 /// that carries file path fields (e.g. \c DIFile, \c DISubprogram,
562 /// \c DICompileUnit). Returns an empty path when \p Scope is null.
563 SmallString<128> getDebugFullPath(const DIScope *Scope) const;
564
565 /// Return an existing \c DebugSource id for file path \c OpString \p
566 /// FileStrReg or emit \c DebugSource and cache it (keyed by \p FileStrReg
567 /// id).
568 MCRegister getOrEmitDebugSourceForFileStrReg(MCRegister FileStrReg,
569 MCRegister VoidTypeReg,
570 MCRegister ExtInstSetReg,
572
573 /// Map \p Scope to the NonSemantic debug id used as a \c Parent operand.
574 ///
575 /// Checks \c DebugScopeRegs in order by scope kind. When \p Scope is null, a
576 /// \c DIFile, or another scope without a dedicated debug instruction, falls
577 /// back to \p FallbackCU or the first module \c DebugCompilationUnit
578 /// recorded in \c DebugScopeRegs.
579 ///
580 /// \returns \c std::nullopt when \p Scope names an emitted scope that has
581 /// not been recorded yet, or when no fallback compile unit is available.
582 std::optional<MCRegister>
583 resolveScope(const DIScope *Scope,
584 const DICompileUnit *FallbackCU = nullptr) const;
585
586 /// Emit \c DebugLexicalBlock for \p S, which must be a \c DILexicalBlock or
587 /// a \c DINamespace. A \c DILexicalBlock supplies Line/Column
588 /// from \c getLine()/getColumn(); a \c DINamespace has neither, so both are
589 /// emitted as 0, and its Name is appended as an extra \c OpString operand.
590 ///
591 /// \returns The result id register on success. Returns \c std::nullopt and
592 /// emits nothing if \c resolveScope returns no id for \c S->getScope().
593 std::optional<MCRegister>
594 emitDebugLexicalBlock(const DIScope *S, MCRegister VoidTypeReg,
595 MCRegister I32TypeReg, MCRegister ExtInstSetReg,
597
598 /// Return a cached \c DebugInlinedAt id for \p IA, or emit one (recursing
599 /// into \c IA->getInlinedAt() first for the optional Inlined operand, so
600 /// outer frames are always emitted before the inner frame that references
601 /// them). Must run after \c DebugScopeRegs is populated, since the Scope
602 /// operand is resolved through \c resolveScope. \c DebugInlinedAt is not in
603 /// the spec's in-block instruction list, so this is only ever called from
604 /// module-scope emission (\c emitNonSemanticGlobalDebugInfo), never from
605 /// per-instruction emission.
606 ///
607 /// \returns An invalid (default-constructed) \c MCRegister, and emits
608 /// nothing, if \p IA's Scope does not resolve.
609 MCRegister getOrEmitDebugInlinedAt(const DILocation *IA,
610 MCRegister VoidTypeReg,
611 MCRegister I32TypeReg,
612 MCRegister ExtInstSetReg,
614};
615
616} // namespace llvm
617
618#endif // LLVM_LIB_TARGET_SPIRV_SPIRVNONSEMANTICDEBUGHANDLER_H
This file defines the StringMap class.
This file defines the DenseMap class.
IRTranslator LLVM IR MI
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static NVPTX::Scope resolveScope(NVPTX::Scope S, const NVPTXSubtarget *T)
SI Fold Operands
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallString class.
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This class is intended to be used as a driving class for all asm writers.
Definition AsmPrinter.h:91
A lightweight wrapper around an expression operand.
DWARF expression.
Base class for scope-like contexts.
Subprogram description. Uses SubclassData1.
Type array for a subprogram.
Base class for types.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Representation of each machine instruction.
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
void endModule() override
Emit all sections that should come after the content.
void emitNonSemanticDebugStrings(SPIRV::ModuleAnalysisInfo &MAI)
Emit OpString instructions for all NSDI file paths and basic type names into the debug section (secti...
void beginModule(Module *M) override
Collect compile-unit metadata from the module.
void endFunctionImpl(const MachineFunction *MF) override
void beginFunctionImpl(const MachineFunction *MF) override
void emitNonSemanticGlobalDebugInfo(SPIRV::ModuleAnalysisInfo &MAI)
Emit module-scope NSDI instructions (DebugSource, DebugCompilationUnit, DebugTypeBasic,...
void prepareModuleOutput(const SPIRVSubtarget &ST, SPIRV::ModuleAnalysisInfo &MAI)
Add SPV_KHR_non_semantic_info extension and NonSemantic.Shader.DebugInfo.100 ext inst set entry to MA...
void endInstruction() override
Process end of an instruction.
void notifyEntryLabelEmitted(const MachineFunction &MF)
Called after the synthesized entry OpLabel has been emitted.
A vector that has set insertion semantics.
Definition SetVector.h:57
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:129
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
DWARFExpression::Operation Op