Skip to content

Commit 6af50cf

Browse files
committed
--Added support for the extension SPV_KHR_relaxed_extended_instruction
--Modified the SPIRVEmitNonSemanticDI.cpp file for forward referencing --Modified the test files to reflect the changes made in SPIRVEmitNonSemanticDI.cpp.
1 parent 6a0a8a6 commit 6af50cf

File tree

3 files changed

+154
-31
lines changed

3 files changed

+154
-31
lines changed

llvm/lib/Target/SPIRV/SPIRVEmitNonSemanticDI.cpp

Lines changed: 81 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ struct SPIRVEmitNonSemanticDI : public MachineFunctionPass {
196196
SPIRVCodeGenContext &Ctx,
197197
const DenseMap<StringRef, Register> &MacroDefRegs);
198198

199+
void emitDebugTypeEnum(
200+
const SmallPtrSetImpl<const DICompositeType *> &EnumTypes,
201+
SPIRVCodeGenContext &Ctx);
202+
199203
void emitDebugQualifiedTypes(
200204
const SmallPtrSetImpl<DIDerivedType *> &QualifiedDerivedTypes,
201205
SPIRVCodeGenContext &Ctx);
@@ -465,30 +469,20 @@ bool SPIRVEmitNonSemanticDI::emitGlobalDI(MachineFunction &MF) {
465469
emitDebugMacroDefs(MF, Ctx);
466470
emitDebugBuildIdentifier(BuildIdentifier, Ctx);
467471
emitDebugStoragePath(BuildStoragePath, Ctx);
468-
469-
// *** MODIFIED ORDER *** Emit basic types first
470472
emitDebugBasicTypes(Collector.BasicTypes, Ctx);
471473

472-
// Now emit types that might depend on basic types or each other
473474
emitSubroutineTypes(Collector.SubRoutineTypes, Ctx);
474-
emitSubprograms(Collector.SubPrograms,
475-
Ctx); // Subprograms use SubroutineTypes
475+
emitSubprograms(Collector.SubPrograms, Ctx);
476476
emitLexicalScopes(Collector.LexicalScopes, Ctx);
477-
emitDebugPointerTypes(Collector.PointerDerivedTypes,
478-
Ctx); // Pointers use BaseTypes (Basic/Composite)
479-
emitDebugArrayTypes(Collector.ArrayTypes, Ctx); // Arrays use BaseTypes
480-
emitAllDebugTypeComposites(
481-
Collector.CompositeTypes,
482-
Ctx); // Composites define types used by Pointers/Members
477+
emitDebugPointerTypes(Collector.PointerDerivedTypes, Ctx);
478+
emitDebugArrayTypes(Collector.ArrayTypes, Ctx);
479+
emitAllDebugTypeComposites(Collector.CompositeTypes, Ctx);
480+
emitDebugTypeEnum(Collector.EnumTypes, Ctx);
483481
emitAllTemplateDebugInstructions(Collector.CompositeTypesWithTemplates,
484-
Ctx); // Templates use Composites & Params
485-
emitDebugQualifiedTypes(Collector.QualifiedDerivedTypes,
486-
Ctx); // Qualifiers use BaseTypes
487-
emitDebugTypedefs(Collector.TypedefTypes, Ctx); // Typedefs use BaseTypes
488-
emitDebugTypePtrToMember(Collector.PtrToMemberTypes,
489-
Ctx); // PtrToMember uses BaseTypes
490-
491-
// Emit entities that use types
482+
Ctx);
483+
emitDebugQualifiedTypes(Collector.QualifiedDerivedTypes, Ctx);
484+
emitDebugTypedefs(Collector.TypedefTypes, Ctx);
485+
emitDebugTypePtrToMember(Collector.PtrToMemberTypes, Ctx);
492486
emitAllDebugGlobalVariables(MF, Ctx);
493487
emitDebugImportedEntities(Collector.ImportedEntities, Ctx);
494488
}
@@ -535,7 +529,6 @@ Register SPIRVEmitNonSemanticDI::EmitDIInstruction(
535529
SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100))
536530
.addImm(Inst);
537531
for (auto Reg : Operands) {
538-
llvm::errs() << "Adding operand register: " << Reg << "\n";
539532
MIB.addUse(Reg);
540533
}
541534
MIB.constrainAllUses(*Ctx.TII, *Ctx.TRI, *Ctx.RBI);
@@ -613,6 +606,72 @@ uint32_t SPIRVEmitNonSemanticDI::mapDebugFlags(DINode::DIFlags DFlags) {
613606
return Flags;
614607
}
615608

