Skip to content

Commit e0fbce7

Browse files
[SPIRV] Generate DebugTypeMatrix (microsoft#6757)
When the OpenCL.DebugInfo.100 debug info was implemented, there was no DebugTypeMatrix. Now that NonSemantic.Shader.DebugInfo.100 has been merged, we should use DebugTypeMatrix. This PR corrects that oversight.
1 parent 4a5253d commit e0fbce7

File tree

10 files changed

+108
-8
lines changed

10 files changed

+108
-8
lines changed

tools/clang/include/clang/SPIRV/SpirvContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ class SpirvContext {
210210
SpirvDebugInstruction *elemType,
211211
uint32_t elemCount);
212212

213+
SpirvDebugType *getDebugTypeMatrix(const SpirvType *spirvType,
214+
SpirvDebugInstruction *vectorType,
215+
uint32_t vectorCount);
216+
213217
SpirvDebugType *getDebugTypeFunction(const SpirvType *spirvType,
214218
uint32_t flags, SpirvDebugType *ret,
215219
llvm::ArrayRef<SpirvDebugType *> params);

tools/clang/include/clang/SPIRV/SpirvInstruction.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class SpirvInstruction {
153153
IK_DebugTypeBasic,
154154
IK_DebugTypeArray,
155155
IK_DebugTypeVector,
156+
IK_DebugTypeMatrix,
156157
IK_DebugTypeFunction,
157158
IK_DebugTypeComposite,
158159
IK_DebugTypeMember,
@@ -2879,6 +2880,31 @@ class SpirvDebugTypeVector : public SpirvDebugType {
28792880
uint32_t elementCount;
28802881
};
28812882

2883+
/// Represents matrix debug types
2884+
class SpirvDebugTypeMatrix : public SpirvDebugType {
2885+
public:
2886+
SpirvDebugTypeMatrix(SpirvDebugTypeVector *vectorType, uint32_t vectorCount);
2887+
2888+
DEFINE_RELEASE_MEMORY_FOR_CLASS(SpirvDebugTypeMatrix)
2889+
2890+
static bool classof(const SpirvInstruction *inst) {
2891+
return inst->getKind() == IK_DebugTypeMatrix;
2892+
}
2893+
2894+
bool invokeVisitor(Visitor *v) override;
2895+
2896+
SpirvDebugTypeVector *getVectorType() const { return vectorType; }
2897+
uint32_t getVectorCount() const { return vectorCount; }
2898+
2899+
uint32_t getSizeInBits() const override {
2900+
return vectorCount * vectorType->getSizeInBits();
2901+
}
2902+
2903+
private:
2904+
SpirvDebugTypeVector *vectorType;
2905+
uint32_t vectorCount;
2906+
};
2907+
28822908
/// Represents a function debug type. Includes the function return type and
28832909
/// parameter types.
28842910
class SpirvDebugTypeFunction : public SpirvDebugType {

tools/clang/include/clang/SPIRV/SpirvVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class Visitor {
133133
DEFINE_VISIT_METHOD(SpirvDebugTypeBasic)
134134
DEFINE_VISIT_METHOD(SpirvDebugTypeArray)
135135
DEFINE_VISIT_METHOD(SpirvDebugTypeVector)
136+
DEFINE_VISIT_METHOD(SpirvDebugTypeMatrix)
136137
DEFINE_VISIT_METHOD(SpirvDebugTypeFunction)
137138
DEFINE_VISIT_METHOD(SpirvDebugTypeComposite)
138139
DEFINE_VISIT_METHOD(SpirvDebugTypeMember)

tools/clang/lib/SPIRV/DebugTypeVisitor.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,18 @@ SpirvDebugType *DebugTypeVisitor::lowerToDebugType(const SpirvType *spirvType) {
365365
break;
366366
}
367367
case SpirvType::TK_Matrix: {
368-
// TODO: I temporarily use a DebugTypeArray for a matrix type.
369-
// However, when the debug info extension supports matrix type
370-
// e.g., DebugTypeMatrix, we must replace DebugTypeArray with
371-
// DebugTypeMatrix.
372368
auto *matType = dyn_cast<MatrixType>(spirvType);
373-
SpirvDebugInstruction *elemDebugType =
374-
lowerToDebugType(matType->getElementType());
375-
debugType = spvContext.getDebugTypeArray(
376-
spirvType, elemDebugType, {matType->numRows(), matType->numCols()});
369+
if (spvOptions.debugInfoVulkan) {
370+
SpirvDebugInstruction *vecDebugType =
371+
lowerToDebugType(matType->getVecType());
372+
debugType = spvContext.getDebugTypeMatrix(spirvType, vecDebugType,
373+
matType->numCols());
374+
} else {
375+
SpirvDebugInstruction *elemDebugType =
376+
lowerToDebugType(matType->getElementType());
377+
debugType = spvContext.getDebugTypeArray(
378+
spirvType, elemDebugType, {matType->numRows(), matType->numCols()});
379+
}
377380
break;
378381
}
379382
case SpirvType::TK_Pointer: {

tools/clang/lib/SPIRV/EmitVisitor.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,21 @@ bool EmitVisitor::visit(SpirvDebugTypeVector *inst) {
16621662
return true;
16631663
}
16641664

1665+
bool EmitVisitor::visit(SpirvDebugTypeMatrix *inst) {
1666+
initInstruction(inst);
1667+
curInst.push_back(inst->getResultTypeId());
1668+
curInst.push_back(getOrAssignResultId<SpirvInstruction>(inst));
1669+
curInst.push_back(
1670+
getOrAssignResultId<SpirvInstruction>(inst->getInstructionSet()));
1671+
curInst.push_back(inst->getDebugOpcode());
1672+
curInst.push_back(
1673+
getOrAssignResultId<SpirvInstruction>(inst->getVectorType()));
1674+
curInst.push_back(getLiteralEncodedForDebugInfo(inst->getVectorCount()));
1675+
curInst.push_back(getLiteralEncodedForDebugInfo(1));
1676+
finalizeInstruction(&richDebugInfo);
1677+
return true;
1678+
}
1679+
16651680
bool EmitVisitor::visit(SpirvDebugTypeArray *inst) {
16661681
initInstruction(inst);
16671682
curInst.push_back(inst->getResultTypeId());

tools/clang/lib/SPIRV/EmitVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ class EmitVisitor : public Visitor {
297297
bool visit(SpirvDebugExpression *) override;
298298
bool visit(SpirvDebugTypeBasic *) override;
299299
bool visit(SpirvDebugTypeVector *) override;
300+
bool visit(SpirvDebugTypeMatrix *) override;
300301
bool visit(SpirvDebugTypeArray *) override;
301302
bool visit(SpirvDebugTypeFunction *) override;
302303
bool visit(SpirvDebugTypeComposite *) override;

tools/clang/lib/SPIRV/SortDebugInfoVisitor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ void SortDebugInfoVisitor::whileEachOperandOfDebugInstruction(
100100
if (!visitor(inst->getElementType()))
101101
break;
102102
} break;
103+
case SpirvInstruction::IK_DebugTypeMatrix: {
104+
SpirvDebugTypeMatrix *inst = cast<SpirvDebugTypeMatrix>(di);
105+
assert(inst != nullptr);
106+
visitor(inst->getVectorType());
107+
} break;
103108
case SpirvInstruction::IK_DebugTypeFunction: {
104109
SpirvDebugTypeFunction *inst = dyn_cast<SpirvDebugTypeFunction>(di);
105110
assert(inst != nullptr);

tools/clang/lib/SPIRV/SpirvContext.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,21 @@ SpirvContext::getDebugTypeVector(const SpirvType *spirvType,
451451
return debugType;
452452
}
453453

454+
SpirvDebugType *
455+
SpirvContext::getDebugTypeMatrix(const SpirvType *spirvType,
456+
SpirvDebugInstruction *vectorType,
457+
uint32_t vectorCount) {
458+
// Reuse existing debug type if possible.
459+
if (debugTypes.find(spirvType) != debugTypes.end())
460+
return debugTypes[spirvType];
461+
462+
auto *eTy = dyn_cast<SpirvDebugTypeVector>(vectorType);
463+
assert(eTy && "Element type must be a SpirvDebugTypeVector.");
464+
auto *debugType = new (this) SpirvDebugTypeMatrix(eTy, vectorCount);
465+
debugTypes[spirvType] = debugType;
466+
return debugType;
467+
}
468+
454469
SpirvDebugType *
455470
SpirvContext::getDebugTypeFunction(const SpirvType *spirvType, uint32_t flags,
456471
SpirvDebugType *ret,

tools/clang/lib/SPIRV/SpirvInstruction.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugScope)
101101
DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeBasic)
102102
DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeArray)
103103
DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeVector)
104+
DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeMatrix)
104105
DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeFunction)
105106
DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeComposite)
106107
DEFINE_INVOKE_VISITOR_FOR_CLASS(SpirvDebugTypeMember)
@@ -1102,6 +1103,11 @@ SpirvDebugTypeVector::SpirvDebugTypeVector(SpirvDebugType *elemType,
11021103
: SpirvDebugType(IK_DebugTypeVector, /*opcode*/ 6u), elementType(elemType),
11031104
elementCount(elemCount) {}
11041105

1106+
SpirvDebugTypeMatrix::SpirvDebugTypeMatrix(SpirvDebugTypeVector *vectorType,
1107+
uint32_t vectorCount)
1108+
: SpirvDebugType(IK_DebugTypeMatrix, /*opcode*/ 108u),
1109+
vectorType(vectorType), vectorCount(vectorCount) {}
1110+
11051111
SpirvDebugTypeFunction::SpirvDebugTypeFunction(
11061112
uint32_t flags, SpirvDebugType *ret,
11071113
llvm::ArrayRef<SpirvDebugType *> params)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %dxc -T ps_6_0 -E main -fspv-debug=vulkan -fcgl %s -spirv | FileCheck %s
2+
// RUN: %dxc -T ps_6_2 -E main -fspv-debug=vulkan -fcgl %s -spirv -enable-16bit-types | FileCheck %s --check-prefix=CHECK-HALF
3+
4+
// CHECK: [[float:%[0-9]+]] = OpExtInst %void {{%[0-9]+}} DebugTypeBasic {{%[0-9]+}} %uint_32 %uint_3 %uint_0
5+
// CHECK: {{%[0-9]+}} = OpExtInst %void {{%[0-9]+}} DebugTypeVector {{%[0-9]+}} %uint_4
6+
// CHECK: {{%[0-9]+}} = OpExtInst %void {{%[0-9]+}} DebugTypeMatrix {{%[0-9]+}} %uint_3 %uint_1
7+
// CHECK: [[float:%[0-9]+]] = OpExtInst %void {{%[0-9]+}} DebugTypeBasic {{%[0-9]+}} %uint_64 %uint_3 %uint_0
8+
// CHECK: {{%[0-9]+}} = OpExtInst %void {{%[0-9]+}} DebugTypeVector {{%[0-9]+}} %uint_4
9+
// CHECK: {{%[0-9]+}} = OpExtInst %void {{%[0-9]+}} DebugTypeMatrix {{%[0-9]+}} %uint_3 %uint_1
10+
// CHECK-HALF: [[float:%[0-9]+]] = OpExtInst %void {{%[0-9]+}} DebugTypeBasic {{%[0-9]+}} %uint_16 %uint_3 %uint_0
11+
// CHECK-HALF: {{%[0-9]+}} = OpExtInst %void {{%[0-9]+}} DebugTypeVector {{%[0-9]+}} %uint_4
12+
// CHECK-HALF: {{%[0-9]+}} = OpExtInst %void {{%[0-9]+}} DebugTypeMatrix {{%[0-9]+}} %uint_3 %uint_1
13+
// CHECK-HALF: [[float:%[0-9]+]] = OpExtInst %void {{%[0-9]+}} DebugTypeBasic {{%[0-9]+}} %uint_64 %uint_3 %uint_0
14+
// CHECK-HALF: {{%[0-9]+}} = OpExtInst %void {{%[0-9]+}} DebugTypeVector {{%[0-9]+}} %uint_4
15+
// CHECK-HALF: {{%[0-9]+}} = OpExtInst %void {{%[0-9]+}} DebugTypeMatrix {{%[0-9]+}} %uint_3 %uint_1
16+
// CHECK-HALF: [[float:%[0-9]+]] = OpExtInst %void {{%[0-9]+}} DebugTypeBasic {{%[0-9]+}} %uint_32 %uint_3 %uint_0
17+
// CHECK-HALF: {{%[0-9]+}} = OpExtInst %void {{%[0-9]+}} DebugTypeVector {{%[0-9]+}} %uint_4
18+
// CHECK-HALF: {{%[0-9]+}} = OpExtInst %void {{%[0-9]+}} DebugTypeMatrix {{%[0-9]+}} %uint_3 %uint_1
19+
20+
void main() {
21+
float3x4 mat_float;
22+
double3x4 mat_double;
23+
half3x4 mat_half;
24+
}

0 commit comments

Comments
 (0)