Skip to content

Commit 43a52bb

Browse files
bwlodarczsys-ce-bb
authored andcommitted
Support for record debug format (#2565)
Recently, LLVM migrated to new debug format replacing debug intrinsics with so-called records. The DIBuilder api supports both formats now with grace deprecation period for intrinsics. Currently Translator during reverse translation have new format disabled by flag and still supports only old one. Documentation for the migration: https://llvm.org/docs/RemoveDIsDebugInfo.html Original commit: KhronosGroup/SPIRV-LLVM-Translator@50c6c3e6b99462a
1 parent 8685a5c commit 43a52bb

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "llvm/BinaryFormat/Dwarf.h"
6262
#include "llvm/IR/AttributeMask.h"
6363
#include "llvm/IR/Constants.h"
64+
#include "llvm/IR/DebugProgramInstruction.h"
6465
#include "llvm/IR/DerivedTypes.h"
6566
#include "llvm/IR/Dominators.h"
6667
#include "llvm/IR/IRBuilder.h"
@@ -2439,7 +2440,18 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
24392440
case SPIRVEIS_OpenCL_DebugInfo_100:
24402441
case SPIRVEIS_NonSemantic_Shader_DebugInfo_100:
24412442
case SPIRVEIS_NonSemantic_Shader_DebugInfo_200:
2442-
return mapValue(BV, DbgTran->transDebugIntrinsic(ExtInst, BB));
2443+
if (!M->IsNewDbgInfoFormat) {
2444+
return mapValue(
2445+
BV, DbgTran->transDebugIntrinsic(ExtInst, BB).get<Instruction *>());
2446+
} else {
2447+
auto MaybeRecord = DbgTran->transDebugIntrinsic(ExtInst, BB);
2448+
if (!MaybeRecord.isNull()) {
2449+
auto *Record = MaybeRecord.get<DbgRecord *>();
2450+
Record->setDebugLoc(
2451+
DbgTran->transDebugScope(static_cast<SPIRVInstruction *>(BV)));
2452+
}
2453+
return mapValue(BV, nullptr);
2454+
}
24432455
default:
24442456
llvm_unreachable("Unknown extended instruction set!");
24452457
}
@@ -5054,9 +5066,6 @@ llvm::convertSpirvToLLVM(LLVMContext &C, SPIRVModule &BM,
50545066
const SPIRV::TranslatorOpts &Opts,
50555067
std::string &ErrMsg) {
50565068
std::unique_ptr<Module> M(new Module("", C));
5057-
// TODO: Migrate to the new debug record format. Until then, keep using the
5058-
// old format.
5059-
M->setNewDbgInfoFormatFlag(false);
50605069

50615070
SPIRVToLLVM BTL(M.get(), &BM);
50625071

llvm-spirv/lib/SPIRV/SPIRVToLLVMDbgTran.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
#include "llvm/ADT/StringExtras.h"
4949
#include "llvm/IR/DIBuilder.h"
50+
#include "llvm/IR/DebugProgramInstruction.h"
5051
#include "llvm/IR/IntrinsicInst.h"
5152
#include "llvm/IR/Module.h"
5253

@@ -1547,7 +1548,7 @@ MDNode *SPIRVToLLVMDbgTran::transDebugInstImpl(const SPIRVExtInst *DebugInst) {
15471548
}
15481549
}
15491550

