From d21926bc04339a37926e42eaea95f538724ddfee Mon Sep 17 00:00:00 2001 From: Konstantin Seurer Date: Sun, 22 Sep 2024 15:26:10 +0200 Subject: [PATCH] spirv: Emit code for NonSemantic.DebugPrintf if supported This can be useful for debugging code in situations where VVL cannot be used. (DGC, meta shaders) Reviewed-by: Samuel Pitoiset Part-of: --- src/compiler/spirv/spirv_to_nir.c | 65 ++++++++++++++++++++++ src/compiler/spirv/tests/non_semantic.cpp | 67 +++++++++++++++++++++++ 2 files changed, 132 insertions(+) diff --git a/src/compiler/spirv/spirv_to_nir.c b/src/compiler/spirv/spirv_to_nir.c index 62236141155..f22f74369cd 100644 --- a/src/compiler/spirv/spirv_to_nir.c +++ b/src/compiler/spirv/spirv_to_nir.c @@ -38,6 +38,7 @@ #include "util/u_math.h" #include "util/u_string.h" #include "util/u_debug.h" +#include "util/u_printf.h" #include "util/mesa-blake3.h" #include @@ -803,6 +804,67 @@ vtn_foreach_instruction(struct vtn_builder *b, const uint32_t *start, return w; } +static bool +vtn_handle_debug_printf(struct vtn_builder *b, SpvOp ext_opcode, + const uint32_t *w, unsigned count) +{ + vtn_assert(ext_opcode == 1); + + struct vtn_value *format = vtn_value(b, w[5], vtn_value_type_string); + + b->shader->printf_info_count++; + b->shader->printf_info = reralloc(b->shader, + b->shader->printf_info, + u_printf_info, + b->shader->printf_info_count); + + u_printf_info *info = + &b->shader->printf_info[b->shader->printf_info_count - 1]; + + uint32_t argc = count - 6; + *info = (u_printf_info) { + .arg_sizes = ralloc_array(b->shader, unsigned, argc), + .num_args = argc, + .strings = ralloc_strdup(b->shader, format->str), + .string_size = strlen(format->str) + 1, + }; + + uint32_t info_index = b->shader->printf_info_count - 1; + + if (argc) { + glsl_struct_field *fields = calloc(argc, sizeof(glsl_struct_field)); + for (uint32_t i = 0; i < argc; i++) { + struct vtn_ssa_value *arg = vtn_ssa_value(b, w[6 + i]); + + fields[i].type = glsl_intN_t_type(arg->def->bit_size); + if (arg->def->num_components > 1) + fields[i].type = glsl_vector_type(fields[i].type->base_type, arg->def->num_components); + + fields[i].name = ""; + + info->arg_sizes[i] = arg->def->bit_size / 8; + } + + nir_variable *packed_args = nir_local_variable_create( + b->nb.impl, glsl_struct_type(fields, argc, "packed_args", false), "packed_args"); + nir_deref_instr *var_deref = nir_build_deref_var(&b->nb, packed_args); + for (uint32_t i = 0; i < argc; i++) { + struct vtn_ssa_value *arg = vtn_ssa_value(b, w[6 + i]); + nir_deref_instr *arg_deref = nir_build_deref_struct(&b->nb, var_deref, i); + nir_store_deref(&b->nb, arg_deref, arg->def, BITFIELD_MASK(NIR_MAX_VEC_COMPONENTS)); + } + + nir_printf(&b->nb, &var_deref->def, .fmt_idx = info_index); + + free(fields); + } else { + nir_printf(&b->nb, nir_undef(&b->nb, 1, 32), .fmt_idx = info_index); + } + + /* Do nothing. */ + return true; +} + static bool vtn_handle_non_semantic_instruction(struct vtn_builder *b, SpvOp ext_opcode, const uint32_t *w, unsigned count) @@ -873,6 +935,9 @@ vtn_handle_extension(struct vtn_builder *b, SpvOp opcode, } else if ((strcmp(ext, "NonSemantic.Shader.DebugInfo.100") == 0) && (b->options && b->options->debug_info)) { val->ext_handler = vtn_handle_non_semantic_debug_info; + } else if (strcmp(ext, "NonSemantic.DebugPrintf") == 0 + && (b->options && b->options->printf)) { + val->ext_handler = vtn_handle_debug_printf; } else if (strstr(ext, "NonSemantic.") == ext) { val->ext_handler = vtn_handle_non_semantic_instruction; } else { diff --git a/src/compiler/spirv/tests/non_semantic.cpp b/src/compiler/spirv/tests/non_semantic.cpp index 9e409c242d4..6f0efad2bd1 100644 --- a/src/compiler/spirv/tests/non_semantic.cpp +++ b/src/compiler/spirv/tests/non_semantic.cpp @@ -51,3 +51,70 @@ TEST_F(NonSemantic, debug_break) nir_intrinsic_instr *intrinsic = find_intrinsic(nir_intrinsic_debug_break, 0); ASSERT_NE(intrinsic, nullptr); } + +TEST_F(NonSemantic, printf) +{ + /* + OpCapability Shader + OpExtension "SPV_KHR_non_semantic_info" + %1 = OpExtInstImport "GLSL.std.450" + %17 = OpExtInstImport "NonSemantic.DebugPrintf" + OpMemoryModel Logical GLSL450 + OpEntryPoint GLCompute %main "main" %gl_WorkGroupID %gl_LocalInvocationIndex + OpExecutionMode %main LocalSize 1 1 1 + %6 = OpString "(%u %u) Hello, World!" + OpSource GLSL 450 + OpSourceExtension "GL_EXT_debug_printf" + OpName %main "main" + OpName %gl_WorkGroupID "gl_WorkGroupID" + OpName %gl_LocalInvocationIndex "gl_LocalInvocationIndex" + OpDecorate %gl_WorkGroupID BuiltIn WorkgroupId + OpDecorate %gl_LocalInvocationIndex BuiltIn LocalInvocationIndex + %void = OpTypeVoid + %3 = OpTypeFunction %void + %uint = OpTypeInt 32 0 + %v3uint = OpTypeVector %uint 3 +%_ptr_Input_v3uint = OpTypePointer Input %v3uint +%gl_WorkGroupID = OpVariable %_ptr_Input_v3uint Input + %uint_0 = OpConstant %uint 0 +%_ptr_Input_uint = OpTypePointer Input %uint +%gl_LocalInvocationIndex = OpVariable %_ptr_Input_uint Input + %main = OpFunction %void None %3 + %5 = OpLabel + %13 = OpAccessChain %_ptr_Input_uint %gl_WorkGroupID %uint_0 + %14 = OpLoad %uint %13 + %16 = OpLoad %uint %gl_LocalInvocationIndex + %18 = OpExtInst %void %17 1 %6 %14 %16 + OpReturn + OpFunctionEnd + */ + static const uint32_t words[] = { + 0x07230203, 0x00010000, 0x0008000b, 0x00000013, 0x00000000, 0x00020011, 0x00000001, 0x0008000a, + 0x5f565053, 0x5f52484b, 0x5f6e6f6e, 0x616d6573, 0x6369746e, 0x666e695f, 0x0000006f, 0x0006000b, + 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0008000b, 0x00000011, 0x536e6f4e, + 0x6e616d65, 0x2e636974, 0x75626544, 0x69725067, 0x0066746e, 0x0003000e, 0x00000000, 0x00000001, + 0x0007000f, 0x00000005, 0x00000004, 0x6e69616d, 0x00000000, 0x0000000a, 0x0000000f, 0x00060010, + 0x00000004, 0x00000011, 0x00000001, 0x00000001, 0x00000001, 0x00080007, 0x00000006, 0x20752528, + 0x20297525, 0x6c6c6548, 0x57202c6f, 0x646c726f, 0x00000021, 0x00030003, 0x00000002, 0x000001c2, + 0x00060004, 0x455f4c47, 0x645f5458, 0x67756265, 0x6972705f, 0x0066746e, 0x00040005, 0x00000004, + 0x6e69616d, 0x00000000, 0x00060005, 0x0000000a, 0x575f6c67, 0x476b726f, 0x70756f72, 0x00004449, + 0x00080005, 0x0000000f, 0x4c5f6c67, 0x6c61636f, 0x6f766e49, 0x69746163, 0x6e496e6f, 0x00786564, + 0x00040047, 0x0000000a, 0x0000000b, 0x0000001a, 0x00040047, 0x0000000f, 0x0000000b, 0x0000001d, + 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00040015, 0x00000007, 0x00000020, + 0x00000000, 0x00040017, 0x00000008, 0x00000007, 0x00000003, 0x00040020, 0x00000009, 0x00000001, + 0x00000008, 0x0004003b, 0x00000009, 0x0000000a, 0x00000001, 0x0004002b, 0x00000007, 0x0000000b, + 0x00000000, 0x00040020, 0x0000000c, 0x00000001, 0x00000007, 0x0004003b, 0x0000000c, 0x0000000f, + 0x00000001, 0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, + 0x00050041, 0x0000000c, 0x0000000d, 0x0000000a, 0x0000000b, 0x0004003d, 0x00000007, 0x0000000e, + 0x0000000d, 0x0004003d, 0x00000007, 0x00000010, 0x0000000f, 0x0008000c, 0x00000002, 0x00000012, + 0x00000011, 0x00000001, 0x00000006, 0x0000000e, 0x00000010, 0x000100fd, 0x00010038, + }; + + spirv_options.printf = true; + + get_nir(sizeof(words) / sizeof(words[0]), words); + + nir_intrinsic_instr *intrinsic = find_intrinsic(nir_intrinsic_printf, 0); + ASSERT_NE(intrinsic, nullptr); + ASSERT_TRUE(intrinsic->src[0].ssa->parent_instr->type == nir_instr_type_deref); +}