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
32extern "C" {
33#endif
34
35typedef struct nir_shader nir_shader;
36struct nir_shader_compiler_options;
37
38struct clc_named_value {
39   const char *name;
40   const char *value;
41};
42
43enum 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
52struct clc_compile_args {
53   const struct clc_named_value *headers;
54   unsigned num_headers;
55   struct clc_named_value source;
56   const char * const *args;
57   unsigned num_args;
58
59   /* SPIRV version to target. */
60   enum clc_spirv_version spirv_version;
61
62   /* Allowed extensions SPIRV extensions the OpenCL->SPIRV translation can
63    * enable. A pointer to a NULL terminated array of strings, allow any
64    * extension if NULL.
65    */
66   const char * const *allowed_spirv_extensions;
67};
68
69struct clc_binary {
70   void *data;
71   size_t size;
72};
73
74struct clc_linker_args {
75   const struct clc_binary * const *in_objs;
76   unsigned num_in_objs;
77   unsigned create_library;
78};
79
80typedef void (*clc_msg_callback)(void *priv, const char *msg);
81
82struct clc_logger {
83   void *priv;
84   clc_msg_callback error;
85   clc_msg_callback warning;
86};
87
88enum clc_kernel_arg_type_qualifier {
89   CLC_KERNEL_ARG_TYPE_CONST = 1 << 0,
90   CLC_KERNEL_ARG_TYPE_RESTRICT = 1 << 1,
91   CLC_KERNEL_ARG_TYPE_VOLATILE = 1 << 2,
92};
93
94enum clc_kernel_arg_access_qualifier {
95   CLC_KERNEL_ARG_ACCESS_READ = 1 << 0,
96   CLC_KERNEL_ARG_ACCESS_WRITE = 1 << 1,
97};
98
99enum clc_kernel_arg_address_qualifier {
100   CLC_KERNEL_ARG_ADDRESS_PRIVATE,
101   CLC_KERNEL_ARG_ADDRESS_CONSTANT,
102   CLC_KERNEL_ARG_ADDRESS_LOCAL,
103   CLC_KERNEL_ARG_ADDRESS_GLOBAL,
104};
105
106struct clc_kernel_arg {
107   const char *name;
108   const char *type_name;
109   unsigned type_qualifier;
110   unsigned access_qualifier;
111   enum clc_kernel_arg_address_qualifier address_qualifier;
112};
113
114enum clc_vec_hint_type {
115   CLC_VEC_HINT_TYPE_CHAR = 0,
116   CLC_VEC_HINT_TYPE_SHORT = 1,
117   CLC_VEC_HINT_TYPE_INT = 2,
118   CLC_VEC_HINT_TYPE_LONG = 3,
119   CLC_VEC_HINT_TYPE_HALF = 4,
120   CLC_VEC_HINT_TYPE_FLOAT = 5,
121   CLC_VEC_HINT_TYPE_DOUBLE = 6
122};
123
124struct clc_kernel_info {
125   const char *name;
126   size_t num_args;
127   const struct clc_kernel_arg *args;
128
129   unsigned vec_hint_size;
130   enum clc_vec_hint_type vec_hint_type;
131};
132
133enum clc_spec_constant_type {
134   CLC_SPEC_CONSTANT_UNKNOWN,
135   CLC_SPEC_CONSTANT_BOOL,
136   CLC_SPEC_CONSTANT_FLOAT,
137   CLC_SPEC_CONSTANT_DOUBLE,
138   CLC_SPEC_CONSTANT_INT8,
139   CLC_SPEC_CONSTANT_UINT8,
140   CLC_SPEC_CONSTANT_INT16,
141   CLC_SPEC_CONSTANT_UINT16,
142   CLC_SPEC_CONSTANT_INT32,
143   CLC_SPEC_CONSTANT_UINT32,
144   CLC_SPEC_CONSTANT_INT64,
145   CLC_SPEC_CONSTANT_UINT64,
146};
147
148struct clc_parsed_spec_constant {
149   uint32_t id;
150   enum clc_spec_constant_type type;
151};
152
153struct clc_parsed_spirv {
154   const struct clc_kernel_info *kernels;
155   unsigned num_kernels;
156
157   const struct clc_parsed_spec_constant *spec_constants;
158   unsigned num_spec_constants;
159};
160
161struct clc_libclc;
162
163struct clc_libclc_options {
164   unsigned optimize;
165   const struct nir_shader_compiler_options *nir_options;
166};
167
168struct clc_libclc *clc_libclc_new(const struct clc_logger *logger, const struct clc_libclc_options *options);
169
170void clc_free_libclc(struct clc_libclc *lib);
171
172const nir_shader *clc_libclc_get_clc_shader(struct clc_libclc *lib);
173
174void clc_libclc_serialize(struct clc_libclc *lib, void **serialized, size_t *size);
175void clc_libclc_free_serialized(void *serialized);
176struct clc_libclc *clc_libclc_deserialize(const void *serialized, size_t size);
177
178bool
179clc_compile_c_to_spir(const struct clc_compile_args *args,
180                      const struct clc_logger *logger,
181                      struct clc_binary *out_spir);
182
183void
184clc_free_spir(struct clc_binary *spir);
185
186bool
187clc_compile_spir_to_spirv(const struct clc_binary *in_spir,
188                          const struct clc_logger *logger,
189                          struct clc_binary *out_spirv);
190
191void
192clc_free_spirv(struct clc_binary *spirv);
193
194bool
195clc_compile_c_to_spirv(const struct clc_compile_args *args,
196                       const struct clc_logger *logger,
197                       struct clc_binary *out_spirv);
198
199bool
200clc_link_spirv(const struct clc_linker_args *args,
201               const struct clc_logger *logger,
202               struct clc_binary *out_spirv);
203
204bool
205clc_parse_spirv(const struct clc_binary *in_spirv,
206                const struct clc_logger *logger,
207                struct clc_parsed_spirv *out_data);
208
209void
210clc_free_parsed_spirv(struct clc_parsed_spirv *data);
211
212typedef union {
213   bool b;
214   float f32;
215   double f64;
216   int8_t i8;
217   uint8_t u8;
218   int16_t i16;
219   uint16_t u16;
220   int32_t i32;
221   uint32_t u32;
222   int64_t i64;
223   uint64_t u64;
224} clc_spirv_const_value;
225
226struct clc_spirv_specialization {
227   uint32_t id;
228   clc_spirv_const_value value;
229   bool defined_on_module;
230};
231
232struct clc_spirv_specialization_consts {
233   const struct clc_spirv_specialization *specializations;
234   unsigned num_specializations;
235};
236
237bool
238clc_specialize_spirv(const struct clc_binary *in_spirv,
239                     const struct clc_parsed_spirv *parsed_data,
240                     const struct clc_spirv_specialization_consts *consts,
241                     struct clc_binary *out_spirv);
242
243#ifdef __cplusplus
244}
245#endif
246
247#endif /* MESA_CLC_H */
248