Skip to content

Commit

Permalink
Fix nonsemantic debuginfo line attribution for cooperative matrix
Browse files Browse the repository at this point in the history
* Generate debuginfo for coopmat types, treating them as an opaque composite. Restore the debug source location after calling convertGlslangToSpvType, fixes the line info in this unit test

* Add a cooperative matrix test case, based on the shader from https://github.com/jeffbolznv/vk_cooperative_matrix_perf/blob/master/shaders/shmem.comp
  • Loading branch information
jeffbolznv authored Sep 30, 2024
1 parent 46ef757 commit ca04c2a
Show file tree
Hide file tree
Showing 9 changed files with 797 additions and 18 deletions.
13 changes: 10 additions & 3 deletions SPIRV/GlslangToSpv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2858,9 +2858,16 @@ bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TInt
// SPIR-V, for an out parameter
std::vector<spv::Id> temporaryLvalues; // temporaries to pass, as proxies for complexLValues

auto resultType = [&invertedType, &node, this](){ return invertedType != spv::NoType ?
invertedType :
convertGlslangToSpvType(node->getType()); };
auto resultType = [&invertedType, &node, this](){
if (invertedType != spv::NoType) {
return invertedType;
} else {
auto ret = convertGlslangToSpvType(node->getType());
// convertGlslangToSpvType may clobber the debug location, reset it
builder.setDebugSourceLocation(node->getLoc().line, node->getLoc().getFilename());
return ret;
}
};

// try texturing
result = createImageTextureFunctionCall(node);
Expand Down
37 changes: 37 additions & 0 deletions SPIRV/SpvBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,43 @@ Id Builder::makeCooperativeMatrixTypeKHR(Id component, Id scope, Id rows, Id col
constantsTypesGlobals.push_back(std::unique_ptr<Instruction>(type));
module.mapInstruction(type);

if (emitNonSemanticShaderDebugInfo)
{
// Find a name for one of the parameters. It can either come from debuginfo for another
// type, or an OpName from a constant.
auto const findName = [&](Id id) {
Id id2 = debugId[id];
for (auto &t : groupedDebugTypes[NonSemanticShaderDebugInfo100DebugTypeBasic]) {
if (t->getResultId() == id2) {
for (auto &s : strings) {
if (s->getResultId() == t->getIdOperand(2)) {
return s->getNameString();
}
}
}
}
for (auto &t : names) {
if (t->getIdOperand(0) == id) {
return t->getNameString();
}
}
return "unknown";
};
std::string debugName = "coopmat<";
debugName += std::string(findName(component)) + ", ";
if (isConstantScalar(scope)) {
debugName += std::string("gl_Scope") + std::string(spv::ScopeToString((spv::Scope)getConstantScalar(scope))) + ", ";
} else {
debugName += std::string(findName(scope)) + ", ";
}
debugName += std::string(findName(rows)) + ", ";
debugName += std::string(findName(cols)) + ">";
// There's no nonsemantic debug info instruction for cooperative matrix types,
// use opaque composite instead.
auto const debugResultId = makeCompositeDebugType({}, debugName.c_str(), NonSemanticShaderDebugInfo100Structure, true);
debugId[type->getResultId()] = debugResultId;
}

return type->getResultId();
}

Expand Down
1 change: 1 addition & 0 deletions SPIRV/SpvBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#define SpvBuilder_H

#include "Logger.h"
#define SPV_ENABLE_UTILITY_CODE
#include "spirv.hpp"
#include "spvIR.h"
namespace spv {
Expand Down
14 changes: 14 additions & 0 deletions SPIRV/spirv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2772,6 +2772,20 @@ inline void HasResultAndType(Op opcode, bool *hasResult, bool *hasResultType) {
case OpGroupLogicalXorKHR: *hasResult = true; *hasResultType = true; break;
}
}

inline const char* ScopeToString(Scope value) {
switch (value) {
case ScopeCrossDevice: return "CrossDevice";
case ScopeDevice: return "Device";
case ScopeWorkgroup: return "Workgroup";
case ScopeSubgroup: return "Subgroup";
case ScopeInvocation: return "Invocation";
case ScopeQueueFamily: return "QueueFamily";
case ScopeShaderCallKHR: return "ShaderCallKHR";
default: return "Unknown";
}
}

#endif /* SPV_ENABLE_UTILITY_CODE */

// Overload bitwise operators for mask bit combining
Expand Down
9 changes: 9 additions & 0 deletions SPIRV/spvIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ class Instruction {
out.push_back(operands[op]);
}

const char *getNameString() const {
if (opCode == OpString) {
return (const char *)&operands[0];
} else {
assert(opCode == OpName);
return (const char *)&operands[1];
}
}

