Skip to content

Commit ae5ee97

Browse files
authored
[SPIR-V] Emit DebugTypePointer from NonSemantic DI (llvm#109287)
Implementation of DebugTypePointer from NonSemantic.Shader.DebugInfo.100.
1 parent db4874c commit ae5ee97

File tree

3 files changed

+339
-4
lines changed

3 files changed

+339
-4
lines changed

llvm/lib/Target/SPIRV/SPIRVEmitNonSemanticDI.cpp

+58-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "SPIRVGlobalRegistry.h"
55
#include "SPIRVRegisterInfo.h"
66
#include "SPIRVTargetMachine.h"
7+
#include "SPIRVUtils.h"
78
#include "llvm/ADT/SmallPtrSet.h"
89
#include "llvm/ADT/SmallString.h"
910
#include "llvm/BinaryFormat/Dwarf.h"
@@ -104,6 +105,7 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
104105
int64_t DwarfVersion = 0;
105106
int64_t DebugInfoVersion = 0;
106107
SmallPtrSet<DIBasicType *, 12> BasicTypes;
108+
SmallPtrSet<DIDerivedType *, 12> PointerDerivedTypes;
107109
// Searching through the Module metadata to find nescessary
108110
// information like DwarfVersion or SourceLanguage
109111
{
@@ -146,8 +148,21 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
146148
for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
147149
DILocalVariable *LocalVariable = DVR.getVariable();
148150
if (auto *BasicType =
149-
dyn_cast<DIBasicType>(LocalVariable->getType()))
151+
dyn_cast<DIBasicType>(LocalVariable->getType())) {
150152
BasicTypes.insert(BasicType);
153+
} else if (auto *DerivedType =
154+
dyn_cast<DIDerivedType>(LocalVariable->getType())) {
155+
if (DerivedType->getTag() == dwarf::DW_TAG_pointer_type) {
156+
PointerDerivedTypes.insert(DerivedType);
157+
// DIBasicType can be unreachable from DbgRecord and only
158+
// pointed on from other DI types
159+
// DerivedType->getBaseType is null when pointer
160+
// is representing a void type
161+
if (DerivedType->getBaseType())
162+
BasicTypes.insert(
163+
cast<DIBasicType>(DerivedType->getBaseType()));
164+
}
165+
}
151166
}
152167
}
153168
}
@@ -206,6 +221,7 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
206221

207222
const Register DwarfVersionReg =
208223
GR->buildConstantInt(DwarfVersion, MIRBuilder, I32Ty, false);
224+
209225
const Register DebugInfoVersionReg =
210226
GR->buildConstantInt(DebugInfoVersion, MIRBuilder, I32Ty, false);
211227

@@ -237,7 +253,6 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
237253
break;
238254
case dwarf::DW_LANG_Zig:
239255
SpirvSourceLanguage = SourceLanguage::Zig;
240-
break;
241256
}
242257

243258
const Register SourceLanguageReg =
@@ -255,6 +270,11 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
255270
const Register I32ZeroReg =
256271
GR->buildConstantInt(0, MIRBuilder, I32Ty, false);
257272

273+
// We need to store pairs because further instructions reference
274+
// the DIBasicTypes and size will be always small so there isn't
275+
// need for any kind of map
276+
SmallVector<std::pair<const DIBasicType *const, const Register>, 12>
277+
BasicTypeRegPairs;
258278
for (auto *BasicType : BasicTypes) {
259279
const Register BasicTypeStrReg = EmitOpString(BasicType->getName());
260280

@@ -288,11 +308,46 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
288308
const Register AttributeEncodingReg =
289309
GR->buildConstantInt(AttributeEncoding, MIRBuilder, I32Ty, false);
290310

291-
[[maybe_unused]]
292311
const Register BasicTypeReg =
293312
EmitDIInstruction(SPIRV::NonSemanticExtInst::DebugTypeBasic,
294313
{BasicTypeStrReg, ConstIntBitwidthReg,
295314
AttributeEncodingReg, I32ZeroReg});
315+
BasicTypeRegPairs.emplace_back(BasicType, BasicTypeReg);
316+
}
317+
318+
if (PointerDerivedTypes.size()) {
319+
for (const auto *PointerDerivedType : PointerDerivedTypes) {
320+
321+
assert(PointerDerivedType->getDWARFAddressSpace().has_value());
322+
const Register StorageClassReg = GR->buildConstantInt(
323+
addressSpaceToStorageClass(
324+
PointerDerivedType->getDWARFAddressSpace().value(),
325+
*TM->getSubtargetImpl()),
326+
MIRBuilder, I32Ty, false);
327+
328+
// If the Pointer is representing a void type it's getBaseType
329+
// is a nullptr
330+
const auto *MaybeNestedBasicType =
331+
cast_or_null<DIBasicType>(PointerDerivedType->getBaseType());
332+
if (MaybeNestedBasicType) {
333+
for (const auto &BasicTypeRegPair : BasicTypeRegPairs) {
334+
const auto &[DefinedBasicType, BasicTypeReg] = BasicTypeRegPair;
335+
if (DefinedBasicType == MaybeNestedBasicType) {
336+
[[maybe_unused]]
337+
const Register DebugPointerTypeReg = EmitDIInstruction(
338+
SPIRV::NonSemanticExtInst::DebugTypePointer,
339+
{BasicTypeReg, StorageClassReg, I32ZeroReg});
340+
}
341+
}
342+
} else {
343+
const Register DebugInfoNoneReg =
344+
EmitDIInstruction(SPIRV::NonSemanticExtInst::DebugInfoNone, {});
345+
[[maybe_unused]]
346+
const Register DebugPointerTypeReg = EmitDIInstruction(
347+
SPIRV::NonSemanticExtInst::DebugTypePointer,
348+
{DebugInfoNoneReg, StorageClassReg, I32ZeroReg});
349+
}
350+
}
296351
}
297352
}
298353
return true;

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ void SPIRVModuleAnalysis::processOtherInstrs(const Module &M) {
436436
namespace NS = SPIRV::NonSemanticExtInst;
437437
static constexpr int64_t GlobalNonSemanticDITy[] = {
438438
NS::DebugSource, NS::DebugCompilationUnit, NS::DebugInfoNone,
439-
NS::DebugTypeBasic};
439+
NS::DebugTypeBasic, NS::DebugTypePointer};
440440
bool IsGlobalDI = false;
441441
for (unsigned Idx = 0; Idx < std::size(GlobalNonSemanticDITy); ++Idx)
442442
IsGlobalDI |= Ins.getImm() == GlobalNonSemanticDITy[Idx];

0 commit comments

Comments
 (0)