xref: /aosp_15_r20/external/mesa3d/src/compiler/spirv/vtn_debug.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2024 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Faith Ekstrand ([email protected])
25  *
26  */
27 
28 #include "vtn_private.h"
29 
30 #ifdef HAVE_SPIRV_TOOLS
31 #include <spirv-tools/libspirv.h>
32 #endif /* HAVE_SPIRV_TOOLS */
33 
34 void
spirv_print_asm(FILE * fp,const uint32_t * words,size_t word_count)35 spirv_print_asm(FILE *fp, const uint32_t *words, size_t word_count)
36 {
37 #ifdef HAVE_SPIRV_TOOLS
38    spv_context ctx = spvContextCreate(SPV_ENV_UNIVERSAL_1_6);
39 
40    spv_binary_to_text_options_t options =
41       SPV_BINARY_TO_TEXT_OPTION_INDENT |
42       SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES;
43 
44    if (MESA_SPIRV_DEBUG(COLOR))
45       options |= SPV_BINARY_TO_TEXT_OPTION_COLOR;
46 
47    spv_text text = NULL;
48    spv_diagnostic diagnostic = NULL;
49    spv_result_t res = spvBinaryToText(ctx, words, word_count, options,
50                                       &text, &diagnostic);
51    if (res == SPV_SUCCESS) {
52       fprintf(fp, "SPIR-V assembly:\n");
53       fwrite(text->str, 1, text->length, fp);
54    } else {
55       fprintf(fp, "Failed to disassemble SPIR-V:\n");
56       spvDiagnosticPrint(diagnostic);
57       spvDiagnosticDestroy(diagnostic);
58    }
59 
60    spvTextDestroy(text);
61 #else
62    fprintf(fp, "Cannot dump SPIR-V assembly. "
63                "You need to build against SPIR-V tools.\n");
64 #endif /* HAVE_SPIRV_TOOLS */
65 }
66