@@ -37,7 +37,7 @@ struct SPIRVEmitNonSemanticDI : MachineFunctionPass {
3737private:
3838 bool IsGlobalDIEmitted = false ;
3939 bool emitGlobalDI (MachineFunction &MF, const Module *M) const ;
40- // bool emitLineDI(MachineFunction &MF);
40+ bool emitLineDI (MachineFunction &MF);
4141};
4242} // namespace llvm
4343
@@ -187,6 +187,19 @@ struct DebugLine {
187187 size_t LineEndId;
188188 size_t ColumnStartId;
189189 size_t ColumnEndId;
190+
191+ DebugLine (const ArrayRef<size_t > AR)
192+ : DebugSourceId(AR[0 ]), LineStartId(AR[1 ]),
193+ LineEndId (AR[2 ]), ColumnStartId(AR[3 ]),
194+ ColumnEndId(AR[4 ]) {}
195+
196+ friend bool operator ==(const DebugLine &Lhs, const DebugLine &Rhs) {
197+ return Lhs.DebugSourceId == Rhs.DebugSourceId &&
198+ Lhs.LineStartId == Rhs.LineStartId &&
199+ Lhs.LineEndId == Rhs.LineEndId &&
200+ Lhs.ColumnStartId == Rhs.ColumnStartId &&
201+ Lhs.ColumnEndId == Rhs.ColumnEndId ;
202+ }
190203};
191204
192205struct DebugInfoNone ;
@@ -243,20 +256,30 @@ template <> struct DebugTypeContainer<DebugInfoNone> {
243256 SPIRV::NonSemanticExtInst::DebugInfoNone;
244257};
245258
259+ template <> struct DebugTypeContainer <DebugLine> {
260+ static constexpr TypesMapping TM = DebugLineArray;
261+ static constexpr SPIRV::NonSemanticExtInst::NonSemanticExtInst Inst =
262+ SPIRV::NonSemanticExtInst::DebugLine;
263+ static SmallVector<DebugLine> Value;
264+ static SmallVector<size_t > Back;
265+ };
266+
246267SmallVector<int64_t > DebugTypeContainer<int64_t >::Value;
247268SmallVector<StringRef> DebugTypeContainer<StringRef>::Value;
248269SmallVector<DebugSource> DebugTypeContainer<DebugSource>::Value;
249270SmallVector<DebugCompilationUnit>
250271 DebugTypeContainer<DebugCompilationUnit>::Value;
251272SmallVector<DebugTypeBasic> DebugTypeContainer<DebugTypeBasic>::Value;
252273SmallVector<DebugTypePointer> DebugTypeContainer<DebugTypePointer>::Value;
274+ SmallVector<DebugLine> DebugTypeContainer<DebugLine>::Value;
253275
254276SmallVector<size_t > DebugTypeContainer<int64_t >::Back;
255277SmallVector<size_t > DebugTypeContainer<StringRef>::Back;
256278SmallVector<size_t > DebugTypeContainer<DebugSource>::Back;
257279SmallVector<size_t > DebugTypeContainer<DebugCompilationUnit>::Back;
258280SmallVector<size_t > DebugTypeContainer<DebugTypeBasic>::Back;
259281SmallVector<size_t > DebugTypeContainer<DebugTypePointer>::Back;
282+ SmallVector<size_t > DebugTypeContainer<DebugLine>::Back;
260283
261284SmallVector<Register> Registers;
262285SmallVector<std::pair<TypesMapping, unsigned >> Instructions;
@@ -365,9 +388,33 @@ size_t push<DebugInfoNone>(ArrayRef<size_t>, MachineIRBuilder &MIRBuilder,
365388 return DebugInfoNoneIdx.value ();
366389}
367390
391+ void cleanup () {
392+ DebugTypeContainer<int64_t >::Value.clear ();
393+ DebugTypeContainer<StringRef>::Value.clear ();
394+ DebugTypeContainer<DebugSource>::Value.clear ();
395+ DebugTypeContainer<DebugCompilationUnit>::Value.clear ();
396+ DebugTypeContainer<DebugTypeBasic>::Value.clear ();
397+ DebugTypeContainer<DebugTypePointer>::Value.clear ();
398+ DebugTypeContainer<DebugLine>::Value.clear ();
399+
400+ DebugTypeContainer<int64_t >::Back.clear ();
401+ DebugTypeContainer<StringRef>::Back.clear ();
402+ DebugTypeContainer<DebugSource>::Back.clear ();
403+ DebugTypeContainer<DebugCompilationUnit>::Back.clear ();
404+ DebugTypeContainer<DebugTypeBasic>::Back.clear ();
405+ DebugTypeContainer<DebugTypePointer>::Back.clear ();
406+ DebugTypeContainer<DebugLine>::Back.clear ();
407+ }
408+
409+ size_t emitDebugSource (const DIFile *File, MachineIRBuilder &MIRBuilder, SPIRVTargetMachine *TM) {
410+ SmallString<128 > FilePath;
411+ sys::path::append (FilePath, File->getDirectory (), File->getFilename ());
412+ const size_t FilePathId = push (StringRef (FilePath.c_str ()), MIRBuilder);
413+ return push<DebugSource>({FilePathId}, MIRBuilder, TM);
414+ }
415+
368416size_t emitDebugCompilationUnits (const Module *M, MachineIRBuilder &MIRBuilder,
369417 const SPIRVTargetMachine *TM) {
370- // TODO: Initialize assholes
371418 std::optional<size_t > DwarfVersionId = std::nullopt ;
372419 std::optional<size_t > DebugInfoVersionId = std::nullopt ;
373420 const NamedMDNode *ModuleFlags = M->getNamedMetadata (" llvm.module.flags" );
@@ -563,16 +610,22 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF,
563610 return true ;
564611}
565612
566- // bool SPIRVEmitNonSemanticDI::emitLineDI(MachineFunction &MF) {
567- // for (auto &MBB : MF) {
568- // for (auto &MI : MBB) {
569- // if (MI.isDebugValue()) {
570- // DebugLoc DL = MI.getDebugLoc();
571- // }
572- // }
573- // }
574- // return false;
575- // }
613+ bool SPIRVEmitNonSemanticDI::emitLineDI (MachineFunction &MF) {
614+ for (auto &MBB : MF) {
615+ for (auto &MI : MBB) {
616+ if (MI.isDebugValue ()) {
617+ MachineIRBuilder MIRBuilder (MBB, MI);
618+ DebugLoc DL = MI.getDebugLoc ();
619+ const auto *File = cast<DISubprogram>(DL.getScope ())->getFile ();
620+ const size_t ScopeIdx = emitDebugSource (File, MIRBuilder, TM);
621+ const size_t LineIdx = push (DL.getLine (), MIRBuilder, TM);
622+ const size_t ColIdx = push (DL.getLine (), MIRBuilder, TM);
623+ push<DebugLine>({ScopeIdx, LineIdx, LineIdx, ColIdx, ColIdx}, MIRBuilder, TM);
624+ }
625+ }
626+ }
627+ return false ;
628+ }
576629
577630bool SPIRVEmitNonSemanticDI::runOnMachineFunction (MachineFunction &MF) {
578631 bool Res = false ;
@@ -591,6 +644,7 @@ bool SPIRVEmitNonSemanticDI::runOnMachineFunction(MachineFunction &MF) {
591644 return false ;
592645 Res = emitGlobalDI (MF, M);
593646 }
594- // Res |= emitLineDI(MF);
647+ Res |= emitLineDI (MF);
648+ cleanup ();
595649 return Res;
596650}
0 commit comments