xref: /third_party/mesa3d/src/compiler/clc/clc.h (revision bf215546)
1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © Microsoft Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#ifndef MESA_CLC_H
25bf215546Sopenharmony_ci#define MESA_CLC_H
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <stdbool.h>
28bf215546Sopenharmony_ci#include <stddef.h>
29bf215546Sopenharmony_ci#include <stdint.h>
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#ifdef __cplusplus
32bf215546Sopenharmony_ciextern "C" {
33bf215546Sopenharmony_ci#endif
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_citypedef struct nir_shader nir_shader;
36bf215546Sopenharmony_cistruct nir_shader_compiler_options;
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistruct clc_named_value {
39bf215546Sopenharmony_ci   const char *name;
40bf215546Sopenharmony_ci   const char *value;
41bf215546Sopenharmony_ci};
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_cienum clc_spirv_version {
44bf215546Sopenharmony_ci   CLC_SPIRV_VERSION_MAX = 0,
45bf215546Sopenharmony_ci   CLC_SPIRV_VERSION_1_0,
46bf215546Sopenharmony_ci   CLC_SPIRV_VERSION_1_1,
47bf215546Sopenharmony_ci   CLC_SPIRV_VERSION_1_2,
48bf215546Sopenharmony_ci   CLC_SPIRV_VERSION_1_3,
49bf215546Sopenharmony_ci   CLC_SPIRV_VERSION_1_4,
50bf215546Sopenharmony_ci};
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_cistruct clc_optional_features {
53bf215546Sopenharmony_ci   bool fp16;
54bf215546Sopenharmony_ci   bool fp64;
55bf215546Sopenharmony_ci   bool int64;
56bf215546Sopenharmony_ci   bool images;
57bf215546Sopenharmony_ci   bool images_read_write;
58bf215546Sopenharmony_ci   bool images_write_3d;
59bf215546Sopenharmony_ci   bool intel_subgroups;
60bf215546Sopenharmony_ci   bool subgroups;
61bf215546Sopenharmony_ci};
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_cistruct clc_compile_args {
64bf215546Sopenharmony_ci   const struct clc_named_value *headers;
65bf215546Sopenharmony_ci   unsigned num_headers;
66bf215546Sopenharmony_ci   struct clc_named_value source;
67bf215546Sopenharmony_ci   const char * const *args;
68bf215546Sopenharmony_ci   unsigned num_args;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   /* SPIRV version to target. */
71bf215546Sopenharmony_ci   enum clc_spirv_version spirv_version;
72bf215546Sopenharmony_ci   struct clc_optional_features features;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   /* Allowed extensions SPIRV extensions the OpenCL->SPIRV translation can
75bf215546Sopenharmony_ci    * enable. A pointer to a NULL terminated array of strings, allow any
76bf215546Sopenharmony_ci    * extension if NULL.
77bf215546Sopenharmony_ci    */
78bf215546Sopenharmony_ci   const char * const *allowed_spirv_extensions;
79bf215546Sopenharmony_ci};
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_cistruct clc_binary {
82bf215546Sopenharmony_ci   void *data;
83bf215546Sopenharmony_ci   size_t size;
84bf215546Sopenharmony_ci};
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_cistruct clc_linker_args {
87bf215546Sopenharmony_ci   const struct clc_binary * const *in_objs;
88bf215546Sopenharmony_ci   unsigned num_in_objs;
89bf215546Sopenharmony_ci   unsigned create_library;
90bf215546Sopenharmony_ci};
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_citypedef void (*clc_msg_callback)(void *priv, const char *msg);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_cistruct clc_logger {
95bf215546Sopenharmony_ci   void *priv;
96bf215546Sopenharmony_ci   clc_msg_callback error;
97bf215546Sopenharmony_ci   clc_msg_callback warning;
98bf215546Sopenharmony_ci};
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_cienum clc_kernel_arg_type_qualifier {
101bf215546Sopenharmony_ci   CLC_KERNEL_ARG_TYPE_CONST = 1 << 0,
102bf215546Sopenharmony_ci   CLC_KERNEL_ARG_TYPE_RESTRICT = 1 << 1,
103bf215546Sopenharmony_ci   CLC_KERNEL_ARG_TYPE_VOLATILE = 1 << 2,
104bf215546Sopenharmony_ci};
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_cienum clc_kernel_arg_access_qualifier {
107bf215546Sopenharmony_ci   CLC_KERNEL_ARG_ACCESS_READ = 1 << 0,
108bf215546Sopenharmony_ci   CLC_KERNEL_ARG_ACCESS_WRITE = 1 << 1,
109bf215546Sopenharmony_ci};
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_cienum clc_kernel_arg_address_qualifier {
112bf215546Sopenharmony_ci   CLC_KERNEL_ARG_ADDRESS_PRIVATE,
113bf215546Sopenharmony_ci   CLC_KERNEL_ARG_ADDRESS_CONSTANT,
114bf215546Sopenharmony_ci   CLC_KERNEL_ARG_ADDRESS_LOCAL,
115bf215546Sopenharmony_ci   CLC_KERNEL_ARG_ADDRESS_GLOBAL,
116bf215546Sopenharmony_ci};
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_cistruct clc_kernel_arg {
119bf215546Sopenharmony_ci   const char *name;
120bf215546Sopenharmony_ci   const char *type_name;
121bf215546Sopenharmony_ci   unsigned type_qualifier;
122bf215546Sopenharmony_ci   unsigned access_qualifier;
123bf215546Sopenharmony_ci   enum clc_kernel_arg_address_qualifier address_qualifier;
124bf215546Sopenharmony_ci};
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_cienum clc_vec_hint_type {
127bf215546Sopenharmony_ci   CLC_VEC_HINT_TYPE_CHAR = 0,
128bf215546Sopenharmony_ci   CLC_VEC_HINT_TYPE_SHORT = 1,
129bf215546Sopenharmony_ci   CLC_VEC_HINT_TYPE_INT = 2,
130bf215546Sopenharmony_ci   CLC_VEC_HINT_TYPE_LONG = 3,
131bf215546Sopenharmony_ci   CLC_VEC_HINT_TYPE_HALF = 4,
132bf215546Sopenharmony_ci   CLC_VEC_HINT_TYPE_FLOAT = 5,
133bf215546Sopenharmony_ci   CLC_VEC_HINT_TYPE_DOUBLE = 6
134bf215546Sopenharmony_ci};
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_cistruct clc_kernel_info {
137bf215546Sopenharmony_ci   const char *name;
138bf215546Sopenharmony_ci   size_t num_args;
139bf215546Sopenharmony_ci   const struct clc_kernel_arg *args;
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   unsigned vec_hint_size;
142bf215546Sopenharmony_ci   enum clc_vec_hint_type vec_hint_type;
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   unsigned local_size[3];
145bf215546Sopenharmony_ci   unsigned local_size_hint[3];
146bf215546Sopenharmony_ci};
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_cienum clc_spec_constant_type {
149bf215546Sopenharmony_ci   CLC_SPEC_CONSTANT_UNKNOWN,
150bf215546Sopenharmony_ci   CLC_SPEC_CONSTANT_BOOL,
151bf215546Sopenharmony_ci   CLC_SPEC_CONSTANT_FLOAT,
152bf215546Sopenharmony_ci   CLC_SPEC_CONSTANT_DOUBLE,
153bf215546Sopenharmony_ci   CLC_SPEC_CONSTANT_INT8,
154bf215546Sopenharmony_ci   CLC_SPEC_CONSTANT_UINT8,
155bf215546Sopenharmony_ci   CLC_SPEC_CONSTANT_INT16,
156bf215546Sopenharmony_ci   CLC_SPEC_CONSTANT_UINT16,
157bf215546Sopenharmony_ci   CLC_SPEC_CONSTANT_INT32,
158bf215546Sopenharmony_ci   CLC_SPEC_CONSTANT_UINT32,
159bf215546Sopenharmony_ci   CLC_SPEC_CONSTANT_INT64,
160bf215546Sopenharmony_ci   CLC_SPEC_CONSTANT_UINT64,
161bf215546Sopenharmony_ci};
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_cistruct clc_parsed_spec_constant {
164bf215546Sopenharmony_ci   uint32_t id;
165bf215546Sopenharmony_ci   enum clc_spec_constant_type type;
166bf215546Sopenharmony_ci};
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_cistruct clc_parsed_spirv {
169bf215546Sopenharmony_ci   const struct clc_kernel_info *kernels;
170bf215546Sopenharmony_ci   unsigned num_kernels;
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci   const struct clc_parsed_spec_constant *spec_constants;
173bf215546Sopenharmony_ci   unsigned num_spec_constants;
174bf215546Sopenharmony_ci};
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_cistruct clc_libclc;
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_cistruct clc_libclc_options {
179bf215546Sopenharmony_ci   unsigned optimize;
180bf215546Sopenharmony_ci   const struct nir_shader_compiler_options *nir_options;
181bf215546Sopenharmony_ci};
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_cistruct clc_libclc *clc_libclc_new(const struct clc_logger *logger, const struct clc_libclc_options *options);
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_civoid clc_free_libclc(struct clc_libclc *lib);
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ciconst nir_shader *clc_libclc_get_clc_shader(struct clc_libclc *lib);
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_civoid clc_libclc_serialize(struct clc_libclc *lib, void **serialized, size_t *size);
190bf215546Sopenharmony_civoid clc_libclc_free_serialized(void *serialized);
191bf215546Sopenharmony_cistruct clc_libclc *clc_libclc_deserialize(const void *serialized, size_t size);
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_cibool
194bf215546Sopenharmony_ciclc_compile_c_to_spir(const struct clc_compile_args *args,
195bf215546Sopenharmony_ci                      const struct clc_logger *logger,
196bf215546Sopenharmony_ci                      struct clc_binary *out_spir);
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_civoid
199bf215546Sopenharmony_ciclc_free_spir(struct clc_binary *spir);
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_cibool
202bf215546Sopenharmony_ciclc_compile_spir_to_spirv(const struct clc_binary *in_spir,
203bf215546Sopenharmony_ci                          const struct clc_logger *logger,
204bf215546Sopenharmony_ci                          struct clc_binary *out_spirv);
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_civoid
207bf215546Sopenharmony_ciclc_free_spirv(struct clc_binary *spirv);
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_cibool
210bf215546Sopenharmony_ciclc_compile_c_to_spirv(const struct clc_compile_args *args,
211bf215546Sopenharmony_ci                       const struct clc_logger *logger,
212bf215546Sopenharmony_ci                       struct clc_binary *out_spirv);
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_cibool
215bf215546Sopenharmony_ciclc_link_spirv(const struct clc_linker_args *args,
216bf215546Sopenharmony_ci               const struct clc_logger *logger,
217bf215546Sopenharmony_ci               struct clc_binary *out_spirv);
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_cibool
220bf215546Sopenharmony_ciclc_parse_spirv(const struct clc_binary *in_spirv,
221bf215546Sopenharmony_ci                const struct clc_logger *logger,
222bf215546Sopenharmony_ci                struct clc_parsed_spirv *out_data);
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_civoid
225bf215546Sopenharmony_ciclc_free_parsed_spirv(struct clc_parsed_spirv *data);
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_citypedef union {
228bf215546Sopenharmony_ci   bool b;
229bf215546Sopenharmony_ci   float f32;
230bf215546Sopenharmony_ci   double f64;
231bf215546Sopenharmony_ci   int8_t i8;
232bf215546Sopenharmony_ci   uint8_t u8;
233bf215546Sopenharmony_ci   int16_t i16;
234bf215546Sopenharmony_ci   uint16_t u16;
235bf215546Sopenharmony_ci   int32_t i32;
236bf215546Sopenharmony_ci   uint32_t u32;
237bf215546Sopenharmony_ci   int64_t i64;
238bf215546Sopenharmony_ci   uint64_t u64;
239bf215546Sopenharmony_ci} clc_spirv_const_value;
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_cistruct clc_spirv_specialization {
242bf215546Sopenharmony_ci   uint32_t id;
243bf215546Sopenharmony_ci   clc_spirv_const_value value;
244bf215546Sopenharmony_ci   bool defined_on_module;
245bf215546Sopenharmony_ci};
246bf215546Sopenharmony_ci
247bf215546Sopenharmony_cistruct clc_spirv_specialization_consts {
248bf215546Sopenharmony_ci   const struct clc_spirv_specialization *specializations;
249bf215546Sopenharmony_ci   unsigned num_specializations;
250bf215546Sopenharmony_ci};
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_cibool
253bf215546Sopenharmony_ciclc_specialize_spirv(const struct clc_binary *in_spirv,
254bf215546Sopenharmony_ci                     const struct clc_parsed_spirv *parsed_data,
255bf215546Sopenharmony_ci                     const struct clc_spirv_specialization_consts *consts,
256bf215546Sopenharmony_ci                     struct clc_binary *out_spirv);
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_ci#ifdef __cplusplus
259bf215546Sopenharmony_ci}
260bf215546Sopenharmony_ci#endif
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci#endif /* MESA_CLC_H */
263