|
| 1 | +From cb882c89b54511c7be82c2f56474dc2a989064a9 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Alexey Sotkin < [email protected]> |
| 3 | +Date: Mon, 13 May 2019 10:41:51 +0300 |
| 4 | +Subject: [PATCH] Translation of llvm.dbg.declare in case the local variable is |
| 5 | + optimized out |
| 6 | + |
| 7 | +--- |
| 8 | + lib/SPIRV/LLVMToSPIRVDbgTran.cpp | 8 +++----- |
| 9 | + lib/SPIRV/SPIRVToLLVMDbgTran.cpp | 15 +++++++++++++++ |
| 10 | + lib/SPIRV/libSPIRV/SPIRVEnum.h | 13 +++++++++++++ |
| 11 | + test/DebugInfo/DebugDeclareUnused.cl | 16 ++++++++++++++++ |
| 12 | + 4 files changed, 47 insertions(+), 5 deletions(-) |
| 13 | + create mode 100644 test/DebugInfo/DebugDeclareUnused.cl |
| 14 | + |
| 15 | +diff --git a/lib/SPIRV/LLVMToSPIRVDbgTran.cpp b/lib/SPIRV/LLVMToSPIRVDbgTran.cpp |
| 16 | +index 21981fa..fae147d 100644 |
| 17 | +--- a/lib/SPIRV/LLVMToSPIRVDbgTran.cpp |
| 18 | ++++ b/lib/SPIRV/LLVMToSPIRVDbgTran.cpp |
| 19 | +@@ -84,12 +84,9 @@ void LLVMToSPIRVDbgTran::transDebugMetadata() { |
| 20 | + SPIRVValue * |
| 21 | + LLVMToSPIRVDbgTran::createDebugDeclarePlaceholder(const DbgDeclareInst *DbgDecl, |
| 22 | + SPIRVBasicBlock *BB) { |
| 23 | +- if (!DbgDecl->getAddress()) |
| 24 | +- return nullptr; // The variable is dead. |
| 25 | +- |
| 26 | + DbgDeclareIntrinsics.push_back(DbgDecl); |
| 27 | + using namespace SPIRVDebug::Operand::DebugDeclare; |
| 28 | +- SPIRVWordVec Ops(OperandCount, getDebugInfoNone()->getId()); |
| 29 | ++ SPIRVWordVec Ops(OperandCount, getDebugInfoNoneId()); |
| 30 | + SPIRVId ExtSetId = BM->getExtInstSetId(SPIRVEIS_Debug); |
| 31 | + return BM->addExtInst(getVoidTy(), ExtSetId, SPIRVDebug::Declare, Ops, BB); |
| 32 | + } |
| 33 | +@@ -108,7 +105,8 @@ void LLVMToSPIRVDbgTran::finalizeDebugDeclare(const DbgDeclareInst *DbgDecl) { |
| 34 | + using namespace SPIRVDebug::Operand::DebugDeclare; |
| 35 | + SPIRVWordVec Ops(OperandCount); |
| 36 | + Ops[DebugLocalVarIdx] = transDbgEntry(DbgDecl->getVariable())->getId(); |
| 37 | +- Ops[VariableIdx] = SPIRVWriter->transValue(Alloca, BB)->getId(); |
| 38 | ++ Ops[VariableIdx] = Alloca ? SPIRVWriter->transValue(Alloca, BB)->getId() |
| 39 | ++ : getDebugInfoNoneId(); |
| 40 | + Ops[ExpressionIdx] = transDbgEntry(DbgDecl->getExpression())->getId(); |
| 41 | + DD->setArguments(Ops); |
| 42 | + } |
| 43 | +diff --git a/lib/SPIRV/SPIRVToLLVMDbgTran.cpp b/lib/SPIRV/SPIRVToLLVMDbgTran.cpp |
| 44 | +index 9b8bb74..1b20652 100644 |
| 45 | +--- a/lib/SPIRV/SPIRVToLLVMDbgTran.cpp |
| 46 | ++++ b/lib/SPIRV/SPIRVToLLVMDbgTran.cpp |
| 47 | +@@ -891,6 +891,21 @@ SPIRVToLLVMDbgTran::transDebugIntrinsic(const SPIRVExtInst *DebugInst, |
| 48 | + case SPIRVDebug::Declare: { |
| 49 | + using namespace SPIRVDebug::Operand::DebugDeclare; |
| 50 | + auto LocalVar = GetLocalVar(Ops[DebugLocalVarIdx]); |
| 51 | ++ if (getDbgInst<SPIRVDebug::DebugInfoNone>(Ops[VariableIdx])) { |
| 52 | ++ // If we don't have the variable(e.g. alloca might be promoted by mem2reg) |
| 53 | ++ // we should generate the following IR: |
| 54 | ++ // call void @llvm.dbg.declare(metadata !4, metadata !14, metadata !5) |
| 55 | ++ // !4 = !{} |
| 56 | ++ // DIBuilder::insertDeclare doesn't allow to pass nullptr for the Storage |
| 57 | ++ // parameter. To work around this limitation we create a dummy temp |
| 58 | ++ // alloca, use it to create llvm.dbg.declare, and then remove the alloca. |
| 59 | ++ auto *AI = new AllocaInst(Type::getInt8Ty(M->getContext()), 0, "tmp", BB); |
| 60 | ++ auto *DbgDeclare = Builder.insertDeclare( |
| 61 | ++ AI, LocalVar.first, GetExpression(Ops[ExpressionIdx]), |
| 62 | ++ LocalVar.second, BB); |
| 63 | ++ AI->eraseFromParent(); |
| 64 | ++ return DbgDeclare; |
| 65 | ++ } |
| 66 | + return Builder.insertDeclare(GetValue(Ops[VariableIdx]), LocalVar.first, |
| 67 | + GetExpression(Ops[ExpressionIdx]), |
| 68 | + LocalVar.second, BB); |
| 69 | +diff --git a/lib/SPIRV/libSPIRV/SPIRVEnum.h b/lib/SPIRV/libSPIRV/SPIRVEnum.h |
| 70 | +index 8a8fe2b..3a071e7 100644 |
| 71 | +--- a/lib/SPIRV/libSPIRV/SPIRVEnum.h |
| 72 | ++++ b/lib/SPIRV/libSPIRV/SPIRVEnum.h |
| 73 | +@@ -113,6 +113,19 @@ typedef spv::GroupOperation SPIRVGroupOperationKind; |
| 74 | + typedef spv::Dim SPIRVImageDimKind; |
| 75 | + typedef std::vector<SPIRVCapabilityKind> SPIRVCapVec; |
| 76 | + |
| 77 | ++enum SPIRVExtensionKind { |
| 78 | ++ SPV_INTEL_device_side_avc_motion_estimation, |
| 79 | ++ SPV_KHR_no_integer_wrap_decoration |
| 80 | ++}; |
| 81 | ++ |
| 82 | ++typedef std::set<SPIRVExtensionKind> SPIRVExtSet; |
| 83 | ++ |
| 84 | ++template <> inline void SPIRVMap<SPIRVExtensionKind, std::string>::init() { |
| 85 | ++ add(SPV_INTEL_device_side_avc_motion_estimation, |
| 86 | ++ "SPV_INTEL_device_side_avc_motion_estimation"); |
| 87 | ++ add(SPV_KHR_no_integer_wrap_decoration, "SPV_KHR_no_integer_wrap_decoration"); |
| 88 | ++} |
| 89 | ++ |
| 90 | + template <> inline void SPIRVMap<SPIRVExtInstSetKind, std::string>::init() { |
| 91 | + add(SPIRVEIS_OpenCL, "OpenCL.std"); |
| 92 | + add(SPIRVEIS_Debug, "SPIRV.debug"); |
| 93 | +diff --git a/test/DebugInfo/DebugDeclareUnused.cl b/test/DebugInfo/DebugDeclareUnused.cl |
| 94 | +new file mode 100644 |
| 95 | +index 0000000..f51c790 |
| 96 | +--- /dev/null |
| 97 | ++++ b/test/DebugInfo/DebugDeclareUnused.cl |
| 98 | +@@ -0,0 +1,16 @@ |
| 99 | ++// Check that we can translate llvm.dbg.declare for a local variable which was |
| 100 | ++// deleted by mem2reg pass(enabled by default in llvm-spirv) |
| 101 | ++ |
| 102 | ++// RUN: %clang_cc1 %s -triple spir -disable-llvm-passes -debug-info-kind=standalone -emit-llvm-bc -o - | llvm-spirv -o %t.spv |
| 103 | ++// RUN: llvm-spirv %t.spv -to-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV |
| 104 | ++// RUN: llvm-spirv -r %t.spv -o - | llvm-dis -o - | FileCheck %s --check-prefix=CHECK-LLVM |
| 105 | ++ |
| 106 | ++ |
| 107 | ++void foo() { |
| 108 | ++ int a; |
| 109 | ++} |
| 110 | ++ |
| 111 | ++// CHECK-SPIRV: ExtInst {{[0-9]+}} [[None:[0-9]+]] {{[0-9]+}} DebugInfoNone |
| 112 | ++// CHECK-SPIRV: ExtInst {{[0-9]+}} {{[0-9]+}} {{[0-9]+}} DebugDeclare {{[0-9]+}} [[None]] {{[0-9]+}} |
| 113 | ++// CHECK-LLVM: call void @llvm.dbg.declare(metadata ![[VarAddr:[0-9]+]], metadata !{{[0-9]+}}, metadata !DIExpression()) |
| 114 | ++// CHECK-LLVM: ![[VarAddr]] = !{} |
| 115 | +-- |
| 116 | +2.7.4 |
| 117 | + |
0 commit comments