609+
void SPIRVEmitNonSemanticDI::emitDebugTypeEnum(
610+
const SmallPtrSetImpl<const DICompositeType *> &EnumTypes,
611+
SPIRVCodeGenContext &Ctx) {
612+
for (auto *EnumTy : EnumTypes) {
613+
if (!EnumTy || EnumTy->getTag() != dwarf::DW_TAG_enumeration_type)
614+
continue;
615+
if (Register Existing = Ctx.GR->getDebugValue(EnumTy); Existing.isValid())
616+
continue;
617+
Register NameStr = EmitOpString(EnumTy->getName(), Ctx);
618+
bool UnderlyingTypeIsFwd = false;
619+
Register UnderlyingTypeReg = findBaseTypeRegisterRecursive(
620+
EnumTy->getBaseType(), Ctx, UnderlyingTypeIsFwd);
621+
if (!UnderlyingTypeReg.isValid()) {
622+
UnderlyingTypeReg = EmitDIInstruction(
623+
SPIRV::NonSemanticExtInst::DebugInfoNone, {}, Ctx, false);
624+
UnderlyingTypeIsFwd = false;
625+
}
626+
Register SourceReg =
627+
findRegisterFromMap(EnumTy->getFile(), Ctx.SourceRegPairs);
628+
if (!SourceReg.isValid()) {
629+
SourceReg = EmitDIInstruction(SPIRV::NonSemanticExtInst::DebugInfoNone,
630+
{}, Ctx, false);
631+
}
632+
Register Line = Ctx.GR->buildConstantInt(EnumTy->getLine(), Ctx.MIRBuilder,
633+
Ctx.I32Ty, false);
634+
Register Column =
635+
Ctx.GR->buildConstantInt(1, Ctx.MIRBuilder, Ctx.I32Ty, false);
636+
637+
Register ParentReg = Ctx.GR->getDebugValue(EnumTy->getScope());
638+
if (!ParentReg.isValid()) {
639+
llvm::errs() << "Warning: Could not find Parent scope register for Enum: "
640+
<< EnumTy->getName() << "\n";
641+
ParentReg = Ctx.GR->getDebugValue(EnumTy->getFile());
642+
if (!ParentReg.isValid()) {
643+
ParentReg = EmitDIInstruction(SPIRV::NonSemanticExtInst::DebugInfoNone,
644+
{}, Ctx, false);
645+
}
646+
}
647+
Register Size = Ctx.GR->buildConstantInt(EnumTy->getSizeInBits(),
648+
Ctx.MIRBuilder, Ctx.I32Ty, false);
649+
uint32_t Flags = transDebugFlags(EnumTy);
650+
Register FlagsReg =
651+
Ctx.GR->buildConstantInt(Flags, Ctx.MIRBuilder, Ctx.I32Ty, false);
652+
SmallVector<Register, 16> EnumOperands;
653+
for (Metadata *MD : EnumTy->getElements()) {
654+
if (auto *E = dyn_cast<DIEnumerator>(MD)) {
655+
Register Val = Ctx.GR->buildConstantInt(
656+
E->getValue().getZExtValue(), Ctx.MIRBuilder, Ctx.I32Ty, false);
657+
Register Name = EmitOpString(E->getName(), Ctx);
658+
EnumOperands.push_back(Val);
659+
EnumOperands.push_back(Name);
660+
}
661+
}
662+
SmallVector<Register, 12> Ops = {
663+
NameStr, UnderlyingTypeReg, SourceReg, Line,
664+
Column, ParentReg, Size, FlagsReg};
665+
Ops.append(EnumOperands);
666+
bool HasForwardRef = UnderlyingTypeIsFwd;
667+
Register DefReg = Ctx.GR->getDebugValue(EnumTy);
668+
Register Res = EmitDIInstruction(SPIRV::NonSemanticExtInst::DebugTypeEnum,
669+
Ops, Ctx, HasForwardRef, DefReg);
670+
Ctx.GR->addDebugValue(EnumTy, Res);
671+
Ctx.CompositeTypeRegPairs.emplace_back(EnumTy, Res);
672+
}
673+
}
674+
616675
void SPIRVEmitNonSemanticDI::emitSingleCompilationUnit(
617676
StringRef FilePath, int64_t Language, SPIRVCodeGenContext &Ctx,
618677
Register DebugInfoVersionReg, Register DwarfVersionReg,
@@ -845,10 +904,7 @@ void SPIRVEmitNonSemanticDI::emitDebugPointerTypes(
845904
Ctx.I32Ty, false, false);
846905

847906
const DIType *BaseTy = PointerDerivedType->getBaseType();
848-
llvm::errs() << "Pointer Derived Type BaseTy: "
849-
<< (BaseTy ? BaseTy->getName() : "null") << "\n";
850-
851-
bool HasForwardRef = false;
907+
bool HasForwardRef = false;
852908
Register BaseTypeReg =
853909
findBaseTypeRegisterRecursive(BaseTy, Ctx, HasForwardRef);
854910

@@ -1230,7 +1286,7 @@ void SPIRVEmitNonSemanticDI::emitDebugArrayTypes(
12301286
Register BaseTypeReg =
12311287
findBaseTypeRegisterRecursive(ElementType, Ctx, HasForwardRef);
12321288

1233-
if (!BaseTypeReg.isValid()) { // If still not valid after recursive lookup
1289+
if (!BaseTypeReg.isValid()) {
12341290
llvm::errs()
12351291
<< "Warning: Could not find element type for Array/Vector.\n";
12361292
BaseTypeReg = EmitDIInstruction(SPIRV::NonSemanticExtInst::DebugInfoNone,
@@ -1433,9 +1489,6 @@ void SPIRVEmitNonSemanticDI::emitDebugTypeComposite(
14331489
Ctx.GR->buildConstantInt(Flags, Ctx.MIRBuilder, Ctx.I32Ty, false, false);
14341490

14351491
Register DefReg = Ctx.GR->getDebugValue(CompTy);
1436-
llvm::errs() << "Emitting DebugTypeComposite for: " << CompTy->getName()
1437-
<< ", Found Res: " << DefReg << "\n";
1438-
14391492
SmallVector<Register, 4> MemberRegs;
14401493
bool HasForwardRef = false;
14411494

@@ -1744,8 +1797,6 @@ Register SPIRVEmitNonSemanticDI::findBaseTypeRegisterRecursive(
17441797
IsForwardRef = Ctx.GR->isForwardPlaceholder(Found);
17451798
return Found;
17461799
}
1747-
llvm::errs() << "Creating placeholder for CompositeType: " << Ty->getName()
1748-
<< "\n";
17491800
Register PlaceholderReg = Ctx.MRI.createVirtualRegister(&SPIRV::IDRegClass);
17501801
Ctx.MRI.setType(PlaceholderReg, LLT::scalar(32));
17511802
Ctx.GR->markAsForwardPlaceholder(PlaceholderReg);

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ void SPIRVModuleAnalysis::processOtherInstrs(const Module &M) {
681681
NS::DebugMacroDef,
682682
NS::DebugMacroUndef,
683683
NS::DebugTypePtrToMember,
684-
NS::DebugTypeInheritance};
684+
NS::DebugTypeEnum};
685685
bool IsGlobalDI = false;
686686
for (unsigned Idx = 0; Idx < std::size(GlobalNonSemanticDITy); ++Idx)
687687
IsGlobalDI |= Ins.getImm() == GlobalNonSemanticDITy[Idx];
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
; RUN: llc --verify-machineinstrs --spv-emit-nonsemantic-debug-info --spirv-ext=+SPV_KHR_non_semantic_info --print-after=spirv-nonsemantic-debug-info -O0 -mtriple=spirv64-unknown-unknown %s -o - 2>&1 | FileCheck %s --check-prefix=CHECK-MIR
2+
; RUN: llc --verify-machineinstrs --spv-emit-nonsemantic-debug-info --spirv-ext=+SPV_KHR_non_semantic_info -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s --check-prefix=CHECK-SPIRV
3+
; RUN: llc --verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown --spirv-ext=+SPV_KHR_non_semantic_info %s -o - | FileCheck %s --check-prefix=CHECK-OPTION
4+
; RUN: %if spirv-tools %{ llc --verify-machineinstrs --spv-emit-nonsemantic-debug-info --spirv-ext=+SPV_KHR_non_semantic_info -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
5+
6+
@c = dso_local global i32 1, align 4, !dbg !0
7+
8+
define dso_local noundef i32 @main() !dbg !21 {
9+
entry:
10+
%retval = alloca i32, align 4
11+
store i32 0, ptr %retval, align 4
12+
%0 = load i32, ptr @c, align 4, !dbg !23
13+
ret i32 %0, !dbg !24
14+
}
15+
16+
; CHECK-MIR-DAG: [[TYPE_I32:%[0-9]+:type]] = OpTypeInt 32, 0
17+
; CHECK-MIR-DAG: [[TYPE_VOID:%[0-9]+:type\(s64\)]] = OpTypeVoid
18+
; CHECK-MIR-DAG: [[C_NULL:%[0-9]+:iid]] = OpConstantNull [[TYPE_I32]]
19+
; CHECK-MIR-DAG: [[C_TW:%[0-9]+:iid\(s32\)]] = OpConstantI [[TYPE_I32]], 256
20+
; CHECK-MIR-DAG: [[C_THIRTYTWO:%[0-9]+:iid\(s32\)]] = OpConstantI [[TYPE_I32]], 32
21+
; CHECK-MIR-DAG: [[C_THIRTEEN:%[0-9]+:iid\(s32\)]] = OpConstantI [[TYPE_I32]], 13
22+
; CHECK-MIR-DAG: [[C_ONE:%[0-9]+:iid]] = OpConstantI [[TYPE_I32]], 1
23+
; CHECK-MIR-DAG: [[C_TWO:%[0-9]+:iid\(s32\)]] = OpConstantI [[TYPE_I32]], 2
24+
; CHECK-MIR-DAG: [[C_THREE:%[0-9]+:iid\(s32\)]] = OpConstantI [[TYPE_I32]], 3
25+
; CHECK-MIR-DAG: [[C_FOUR:%[0-9]+:iid\(s32\)]] = OpConstantI [[TYPE_I32]], 4
26+
; CHECK-MIR-DAG: [[C_FIVE:%[0-9]+:iid\(s32\)]] = OpConstantI [[TYPE_I32]], 5
27+
; CHECK-MIR-DAG: [[STR_RED:%[0-9]+:id\(s32\)]] = OpString 6579538
28+
; CHECK-MIR-DAG: [[STR_GREEN:%[0-9]+:id\(s32\)]] = OpString 1701147207, 110
29+
; CHECK-MIR-DAG: [[STR_BLUE:%[0-9]+:id\(s32\)]] = OpString 1702194242, 0
30+
; CHECK-MIR-DAG: [[DBG_SOURCE:%[0-9]+:id\(s32\)]] = OpExtInst [[TYPE_VOID]], 3, 35
31+
; CHECK-MIR-DAG: [[DBG_CU:%[0-9]+:id\(s32\)]] = OpExtInst [[TYPE_VOID]], 3, 1, [[C_THREE]], [[C_FIVE]], [[DBG_SOURCE]], [[C_THIRTEEN]]
32+
; CHECK-MIR-DAG: [[DBG_ENUM:%[0-9]+:id\(s32\)]] = OpExtInst [[TYPE_VOID]], 3, 9, {{%[0-9]+\:[a-z0-9\(\)]+}}, {{%[0-9]+\:[a-z0-9\(\)]+}}, [[DBG_SOURCE]], [[C_ONE]], [[C_ONE]], [[DBG_CU]], [[C_THIRTYTWO]], [[C_THREE]], [[C_ONE]], [[STR_RED]], [[C_TWO]], [[STR_GREEN]], [[C_FOUR]], [[STR_BLUE]]
33+
34+
; CHECK-SPIRV-DAG: %[[DBG_SRC:[0-9]+]] = OpExtInst %[[VOID:[0-9]+]] {{%[0-9]+}} DebugSource
35+
; CHECK-SPIRV-DAG: %[[DBG_CU:[0-9]+]] = OpExtInst %[[VOID]] {{%[0-9]+}} DebugCompilationUnit {{%[0-9]+}} {{%[0-9]+}} %[[DBG_SRC]] {{%[0-9]+}}
36+
; CHECK-SPIRV-DAG: %[[STR_INT:[0-9]+]] = OpString "int"
37+
; CHECK-SPIRV-DAG: %[[DBG_TY_INT:[0-9]+]] = OpExtInst %[[VOID]] {{%[0-9]+}} DebugTypeBasic %[[STR_INT]] {{%[0-9]+}} {{%[0-9]+}} {{%[0-9]+}}
38+
; CHECK-SPIRV-DAG: %[[STR_COLOR:[0-9]+]] = OpString "Color"
39+
; CHECK-SPIRV-DAG: %[[STR_RED:[0-9]+]] = OpString "Red"
40+
; CHECK-SPIRV-DAG: %[[STR_GREEN:[0-9]+]] = OpString "Green"
41+
; CHECK-SPIRV-DAG: %[[STR_BLUE:[0-9]+]] = OpString "Blue"
42+
; CHECK-SPIRV-DAG: %[[DBG_ENUM_COLOR:[0-9]+]] = OpExtInst %[[VOID]] {{%[0-9]+}} DebugTypeEnum %[[STR_COLOR]] %[[DBG_TY_INT]] %[[DBG_SRC]] {{%[0-9]+}} {{%[0-9]+}} %[[DBG_CU]] {{%[0-9]+}} {{%[0-9]+}} {{%[0-9]+}} %[[STR_RED]] {{%[0-9]+}} %[[STR_GREEN]] {{%[0-9]+}} %[[STR_BLUE]]
43+
44+
; CHECK-OPTION-NOT: DebugTypeEnum
45+
46+
!llvm.dbg.cu = !{!2}
47+
!llvm.module.flags = !{!13, !14, !15, !16, !17, !18, !19}
48+
49+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
50+
!1 = distinct !DIGlobalVariable(name: "c", scope: !2, file: !3, line: 7, type: !5, isLocal: false, isDefinition: true)
51+
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, producer: "clang version XX.X.XXXX (FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, retainedTypes: !11, globals: !12, splitDebugInlining: false, nameTableKind: None)
52+
!3 = !DIFile(filename: "example.cpp", directory: "/AAAAAAAAAA/BBBBBBBB/CCCCCCCCC", checksumkind: CSK_MD5, checksum: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF")
53+
!4 = !{!5}
54+
!5 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "Color", file: !3, line: 1, baseType: !6, size: 32, elements: !7, identifier: "_ZTS5Color", flags: DIFlagPublic)
55+
!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed, flags: DIFlagPublic)
56+
!7 = !{!8, !9, !10}
57+
!8 = !DIEnumerator(name: "Red", value: 1)
58+
!9 = !DIEnumerator(name: "Green", value: 2)
59+
!10 = !DIEnumerator(name: "Blue", value: -4)
60+
!11 = !{!6}
61+
!12 = !{!0}
62+
!13 = !{i32 7, !"Dwarf Version", i32 5}
63+
!14 = !{i32 2, !"Debug Info Version", i32 3}
64+
!15 = !{i32 1, !"wchar_size", i32 4}
65+
!16 = !{i32 8, !"PIC Level", i32 2}
66+
!17 = !{i32 7, !"PIE Level", i32 2}
67+
!18 = !{i32 7, !"uwtable", i32 2}
68+
!19 = !{i32 7, !"frame-pointer", i32 2}
69+
!21 = distinct !DISubprogram(name: "main", scope: !3, file: !3, line: 9, type: !22, scopeLine: 9, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !2)
70+
!22 = !DISubroutineType(types: !11, flags: DIFlagPublic)
71+
!23 = !DILocation(line: 10, column: 29, scope: !21)
72+
!24 = !DILocation(line: 10, column: 5, scope: !21)

0 commit comments

Comments
 (0)