xref: /aosp_15_r20/external/mesa3d/src/compiler/clc/clc.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © Microsoft Corporation
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 
24 #include "nir/nir.h"
25 #include "nir/nir_serialize.h"
26 #include "glsl_types.h"
27 #include "clc.h"
28 #include "clc_helpers.h"
29 #include "nir_clc_helpers.h"
30 #include "spirv/nir_spirv.h"
31 #include "util/u_debug.h"
32 
33 #include <stdlib.h>
34 
35 static const struct debug_named_value clc_debug_options[] = {
36    { "dump_spirv",  CLC_DEBUG_DUMP_SPIRV, "Dump spirv blobs" },
37    { "dump_llvm",  CLC_DEBUG_DUMP_LLVM, "Dump LLVM blobs" },
38    { "verbose",  CLC_DEBUG_VERBOSE, NULL },
39    DEBUG_NAMED_VALUE_END
40 };
41 
42 DEBUG_GET_ONCE_FLAGS_OPTION(debug_clc, "CLC_DEBUG", clc_debug_options, 0)
43 
clc_debug_flags(void)44 uint64_t clc_debug_flags(void)
45 {
46    return debug_get_option_debug_clc();
47 }
48 
49 static void
clc_print_kernels_info(const struct clc_parsed_spirv * obj)50 clc_print_kernels_info(const struct clc_parsed_spirv *obj)
51 {
52    fprintf(stdout, "Kernels:\n");
53    for (unsigned i = 0; i < obj->num_kernels; i++) {
54       const struct clc_kernel_arg *args = obj->kernels[i].args;
55       bool first = true;
56 
57       fprintf(stdout, "\tvoid %s(", obj->kernels[i].name);
58       for (unsigned j = 0; j < obj->kernels[i].num_args; j++) {
59          if (!first)
60             fprintf(stdout, ", ");
61          else
62             first = false;
63 
64          switch (args[j].address_qualifier) {
65          case CLC_KERNEL_ARG_ADDRESS_GLOBAL:
66             fprintf(stdout, "__global ");
67             break;
68          case CLC_KERNEL_ARG_ADDRESS_LOCAL:
69             fprintf(stdout, "__local ");
70             break;
71          case CLC_KERNEL_ARG_ADDRESS_CONSTANT:
72             fprintf(stdout, "__constant ");
73             break;
74          default:
75             break;
76          }
77 
78          if (args[j].type_qualifier & CLC_KERNEL_ARG_TYPE_VOLATILE)
79             fprintf(stdout, "volatile ");
80          if (args[j].type_qualifier & CLC_KERNEL_ARG_TYPE_CONST)
81             fprintf(stdout, "const ");
82          if (args[j].type_qualifier & CLC_KERNEL_ARG_TYPE_RESTRICT)
83             fprintf(stdout, "restrict ");
84 
85          fprintf(stdout, "%s %s", args[j].type_name, args[j].name);
86       }
87       fprintf(stdout, ");\n");
88    }
89 }
90 
91 bool
clc_compile_c_to_spir(const struct clc_compile_args * args,const struct clc_logger * logger,struct clc_binary * out_spir)92 clc_compile_c_to_spir(const struct clc_compile_args *args,
93                       const struct clc_logger *logger,
94                       struct clc_binary *out_spir)
95 {
96    return clc_c_to_spir(args, logger, out_spir) >= 0;
97 }
98 
99 void
clc_free_spir(struct clc_binary * spir)100 clc_free_spir(struct clc_binary *spir)
101 {
102    clc_free_spir_binary(spir);
103 }
104 
105 bool
clc_compile_spir_to_spirv(const struct clc_binary * in_spir,const struct clc_logger * logger,struct clc_binary * out_spirv)106 clc_compile_spir_to_spirv(const struct clc_binary *in_spir,
107                           const struct clc_logger *logger,
108                           struct clc_binary *out_spirv)
109 {
110    if (clc_spir_to_spirv(in_spir, logger, out_spirv) < 0)
111       return false;
112 
113    if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
114       clc_dump_spirv(out_spirv, stdout);
115 
116    return true;
117 }
118 
119 void
clc_free_spirv(struct clc_binary * spirv)120 clc_free_spirv(struct clc_binary *spirv)
121 {
122    clc_free_spirv_binary(spirv);
123 }
124 
125 bool
clc_compile_c_to_spirv(const struct clc_compile_args * args,const struct clc_logger * logger,struct clc_binary * out_spirv)126 clc_compile_c_to_spirv(const struct clc_compile_args *args,
127                        const struct clc_logger *logger,
128                        struct clc_binary *out_spirv)
129 {
130    if (clc_c_to_spirv(args, logger, out_spirv) < 0)
131       return false;
132 
133    if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
134       clc_dump_spirv(out_spirv, stdout);
135 
136    return true;
137 }
138 
139 bool
clc_link_spirv(const struct clc_linker_args * args,const struct clc_logger * logger,struct clc_binary * out_spirv)140 clc_link_spirv(const struct clc_linker_args *args,
141                const struct clc_logger *logger,
142                struct clc_binary *out_spirv)
143 {
144    if (clc_link_spirv_binaries(args, logger, out_spirv) < 0)
145       return false;
146 
147    if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
148       clc_dump_spirv(out_spirv, stdout);
149 
150    return true;
151 }
152 
153 bool
clc_parse_spirv(const struct clc_binary * in_spirv,const struct clc_logger * logger,struct clc_parsed_spirv * out_data)154 clc_parse_spirv(const struct clc_binary *in_spirv,
155                 const struct clc_logger *logger,
156                 struct clc_parsed_spirv *out_data)
157 {
158    if (!clc_spirv_get_kernels_info(in_spirv,
159       &out_data->kernels,
160       &out_data->num_kernels,
161       &out_data->spec_constants,
162       &out_data->num_spec_constants,
163       logger))
164       return false;
165 
166    if (debug_get_option_debug_clc() & CLC_DEBUG_VERBOSE)
167       clc_print_kernels_info(out_data);
168 
169    return true;
170 }
171 
clc_free_parsed_spirv(struct clc_parsed_spirv * data)172 void clc_free_parsed_spirv(struct clc_parsed_spirv *data)
173 {
174    clc_free_kernels_info(data->kernels, data->num_kernels);
175 }
176 
177 bool
clc_specialize_spirv(const struct clc_binary * in_spirv,const struct clc_parsed_spirv * parsed_data,const struct clc_spirv_specialization_consts * consts,struct clc_binary * out_spirv)178 clc_specialize_spirv(const struct clc_binary *in_spirv,
179                      const struct clc_parsed_spirv *parsed_data,
180                      const struct clc_spirv_specialization_consts *consts,
181                      struct clc_binary *out_spirv)
182 {
183    if (!clc_spirv_specialize(in_spirv, parsed_data, consts, out_spirv))
184       return false;
185 
186    if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
187       clc_dump_spirv(out_spirv, stdout);
188 
189    return true;
190 }
191