yum-mirror/glslang

git clone https://git.yummers.dev/yum-mirror/glslang

Diego NovilloInitial support for NonSemantic.Shader.DebugInfo 101 (#4220)0971d794

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