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