Skip to content

Commit 201285f

Browse files
MrSidimsjsji
authored andcommitted
Fix parent scope index for ImportedEntity (#2095)
Should be more cautious with inline namespaces Signed-off-by: Sidorov, Dmitry <[email protected]> Original commit: KhronosGroup/SPIRV-LLVM-Translator@668a97d
1 parent d2c280e commit 201285f

File tree

4 files changed

+80
-29
lines changed

4 files changed

+80
-29
lines changed

llvm-spirv/lib/SPIRV/LLVMToSPIRVDbgTran.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,20 +1611,19 @@ LLVMToSPIRVDbgTran::transDbgImportedEntry(const DIImportedEntity *IE) {
16111611
auto Tag = static_cast<dwarf::Tag>(IE->getTag());
16121612
// FIXME: 'OpenCL/bugged' version is kept because it's hard to remove it
16131613
// It's W/A for missing 2nd index in OpenCL's implementation
1614-
const SPIRVWord OffsetIdx =
1615-
isNonSemanticDebugInfo() ? OperandCount - NonSemantic::OperandCount : 0;
1616-
SPIRVWordVec Ops(OperandCount - OffsetIdx);
1617-
Ops[NameIdx] = BM->getString(IE->getName().str())->getId();
1618-
Ops[TagIdx] = SPIRV::DbgImportedEntityMap::map(Tag);
1619-
Ops[SourceIdx - OffsetIdx] = getSource(IE->getFile())->getId();
1620-
Ops[EntityIdx - OffsetIdx] = transDbgEntry(IE->getEntity())->getId();
1621-
Ops[LineIdx - OffsetIdx] = IE->getLine();
1614+
const SPIRVWord OffsetIdx = static_cast<int>(isNonSemanticDebugInfo());
1615+
SPIRVWordVec Ops(OpenCL::OperandCount - OffsetIdx);
1616+
Ops[OpenCL::NameIdx] = BM->getString(IE->getName().str())->getId();
1617+
Ops[OpenCL::TagIdx] = SPIRV::DbgImportedEntityMap::map(Tag);
1618+
Ops[OpenCL::SourceIdx - OffsetIdx] = getSource(IE->getFile())->getId();
1619+
Ops[OpenCL::EntityIdx - OffsetIdx] = transDbgEntry(IE->getEntity())->getId();
1620+
Ops[OpenCL::LineIdx - OffsetIdx] = IE->getLine();
16221621
// This version of DIImportedEntity has no column number
1623-
Ops[ColumnIdx - OffsetIdx] = 0;
1624-
Ops[ParentIdx - OffsetIdx] = getScope(IE->getScope())->getId();
1622+
Ops[OpenCL::ColumnIdx - OffsetIdx] = 0;
1623+
Ops[OpenCL::ParentIdx - OffsetIdx] = getScope(IE->getScope())->getId();
16251624
if (isNonSemanticDebugInfo())
1626-
transformToConstant(Ops,
1627-
{TagIdx, LineIdx - OffsetIdx, ColumnIdx - OffsetIdx});
1625+
transformToConstant(Ops, {OpenCL::TagIdx, OpenCL::LineIdx - OffsetIdx,
1626+
OpenCL::ColumnIdx - OffsetIdx});
16281627
return BM->addDebugInfo(SPIRVDebug::ImportedEntity, getVoidTy(), Ops);
16291628
}
16301629

llvm-spirv/lib/SPIRV/SPIRVToLLVMDbgTran.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,20 +1265,19 @@ DINode *SPIRVToLLVMDbgTran::transImportedEntry(const SPIRVExtInst *DebugInst) {
12651265
const SPIRVWordVec &Ops = DebugInst->getArguments();
12661266
// FIXME: 'OpenCL/bugged' version is kept because it's hard to remove it
12671267
// It's W/A for missing 2nd index in OpenCL's implementation
1268-
const SPIRVWord OffsetIdx = isNonSemanticDebugInfo(DebugInst->getExtSetKind())
1269-
? OperandCount - NonSemantic::OperandCount
1270-
: 0;
1268+
const SPIRVWord OffsetIdx =
1269+
static_cast<int>(isNonSemanticDebugInfo(DebugInst->getExtSetKind()));
12711270

1272-
assert(Ops.size() == (OperandCount - OffsetIdx) &&
1271+
assert(Ops.size() == (OpenCL::OperandCount - OffsetIdx) &&
12731272
"Invalid number of operands");
1274-
DIScope *Scope = getScope(BM->getEntry(Ops[ParentIdx - OffsetIdx]));
1275-
SPIRVWord Line = getConstantValueOrLiteral(Ops, LineIdx - OffsetIdx,
1273+
DIScope *Scope = getScope(BM->getEntry(Ops[OpenCL::ParentIdx - OffsetIdx]));
1274+
SPIRVWord Line = getConstantValueOrLiteral(Ops, OpenCL::LineIdx - OffsetIdx,
12761275
DebugInst->getExtSetKind());
1277-
DIFile *File = getFile(Ops[SourceIdx - OffsetIdx]);
1278-
auto *Entity =
1279-
transDebugInst<DINode>(BM->get<SPIRVExtInst>(Ops[EntityIdx - OffsetIdx]));
1280-
SPIRVWord Tag =
1281-
getConstantValueOrLiteral(Ops, TagIdx, DebugInst->getExtSetKind());
1276+
DIFile *File = getFile(Ops[OpenCL::SourceIdx - OffsetIdx]);
1277+
auto *Entity = transDebugInst<DINode>(
1278+
BM->get<SPIRVExtInst>(Ops[OpenCL::EntityIdx - OffsetIdx]));
1279+
SPIRVWord Tag = getConstantValueOrLiteral(Ops, OpenCL::TagIdx,
1280+
DebugInst->getExtSetKind());
12821281
if (Tag == SPIRVDebug::ImportedModule) {
12831282
if (!Entity)
12841283
return getDIBuilder(DebugInst).createImportedModule(
@@ -1294,7 +1293,7 @@ DINode *SPIRVToLLVMDbgTran::transImportedEntry(const SPIRVExtInst *DebugInst) {
12941293
Line);
12951294
}
12961295
if (Tag == SPIRVDebug::ImportedDeclaration) {
1297-
StringRef Name = getString(Ops[NameIdx]);
1296+
StringRef Name = getString(Ops[OpenCL::NameIdx]);
12981297
if (DIGlobalVariableExpression *GVE =
12991298
dyn_cast<DIGlobalVariableExpression>(Entity))
13001299
return getDIBuilder(DebugInst).createImportedDeclaration(

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ static std::map<ExpressionOpCode, unsigned> OpCountMap {
933933
}
934934

935935
namespace ImportedEntity {
936-
inline namespace OpenCL {
936+
namespace OpenCL {
937937
// it's bugged version, note 2nd index is missing
938938
// FIXME: need to remove it after some graceful period
939939
enum {
@@ -977,9 +977,9 @@ enum {
977977

978978
// helper function to get parent scope of debug instruction, to be used
979979
// to determine with which compile unit the particular instruction relates
980-
inline bool hasDbgInstParentScopeIdx(const uint32_t Kind,
981-
uint32_t &ParentScopeIdx,
982-
const SPIRV::SPIRVExtInstSetKind ExtKind = SPIRV::SPIRVEIS_OpenCL) {
980+
inline bool hasDbgInstParentScopeIdx(
981+
const uint32_t Kind, uint32_t &ParentScopeIdx,
982+
const SPIRV::SPIRVExtInstSetKind ExtKind = SPIRV::SPIRVEIS_OpenCL) {
983983
switch (Kind) {
984984
case SPIRVDebug::Typedef:
985985
ParentScopeIdx = Typedef::ParentIdx;
@@ -1021,7 +1021,10 @@ inline bool hasDbgInstParentScopeIdx(const uint32_t Kind,
10211021
ParentScopeIdx = LocalVariable::ParentIdx;
10221022
return true;
10231023
case SPIRVDebug::ImportedEntity:
1024-
ParentScopeIdx = ImportedEntity::ParentIdx;
1024+
if (ExtKind == SPIRV::SPIRVEIS_OpenCL_DebugInfo_100)
1025+
ParentScopeIdx = ImportedEntity::OpenCL::ParentIdx;
1026+
else
1027+
ParentScopeIdx = ImportedEntity::NonSemantic::ParentIdx;
10251028
return true;
10261029
case SPIRVDebug::ModuleINTEL:
10271030
ParentScopeIdx = ModuleINTEL::ParentIdx;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
; ModuleID = '/Volumes/Data/apple-internal/llvm/tools/clang/test/Modules/debug-info-moduleimport.m'
2+
; RUN: llvm-as < %s -o %t.bc
3+
4+
; RUN: llvm-spirv --spirv-debug-info-version=nonsemantic-shader-100 %t.bc -spirv-text -o %t.spt
5+
; RUN: FileCheck %s --input-file %t.spt --check-prefix CHECK-SPIRV
6+
; RUN: llvm-spirv --spirv-debug-info-version=nonsemantic-shader-100 %t.bc -o %t.spv
7+
; RUN: llvm-spirv -r %t.spv -o - | llvm-dis -o %t.ll
8+
; RUN: FileCheck %s --input-file %t.ll --check-prefix CHECK-LLVM
9+
10+
; RUN: llvm-spirv --spirv-debug-info-version=nonsemantic-shader-200 %t.bc -spirv-text -o %t.spt
11+
; RUN: FileCheck %s --input-file %t.spt --check-prefix CHECK-SPIRV
12+
; RUN: llvm-spirv --spirv-debug-info-version=nonsemantic-shader-200 %t.bc -o %t.spv
13+
; RUN: llvm-spirv -r %t.spv -o - | llvm-dis -o %t.ll
14+
; RUN: FileCheck %s --input-file %t.ll --check-prefix CHECK-LLVM
15+
16+
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
17+
target triple = "spir64-unknown-unknown"
18+
19+
; CHECK-SPIRV-DAG: ExtInstImport [[#EISId:]] "NonSemantic.Shader.DebugInfo
20+
; CHECK-SPIRV-DAG: String [[#Name:]] ""
21+
; CHECK-SPIRV-DAG: TypeInt [[#Int:]] 32 0
22+
; CHECK-SPIRV-DAG: Constant [[#Int]] [[#One:]] 1
23+
; CHECK-SPIRV-DAG: Constant [[#Int]] [[#Zero:]] 0
24+
; CHECK-SPIRV-DAG: Constant [[#Int]] [[#Five:]] 5
25+
; CHECK-SPIRV: ExtInst [[#]] [[#Source:]] [[#]] DebugSource
26+
; CHECK-SPIRV: ExtInst [[#]] [[#CU:]] [[#]] DebugCompilationUnit
27+
; CHECK-SPIRV: ExtInst [[#]] [[#Typedef:]] [[#]] DebugTypedef
28+
; CHECK-SPIRV: ExtInst [[#]] [[#]] [[#EISId]] DebugImportedEntity [[#Name]] [[#One]] [[#Source]] [[#Typedef]] [[#Five]] [[#Zero]] [[#CU]] {{$}}
29+
30+
; CHECK-LLVM: ![[#CU:]] = distinct !DICompileUnit(language: DW_LANG_OpenCL, file: ![[#File:]],{{.*}}isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, imports: ![[#Import:]])
31+
; CHECK-LLVM: ![[#File]] = !DIFile(filename: "<stdin>", directory: "/llvm/tools/clang/test/Modules")
32+
; CHECK-LLVM: ![[#Import]] = !{![[#Entity:]]}
33+
; CHECK-LLVM: ![[#Entity]] = !DIImportedEntity(tag: DW_TAG_imported_declaration, scope: ![[#CU]], entity: ![[#Typedef:]], file: ![[#File]], line: 5)
34+
; CHECK-LLVM: ![[#Typedef]] = !DIDerivedType(tag: DW_TAG_typedef, name: "max_align_t", file: ![[#File]], baseType: ![[#]])
35+
; CHECK-LLVM: !DICompositeType(tag: DW_TAG_structure_type, file: ![[#File]], line: 5, size: 256, flags: DIFlagFwdDecl, elements: ![[#]], identifier: "_ZTS11max_align_t")
36+
37+
!llvm.dbg.cu = !{!0}
38+
!llvm.module.flags = !{!6, !7}
39+
!llvm.ident = !{!8}
40+
41+
!0 = distinct !DICompileUnit(language: DW_LANG_OpenCL, file: !1, producer: "LLVM version 3.7.0", isOptimized: false, runtimeVersion: 2, emissionKind: FullDebug, enums: !2, retainedTypes: !2, globals: !2, imports: !3, sysroot: "/")
42+
!1 = !DIFile(filename: "/llvm/tools/clang/test/Modules/<stdin>", directory: "/")
43+
!2 = !{}
44+
!3 = !{!4}
45+
!4 = !DIImportedEntity(tag: DW_TAG_imported_declaration, scope: !0, entity: !9, file: !1, line: 5)
46+
!6 = !{i32 2, !"Dwarf Version", i32 4}
47+
!7 = !{i32 2, !"Debug Info Version", i32 3}
48+
!8 = !{!"LLVM version 3.7.0"}
49+
!9 = !DIDerivedType(tag: DW_TAG_typedef, name: "max_align_t", file: !1, baseType: !10)
50+
!10 = !DICompositeType(tag: DW_TAG_structure_type, file: !1, line: 5, size: 256, flags: DIFlagFwdDecl, elements: !2, identifier: "_ZTS11max_align_t")

0 commit comments

Comments
 (0)