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 ;
0 commit comments