LLVM 23.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"
26#include "llvm/ADT/StringMap.h"
29#include "llvm/MC/MCInst.h"
30#include "llvm/MC/MCRegister.h"
31#include <optional>
32
33namespace llvm {
34
35class SPIRVSubtarget;
36
37/// AsmPrinter handler that emits NonSemantic.Shader.DebugInfo.100 (NSDI)
38/// instructions for the SPIR-V backend. Registered with SPIRVAsmPrinter when
39/// the module contains debug info (llvm.dbg.cu).
40///
41/// Call sequence:
42/// beginModule() -- collect compile-unit metadata.
43/// prepareModuleOutput() -- add extension + ext inst set to MAI.
44/// emitNonSemanticDebugStrings() -- OpString for NSDI strings (sec. 7).
45/// emitNonSemanticGlobalDebugInfo() -- emit DebugSource,
46/// DebugCompilationUnit, DebugTypeBasic,
47/// DebugTypePointer, DebugTypeFunction.
48/// beginFunctionImpl() -- no-op (no per-function DI yet).
49/// endFunctionImpl() -- no-op.
51 struct CompileUnitInfo {
52 SmallString<128> FilePath;
53 unsigned SpirvSourceLanguage = 0; // NonSemantic.Shader.DebugInfo.100 source
54 // language code (section 4.3)
55 };
56 // TODO: When per-function NSDI emission is implemented, augment
57 // CompileUnitInfo with the originating DICompileUnit pointer so that
58 // Parent operands on DebugFunction and similar instructions can resolve
59 // the compile unit's result register.
61 int64_t DwarfVersion = 0;
62
63 // DI types partitioned from DebugInfoFinder.types() in beginModule()
64 // (basics, pointers, vectors, subroutine types NSDI v1 may emit).
68 // DICompositeType nodes with DW_TAG_array_type and DINode::FlagVector,
69 // partitioned from DebugInfoFinder.types() in beginModule().
71
72 // Filled in emitNonSemanticGlobalDebugInfo(): DI types to their result
73 // registers.
75
76 // Maps OpString contents to result id. Populated only by emitOpStringIfNew()
77 // during section 7; section 10 uses getCachedOpStringReg() (lookup only).
78 StringMap<MCRegister> OpStringContentCache;
79
80#ifndef NDEBUG // Only declare the variable for debugging purposes.
81 // True after emitNonSemanticDebugStrings() emitted the NSDI OpStrings for
82 // this module. SPIRVAsmPrinter calls that before
83 // emitNonSemanticGlobalDebugInfo().
84 bool NonSemanticOpStringsSectionEmitted = false;
85#endif
86
87 MCRegister CachedDebugInfoNoneReg;
88
89 MCRegister CachedOpTypeVoidReg;
90
91 MCRegister CachedOpTypeInt32Reg;
92
93 // Cache of already-emitted i32 constants, keyed by value. Prevents
94 // duplicate OpConstant instructions for the same integer value.
95 DenseMap<uint32_t, MCRegister> I32ConstantCache;
96
97 // Cache of already-emitted DebugTypeFunction instructions, keyed by operand
98 // ids (flags, return type, parameters).
99 DenseMap<SmallVector<MCRegister, 8>, MCRegister> DebugTypeFunctionCache;
100
101 // True once emitNonSemanticGlobalDebugInfo() has run. Both
102 // SPIRVAsmPrinter::emitFunctionHeader() and emitEndOfAsmFile() may call
103 // outputModuleSections(), each guarded by ModuleSectionsEmitted, so only
104 // one fires. This flag provides a secondary guard in case the call sites
105 // change.
106 bool GlobalDIEmitted = false;
107
108public:
110
111 /// Collect compile-unit metadata from the module. Called by
112 /// AsmPrinter::doInitialization() via the handler list. No emission.
113 void beginModule(Module *M) override;
114
115 /// Emit OpString instructions for all NSDI file paths and basic type names
116 /// into the debug section (section 7 of the SPIR-V module layout). Must be
117 /// called from SPIRVAsmPrinter::outputDebugSourceAndStrings(), after
118 /// prepareModuleOutput() has registered the ext inst set. Registers are
119 /// stored in OpStringContentCache; emitNonSemanticGlobalDebugInfo() resolves
120 /// them via getCachedOpStringReg().
122
123 /// Add SPV_KHR_non_semantic_info extension and
124 /// NonSemantic.Shader.DebugInfo.100 ext inst set entry to MAI. Must be called
125 /// before outputGlobalRequirements() and outputOpExtInstImports() in
126 /// SPIRVAsmPrinter::outputModuleSections().
127 void prepareModuleOutput(const SPIRVSubtarget &ST,
129
130 /// Emit module-scope NSDI instructions (DebugSource, DebugCompilationUnit,
131 /// DebugTypeBasic, DebugTypePointer, DebugTypeFunction). Called by
132 /// SPIRVAsmPrinter::outputModuleSections() at section 10 in place of
133 /// outputModuleSection(MB_NonSemanticGlobalDI). Requires
134 /// emitNonSemanticDebugStrings() to have run first when NSDI strings apply.
136
137protected:
138 // All module-level output is driven by emitNonSemanticGlobalDebugInfo(),
139 // called explicitly from SPIRVAsmPrinter::outputModuleSections(). Nothing
140 // needs to happen in the AsmPrinterHandler::endModule() callback.
141 void endModule() override {}
142
143 // DebugHandlerBase stores MMI as a pointer copy from Asm->MMI at construction
144 // time (DebugHandlerBase.cpp: `MMI(Asm->MMI)`). The handler is constructed
145 // before AsmPrinter::doInitialization() runs, so Asm->MMI is null at that
146 // point and MMI remains null for this handler's entire lifetime. The
147 // base-class beginInstruction/endInstruction dereference MMI to create temp
148 // symbols for label tracking and would crash. Override them as no-ops.
149 // When per-function NSDI is implemented, use Asm->OutStreamer->getContext()
150 // for MCContext access rather than MMI->getContext().
151 void beginInstruction(const MachineInstr *MI) override {}
152 void endInstruction() override {}
153
154 // TODO: Emit DebugFunction and DebugFunctionDefinition here once per-function
155 // NSDI emission is implemented. DebugHandlerBase::beginFunction() populates
156 // LScopes and DbgValues, which are needed for DebugLine emission. Do not
157 // override beginFunction() until that work is in place.
158 void beginFunctionImpl(const MachineFunction *MF) override {}
159 // TODO: Add per-function cleanup when DebugFunction emission is in place.
160 void endFunctionImpl(const MachineFunction *MF) override {}
161
162private:
163 void emitMCInst(MCInst &Inst);
164 MCRegister emitOpString(StringRef S, SPIRV::ModuleAnalysisInfo &MAI);
165
166 /// Section 7 only: emit OpString and cache it if not already present. Must
167 /// not be called after NonSemanticOpStringsSectionEmitted is set.
168 void emitOpStringIfNew(StringRef S, SPIRV::ModuleAnalysisInfo &MAI);
169
170 /// Section 10 only: lookup OpString id from cache; asserts if missing or if
171 /// section 7 did not complete.
172 MCRegister getCachedOpStringReg(StringRef S);
173 MCRegister emitOpConstantI32(uint32_t Value, MCRegister I32TypeReg,
175 MCRegister emitExtInst(SPIRV::NonSemanticExtInst::NonSemanticExtInst Opcode,
176 MCRegister VoidTypeReg, MCRegister ExtInstSetReg,
177 ArrayRef<MCRegister> Operands,
179
180 /// Return a cached DebugTypeFunction id when \p Ops matches a prior emission,
181 /// otherwise emit and cache a new instruction.
182 MCRegister getOrEmitDebugTypeFunction(ArrayRef<MCRegister> Ops,
183 MCRegister VoidTypeReg,
184 MCRegister ExtInstSetReg,
186
187 /// Return OpTypeVoid id for this module (lazy lookup / emit, then cache).
188 MCRegister getOrEmitOpTypeVoidReg(SPIRV::ModuleAnalysisInfo &MAI);
189
190 /// Return OpTypeInt 32 0 id for this module (lazy lookup / emit, then cache).
191 MCRegister getOrEmitOpTypeInt32Reg(SPIRV::ModuleAnalysisInfo &MAI);
192
193 /// Find OpTypeVoid in the already-emitted TypeConstVars section, or emit one
194 /// if the module does not contain it (e.g. no void-returning functions).
195 MCRegister findOrEmitOpTypeVoid(SPIRV::ModuleAnalysisInfo &MAI);
196
197 /// Find OpTypeInt 32 0 in the already-emitted TypeConstVars section, or emit
198 /// one if the module does not contain it.
199 MCRegister findOrEmitOpTypeInt32(SPIRV::ModuleAnalysisInfo &MAI);
200
201 /// Emit \c DebugTypePointer for pointer metadata \p PT.
202 ///
203 /// \returns The result id register on success. Returns \c std::nullopt and
204 /// emits nothing if \p PT has no DWARF address space (needed to pick the
205 /// SPIR-V storage class), or if \p PT has a non-null base DI type that is not
206 /// yet in \c DebugTypeRegs (the pointee was not emitted as a debug type).
207 ///
208 /// Base Type operand: the register from \c DebugTypeRegs for \p PT's base
209 /// type when it is set and mapped; \c DebugInfoNone when there is no base
210 /// type (e.g. \c void * in IR), consistent with SPIRV-LLVM-Translator.
211 std::optional<MCRegister>
212 emitDebugTypePointer(const DIDerivedType *PT, MCRegister ExtInstSetReg,
214
215 /// Emit one DebugTypeFunction for ST when every DI operand maps to a debug
216 /// type id; otherwise emit nothing and return std::nullopt.
217 std::optional<MCRegister>
218 emitDebugTypeFunctionForSubroutineType(const DISubroutineType *ST,
219 MCRegister ExtInstSetReg,
221
222 /// Emit \c DebugTypeVector for the vector composite type \p VT.
223 ///
224 /// \returns The result id register on success. Returns \c std::nullopt and
225 /// emits nothing if \p VT has no \c DIBasicType base type, if the base type
226 /// has not been emitted yet, if \p VT has more than one \c DISubrange
227 /// element, or if the component count is not a compile-time constant.
228 std::optional<MCRegister> emitDebugTypeVector(const DICompositeType *VT,
229 MCRegister ExtInstSetReg,
231
232 /// Map a \c DISubroutineType::getTypeArray() element to an operand register
233 /// for
234 /// \c DebugTypeFunction. Non-null \p Ty resolves via \c DebugTypeRegs; if the
235 /// type was never emitted, returns \c std::nullopt.
236 ///
237 /// LLVM encodes a void return as a null first element (and may use null in
238 /// later slots). NonSemantic \c DebugTypeFunction
239 /// requires a concrete return-type operand, so when \p ReturnType is true and
240 /// \p Ty is null, this returns \p VoidTypeReg (\c OpTypeVoid). When
241 /// \p ReturnType is false and \p Ty is null, this returns
242 /// \c CachedDebugInfoNoneReg (\c DebugInfoNone).
243 std::optional<MCRegister> mapDISignatureTypeToReg(const DIType *Ty,
244 MCRegister VoidTypeReg,
245 bool ReturnType);
246
247 /// Map a DWARF source language code to a NonSemantic.Shader.DebugInfo.100
248 /// source language code.
249 static unsigned toNSDISrcLang(unsigned DwarfSrcLang);
250};
251
252} // namespace llvm
253
254#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[]
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
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.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
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 beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
void beginFunctionImpl(const MachineFunction *MF) override
void endFunctionImpl(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.
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:128
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.