Skip to content

Commit 3f6d94a

Browse files
authored
Implement DebugLine and DebugNoLine (#2012)
Implement DebugLine/DebugNoLine for NonSemantic.Shader.DebugInfo.100 and NonSemantic.Shader.DebugInfo.200. Updated test/DebugInfo/NonSemantic/Shader200/DebugInfoStringType.ll to test these changes.
1 parent 0946e53 commit 3f6d94a

14 files changed

+365
-20
lines changed

lib/SPIRV/LLVMToSPIRVDbgTran.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,16 @@ void LLVMToSPIRVDbgTran::transLocationInfo() {
235235
V = VPrev;
236236
}
237237
}
238-
BM->addLine(V, File ? File->getId() : getDebugInfoNone()->getId(),
239-
LineNo, Col);
238+
if (BM->getDebugInfoEIS() ==
239+
SPIRVEIS_NonSemantic_Shader_DebugInfo_100 ||
240+
BM->getDebugInfoEIS() ==
241+
SPIRVEIS_NonSemantic_Shader_DebugInfo_200)
242+
BM->addDebugLine(V, getVoidTy(),
243+
File ? File->getId() : getDebugInfoNoneId(),
244+
LineNo, LineNo, Col, Col + 1);
245+
else
246+
BM->addLine(V, File ? File->getId() : getDebugInfoNoneId(), LineNo,
247+
Col);
240248
}
241249
} // Instructions
242250
} // Basic Blocks

