Skip to content

Commit 0d72c5b

Browse files
vmaksimosys-ce-bb
authored andcommitted
Translate checksum info inside DebugSource instruction (#1996)
It's done in scope of NonSemantic.Shader.200.DebugInfo spec to have a proper solution for translation of checksum info (instead of the W/A done for OpenCL DebugInfo spec in #936) Original commit: KhronosGroup/SPIRV-LLVM-Translator@31d5db3
1 parent 6cd2668 commit 0d72c5b

File tree

7 files changed

+118
-23
lines changed

7 files changed

+118
-23
lines changed

llvm-spirv/lib/SPIRV/LLVMToSPIRVDbgTran.cpp

+22-4
Original file line numberDiff line numberDiff line change
@@ -1347,11 +1347,24 @@ SPIRVExtInst *LLVMToSPIRVDbgTran::getSource(const T *DIEntry) {
13471347
Ops[FileIdx] = BM->getString(FileName)->getId();
13481348
DIFile *F = DIEntry ? DIEntry->getFile() : nullptr;
13491349

1350-
if (F && F->getRawChecksum() && !isNonSemanticDebugInfo()) {
1350+
if (F && F->getRawChecksum()) {
13511351
auto CheckSum = F->getChecksum().value();
1352-
Ops.push_back(BM->getString("//__" + CheckSum.getKindAsString().str() +
1353-
":" + CheckSum.Value.str())
1354-
->getId());
1352+
1353+
if (!isNonSemanticDebugInfo())
1354+
Ops.push_back(BM->getString("//__" + CheckSum.getKindAsString().str() +
1355+
":" + CheckSum.Value.str())
1356+
->getId());
1357+
else if (BM->getDebugInfoEIS() ==
1358+
SPIRVEIS_NonSemantic_Shader_DebugInfo_200) {
1359+
SPIRVDebug::FileChecksumKind ChecksumKind =
1360+
SPIRV::DbgChecksumKindMap::map(CheckSum.Kind);
1361+
1362+
Ops.push_back(
1363+
BM->addIntegerConstant(static_cast<SPIRVTypeInt *>(getInt32Ty()),
1364+
ChecksumKind)
1365+
->getId());
1366+
Ops.push_back(BM->getString(CheckSum.Value.str())->getId());
1367+
}
13551368
}
13561369

13571370
if (F && F->getRawSource() && isNonSemanticDebugInfo()) {
@@ -1361,6 +1374,11 @@ SPIRVExtInst *LLVMToSPIRVDbgTran::getSource(const T *DIEntry) {
13611374
constexpr size_t MaxStrSize = MaxNumWords * 4 - 1;
13621375
const size_t NumWords = getSizeInWords(Str);
13631376

1377+
if (BM->getDebugInfoEIS() == SPIRVEIS_NonSemantic_Shader_DebugInfo_200 &&
1378+
Ops.size() == MinOperandCount) {
1379+
Ops.push_back(getDebugInfoNoneId());
1380+
Ops.push_back(getDebugInfoNoneId());
1381+
}
13641382
Ops.push_back(BM->getString(Str.substr(0, MaxStrSize))->getId());
13651383
SPIRVExtInst *Source = static_cast<SPIRVExtInst *>(
13661384
BM->addDebugInfo(SPIRVDebug::Source, getVoidTy(), Ops));

llvm-spirv/lib/SPIRV/SPIRVToLLVMDbgTran.cpp

+25-5
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ const std::string &SPIRVToLLVMDbgTran::getString(const SPIRVId Id) {
136136
}
137137

138138
const std::string
139-
SPIRVToLLVMDbgTran::getStringContinued(const SPIRVId Id,
140-
SPIRVExtInst *DebugInst) {
141-
if (getDbgInst<SPIRVDebug::DebugInfoNone>(Id))
139+
SPIRVToLLVMDbgTran::getStringSourceContinued(const SPIRVId Id,
140+
SPIRVExtInst *DebugInst) {
141+
if (!isValidId(Id) || getDbgInst<SPIRVDebug::DebugInfoNone>(Id))
142142
return "";
143143
std::string Str = BM->get<SPIRVString>(Id)->getStr();
144144
using namespace SPIRVDebug::Operand::SourceContinued;
@@ -1488,8 +1488,28 @@ DIFile *SPIRVToLLVMDbgTran::getFile(const SPIRVId SourceId) {
14881488
ParseChecksum(ChecksumStr));
14891489
}
14901490

1491-
return getDIFile(getString(SourceArgs[FileIdx]), std::nullopt,
1492-
getStringContinued(SourceArgs[TextIdx], Source));
1491+
std::optional<DIFile::ChecksumInfo<StringRef>> CS;
1492+
SPIRVWord StrIdx = SourceArgs[TextIdx];
1493+
if (Source->getExtSetKind() == SPIRVEIS_NonSemantic_Shader_DebugInfo_200) {
1494+
if (!getDbgInst<SPIRVDebug::DebugInfoNone>(SourceArgs[ChecksumKind]) &&
1495+
!getDbgInst<SPIRVDebug::DebugInfoNone>(SourceArgs[ChecksumValue])) {
1496+
llvm::DIFile::ChecksumKind Kind = SPIRV::DbgChecksumKindMap::rmap(
1497+
static_cast<SPIRVDebug::FileChecksumKind>(
1498+
BM->get<SPIRVConstant>(SourceArgs[ChecksumKind])
1499+
->getZExtIntValue()));
1500+
StringRef Checksum = getString(SourceArgs[ChecksumValue]);
1501+
size_t ChecksumEndPos = Checksum.find_if_not(llvm::isHexDigit);
1502+
CS.emplace(Kind, Checksum.substr(0, ChecksumEndPos));
1503+
}
1504+
1505+
if (SourceArgs.size() == MaxOperandCount)
1506+
StrIdx = SourceArgs[TextNonSemIdx];
1507+
else
1508+
StrIdx = SPIRVID_INVALID;
1509+
}
1510+
1511+
return getDIFile(getString(SourceArgs[FileIdx]), CS,
1512+
getStringSourceContinued(StrIdx, Source));
14931513
}
14941514

14951515
DIBuilder &SPIRVToLLVMDbgTran::getDIBuilder(const SPIRVExtInst *DebugInst) {

llvm-spirv/lib/SPIRV/SPIRVToLLVMDbgTran.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ class SPIRVToLLVMDbgTran {
196196
return nullptr;
197197
}
198198
const std::string &getString(const SPIRVId Id);
199-
const std::string getStringContinued(const SPIRVId Id,
200-
SPIRVExtInst *DebugInst);
199+
const std::string getStringSourceContinued(const SPIRVId Id,
200+
SPIRVExtInst *DebugInst);
201201
SPIRVWord getConstantValueOrLiteral(const std::vector<SPIRVWord> &,
202202
const SPIRVWord,
203203
const SPIRVExtInstSetKind);

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "spirv/unified1/spirv.hpp"
55
#include "spirv_internal.hpp"
66
#include "llvm/BinaryFormat/Dwarf.h"
7+
#include "llvm/IR/DebugInfoMetadata.h"
78

89
namespace SPIRVDebug {
910

@@ -287,6 +288,12 @@ enum ImportedEntityTag {
287288
ImportedDeclaration = 1,
288289
};
289290

291+
enum FileChecksumKind {
292+
MD5 = 0,
293+
SHA1 = 1,
294+
SHA256 = 2,
295+
};
296+
290297
namespace Operand {
291298

292299
namespace CompilationUnit {
@@ -305,7 +312,12 @@ namespace Source {
305312
enum {
306313
FileIdx = 0,
307314
TextIdx = 1,
308-
MinOperandCount = 1
315+
// For NonSemantic.Shader.DebugInfo.200
316+
ChecksumKind = 1,
317+
ChecksumValue = 2,
318+
TextNonSemIdx = 3,
319+
MinOperandCount = 1,
320+
MaxOperandCount = 4
309321
};
310322
}
311323

@@ -1339,6 +1351,15 @@ inline void DbgImportedEntityMap::init() {
13391351
add(dwarf::DW_TAG_imported_declaration, SPIRVDebug::ImportedDeclaration);
13401352
}
13411353

1354+
typedef SPIRVMap<llvm::DIFile::ChecksumKind, SPIRVDebug::FileChecksumKind>
1355+
DbgChecksumKindMap;
1356+
template <>
1357+
inline void DbgChecksumKindMap::init() {
1358+
add(llvm::DIFile::CSK_MD5, SPIRVDebug::MD5);
1359+
add(llvm::DIFile::CSK_SHA1, SPIRVDebug::SHA1);
1360+
add(llvm::DIFile::CSK_SHA256, SPIRVDebug::SHA256);
1361+
}
1362+
13421363
} // namespace SPIRV
13431364

13441365
#endif // SPIRV_DEBUG_H

llvm-spirv/test/DebugInfo/DebugInfoChecksum.ll

+16-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@
1111
; ./clang -cc1 -debug-info-kind=standalone -S -emit-llvm -triple spir -gcodeview -gcodeview-ghash main.cpp
1212

1313
; RUN: llvm-as %s -o %t.bc
14-
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix CHECK-SPIRV
14+
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix CHECK-SPIRV-OCL
1515
; RUN: llvm-spirv %t.bc -o %t.spv
1616
; RUN: llvm-spirv -r -emit-opaque-pointers %t.spv -o %t.rev.bc
1717
; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
1818
; RUN: FileCheck %s --input-file %t.rev.ll --check-prefix CHECK-LLVM
1919

20+
; RUN: llvm-spirv %t.bc -spirv-text --spirv-debug-info-version=nonsemantic-shader-200 -o - | FileCheck %s --check-prefix CHECK-SPIRV-200
21+
; RUN: llvm-spirv %t.bc --spirv-debug-info-version=nonsemantic-shader-200 -o %t.spv
22+
; RUN: llvm-spirv -r -emit-opaque-pointers %t.spv -o %t.rev.bc
23+
; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
24+
; RUN: FileCheck %s --input-file %t.rev.ll --check-prefix CHECK-LLVM
25+
2026
; ModuleID = 'source.bc'
2127
source_filename = "main.cpp"
2228
target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
@@ -41,9 +47,15 @@ attributes #0 = { noinline norecurse nounwind optnone "correctly-rounded-divide-
4147

4248
; CHECK-LLVM: !DIFile(filename: "main.cpp"
4349
; CHECK-LLVM-SAME: checksumkind: CSK_MD5, checksum: "7bb56387968a9caa6e9e35fff94eaf7b"
44-
; CHECK-SPIRV: String [[#REG:]] "//__CSK_MD5:7bb56387968a9caa6e9e35fff94eaf7b"
45-
; CHECK-SPIRV: DebugSource
46-
; CHECK-SPIRV-SAME: [[#REG]]
50+
51+
; CHECK-SPIRV-OCL: String [[#REG:]] "//__CSK_MD5:7bb56387968a9caa6e9e35fff94eaf7b"
52+
; CHECK-SPIRV-OCL: DebugSource [[#]] [[#REG]]
53+
54+
; 0 means MD5
55+
; CHECK-SPIRV-200: String [[#Val:]] "7bb56387968a9caa6e9e35fff94eaf7b"
56+
; CHECK-SPIRV-200: TypeInt [[#TypeInt32:]] 32
57+
; CHECK-SPIRV-200: Constant [[#TypeInt32]] [[#Kind:]] 0
58+
; CHECK-SPIRV-200: DebugSource [[#]] [[#Kind]] [[#Val]]
4759

4860
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 13.0.0 (https://github.com/llvm/llvm-project.git 7d09e1d7cf27ce781e83f9d388a7a3e1e6487ead)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
4961
!1 = !DIFile(filename: "<stdin>", directory: "oneAPI", checksumkind: CSK_MD5, checksum: "7bb56387968a9caa6e9e35fff94eaf7b")
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
; RUN: llvm-as %s -o %t.bc
2-
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix CHECK-SPIRV
2+
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix CHECK-SPIRV-OCL
33
; RUN: llvm-spirv %t.bc -o %t.spv
44
; RUN: llvm-spirv -r -emit-opaque-pointers %t.spv -o %t.rev.bc
55
; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
66
; RUN: FileCheck %s --input-file %t.rev.ll --check-prefix CHECK-LLVM
77

8+
; RUN: llvm-spirv %t.bc --spirv-debug-info-version=nonsemantic-shader-200 -spirv-text -o - | FileCheck %s --check-prefix CHECK-SPIRV-200
9+
; RUN: llvm-spirv %t.bc --spirv-debug-info-version=nonsemantic-shader-200 -o %t.spv
10+
; RUN: llvm-spirv -r -emit-opaque-pointers %t.spv -o %t.rev.bc
11+
; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
12+
; RUN: FileCheck %s --input-file %t.rev.ll --check-prefixes=CHECK-LLVM,CHECK-LLVM-200
13+
814
; ModuleID = 'array-transform.bc'
915
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024"
1016
target triple = "spir64-unknown-unknown"
@@ -14,11 +20,20 @@ target triple = "spir64-unknown-unknown"
1420

1521
; CHECK-LLVM: !DIFile(filename: "array-transform.cpp"
1622
; CHECK-LLVM-SAME: checksumkind: CSK_MD5, checksum: "7768106c1e51aa084de0ffae6fbe50c4"
17-
; CHECK-SPIRV: String [[#ChecksumInfo:]] "//__CSK_MD5:7768106c1e51aa084de0ffae6fbe50c4"
18-
; CHECK-SPIRV: DebugSource
19-
; CHECK-SPIRV-SAME: [[#ChecksumInfo]]
23+
; CHECK-LLVM-200-SAME: source: "int main() {}"
24+
25+
; CHECK-SPIRV-OCL: String [[#ChecksumInfo:]] "//__CSK_MD5:7768106c1e51aa084de0ffae6fbe50c4"
26+
; CHECK-SPIRV-OCL: DebugSource
27+
; CHECK-SPIRV-OCL-SAME: [[#ChecksumInfo]]
28+
29+
; CHECK-SPIRV-200: String [[#Val:]] "7768106c1e51aa084de0ffae6fbe50c4"
30+
; CHECK-SPIRV-200: String [[#Source:]] "int main() {}"
31+
; CHECK-SPIRV-200: TypeInt [[#TypeInt32:]] 32
32+
; 0 means MD5
33+
; CHECK-SPIRV-200: Constant [[#TypeInt32]] [[#Kind:]] 0
34+
; CHECK-SPIRV-200: DebugSource [[#]] [[#Kind]] [[#Val]] [[#Source]]
2035

2136
!0 = !{i32 2, !"Debug Info Version", i32 3}
2237
!1 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !2, producer: "spirv", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !3, imports: !3)
23-
!2 = !DIFile(filename: "array-transform.cpp", directory: "D:\\path\\to", checksumkind: CSK_MD5, checksum: "7768106c1e51aa084de0ffae6fbe50c4")
38+
!2 = !DIFile(filename: "array-transform.cpp", directory: "D:\\path\\to", checksumkind: CSK_MD5, checksum: "7768106c1e51aa084de0ffae6fbe50c4", source: "int main() {}")
2439
!3 = !{}

llvm-spirv/test/DebugInfo/NonSemantic/DebugSourceContinued.ll

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
; RUN: llvm-as %s -o %t.bc
22
; RUN: llvm-spirv %t.bc --spirv-debug-info-version=nonsemantic-shader-100 -spirv-text -o %t.spt
3-
; RUN: FileCheck %s --input-file %t.spt --check-prefix CHECK-SPIRV
3+
; RUN: FileCheck %s --input-file %t.spt --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-100
44
; RUN: llvm-spirv %t.bc --spirv-debug-info-version=nonsemantic-shader-100 -o %t.spv
55
; RUN: llvm-spirv -r -emit-opaque-pointers %t.spv -o %t.rev.bc
66
; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
77
; RUN: FileCheck %s --input-file %t.rev.ll --check-prefix CHECK-LLVM
88

9+
; RUN: llvm-spirv %t.bc --spirv-debug-info-version=nonsemantic-shader-200 -spirv-text -o %t.spt
10+
; RUN: FileCheck %s --input-file %t.spt --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-200
11+
; RUN: llvm-spirv %t.bc --spirv-debug-info-version=nonsemantic-shader-200 -o %t.spv
12+
; RUN: llvm-spirv -r -emit-opaque-pointers %t.spv -o %t.rev.bc
13+
; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
14+
; RUN: FileCheck %s --input-file %t.rev.ll --check-prefix CHECK-LLVM
15+
916
; CHECK-LLVM: !DIFile(filename: "t.c", directory: "/test", source: "A
1017
; CHECK-LLVM-SAME-COUNT-200000: A
1118
; CHECK-LLVM-SAME: MayThe4thBeWithYou
@@ -19,7 +26,9 @@
1926
; CHECK-SPIRV-SAME-COUNT-262130: A
2027
; CHECK-SPIRV: String [[#Str3:]] "A
2128
; CHECK-SPIRV-SAME-COUNT-5755: A
22-
; CHECK-SPIRV: DebugSource [[#]] [[#Str]]
29+
; CHECK-SPIRV-100: DebugSource [[#]] [[#Str]]
30+
; CHECK-SPIRV-200: [[#NONE:]] [[#]] DebugInfoNone
31+
; CHECK-SPIRV-200: DebugSource [[#]] [[#NONE]] [[#NONE]] [[#Str]]
2332
; CHECK-SPIRV: DebugSourceContinued [[#Str2]]
2433
; CHECK-SPIRV: DebugSourceContinued [[#Str3]]
2534

0 commit comments

Comments
 (0)