1550-
Instruction *
1551+
DbgInstPtr
15511552
SPIRVToLLVMDbgTran::transDebugIntrinsic(const SPIRVExtInst *DebugInst,
15521553
BasicBlock *BB) {
15531554
auto GetLocalVar = [&](SPIRVId Id) -> std::pair<DILocalVariable *, DebugLoc> {
@@ -1572,6 +1573,7 @@ SPIRVToLLVMDbgTran::transDebugIntrinsic(const SPIRVExtInst *DebugInst,
15721573
case SPIRVDebug::Declare: {
15731574
using namespace SPIRVDebug::Operand::DebugDeclare;
15741575
auto LocalVar = GetLocalVar(Ops[DebugLocalVarIdx]);
1576+
DIBuilder &DIB = getDIBuilder(DebugInst);
15751577
if (getDbgInst<SPIRVDebug::DebugInfoNone>(Ops[VariableIdx])) {
15761578
// If we don't have the variable(e.g. alloca might be promoted by mem2reg)
15771579
// we should generate the following IR:
@@ -1581,16 +1583,15 @@ SPIRVToLLVMDbgTran::transDebugIntrinsic(const SPIRVExtInst *DebugInst,
15811583
// parameter. To work around this limitation we create a dummy temp
15821584
// alloca, use it to create llvm.dbg.declare, and then remove the alloca.
15831585
auto *AI = new AllocaInst(Type::getInt8Ty(M->getContext()), 0, "tmp", BB);
1584-
DbgInstPtr DbgDeclare = getDIBuilder(DebugInst).insertDeclare(
1586+
DbgInstPtr DbgDeclare = DIB.insertDeclare(
15851587
AI, LocalVar.first, GetExpression(Ops[ExpressionIdx]),
15861588
LocalVar.second, BB);
15871589
AI->eraseFromParent();
1588-
return DbgDeclare.get<Instruction *>();
1590+
return DbgDeclare;
15891591
}
1590-
return getDIBuilder(DebugInst)
1591-
.insertDeclare(GetValue(Ops[VariableIdx]), LocalVar.first,
1592-
GetExpression(Ops[ExpressionIdx]), LocalVar.second, BB)
1593-
.get<Instruction *>();
1592+
return DIB.insertDeclare(GetValue(Ops[VariableIdx]), LocalVar.first,
1593+
GetExpression(Ops[ExpressionIdx]), LocalVar.second,
1594+
BB);
15941595
}
15951596
case SPIRVDebug::Value: {
15961597
using namespace SPIRVDebug::Operand::DebugValue;
@@ -1606,10 +1607,15 @@ SPIRVToLLVMDbgTran::transDebugIntrinsic(const SPIRVExtInst *DebugInst,
16061607
}
16071608
if (!MDs.empty()) {
16081609
DIArgList *AL = DIArgList::get(M->getContext(), MDs);
1609-
cast<DbgVariableIntrinsic>(DbgValIntr.get<Instruction *>())
1610-
->setRawLocation(AL);
1610+
if (M->IsNewDbgInfoFormat) {
1611+
cast<DbgVariableRecord>(DbgValIntr.get<DbgRecord *>())
1612+
->setRawLocation(AL);
1613+
} else {
1614+
cast<DbgVariableIntrinsic>(DbgValIntr.get<Instruction *>())
1615+
->setRawLocation(AL);
1616+
}
16111617
}
1612-
return DbgValIntr.get<Instruction *>();
1618+
return DbgValIntr;
16131619
}
16141620
default:
16151621
llvm_unreachable("Unknown debug intrinsic!");

llvm-spirv/lib/SPIRV/SPIRVToLLVMDbgTran.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ class SPIRVToLLVMDbgTran {
8484
return static_cast<T *>(Res);
8585
}
8686

87-
Instruction *transDebugIntrinsic(const SPIRVExtInst *DebugInst,
88-
BasicBlock *BB);
87+
DbgInstPtr transDebugIntrinsic(const SPIRVExtInst *DebugInst, BasicBlock *BB);
8988
void finalize();
89+
llvm::DebugLoc transDebugScope(const SPIRVInstruction *Inst);
9090

9191
private:
9292
DIFile *getFile(const SPIRVId SourceId);
@@ -104,8 +104,6 @@ class SPIRVToLLVMDbgTran {
104104

105105
llvm::DebugLoc transDebugLocation(const SPIRVExtInst *DebugInst);
106106

107-
llvm::DebugLoc transDebugScope(const SPIRVInstruction *Inst);
108-
109107
MDNode *transDebugInlined(const SPIRVExtInst *Inst);
110108
MDNode *transDebugInlinedNonSemanticShader200(const SPIRVExtInst *Inst);
111109

0 commit comments

Comments
 (0)