• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 struct clc_libclc {
92    const nir_shader *libclc_nir;
93 };
94 
95 struct clc_libclc *
clc_libclc_new(const struct clc_logger * logger,const struct clc_libclc_options * options)96 clc_libclc_new(const struct clc_logger *logger, const struct clc_libclc_options *options)
97 {
98    struct clc_libclc *ctx = rzalloc(NULL, struct clc_libclc);
99    if (!ctx) {
100       clc_error(logger, "D3D12: failed to allocate a clc_libclc");
101       return NULL;
102    }
103 
104    const struct spirv_to_nir_options libclc_spirv_options = {
105       .environment = NIR_SPIRV_OPENCL,
106       .create_library = true,
107       .constant_addr_format = nir_address_format_32bit_index_offset_pack64,
108       .global_addr_format = nir_address_format_32bit_index_offset_pack64,
109       .shared_addr_format = nir_address_format_32bit_offset_as_64bit,
110       .temp_addr_format = nir_address_format_32bit_offset_as_64bit,
111       .float_controls_execution_mode = FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32,
112       .caps = {
113          .address = true,
114          .float64 = true,
115          .int8 = true,
116          .int16 = true,
117          .int64 = true,
118          .kernel = true,
119       },
120    };
121 
122    glsl_type_singleton_init_or_ref();
123    bool optimize = options && options->optimize;
124    nir_shader *s =
125       nir_load_libclc_shader(64, NULL, &libclc_spirv_options, options->nir_options, optimize);
126    if (!s) {
127       clc_error(logger, "D3D12: spirv_to_nir failed on libclc blob");
128       ralloc_free(ctx);
129       return NULL;
130    }
131 
132    ralloc_steal(ctx, s);
133    ctx->libclc_nir = s;
134 
135    return ctx;
136 }
137 
clc_free_libclc(struct clc_libclc * ctx)138 void clc_free_libclc(struct clc_libclc *ctx)
139 {
140    ralloc_free(ctx);
141    glsl_type_singleton_decref();
142 }
143 
clc_libclc_get_clc_shader(struct clc_libclc * ctx)144 const nir_shader *clc_libclc_get_clc_shader(struct clc_libclc *ctx)
145 {
146    return ctx->libclc_nir;
147 }
148 
clc_libclc_serialize(struct clc_libclc * context,void ** serialized,size_t * serialized_size)149 void clc_libclc_serialize(struct clc_libclc *context,
150                            void **serialized,
151                            size_t *serialized_size)
152 {
153    struct blob tmp;
154    blob_init(&tmp);
155    nir_serialize(&tmp, context->libclc_nir, true);
156 
157    blob_finish_get_buffer(&tmp, serialized, serialized_size);
158 }
159 
clc_libclc_free_serialized(void * serialized)160 void clc_libclc_free_serialized(void *serialized)
161 {
162    free(serialized);
163 }
164 
165 struct clc_libclc *
clc_libclc_deserialize(const void * serialized,size_t serialized_size)166 clc_libclc_deserialize(const void *serialized, size_t serialized_size)
167 {
168    struct clc_libclc *ctx = rzalloc(NULL, struct clc_libclc);
169    if (!ctx) {
170       return NULL;
171    }
172 
173    glsl_type_singleton_init_or_ref();
174 
175    struct blob_reader tmp;
176    blob_reader_init(&tmp, serialized, serialized_size);
177 
178    nir_shader *s = nir_deserialize(NULL, NULL, &tmp);
179    if (!s) {
180       ralloc_free(ctx);
181       return NULL;
182    }
183 
184    ralloc_steal(ctx, s);
185    ctx->libclc_nir = s;
186 
187    return ctx;
188 }
189 
190 bool
clc_compile_c_to_spir(const struct clc_compile_args * args,const struct clc_logger * logger,struct clc_binary * out_spir)191 clc_compile_c_to_spir(const struct clc_compile_args *args,
192                       const struct clc_logger *logger,
193                       struct clc_binary *out_spir)
194 {
195    return clc_c_to_spir(args, logger, out_spir) >= 0;
196 }
197 
198 void
clc_free_spir(struct clc_binary * spir)199 clc_free_spir(struct clc_binary *spir)
200 {
201    clc_free_spir_binary(spir);
202 }
203 
204 bool
clc_compile_spir_to_spirv(const struct clc_binary * in_spir,const struct clc_logger * logger,struct clc_binary * out_spirv)205 clc_compile_spir_to_spirv(const struct clc_binary *in_spir,
206                           const struct clc_logger *logger,
207                           struct clc_binary *out_spirv)
208 {
209    if (clc_spir_to_spirv(in_spir, logger, out_spirv) < 0)
210       return false;
211 
212    if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
213       clc_dump_spirv(out_spirv, stdout);
214 
215    return true;
216 }
217 
218 void
clc_free_spirv(struct clc_binary * spirv)219 clc_free_spirv(struct clc_binary *spirv)
220 {
221    clc_free_spirv_binary(spirv);
222 }
223 
224 bool
clc_compile_c_to_spirv(const struct clc_compile_args * args,const struct clc_logger * logger,struct clc_binary * out_spirv)225 clc_compile_c_to_spirv(const struct clc_compile_args *args,
226                        const struct clc_logger *logger,
227                        struct clc_binary *out_spirv)
228 {
229    if (clc_c_to_spirv(args, logger, out_spirv) < 0)
230       return false;
231 
232    if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
233       clc_dump_spirv(out_spirv, stdout);
234 
235    return true;
236 }
237 
238 bool
clc_link_spirv(const struct clc_linker_args * args,const struct clc_logger * logger,struct clc_binary * out_spirv)239 clc_link_spirv(const struct clc_linker_args *args,
240                const struct clc_logger *logger,
241                struct clc_binary *out_spirv)
242 {
243    if (clc_link_spirv_binaries(args, logger, out_spirv) < 0)
244       return false;
245 
246    if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
247       clc_dump_spirv(out_spirv, stdout);
248 
249    return true;
250 }
251 
252 bool
clc_parse_spirv(const struct clc_binary * in_spirv,const struct clc_logger * logger,struct clc_parsed_spirv * out_data)253 clc_parse_spirv(const struct clc_binary *in_spirv,
254                 const struct clc_logger *logger,
255                 struct clc_parsed_spirv *out_data)
256 {
257    if (!clc_spirv_get_kernels_info(in_spirv,
258       &out_data->kernels,
259       &out_data->num_kernels,
260       &out_data->spec_constants,
261       &out_data->num_spec_constants,
262       logger))
263       return false;
264 
265    if (debug_get_option_debug_clc() & CLC_DEBUG_VERBOSE)
266       clc_print_kernels_info(out_data);
267 
268    return true;
269 }
270 
clc_free_parsed_spirv(struct clc_parsed_spirv * data)271 void clc_free_parsed_spirv(struct clc_parsed_spirv *data)
272 {
273    clc_free_kernels_info(data->kernels, data->num_kernels);
274 }
275 
276 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)277 clc_specialize_spirv(const struct clc_binary *in_spirv,
278                      const struct clc_parsed_spirv *parsed_data,
279                      const struct clc_spirv_specialization_consts *consts,
280                      struct clc_binary *out_spirv)
281 {
282    if (!clc_spirv_specialize(in_spirv, parsed_data, consts, out_spirv))
283       return false;
284 
285    if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
286       clc_dump_spirv(out_spirv, stdout);
287 
288    return true;
289 }
290