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