Skip to content

Commit 2b3a2f3

Browse files
authored
Implement SPV_INTEL_debug_module extension (#1089)
* Implement SPV_INTEL_debug_module extension Spec intel/llvm#3976
1 parent a84f589 commit 2b3a2f3

14 files changed

+308
-6
lines changed

include/LLVMSPIRVExtensions.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ EXT(SPV_INTEL_fpga_dsp_control)
4040
EXT(SPV_INTEL_memory_access_aliasing)
4141
EXT(SPV_INTEL_fpga_invocation_pipelining_attributes)
4242
EXT(SPV_INTEL_token_type)
43+
EXT(SPV_INTEL_debug_module)

lib/SPIRV/LLVMToSPIRVDbgTran.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ SPIRVEntry *LLVMToSPIRVDbgTran::transDbgEntryImpl(const MDNode *MDN) {
343343
case dwarf::DW_TAG_imported_declaration:
344344
return transDbgImportedEntry(cast<DIImportedEntity>(DIEntry));
345345

346+
case dwarf::DW_TAG_module: {
347+
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_debug_module))
348+
return transDbgModule(cast<DIModule>(DIEntry));
349+
return getDebugInfoNone();
350+
}
351+
346352
default:
347353
return getDebugInfoNone();
348354
}
@@ -813,9 +819,10 @@ LLVMToSPIRVDbgTran::transDbgGlobalVariable(const DIGlobalVariable *GV) {
813819
// Parent scope
814820
DIScope *Context = GV->getScope();
815821
SPIRVEntry *Parent = SPIRVCU;
816-
// Global variable may be declared in scope of a namespace or it may be a
817-
// static variable declared in scope of a function
818-
if (Context && (isa<DINamespace>(Context) || isa<DISubprogram>(Context)))
822+
// Global variable may be declared in scope of a namespace or imported module,
823+
// it may also be a static variable declared in scope of a function.
824+
if (Context && (isa<DINamespace>(Context) || isa<DISubprogram>(Context) ||
825+
isa<DIModule>(Context)))
819826
Parent = transDbgEntry(Context);
820827
Ops[ParentIdx] = Parent->getId();
821828

@@ -1036,3 +1043,20 @@ LLVMToSPIRVDbgTran::transDbgImportedEntry(const DIImportedEntity *IE) {
10361043
Ops[ParentIdx] = getScope(IE->getScope())->getId();
10371044
return BM->addDebugInfo(SPIRVDebug::ImportedEntity, getVoidTy(), Ops);
10381045
}
1046+
1047+
SPIRVEntry *LLVMToSPIRVDbgTran::transDbgModule(const DIModule *Module) {
1048+
using namespace SPIRVDebug::Operand::ModuleINTEL;
1049+
SPIRVWordVec Ops(OperandCount);
1050+
Ops[NameIdx] = BM->getString(Module->getName().str())->getId();
1051+
Ops[SourceIdx] = getSource(Module->getFile())->getId();
1052+
Ops[LineIdx] = Module->getLineNo();
1053+
Ops[ParentIdx] = getScope(Module->getScope())->getId();
1054+
Ops[ConfigMacrosIdx] =
1055+
BM->getString(Module->getConfigurationMacros().str())->getId();
1056+
Ops[IncludePathIdx] = BM->getString(Module->getIncludePath().str())->getId();
1057+
Ops[ApiNotesIdx] = BM->getString(Module->getAPINotesFile().str())->getId();
1058+
Ops[IsDeclIdx] = Module->getIsDecl();
1059+
BM->addExtension(ExtensionID::SPV_INTEL_debug_module);
1060+
BM->addCapability(spv::internal::CapabilityDebugInfoModuleINTEL);
1061+
return BM->addDebugInfo(SPIRVDebug::ModuleINTEL, getVoidTy(), Ops);
1062+
}

lib/SPIRV/LLVMToSPIRVDbgTran.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ class LLVMToSPIRVDbgTran {
143143
// Imported declarations and modules
144144
SPIRVEntry *transDbgImportedEntry(const DIImportedEntity *IE);
145145

146+
// A module in programming language. Example - Fortran module, clang module.
147+
SPIRVEntry *transDbgModule(const DIModule *IE);
148+
146149
SPIRVModule *BM;
147150
Module *M;
148151
LLVMToSPIRVBase *SPIRVWriter;

lib/SPIRV/SPIRVToLLVMDbgTran.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,8 @@ DINode *SPIRVToLLVMDbgTran::transImportedEntry(const SPIRVExtInst *DebugInst) {
783783
if (!Entity)
784784
return Builder.createImportedModule(
785785
Scope, static_cast<DIImportedEntity *>(nullptr), File, Line);
786+
if (DIModule *DM = dyn_cast<DIModule>(Entity))
787+
return Builder.createImportedModule(Scope, DM, File, Line);
786788
if (DIImportedEntity *IE = dyn_cast<DIImportedEntity>(Entity))
787789
return Builder.createImportedModule(Scope, IE, File, Line);
788790
if (DINamespace *NS = dyn_cast<DINamespace>(Entity))
@@ -799,6 +801,23 @@ DINode *SPIRVToLLVMDbgTran::transImportedEntry(const SPIRVExtInst *DebugInst) {
799801
llvm_unreachable("Unexpected kind of imported entity!");
800802
}
801803

804+
DINode *SPIRVToLLVMDbgTran::transModule(const SPIRVExtInst *DebugInst) {
805+
using namespace SPIRVDebug::Operand::ModuleINTEL;
806+
const SPIRVWordVec &Ops = DebugInst->getArguments();
807+
assert(Ops.size() >= OperandCount && "Invalid number of operands");
808+
DIScope *Scope = getScope(BM->getEntry(Ops[ParentIdx]));
809+
unsigned Line = Ops[LineIdx];
810+
DIFile *File = getFile(Ops[SourceIdx]);
811+
StringRef Name = getString(Ops[NameIdx]);
812+
StringRef ConfigMacros = getString(Ops[ConfigMacrosIdx]);
813+
StringRef IncludePath = getString(Ops[IncludePathIdx]);
814+
StringRef ApiNotes = getString(Ops[ApiNotesIdx]);
815+
bool IsDecl = Ops[IsDeclIdx];
816+
817+
return Builder.createModule(Scope, Name, ConfigMacros, IncludePath, ApiNotes,
818+
File, Line, IsDecl);
819+
}
820+
802821
MDNode *SPIRVToLLVMDbgTran::transExpression(const SPIRVExtInst *DebugInst) {
803822
const SPIRVWordVec &Args = DebugInst->getArguments();
804823
std::vector<int64_t> Ops;
@@ -895,6 +914,9 @@ MDNode *SPIRVToLLVMDbgTran::transDebugInstImpl(const SPIRVExtInst *DebugInst) {
895914
case SPIRVDebug::ImportedEntity:
896915
return transImportedEntry(DebugInst);
897916

917+
case SPIRVDebug::ModuleINTEL:
918+
return transModule(DebugInst);
919+
898920
case SPIRVDebug::Operation: // To be translated with transExpression
899921
case SPIRVDebug::Source: // To be used by other instructions
900922
return nullptr;

lib/SPIRV/SPIRVToLLVMDbgTran.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ class SPIRVToLLVMDbgTran {
143143

144144
DINode *transImportedEntry(const SPIRVExtInst *DebugInst);
145145

146+
DINode *transModule(const SPIRVExtInst *DebugInst);
147+
146148
MDNode *transExpression(const SPIRVExtInst *DebugInst);
147149

148150
SPIRVModule *BM;

lib/SPIRV/libSPIRV/SPIRV.debug.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ enum Instruction {
4848
MacroUndef = 33,
4949
ImportedEntity = 34,
5050
Source = 35,
51-
InstCount = 36
51+
ModuleINTEL = 36,
52+
InstCount = 37
5253
};
5354

5455
enum Flag {
@@ -770,6 +771,20 @@ enum {
770771
};
771772
}
772773

774+
namespace ModuleINTEL {
775+
enum {
776+
NameIdx = 0,
777+
SourceIdx = 1,
778+
LineIdx = 2,
779+
ParentIdx = 3,
780+
ConfigMacrosIdx = 4,
781+
IncludePathIdx = 5,
782+
ApiNotesIdx = 6,
783+
IsDeclIdx = 7,
784+
OperandCount = 8
785+
};
786+
}
787+
773788
} // namespace Operand
774789
} // namespace SPIRVDebug
775790

lib/SPIRV/libSPIRV/SPIRVExtInst.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ template <> inline void SPIRVMap<SPIRVDebugExtOpKind, std::string>::init() {
253253
add(SPIRVDebug::NoScope, "DebugNoScope");
254254
add(SPIRVDebug::InlinedAt, "DebugInlinedAt");
255255
add(SPIRVDebug::ImportedEntity, "DebugImportedEntity");
256+
add(SPIRVDebug::ModuleINTEL, "DebugModuleINTEL");
256257
add(SPIRVDebug::Expression, "DebugExpression");
257258
add(SPIRVDebug::Operation, "DebugOperation");
258259
}

lib/SPIRV/libSPIRV/SPIRVNameMapEnum.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,8 @@ template <> inline void SPIRVMap<Capability, std::string>::init() {
571571
add(internal::CapabilityFPGAInvocationPipeliningAttributesINTEL,
572572
"FPGAInvocationPipeliningAttributesINTEL");
573573
add(internal::CapabilityTokenTypeINTEL, "TokenTypeINTEL");
574-
575574
add(CapabilityMax, "Max");
575+
add(internal::CapabilityDebugInfoModuleINTEL, "DebugInfoModuleINTEL");
576576
}
577577
SPIRV_DEF_NAMEMAP(Capability, SPIRVCapabilityNameMap)
578578

lib/SPIRV/libSPIRV/spirv_internal.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ enum InternalCapability {
5959
ICapFPGAInvocationPipeliningAttributesINTEL = 5916,
6060
ICapFastCompositeINTEL = 6093,
6161
ICapOptNoneINTEL = 6094,
62-
ICapTokenTypeINTEL = 6112
62+
ICapTokenTypeINTEL = 6112,
63+
ICapDebugInfoModuleINTEL = 6114
6364
};
6465

6566
enum InternalFunctionControlMask { IFunctionControlOptNoneINTELMask = 0x10000 };
@@ -108,6 +109,8 @@ constexpr Capability CapabilityFPGAInvocationPipeliningAttributesINTEL =
108109
static_cast<Capability>(ICapFPGAInvocationPipeliningAttributesINTEL);
109110
constexpr Capability CapabilityTokenTypeINTEL =
110111
static_cast<Capability>(ICapTokenTypeINTEL);
112+
constexpr Capability CapabilityDebugInfoModuleINTEL =
113+
static_cast<Capability>(ICapDebugInfoModuleINTEL);
111114

112115
constexpr FunctionControlMask FunctionControlOptNoneINTELMask =
113116
static_cast<FunctionControlMask>(IFunctionControlOptNoneINTELMask);

test/DebugInfo/DebugInfoNoneEntity.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
; RUN: llvm-spirv %t.bc -o %t.spv
55
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
66

7+
; RUN: llvm-spirv -spirv-ext=+SPV_INTEL_debug_module %t.bc -o %t.spv
8+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
9+
710
source_filename = "llvm-link"
811
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
912
target triple = "spir64"

0 commit comments

Comments
 (0)