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