protected:
Instruction(const Instruction&);
Id resultId;
Expand Down
623 changes: 623 additions & 0 deletions Test/baseResults/spv.debuginfo.coopmatKHR.comp.out

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions Test/baseResults/spv.debuginfo.hlsl.vert.out
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,12 @@ spv.debuginfo.hlsl.vert
202: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 30 203 203 16 16
201: 8(float) Load 128(s)
204: 8(float) Load 176(c)
207: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 30 195 195 16 16
206: 18(fvec3) CompositeConstruct 198 200 97
207: 18(fvec3) CompositeConstruct 201 204 97
208: 18(fvec3) CompositeConstruct 97 97 205
209: 188 CompositeConstruct 206 207 208
210: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 30 195 195 16 16
Store 192(mx) 209
208: 18(fvec3) CompositeConstruct 201 204 97
209: 18(fvec3) CompositeConstruct 97 97 205
210: 188 CompositeConstruct 206 208 209
Store 192(mx) 210
212: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 30 213 213 16 16
211: 126(ptr) AccessChain 77(input) 134 52
214: 8(float) Load 211
Expand All @@ -468,12 +468,12 @@ spv.debuginfo.hlsl.vert
237: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 30 238 238 16 16
236: 8(float) Load 128(s)
239: 8(float) Load 176(c)
241: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 30 230 230 16 16
240: 18(fvec3) CompositeConstruct 233 97 235
241: 18(fvec3) CompositeConstruct 97 205 97
242: 18(fvec3) CompositeConstruct 236 97 239
243: 188 CompositeConstruct 240 241 242
244: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 30 230 230 16 16
Store 227(my) 243
242: 18(fvec3) CompositeConstruct 97 205 97
243: 18(fvec3) CompositeConstruct 236 97 239
244: 188 CompositeConstruct 240 242 243
Store 227(my) 244
246: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 30 247 247 16 16
245: 126(ptr) AccessChain 77(input) 134 21
248: 8(float) Load 245
Expand All @@ -499,12 +499,12 @@ spv.debuginfo.hlsl.vert
273: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 30 274 274 16 16
272: 8(float) Load 128(s)
275: 8(float) Load 176(c)
277: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 30 264 264 16 16
276: 18(fvec3) CompositeConstruct 205 97 97
277: 18(fvec3) CompositeConstruct 97 267 271
278: 18(fvec3) CompositeConstruct 97 272 275
279: 188 CompositeConstruct 276 277 278
280: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 30 264 264 16 16
Store 261(mz) 279
278: 18(fvec3) CompositeConstruct 97 267 271
279: 18(fvec3) CompositeConstruct 97 272 275
280: 188 CompositeConstruct 276 278 279
Store 261(mz) 280
286: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 103(DebugLine) 30 284 284 16 16
285: 4 ExtInst 1(NonSemantic.Shader.DebugInfo.100) 28(DebugDeclare) 282 281(rotMat) 85
287: 188 Load 192(mx)
Expand Down
87 changes: 87 additions & 0 deletions Test/spv.debuginfo.coopmatKHR.comp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#version 450 core
#pragma use_vulkan_memory_model
#extension GL_EXT_scalar_block_layout : enable
#extension GL_KHR_memory_scope_semantics : enable
#extension GL_KHR_cooperative_matrix : enable
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable
#extension GL_EXT_buffer_reference : enable
#extension GL_EXT_control_flow_attributes : enable

layout(constant_id = 0) const uint lM = 1;
layout(constant_id = 1) const uint lN = 1;
layout(constant_id = 2) const uint lK = 1;
layout(constant_id = 3) const uint TILE_M = 1;
layout(constant_id = 4) const uint TILE_N = 1;
layout(constant_id = 5) const uint TILE_K = 1;
layout(constant_id = 6) const uint K = 1;

#define A_BITS 16
#define A_TYPE float16_t
#define C_BITS 16
#define C_TYPE float16_t

buffer Output { C_TYPE x[]; } outputO;

shared uvec4 Ash[128];
shared uvec4 Bsh[128];

const uint C_ROWS = 2;
const uint C_COLS = 2;
coopmat<C_TYPE, gl_ScopeSubgroup, lM, lN, gl_MatrixUseAccumulator> result[C_ROWS][C_COLS];

void main()
{
[[unroll]] for (uint i = 0; i < C_ROWS; ++i) {
[[unroll]] for (uint j = 0; j < C_COLS; ++j) {
result[i][j] = coopmat<C_TYPE, gl_ScopeSubgroup, lM, lN, gl_MatrixUseAccumulator>(0.0);
}
}

for (uint chunkK = 0; chunkK < K; chunkK += TILE_K) {
[[unroll]] for (uint k = 0; k < TILE_K / lK; ++k)
{
coopmat<A_TYPE, gl_ScopeSubgroup, lM, lK, gl_MatrixUseA> matA[C_ROWS];
[[unroll]] for (uint i = 0; i < C_ROWS; ++i) {
coopMatLoad(matA[i], Ash, 0, 0, gl_CooperativeMatrixLayoutRowMajor);
}

coopmat<A_TYPE, gl_ScopeSubgroup, lK, lN, gl_MatrixUseB> matB;
[[unroll]] for (uint j = 0; j < C_COLS; ++j) {
coopMatLoad(matB, Bsh, 0, 0, gl_CooperativeMatrixLayoutRowMajor);

[[unroll]] for (uint i = 0; i < C_ROWS; ++i) {
result[i][j] = coopMatMulAdd(matA[i], matB, result[i][j]);
}
}
}
}

[[unroll]] for (uint i = 0; i < C_ROWS; ++i) {
[[unroll]] for (uint j = 0; j < C_COLS; ++j) {
coopMatStore(result[i][j], outputO.x, 0, 0, gl_CooperativeMatrixLayoutRowMajor);
}
}
}
1 change: 1 addition & 0 deletions gtests/Spv.FromFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,7 @@ INSTANTIATE_TEST_SUITE_P(
"spv.debuginfo.include.glsl.frag",
"spv.debuginfo.multiline.glsl.frag",
"spv.debuginfo.implicit_br.glsl.frag",
"spv.debuginfo.coopmatKHR.comp",
})),
FileNameAsCustomTestSuffix
);
Expand Down

0 comments on commit ca04c2a

Please sign in to comment.