yum-mirror/glslang

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

Jeff BolzImplement GL_EXT_ocp_microscaling_types62f5430e

16.5.0
23.5 KiB556 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 "SPIRV/NonSemanticShaderDebugInfo.h"
38#include "glslang/Public/ResourceLimits.h"
39#include <gtest/gtest.h>
40#include <cstdint>
41#include <sstream>
42#include <string>
43#include <vector>
44
45namespace glslangtest {
46namespace {
47
48std::string decodeLiteralString(const uint32_t* words, size_t wordCount)
49{
50    std::string result;
51    for (size_t wordIndex = 0; wordIndex < wordCount; ++wordIndex) {
52        for (unsigned int byteIndex = 0; byteIndex < 4; ++byteIndex) {
53            const char character = static_cast<char>((words[wordIndex] >> (8 * byteIndex)) & 0xff);
54            if (character == '\0')
55                return result;
56            result.push_back(character);
57        }
58    }
59    return result;
60}
61
62bool containsDebugTypeBasic(const std::vector<uint32_t>& spirv, const char* expectedName,
63                            uint32_t expectedWidth, spv::FPEncoding expectedFPEncoding)
64{
65    if (spirv.size() < 5 || spirv[0] != spv::MagicNumber)
66        return false;
67
68    const uint32_t idBound = spirv[3];
69    std::vector<std::string> strings(idBound);
70    std::vector<uint32_t> constants(idBound);
71    std::vector<bool> isConstant(idBound);
72    uint32_t debugInfoImportId = 0;
73
74    size_t end = spirv.size();
75
76    // Resolve the IDs referenced by DebugTypeBasic before inspecting the instruction itself.
77    // OpExtInstImport gives the result ID used to select the .101 debug-info instruction set,
78    // OpString gives the source-level type name, and OpConstant gives the values of the width,
79    // base encoding, flags, and optional FPEncoding operands.
80    //
81    // For example, the relevant SPIR-V for floate2m1_t has this shape:
82    //   %debug = OpExtInstImport "NonSemantic.Shader.DebugInfo.101"
83    //   %name = OpString "floate2m1_t"
84    //   %width = OpConstant %uint 4
85    //   %encoding = OpConstant %uint 3       ; NonSemanticShaderDebugInfoFloat
86    //   %flags = OpConstant %uint 0          ; NonSemanticShaderDebugInfoNone
87    //   %fpEncoding = OpConstant %uint 4225  ; Float4E2M1EXT
88    //   %type = OpExtInst %void %debug DebugTypeBasic
89    //               %name %width %encoding %flags %fpEncoding
90    for (size_t offset = 5; offset < spirv.size();) {
91        const uint32_t wordCount = spirv[offset] >> spv::WordCountShift;
92        const spv::Op opcode = static_cast<spv::Op>(spirv[offset] & spv::OpCodeMask);
93        if (wordCount == 0 || offset + wordCount > spirv.size())
94            return false;
95
96        if (opcode == spv::Op::OpFunction) {
97            // SPIR-V's logical layout places imports, debug strings, constants, and global
98            // debug-type instructions before the first OpFunction. Record that boundary while
99            // resolving IDs so this pass can exit early and the subsequent DebugTypeBasic scan
100            // can stop there as well.
101            end = offset;
102            break;
103        }
104
105        if (opcode == spv::Op::OpExtInstImport && wordCount >= 3) {
106            const uint32_t resultId = spirv[offset + 1];
107            if (resultId < idBound &&
108                decodeLiteralString(&spirv[offset + 2], wordCount - 2) ==
109                    "NonSemantic.Shader.DebugInfo.101") {
110                debugInfoImportId = resultId;
111            }
112        } else if (opcode == spv::Op::OpString && wordCount >= 3) {
113            const uint32_t resultId = spirv[offset + 1];
114            if (resultId < idBound)
115                strings[resultId] = decodeLiteralString(&spirv[offset + 2], wordCount - 2);
116        } else if (opcode == spv::Op::OpConstant && wordCount == 4) {
117            const uint32_t resultId = spirv[offset + 2];
118            if (resultId < idBound) {
119                constants[resultId] = spirv[offset + 3];
120                isConstant[resultId] = true;
121            }
122        }
123
124        offset += wordCount;
125    }
126
127    if (debugInfoImportId == 0)
128        return false;
129
130    const auto hasConstantValue = [&](uint32_t id, uint32_t value) {
131        return id < idBound && isConstant[id] && constants[id] == value;
132    };
133
134    // A DebugTypeBasic with an FPEncoding has five operands after its instruction number:
135    // Name, Size, Encoding, Flags, and FPEncoding. Including the five-word OpExtInst prefix,
136    // that gives a word count of 10. Match the imported .101 instruction set and resolve each
137    // operand ID through the OpString and OpConstant tables populated above.
138    for (size_t offset = 5; offset < end;) {
139        const uint32_t wordCount = spirv[offset] >> spv::WordCountShift;
140        const spv::Op opcode = static_cast<spv::Op>(spirv[offset] & spv::OpCodeMask);
141        if (wordCount == 0 || offset + wordCount > spirv.size())
142            return false;
143
144        if (opcode == spv::Op::OpExtInst && wordCount == 10 &&
145            spirv[offset + 3] == debugInfoImportId &&
146            spirv[offset + 4] == NonSemanticShaderDebugInfoDebugTypeBasic) {
147            const uint32_t nameId = spirv[offset + 5];
148            const uint32_t widthId = spirv[offset + 6];
149            const uint32_t encodingId = spirv[offset + 7];
150            const uint32_t flagsId = spirv[offset + 8];
151            const uint32_t fpEncodingId = spirv[offset + 9];
152            if (nameId < idBound && strings[nameId] == expectedName &&
153                hasConstantValue(widthId, expectedWidth) &&
154                hasConstantValue(encodingId, NonSemanticShaderDebugInfoFloat) &&
155                hasConstantValue(flagsId, NonSemanticShaderDebugInfoNone) &&
156                hasConstantValue(fpEncodingId, static_cast<uint32_t>(expectedFPEncoding))) {
157                return true;
158            }
159        }
160
161        offset += wordCount;
162    }
163
164    return false;
165}
166
167class SpvDebugInfoTest : public ::testing::Test {
168protected:
169    bool compileToSpirvWithDebugInfo(const std::string& source, std::vector<uint32_t>& spirv,
170                                     std::string& error)
171    {
172        spirv.clear();
173        error.clear();
174        glslang::TShader shader(EShLangCompute);
175        const char* str = source.c_str();
176        shader.setStrings(&str, 1);
177        shader.setEnvInput(glslang::EShSourceGlsl, EShLangCompute,
178                           glslang::EShClientVulkan, 110);
179        shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_1);
180        shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_3);
181
182        EShMessages messages = static_cast<EShMessages>(EShMsgDefault | EShMsgDebugInfo);
183        if (!shader.parse(GetDefaultResources(), 450, false, messages)) {
184            error = "COMPILATION_FAILED: " + std::string(shader.getInfoLog());
185            return false;
186        }
187
188        glslang::TProgram program;
189        program.addShader(&shader);
190        if (!program.link(messages)) {
191            error = "LINKING_FAILED: " + std::string(program.getInfoLog());
192            return false;
193        }
194
195        glslang::SpvOptions opts;
196        opts.generateDebugInfo = true;
197        opts.emitNonSemanticShaderDebugInfo = true;
198        opts.disableOptimizer = true;
199
200        glslang::GlslangToSpv(*program.getIntermediate(EShLangCompute), spirv, &opts);
201        return true;
202    }
203
204    // Compile a GLSL compute shader with NonSemantic debug info enabled, targeting
205    // Vulkan 1.1 / SPIR-V 1.3, and return the disassembly.
206    std::string compileWithDebugInfo(const std::string& source)
207    {
208        std::vector<uint32_t> spirv;
209        std::string error;
210        if (!compileToSpirvWithDebugInfo(source, spirv, error))
211            return error;
212
213        std::ostringstream out;
214        spv::Disassemble(out, spirv);
215        return out.str();
216    }
217};
218
219// DebugTypeVectorIdEXT (opcode 109) must be emitted for OpTypeCooperativeVectorNV
220// when NonSemantic debug info is enabled.
221TEST_F(SpvDebugInfoTest, CooperativeVectorNVEmitsDebugTypeVectorIdEXT)
222{
223    const std::string source = R"(
224#version 450 core
225#extension GL_KHR_memory_scope_semantics : enable
226#extension GL_NV_cooperative_vector : enable
227#extension GL_EXT_shader_explicit_arithmetic_types : enable
228layout(local_size_x = 64) in;
229layout(set = 0, binding = 0) buffer B { float data[]; } buf;
230void main() {
231    coopvecNV<float, 8> v = coopvecNV<float, 8>(0.0);
232    v = v + v;
233    buf.data[gl_GlobalInvocationID.x] = v[0];
234}
235)";
236    std::string spirv = compileWithDebugInfo(source);
237    EXPECT_NE(spirv.find("DebugTypeVectorIdEXT"), std::string::npos)
238        << "Expected DebugTypeVectorIdEXT in disassembly.\nSPIR-V:\n" << spirv;
239    EXPECT_NE(spirv.find("TypeCooperativeVectorNV"), std::string::npos)
240        << "Expected TypeCooperativeVectorNV in disassembly.\nSPIR-V:\n" << spirv;
241}
242
243// DebugTypeVectorIdEXT must also be emitted when the component count is a
244// specialization constant (OpSpecConstant), since the spec allows any constant
245// instruction for the ComponentCount operand.
246TEST_F(SpvDebugInfoTest, CooperativeVectorNVSpecConstCountEmitsDebugTypeVectorIdEXT)
247{
248    const std::string source = R"(
249#version 450 core
250#extension GL_KHR_memory_scope_semantics : enable
251#extension GL_NV_cooperative_vector : enable
252#extension GL_EXT_shader_explicit_arithmetic_types : enable
253layout(local_size_x = 64) in;
254layout(constant_id = 0) const uint N = 16;
255layout(set = 0, binding = 0) buffer B { float data[]; } buf;
256void main() {
257    coopvecNV<float, N> v = coopvecNV<float, N>(1.0);
258    buf.data[gl_GlobalInvocationID.x] = v[0];
259}
260)";
261    std::string spirv = compileWithDebugInfo(source);
262    EXPECT_NE(spirv.find("DebugTypeVectorIdEXT"), std::string::npos)
263        << "Expected DebugTypeVectorIdEXT with spec-constant count.\nSPIR-V:\n" << spirv;
264}
265
266// DebugTypeCooperativeMatrixKHR (opcode 110) must replace the former
267// opaque-composite workaround for OpTypeCooperativeMatrixKHR.
268TEST_F(SpvDebugInfoTest, CooperativeMatrixKHREmitsDebugTypeCooperativeMatrixKHR)
269{
270    const std::string source = R"(
271#version 450 core
272#pragma use_vulkan_memory_model
273#extension GL_KHR_memory_scope_semantics : enable
274#extension GL_KHR_cooperative_matrix : enable
275#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
276layout(local_size_x = 32) in;
277layout(set = 0, binding = 0) buffer B { float16_t data[]; } buf;
278void main() {
279    coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator> m =
280        coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator>(float16_t(0.0));
281    coopMatStore(m, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor);
282}
283)";
284    std::string spirv = compileWithDebugInfo(source);
285    EXPECT_NE(spirv.find("DebugTypeCooperativeMatrixKHR"), std::string::npos)
286        << "Expected DebugTypeCooperativeMatrixKHR in disassembly.\nSPIR-V:\n" << spirv;
287    // The old opaque-composite workaround must not appear.
288    EXPECT_EQ(spirv.find("coopmat<"), std::string::npos)
289        << "Opaque-composite workaround should not appear in disassembly.\nSPIR-V:\n" << spirv;
290}
291
292// DebugTypeCooperativeMatrixKHR must be emitted when Rows and Columns are
293// specialization constants (OpSpecConstant), since the spec allows any constant
294// instruction for those operands.
295TEST_F(SpvDebugInfoTest, CooperativeMatrixKHRSpecConstDimsEmitsDebugType)
296{
297    const std::string source = R"(
298#version 450 core
299#pragma use_vulkan_memory_model
300#extension GL_KHR_memory_scope_semantics : enable
301#extension GL_KHR_cooperative_matrix : enable
302#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
303layout(local_size_x = 32) in;
304layout(constant_id = 0) const uint M = 16;
305layout(constant_id = 1) const uint N = 16;
306layout(set = 0, binding = 0) buffer B { float16_t data[]; } buf;
307void main() {
308    coopmat<float16_t, gl_ScopeSubgroup, M, N, gl_MatrixUseAccumulator> m =
309        coopmat<float16_t, gl_ScopeSubgroup, M, N, gl_MatrixUseAccumulator>(float16_t(0.0));
310    coopMatStore(m, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor);
311}
312)";
313    std::string spirv = compileWithDebugInfo(source);
314    EXPECT_NE(spirv.find("DebugTypeCooperativeMatrixKHR"), std::string::npos)
315        << "Expected DebugTypeCooperativeMatrixKHR with spec-constant dims.\nSPIR-V:\n" << spirv;
316}
317
318// DebugTypeCooperativeMatrixKHR must be emitted for each distinct (component,
319// scope, rows, cols, use) tuple -- MatrixUseA, MatrixUseB, and MatrixUseAccumulator
320// with different component types all produce separate debug type instructions.
321TEST_F(SpvDebugInfoTest, CooperativeMatrixKHRMultipleUsesEmitDistinctDebugTypes)
322{
323    const std::string source = R"(
324#version 450 core
325#pragma use_vulkan_memory_model
326#extension GL_KHR_memory_scope_semantics : enable
327#extension GL_KHR_cooperative_matrix : enable
328#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
329layout(local_size_x = 32) in;
330layout(set = 0, binding = 0) buffer B { float16_t data[]; } buf;
331void main() {
332    coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseA>           matA;
333    coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseB>           matB;
334    coopmat<float,     gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator>  matC =
335        coopmat<float, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator>(0.0);
336    matC = coopMatMulAdd(matA, matB, matC);
337    coopMatStore(matC, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor);
338}
339)";
340    std::string spirv = compileWithDebugInfo(source);
341
342    // Count occurrences of DebugTypeCooperativeMatrixKHR: one for each distinct
343    // (component, scope, rows, cols, use) combination used above.
344    size_t count = 0;
345    size_t pos = 0;
346    while ((pos = spirv.find("DebugTypeCooperativeMatrixKHR", pos)) != std::string::npos) {
347        ++count;
348        pos += 1;
349    }
350    EXPECT_GE(count, 3u)
351        << "Expected at least 3 DebugTypeCooperativeMatrixKHR instructions.\nSPIR-V:\n" << spirv;
352}
353
354// The removed golden-file test spv.debuginfo.coopmatKHR.comp combined spec-constant
355// dimensions with all three matrix use types. Verify that distinct
356// DebugTypeCooperativeMatrixKHR instructions are emitted for MatrixUseA (lM x lK),
357// MatrixUseB (lK x lN), and MatrixUseAccumulator (lM x lN) when all dimensions are
358// specialization constants.
359TEST_F(SpvDebugInfoTest, CooperativeMatrixKHRSpecConstDimsMultipleUsesEmitDistinctDebugTypes)
360{
361    const std::string source = R"(
362#version 450 core
363#pragma use_vulkan_memory_model
364#extension GL_KHR_memory_scope_semantics : enable
365#extension GL_KHR_cooperative_matrix : enable
366#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
367layout(local_size_x = 32) in;
368layout(constant_id = 0) const uint lM = 16;
369layout(constant_id = 1) const uint lN = 16;
370layout(constant_id = 2) const uint lK = 16;
371layout(set = 0, binding = 0) buffer B { float data[]; } buf;
372void main() {
373    coopmat<float16_t, gl_ScopeSubgroup, lM, lK, gl_MatrixUseA>           matA;
374    coopmat<float16_t, gl_ScopeSubgroup, lK, lN, gl_MatrixUseB>           matB;
375    coopmat<float,     gl_ScopeSubgroup, lM, lN, gl_MatrixUseAccumulator>  matC =
376        coopmat<float, gl_ScopeSubgroup, lM, lN, gl_MatrixUseAccumulator>(0.0);
377    matC = coopMatMulAdd(matA, matB, matC);
378    coopMatStore(matC, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor);
379}
380)";
381    std::string spirv = compileWithDebugInfo(source);
382
383    size_t count = 0;
384    size_t pos = 0;
385    while ((pos = spirv.find("DebugTypeCooperativeMatrixKHR", pos)) != std::string::npos) {
386        ++count;
387        pos += 1;
388    }
389    EXPECT_GE(count, 3u)
390        << "Expected at least 3 DebugTypeCooperativeMatrixKHR instructions "
391           "(one per use type with spec-constant dimensions).\nSPIR-V:\n" << spirv;
392}
393
394// DebugTypeBasic for bfloat16_t must carry the debug type name "bfloat16_t",
395// which is emitted only when the FPEncoding operand (BFloat16KHR = 0) is present.
396TEST_F(SpvDebugInfoTest, BFloat16EmitsDebugTypeBasicWithFPEncoding)
397{
398    const std::string source = R"(
399#version 450 core
400#extension GL_EXT_bfloat16 : require
401#extension GL_EXT_shader_explicit_arithmetic_types : enable
402layout(local_size_x = 64) in;
403layout(set = 0, binding = 0) buffer B { float data[]; } buf;
404void main() {
405    bfloat16_t bf = bfloat16_t(1.0);
406    buf.data[gl_GlobalInvocationID.x] = float(bf);
407}
408)";
409    std::string spirv = compileWithDebugInfo(source);
410    // The DebugTypeBasic for bfloat16_t should contain the name "bfloat16_t".
411    EXPECT_NE(spirv.find("\"bfloat16_t\""), std::string::npos)
412        << "Expected \"bfloat16_t\" string in debug type.\nSPIR-V:\n" << spirv;
413}
414
415// DebugTypeBasic for floate4m3_t must carry the debug type name "floate4m3_t",
416// which is emitted only when the FPEncoding operand (Float8E4M3EXT = 4214) is present.
417TEST_F(SpvDebugInfoTest, Float8E4M3EmitsDebugTypeBasicWithFPEncoding)
418{
419    const std::string source = R"(
420#version 450 core
421#extension GL_EXT_bfloat16 : require
422#extension GL_EXT_float_e4m3 : require
423#extension GL_EXT_shader_explicit_arithmetic_types : enable
424layout(local_size_x = 64) in;
425layout(set = 0, binding = 0) buffer B { float data[]; } buf;
426void main() {
427    floate4m3_t f = floate4m3_t(2.0);
428    buf.data[gl_GlobalInvocationID.x] = float(f);
429}
430)";
431    std::string spirv = compileWithDebugInfo(source);
432    EXPECT_NE(spirv.find("\"floate4m3_t\""), std::string::npos)
433        << "Expected \"floate4m3_t\" string in debug type.\nSPIR-V:\n" << spirv;
434}
435
436// DebugTypeBasic for floate5m2_t must carry the debug type name "floate5m2_t",
437// which is emitted only when the FPEncoding operand (Float8E5M2EXT = 4215) is present.
438TEST_F(SpvDebugInfoTest, Float8E5M2EmitsDebugTypeBasicWithFPEncoding)
439{
440    const std::string source = R"(
441#version 450 core
442#extension GL_EXT_bfloat16 : require
443#extension GL_EXT_float_e5m2 : require
444#extension GL_EXT_shader_explicit_arithmetic_types : enable
445layout(local_size_x = 64) in;
446layout(set = 0, binding = 0) buffer B { float data[]; } buf;
447void main() {
448    floate5m2_t f = floate5m2_t(3.0);
449    buf.data[gl_GlobalInvocationID.x] = float(f);
450}
451)";
452    std::string spirv = compileWithDebugInfo(source);
453    EXPECT_NE(spirv.find("\"floate5m2_t\""), std::string::npos)
454        << "Expected \"floate5m2_t\" string in debug type.\nSPIR-V:\n" << spirv;
455}
456
457// DebugTypeBasic for OCP microscaling types must carry their source-level type
458// names, which are emitted only when the FPEncoding operand is present.
459TEST_F(SpvDebugInfoTest, OcpMicroscalingTypesEmitDebugTypeBasicWithFPEncoding)
460{
461    const std::string source = R"(
462#version 450 core
463#extension GL_EXT_float_e2m1 : require
464#extension GL_EXT_float_e3m2 : require
465#extension GL_EXT_float_e2m3 : require
466#extension GL_EXT_float_ue8m0 : require
467#extension GL_EXT_float_mxint8 : require
468#extension GL_EXT_shader_explicit_arithmetic_types : enable
469layout(local_size_x = 64) in;
470layout(set = 0, binding = 0) buffer B { float data[]; } buf;
471void main() {
472    floate2m1_t e2m1 = floate2m1_t(1.0fe2m1);
473    floate3m2_t e3m2 = floate3m2_t(1.0fe3m2);
474    floate2m3_t e2m3 = floate2m3_t(1.0fe2m3);
475    floatue8m0_t ue8m0 = floatue8m0_t(1.0fue8m0);
476    floatmxint8_t mxint8 = floatmxint8_t(1.0fmxint8);
477    buf.data[gl_GlobalInvocationID.x] =
478        float(e2m1) + float(e3m2) + float(e2m3) + float(ue8m0) + float(mxint8);
479}
480)";
481    std::vector<uint32_t> spirv;
482    std::string error;
483    ASSERT_TRUE(compileToSpirvWithDebugInfo(source, spirv, error)) << error;
484
485    struct ExpectedDebugType {
486        const char* name;
487        uint32_t width;
488        spv::FPEncoding fpEncoding;
489    };
490    const ExpectedDebugType expectedTypes[] = {
491        {"floate2m1_t", 4, spv::FPEncoding::Float4E2M1EXT},
492        {"floate3m2_t", 6, spv::FPEncoding::Float6E3M2EXT},
493        {"floate2m3_t", 6, spv::FPEncoding::Float6E2M3EXT},
494        {"floatue8m0_t", 8, spv::FPEncoding::Float8UnsignedE8M0EXT},
495        {"floatmxint8_t", 8, spv::FPEncoding::MXInt8EXT},
496    };
497
498    std::ostringstream disassembly;
499    spv::Disassemble(disassembly, spirv);
500    for (const auto& expected : expectedTypes) {
501        EXPECT_TRUE(containsDebugTypeBasic(spirv, expected.name, expected.width, expected.fpEncoding))
502            << "Expected DebugTypeBasic for \"" << expected.name << "\" with width " << expected.width
503            << " and FPEncoding " << static_cast<uint32_t>(expected.fpEncoding)
504            << ".\nSPIR-V:\n" << disassembly.str();
505    }
506}
507
508// When a version-101 opcode is emitted the import string must be promoted to
509// NonSemantic.Shader.DebugInfo.101.
510TEST_F(SpvDebugInfoTest, Version101OpcodePromotesImportStringTo101)
511{
512    // DebugTypeCooperativeMatrixKHR is a version-101 opcode; using it must
513    // cause the import string to read ".101", not ".100".
514    const std::string source = R"(
515#version 450 core
516#pragma use_vulkan_memory_model
517#extension GL_KHR_memory_scope_semantics : enable
518#extension GL_KHR_cooperative_matrix : enable
519#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
520layout(local_size_x = 32) in;
521layout(set = 0, binding = 0) buffer B { float16_t data[]; } buf;
522void main() {
523    coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator> m =
524        coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator>(float16_t(0.0));
525    coopMatStore(m, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor);
526}
527)";
528    std::string spirv = compileWithDebugInfo(source);
529    EXPECT_NE(spirv.find("\"NonSemantic.Shader.DebugInfo.101\""), std::string::npos)
530        << "Expected import string NonSemantic.Shader.DebugInfo.101.\nSPIR-V:\n" << spirv;
531    EXPECT_EQ(spirv.find("\"NonSemantic.Shader.DebugInfo.100\""), std::string::npos)
532        << "Import string must not be .100 when version-101 opcodes are used.\nSPIR-V:\n" << spirv;
533}
534
535// When only version-100 opcodes are emitted the import string must stay at
536// NonSemantic.Shader.DebugInfo.100 and must not be promoted to .101.
537TEST_F(SpvDebugInfoTest, Version100OnlyKeepsImportStringAt100)
538{
539    const std::string source = R"(
540#version 450 core
541layout(local_size_x = 64) in;
542layout(set = 0, binding = 0) buffer B { float data[]; } buf;
543void main() {
544    float x = buf.data[gl_GlobalInvocationID.x];
545    buf.data[gl_GlobalInvocationID.x] = x * 2.0;
546}
547)";
548    std::string spirv = compileWithDebugInfo(source);
549    EXPECT_NE(spirv.find("\"NonSemantic.Shader.DebugInfo.100\""), std::string::npos)
550        << "Expected import string NonSemantic.Shader.DebugInfo.100.\nSPIR-V:\n" << spirv;
551    EXPECT_EQ(spirv.find("\"NonSemantic.Shader.DebugInfo.101\""), std::string::npos)
552        << "Import string must not be .101 when only version-100 opcodes are used.\nSPIR-V:\n" << spirv;
553}
554
555} // namespace
556} // namespace glslangtest