libspirv.h 44 KB

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