lib/SPIRV/SPIRVToLLVMDbgTran.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,16 @@ DebugLoc SPIRVToLLVMDbgTran::transDebugScope(const SPIRVInstruction *Inst) {
15111511
unsigned Col = 0;
15121512
MDNode *Scope = nullptr;
15131513
MDNode *InlinedAt = nullptr;
1514-
if (auto L = Inst->getLine()) {
1514+
1515+
// If DebugLine and OpLine are both active give DebugLine priority
1516+
if (auto DL = Inst->getDebugLine()) {
1517+
using namespace SPIRVDebug::Operand::DebugLine;
1518+
SPIRVWordVec DebugLineArgs = DL->getArguments();
1519+
Line =
1520+
getConstantValueOrLiteral(DebugLineArgs, StartIdx, DL->getExtSetKind());
1521+
Col = getConstantValueOrLiteral(DebugLineArgs, ColumnStartIdx,
1522+
DL->getExtSetKind());
1523+
} else if (auto L = Inst->getLine()) {
15151524
Line = L->getLine();
15161525
Col = L->getColumn();
15171526
}

lib/SPIRV/libSPIRV/SPIRV.debug.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ enum Instruction {
5757
InstCount = 37,
5858
FunctionDefinition = 101,
5959
SourceContinued = 102,
60+
DebugLine = 103,
61+
DebugNoLine = 104,
6062
BuildIdentifier = 105,
6163
StoragePath = 106,
6264
EntryPoint = 107,
@@ -627,6 +629,23 @@ enum {
627629
};
628630
}
629631

632+
namespace DebugLine {
633+
enum {
634+
SourceIdx = 0,
635+
StartIdx = 1,
636+
EndIdx = 2,
637+
ColumnStartIdx = 3,
638+
ColumnEndIdx = 4,
639+
OperandCount = 5
640+
};
641+
}
642+
643+
namespace DebugNoLine {
644+
enum {
645+
OperandCount = 0
646+
};
647+
}
648+
630649
namespace EntryPoint {
631650
enum {
632651
EntryPointIdx = 0,

lib/SPIRV/libSPIRV/SPIRVEntry.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,51 @@ void SPIRVEntry::encodeLine(spv_ostream &O) const {
179179
if (!Module)
180180
return;
181181
const std::shared_ptr<const SPIRVLine> &CurrLine = Module->getCurrentLine();
182-
if (Line && ((CurrLine && *Line != *CurrLine) || !CurrLine)) {
182+
if (Line && (!CurrLine || *Line != *CurrLine)) {
183183
O << *Line;
184184
Module->setCurrentLine(Line);
185185
}
186186
if (isEndOfBlock() || OpCode == OpNoLine)
187187
Module->setCurrentLine(nullptr);
188188
}
189189

190+
namespace {
191+
bool isDebugLineEqual(const SPIRVExtInst &DL1, const SPIRVExtInst &DL2) {
192+
std::vector<SPIRVWord> DL1Args = DL1.getArguments();
193+
std::vector<SPIRVWord> DL2Args = DL2.getArguments();
194+
195+
using namespace SPIRVDebug::Operand::DebugLine;
196+
assert(DL1Args.size() == OperandCount && DL2Args.size() == OperandCount &&
197+
"Invalid number of operands");
198+
return DL1Args[SourceIdx] == DL2Args[SourceIdx] &&
199+
DL1Args[StartIdx] == DL2Args[StartIdx] &&
200+
DL1Args[EndIdx] == DL2Args[EndIdx] &&
201+
DL1Args[ColumnStartIdx] == DL2Args[ColumnStartIdx] &&
202+
DL1Args[ColumnEndIdx] == DL2Args[ColumnEndIdx];
203+
}
204+
} // namespace
205+
206+
void SPIRVEntry::encodeDebugLine(spv_ostream &O) const {
207+
if (!Module)
208+
return;
209+
const std::shared_ptr<const SPIRVExtInst> &CurrDebugLine =
210+
Module->getCurrentDebugLine();
211+
if (DebugLine &&
212+
(!CurrDebugLine || !isDebugLineEqual(*DebugLine, *CurrDebugLine))) {
213+
O << *DebugLine;
214+
Module->setCurrentDebugLine(DebugLine);
215+
}
216+
if (isEndOfBlock() ||
217+
isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_100,
218+
SPIRVDebug::DebugNoLine) ||
219+
isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_200,
220+
SPIRVDebug::DebugNoLine))
221+
Module->setCurrentDebugLine(nullptr);
222+
}
223+
190224
void SPIRVEntry::encodeAll(spv_ostream &O) const {
191225
encodeLine(O);
226+
encodeDebugLine(O);
192227
encodeWordCountOpCode(O);
193228
encode(O);
194229
encodeChildren(O);
@@ -305,6 +340,11 @@ void SPIRVEntry::setLine(const std::shared_ptr<const SPIRVLine> &L) {
305340
SPIRVDBG(if (L) spvdbgs() << "[setLine] " << *L << '\n';)
306341
}
307342

343+
void SPIRVEntry::setDebugLine(const std::shared_ptr<const SPIRVExtInst> &DL) {
344+
DebugLine = DL;
345+
SPIRVDBG(if (DL) spvdbgs() << "[setDebugLine] " << *DL << '\n';)
346+
}
347+
308348
void SPIRVEntry::addMemberDecorate(SPIRVMemberDecorate *Dec) {
309349
assert(canHaveMemberDecorates());
310350
MemberDecorates.insert(std::make_pair(
@@ -663,8 +703,6 @@ void SPIRVLine::encode(spv_ostream &O) const {
663703

664704
void SPIRVLine::decode(std::istream &I) {
665705
getDecoder(I) >> FileName >> Line >> Column;
666-
std::shared_ptr<const SPIRVLine> L(this);
667-
Module->setCurrentLine(L);
668706
}
669707

670708
void SPIRVLine::validate() const {

lib/SPIRV/libSPIRV/SPIRVEntry.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ class SPIRVEntry {
289289
return Id;
290290
}
291291
std::shared_ptr<const SPIRVLine> getLine() const { return Line; }
292+
std::shared_ptr<const SPIRVExtInst> getDebugLine() const { return DebugLine; }
292293
SPIRVLinkageTypeKind getLinkageType() const;
293294
Op getOpCode() const { return OpCode; }
294295
SPIRVModule *getModule() const { return Module; }
@@ -361,6 +362,7 @@ class SPIRVEntry {
361362
void setHasNoId() { Attrib |= SPIRVEA_NOID; }
362363
void setId(SPIRVId TheId) { Id = TheId; }
363364
void setLine(const std::shared_ptr<const SPIRVLine> &L);
365+
void setDebugLine(const std::shared_ptr<const SPIRVExtInst> &DL);
364366
void setLinkageType(SPIRVLinkageTypeKind);
365367
void setModule(SPIRVModule *TheModule);
366368
void setName(const std::string &TheName);
@@ -387,6 +389,7 @@ class SPIRVEntry {
387389
friend spv_ostream &operator<<(spv_ostream &O, const SPIRVEntry &E);
388390
friend std::istream &operator>>(std::istream &I, SPIRVEntry &E);
389391
virtual void encodeLine(spv_ostream &O) const;
392+
virtual void encodeDebugLine(spv_ostream &O) const;
390393
virtual void encodeAll(spv_ostream &O) const;
391394
virtual void encodeName(spv_ostream &O) const;
392395
virtual void encodeChildren(spv_ostream &O) const;
@@ -451,6 +454,7 @@ class SPIRVEntry {
451454
DecorateIdMapType DecorateIds;
452455
MemberDecorateMapType MemberDecorates;
453456
std::shared_ptr<const SPIRVLine> Line;
457+
std::shared_ptr<const SPIRVExtInst> DebugLine;
454458
};
455459

456460
class SPIRVEntryNoIdGeneric : public SPIRVEntry {

lib/SPIRV/libSPIRV/SPIRVExtInst.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ template <> inline void SPIRVMap<SPIRVDebugExtOpKind, std::string>::init() {
263263
add(SPIRVDebug::Operation, "DebugOperation");
264264
add(SPIRVDebug::FunctionDefinition, "DebugFunctionDefinition");
265265
add(SPIRVDebug::SourceContinued, "DebugSourceContinued");
266+
add(SPIRVDebug::DebugLine, "DebugLine");
267+
add(SPIRVDebug::DebugNoLine, "DebugNoLine");
266268
add(SPIRVDebug::EntryPoint, "DebugEntryPoint");
267269
add(SPIRVDebug::BuildIdentifier, "DebugBuildIdentifier");
268270
add(SPIRVDebug::StoragePath, "DebugStoragePath");

lib/SPIRV/libSPIRV/SPIRVFunction.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ bool SPIRVFunction::decodeBB(SPIRVDecoder &Decoder) {
143143
SPIRVEntry *Entry = Decoder.getEntry();
144144

145145
if (Decoder.OpCode == OpLine) {
146-
Module->add(Entry);
146+
std::shared_ptr<const SPIRVLine> L(static_cast<SPIRVLine *>(Entry));
147+
Module->setCurrentLine(L);
147148
continue;
148149
}
149150

@@ -159,6 +160,17 @@ bool SPIRVFunction::decodeBB(SPIRVDecoder &Decoder) {
159160
assert(Inst);
160161
if (Inst->getOpCode() == OpUndef) {
161162
Module->add(Inst);
163+
} else if (Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_100,
164+
SPIRVDebug::DebugNoLine) ||
165+
Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_200,
166+
SPIRVDebug::DebugNoLine)) {
167+
continue;
168+
} else if (Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_100,
169+
SPIRVDebug::DebugLine) ||
170+
Inst->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_200,
171+
SPIRVDebug::DebugLine)) {
172+
std::shared_ptr<const SPIRVExtInst> DL(static_cast<SPIRVExtInst *>(Inst));
173+
Module->setCurrentDebugLine(DL);
162174
} else {
163175
if (Inst->isExtInst(SPIRVEIS_Debug, SPIRVDebug::Scope) ||
164176
Inst->isExtInst(SPIRVEIS_OpenCL_DebugInfo_100, SPIRVDebug::Scope) ||

lib/SPIRV/libSPIRV/SPIRVModule.cpp

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ class SPIRVModuleImpl : public SPIRVModule {
189189
SPIRVWord Column) override;
190190
const std::shared_ptr<const SPIRVLine> &getCurrentLine() const override;
191191
void setCurrentLine(const std::shared_ptr<const SPIRVLine> &Line) override;
192+
void addDebugLine(SPIRVEntry *E, SPIRVType *TheType, SPIRVId FileNameId,
193+
SPIRVWord LineStart, SPIRVWord LineEnd,
194+
SPIRVWord ColumnStart, SPIRVWord ColumnEnd) override;
195+
const std::shared_ptr<const SPIRVExtInst> &
196+
getCurrentDebugLine() const override;
197+
void setCurrentDebugLine(
198+
const std::shared_ptr<const SPIRVExtInst> &DebugLine) override;
192199
void addCapability(SPIRVCapabilityKind) override;
193200
void addCapabilityInternal(SPIRVCapabilityKind) override;
194201
void addExtension(ExtensionID) override;
@@ -296,6 +303,8 @@ class SPIRVModuleImpl : public SPIRVModule {
296303
const std::vector<SPIRVValue *> &,
297304
SPIRVBasicBlock *,
298305
SPIRVInstruction * = nullptr) override;
306+
SPIRVEntry *createDebugInfo(SPIRVWord, SPIRVType *TheType,
307+
const std::vector<SPIRVWord> &) override;
299308
SPIRVEntry *addDebugInfo(SPIRVWord, SPIRVType *TheType,
300309
const std::vector<SPIRVWord> &) override;
301310
SPIRVEntry *addAuxData(SPIRVWord, SPIRVType *TheType,
@@ -511,6 +520,7 @@ class SPIRVModuleImpl : public SPIRVModule {
511520
SPIRVStringVec StringVec;
512521
SPIRVMemberNameVec MemberNameVec;
513522
std::shared_ptr<const SPIRVLine> CurrentLine;
523+
std::shared_ptr<const SPIRVExtInst> CurrentDebugLine;
514524
SPIRVDecorateVec DecorateVec;
515525
SPIRVDecGroupVec DecGroupVec;
516526
SPIRVGroupDecVec GroupDecVec;
@@ -564,6 +574,60 @@ void SPIRVModuleImpl::addLine(SPIRVEntry *E, SPIRVId FileNameId, SPIRVWord Line,
564574
E->setLine(CurrentLine);
565575
}
566576

577+
const std::shared_ptr<const SPIRVExtInst> &
578+
SPIRVModuleImpl::getCurrentDebugLine() const {
579+
return CurrentDebugLine;
580+
}
581+
582+
void SPIRVModuleImpl::setCurrentDebugLine(
583+
const std::shared_ptr<const SPIRVExtInst> &DebugLine) {
584+
CurrentDebugLine = DebugLine;
585+
}
586+
587+
namespace {
588+
bool isDebugLineEqual(const SPIRVExtInst &CurrentDebugLine, SPIRVId FileNameId,
589+
SPIRVId LineStartId, SPIRVId LineEndId,
590+
SPIRVId ColumnStartId, SPIRVId ColumnEndId) {
591+
assert(CurrentDebugLine.getExtOp() == SPIRVDebug::DebugLine);
592+
const std::vector<SPIRVWord> CurrentDebugLineArgs =
593+
CurrentDebugLine.getArguments();
594+
595+
using namespace SPIRVDebug::Operand::DebugLine;
596+
return CurrentDebugLineArgs[SourceIdx] == FileNameId &&
597+
CurrentDebugLineArgs[StartIdx] == LineStartId &&
598+
CurrentDebugLineArgs[EndIdx] == LineEndId &&
599+
CurrentDebugLineArgs[ColumnStartIdx] == ColumnStartId &&
600+
CurrentDebugLineArgs[ColumnEndIdx] == ColumnEndId;
601+
}
602+
} // namespace
603+
604+
void SPIRVModuleImpl::addDebugLine(SPIRVEntry *E, SPIRVType *TheType,
605+
SPIRVId FileNameId, SPIRVWord LineStart,
606+
SPIRVWord LineEnd, SPIRVWord ColumnStart,
607+
SPIRVWord ColumnEnd) {
608+
if (!(CurrentDebugLine &&
609+
isDebugLineEqual(*CurrentDebugLine, FileNameId,
610+
getLiteralAsConstant(LineStart)->getId(),
611+
getLiteralAsConstant(LineEnd)->getId(),
612+
getLiteralAsConstant(ColumnStart)->getId(),
613+
getLiteralAsConstant(ColumnEnd)->getId()))) {
614+
using namespace SPIRVDebug::Operand::DebugLine;
615+
616+
std::vector<SPIRVWord> DebugLineOps(OperandCount);
617+
DebugLineOps[SourceIdx] = FileNameId;
618+
DebugLineOps[StartIdx] = getLiteralAsConstant(LineStart)->getId();
619+
DebugLineOps[EndIdx] = getLiteralAsConstant(LineEnd)->getId();
620+
DebugLineOps[ColumnStartIdx] = getLiteralAsConstant(ColumnStart)->getId();
621+
DebugLineOps[ColumnEndIdx] = getLiteralAsConstant(ColumnEnd)->getId();
622+
623+
CurrentDebugLine.reset(static_cast<SPIRVExtInst *>(
624+
createDebugInfo(SPIRVDebug::DebugLine, TheType, DebugLineOps)));
625+
}
626+
627+
assert(E && "invalid entry");
628+
E->setDebugLine(CurrentDebugLine);
629+
}
630+
567631
SPIRVValue *SPIRVModuleImpl::addSamplerConstant(SPIRVType *TheType,
568632
SPIRVWord AddrMode,
569633
SPIRVWord ParametricMode,
@@ -1306,11 +1370,16 @@ SPIRVInstruction *SPIRVModuleImpl::addExtInst(
13061370
InsertBefore);
13071371
}
13081372

1373+
SPIRVEntry *
1374+
SPIRVModuleImpl::createDebugInfo(SPIRVWord InstId, SPIRVType *TheType,
1375+
const std::vector<SPIRVWord> &Args) {
1376+
return new SPIRVExtInst(this, getId(), TheType, SPIRVEIS_OpenCL_DebugInfo_100,
1377+
ExtInstSetIds[getDebugInfoEIS()], InstId, Args);
1378+
}
1379+
13091380
SPIRVEntry *SPIRVModuleImpl::addDebugInfo(SPIRVWord InstId, SPIRVType *TheType,
13101381
const std::vector<SPIRVWord> &Args) {
1311-
return addEntry(
1312-
new SPIRVExtInst(this, getId(), TheType, SPIRVEIS_OpenCL_DebugInfo_100,
1313-
ExtInstSetIds[getDebugInfoEIS()], InstId, Args));
1382+
return addEntry(createDebugInfo(InstId, TheType, Args));
13141383
}
13151384

13161385
SPIRVEntry *SPIRVModuleImpl::addAuxData(SPIRVWord InstId, SPIRVType *TheType,
@@ -1836,6 +1905,7 @@ spv_ostream &operator<<(spv_ostream &O, SPIRVModule &M) {
18361905
SPIRVModuleImpl &MI = *static_cast<SPIRVModuleImpl *>(&M);
18371906
// Start tracking of the current line with no line
18381907
MI.CurrentLine.reset();
1908+
MI.CurrentDebugLine.reset();
18391909

18401910
SPIRVEncoder Encoder(O);
18411911
Encoder << MagicNumber << MI.SPIRVVersion

lib/SPIRV/libSPIRV/SPIRVModule.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ class SPIRVModule {
200200
SPIRVWord Column) = 0;
201201
virtual const std::shared_ptr<const SPIRVLine> &getCurrentLine() const = 0;
202202
virtual void setCurrentLine(const std::shared_ptr<const SPIRVLine> &) = 0;
203+
virtual void addDebugLine(SPIRVEntry *E, SPIRVType *TheType,
204+
SPIRVId FileNameId, SPIRVWord LineStart,
205+
SPIRVWord LineEnd, SPIRVWord ColumnStart,
206+
SPIRVWord ColumnEnd) = 0;
207+
virtual const std::shared_ptr<const SPIRVExtInst> &
208+
getCurrentDebugLine() const = 0;
209+
virtual void
210+
setCurrentDebugLine(const std::shared_ptr<const SPIRVExtInst> &) = 0;
203211
virtual const SPIRVDecorateGeneric *addDecorate(SPIRVDecorateGeneric *) = 0;
204212
virtual SPIRVDecorationGroup *addDecorationGroup() = 0;
205213
virtual SPIRVDecorationGroup *
@@ -309,6 +317,8 @@ class SPIRVModule {
309317
const std::vector<SPIRVValue *> &,
310318
SPIRVBasicBlock *,
311319
SPIRVInstruction * = nullptr) = 0;
320+
virtual SPIRVEntry *createDebugInfo(SPIRVWord, SPIRVType *,
321+
const std::vector<SPIRVWord> &) = 0;
312322
virtual SPIRVEntry *addDebugInfo(SPIRVWord, SPIRVType *,
313323
const std::vector<SPIRVWord> &) = 0;
314324
virtual SPIRVEntry *addAuxData(SPIRVWord, SPIRVType *,

lib/SPIRV/libSPIRV/SPIRVStream.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,21 @@ SPIRVEntry *SPIRVDecoder::getEntry() {
246246
Entry->setWordCount(WordCount);
247247
if (OpCode != OpLine)
248248
Entry->setLine(M.getCurrentLine());
249+
if (!Entry->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_100,
250+
SPIRVDebug::DebugLine) &&
251+
!Entry->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_200,
252+
SPIRVDebug::DebugLine))
253+
Entry->setDebugLine(M.getCurrentDebugLine());
254+
249255
IS >> *Entry;
250256
if (Entry->isEndOfBlock() || OpCode == OpNoLine)
251257
M.setCurrentLine(nullptr);
258+
if (Entry->isEndOfBlock() ||
259+
Entry->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_100,
260+
SPIRVDebug::DebugNoLine) ||
261+
Entry->isExtInst(SPIRVEIS_NonSemantic_Shader_DebugInfo_200,
262+
SPIRVDebug::DebugNoLine))
263+
M.setCurrentDebugLine(nullptr);
252264

253265
if (OpExtension == OpCode) {
254266
auto *OpExt = static_cast<SPIRVExtension *>(Entry);

0 commit comments

Comments
 (0)