xref: /freebsd/contrib/llvm-project/llvm/lib/Target/SPIRV/SPIRVEmitNonSemanticDI.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric #include "MCTargetDesc/SPIRVBaseInfo.h"
2*700637cbSDimitry Andric #include "MCTargetDesc/SPIRVMCTargetDesc.h"
3*700637cbSDimitry Andric #include "SPIRV.h"
4*700637cbSDimitry Andric #include "SPIRVGlobalRegistry.h"
5*700637cbSDimitry Andric #include "SPIRVRegisterInfo.h"
6*700637cbSDimitry Andric #include "SPIRVTargetMachine.h"
7*700637cbSDimitry Andric #include "SPIRVUtils.h"
8*700637cbSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
9*700637cbSDimitry Andric #include "llvm/ADT/SmallString.h"
10*700637cbSDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
11*700637cbSDimitry Andric #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
12*700637cbSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
13*700637cbSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
14*700637cbSDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
15*700637cbSDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
16*700637cbSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
17*700637cbSDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
18*700637cbSDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
19*700637cbSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
20*700637cbSDimitry Andric #include "llvm/CodeGen/Register.h"
21*700637cbSDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
22*700637cbSDimitry Andric #include "llvm/IR/DebugProgramInstruction.h"
23*700637cbSDimitry Andric #include "llvm/IR/Metadata.h"
24*700637cbSDimitry Andric #include "llvm/Support/Casting.h"
25*700637cbSDimitry Andric #include "llvm/Support/Path.h"
26*700637cbSDimitry Andric 
27*700637cbSDimitry Andric #define DEBUG_TYPE "spirv-nonsemantic-debug-info"
28*700637cbSDimitry Andric 
29*700637cbSDimitry Andric using namespace llvm;
30*700637cbSDimitry Andric 
31*700637cbSDimitry Andric namespace {
32*700637cbSDimitry Andric struct SPIRVEmitNonSemanticDI : public MachineFunctionPass {
33*700637cbSDimitry Andric   static char ID;
34*700637cbSDimitry Andric   SPIRVTargetMachine *TM;
SPIRVEmitNonSemanticDI__anona05e3cd80111::SPIRVEmitNonSemanticDI35*700637cbSDimitry Andric   SPIRVEmitNonSemanticDI(SPIRVTargetMachine *TM = nullptr)
36*700637cbSDimitry Andric       : MachineFunctionPass(ID), TM(TM) {}
37*700637cbSDimitry Andric 
38*700637cbSDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
39*700637cbSDimitry Andric 
40*700637cbSDimitry Andric private:
41*700637cbSDimitry Andric   bool IsGlobalDIEmitted = false;
42*700637cbSDimitry Andric   bool emitGlobalDI(MachineFunction &MF);
43*700637cbSDimitry Andric };
44*700637cbSDimitry Andric } // anonymous namespace
45*700637cbSDimitry Andric 
46*700637cbSDimitry Andric INITIALIZE_PASS(SPIRVEmitNonSemanticDI, DEBUG_TYPE,
47*700637cbSDimitry Andric                 "SPIRV NonSemantic.Shader.DebugInfo.100 emitter", false, false)
48*700637cbSDimitry Andric 
49*700637cbSDimitry Andric char SPIRVEmitNonSemanticDI::ID = 0;
50*700637cbSDimitry Andric 
51*700637cbSDimitry Andric MachineFunctionPass *
createSPIRVEmitNonSemanticDIPass(SPIRVTargetMachine * TM)52*700637cbSDimitry Andric llvm::createSPIRVEmitNonSemanticDIPass(SPIRVTargetMachine *TM) {
53*700637cbSDimitry Andric   return new SPIRVEmitNonSemanticDI(TM);
54*700637cbSDimitry Andric }
55*700637cbSDimitry Andric 
56*700637cbSDimitry Andric enum BaseTypeAttributeEncoding {
57*700637cbSDimitry Andric   Unspecified = 0,
58*700637cbSDimitry Andric   Address = 1,
59*700637cbSDimitry Andric   Boolean = 2,
60*700637cbSDimitry Andric   Float = 3,
61*700637cbSDimitry Andric   Signed = 4,
62*700637cbSDimitry Andric   SignedChar = 5,
63*700637cbSDimitry Andric   Unsigned = 6,
64*700637cbSDimitry Andric   UnsignedChar = 7
65*700637cbSDimitry Andric };
66*700637cbSDimitry Andric 
67*700637cbSDimitry Andric enum SourceLanguage {
68*700637cbSDimitry Andric   Unknown = 0,
69*700637cbSDimitry Andric   ESSL = 1,
70*700637cbSDimitry Andric   GLSL = 2,
71*700637cbSDimitry Andric   OpenCL_C = 3,
72*700637cbSDimitry Andric   OpenCL_CPP = 4,
73*700637cbSDimitry Andric   HLSL = 5,
74*700637cbSDimitry Andric   CPP_for_OpenCL = 6,
75*700637cbSDimitry Andric   SYCL = 7,
76*700637cbSDimitry Andric   HERO_C = 8,
77*700637cbSDimitry Andric   NZSL = 9,
78*700637cbSDimitry Andric   WGSL = 10,
79*700637cbSDimitry Andric   Slang = 11,
80*700637cbSDimitry Andric   Zig = 12
81*700637cbSDimitry Andric };
82*700637cbSDimitry Andric 
emitGlobalDI(MachineFunction & MF)83*700637cbSDimitry Andric bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
84*700637cbSDimitry Andric   // If this MachineFunction doesn't have any BB repeat procedure
85*700637cbSDimitry Andric   // for the next
86*700637cbSDimitry Andric   if (MF.begin() == MF.end()) {
87*700637cbSDimitry Andric     IsGlobalDIEmitted = false;
88*700637cbSDimitry Andric     return false;
89*700637cbSDimitry Andric   }
90*700637cbSDimitry Andric 
91*700637cbSDimitry Andric   // Required variables to get from metadata search
92*700637cbSDimitry Andric   LLVMContext *Context;
93*700637cbSDimitry Andric   SmallVector<SmallString<128>> FilePaths;
94*700637cbSDimitry Andric   SmallVector<int64_t> LLVMSourceLanguages;
95*700637cbSDimitry Andric   int64_t DwarfVersion = 0;
96*700637cbSDimitry Andric   int64_t DebugInfoVersion = 0;
97*700637cbSDimitry Andric   SmallPtrSet<DIBasicType *, 12> BasicTypes;
98*700637cbSDimitry Andric   SmallPtrSet<DIDerivedType *, 12> PointerDerivedTypes;
99*700637cbSDimitry Andric   // Searching through the Module metadata to find nescessary
100*700637cbSDimitry Andric   // information like DwarfVersion or SourceLanguage
101*700637cbSDimitry Andric   {
102*700637cbSDimitry Andric     const MachineModuleInfo &MMI =
103*700637cbSDimitry Andric         getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
104*700637cbSDimitry Andric     const Module *M = MMI.getModule();
105*700637cbSDimitry Andric     Context = &M->getContext();
106*700637cbSDimitry Andric     const NamedMDNode *DbgCu = M->getNamedMetadata("llvm.dbg.cu");
107*700637cbSDimitry Andric     if (!DbgCu)
108*700637cbSDimitry Andric       return false;
109*700637cbSDimitry Andric     for (const auto *Op : DbgCu->operands()) {
110*700637cbSDimitry Andric       if (const auto *CompileUnit = dyn_cast<DICompileUnit>(Op)) {
111*700637cbSDimitry Andric         DIFile *File = CompileUnit->getFile();
112*700637cbSDimitry Andric         FilePaths.emplace_back();
113*700637cbSDimitry Andric         sys::path::append(FilePaths.back(), File->getDirectory(),
114*700637cbSDimitry Andric                           File->getFilename());
115*700637cbSDimitry Andric         LLVMSourceLanguages.push_back(CompileUnit->getSourceLanguage());
116*700637cbSDimitry Andric       }
117*700637cbSDimitry Andric     }
118*700637cbSDimitry Andric     const NamedMDNode *ModuleFlags = M->getNamedMetadata("llvm.module.flags");
119*700637cbSDimitry Andric     for (const auto *Op : ModuleFlags->operands()) {
120*700637cbSDimitry Andric       const MDOperand &MaybeStrOp = Op->getOperand(1);
121*700637cbSDimitry Andric       if (MaybeStrOp.equalsStr("Dwarf Version"))
122*700637cbSDimitry Andric         DwarfVersion =
123*700637cbSDimitry Andric             cast<ConstantInt>(
124*700637cbSDimitry Andric                 cast<ConstantAsMetadata>(Op->getOperand(2))->getValue())
125*700637cbSDimitry Andric                 ->getSExtValue();
126*700637cbSDimitry Andric       else if (MaybeStrOp.equalsStr("Debug Info Version"))
127*700637cbSDimitry Andric         DebugInfoVersion =
128*700637cbSDimitry Andric             cast<ConstantInt>(
129*700637cbSDimitry Andric                 cast<ConstantAsMetadata>(Op->getOperand(2))->getValue())
130*700637cbSDimitry Andric                 ->getSExtValue();
131*700637cbSDimitry Andric     }
132*700637cbSDimitry Andric 
133*700637cbSDimitry Andric     // This traversal is the only supported way to access
134*700637cbSDimitry Andric     // instruction related DI metadata like DIBasicType
135*700637cbSDimitry Andric     for (auto &F : *M) {
136*700637cbSDimitry Andric       for (auto &BB : F) {
137*700637cbSDimitry Andric         for (auto &I : BB) {
138*700637cbSDimitry Andric           for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
139*700637cbSDimitry Andric             DILocalVariable *LocalVariable = DVR.getVariable();
140*700637cbSDimitry Andric             if (auto *BasicType =
141*700637cbSDimitry Andric                     dyn_cast<DIBasicType>(LocalVariable->getType())) {
142*700637cbSDimitry Andric               BasicTypes.insert(BasicType);
143*700637cbSDimitry Andric             } else if (auto *DerivedType =
144*700637cbSDimitry Andric                            dyn_cast<DIDerivedType>(LocalVariable->getType())) {
145*700637cbSDimitry Andric               if (DerivedType->getTag() == dwarf::DW_TAG_pointer_type) {
146*700637cbSDimitry Andric                 PointerDerivedTypes.insert(DerivedType);
147*700637cbSDimitry Andric                 // DIBasicType can be unreachable from DbgRecord and only
148*700637cbSDimitry Andric                 // pointed on from other DI types
149*700637cbSDimitry Andric                 // DerivedType->getBaseType is null when pointer
150*700637cbSDimitry Andric                 // is representing a void type
151*700637cbSDimitry Andric                 if (auto *BT = dyn_cast_or_null<DIBasicType>(
152*700637cbSDimitry Andric                         DerivedType->getBaseType()))
153*700637cbSDimitry Andric                   BasicTypes.insert(BT);
154*700637cbSDimitry Andric               }
155*700637cbSDimitry Andric             }
156*700637cbSDimitry Andric           }
157*700637cbSDimitry Andric         }
158*700637cbSDimitry Andric       }
159*700637cbSDimitry Andric     }
160*700637cbSDimitry Andric   }
161*700637cbSDimitry Andric   // NonSemantic.Shader.DebugInfo.100 global DI instruction emitting
162*700637cbSDimitry Andric   {
163*700637cbSDimitry Andric     // Required LLVM variables for emitting logic
164*700637cbSDimitry Andric     const SPIRVInstrInfo *TII = TM->getSubtargetImpl()->getInstrInfo();
165*700637cbSDimitry Andric     const SPIRVRegisterInfo *TRI = TM->getSubtargetImpl()->getRegisterInfo();
166*700637cbSDimitry Andric     const RegisterBankInfo *RBI = TM->getSubtargetImpl()->getRegBankInfo();
167*700637cbSDimitry Andric     SPIRVGlobalRegistry *GR = TM->getSubtargetImpl()->getSPIRVGlobalRegistry();
168*700637cbSDimitry Andric     MachineRegisterInfo &MRI = MF.getRegInfo();
169*700637cbSDimitry Andric     MachineBasicBlock &MBB = *MF.begin();
170*700637cbSDimitry Andric 
171*700637cbSDimitry Andric     // To correct placement of a OpLabel instruction during SPIRVAsmPrinter
172*700637cbSDimitry Andric     // emission all new instructions needs to be placed after OpFunction
173*700637cbSDimitry Andric     // and before first terminator
174*700637cbSDimitry Andric     MachineIRBuilder MIRBuilder(MBB, MBB.getFirstTerminator());
175*700637cbSDimitry Andric 
176*700637cbSDimitry Andric     const auto EmitOpString = [&](StringRef SR) {
177*700637cbSDimitry Andric       const Register StrReg = MRI.createVirtualRegister(&SPIRV::IDRegClass);
178*700637cbSDimitry Andric       MRI.setType(StrReg, LLT::scalar(32));
179*700637cbSDimitry Andric       MachineInstrBuilder MIB = MIRBuilder.buildInstr(SPIRV::OpString);
180*700637cbSDimitry Andric       MIB.addDef(StrReg);
181*700637cbSDimitry Andric       addStringImm(SR, MIB);
182*700637cbSDimitry Andric       return StrReg;
183*700637cbSDimitry Andric     };
184*700637cbSDimitry Andric 
185*700637cbSDimitry Andric     const SPIRVType *VoidTy =
186*700637cbSDimitry Andric         GR->getOrCreateSPIRVType(Type::getVoidTy(*Context), MIRBuilder,
187*700637cbSDimitry Andric                                  SPIRV::AccessQualifier::ReadWrite, false);
188*700637cbSDimitry Andric 
189*700637cbSDimitry Andric     const auto EmitDIInstruction =
190*700637cbSDimitry Andric         [&](SPIRV::NonSemanticExtInst::NonSemanticExtInst Inst,
191*700637cbSDimitry Andric             std::initializer_list<Register> Registers) {
192*700637cbSDimitry Andric           const Register InstReg =
193*700637cbSDimitry Andric               MRI.createVirtualRegister(&SPIRV::IDRegClass);
194*700637cbSDimitry Andric           MRI.setType(InstReg, LLT::scalar(32));
195*700637cbSDimitry Andric           MachineInstrBuilder MIB =
196*700637cbSDimitry Andric               MIRBuilder.buildInstr(SPIRV::OpExtInst)
197*700637cbSDimitry Andric                   .addDef(InstReg)
198*700637cbSDimitry Andric                   .addUse(GR->getSPIRVTypeID(VoidTy))
199*700637cbSDimitry Andric                   .addImm(static_cast<int64_t>(
200*700637cbSDimitry Andric                       SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100))
201*700637cbSDimitry Andric                   .addImm(Inst);
202*700637cbSDimitry Andric           for (auto Reg : Registers) {
203*700637cbSDimitry Andric             MIB.addUse(Reg);
204*700637cbSDimitry Andric           }
205*700637cbSDimitry Andric           MIB.constrainAllUses(*TII, *TRI, *RBI);
206*700637cbSDimitry Andric           GR->assignSPIRVTypeToVReg(VoidTy, InstReg, MF);
207*700637cbSDimitry Andric           return InstReg;
208*700637cbSDimitry Andric         };
209*700637cbSDimitry Andric 
210*700637cbSDimitry Andric     const SPIRVType *I32Ty =
211*700637cbSDimitry Andric         GR->getOrCreateSPIRVType(Type::getInt32Ty(*Context), MIRBuilder,
212*700637cbSDimitry Andric                                  SPIRV::AccessQualifier::ReadWrite, false);
213*700637cbSDimitry Andric 
214*700637cbSDimitry Andric     const Register DwarfVersionReg =
215*700637cbSDimitry Andric         GR->buildConstantInt(DwarfVersion, MIRBuilder, I32Ty, false);
216*700637cbSDimitry Andric 
217*700637cbSDimitry Andric     const Register DebugInfoVersionReg =
218*700637cbSDimitry Andric         GR->buildConstantInt(DebugInfoVersion, MIRBuilder, I32Ty, false);
219*700637cbSDimitry Andric 
220*700637cbSDimitry Andric     for (unsigned Idx = 0; Idx < LLVMSourceLanguages.size(); ++Idx) {
221*700637cbSDimitry Andric       const Register FilePathStrReg = EmitOpString(FilePaths[Idx]);
222*700637cbSDimitry Andric 
223*700637cbSDimitry Andric       const Register DebugSourceResIdReg = EmitDIInstruction(
224*700637cbSDimitry Andric           SPIRV::NonSemanticExtInst::DebugSource, {FilePathStrReg});
225*700637cbSDimitry Andric 
226*700637cbSDimitry Andric       SourceLanguage SpirvSourceLanguage = SourceLanguage::Unknown;
227*700637cbSDimitry Andric       switch (LLVMSourceLanguages[Idx]) {
228*700637cbSDimitry Andric       case dwarf::DW_LANG_OpenCL:
229*700637cbSDimitry Andric         SpirvSourceLanguage = SourceLanguage::OpenCL_C;
230*700637cbSDimitry Andric         break;
231*700637cbSDimitry Andric       case dwarf::DW_LANG_OpenCL_CPP:
232*700637cbSDimitry Andric         SpirvSourceLanguage = SourceLanguage::OpenCL_CPP;
233*700637cbSDimitry Andric         break;
234*700637cbSDimitry Andric       case dwarf::DW_LANG_CPP_for_OpenCL:
235*700637cbSDimitry Andric         SpirvSourceLanguage = SourceLanguage::CPP_for_OpenCL;
236*700637cbSDimitry Andric         break;
237*700637cbSDimitry Andric       case dwarf::DW_LANG_GLSL:
238*700637cbSDimitry Andric         SpirvSourceLanguage = SourceLanguage::GLSL;
239*700637cbSDimitry Andric         break;
240*700637cbSDimitry Andric       case dwarf::DW_LANG_HLSL:
241*700637cbSDimitry Andric         SpirvSourceLanguage = SourceLanguage::HLSL;
242*700637cbSDimitry Andric         break;
243*700637cbSDimitry Andric       case dwarf::DW_LANG_SYCL:
244*700637cbSDimitry Andric         SpirvSourceLanguage = SourceLanguage::SYCL;
245*700637cbSDimitry Andric         break;
246*700637cbSDimitry Andric       case dwarf::DW_LANG_Zig:
247*700637cbSDimitry Andric         SpirvSourceLanguage = SourceLanguage::Zig;
248*700637cbSDimitry Andric       }
249*700637cbSDimitry Andric 
250*700637cbSDimitry Andric       const Register SourceLanguageReg =
251*700637cbSDimitry Andric           GR->buildConstantInt(SpirvSourceLanguage, MIRBuilder, I32Ty, false);
252*700637cbSDimitry Andric 
253*700637cbSDimitry Andric       [[maybe_unused]]
254*700637cbSDimitry Andric       const Register DebugCompUnitResIdReg =
255*700637cbSDimitry Andric           EmitDIInstruction(SPIRV::NonSemanticExtInst::DebugCompilationUnit,
256*700637cbSDimitry Andric                             {DebugInfoVersionReg, DwarfVersionReg,
257*700637cbSDimitry Andric                              DebugSourceResIdReg, SourceLanguageReg});
258*700637cbSDimitry Andric     }
259*700637cbSDimitry Andric 
260*700637cbSDimitry Andric     // We aren't extracting any DebugInfoFlags now so we
261*700637cbSDimitry Andric     // emitting zero to use as <id>Flags argument for DebugBasicType
262*700637cbSDimitry Andric     const Register I32ZeroReg =
263*700637cbSDimitry Andric         GR->buildConstantInt(0, MIRBuilder, I32Ty, false, false);
264*700637cbSDimitry Andric 
265*700637cbSDimitry Andric     // We need to store pairs because further instructions reference
266*700637cbSDimitry Andric     // the DIBasicTypes and size will be always small so there isn't
267*700637cbSDimitry Andric     // need for any kind of map
268*700637cbSDimitry Andric     SmallVector<std::pair<const DIBasicType *const, const Register>, 12>
269*700637cbSDimitry Andric         BasicTypeRegPairs;
270*700637cbSDimitry Andric     for (auto *BasicType : BasicTypes) {
271*700637cbSDimitry Andric       const Register BasicTypeStrReg = EmitOpString(BasicType->getName());
272*700637cbSDimitry Andric 
273*700637cbSDimitry Andric       const Register ConstIntBitwidthReg = GR->buildConstantInt(
274*700637cbSDimitry Andric           BasicType->getSizeInBits(), MIRBuilder, I32Ty, false);
275*700637cbSDimitry Andric 
276*700637cbSDimitry Andric       uint64_t AttributeEncoding = BaseTypeAttributeEncoding::Unspecified;
277*700637cbSDimitry Andric       switch (BasicType->getEncoding()) {
278*700637cbSDimitry Andric       case dwarf::DW_ATE_signed:
279*700637cbSDimitry Andric         AttributeEncoding = BaseTypeAttributeEncoding::Signed;
280*700637cbSDimitry Andric         break;
281*700637cbSDimitry Andric       case dwarf::DW_ATE_unsigned:
282*700637cbSDimitry Andric         AttributeEncoding = BaseTypeAttributeEncoding::Unsigned;
283*700637cbSDimitry Andric         break;
284*700637cbSDimitry Andric       case dwarf::DW_ATE_unsigned_char:
285*700637cbSDimitry Andric         AttributeEncoding = BaseTypeAttributeEncoding::UnsignedChar;
286*700637cbSDimitry Andric         break;
287*700637cbSDimitry Andric       case dwarf::DW_ATE_signed_char:
288*700637cbSDimitry Andric         AttributeEncoding = BaseTypeAttributeEncoding::SignedChar;
289*700637cbSDimitry Andric         break;
290*700637cbSDimitry Andric       case dwarf::DW_ATE_float:
291*700637cbSDimitry Andric         AttributeEncoding = BaseTypeAttributeEncoding::Float;
292*700637cbSDimitry Andric         break;
293*700637cbSDimitry Andric       case dwarf::DW_ATE_boolean:
294*700637cbSDimitry Andric         AttributeEncoding = BaseTypeAttributeEncoding::Boolean;
295*700637cbSDimitry Andric         break;
296*700637cbSDimitry Andric       case dwarf::DW_ATE_address:
297*700637cbSDimitry Andric         AttributeEncoding = BaseTypeAttributeEncoding::Address;
298*700637cbSDimitry Andric       }
299*700637cbSDimitry Andric 
300*700637cbSDimitry Andric       const Register AttributeEncodingReg =
301*700637cbSDimitry Andric           GR->buildConstantInt(AttributeEncoding, MIRBuilder, I32Ty, false);
302*700637cbSDimitry Andric 
303*700637cbSDimitry Andric       const Register BasicTypeReg =
304*700637cbSDimitry Andric           EmitDIInstruction(SPIRV::NonSemanticExtInst::DebugTypeBasic,
305*700637cbSDimitry Andric                             {BasicTypeStrReg, ConstIntBitwidthReg,
306*700637cbSDimitry Andric                              AttributeEncodingReg, I32ZeroReg});
307*700637cbSDimitry Andric       BasicTypeRegPairs.emplace_back(BasicType, BasicTypeReg);
308*700637cbSDimitry Andric     }
309*700637cbSDimitry Andric 
310*700637cbSDimitry Andric     if (PointerDerivedTypes.size()) {
311*700637cbSDimitry Andric       for (const auto *PointerDerivedType : PointerDerivedTypes) {
312*700637cbSDimitry Andric 
313*700637cbSDimitry Andric         assert(PointerDerivedType->getDWARFAddressSpace().has_value());
314*700637cbSDimitry Andric         const Register StorageClassReg = GR->buildConstantInt(
315*700637cbSDimitry Andric             addressSpaceToStorageClass(
316*700637cbSDimitry Andric                 PointerDerivedType->getDWARFAddressSpace().value(),
317*700637cbSDimitry Andric                 *TM->getSubtargetImpl()),
318*700637cbSDimitry Andric             MIRBuilder, I32Ty, false);
319*700637cbSDimitry Andric 
320*700637cbSDimitry Andric         // If the Pointer is representing a void type it's getBaseType
321*700637cbSDimitry Andric         // is a nullptr
322*700637cbSDimitry Andric         const auto *MaybeNestedBasicType =
323*700637cbSDimitry Andric             dyn_cast_or_null<DIBasicType>(PointerDerivedType->getBaseType());
324*700637cbSDimitry Andric         if (MaybeNestedBasicType) {
325*700637cbSDimitry Andric           for (const auto &BasicTypeRegPair : BasicTypeRegPairs) {
326*700637cbSDimitry Andric             const auto &[DefinedBasicType, BasicTypeReg] = BasicTypeRegPair;
327*700637cbSDimitry Andric             if (DefinedBasicType == MaybeNestedBasicType) {
328*700637cbSDimitry Andric               [[maybe_unused]]
329*700637cbSDimitry Andric               const Register DebugPointerTypeReg = EmitDIInstruction(
330*700637cbSDimitry Andric                   SPIRV::NonSemanticExtInst::DebugTypePointer,
331*700637cbSDimitry Andric                   {BasicTypeReg, StorageClassReg, I32ZeroReg});
332*700637cbSDimitry Andric             }
333*700637cbSDimitry Andric           }
334*700637cbSDimitry Andric         } else {
335*700637cbSDimitry Andric           const Register DebugInfoNoneReg =
336*700637cbSDimitry Andric               EmitDIInstruction(SPIRV::NonSemanticExtInst::DebugInfoNone, {});
337*700637cbSDimitry Andric           [[maybe_unused]]
338*700637cbSDimitry Andric           const Register DebugPointerTypeReg = EmitDIInstruction(
339*700637cbSDimitry Andric               SPIRV::NonSemanticExtInst::DebugTypePointer,
340*700637cbSDimitry Andric               {DebugInfoNoneReg, StorageClassReg, I32ZeroReg});
341*700637cbSDimitry Andric         }
342*700637cbSDimitry Andric       }
343*700637cbSDimitry Andric     }
344*700637cbSDimitry Andric   }
345*700637cbSDimitry Andric   return true;
346*700637cbSDimitry Andric }
347*700637cbSDimitry Andric 
runOnMachineFunction(MachineFunction & MF)348*700637cbSDimitry Andric bool SPIRVEmitNonSemanticDI::runOnMachineFunction(MachineFunction &MF) {
349*700637cbSDimitry Andric   bool Res = false;
350*700637cbSDimitry Andric   // emitGlobalDI needs to be executed only once to avoid
351*700637cbSDimitry Andric   // emitting duplicates
352*700637cbSDimitry Andric   if (!IsGlobalDIEmitted) {
353*700637cbSDimitry Andric     IsGlobalDIEmitted = true;
354*700637cbSDimitry Andric     Res = emitGlobalDI(MF);
355*700637cbSDimitry Andric   }
356*700637cbSDimitry Andric   return Res;
357*700637cbSDimitry Andric }
358