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