• 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 #ifndef MESA_CLC_H
25 #define MESA_CLC_H
26 
27 #include <stdbool.h>
28 #include <stddef.h>
29 #include <stdint.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 typedef struct nir_shader nir_shader;
36 struct nir_shader_compiler_options;
37 
38 struct clc_named_value {
39    const char *name;
40    const char *value;
41 };
42 
43 enum clc_spirv_version {
44    CLC_SPIRV_VERSION_MAX = 0,
45    CLC_SPIRV_VERSION_1_0,
46    CLC_SPIRV_VERSION_1_1,
47    CLC_SPIRV_VERSION_1_2,
48    CLC_SPIRV_VERSION_1_3,
49    CLC_SPIRV_VERSION_1_4,
50 };
51 
52 struct clc_optional_features {
53    bool fp16;
54    bool fp64;
55    bool int64;
56    bool images;
57    bool images_depth;
58    bool images_gl_depth;
59    bool images_gl_msaa;
60    bool images_mipmap;
61    bool images_mipmap_writes;
62    bool images_read_write;
63    bool images_write_3d;
64    bool integer_dot_product;
65    bool intel_subgroups;
66    /* OpenCL core subgroups */
67    bool subgroups;
68    /* OpenCL extension cl_khr_subgroups, which requires independent forward
69     * progress
70     */
71    bool subgroups_ifp;
72    bool subgroups_shuffle;
73    bool subgroups_shuffle_relative;
74    bool subgroups_ballot;
75 };
76 
77 struct clc_compile_args {
78    const struct clc_named_value *headers;
79    unsigned num_headers;
80    struct clc_named_value source;
81    const char * const *args;
82    unsigned num_args;
83 
84    /* SPIRV version to target. */
85    enum clc_spirv_version spirv_version;
86    struct clc_optional_features features;
87    bool use_llvm_spirv_target;
88 
89    /* Allowed extensions SPIRV extensions the OpenCL->SPIRV translation can
90     * enable. A pointer to a NULL terminated array of strings, allow any
91     * extension if NULL.
92     */
93    const char * const *allowed_spirv_extensions;
94 
95    unsigned address_bits;
96 };
97 
98 struct clc_validator_options {
99    uint32_t limit_max_function_arg;
100 };
101 
102 struct clc_binary {
103    void *data;
104    size_t size;
105 };
106 
107 struct clc_linker_args {
108    const struct clc_binary * const *in_objs;
109    unsigned num_in_objs;
110    unsigned create_library;
111 };
112 
113 typedef void (*clc_msg_callback)(void *priv, const char *msg);
114 
115 struct clc_logger {
116    void *priv;
117    clc_msg_callback error;
118    clc_msg_callback warning;
119 };
120 
121 enum clc_kernel_arg_type_qualifier {
122    CLC_KERNEL_ARG_TYPE_CONST = 1 << 0,
123    CLC_KERNEL_ARG_TYPE_RESTRICT = 1 << 1,
124    CLC_KERNEL_ARG_TYPE_VOLATILE = 1 << 2,
125 };
126 
127 enum clc_kernel_arg_access_qualifier {
128    CLC_KERNEL_ARG_ACCESS_READ = 1 << 0,
129    CLC_KERNEL_ARG_ACCESS_WRITE = 1 << 1,
130 };
131 
132 enum clc_kernel_arg_address_qualifier {
133    CLC_KERNEL_ARG_ADDRESS_PRIVATE,
134    CLC_KERNEL_ARG_ADDRESS_CONSTANT,
135    CLC_KERNEL_ARG_ADDRESS_LOCAL,
136    CLC_KERNEL_ARG_ADDRESS_GLOBAL,
137 };
138 
139 struct clc_kernel_arg {
140    const char *name;
141    const char *type_name;
142    unsigned type_qualifier;
143    unsigned access_qualifier;
144    enum clc_kernel_arg_address_qualifier address_qualifier;
145 };
146 
147 enum clc_vec_hint_type {
148    CLC_VEC_HINT_TYPE_CHAR = 0,
149    CLC_VEC_HINT_TYPE_SHORT = 1,
150    CLC_VEC_HINT_TYPE_INT = 2,
151    CLC_VEC_HINT_TYPE_LONG = 3,
152    CLC_VEC_HINT_TYPE_HALF = 4,
153    CLC_VEC_HINT_TYPE_FLOAT = 5,
154    CLC_VEC_HINT_TYPE_DOUBLE = 6
155 };
156 
157 struct clc_kernel_info {
158    const char *name;
159    size_t num_args;
160    const struct clc_kernel_arg *args;
161 
162    unsigned vec_hint_size;
163    enum clc_vec_hint_type vec_hint_type;
164 
165    unsigned local_size[3];
166    unsigned local_size_hint[3];
167 };
168 
169 enum clc_spec_constant_type {
170    CLC_SPEC_CONSTANT_UNKNOWN,
171    CLC_SPEC_CONSTANT_BOOL,
172    CLC_SPEC_CONSTANT_FLOAT,
173    CLC_SPEC_CONSTANT_DOUBLE,
174    CLC_SPEC_CONSTANT_INT8,
175    CLC_SPEC_CONSTANT_UINT8,
176    CLC_SPEC_CONSTANT_INT16,
177    CLC_SPEC_CONSTANT_UINT16,
178    CLC_SPEC_CONSTANT_INT32,
179    CLC_SPEC_CONSTANT_UINT32,
180    CLC_SPEC_CONSTANT_INT64,
181    CLC_SPEC_CONSTANT_UINT64,
182 };
183 
184 struct clc_parsed_spec_constant {
185    uint32_t id;
186    enum clc_spec_constant_type type;
187 };
188 
189 struct clc_parsed_spirv {
190    const struct clc_kernel_info *kernels;
191    unsigned num_kernels;
192 
193    const struct clc_parsed_spec_constant *spec_constants;
194    unsigned num_spec_constants;
195 };
196 
197 struct clc_libclc;
198 
199 struct clc_libclc_options {
200    unsigned optimize;
201    const struct nir_shader_compiler_options *nir_options;
202 };
203 
204 struct clc_libclc *clc_libclc_new(const struct clc_logger *logger, const struct clc_libclc_options *options);
205 
206 void clc_free_libclc(struct clc_libclc *lib);
207 
208 const nir_shader *clc_libclc_get_clc_shader(struct clc_libclc *lib);
209 
210 void clc_libclc_serialize(struct clc_libclc *lib, void **serialized, size_t *size);
211 void clc_libclc_free_serialized(void *serialized);
212 struct clc_libclc *clc_libclc_deserialize(const void *serialized, size_t size);
213 
214 /* Forward declare */
215 struct set;
216 
217 bool
218 clc_compile_c_to_spir(const struct clc_compile_args *args,
219                       const struct clc_logger *logger,
220                       struct clc_binary *out_spir,
221                       struct set *dependencies);
222 
223 void
224 clc_free_spir(struct clc_binary *spir);
225 
226 bool
227 clc_compile_spir_to_spirv(const struct clc_binary *in_spir,
228                           const struct clc_logger *logger,
229                           struct clc_binary *out_spirv);
230 
231 void
232 clc_free_spirv(struct clc_binary *spirv);
233 
234 bool
235 clc_compile_c_to_spirv(const struct clc_compile_args *args,
236                        const struct clc_logger *logger,
237                        struct clc_binary *out_spirv,
238                        struct set *dependencies);
239 
240 bool
241 clc_link_spirv(const struct clc_linker_args *args,
242                const struct clc_logger *logger,
243                struct clc_binary *out_spirv);
244 
245 bool
246 clc_parse_spirv(const struct clc_binary *in_spirv,
247                 const struct clc_logger *logger,
248                 struct clc_parsed_spirv *out_data);
249 
250 void
251 clc_free_parsed_spirv(struct clc_parsed_spirv *data);
252 
253 typedef union {
254    bool b;
255    float f32;
256    double f64;
257    int8_t i8;
258    uint8_t u8;
259    int16_t i16;
260    uint16_t u16;
261    int32_t i32;
262    uint32_t u32;
263    int64_t i64;
264    uint64_t u64;
265 } clc_spirv_const_value;
266 
267 struct clc_spirv_specialization {
268    uint32_t id;
269    clc_spirv_const_value value;
270    bool defined_on_module;
271 };
272 
273 struct clc_spirv_specialization_consts {
274    const struct clc_spirv_specialization *specializations;
275    unsigned num_specializations;
276 };
277 
278 bool
279 clc_specialize_spirv(const struct clc_binary *in_spirv,
280                      const struct clc_parsed_spirv *parsed_data,
281                      const struct clc_spirv_specialization_consts *consts,
282                      struct clc_binary *out_spirv);
283 
284 enum clc_debug_flags {
285    CLC_DEBUG_DUMP_SPIRV = 1 << 0,
286    CLC_DEBUG_DUMP_LLVM = 1 << 1,
287    CLC_DEBUG_VERBOSE = 1 << 2,
288 };
289 uint64_t clc_debug_flags(void);
290 
291 #ifdef __cplusplus
292 }
293 #endif
294 
295 #endif /* MESA_CLC_H */
296