-
Notifications
You must be signed in to change notification settings - Fork 970
Expand file tree
/
Copy pathSpvDebugInfoTest.cpp
More file actions
368 lines (345 loc) · 15.6 KB
/
SpvDebugInfoTest.cpp
File metadata and controls
368 lines (345 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright (C) 2026 NVIDIA Corporation.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// Tests for NonSemantic.Shader.DebugInfo (NSDI) features.
#include "TestFixture.h"
#include "glslang/Public/ResourceLimits.h"
#include <gtest/gtest.h>
#include <sstream>
#include <string>
namespace glslangtest {
namespace {
class SpvDebugInfoTest : public ::testing::Test {
protected:
// Compile a GLSL compute shader with NonSemantic debug info enabled, targeting
// Vulkan 1.1 / SPIR-V 1.3, and return the disassembly.
std::string compileWithDebugInfo(const std::string& source)
{
glslang::TShader shader(EShLangCompute);
const char* str = source.c_str();
shader.setStrings(&str, 1);
shader.setEnvInput(glslang::EShSourceGlsl, EShLangCompute,
glslang::EShClientVulkan, 110);
shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_1);
shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_3);
EShMessages messages = static_cast<EShMessages>(EShMsgDefault | EShMsgDebugInfo);
if (!shader.parse(GetDefaultResources(), 450, false, messages))
return "COMPILATION_FAILED: " + std::string(shader.getInfoLog());
glslang::TProgram program;
program.addShader(&shader);
if (!program.link(messages))
return "LINKING_FAILED: " + std::string(program.getInfoLog());
glslang::SpvOptions opts;
opts.generateDebugInfo = true;
opts.emitNonSemanticShaderDebugInfo = true;
opts.disableOptimizer = true;
std::vector<uint32_t> spirv;
glslang::GlslangToSpv(*program.getIntermediate(EShLangCompute), spirv, &opts);
std::ostringstream out;
spv::Disassemble(out, spirv);
return out.str();
}
};
// DebugTypeVectorIdEXT (opcode 109) must be emitted for OpTypeCooperativeVectorNV
// when NonSemantic debug info is enabled.
TEST_F(SpvDebugInfoTest, CooperativeVectorNVEmitsDebugTypeVectorIdEXT)
{
const std::string source = R"(
#version 450 core
#extension GL_KHR_memory_scope_semantics : enable
#extension GL_NV_cooperative_vector : enable
#extension GL_EXT_shader_explicit_arithmetic_types : enable
layout(local_size_x = 64) in;
layout(set = 0, binding = 0) buffer B { float data[]; } buf;
void main() {
coopvecNV<float, 8> v = coopvecNV<float, 8>(0.0);
v = v + v;
buf.data[gl_GlobalInvocationID.x] = v[0];
}
)";
std::string spirv = compileWithDebugInfo(source);
EXPECT_NE(spirv.find("DebugTypeVectorIdEXT"), std::string::npos)
<< "Expected DebugTypeVectorIdEXT in disassembly.\nSPIR-V:\n" << spirv;
EXPECT_NE(spirv.find("TypeCooperativeVectorNV"), std::string::npos)
<< "Expected TypeCooperativeVectorNV in disassembly.\nSPIR-V:\n" << spirv;
}
// DebugTypeVectorIdEXT must also be emitted when the component count is a
// specialization constant (OpSpecConstant), since the spec allows any constant
// instruction for the ComponentCount operand.
TEST_F(SpvDebugInfoTest, CooperativeVectorNVSpecConstCountEmitsDebugTypeVectorIdEXT)
{
const std::string source = R"(
#version 450 core
#extension GL_KHR_memory_scope_semantics : enable
#extension GL_NV_cooperative_vector : enable
#extension GL_EXT_shader_explicit_arithmetic_types : enable
layout(local_size_x = 64) in;
layout(constant_id = 0) const uint N = 16;
layout(set = 0, binding = 0) buffer B { float data[]; } buf;
void main() {
coopvecNV<float, N> v = coopvecNV<float, N>(1.0);
buf.data[gl_GlobalInvocationID.x] = v[0];
}
)";
std::string spirv = compileWithDebugInfo(source);
EXPECT_NE(spirv.find("DebugTypeVectorIdEXT"), std::string::npos)
<< "Expected DebugTypeVectorIdEXT with spec-constant count.\nSPIR-V:\n" << spirv;
}
// DebugTypeCooperativeMatrixKHR (opcode 110) must replace the former
// opaque-composite workaround for OpTypeCooperativeMatrixKHR.
TEST_F(SpvDebugInfoTest, CooperativeMatrixKHREmitsDebugTypeCooperativeMatrixKHR)
{
const std::string source = R"(
#version 450 core
#pragma use_vulkan_memory_model
#extension GL_KHR_memory_scope_semantics : enable
#extension GL_KHR_cooperative_matrix : enable
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
layout(local_size_x = 32) in;
layout(set = 0, binding = 0) buffer B { float16_t data[]; } buf;
void main() {
coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator> m =
coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator>(float16_t(0.0));
coopMatStore(m, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor);
}
)";
std::string spirv = compileWithDebugInfo(source);
EXPECT_NE(spirv.find("DebugTypeCooperativeMatrixKHR"), std::string::npos)
<< "Expected DebugTypeCooperativeMatrixKHR in disassembly.\nSPIR-V:\n" << spirv;
// The old opaque-composite workaround must not appear.
EXPECT_EQ(spirv.find("coopmat<"), std::string::npos)
<< "Opaque-composite workaround should not appear in disassembly.\nSPIR-V:\n" << spirv;
}
// DebugTypeCooperativeMatrixKHR must be emitted when Rows and Columns are
// specialization constants (OpSpecConstant), since the spec allows any constant
// instruction for those operands.
TEST_F(SpvDebugInfoTest, CooperativeMatrixKHRSpecConstDimsEmitsDebugType)
{
const std::string source = R"(
#version 450 core
#pragma use_vulkan_memory_model
#extension GL_KHR_memory_scope_semantics : enable
#extension GL_KHR_cooperative_matrix : enable
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
layout(local_size_x = 32) in;
layout(constant_id = 0) const uint M = 16;
layout(constant_id = 1) const uint N = 16;
layout(set = 0, binding = 0) buffer B { float16_t data[]; } buf;
void main() {
coopmat<float16_t, gl_ScopeSubgroup, M, N, gl_MatrixUseAccumulator> m =
coopmat<float16_t, gl_ScopeSubgroup, M, N, gl_MatrixUseAccumulator>(float16_t(0.0));
coopMatStore(m, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor);
}
)";
std::string spirv = compileWithDebugInfo(source);
EXPECT_NE(spirv.find("DebugTypeCooperativeMatrixKHR"), std::string::npos)
<< "Expected DebugTypeCooperativeMatrixKHR with spec-constant dims.\nSPIR-V:\n" << spirv;
}
// DebugTypeCooperativeMatrixKHR must be emitted for each distinct (component,
// scope, rows, cols, use) tuple -- MatrixUseA, MatrixUseB, and MatrixUseAccumulator
// with different component types all produce separate debug type instructions.
TEST_F(SpvDebugInfoTest, CooperativeMatrixKHRMultipleUsesEmitDistinctDebugTypes)
{
const std::string source = R"(
#version 450 core
#pragma use_vulkan_memory_model
#extension GL_KHR_memory_scope_semantics : enable
#extension GL_KHR_cooperative_matrix : enable
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
layout(local_size_x = 32) in;
layout(set = 0, binding = 0) buffer B { float16_t data[]; } buf;
void main() {
coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseA> matA;
coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseB> matB;
coopmat<float, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator> matC =
coopmat<float, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator>(0.0);
matC = coopMatMulAdd(matA, matB, matC);
coopMatStore(matC, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor);
}
)";
std::string spirv = compileWithDebugInfo(source);
// Count occurrences of DebugTypeCooperativeMatrixKHR: one for each distinct
// (component, scope, rows, cols, use) combination used above.
size_t count = 0;
size_t pos = 0;
while ((pos = spirv.find("DebugTypeCooperativeMatrixKHR", pos)) != std::string::npos) {
++count;
pos += 1;
}
EXPECT_GE(count, 3u)
<< "Expected at least 3 DebugTypeCooperativeMatrixKHR instructions.\nSPIR-V:\n" << spirv;
}
// The removed golden-file test spv.debuginfo.coopmatKHR.comp combined spec-constant
// dimensions with all three matrix use types. Verify that distinct
// DebugTypeCooperativeMatrixKHR instructions are emitted for MatrixUseA (lM x lK),
// MatrixUseB (lK x lN), and MatrixUseAccumulator (lM x lN) when all dimensions are
// specialization constants.
TEST_F(SpvDebugInfoTest, CooperativeMatrixKHRSpecConstDimsMultipleUsesEmitDistinctDebugTypes)
{
const std::string source = R"(
#version 450 core
#pragma use_vulkan_memory_model
#extension GL_KHR_memory_scope_semantics : enable
#extension GL_KHR_cooperative_matrix : enable
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
layout(local_size_x = 32) in;
layout(constant_id = 0) const uint lM = 16;
layout(constant_id = 1) const uint lN = 16;
layout(constant_id = 2) const uint lK = 16;
layout(set = 0, binding = 0) buffer B { float data[]; } buf;
void main() {
coopmat<float16_t, gl_ScopeSubgroup, lM, lK, gl_MatrixUseA> matA;
coopmat<float16_t, gl_ScopeSubgroup, lK, lN, gl_MatrixUseB> matB;
coopmat<float, gl_ScopeSubgroup, lM, lN, gl_MatrixUseAccumulator> matC =
coopmat<float, gl_ScopeSubgroup, lM, lN, gl_MatrixUseAccumulator>(0.0);
matC = coopMatMulAdd(matA, matB, matC);
coopMatStore(matC, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor);
}
)";
std::string spirv = compileWithDebugInfo(source);
size_t count = 0;
size_t pos = 0;
while ((pos = spirv.find("DebugTypeCooperativeMatrixKHR", pos)) != std::string::npos) {
++count;
pos += 1;
}
EXPECT_GE(count, 3u)
<< "Expected at least 3 DebugTypeCooperativeMatrixKHR instructions "
"(one per use type with spec-constant dimensions).\nSPIR-V:\n" << spirv;
}
// DebugTypeBasic for bfloat16_t must carry the debug type name "bfloat16_t",
// which is emitted only when the FPEncoding operand (BFloat16KHR = 0) is present.
TEST_F(SpvDebugInfoTest, BFloat16EmitsDebugTypeBasicWithFPEncoding)
{
const std::string source = R"(
#version 450 core
#extension GL_EXT_bfloat16 : require
#extension GL_EXT_shader_explicit_arithmetic_types : enable
layout(local_size_x = 64) in;
layout(set = 0, binding = 0) buffer B { float data[]; } buf;
void main() {
bfloat16_t bf = bfloat16_t(1.0);
buf.data[gl_GlobalInvocationID.x] = float(bf);
}
)";
std::string spirv = compileWithDebugInfo(source);
// The DebugTypeBasic for bfloat16_t should contain the name "bfloat16_t".
EXPECT_NE(spirv.find("\"bfloat16_t\""), std::string::npos)
<< "Expected \"bfloat16_t\" string in debug type.\nSPIR-V:\n" << spirv;
}
// DebugTypeBasic for floate4m3_t must carry the debug type name "floate4m3_t",
// which is emitted only when the FPEncoding operand (Float8E4M3EXT = 4214) is present.
TEST_F(SpvDebugInfoTest, Float8E4M3EmitsDebugTypeBasicWithFPEncoding)
{
const std::string source = R"(
#version 450 core
#extension GL_EXT_bfloat16 : require
#extension GL_EXT_float_e4m3 : require
#extension GL_EXT_shader_explicit_arithmetic_types : enable
layout(local_size_x = 64) in;
layout(set = 0, binding = 0) buffer B { float data[]; } buf;
void main() {
floate4m3_t f = floate4m3_t(2.0);
buf.data[gl_GlobalInvocationID.x] = float(f);
}
)";
std::string spirv = compileWithDebugInfo(source);
EXPECT_NE(spirv.find("\"floate4m3_t\""), std::string::npos)
<< "Expected \"floate4m3_t\" string in debug type.\nSPIR-V:\n" << spirv;
}
// DebugTypeBasic for floate5m2_t must carry the debug type name "floate5m2_t",
// which is emitted only when the FPEncoding operand (Float8E5M2EXT = 4215) is present.
TEST_F(SpvDebugInfoTest, Float8E5M2EmitsDebugTypeBasicWithFPEncoding)
{
const std::string source = R"(
#version 450 core
#extension GL_EXT_bfloat16 : require
#extension GL_EXT_float_e5m2 : require
#extension GL_EXT_shader_explicit_arithmetic_types : enable
layout(local_size_x = 64) in;
layout(set = 0, binding = 0) buffer B { float data[]; } buf;
void main() {
floate5m2_t f = floate5m2_t(3.0);
buf.data[gl_GlobalInvocationID.x] = float(f);
}
)";
std::string spirv = compileWithDebugInfo(source);
EXPECT_NE(spirv.find("\"floate5m2_t\""), std::string::npos)
<< "Expected \"floate5m2_t\" string in debug type.\nSPIR-V:\n" << spirv;
}
// When a version-101 opcode is emitted the import string must be promoted to
// NonSemantic.Shader.DebugInfo.101.
TEST_F(SpvDebugInfoTest, Version101OpcodePromotesImportStringTo101)
{
// DebugTypeCooperativeMatrixKHR is a version-101 opcode; using it must
// cause the import string to read ".101", not ".100".
const std::string source = R"(
#version 450 core
#pragma use_vulkan_memory_model
#extension GL_KHR_memory_scope_semantics : enable
#extension GL_KHR_cooperative_matrix : enable
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
layout(local_size_x = 32) in;
layout(set = 0, binding = 0) buffer B { float16_t data[]; } buf;
void main() {
coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator> m =
coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator>(float16_t(0.0));
coopMatStore(m, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor);
}
)";
std::string spirv = compileWithDebugInfo(source);
EXPECT_NE(spirv.find("\"NonSemantic.Shader.DebugInfo.101\""), std::string::npos)
<< "Expected import string NonSemantic.Shader.DebugInfo.101.\nSPIR-V:\n" << spirv;
EXPECT_EQ(spirv.find("\"NonSemantic.Shader.DebugInfo.100\""), std::string::npos)
<< "Import string must not be .100 when version-101 opcodes are used.\nSPIR-V:\n" << spirv;
}
// When only version-100 opcodes are emitted the import string must stay at
// NonSemantic.Shader.DebugInfo.100 and must not be promoted to .101.
TEST_F(SpvDebugInfoTest, Version100OnlyKeepsImportStringAt100)
{
const std::string source = R"(
#version 450 core
layout(local_size_x = 64) in;
layout(set = 0, binding = 0) buffer B { float data[]; } buf;
void main() {
float x = buf.data[gl_GlobalInvocationID.x];
buf.data[gl_GlobalInvocationID.x] = x * 2.0;
}
)";
std::string spirv = compileWithDebugInfo(source);
EXPECT_NE(spirv.find("\"NonSemantic.Shader.DebugInfo.100\""), std::string::npos)
<< "Expected import string NonSemantic.Shader.DebugInfo.100.\nSPIR-V:\n" << spirv;
EXPECT_EQ(spirv.find("\"NonSemantic.Shader.DebugInfo.101\""), std::string::npos)
<< "Import string must not be .101 when only version-100 opcodes are used.\nSPIR-V:\n" << spirv;
}
} // namespace
} // namespace glslangtest