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