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,struct set * dependencies)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 struct set *dependencies)
96 {
97 return clc_c_to_spir(args, logger, out_spir, dependencies) >= 0;
98 }
99
100 void
clc_free_spir(struct clc_binary * spir)101 clc_free_spir(struct clc_binary *spir)
102 {
103 clc_free_spir_binary(spir);
104 }
105
106 bool
clc_compile_spir_to_spirv(const struct clc_binary * in_spir,const struct clc_logger * logger,struct clc_binary * out_spirv)107 clc_compile_spir_to_spirv(const struct clc_binary *in_spir,
108 const struct clc_logger *logger,
109 struct clc_binary *out_spirv)
110 {
111 if (clc_spir_to_spirv(in_spir, logger, out_spirv) < 0)
112 return false;
113
114 if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
115 clc_dump_spirv(out_spirv, stdout);
116
117 return true;
118 }
119
120 void
clc_free_spirv(struct clc_binary * spirv)121 clc_free_spirv(struct clc_binary *spirv)
122 {
123 clc_free_spirv_binary(spirv);
124 }
125
126 bool
clc_compile_c_to_spirv(const struct clc_compile_args * args,const struct clc_logger * logger,struct clc_binary * out_spirv,struct set * dependencies)127 clc_compile_c_to_spirv(const struct clc_compile_args *args,
128 const struct clc_logger *logger,
129 struct clc_binary *out_spirv,
130 struct set *dependencies)
131 {
132 if (clc_c_to_spirv(args, logger, out_spirv, dependencies) < 0)
133 return false;
134
135 if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
136 clc_dump_spirv(out_spirv, stdout);
137
138 return true;
139 }
140
141 bool
clc_link_spirv(const struct clc_linker_args * args,const struct clc_logger * logger,struct clc_binary * out_spirv)142 clc_link_spirv(const struct clc_linker_args *args,
143 const struct clc_logger *logger,
144 struct clc_binary *out_spirv)
145 {
146 if (clc_link_spirv_binaries(args, logger, out_spirv) < 0)
147 return false;
148
149 if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
150 clc_dump_spirv(out_spirv, stdout);
151
152 return true;
153 }
154
155 bool
clc_parse_spirv(const struct clc_binary * in_spirv,const struct clc_logger * logger,struct clc_parsed_spirv * out_data)156 clc_parse_spirv(const struct clc_binary *in_spirv,
157 const struct clc_logger *logger,
158 struct clc_parsed_spirv *out_data)
159 {
160 if (!clc_spirv_get_kernels_info(in_spirv,
161 &out_data->kernels,
162 &out_data->num_kernels,
163 &out_data->spec_constants,
164 &out_data->num_spec_constants,
165 logger))
166 return false;
167
168 if (debug_get_option_debug_clc() & CLC_DEBUG_VERBOSE)
169 clc_print_kernels_info(out_data);
170
171 return true;
172 }
173
clc_free_parsed_spirv(struct clc_parsed_spirv * data)174 void clc_free_parsed_spirv(struct clc_parsed_spirv *data)
175 {
176 clc_free_kernels_info(data->kernels, data->num_kernels);
177 }
178
179 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)180 clc_specialize_spirv(const struct clc_binary *in_spirv,
181 const struct clc_parsed_spirv *parsed_data,
182 const struct clc_spirv_specialization_consts *consts,
183 struct clc_binary *out_spirv)
184 {
185 if (!clc_spirv_specialize(in_spirv, parsed_data, consts, out_spirv))
186 return false;
187
188 if (debug_get_option_debug_clc() & CLC_DEBUG_DUMP_SPIRV)
189 clc_dump_spirv(out_spirv, stdout);
190
191 return true;
192 }
193