4
4
#include " SPIRVGlobalRegistry.h"
5
5
#include " SPIRVRegisterInfo.h"
6
6
#include " SPIRVTargetMachine.h"
7
+ #include " SPIRVUtils.h"
7
8
#include " llvm/ADT/SmallPtrSet.h"
8
9
#include " llvm/ADT/SmallString.h"
9
10
#include " llvm/BinaryFormat/Dwarf.h"
@@ -104,6 +105,7 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
104
105
int64_t DwarfVersion = 0 ;
105
106
int64_t DebugInfoVersion = 0 ;
106
107
SmallPtrSet<DIBasicType *, 12 > BasicTypes;
108
+ SmallPtrSet<DIDerivedType *, 12 > PointerDerivedTypes;
107
109
// Searching through the Module metadata to find nescessary
108
110
// information like DwarfVersion or SourceLanguage
109
111
{
@@ -146,8 +148,21 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
146
148
for (DbgVariableRecord &DVR : filterDbgVars (I.getDbgRecordRange ())) {
147
149
DILocalVariable *LocalVariable = DVR.getVariable ();
148
150
if (auto *BasicType =
149
- dyn_cast<DIBasicType>(LocalVariable->getType ()))
151
+ dyn_cast<DIBasicType>(LocalVariable->getType ())) {
150
152
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
+ }
151
166
}
152
167
}
153
168
}
@@ -206,6 +221,7 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
206
221
207
222
const Register DwarfVersionReg =
208
223
GR->buildConstantInt (DwarfVersion, MIRBuilder, I32Ty, false );
224
+
209
225
const Register DebugInfoVersionReg =
210
226
GR->buildConstantInt (DebugInfoVersion, MIRBuilder, I32Ty, false );
211
227
@@ -237,7 +253,6 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
237
253
break ;
238
254
case dwarf::DW_LANG_Zig:
239
255
SpirvSourceLanguage = SourceLanguage::Zig;
240
- break ;
241
256
}
242
257
243
258
const Register SourceLanguageReg =
@@ -255,6 +270,11 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
255
270
const Register I32ZeroReg =
256
271
GR->buildConstantInt (0 , MIRBuilder, I32Ty, false );
257
272
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;
258
278
for (auto *BasicType : BasicTypes) {
259
279
const Register BasicTypeStrReg = EmitOpString (BasicType->getName ());
260
280
@@ -288,11 +308,46 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
288
308
const Register AttributeEncodingReg =
289
309
GR->buildConstantInt (AttributeEncoding, MIRBuilder, I32Ty, false );
290
310
291
- [[maybe_unused]]
292
311
const Register BasicTypeReg =
293
312
EmitDIInstruction (SPIRV::NonSemanticExtInst::DebugTypeBasic,
294
313
{BasicTypeStrReg, ConstIntBitwidthReg,
295
314
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
+ }
296
351
}
297
352
}
298
353
return true ;
0 commit comments