@@ -212,8 +212,8 @@ template <> struct DebugTypeContainer<DebugLine> {
212212};
213213
214214template <typename T> struct DebugLiveContainer {
215- SmallVector<T> Value ;
216- SmallVector<size_t > Back ;
215+ SmallVector<T> Values ;
216+ SmallVector<size_t > BackIdx ;
217217};
218218
219219class LiveRepository {
@@ -228,46 +228,47 @@ class LiveRepository {
228228 SmallVector<Register> Registers;
229229 SmallVector<std::pair<TypesMapping, unsigned >> Instructions;
230230
231- template <typename T> constexpr SmallVector<T> &value () {
231+ template <typename T> constexpr SmallVector<T> &values () {
232232 if constexpr (std::is_same_v<T, int64_t >) {
233- return PrimitiveInts.Value ;
233+ return PrimitiveInts.Values ;
234234 } else if constexpr (std::is_same_v<T, StringRef>) {
235- return PrimitiveStrings.Value ;
235+ return PrimitiveStrings.Values ;
236236 } else if constexpr (std::is_same_v<T, DebugLine>) {
237- return DebugLines.Value ;
237+ return DebugLines.Values ;
238238 } else if constexpr (std::is_same_v<T, DebugSource>) {
239- return DebugSources.Value ;
239+ return DebugSources.Values ;
240240 } else if constexpr (std::is_same_v<T, DebugCompilationUnit>) {
241- return DebugCompilationUnits.Value ;
241+ return DebugCompilationUnits.Values ;
242242 } else if constexpr (std::is_same_v<T, DebugTypeBasic>) {
243- return DebugTypeBasics.Value ;
243+ return DebugTypeBasics.Values ;
244244 } else if constexpr (std::is_same_v<T, DebugTypePointer>) {
245- return DebugTypePointers.Value ;
245+ return DebugTypePointers.Values ;
246246 }
247247 llvm_unreachable (" unreachable" );
248248 }
249249
250- template <typename T> constexpr SmallVector<size_t > &back () {
250+ template <typename T> constexpr SmallVector<size_t > &backIdx () {
251251 if constexpr (std::is_same_v<T, int64_t >) {
252- return PrimitiveInts.Back ;
252+ return PrimitiveInts.BackIdx ;
253253 } else if constexpr (std::is_same_v<T, StringRef>) {
254- return PrimitiveStrings.Back ;
254+ return PrimitiveStrings.BackIdx ;
255255 } else if constexpr (std::is_same_v<T, DebugLine>) {
256- return DebugLines.Back ;
256+ return DebugLines.BackIdx ;
257257 } else if constexpr (std::is_same_v<T, DebugSource>) {
258- return DebugSources.Back ;
258+ return DebugSources.BackIdx ;
259259 } else if constexpr (std::is_same_v<T, DebugCompilationUnit>) {
260- return DebugCompilationUnits.Back ;
260+ return DebugCompilationUnits.BackIdx ;
261261 } else if constexpr (std::is_same_v<T, DebugTypeBasic>) {
262- return DebugTypeBasics.Back ;
262+ return DebugTypeBasics.BackIdx ;
263263 } else if constexpr (std::is_same_v<T, DebugTypePointer>) {
264- return DebugTypePointers.Back ;
264+ return DebugTypePointers.BackIdx ;
265265 }
266266 llvm_unreachable (" unreachable" );
267267 }
268268
269269 template <typename T>
270- static std::pair<size_t , bool > helper (T Val, SmallVectorImpl<T> &SV) {
270+ static std::pair<size_t , bool >
271+ emplaceOrReturnDuplicate (T Val, SmallVectorImpl<T> &SV) {
271272 for (unsigned Idx = 0 ; Idx < SV.size (); ++Idx) {
272273 if (Val == SV[Idx]) {
273274 return {Idx, true };
@@ -318,44 +319,45 @@ class LiveRepository {
318319public:
319320 size_t push (const int64_t Val, MachineIRBuilder &MIRBuilder,
320321 const SPIRVTargetMachine *TM) {
321- auto &SV = value <int64_t >();
322- const auto [ConcreteIdx, IsDuplicate] = helper (Val, SV);
322+ auto &SV = values <int64_t >();
323+ const auto [ConcreteIdx, IsDuplicate] = emplaceOrReturnDuplicate (Val, SV);
323324 if (IsDuplicate) {
324- return back <int64_t >()[ConcreteIdx];
325+ return backIdx <int64_t >()[ConcreteIdx];
325326 }
326327 Instructions.emplace_back (DebugTypeContainer<int64_t >::TM, ConcreteIdx);
327328 SPIRVGlobalRegistry *GR = TM->getSubtargetImpl ()->getSPIRVGlobalRegistry ();
328329 const SPIRVType *I32Ty = GR->getOrCreateSPIRVType (
329330 Type::getInt32Ty (MIRBuilder.getContext ()), MIRBuilder);
330331 Registers.emplace_back (GR->buildConstantInt (Val, MIRBuilder, I32Ty, false ));
331- back <int64_t >().emplace_back (Instructions.size () - 1 );
332+ backIdx <int64_t >().emplace_back (Instructions.size () - 1 );
332333 return Instructions.size () - 1 ;
333334 }
334335
335336 size_t push (const StringRef Val, MachineIRBuilder &MIRBuilder) {
336- auto &SV = value <StringRef>();
337- const auto [ConcreteIdx, IsDuplicate] = helper (Val, SV);
337+ auto &SV = values <StringRef>();
338+ const auto [ConcreteIdx, IsDuplicate] = emplaceOrReturnDuplicate (Val, SV);
338339 if (IsDuplicate) {
339- return back <StringRef>()[ConcreteIdx];
340+ return backIdx <StringRef>()[ConcreteIdx];
340341 }
341342 Instructions.emplace_back (DebugTypeContainer<StringRef>::TM, ConcreteIdx);
342343 Registers.emplace_back (emitOpString (Val, MIRBuilder));
343- back <StringRef>().emplace_back (Instructions.size () - 1 );
344+ backIdx <StringRef>().emplace_back (Instructions.size () - 1 );
344345 return Instructions.size () - 1 ;
345346 }
346347
347348 template <typename T>
348349 constexpr size_t push (ArrayRef<size_t > Args, MachineIRBuilder &MIRBuilder,
349350 const SPIRVTargetMachine *TM) {
350- auto &SV = value<T>();
351- const auto [ConcreteIdx, IsDuplicate] = helper (T (Args), SV);
351+ auto &SV = values<T>();
352+ const auto [ConcreteIdx, IsDuplicate] =
353+ emplaceOrReturnDuplicate (T (Args), SV);
352354 if (IsDuplicate) {
353- return back <T>()[ConcreteIdx];
355+ return backIdx <T>()[ConcreteIdx];
354356 }
355357 Instructions.emplace_back (DebugTypeContainer<T>::TM, ConcreteIdx);
356358 Registers.emplace_back (
357359 emitDIInstruction (DebugTypeContainer<T>::Inst, Args, MIRBuilder, TM));
358- back <T>().emplace_back (Instructions.size () - 1 );
360+ backIdx <T>().emplace_back (Instructions.size () - 1 );
359361 return Instructions.size () - 1 ;
360362 }
361363};
@@ -404,7 +406,7 @@ size_t emitDebugCompilationUnits(const Module *M, MachineIRBuilder &MIRBuilder,
404406 DebugInfoVersionId = LR.push (DebugInfoVersion, MIRBuilder, TM);
405407 }
406408 }
407-
409+ assert (DwarfVersionId. has_value () && DebugInfoVersionId. has_value ());
408410 const NamedMDNode *DbgCu = M->getNamedMetadata (" llvm.dbg.cu" );
409411 for (const auto *Op : DbgCu->operands ()) {
410412 if (const auto *CompileUnit = dyn_cast<DICompileUnit>(Op)) {
@@ -522,9 +524,6 @@ size_t emitDebugTypePointer(const DIDerivedType *DT, const size_t I32ZeroIdx,
522524 LR.push (addressSpaceToStorageClass (DT->getDWARFAddressSpace ().value (),
523525 *TM->getSubtargetImpl ()),
524526 MIRBuilder, TM);
525-
526- // If the Pointer is representing a void type it's getBaseType
527- // is a nullptr
528527 size_t DebugInfoNoneIdx = LR.push <DebugInfoNone>({}, MIRBuilder, TM);
529528 return LR.push <DebugTypePointer>(
530529 {DebugInfoNoneIdx, StorageClassIdx, I32ZeroIdx}, MIRBuilder, TM);
@@ -573,9 +572,9 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF, const Module *M,
573572 LiveRepository &LR) const {
574573 MachineBasicBlock &MBB = *MF.begin ();
575574
576- // To correct placement of a OpLabel instruction during SPIRVAsmPrinter
577- // emission all new instructions needs to be placed after OpFunction
578- // and before first terminator
575+ // To ensure correct placement of an OpLabel instruction during
576+ // SPIRVAsmPrinter emission, all new instructions must be positioned after
577+ // OpFunction and before the first terminator.
579578 MachineIRBuilder MIRBuilder (MBB, MBB.getFirstTerminator ());
580579
581580 emitDebugCompilationUnits (M, MIRBuilder, TM, LR);
@@ -587,22 +586,20 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF, const Module *M,
587586 const DILocalVariable *LocalVariable = DVR.getVariable ();
588587 if (const auto *BasicType =
589588 dyn_cast<DIBasicType>(LocalVariable->getType ())) {
590- // We aren't extracting any DebugInfoFlags now so we're
591- // emitting zero to use as <id>Flags argument for DebugBasicType
589+ // Currently, we are not extracting any DebugInfoFlags,
590+ // so we emit zero as the <id>Flags argument for DebugBasicType.
592591 const size_t I32ZeroIdx = LR.push (0 , MIRBuilder, TM);
593592 emitDebugTypeBasic (BasicType, I32ZeroIdx, MIRBuilder, TM, LR);
594593 continue ;
595594 }
596- // Beware else if here. Types from previous scopes are
597- // counterintuitively still visible for the next ifs scopes.
598595 if (const auto *DerivedType =
599596 dyn_cast<DIDerivedType>(LocalVariable->getType ())) {
600597 if (DerivedType->getTag () == dwarf::DW_TAG_pointer_type) {
601598 const size_t I32ZeroIdx = LR.push (0 , MIRBuilder, TM);
602- // DIBasicType can be unreachable from DbgRecord and only
603- // pointed on from other DI types
604- // DerivedType->getBaseType is null when pointer
605- // is representing a void type
599+ // DIBasicType may be unreachable from DbgRecord and can only be
600+ // referenced by other Debug Information (DI) types. Note:
601+ // DerivedType->getBaseType returns null when the pointer
602+ // represents a void type.
606603 if (const auto *BasicType = dyn_cast_if_present<DIBasicType>(
607604 DerivedType->getBaseType ())) {
608605 const size_t BTIdx = emitDebugTypeBasic (BasicType, I32ZeroIdx,
@@ -642,23 +639,26 @@ bool SPIRVEmitNonSemanticDI::emitLineDI(MachineFunction &MF,
642639}
643640
644641bool SPIRVEmitNonSemanticDI::runOnMachineFunction (MachineFunction &MF) {
645- bool Res = false ;
646- LiveRepository LR;
647- // emitGlobalDI needs to be executed only once to avoid
648- // emitting duplicates
649- if (!IsGlobalDIEmitted) {
650- if (MF.begin () == MF.end ()) {
651- return false ;
642+ if (MF.begin () == MF.end ()) {
643+ return false ;
644+ }
645+ static bool IsDIInModule = true ;
646+ bool IsFunctionModified = false ;
647+ if (IsDIInModule) {
648+ LiveRepository LR;
649+ // emitGlobalDI should be executed only once to prevent
650+ // the emission of duplicate entries.
651+ if (!IsGlobalDIEmitted) {
652+ IsGlobalDIEmitted = true ;
653+ const MachineModuleInfo &MMI =
654+ getAnalysis<MachineModuleInfoWrapperPass>().getMMI ();
655+ const Module *M = MMI.getModule ();
656+ const NamedMDNode *DbgCu = M->getNamedMetadata (" llvm.dbg.cu" );
657+ if (!DbgCu)
658+ IsDIInModule = false ;
659+ IsFunctionModified = emitGlobalDI (MF, M, LR);
652660 }
653- IsGlobalDIEmitted = true ;
654- const MachineModuleInfo &MMI =
655- getAnalysis<MachineModuleInfoWrapperPass>().getMMI ();
656- const Module *M = MMI.getModule ();
657- const NamedMDNode *DbgCu = M->getNamedMetadata (" llvm.dbg.cu" );
658- if (!DbgCu)
659- return false ;
660- Res = emitGlobalDI (MF, M, LR);
661+ IsFunctionModified |= emitLineDI (MF, LR);
661662 }
662- Res |= emitLineDI (MF, LR);
663- return Res;
663+ return IsFunctionModified;
664664}
0 commit comments