1 // Copyright (c) 2015-2020 The Khronos Group Inc. 2 // Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights 3 // reserved. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #ifndef INCLUDE_SPIRV_TOOLS_LIBSPIRV_H_ 18 #define INCLUDE_SPIRV_TOOLS_LIBSPIRV_H_ 19 20 #ifdef __cplusplus 21 extern "C" { 22 #else 23 #include <stdbool.h> 24 #endif 25 26 #include <stddef.h> 27 #include <stdint.h> 28 29 #if defined(SPIRV_TOOLS_SHAREDLIB) 30 #if defined(_WIN32) 31 #if defined(SPIRV_TOOLS_IMPLEMENTATION) 32 #define SPIRV_TOOLS_EXPORT __declspec(dllexport) 33 #else 34 #define SPIRV_TOOLS_EXPORT __declspec(dllimport) 35 #endif 36 #define SPIRV_TOOLS_LOCAL 37 #else 38 #if defined(SPIRV_TOOLS_IMPLEMENTATION) 39 #define SPIRV_TOOLS_EXPORT __attribute__((visibility("default"))) 40 #define SPIRV_TOOLS_LOCAL __attribute__((visibility("hidden"))) 41 #else 42 #define SPIRV_TOOLS_EXPORT 43 #define SPIRV_TOOLS_LOCAL 44 #endif 45 #endif 46 #else 47 #define SPIRV_TOOLS_EXPORT 48 #define SPIRV_TOOLS_LOCAL 49 #endif 50 51 // Helpers 52 53 #define SPV_BIT(shift) (1 << (shift)) 54 55 #define SPV_FORCE_16_BIT_ENUM(name) SPV_FORCE_16BIT_##name = 0x7fff 56 #define SPV_FORCE_32_BIT_ENUM(name) SPV_FORCE_32BIT_##name = 0x7fffffff 57 58 // Enumerations 59 60 typedef enum spv_result_t { 61 SPV_SUCCESS = 0, 62 SPV_UNSUPPORTED = 1, 63 SPV_END_OF_STREAM = 2, 64 SPV_WARNING = 3, 65 SPV_FAILED_MATCH = 4, 66 SPV_REQUESTED_TERMINATION = 5, // Success, but signals early termination. 67 SPV_ERROR_INTERNAL = -1, 68 SPV_ERROR_OUT_OF_MEMORY = -2, 69 SPV_ERROR_INVALID_POINTER = -3, 70 SPV_ERROR_INVALID_BINARY = -4, 71 SPV_ERROR_INVALID_TEXT = -5, 72 SPV_ERROR_INVALID_TABLE = -6, 73 SPV_ERROR_INVALID_VALUE = -7, 74 SPV_ERROR_INVALID_DIAGNOSTIC = -8, 75 SPV_ERROR_INVALID_LOOKUP = -9, 76 SPV_ERROR_INVALID_ID = -10, 77 SPV_ERROR_INVALID_CFG = -11, 78 SPV_ERROR_INVALID_LAYOUT = -12, 79 SPV_ERROR_INVALID_CAPABILITY = -13, 80 SPV_ERROR_INVALID_DATA = -14, // Indicates data rules validation failure. 81 SPV_ERROR_MISSING_EXTENSION = -15, 82 SPV_ERROR_WRONG_VERSION = -16, // Indicates wrong SPIR-V version 83 SPV_FORCE_32_BIT_ENUM(spv_result_t) 84 } spv_result_t; 85 86 // Severity levels of messages communicated to the consumer. 87 typedef enum spv_message_level_t { 88 SPV_MSG_FATAL, // Unrecoverable error due to environment. 89 // Will exit the program immediately. E.g., 90 // out of memory. 91 SPV_MSG_INTERNAL_ERROR, // Unrecoverable error due to SPIRV-Tools 92 // internals. 93 // Will exit the program immediately. E.g., 94 // unimplemented feature. 95 SPV_MSG_ERROR, // Normal error due to user input. 96 SPV_MSG_WARNING, // Warning information. 97 SPV_MSG_INFO, // General information. 98 SPV_MSG_DEBUG, // Debug information. 99 } spv_message_level_t; 100 101 typedef enum spv_endianness_t { 102 SPV_ENDIANNESS_LITTLE, 103 SPV_ENDIANNESS_BIG, 104 SPV_FORCE_32_BIT_ENUM(spv_endianness_t) 105 } spv_endianness_t; 106 107 // The kinds of operands that an instruction may have. 108 // 109 // Some operand types are "concrete". The binary parser uses a concrete 110 // operand type to describe an operand of a parsed instruction. 111 // 112 // The assembler uses all operand types. In addition to determining what 113 // kind of value an operand may be, non-concrete operand types capture the 114 // fact that an operand might be optional (may be absent, or present exactly 115 // once), or might occur zero or more times. 116 // 117 // Sometimes we also need to be able to express the fact that an operand 118 // is a member of an optional tuple of values. In that case the first member 119 // would be optional, and the subsequent members would be required. 120 // 121 // NOTE: Although we don't promise binary compatibility, as a courtesy, please 122 // add new enum values at the end. 123 typedef enum spv_operand_type_t { 124 // A sentinel value. 125 SPV_OPERAND_TYPE_NONE = 0, 126 127 // Set 1: Operands that are IDs. 128 SPV_OPERAND_TYPE_ID, 129 SPV_OPERAND_TYPE_TYPE_ID, 130 SPV_OPERAND_TYPE_RESULT_ID, 131 SPV_OPERAND_TYPE_MEMORY_SEMANTICS_ID, // SPIR-V Sec 3.25 132 SPV_OPERAND_TYPE_SCOPE_ID, // SPIR-V Sec 3.27 133 134 // Set 2: Operands that are literal numbers. 135 SPV_OPERAND_TYPE_LITERAL_INTEGER, // Always unsigned 32-bits. 136 // The Instruction argument to OpExtInst. It's an unsigned 32-bit literal 137 // number indicating which instruction to use from an extended instruction 138 // set. 139 SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER, 140 // The Opcode argument to OpSpecConstantOp. It determines the operation 141 // to be performed on constant operands to compute a specialization constant 142 // result. 143 SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER, 144 // A literal number whose format and size are determined by a previous operand 145 // in the same instruction. It's a signed integer, an unsigned integer, or a 146 // floating point number. It also has a specified bit width. The width 147 // may be larger than 32, which would require such a typed literal value to 148 // occupy multiple SPIR-V words. 149 SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER, 150 SPV_OPERAND_TYPE_LITERAL_FLOAT, // Always 32-bit float. 151 152 // Set 3: The literal string operand type. 153 SPV_OPERAND_TYPE_LITERAL_STRING, 154 155 // Set 4: Operands that are a single word enumerated value. 156 SPV_OPERAND_TYPE_SOURCE_LANGUAGE, // SPIR-V Sec 3.2 157 SPV_OPERAND_TYPE_EXECUTION_MODEL, // SPIR-V Sec 3.3 158 SPV_OPERAND_TYPE_ADDRESSING_MODEL, // SPIR-V Sec 3.4 159 SPV_OPERAND_TYPE_MEMORY_MODEL, // SPIR-V Sec 3.5 160 SPV_OPERAND_TYPE_EXECUTION_MODE, // SPIR-V Sec 3.6 161 SPV_OPERAND_TYPE_STORAGE_CLASS, // SPIR-V Sec 3.7 162 SPV_OPERAND_TYPE_DIMENSIONALITY, // SPIR-V Sec 3.8 163 SPV_OPERAND_TYPE_SAMPLER_ADDRESSING_MODE, // SPIR-V Sec 3.9 164 SPV_OPERAND_TYPE_SAMPLER_FILTER_MODE, // SPIR-V Sec 3.10 165 SPV_OPERAND_TYPE_SAMPLER_IMAGE_FORMAT, // SPIR-V Sec 3.11 166 SPV_OPERAND_TYPE_IMAGE_CHANNEL_ORDER, // SPIR-V Sec 3.12 167 SPV_OPERAND_TYPE_IMAGE_CHANNEL_DATA_TYPE, // SPIR-V Sec 3.13 168 SPV_OPERAND_TYPE_FP_ROUNDING_MODE, // SPIR-V Sec 3.16 169 SPV_OPERAND_TYPE_LINKAGE_TYPE, // SPIR-V Sec 3.17 170 SPV_OPERAND_TYPE_ACCESS_QUALIFIER, // SPIR-V Sec 3.18 171 SPV_OPERAND_TYPE_FUNCTION_PARAMETER_ATTRIBUTE, // SPIR-V Sec 3.19 172 SPV_OPERAND_TYPE_DECORATION, // SPIR-V Sec 3.20 173 SPV_OPERAND_TYPE_BUILT_IN, // SPIR-V Sec 3.21 174 SPV_OPERAND_TYPE_GROUP_OPERATION, // SPIR-V Sec 3.28 175 SPV_OPERAND_TYPE_KERNEL_ENQ_FLAGS, // SPIR-V Sec 3.29 176 SPV_OPERAND_TYPE_KERNEL_PROFILING_INFO, // SPIR-V Sec 3.30 177 SPV_OPERAND_TYPE_CAPABILITY, // SPIR-V Sec 3.31 178 179 // NOTE: New concrete enum values should be added at the end. 180 181 // Set 5: Operands that are a single word bitmask. 182 // Sometimes a set bit indicates the instruction requires still more operands. 183 SPV_OPERAND_TYPE_IMAGE, // SPIR-V Sec 3.14 184 SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, // SPIR-V Sec 3.15 185 SPV_OPERAND_TYPE_SELECTION_CONTROL, // SPIR-V Sec 3.22 186 SPV_OPERAND_TYPE_LOOP_CONTROL, // SPIR-V Sec 3.23 187 SPV_OPERAND_TYPE_FUNCTION_CONTROL, // SPIR-V Sec 3.24 188 SPV_OPERAND_TYPE_MEMORY_ACCESS, // SPIR-V Sec 3.26 189 SPV_OPERAND_TYPE_FRAGMENT_SHADING_RATE, // SPIR-V Sec 3.FSR 190 191 // NOTE: New concrete enum values should be added at the end. 192 193 // The "optional" and "variable" operand types are only used internally by 194 // the assembler and the binary parser. 195 // There are two categories: 196 // Optional : expands to 0 or 1 operand, like ? in regular expressions. 197 // Variable : expands to 0, 1 or many operands or pairs of operands. 198 // This is similar to * in regular expressions. 199 200 // NOTE: These FIRST_* and LAST_* enum values are DEPRECATED. 201 // The concept of "optional" and "variable" operand types are only intended 202 // for use as an implementation detail of parsing SPIR-V, either in text or 203 // binary form. Instead of using enum ranges, use characteristic function 204 // spvOperandIsConcrete. 205 // The use of enum value ranges in a public API makes it difficult to insert 206 // new values into a range without also breaking binary compatibility. 207 // 208 // Macros for defining bounds on optional and variable operand types. 209 // Any variable operand type is also optional. 210 // TODO(dneto): Remove SPV_OPERAND_TYPE_FIRST_* and SPV_OPERAND_TYPE_LAST_* 211 #define FIRST_OPTIONAL(ENUM) ENUM, SPV_OPERAND_TYPE_FIRST_OPTIONAL_TYPE = ENUM 212 #define FIRST_VARIABLE(ENUM) ENUM, SPV_OPERAND_TYPE_FIRST_VARIABLE_TYPE = ENUM 213 #define LAST_VARIABLE(ENUM) \ 214 ENUM, SPV_OPERAND_TYPE_LAST_VARIABLE_TYPE = ENUM, \ 215 SPV_OPERAND_TYPE_LAST_OPTIONAL_TYPE = ENUM 216 217 // An optional operand represents zero or one logical operands. 218 // In an instruction definition, this may only appear at the end of the 219 // operand types. 220 FIRST_OPTIONAL(SPV_OPERAND_TYPE_OPTIONAL_ID), 221 // An optional image operand type. 222 SPV_OPERAND_TYPE_OPTIONAL_IMAGE, 223 // An optional memory access type. 224 SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS, 225 // An optional literal integer. 226 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_INTEGER, 227 // An optional literal number, which may be either integer or floating point. 228 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_NUMBER, 229 // Like SPV_OPERAND_TYPE_TYPED_LITERAL_NUMBER, but optional, and integral. 230 SPV_OPERAND_TYPE_OPTIONAL_TYPED_LITERAL_INTEGER, 231 // An optional literal string. 232 SPV_OPERAND_TYPE_OPTIONAL_LITERAL_STRING, 233 // An optional access qualifier 234 SPV_OPERAND_TYPE_OPTIONAL_ACCESS_QUALIFIER, 235 // An optional context-independent value, or CIV. CIVs are tokens that we can 236 // assemble regardless of where they occur -- literals, IDs, immediate 237 // integers, etc. 238 SPV_OPERAND_TYPE_OPTIONAL_CIV, 239 240 // A variable operand represents zero or more logical operands. 241 // In an instruction definition, this may only appear at the end of the 242 // operand types. 243 FIRST_VARIABLE(SPV_OPERAND_TYPE_VARIABLE_ID), 244 SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER, 245 // A sequence of zero or more pairs of (typed literal integer, Id). 246 // Expands to zero or more: 247 // (SPV_OPERAND_TYPE_TYPED_LITERAL_INTEGER, SPV_OPERAND_TYPE_ID) 248 // where the literal number must always be an integer of some sort. 249 SPV_OPERAND_TYPE_VARIABLE_LITERAL_INTEGER_ID, 250 // A sequence of zero or more pairs of (Id, Literal integer) 251 LAST_VARIABLE(SPV_OPERAND_TYPE_VARIABLE_ID_LITERAL_INTEGER), 252 253 // The following are concrete enum types from the DebugInfo extended 254 // instruction set. 255 SPV_OPERAND_TYPE_DEBUG_INFO_FLAGS, // DebugInfo Sec 3.2. A mask. 256 SPV_OPERAND_TYPE_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING, // DebugInfo Sec 3.3 257 SPV_OPERAND_TYPE_DEBUG_COMPOSITE_TYPE, // DebugInfo Sec 3.4 258 SPV_OPERAND_TYPE_DEBUG_TYPE_QUALIFIER, // DebugInfo Sec 3.5 259 SPV_OPERAND_TYPE_DEBUG_OPERATION, // DebugInfo Sec 3.6 260 261 // The following are concrete enum types from the OpenCL.DebugInfo.100 262 // extended instruction set. 263 SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_INFO_FLAGS, // Sec 3.2. A Mask 264 SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_BASE_TYPE_ATTRIBUTE_ENCODING, // Sec 3.3 265 SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_COMPOSITE_TYPE, // Sec 3.4 266 SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_TYPE_QUALIFIER, // Sec 3.5 267 SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_OPERATION, // Sec 3.6 268 SPV_OPERAND_TYPE_CLDEBUG100_DEBUG_IMPORTED_ENTITY, // Sec 3.7 269 270 // The following are concrete enum types from SPV_INTEL_float_controls2 271 // https://github.com/intel/llvm/blob/39fa9b0cbfbae88327118990a05c5b387b56d2ef/sycl/doc/extensions/SPIRV/SPV_INTEL_float_controls2.asciidoc 272 SPV_OPERAND_TYPE_FPDENORM_MODE, // Sec 3.17 FP Denorm Mode 273 SPV_OPERAND_TYPE_FPOPERATION_MODE, // Sec 3.18 FP Operation Mode 274 // A value enum from https://github.com/KhronosGroup/SPIRV-Headers/pull/177 275 SPV_OPERAND_TYPE_QUANTIZATION_MODES, 276 // A value enum from https://github.com/KhronosGroup/SPIRV-Headers/pull/177 277 SPV_OPERAND_TYPE_OVERFLOW_MODES, 278 279 // Concrete operand types for the provisional Vulkan ray tracing feature. 280 SPV_OPERAND_TYPE_RAY_FLAGS, // SPIR-V Sec 3.RF 281 SPV_OPERAND_TYPE_RAY_QUERY_INTERSECTION, // SPIR-V Sec 3.RQIntersection 282 SPV_OPERAND_TYPE_RAY_QUERY_COMMITTED_INTERSECTION_TYPE, // SPIR-V Sec 283 // 3.RQCommitted 284 SPV_OPERAND_TYPE_RAY_QUERY_CANDIDATE_INTERSECTION_TYPE, // SPIR-V Sec 285 // 3.RQCandidate 286 287 // Concrete operand types for integer dot product. 288 // Packed vector format 289 SPV_OPERAND_TYPE_PACKED_VECTOR_FORMAT, // SPIR-V Sec 3.x 290 // An optional packed vector format 291 SPV_OPERAND_TYPE_OPTIONAL_PACKED_VECTOR_FORMAT, 292 293 // Concrete operand types for cooperative matrix. 294 SPV_OPERAND_TYPE_COOPERATIVE_MATRIX_OPERANDS, 295 // An optional cooperative matrix operands 296 SPV_OPERAND_TYPE_OPTIONAL_COOPERATIVE_MATRIX_OPERANDS, 297 SPV_OPERAND_TYPE_COOPERATIVE_MATRIX_LAYOUT, 298 SPV_OPERAND_TYPE_COOPERATIVE_MATRIX_USE, 299 300 // Enum type from SPV_INTEL_global_variable_fpga_decorations 301 SPV_OPERAND_TYPE_INITIALIZATION_MODE_QUALIFIER, 302 // Enum type from SPV_INTEL_global_variable_host_access 303 SPV_OPERAND_TYPE_HOST_ACCESS_QUALIFIER, 304 // Enum type from SPV_INTEL_cache_controls 305 SPV_OPERAND_TYPE_LOAD_CACHE_CONTROL, 306 // Enum type from SPV_INTEL_cache_controls 307 SPV_OPERAND_TYPE_STORE_CACHE_CONTROL, 308 // Enum type from SPV_INTEL_maximum_registers 309 SPV_OPERAND_TYPE_NAMED_MAXIMUM_NUMBER_OF_REGISTERS, 310 // Enum type from SPV_NV_raw_access_chains 311 SPV_OPERAND_TYPE_RAW_ACCESS_CHAIN_OPERANDS, 312 // Optional enum type from SPV_NV_raw_access_chains 313 SPV_OPERAND_TYPE_OPTIONAL_RAW_ACCESS_CHAIN_OPERANDS, 314 315 // This is a sentinel value, and does not represent an operand type. 316 // It should come last. 317 SPV_OPERAND_TYPE_NUM_OPERAND_TYPES, 318 319 SPV_FORCE_32_BIT_ENUM(spv_operand_type_t) 320 } spv_operand_type_t; 321 322 // Returns true if the given type is concrete. 323 bool spvOperandIsConcrete(spv_operand_type_t type); 324 325 // Returns true if the given type is concrete and also a mask. 326 bool spvOperandIsConcreteMask(spv_operand_type_t type); 327 328 typedef enum spv_ext_inst_type_t { 329 SPV_EXT_INST_TYPE_NONE = 0, 330 SPV_EXT_INST_TYPE_GLSL_STD_450, 331 SPV_EXT_INST_TYPE_OPENCL_STD, 332 SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER, 333 SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX, 334 SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER, 335 SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT, 336 SPV_EXT_INST_TYPE_DEBUGINFO, 337 SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100, 338 SPV_EXT_INST_TYPE_NONSEMANTIC_CLSPVREFLECTION, 339 SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100, 340 SPV_EXT_INST_TYPE_NONSEMANTIC_VKSPREFLECTION, 341 342 // Multiple distinct extended instruction set types could return this 343 // value, if they are prefixed with NonSemantic. and are otherwise 344 // unrecognised 345 SPV_EXT_INST_TYPE_NONSEMANTIC_UNKNOWN, 346 347 SPV_FORCE_32_BIT_ENUM(spv_ext_inst_type_t) 348 } spv_ext_inst_type_t; 349 350 // This determines at a high level the kind of a binary-encoded literal 351 // number, but not the bit width. 352 // In principle, these could probably be folded into new entries in 353 // spv_operand_type_t. But then we'd have some special case differences 354 // between the assembler and disassembler. 355 typedef enum spv_number_kind_t { 356 SPV_NUMBER_NONE = 0, // The default for value initialization. 357 SPV_NUMBER_UNSIGNED_INT, 358 SPV_NUMBER_SIGNED_INT, 359 SPV_NUMBER_FLOATING, 360 } spv_number_kind_t; 361 362 typedef enum spv_text_to_binary_options_t { 363 SPV_TEXT_TO_BINARY_OPTION_NONE = SPV_BIT(0), 364 // Numeric IDs in the binary will have the same values as in the source. 365 // Non-numeric IDs are allocated by filling in the gaps, starting with 1 366 // and going up. 367 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS = SPV_BIT(1), 368 SPV_FORCE_32_BIT_ENUM(spv_text_to_binary_options_t) 369 } spv_text_to_binary_options_t; 370 371 typedef enum spv_binary_to_text_options_t { 372 SPV_BINARY_TO_TEXT_OPTION_NONE = SPV_BIT(0), 373 SPV_BINARY_TO_TEXT_OPTION_PRINT = SPV_BIT(1), 374 SPV_BINARY_TO_TEXT_OPTION_COLOR = SPV_BIT(2), 375 SPV_BINARY_TO_TEXT_OPTION_INDENT = SPV_BIT(3), 376 SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET = SPV_BIT(4), 377 // Do not output the module header as leading comments in the assembly. 378 SPV_BINARY_TO_TEXT_OPTION_NO_HEADER = SPV_BIT(5), 379 // Use friendly names where possible. The heuristic may expand over 380 // time, but will use common names for scalar types, and debug names from 381 // OpName instructions. 382 SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES = SPV_BIT(6), 383 // Add some comments to the generated assembly 384 SPV_BINARY_TO_TEXT_OPTION_COMMENT = SPV_BIT(7), 385 SPV_FORCE_32_BIT_ENUM(spv_binary_to_text_options_t) 386 } spv_binary_to_text_options_t; 387 388 // Constants 389 390 // The default id bound is to the minimum value for the id limit 391 // in the spir-v specification under the section "Universal Limits". 392 const uint32_t kDefaultMaxIdBound = 0x3FFFFF; 393 394 // Structures 395 396 // Information about an operand parsed from a binary SPIR-V module. 397 // Note that the values are not included. You still need access to the binary 398 // to extract the values. 399 typedef struct spv_parsed_operand_t { 400 // Location of the operand, in words from the start of the instruction. 401 uint16_t offset; 402 // Number of words occupied by this operand. 403 uint16_t num_words; 404 // The "concrete" operand type. See the definition of spv_operand_type_t 405 // for details. 406 spv_operand_type_t type; 407 // If type is a literal number type, then number_kind says whether it's 408 // a signed integer, an unsigned integer, or a floating point number. 409 spv_number_kind_t number_kind; 410 // The number of bits for a literal number type. 411 uint32_t number_bit_width; 412 } spv_parsed_operand_t; 413 414 // An instruction parsed from a binary SPIR-V module. 415 typedef struct spv_parsed_instruction_t { 416 // An array of words for this instruction, in native endianness. 417 const uint32_t* words; 418 // The number of words in this instruction. 419 uint16_t num_words; 420 uint16_t opcode; 421 // The extended instruction type, if opcode is OpExtInst. Otherwise 422 // this is the "none" value. 423 spv_ext_inst_type_t ext_inst_type; 424 // The type id, or 0 if this instruction doesn't have one. 425 uint32_t type_id; 426 // The result id, or 0 if this instruction doesn't have one. 427 uint32_t result_id; 428 // The array of parsed operands. 429 const spv_parsed_operand_t* operands; 430 uint16_t num_operands; 431 } spv_parsed_instruction_t; 432 433 typedef struct spv_parsed_header_t { 434 // The magic number of the SPIR-V module. 435 uint32_t magic; 436 // Version number. 437 uint32_t version; 438 // Generator's magic number. 439 uint32_t generator; 440 // IDs bound for this module (0 < id < bound). 441 uint32_t bound; 442 // reserved. 443 uint32_t reserved; 444 } spv_parsed_header_t; 445 446 typedef struct spv_const_binary_t { 447 const uint32_t* code; 448 const size_t wordCount; 449 } spv_const_binary_t; 450 451 typedef struct spv_binary_t { 452 uint32_t* code; 453 size_t wordCount; 454 } spv_binary_t; 455 456 typedef struct spv_text_t { 457 const char* str; 458 size_t length; 459 } spv_text_t; 460 461 typedef struct spv_position_t { 462 size_t line; 463 size_t column; 464 size_t index; 465 } spv_position_t; 466 467 typedef struct spv_diagnostic_t { 468 spv_position_t position; 469 char* error; 470 bool isTextSource; 471 } spv_diagnostic_t; 472 473 // Opaque struct containing the context used to operate on a SPIR-V module. 474 // Its object is used by various translation API functions. 475 typedef struct spv_context_t spv_context_t; 476 477 typedef struct spv_validator_options_t spv_validator_options_t; 478 479 typedef struct spv_optimizer_options_t spv_optimizer_options_t; 480 481 typedef struct spv_reducer_options_t spv_reducer_options_t; 482 483 typedef struct spv_fuzzer_options_t spv_fuzzer_options_t; 484 485 typedef struct spv_optimizer_t spv_optimizer_t; 486 487 // Type Definitions 488 489 typedef spv_const_binary_t* spv_const_binary; 490 typedef spv_binary_t* spv_binary; 491 typedef spv_text_t* spv_text; 492 typedef spv_position_t* spv_position; 493 typedef spv_diagnostic_t* spv_diagnostic; 494 typedef const spv_context_t* spv_const_context; 495 typedef spv_context_t* spv_context; 496 typedef spv_validator_options_t* spv_validator_options; 497 typedef const spv_validator_options_t* spv_const_validator_options; 498 typedef spv_optimizer_options_t* spv_optimizer_options; 499 typedef const spv_optimizer_options_t* spv_const_optimizer_options; 500 typedef spv_reducer_options_t* spv_reducer_options; 501 typedef const spv_reducer_options_t* spv_const_reducer_options; 502 typedef spv_fuzzer_options_t* spv_fuzzer_options; 503 typedef const spv_fuzzer_options_t* spv_const_fuzzer_options; 504 505 // Platform API 506 507 // Returns the SPIRV-Tools software version as a null-terminated string. 508 // The contents of the underlying storage is valid for the remainder of 509 // the process. 510 SPIRV_TOOLS_EXPORT const char* spvSoftwareVersionString(void); 511 // Returns a null-terminated string containing the name of the project, 512 // the software version string, and commit details. 513 // The contents of the underlying storage is valid for the remainder of 514 // the process. 515 SPIRV_TOOLS_EXPORT const char* spvSoftwareVersionDetailsString(void); 516 517 // Certain target environments impose additional restrictions on SPIR-V, so it's 518 // often necessary to specify which one applies. SPV_ENV_UNIVERSAL_* implies an 519 // environment-agnostic SPIR-V. 520 // 521 // When an API method needs to derive a SPIR-V version from a target environment 522 // (from the spv_context object), the method will choose the highest version of 523 // SPIR-V supported by the target environment. Examples: 524 // SPV_ENV_VULKAN_1_0 -> SPIR-V 1.0 525 // SPV_ENV_VULKAN_1_1 -> SPIR-V 1.3 526 // SPV_ENV_VULKAN_1_1_SPIRV_1_4 -> SPIR-V 1.4 527 // SPV_ENV_VULKAN_1_2 -> SPIR-V 1.5 528 // SPV_ENV_VULKAN_1_3 -> SPIR-V 1.6 529 // Consult the description of API entry points for specific rules. 530 typedef enum { 531 SPV_ENV_UNIVERSAL_1_0, // SPIR-V 1.0 latest revision, no other restrictions. 532 SPV_ENV_VULKAN_1_0, // Vulkan 1.0 latest revision. 533 SPV_ENV_UNIVERSAL_1_1, // SPIR-V 1.1 latest revision, no other restrictions. 534 SPV_ENV_OPENCL_2_1, // OpenCL Full Profile 2.1 latest revision. 535 SPV_ENV_OPENCL_2_2, // OpenCL Full Profile 2.2 latest revision. 536 SPV_ENV_OPENGL_4_0, // OpenGL 4.0 plus GL_ARB_gl_spirv, latest revisions. 537 SPV_ENV_OPENGL_4_1, // OpenGL 4.1 plus GL_ARB_gl_spirv, latest revisions. 538 SPV_ENV_OPENGL_4_2, // OpenGL 4.2 plus GL_ARB_gl_spirv, latest revisions. 539 SPV_ENV_OPENGL_4_3, // OpenGL 4.3 plus GL_ARB_gl_spirv, latest revisions. 540 // There is no variant for OpenGL 4.4. 541 SPV_ENV_OPENGL_4_5, // OpenGL 4.5 plus GL_ARB_gl_spirv, latest revisions. 542 SPV_ENV_UNIVERSAL_1_2, // SPIR-V 1.2, latest revision, no other restrictions. 543 SPV_ENV_OPENCL_1_2, // OpenCL Full Profile 1.2 plus cl_khr_il_program, 544 // latest revision. 545 SPV_ENV_OPENCL_EMBEDDED_1_2, // OpenCL Embedded Profile 1.2 plus 546 // cl_khr_il_program, latest revision. 547 SPV_ENV_OPENCL_2_0, // OpenCL Full Profile 2.0 plus cl_khr_il_program, 548 // latest revision. 549 SPV_ENV_OPENCL_EMBEDDED_2_0, // OpenCL Embedded Profile 2.0 plus 550 // cl_khr_il_program, latest revision. 551 SPV_ENV_OPENCL_EMBEDDED_2_1, // OpenCL Embedded Profile 2.1 latest revision. 552 SPV_ENV_OPENCL_EMBEDDED_2_2, // OpenCL Embedded Profile 2.2 latest revision. 553 SPV_ENV_UNIVERSAL_1_3, // SPIR-V 1.3 latest revision, no other restrictions. 554 SPV_ENV_VULKAN_1_1, // Vulkan 1.1 latest revision. 555 SPV_ENV_WEBGPU_0, // DEPRECATED, may be removed in the future. 556 SPV_ENV_UNIVERSAL_1_4, // SPIR-V 1.4 latest revision, no other restrictions. 557 558 // Vulkan 1.1 with VK_KHR_spirv_1_4, i.e. SPIR-V 1.4 binary. 559 SPV_ENV_VULKAN_1_1_SPIRV_1_4, 560 561 SPV_ENV_UNIVERSAL_1_5, // SPIR-V 1.5 latest revision, no other restrictions. 562 SPV_ENV_VULKAN_1_2, // Vulkan 1.2 latest revision. 563 564 SPV_ENV_UNIVERSAL_1_6, // SPIR-V 1.6 latest revision, no other restrictions. 565 SPV_ENV_VULKAN_1_3, // Vulkan 1.3 latest revision. 566 567 SPV_ENV_MAX // Keep this as the last enum value. 568 } spv_target_env; 569 570 // SPIR-V Validator can be parameterized with the following Universal Limits. 571 typedef enum { 572 spv_validator_limit_max_struct_members, 573 spv_validator_limit_max_struct_depth, 574 spv_validator_limit_max_local_variables, 575 spv_validator_limit_max_global_variables, 576 spv_validator_limit_max_switch_branches, 577 spv_validator_limit_max_function_args, 578 spv_validator_limit_max_control_flow_nesting_depth, 579 spv_validator_limit_max_access_chain_indexes, 580 spv_validator_limit_max_id_bound, 581 } spv_validator_limit; 582 583 // Returns a string describing the given SPIR-V target environment. 584 SPIRV_TOOLS_EXPORT const char* spvTargetEnvDescription(spv_target_env env); 585 586 // Parses s into *env and returns true if successful. If unparsable, returns 587 // false and sets *env to SPV_ENV_UNIVERSAL_1_0. 588 SPIRV_TOOLS_EXPORT bool spvParseTargetEnv(const char* s, spv_target_env* env); 589 590 // Determines the target env value with the least features but which enables 591 // the given Vulkan and SPIR-V versions. If such a target is supported, returns 592 // true and writes the value to |env|, otherwise returns false. 593 // 594 // The Vulkan version is given as an unsigned 32-bit number as specified in 595 // Vulkan section "29.2.1 Version Numbers": the major version number appears 596 // in bits 22 to 21, and the minor version is in bits 12 to 21. The SPIR-V 597 // version is given in the SPIR-V version header word: major version in bits 598 // 16 to 23, and minor version in bits 8 to 15. 599 SPIRV_TOOLS_EXPORT bool spvParseVulkanEnv(uint32_t vulkan_ver, 600 uint32_t spirv_ver, 601 spv_target_env* env); 602 603 // Creates a context object for most of the SPIRV-Tools API. 604 // Returns null if env is invalid. 605 // 606 // See specific API calls for how the target environment is interpreted 607 // (particularly assembly and validation). 608 SPIRV_TOOLS_EXPORT spv_context spvContextCreate(spv_target_env env); 609 610 // Destroys the given context object. 611 SPIRV_TOOLS_EXPORT void spvContextDestroy(spv_context context); 612 613 // Creates a Validator options object with default options. Returns a valid 614 // options object. The object remains valid until it is passed into 615 // spvValidatorOptionsDestroy. 616 SPIRV_TOOLS_EXPORT spv_validator_options spvValidatorOptionsCreate(void); 617 618 // Destroys the given Validator options object. 619 SPIRV_TOOLS_EXPORT void spvValidatorOptionsDestroy( 620 spv_validator_options options); 621 622 // Records the maximum Universal Limit that is considered valid in the given 623 // Validator options object. <options> argument must be a valid options object. 624 SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetUniversalLimit( 625 spv_validator_options options, spv_validator_limit limit_type, 626 uint32_t limit); 627 628 // Record whether or not the validator should relax the rules on types for 629 // stores to structs. When relaxed, it will allow a type mismatch as long as 630 // the types are structs with the same layout. Two structs have the same layout 631 // if 632 // 633 // 1) the members of the structs are either the same type or are structs with 634 // same layout, and 635 // 636 // 2) the decorations that affect the memory layout are identical for both 637 // types. Other decorations are not relevant. 638 SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetRelaxStoreStruct( 639 spv_validator_options options, bool val); 640 641 // Records whether or not the validator should relax the rules on pointer usage 642 // in logical addressing mode. 643 // 644 // When relaxed, it will allow the following usage cases of pointers: 645 // 1) OpVariable allocating an object whose type is a pointer type 646 // 2) OpReturnValue returning a pointer value 647 SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetRelaxLogicalPointer( 648 spv_validator_options options, bool val); 649 650 // Records whether or not the validator should relax the rules because it is 651 // expected that the optimizations will make the code legal. 652 // 653 // When relaxed, it will allow the following: 654 // 1) It will allow relaxed logical pointers. Setting this option will also 655 // set that option. 656 // 2) Pointers that are pass as parameters to function calls do not have to 657 // match the storage class of the formal parameter. 658 // 3) Pointers that are actual parameters on function calls do not have to point 659 // to the same type pointed as the formal parameter. The types just need to 660 // logically match. 661 // 4) GLSLstd450 Interpolate* instructions can have a load of an interpolant 662 // for a first argument. 663 SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetBeforeHlslLegalization( 664 spv_validator_options options, bool val); 665 666 // Records whether the validator should use "relaxed" block layout rules. 667 // Relaxed layout rules are described by Vulkan extension 668 // VK_KHR_relaxed_block_layout, and they affect uniform blocks, storage blocks, 669 // and push constants. 670 // 671 // This is enabled by default when targeting Vulkan 1.1 or later. 672 // Relaxed layout is more permissive than the default rules in Vulkan 1.0. 673 SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetRelaxBlockLayout( 674 spv_validator_options options, bool val); 675 676 // Records whether the validator should use standard block layout rules for 677 // uniform blocks. 678 SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetUniformBufferStandardLayout( 679 spv_validator_options options, bool val); 680 681 // Records whether the validator should use "scalar" block layout rules. 682 // Scalar layout rules are more permissive than relaxed block layout. 683 // 684 // See Vulkan extension VK_EXT_scalar_block_layout. The scalar alignment is 685 // defined as follows: 686 // - scalar alignment of a scalar is the scalar size 687 // - scalar alignment of a vector is the scalar alignment of its component 688 // - scalar alignment of a matrix is the scalar alignment of its component 689 // - scalar alignment of an array is the scalar alignment of its element 690 // - scalar alignment of a struct is the max scalar alignment among its 691 // members 692 // 693 // For a struct in Uniform, StorageClass, or PushConstant: 694 // - a member Offset must be a multiple of the member's scalar alignment 695 // - ArrayStride or MatrixStride must be a multiple of the array or matrix 696 // scalar alignment 697 SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetScalarBlockLayout( 698 spv_validator_options options, bool val); 699 700 // Records whether the validator should use "scalar" block layout 701 // rules (as defined above) for Workgroup blocks. See Vulkan 702 // extension VK_KHR_workgroup_memory_explicit_layout. 703 SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetWorkgroupScalarBlockLayout( 704 spv_validator_options options, bool val); 705 706 // Records whether or not the validator should skip validating standard 707 // uniform/storage block layout. 708 SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetSkipBlockLayout( 709 spv_validator_options options, bool val); 710 711 // Records whether or not the validator should allow the LocalSizeId 712 // decoration where the environment otherwise would not allow it. 713 SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetAllowLocalSizeId( 714 spv_validator_options options, bool val); 715 716 // Whether friendly names should be used in validation error messages. 717 SPIRV_TOOLS_EXPORT void spvValidatorOptionsSetFriendlyNames( 718 spv_validator_options options, bool val); 719 720 // Creates an optimizer options object with default options. Returns a valid 721 // options object. The object remains valid until it is passed into 722 // |spvOptimizerOptionsDestroy|. 723 SPIRV_TOOLS_EXPORT spv_optimizer_options spvOptimizerOptionsCreate(void); 724 725 // Destroys the given optimizer options object. 726 SPIRV_TOOLS_EXPORT void spvOptimizerOptionsDestroy( 727 spv_optimizer_options options); 728 729 // Records whether or not the optimizer should run the validator before 730 // optimizing. If |val| is true, the validator will be run. 731 SPIRV_TOOLS_EXPORT void spvOptimizerOptionsSetRunValidator( 732 spv_optimizer_options options, bool val); 733 734 // Records the validator options that should be passed to the validator if it is 735 // run. 736 SPIRV_TOOLS_EXPORT void spvOptimizerOptionsSetValidatorOptions( 737 spv_optimizer_options options, spv_validator_options val); 738 739 // Records the maximum possible value for the id bound. 740 SPIRV_TOOLS_EXPORT void spvOptimizerOptionsSetMaxIdBound( 741 spv_optimizer_options options, uint32_t val); 742 743 // Records whether all bindings within the module should be preserved. 744 SPIRV_TOOLS_EXPORT void spvOptimizerOptionsSetPreserveBindings( 745 spv_optimizer_options options, bool val); 746 747 // Records whether all specialization constants within the module 748 // should be preserved. 749 SPIRV_TOOLS_EXPORT void spvOptimizerOptionsSetPreserveSpecConstants( 750 spv_optimizer_options options, bool val); 751 752 // Creates a reducer options object with default options. Returns a valid 753 // options object. The object remains valid until it is passed into 754 // |spvReducerOptionsDestroy|. 755 SPIRV_TOOLS_EXPORT spv_reducer_options spvReducerOptionsCreate(void); 756 757 // Destroys the given reducer options object. 758 SPIRV_TOOLS_EXPORT void spvReducerOptionsDestroy(spv_reducer_options options); 759 760 // Sets the maximum number of reduction steps that should run before the reducer 761 // gives up. 762 SPIRV_TOOLS_EXPORT void spvReducerOptionsSetStepLimit( 763 spv_reducer_options options, uint32_t step_limit); 764 765 // Sets the fail-on-validation-error option; if true, the reducer will return 766 // kStateInvalid if a reduction step yields a state that fails SPIR-V 767 // validation. Otherwise, an invalid state is treated as uninteresting and the 768 // reduction backtracks and continues. 769 SPIRV_TOOLS_EXPORT void spvReducerOptionsSetFailOnValidationError( 770 spv_reducer_options options, bool fail_on_validation_error); 771 772 // Sets the function that the reducer should target. If set to zero the reducer 773 // will target all functions as well as parts of the module that lie outside 774 // functions. Otherwise the reducer will restrict reduction to the function 775 // with result id |target_function|, which is required to exist. 776 SPIRV_TOOLS_EXPORT void spvReducerOptionsSetTargetFunction( 777 spv_reducer_options options, uint32_t target_function); 778 779 // Creates a fuzzer options object with default options. Returns a valid 780 // options object. The object remains valid until it is passed into 781 // |spvFuzzerOptionsDestroy|. 782 SPIRV_TOOLS_EXPORT spv_fuzzer_options spvFuzzerOptionsCreate(void); 783 784 // Destroys the given fuzzer options object. 785 SPIRV_TOOLS_EXPORT void spvFuzzerOptionsDestroy(spv_fuzzer_options options); 786 787 // Enables running the validator after every transformation is applied during 788 // a replay. 789 SPIRV_TOOLS_EXPORT void spvFuzzerOptionsEnableReplayValidation( 790 spv_fuzzer_options options); 791 792 // Sets the seed with which the random number generator used by the fuzzer 793 // should be initialized. 794 SPIRV_TOOLS_EXPORT void spvFuzzerOptionsSetRandomSeed( 795 spv_fuzzer_options options, uint32_t seed); 796 797 // Sets the range of transformations that should be applied during replay: 0 798 // means all transformations, +N means the first N transformations, -N means all 799 // except the final N transformations. 800 SPIRV_TOOLS_EXPORT void spvFuzzerOptionsSetReplayRange( 801 spv_fuzzer_options options, int32_t replay_range); 802 803 // Sets the maximum number of steps that the shrinker should take before giving 804 // up. 805 SPIRV_TOOLS_EXPORT void spvFuzzerOptionsSetShrinkerStepLimit( 806 spv_fuzzer_options options, uint32_t shrinker_step_limit); 807 808 // Enables running the validator after every pass is applied during a fuzzing 809 // run. 810 SPIRV_TOOLS_EXPORT void spvFuzzerOptionsEnableFuzzerPassValidation( 811 spv_fuzzer_options options); 812 813 // Enables all fuzzer passes during a fuzzing run (instead of a random subset 814 // of passes). 815 SPIRV_TOOLS_EXPORT void spvFuzzerOptionsEnableAllPasses( 816 spv_fuzzer_options options); 817 818 // Encodes the given SPIR-V assembly text to its binary representation. The 819 // length parameter specifies the number of bytes for text. Encoded binary will 820 // be stored into *binary. Any error will be written into *diagnostic if 821 // diagnostic is non-null, otherwise the context's message consumer will be 822 // used. The generated binary is independent of the context and may outlive it. 823 // The SPIR-V binary version is set to the highest version of SPIR-V supported 824 // by the context's target environment. 825 SPIRV_TOOLS_EXPORT spv_result_t spvTextToBinary(const spv_const_context context, 826 const char* text, 827 const size_t length, 828 spv_binary* binary, 829 spv_diagnostic* diagnostic); 830 831 // Encodes the given SPIR-V assembly text to its binary representation. Same as 832 // spvTextToBinary but with options. The options parameter is a bit field of 833 // spv_text_to_binary_options_t. 834 SPIRV_TOOLS_EXPORT spv_result_t spvTextToBinaryWithOptions( 835 const spv_const_context context, const char* text, const size_t length, 836 const uint32_t options, spv_binary* binary, spv_diagnostic* diagnostic); 837 838 // Frees an allocated text stream. This is a no-op if the text parameter 839 // is a null pointer. 840 SPIRV_TOOLS_EXPORT void spvTextDestroy(spv_text text); 841 842 // Decodes the given SPIR-V binary representation to its assembly text. The 843 // word_count parameter specifies the number of words for binary. The options 844 // parameter is a bit field of spv_binary_to_text_options_t. Decoded text will 845 // be stored into *text. Any error will be written into *diagnostic if 846 // diagnostic is non-null, otherwise the context's message consumer will be 847 // used. 848 SPIRV_TOOLS_EXPORT spv_result_t spvBinaryToText(const spv_const_context context, 849 const uint32_t* binary, 850 const size_t word_count, 851 const uint32_t options, 852 spv_text* text, 853 spv_diagnostic* diagnostic); 854 855 // Frees a binary stream from memory. This is a no-op if binary is a null 856 // pointer. 857 SPIRV_TOOLS_EXPORT void spvBinaryDestroy(spv_binary binary); 858 859 // Validates a SPIR-V binary for correctness. Any errors will be written into 860 // *diagnostic if diagnostic is non-null, otherwise the context's message 861 // consumer will be used. 862 // 863 // Validate for SPIR-V spec rules for the SPIR-V version named in the 864 // binary's header (at word offset 1). Additionally, if the context target 865 // environment is a client API (such as Vulkan 1.1), then validate for that 866 // client API version, to the extent that it is verifiable from data in the 867 // binary itself. 868 SPIRV_TOOLS_EXPORT spv_result_t spvValidate(const spv_const_context context, 869 const spv_const_binary binary, 870 spv_diagnostic* diagnostic); 871 872 // Validates a SPIR-V binary for correctness. Uses the provided Validator 873 // options. Any errors will be written into *diagnostic if diagnostic is 874 // non-null, otherwise the context's message consumer will be used. 875 // 876 // Validate for SPIR-V spec rules for the SPIR-V version named in the 877 // binary's header (at word offset 1). Additionally, if the context target 878 // environment is a client API (such as Vulkan 1.1), then validate for that 879 // client API version, to the extent that it is verifiable from data in the 880 // binary itself, or in the validator options. 881 SPIRV_TOOLS_EXPORT spv_result_t spvValidateWithOptions( 882 const spv_const_context context, const spv_const_validator_options options, 883 const spv_const_binary binary, spv_diagnostic* diagnostic); 884 885 // Validates a raw SPIR-V binary for correctness. Any errors will be written 886 // into *diagnostic if diagnostic is non-null, otherwise the context's message 887 // consumer will be used. 888 SPIRV_TOOLS_EXPORT spv_result_t 889 spvValidateBinary(const spv_const_context context, const uint32_t* words, 890 const size_t num_words, spv_diagnostic* diagnostic); 891 892 // Creates a diagnostic object. The position parameter specifies the location in 893 // the text/binary stream. The message parameter, copied into the diagnostic 894 // object, contains the error message to display. 895 SPIRV_TOOLS_EXPORT spv_diagnostic 896 spvDiagnosticCreate(const spv_position position, const char* message); 897 898 // Destroys a diagnostic object. This is a no-op if diagnostic is a null 899 // pointer. 900 SPIRV_TOOLS_EXPORT void spvDiagnosticDestroy(spv_diagnostic diagnostic); 901 902 // Prints the diagnostic to stderr. 903 SPIRV_TOOLS_EXPORT spv_result_t 904 spvDiagnosticPrint(const spv_diagnostic diagnostic); 905 906 // Gets the name of an instruction, without the "Op" prefix. 907 SPIRV_TOOLS_EXPORT const char* spvOpcodeString(const uint32_t opcode); 908 909 // The binary parser interface. 910 911 // A pointer to a function that accepts a parsed SPIR-V header. 912 // The integer arguments are the 32-bit words from the header, as specified 913 // in SPIR-V 1.0 Section 2.3 Table 1. 914 // The function should return SPV_SUCCESS if parsing should continue. 915 typedef spv_result_t (*spv_parsed_header_fn_t)( 916 void* user_data, spv_endianness_t endian, uint32_t magic, uint32_t version, 917 uint32_t generator, uint32_t id_bound, uint32_t reserved); 918 919 // A pointer to a function that accepts a parsed SPIR-V instruction. 920 // The parsed_instruction value is transient: it may be overwritten 921 // or released immediately after the function has returned. That also 922 // applies to the words array member of the parsed instruction. The 923 // function should return SPV_SUCCESS if and only if parsing should 924 // continue. 925 typedef spv_result_t (*spv_parsed_instruction_fn_t)( 926 void* user_data, const spv_parsed_instruction_t* parsed_instruction); 927 928 // Parses a SPIR-V binary, specified as counted sequence of 32-bit words. 929 // Parsing feedback is provided via two callbacks provided as function 930 // pointers. Each callback function pointer can be a null pointer, in 931 // which case it is never called. Otherwise, in a valid parse the 932 // parsed-header callback is called once, and then the parsed-instruction 933 // callback once for each instruction in the stream. The user_data parameter 934 // is supplied as context to the callbacks. Returns SPV_SUCCESS on successful 935 // parse where the callbacks always return SPV_SUCCESS. For an invalid parse, 936 // returns a status code other than SPV_SUCCESS, and if diagnostic is non-null 937 // also emits a diagnostic. If diagnostic is null the context's message consumer 938 // will be used to emit any errors. If a callback returns anything other than 939 // SPV_SUCCESS, then that status code is returned, no further callbacks are 940 // issued, and no additional diagnostics are emitted. 941 SPIRV_TOOLS_EXPORT spv_result_t spvBinaryParse( 942 const spv_const_context context, void* user_data, const uint32_t* words, 943 const size_t num_words, spv_parsed_header_fn_t parse_header, 944 spv_parsed_instruction_fn_t parse_instruction, spv_diagnostic* diagnostic); 945 946 // The optimizer interface. 947 948 // A pointer to a function that accepts a log message from an optimizer. 949 typedef void (*spv_message_consumer)( 950 spv_message_level_t, const char*, const spv_position_t*, const char*); 951 952 // Creates and returns an optimizer object. This object must be passed to 953 // optimizer APIs below and is valid until passed to spvOptimizerDestroy. 954 SPIRV_TOOLS_EXPORT spv_optimizer_t* spvOptimizerCreate(spv_target_env env); 955 956 // Destroys the given optimizer object. 957 SPIRV_TOOLS_EXPORT void spvOptimizerDestroy(spv_optimizer_t* optimizer); 958 959 // Sets an spv_message_consumer on an optimizer object. 960 SPIRV_TOOLS_EXPORT void spvOptimizerSetMessageConsumer( 961 spv_optimizer_t* optimizer, spv_message_consumer consumer); 962 963 // Registers passes that attempt to legalize the generated code. 964 SPIRV_TOOLS_EXPORT void spvOptimizerRegisterLegalizationPasses( 965 spv_optimizer_t* optimizer); 966 967 // Registers passes that attempt to improve performance of generated code. 968 SPIRV_TOOLS_EXPORT void spvOptimizerRegisterPerformancePasses( 969 spv_optimizer_t* optimizer); 970 971 // Registers passes that attempt to improve the size of generated code. 972 SPIRV_TOOLS_EXPORT void spvOptimizerRegisterSizePasses( 973 spv_optimizer_t* optimizer); 974 975 // Registers a pass specified by a flag in an optimizer object. 976 SPIRV_TOOLS_EXPORT bool spvOptimizerRegisterPassFromFlag( 977 spv_optimizer_t* optimizer, const char* flag); 978 979 // Registers passes specified by length number of flags in an optimizer object. 980 // Passes may remove interface variables that are unused. 981 SPIRV_TOOLS_EXPORT bool spvOptimizerRegisterPassesFromFlags( 982 spv_optimizer_t* optimizer, const char** flags, const size_t flag_count); 983 984 // Registers passes specified by length number of flags in an optimizer object. 985 // Passes will not remove interface variables. 986 SPIRV_TOOLS_EXPORT bool 987 spvOptimizerRegisterPassesFromFlagsWhilePreservingTheInterface( 988 spv_optimizer_t* optimizer, const char** flags, const size_t flag_count); 989 990 // Optimizes the SPIR-V code of size |word_count| pointed to by |binary| and 991 // returns an optimized spv_binary in |optimized_binary|. 992 // 993 // Returns SPV_SUCCESS on successful optimization, whether or not the module is 994 // modified. Returns an SPV_ERROR_* if the module fails to validate or if 995 // errors occur when processing using any of the registered passes. In that 996 // case, no further passes are executed and the |optimized_binary| contents may 997 // be invalid. 998 // 999 // By default, the binary is validated before any transforms are performed, 1000 // and optionally after each transform. Validation uses SPIR-V spec rules 1001 // for the SPIR-V version named in the binary's header (at word offset 1). 1002 // Additionally, if the target environment is a client API (such as 1003 // Vulkan 1.1), then validate for that client API version, to the extent 1004 // that it is verifiable from data in the binary itself, or from the 1005 // validator options set on the optimizer options. 1006 SPIRV_TOOLS_EXPORT spv_result_t spvOptimizerRun( 1007 spv_optimizer_t* optimizer, const uint32_t* binary, const size_t word_count, 1008 spv_binary* optimized_binary, const spv_optimizer_options options); 1009 1010 #ifdef __cplusplus 1011 } 1012 #endif 1013 1014 #endif // INCLUDE_SPIRV_TOOLS_LIBSPIRV_H_ 1015