yum-mirror/glslang
git clone https://git.yummers.dev/yum-mirror/glslang
0971d794
16.3.0
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. 49std :: string compileWithDebugInfo ( const std :: string & source ) 50{ 51glslang :: TShader shader ( EShLangCompute ) ; 52const char * str = source . c_str () ; 53shader . setStrings ( & str , 1 ) ; 54shader . setEnvInput ( glslang :: EShSourceGlsl , EShLangCompute , 55glslang :: EShClientVulkan , 110 ) ; 56shader . setEnvClient ( glslang :: EShClientVulkan , glslang :: EShTargetVulkan_1_1 ) ; 57shader . setEnvTarget ( glslang :: EShTargetSpv , glslang :: EShTargetSpv_1_3 ) ; 58 59EShMessages messages = static_cast < EShMessages > ( EShMsgDefault | EShMsgDebugInfo ) ; 60if ( ! shader . parse ( GetDefaultResources () , 450 , false , messages )) 61return "COMPILATION_FAILED: " + std :: string ( shader . getInfoLog ()) ; 62 63glslang :: TProgram program ; 64program . addShader ( & shader ) ; 65if ( ! program . link ( messages )) 66return "LINKING_FAILED: " + std :: string ( program . getInfoLog ()) ; 67 68glslang :: SpvOptions opts ; 69opts . generateDebugInfo = true ; 70opts . emitNonSemanticShaderDebugInfo = true ; 71opts . disableOptimizer = true ; 72 73std :: vector < uint32_t > spirv ; 74glslang :: GlslangToSpv ( * program . getIntermediate ( EShLangCompute ) , spirv , & opts ) ; 75 76std :: ostringstream out ; 77spv :: Disassemble ( out , spirv ) ; 78return 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{ 86const 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() { 94coopvecNV<float, 8> v = coopvecNV<float, 8>(0.0); 95v = v + v; 96buf.data[gl_GlobalInvocationID.x] = v[0]; 97} 98)" ; 99std :: string spirv = compileWithDebugInfo ( source ) ; 100EXPECT_NE ( spirv . find ( "DebugTypeVectorIdEXT" ) , std :: string :: npos ) 101<< "Expected DebugTypeVectorIdEXT in disassembly. \n SPIR-V: \n " << spirv ; 102EXPECT_NE ( spirv . find ( "TypeCooperativeVectorNV" ) , std :: string :: npos ) 103<< "Expected TypeCooperativeVectorNV in disassembly. \n SPIR-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{ 111const 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() { 120coopvecNV<float, N> v = coopvecNV<float, N>(1.0); 121buf.data[gl_GlobalInvocationID.x] = v[0]; 122} 123)" ; 124std :: string spirv = compileWithDebugInfo ( source ) ; 125EXPECT_NE ( spirv . find ( "DebugTypeVectorIdEXT" ) , std :: string :: npos ) 126<< "Expected DebugTypeVectorIdEXT with spec-constant count. \n SPIR-V: \n " << spirv ; 127} 128 129// DebugTypeCooperativeMatrixKHR (opcode 110) must replace the former 130// opaque-composite workaround for OpTypeCooperativeMatrixKHR. 131TEST_F ( SpvDebugInfoTest , CooperativeMatrixKHREmitsDebugTypeCooperativeMatrixKHR ) 132{ 133const 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() { 142coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator> m = 143coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator>(float16_t(0.0)); 144coopMatStore(m, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor); 145} 146)" ; 147std :: string spirv = compileWithDebugInfo ( source ) ; 148EXPECT_NE ( spirv . find ( "DebugTypeCooperativeMatrixKHR" ) , std :: string :: npos ) 149<< "Expected DebugTypeCooperativeMatrixKHR in disassembly. \n SPIR-V: \n " << spirv ; 150// The old opaque-composite workaround must not appear. 151EXPECT_EQ ( spirv . find ( "coopmat<" ) , std :: string :: npos ) 152<< "Opaque-composite workaround should not appear in disassembly. \n SPIR-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{ 160const 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() { 171coopmat<float16_t, gl_ScopeSubgroup, M, N, gl_MatrixUseAccumulator> m = 172coopmat<float16_t, gl_ScopeSubgroup, M, N, gl_MatrixUseAccumulator>(float16_t(0.0)); 173coopMatStore(m, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor); 174} 175)" ; 176std :: string spirv = compileWithDebugInfo ( source ) ; 177EXPECT_NE ( spirv . find ( "DebugTypeCooperativeMatrixKHR" ) , std :: string :: npos ) 178<< "Expected DebugTypeCooperativeMatrixKHR with spec-constant dims. \n SPIR-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{ 186const 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() { 195coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseA> matA; 196coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseB> matB; 197coopmat<float, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator> matC = 198coopmat<float, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator>(0.0); 199matC = coopMatMulAdd(matA, matB, matC); 200coopMatStore(matC, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor); 201} 202)" ; 203std :: string spirv = compileWithDebugInfo ( source ) ; 204 205// Count occurrences of DebugTypeCooperativeMatrixKHR: one for each distinct 206// (component, scope, rows, cols, use) combination used above. 207size_t count = 0 ; 208size_t pos = 0 ; 209while (( pos = spirv . find ( "DebugTypeCooperativeMatrixKHR" , pos )) != std :: string :: npos ) { 210++ count ; 211pos += 1 ; 212} 213EXPECT_GE ( count , 3u ) 214<< "Expected at least 3 DebugTypeCooperativeMatrixKHR instructions. \n SPIR-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{ 224const 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() { 236coopmat<float16_t, gl_ScopeSubgroup, lM, lK, gl_MatrixUseA> matA; 237coopmat<float16_t, gl_ScopeSubgroup, lK, lN, gl_MatrixUseB> matB; 238coopmat<float, gl_ScopeSubgroup, lM, lN, gl_MatrixUseAccumulator> matC = 239coopmat<float, gl_ScopeSubgroup, lM, lN, gl_MatrixUseAccumulator>(0.0); 240matC = coopMatMulAdd(matA, matB, matC); 241coopMatStore(matC, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor); 242} 243)" ; 244std :: string spirv = compileWithDebugInfo ( source ) ; 245 246size_t count = 0 ; 247size_t pos = 0 ; 248while (( pos = spirv . find ( "DebugTypeCooperativeMatrixKHR" , pos )) != std :: string :: npos ) { 249++ count ; 250pos += 1 ; 251} 252EXPECT_GE ( count , 3u ) 253<< "Expected at least 3 DebugTypeCooperativeMatrixKHR instructions " 254"(one per use type with spec-constant dimensions). \n SPIR-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{ 261const 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() { 268bfloat16_t bf = bfloat16_t(1.0); 269buf.data[gl_GlobalInvocationID.x] = float(bf); 270} 271)" ; 272std :: string spirv = compileWithDebugInfo ( source ) ; 273// The DebugTypeBasic for bfloat16_t should contain the name "bfloat16_t". 274EXPECT_NE ( spirv . find ( " \" bfloat16_t \" " ) , std :: string :: npos ) 275<< "Expected \" bfloat16_t \" string in debug type. \n SPIR-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{ 282const 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() { 290floate4m3_t f = floate4m3_t(2.0); 291buf.data[gl_GlobalInvocationID.x] = float(f); 292} 293)" ; 294std :: string spirv = compileWithDebugInfo ( source ) ; 295EXPECT_NE ( spirv . find ( " \" floate4m3_t \" " ) , std :: string :: npos ) 296<< "Expected \" floate4m3_t \" string in debug type. \n SPIR-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{ 303const 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() { 311floate5m2_t f = floate5m2_t(3.0); 312buf.data[gl_GlobalInvocationID.x] = float(f); 313} 314)" ; 315std :: string spirv = compileWithDebugInfo ( source ) ; 316EXPECT_NE ( spirv . find ( " \" floate5m2_t \" " ) , std :: string :: npos ) 317<< "Expected \" floate5m2_t \" string in debug type. \n SPIR-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". 326const 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() { 335coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator> m = 336coopmat<float16_t, gl_ScopeSubgroup, 16, 16, gl_MatrixUseAccumulator>(float16_t(0.0)); 337coopMatStore(m, buf.data, 0, 0, gl_CooperativeMatrixLayoutRowMajor); 338} 339)" ; 340std :: string spirv = compileWithDebugInfo ( source ) ; 341EXPECT_NE ( spirv . find ( " \" NonSemantic.Shader.DebugInfo.101 \" " ) , std :: string :: npos ) 342<< "Expected import string NonSemantic.Shader.DebugInfo.101. \n SPIR-V: \n " << spirv ; 343EXPECT_EQ ( spirv . find ( " \" NonSemantic.Shader.DebugInfo.100 \" " ) , std :: string :: npos ) 344<< "Import string must not be .100 when version-101 opcodes are used. \n SPIR-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{ 351const 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() { 356float x = buf.data[gl_GlobalInvocationID.x]; 357buf.data[gl_GlobalInvocationID.x] = x * 2.0; 358} 359)" ; 360std :: string spirv = compileWithDebugInfo ( source ) ; 361EXPECT_NE ( spirv . find ( " \" NonSemantic.Shader.DebugInfo.100 \" " ) , std :: string :: npos ) 362<< "Expected import string NonSemantic.Shader.DebugInfo.100. \n SPIR-V: \n " << spirv ; 363EXPECT_EQ ( spirv . find ( " \" NonSemantic.Shader.DebugInfo.101 \" " ) , std :: string :: npos ) 364<< "Import string must not be .101 when only version-100 opcodes are used. \n SPIR-V: \n " << spirv ; 365} 366 367} // namespace 368} // namespace glslangtest