[PATCH 52/79] [DebugInfo] Translate checksum info inside DebugSource instruction...
authorViktoria Maximova <viktoria.maksimova@intel.com>
Fri, 19 May 2023 13:16:07 +0000 (06:16 -0700)
committerAndreas Beckmann <anbe@debian.org>
Thu, 8 Feb 2024 21:48:18 +0000 (22:48 +0100)
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)

Gbp-Pq: Name 0052-DebugInfo-Translate-checksum-info-inside-DebugSource.patch

lib/SPIRV/LLVMToSPIRVDbgTran.cpp
lib/SPIRV/SPIRVToLLVMDbgTran.cpp
lib/SPIRV/SPIRVToLLVMDbgTran.h
lib/SPIRV/libSPIRV/SPIRV.debug.h
test/DebugInfo/DebugInfoChecksum.ll
test/DebugInfo/DebugInfoChecksumCompileUnit.ll
test/DebugInfo/NonSemantic/DebugSourceContinued.ll

index 7b4ce6c55acb5184e0aa3b50be366e5348566fc2..753b1a2273bf3e9aed8ce7109fea29d51159de6d 100644 (file)
@@ -1345,11 +1345,24 @@ SPIRVExtInst *LLVMToSPIRVDbgTran::getSource(const T *DIEntry) {
   Ops[FileIdx] = BM->getString(FileName)->getId();
   DIFile *F = DIEntry ? DIEntry->getFile() : nullptr;
 
-  if (F && F->getRawChecksum() && !isNonSemanticDebugInfo()) {
+  if (F && F->getRawChecksum()) {
     auto CheckSum = F->getChecksum().getValue();
-    Ops.push_back(BM->getString("//__" + CheckSum.getKindAsString().str() +
-                                ":" + CheckSum.Value.str())
-                      ->getId());
+
+    if (!isNonSemanticDebugInfo())
+      Ops.push_back(BM->getString("//__" + CheckSum.getKindAsString().str() +
+                                  ":" + CheckSum.Value.str())
+                        ->getId());
+    else if (BM->getDebugInfoEIS() ==
+             SPIRVEIS_NonSemantic_Shader_DebugInfo_200) {
+      SPIRVDebug::FileChecksumKind ChecksumKind =
+          SPIRV::DbgChecksumKindMap::map(CheckSum.Kind);
+
+      Ops.push_back(
+          BM->addIntegerConstant(static_cast<SPIRVTypeInt *>(getInt32Ty()),
+                                 ChecksumKind)
+              ->getId());
+      Ops.push_back(BM->getString(CheckSum.Value.str())->getId());
+    }
   }
 
   if (F && F->getRawSource() && isNonSemanticDebugInfo()) {
@@ -1359,6 +1372,11 @@ SPIRVExtInst *LLVMToSPIRVDbgTran::getSource(const T *DIEntry) {
     constexpr size_t MaxStrSize = MaxNumWords * 4 - 1;
     const size_t NumWords = getSizeInWords(Str);
 
+    if (BM->getDebugInfoEIS() == SPIRVEIS_NonSemantic_Shader_DebugInfo_200 &&
+        Ops.size() == MinOperandCount) {
+      Ops.push_back(getDebugInfoNoneId());
+      Ops.push_back(getDebugInfoNoneId());
+    }
     Ops.push_back(BM->getString(Str.substr(0, MaxStrSize))->getId());
     SPIRVExtInst *Source = static_cast<SPIRVExtInst *>(
         BM->addDebugInfo(SPIRVDebug::Source, getVoidTy(), Ops));
index fcbf47e0a9919eec61c6ca12464aea1ae5bfa612..40ad90d0c4b1637014ac059299a889c879ca008e 100644 (file)
@@ -141,11 +141,11 @@ const std::string &SPIRVToLLVMDbgTran::getString(const SPIRVId Id) {
   return String->getStr();
 }
 
-Optional<std::string>
-SPIRVToLLVMDbgTran::getStringContinued(const SPIRVId Id,
-                                       SPIRVExtInst *DebugInst) {
-  if (getDbgInst<SPIRVDebug::DebugInfoNone>(Id))
-    return std::string();
+const std::string
+SPIRVToLLVMDbgTran::getStringSourceContinued(const SPIRVId Id,
+                                             SPIRVExtInst *DebugInst) {
+  if (!isValidId(Id) || getDbgInst<SPIRVDebug::DebugInfoNone>(Id))
+    return "";
   std::string Str = BM->get<SPIRVString>(Id)->getStr();
   using namespace SPIRVDebug::Operand::SourceContinued;
   for (auto *I : DebugInst->getContinuedInstructions()) {
@@ -1490,8 +1490,28 @@ DIFile *SPIRVToLLVMDbgTran::getFile(const SPIRVId SourceId) {
                      ParseChecksum(ChecksumStr));
   }
 
-  return getDIFile(getString(SourceArgs[FileIdx]), None,
-                   getStringContinued(SourceArgs[TextIdx], Source));
+  Optional<DIFile::ChecksumInfo<StringRef>> CS;
+  SPIRVWord StrIdx = SourceArgs[TextIdx];
+  if (Source->getExtSetKind() == SPIRVEIS_NonSemantic_Shader_DebugInfo_200) {
+    if (!getDbgInst<SPIRVDebug::DebugInfoNone>(SourceArgs[ChecksumKind]) &&
+        !getDbgInst<SPIRVDebug::DebugInfoNone>(SourceArgs[ChecksumValue])) {
+      llvm::DIFile::ChecksumKind Kind = SPIRV::DbgChecksumKindMap::rmap(
+          static_cast<SPIRVDebug::FileChecksumKind>(
+              BM->get<SPIRVConstant>(SourceArgs[ChecksumKind])
+                  ->getZExtIntValue()));
+      StringRef Checksum = getString(SourceArgs[ChecksumValue]);
+      size_t ChecksumEndPos = Checksum.find_if_not(llvm::isHexDigit);
+      CS.emplace(Kind, Checksum.substr(0, ChecksumEndPos));
+    }
+
+    if (SourceArgs.size() == MaxOperandCount)
+      StrIdx = SourceArgs[TextNonSemIdx];
+    else
+      StrIdx = SPIRVID_INVALID;
+  }
+
+  return getDIFile(getString(SourceArgs[FileIdx]), CS,
+                   getStringSourceContinued(StrIdx, Source));
 }
 
 DIBuilder &SPIRVToLLVMDbgTran::getDIBuilder(const SPIRVExtInst *DebugInst) {
index 14acaf48911204ec2037ad2b40c968bdc3989dc2..9e9dbc4c9b930403c023c4fb5bf4b854193ad86b 100644 (file)
@@ -197,8 +197,8 @@ private:
     return nullptr;
   }
   const std::string &getString(const SPIRVId Id);
-  Optional<std::string> getStringContinued(const SPIRVId Id,
-                                           SPIRVExtInst *DebugInst);
+  const std::string getStringSourceContinued(const SPIRVId Id,
+                                             SPIRVExtInst *DebugInst);
   SPIRVWord getConstantValueOrLiteral(const std::vector<SPIRVWord> &,
                                       const SPIRVWord,
                                       const SPIRVExtInstSetKind);
index 1ca7ee072e34037f4937bfaf66e18864bbe0c4fe..2d52fc9aff36418c1cc6bee2273fa94603d4ad20 100644 (file)
@@ -4,6 +4,7 @@
 #include "spirv/unified1/spirv.hpp"
 #include "spirv_internal.hpp"
 #include "llvm/BinaryFormat/Dwarf.h"
+#include "llvm/IR/DebugInfoMetadata.h"
 
 namespace SPIRVDebug {
 
@@ -287,6 +288,12 @@ enum ImportedEntityTag {
   ImportedDeclaration = 1,
 };
 
+enum FileChecksumKind {
+  MD5 = 0,
+  SHA1 = 1,
+  SHA256 = 2,
+};
+
 namespace Operand {
 
 namespace CompilationUnit {
@@ -305,7 +312,12 @@ namespace Source {
 enum {
   FileIdx         = 0,
   TextIdx         = 1,
-  MinOperandCount = 1
+  // For NonSemantic.Shader.DebugInfo.200
+  ChecksumKind    = 1,
+  ChecksumValue   = 2,
+  TextNonSemIdx   = 3,
+  MinOperandCount = 1,
+  MaxOperandCount = 4
 };
 }
 
@@ -1322,6 +1334,15 @@ inline void DbgImportedEntityMap::init() {
   add(dwarf::DW_TAG_imported_declaration, SPIRVDebug::ImportedDeclaration);
 }
 
+typedef SPIRVMap<llvm::DIFile::ChecksumKind, SPIRVDebug::FileChecksumKind>
+  DbgChecksumKindMap;
+template <>
+inline void DbgChecksumKindMap::init() {
+  add(llvm::DIFile::CSK_MD5,    SPIRVDebug::MD5);
+  add(llvm::DIFile::CSK_SHA1,   SPIRVDebug::SHA1);
+  add(llvm::DIFile::CSK_SHA256, SPIRVDebug::SHA256);
+}
+
 } // namespace SPIRV
 
 #endif // SPIRV_DEBUG_H
index df9513696aa08aeea1eab26ea0cddc1d00c581f9..b38d92280c3538341a634aa7ca2ef94a2eeb28bb 100644 (file)
 ; ./clang -cc1 -debug-info-kind=standalone -S -emit-llvm -triple spir -gcodeview -gcodeview-ghash main.cpp
 
 ; RUN: llvm-as %s -o %t.bc
-; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix CHECK-SPIRV
+; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix CHECK-SPIRV-OCL
 ; RUN: llvm-spirv %t.bc -o %t.spv
 ; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
 ; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
 ; RUN: FileCheck %s --input-file %t.rev.ll --check-prefix CHECK-LLVM
 
+; RUN: llvm-spirv %t.bc -spirv-text --spirv-debug-info-version=nonsemantic-shader-200 -o - | FileCheck %s --check-prefix CHECK-SPIRV-200
+; RUN: llvm-spirv %t.bc --spirv-debug-info-version=nonsemantic-shader-200 -o %t.spv
+; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
+; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
+; RUN: FileCheck %s --input-file %t.rev.ll --check-prefix CHECK-LLVM
+
 ; ModuleID = 'source.bc'
 source_filename = "main.cpp"
 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-
 
 ; CHECK-LLVM: !DIFile(filename: "main.cpp"
 ; CHECK-LLVM-SAME: checksumkind: CSK_MD5, checksum: "7bb56387968a9caa6e9e35fff94eaf7b"
-; CHECK-SPIRV: String [[#REG:]] "//__CSK_MD5:7bb56387968a9caa6e9e35fff94eaf7b"
-; CHECK-SPIRV: DebugSource
-; CHECK-SPIRV-SAME: [[#REG]]
+
+; CHECK-SPIRV-OCL: String [[#REG:]] "//__CSK_MD5:7bb56387968a9caa6e9e35fff94eaf7b"
+; CHECK-SPIRV-OCL: DebugSource [[#]] [[#REG]]
+
+; 0 means MD5
+; CHECK-SPIRV-200: String [[#Val:]] "7bb56387968a9caa6e9e35fff94eaf7b"
+; CHECK-SPIRV-200: TypeInt [[#TypeInt32:]] 32
+; CHECK-SPIRV-200: Constant [[#TypeInt32]] [[#Kind:]] 0
+; CHECK-SPIRV-200: DebugSource [[#]] [[#Kind]] [[#Val]]
 
 !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)
 !1 = !DIFile(filename: "<stdin>", directory: "oneAPI", checksumkind: CSK_MD5, checksum: "7bb56387968a9caa6e9e35fff94eaf7b")
@@ -60,4 +72,4 @@ attributes #0 = { noinline norecurse nounwind optnone "correctly-rounded-divide-
 !12 = !DISubroutineType(types: !13)
 !13 = !{!14}
 !14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
-!15 = !DILocation(line: 2, column: 3, scope: !10)
\ No newline at end of file
+!15 = !DILocation(line: 2, column: 3, scope: !10)
index 3028481838f406ad9ef5a9c1e3ef918b86086768..1e42eea97b88acfba4aeb96abf045f10164c3c4b 100644 (file)
@@ -1,10 +1,16 @@
 ; RUN: llvm-as %s -o %t.bc\r
-; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix CHECK-SPIRV\r
+; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix CHECK-SPIRV-OCL\r
 ; RUN: llvm-spirv %t.bc -o %t.spv\r
 ; RUN: llvm-spirv -r %t.spv -o %t.rev.bc\r
 ; RUN: llvm-dis %t.rev.bc -o %t.rev.ll\r
 ; RUN: FileCheck %s --input-file %t.rev.ll --check-prefix CHECK-LLVM\r
 \r
+; RUN: llvm-spirv %t.bc --spirv-debug-info-version=nonsemantic-shader-200 -spirv-text -o - | FileCheck %s --check-prefix CHECK-SPIRV-200\r
+; RUN: llvm-spirv %t.bc --spirv-debug-info-version=nonsemantic-shader-200 -o %t.spv\r
+; RUN: llvm-spirv -r %t.spv -o %t.rev.bc\r
+; RUN: llvm-dis %t.rev.bc -o %t.rev.ll\r
+; RUN: FileCheck %s --input-file %t.rev.ll --check-prefixes=CHECK-LLVM,CHECK-LLVM-200\r
+\r
 ; ModuleID = 'array-transform.bc'\r
 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"\r
 target triple = "spir64-unknown-unknown"\r
@@ -14,11 +20,20 @@ target triple = "spir64-unknown-unknown"
 \r
 ; CHECK-LLVM: !DIFile(filename: "array-transform.cpp"\r
 ; CHECK-LLVM-SAME: checksumkind: CSK_MD5, checksum: "7768106c1e51aa084de0ffae6fbe50c4"\r
-; CHECK-SPIRV: String [[#ChecksumInfo:]] "//__CSK_MD5:7768106c1e51aa084de0ffae6fbe50c4"\r
-; CHECK-SPIRV: DebugSource\r
-; CHECK-SPIRV-SAME: [[#ChecksumInfo]]\r
+; CHECK-LLVM-200-SAME: source: "int main() {}"\r
+\r
+; CHECK-SPIRV-OCL: String [[#ChecksumInfo:]] "//__CSK_MD5:7768106c1e51aa084de0ffae6fbe50c4"\r
+; CHECK-SPIRV-OCL: DebugSource\r
+; CHECK-SPIRV-OCL-SAME: [[#ChecksumInfo]]\r
+\r
+; CHECK-SPIRV-200: String [[#Val:]] "7768106c1e51aa084de0ffae6fbe50c4"\r
+; CHECK-SPIRV-200: String [[#Source:]] "int main() {}"\r
+; CHECK-SPIRV-200: TypeInt [[#TypeInt32:]] 32\r
+; 0 means MD5\r
+; CHECK-SPIRV-200: Constant [[#TypeInt32]] [[#Kind:]] 0\r
+; CHECK-SPIRV-200: DebugSource [[#]] [[#Kind]] [[#Val]] [[#Source]]\r
 \r
 !0 = !{i32 2, !"Debug Info Version", i32 3}\r
 !1 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !2, producer: "spirv", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !3, imports: !3)\r
-!2 = !DIFile(filename: "array-transform.cpp", directory: "D:\\path\\to", checksumkind: CSK_MD5, checksum: "7768106c1e51aa084de0ffae6fbe50c4")\r
+!2 = !DIFile(filename: "array-transform.cpp", directory: "D:\\path\\to", checksumkind: CSK_MD5, checksum: "7768106c1e51aa084de0ffae6fbe50c4", source: "int main() {}")\r
 !3 = !{}\r
index 732b6c0c77091d099d0dec988289c9ea83e1f6ea..5bcef8d92ac7c95129acb4b2b1cff26e06183037 100644 (file)
@@ -1,11 +1,18 @@
 ; RUN: llvm-as %s -o %t.bc
 ; RUN: llvm-spirv %t.bc --spirv-debug-info-version=nonsemantic-shader-100 -spirv-text -o %t.spt
-; RUN: FileCheck %s --input-file %t.spt --check-prefix CHECK-SPIRV
+; RUN: FileCheck %s --input-file %t.spt --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-100
 ; RUN: llvm-spirv %t.bc  --spirv-debug-info-version=nonsemantic-shader-100 -o %t.spv
 ; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
 ; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
 ; RUN: FileCheck %s --input-file %t.rev.ll --check-prefix CHECK-LLVM
 
+; RUN: llvm-spirv %t.bc --spirv-debug-info-version=nonsemantic-shader-200 -spirv-text -o %t.spt
+; RUN: FileCheck %s --input-file %t.spt --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-200
+; RUN: llvm-spirv %t.bc  --spirv-debug-info-version=nonsemantic-shader-200 -o %t.spv
+; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
+; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
+; RUN: FileCheck %s --input-file %t.rev.ll --check-prefix CHECK-LLVM
+
 ; CHECK-LLVM: !DIFile(filename: "t.c", directory: "/test", source: "A
 ; CHECK-LLVM-SAME-COUNT-200000: A
 ; CHECK-LLVM-SAME: MayThe4thBeWithYou
@@ -19,7 +26,9 @@
 ; CHECK-SPIRV-SAME-COUNT-262130: A
 ; CHECK-SPIRV: String [[#Str3:]] "A
 ; CHECK-SPIRV-SAME-COUNT-5755: A
-; CHECK-SPIRV: DebugSource [[#]] [[#Str]]
+; CHECK-SPIRV-100: DebugSource [[#]] [[#Str]]
+; CHECK-SPIRV-200: [[#NONE:]] [[#]] DebugInfoNone
+; CHECK-SPIRV-200: DebugSource [[#]] [[#NONE]] [[#NONE]] [[#Str]]
 ; CHECK-SPIRV: DebugSourceContinued [[#Str2]]
 ; CHECK-SPIRV: DebugSourceContinued [[#Str3]]