Skip to content

Commit 7e710ae

Browse files
LU-JOHNChenyang-L
authored andcommitted
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. Original commit: KhronosGroup/SPIRV-LLVM-Translator@3f6d94a
1 parent fd83926 commit 7e710ae

14 files changed

+363
-18
lines changed

llvm-spirv/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

llvm-spirv/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
}

llvm-spirv/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,

llvm-spirv/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(
@@ -661,8 +701,6 @@ void SPIRVLine::encode(spv_ostream &O) const {
661701

662702
void SPIRVLine::decode(std::istream &I) {
663703
getDecoder(I) >> FileName >> Line >> Column;
664-
std::shared_ptr<const SPIRVLine> L(this);
665-
Module->setCurrentLine(L);
666704
}
667705

668706
void SPIRVLine::validate() const {

llvm-spirv/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 {

llvm-spirv/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");

llvm-spirv/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) ||

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVModule.cpp

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ class SPIRVModuleImpl : public SPIRVModule {
203203
SPIRVWord Column) override;
204204
const std::shared_ptr<const SPIRVLine> &getCurrentLine() const override;
205205
void setCurrentLine(const std::shared_ptr<const SPIRVLine> &Line) override;
206+
void addDebugLine(SPIRVEntry *E, SPIRVType *TheType, SPIRVId FileNameId,
207+
SPIRVWord LineStart, SPIRVWord LineEnd,
208+
SPIRVWord ColumnStart, SPIRVWord ColumnEnd) override;
209+
const std::shared_ptr<const SPIRVExtInst> &
210+
getCurrentDebugLine() const override;
211+
void setCurrentDebugLine(
212+
const std::shared_ptr<const SPIRVExtInst> &DebugLine) override;
206213
void addCapability(SPIRVCapabilityKind) override;
207214
void addCapabilityInternal(SPIRVCapabilityKind) override;
208215
void addExtension(ExtensionID) override;
@@ -309,6 +316,8 @@ class SPIRVModuleImpl : public SPIRVModule {
309316
const std::vector<SPIRVValue *> &,
310317
SPIRVBasicBlock *,
311318
SPIRVInstruction * = nullptr) override;
319+
SPIRVEntry *createDebugInfo(SPIRVWord, SPIRVType *TheType,
320+
const std::vector<SPIRVWord> &) override;
312321
SPIRVEntry *addDebugInfo(SPIRVWord, SPIRVType *TheType,
313322
const std::vector<SPIRVWord> &) override;
314323
SPIRVEntry *addAuxData(SPIRVWord, SPIRVType *TheType,
@@ -524,6 +533,7 @@ class SPIRVModuleImpl : public SPIRVModule {
524533
SPIRVStringVec StringVec;
525534
SPIRVMemberNameVec MemberNameVec;
526535
std::shared_ptr<const SPIRVLine> CurrentLine;
536+
std::shared_ptr<const SPIRVExtInst> CurrentDebugLine;
527537
SPIRVDecorateVec DecorateVec;
528538
SPIRVDecGroupVec DecGroupVec;
529539
SPIRVGroupDecVec GroupDecVec;
@@ -577,6 +587,60 @@ void SPIRVModuleImpl::addLine(SPIRVEntry *E, SPIRVId FileNameId, SPIRVWord Line,
577587
E->setLine(CurrentLine);
578588
}
579589

590+
const std::shared_ptr<const SPIRVExtInst> &
591+
SPIRVModuleImpl::getCurrentDebugLine() const {
592+
return CurrentDebugLine;
593+
}
594+
595+
void SPIRVModuleImpl::setCurrentDebugLine(
596+
const std::shared_ptr<const SPIRVExtInst> &DebugLine) {
597+
CurrentDebugLine = DebugLine;
598+
}
599+
600+
namespace {
601+
bool isDebugLineEqual(const SPIRVExtInst &CurrentDebugLine, SPIRVId FileNameId,
602+
SPIRVId LineStartId, SPIRVId LineEndId,
603+
SPIRVId ColumnStartId, SPIRVId ColumnEndId) {
604+
assert(CurrentDebugLine.getExtOp() == SPIRVDebug::DebugLine);
605+
const std::vector<SPIRVWord> CurrentDebugLineArgs =
606+
CurrentDebugLine.getArguments();
607+
608+
using namespace SPIRVDebug::Operand::DebugLine;
609+
return CurrentDebugLineArgs[SourceIdx] == FileNameId &&
610+
CurrentDebugLineArgs[StartIdx] == LineStartId &&
611+
CurrentDebugLineArgs[EndIdx] == LineEndId &&
612+
CurrentDebugLineArgs[ColumnStartIdx] == ColumnStartId &&
613+
CurrentDebugLineArgs[ColumnEndIdx] == ColumnEndId;
614+
}
615+
} // namespace
616+
617+
void SPIRVModuleImpl::addDebugLine(SPIRVEntry *E, SPIRVType *TheType,
618+
SPIRVId FileNameId, SPIRVWord LineStart,
619+
SPIRVWord LineEnd, SPIRVWord ColumnStart,
620+
SPIRVWord ColumnEnd) {
621+
if (!(CurrentDebugLine &&
622+
isDebugLineEqual(*CurrentDebugLine, FileNameId,
623+
getLiteralAsConstant(LineStart)->getId(),
624+
getLiteralAsConstant(LineEnd)->getId(),
625+
getLiteralAsConstant(ColumnStart)->getId(),
626+
getLiteralAsConstant(ColumnEnd)->getId()))) {
627+
using namespace SPIRVDebug::Operand::DebugLine;
628+
629+
std::vector<SPIRVWord> DebugLineOps(OperandCount);
630+
DebugLineOps[SourceIdx] = FileNameId;
631+
DebugLineOps[StartIdx] = getLiteralAsConstant(LineStart)->getId();
632+
DebugLineOps[EndIdx] = getLiteralAsConstant(LineEnd)->getId();
633+
DebugLineOps[ColumnStartIdx] = getLiteralAsConstant(ColumnStart)->getId();
634+
DebugLineOps[ColumnEndIdx] = getLiteralAsConstant(ColumnEnd)->getId();
635+
636+
CurrentDebugLine.reset(static_cast<SPIRVExtInst *>(
637+
createDebugInfo(SPIRVDebug::DebugLine, TheType, DebugLineOps)));
638+
}
639+
640+
assert(E && "invalid entry");
641+
E->setDebugLine(CurrentDebugLine);
642+
}
643+
580644
SPIRVValue *SPIRVModuleImpl::addSamplerConstant(SPIRVType *TheType,
581645
SPIRVWord AddrMode,
582646
SPIRVWord ParametricMode,
@@ -1316,11 +1380,16 @@ SPIRVInstruction *SPIRVModuleImpl::addExtInst(
13161380
InsertBefore);
13171381
}
13181382

1383+
SPIRVEntry *
1384+
SPIRVModuleImpl::createDebugInfo(SPIRVWord InstId, SPIRVType *TheType,
1385+
const std::vector<SPIRVWord> &Args) {
1386+
return new SPIRVExtInst(this, getId(), TheType, SPIRVEIS_OpenCL_DebugInfo_100,
1387+
ExtInstSetIds[getDebugInfoEIS()], InstId, Args);
1388+
}
1389+
13191390
SPIRVEntry *SPIRVModuleImpl::addDebugInfo(SPIRVWord InstId, SPIRVType *TheType,
13201391
const std::vector<SPIRVWord> &Args) {
1321-
return addEntry(
1322-
new SPIRVExtInst(this, getId(), TheType, SPIRVEIS_OpenCL_DebugInfo_100,
1323-
ExtInstSetIds[getDebugInfoEIS()], InstId, Args));
1392+
return addEntry(createDebugInfo(InstId, TheType, Args));
13241393
}
13251394

13261395
SPIRVEntry *SPIRVModuleImpl::addAuxData(SPIRVWord InstId, SPIRVType *TheType,
@@ -1846,6 +1915,7 @@ spv_ostream &operator<<(spv_ostream &O, SPIRVModule &M) {
18461915
SPIRVModuleImpl &MI = *static_cast<SPIRVModuleImpl *>(&M);
18471916
// Start tracking of the current line with no line
18481917
MI.CurrentLine.reset();
1918+
MI.CurrentDebugLine.reset();
18491919

18501920
SPIRVEncoder Encoder(O);
18511921
Encoder << MagicNumber << MI.SPIRVVersion

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVModule.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ class SPIRVModule {
203203
SPIRVWord Column) = 0;
204204
virtual const std::shared_ptr<const SPIRVLine> &getCurrentLine() const = 0;
205205
virtual void setCurrentLine(const std::shared_ptr<const SPIRVLine> &) = 0;
206+
virtual void addDebugLine(SPIRVEntry *E, SPIRVType *TheType,
207+
SPIRVId FileNameId, SPIRVWord LineStart,
208+
SPIRVWord LineEnd, SPIRVWord ColumnStart,
209+
SPIRVWord ColumnEnd) = 0;
210+
virtual const std::shared_ptr<const SPIRVExtInst> &
211+
getCurrentDebugLine() const = 0;
212+
virtual void
213+
setCurrentDebugLine(const std::shared_ptr<const SPIRVExtInst> &) = 0;
206214
virtual const SPIRVDecorateGeneric *addDecorate(SPIRVDecorateGeneric *) = 0;
207215
virtual SPIRVDecorationGroup *addDecorationGroup() = 0;
208216
virtual SPIRVDecorationGroup *
@@ -310,6 +318,8 @@ class SPIRVModule {
310318
const std::vector<SPIRVValue *> &,
311319
SPIRVBasicBlock *,
312320
SPIRVInstruction * = nullptr) = 0;
321+
virtual SPIRVEntry *createDebugInfo(SPIRVWord, SPIRVType *,
322+
const std::vector<SPIRVWord> &) = 0;
313323
virtual SPIRVEntry *addDebugInfo(SPIRVWord, SPIRVType *,
314324
const std::vector<SPIRVWord> &) = 0;
315325
virtual SPIRVEntry *addAuxData(SPIRVWord, SPIRVType *,

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVStream.cpp

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

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

0 commit comments

Comments
 (0)