blob: ec842db586f77ab56e6090f00c67e8ce41656fd0 [file] [log] [blame]
Anastasia Stulova72832ef2022-05-23 14:03:54 +01001=============================
2User Guide for SPIR-V Target
3=============================
4
5.. contents::
6 :local:
7
8.. toctree::
9 :hidden:
10
11Introduction
12============
13
14The SPIR-V target provides code generation for the SPIR-V binary format described
15in `the official SPIR-V specification <https://www.khronos.org/registry/SPIR-V/>`_.
16
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -070017Usage
18=====
19
Alex Voicu88e2bb42024-06-07 13:50:23 +030020The SPIR-V backend can be invoked either from LLVM's Static Compiler (llc) or Clang,
21allowing developers to compile LLVM intermediate language (IL) files or OpenCL kernel
22sources directly to SPIR-V. This section outlines the usage of various commands to
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -070023leverage the SPIR-V backend for different purposes.
24
25Static Compiler Commands
26------------------------
27
281. **Basic SPIR-V Compilation**
29 Command: `llc -mtriple=spirv32-unknown-unknown input.ll -o output.spvt`
30 Description: This command compiles an LLVM IL file (`input.ll`) to a SPIR-V binary (`output.spvt`) for a 32-bit architecture.
31
322. **Compilation with Extensions and Optimization**
33 Command: `llc -O1 -mtriple=spirv64-unknown-unknown --spirv-ext=+SPV_INTEL_arbitrary_precision_integers input.ll -o output.spvt`
34 Description: Compiles an LLVM IL file to SPIR-V with (`-O1`) optimizations, targeting a 64-bit architecture. It enables the SPV_INTEL_arbitrary_precision_integers extension.
35
bwlodarcz62da3592024-08-23 05:27:36 +0200363. **Compilation with experimental NonSemantic.Shader.DebugInfo.100 support**
37 Command: `llc --spv-emit-nonsemantic-debug-info --spirv-ext=+SPV_KHR_non_semantic_info input.ll -o output.spvt`
38 Description: Compiles an LLVM IL file to SPIR-V with additional NonSemantic.Shader.DebugInfo.100 instructions. It enables the required SPV_KHR_non_semantic_info extension.
39
404. **SPIR-V Binary Generation**
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -070041 Command: `llc -O0 -mtriple=spirv64-unknown-unknown -filetype=obj input.ll -o output.spvt`
42 Description: Generates a SPIR-V object file (`output.spvt`) from an LLVM module, targeting a 64-bit SPIR-V architecture with no optimizations.
43
44Clang Commands
45--------------
46
471. **SPIR-V Generation**
48 Command: `clang –target=spirv64 input.cl`
49 Description: Generates a SPIR-V file directly from an OpenCL kernel source file (`input.cl`).
50
51Compiler Options
52================
53
Anastasia Stulova72832ef2022-05-23 14:03:54 +010054.. _spirv-target-triples:
55
56Target Triples
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -070057--------------
Anastasia Stulova72832ef2022-05-23 14:03:54 +010058
59For cross-compilation into SPIR-V use option
60
61``-target <Architecture><Subarchitecture>-<Vendor>-<OS>-<Environment>``
62
63to specify the target triple:
64
65 .. table:: SPIR-V Architectures
66
67 ============ ==============================================================
68 Architecture Description
69 ============ ==============================================================
70 ``spirv32`` SPIR-V with 32-bit pointer width.
71 ``spirv64`` SPIR-V with 64-bit pointer width.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -070072 ``spirv`` SPIR-V with logical memory layout.
Anastasia Stulova72832ef2022-05-23 14:03:54 +010073 ============ ==============================================================
74
75 .. table:: SPIR-V Subarchitectures
76
Anastasia Stulova7df25972022-05-27 11:12:44 +010077 =============== ==============================================================
78 Subarchitecture Description
79 =============== ==============================================================
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -070080 *<empty>* SPIR-V version deduced by backend based on the input.
Anastasia Stulova7df25972022-05-27 11:12:44 +010081 ``v1.0`` SPIR-V version 1.0.
82 ``v1.1`` SPIR-V version 1.1.
83 ``v1.2`` SPIR-V version 1.2.
84 ``v1.3`` SPIR-V version 1.3.
85 ``v1.4`` SPIR-V version 1.4.
86 ``v1.5`` SPIR-V version 1.5.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -070087 ``v1.6`` SPIR-V version 1.6.
Anastasia Stulova7df25972022-05-27 11:12:44 +010088 =============== ==============================================================
Anastasia Stulova72832ef2022-05-23 14:03:54 +010089
90 .. table:: SPIR-V Vendors
91
92 ===================== ==============================================================
93 Vendor Description
94 ===================== ==============================================================
95 *<empty>*/``unknown`` Generic SPIR-V target without any vendor-specific settings.
Alex Voicu88e2bb42024-06-07 13:50:23 +030096 ``amd`` AMDGCN SPIR-V target, with support for target specific
97 builtins and ASM, meant to be consumed by AMDGCN toolchains.
Anastasia Stulova72832ef2022-05-23 14:03:54 +010098 ===================== ==============================================================
99
100 .. table:: Operating Systems
101
Alex Voicu88e2bb42024-06-07 13:50:23 +0300102 ===================== ==============================================================
Anastasia Stulova72832ef2022-05-23 14:03:54 +0100103 OS Description
Alex Voicu88e2bb42024-06-07 13:50:23 +0300104 ===================== ==============================================================
Anastasia Stulova72832ef2022-05-23 14:03:54 +0100105 *<empty>*/``unknown`` Defaults to the OpenCL runtime.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700106 ``vulkan`` Vulkan shader runtime.
107 ``vulkan1.2`` Vulkan 1.2 runtime, corresponding to SPIR-V 1.5.
108 ``vulkan1.3`` Vulkan 1.3 runtime, corresponding to SPIR-V 1.6.
Alex Voicu88e2bb42024-06-07 13:50:23 +0300109 ``amdhsa`` AMDHSA runtime, meant to be used on HSA compatible runtimes,
110 corresponding to SPIR-V 1.6.
111 ===================== ==============================================================
Anastasia Stulova72832ef2022-05-23 14:03:54 +0100112
113 .. table:: SPIR-V Environments
114
115 ===================== ==============================================================
116 Environment Description
117 ===================== ==============================================================
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700118 *<empty>*/``unknown`` OpenCL environment or deduced by backend based on the input.
Anastasia Stulova72832ef2022-05-23 14:03:54 +0100119 ===================== ==============================================================
Anastasia Stulova7df25972022-05-27 11:12:44 +0100120
121Example:
122
123``-target spirv64v1.0`` can be used to compile for SPIR-V version 1.0 with 64-bit pointer width.
Joshua Cranmerbcad1612023-03-13 14:14:12 -0400124
Alex Voicu88e2bb42024-06-07 13:50:23 +0300125``-target spirv64-amd-amdhsa`` can be used to compile for AMDGCN flavoured SPIR-V with 64-bit pointer width.
126
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700127.. _spirv-extensions:
Joshua Cranmerbcad1612023-03-13 14:14:12 -0400128
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700129Extensions
130----------
131
Alex Voicu88e2bb42024-06-07 13:50:23 +0300132The SPIR-V backend supports a variety of `extensions <https://github.com/KhronosGroup/SPIRV-Registry/tree/main/extensions>`_
133that enable or enhance features beyond the core SPIR-V specification.
134These extensions can be enabled using the ``-spirv-extensions`` option
135followed by the name of the extension(s) you wish to enable. Below is a
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700136list of supported SPIR-V extensions, sorted alphabetically by their extension names:
137
138.. list-table:: Supported SPIR-V Extensions
139 :widths: 50 150
140 :header-rows: 1
141
142 * - Extension Name
143 - Description
Vyacheslav Levytskyy874b4fb2024-12-03 16:18:06 +0100144 * - ``SPV_EXT_arithmetic_fence``
145 - Adds an instruction that prevents fast-math optimizations between its argument and the expression that contains it.
146 * - ``SPV_EXT_demote_to_helper_invocation``
147 - Adds an instruction that demotes a fragment shader invocation to a helper invocation.
148 * - ``SPV_EXT_optnone``
149 - Adds OptNoneEXT value for Function Control mask that indicates a request to not optimize the function.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700150 * - ``SPV_EXT_shader_atomic_float16_add``
151 - Extends the SPV_EXT_shader_atomic_float_add extension to support atomically adding to 16-bit floating-point numbers in memory.
152 * - ``SPV_EXT_shader_atomic_float_add``
153 - Adds atomic add instruction on floating-point numbers.
154 * - ``SPV_EXT_shader_atomic_float_min_max``
155 - Adds atomic min and max instruction on floating-point numbers.
156 * - ``SPV_INTEL_arbitrary_precision_integers``
157 - Allows generating arbitrary width integer types.
Dmitry Sidorov55fa2fa2025-02-20 10:27:15 +0100158 * - ``SPV_INTEL_bindless_images``
159 - Adds instructions to convert convert unsigned integer handles to images, samplers and sampled images.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700160 * - ``SPV_INTEL_bfloat16_conversion``
161 - Adds instructions to convert between single-precision 32-bit floating-point values and 16-bit bfloat16 values.
Vyacheslav Levytskyyf63adf32024-05-29 12:53:08 +0200162 * - ``SPV_INTEL_cache_controls``
163 - Allows cache control information to be applied to memory access instructions.
Vyacheslav Levytskyy978de2d2024-12-16 10:29:46 +0100164 * - ``SPV_INTEL_float_controls2``
165 - Adds execution modes and decorations to control floating-point computations.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700166 * - ``SPV_INTEL_function_pointers``
167 - Allows translation of function pointers.
Vyacheslav Levytskyy214e6b42024-05-24 15:15:03 +0200168 * - ``SPV_INTEL_inline_assembly``
169 - Allows to use inline assembly.
Vyacheslav Levytskyyf63adf32024-05-29 12:53:08 +0200170 * - ``SPV_INTEL_global_variable_host_access``
171 - Adds decorations that can be applied to global (module scope) variables.
172 * - ``SPV_INTEL_global_variable_fpga_decorations``
173 - Adds decorations that can be applied to global (module scope) variables to help code generation for FPGA devices.
Viktoria Maximova4a6ecd32024-12-03 13:47:18 +0100174 * - ``SPV_INTEL_media_block_io``
175 - Adds additional subgroup block read and write functionality that allow applications to flexibly specify the width and height of the block to read from or write to a 2D image.
Dmitry Sidorov7a44ff12025-03-06 19:44:21 +0100176 * - ``SPV_INTEL_memory_access_aliasing``
177 - Adds instructions and decorations to specify memory access aliasing, similar to alias.scope and noalias LLVM metadata.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700178 * - ``SPV_INTEL_optnone``
179 - Adds OptNoneINTEL value for Function Control mask that indicates a request to not optimize the function.
Vyacheslav Levytskyybfe84f72024-10-15 18:43:09 +0200180 * - ``SPV_INTEL_split_barrier``
181 - Adds SPIR-V instructions to split a control barrier into two separate operations: the first indicates that an invocation has "arrived" at the barrier but should continue executing, and the second indicates that an invocation should "wait" for other invocations to arrive at the barrier before executing further.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700182 * - ``SPV_INTEL_subgroups``
183 - Allows work items in a subgroup to share data without the use of local memory and work group barriers, and to utilize specialized hardware to load and store blocks of data from images or buffers.
184 * - ``SPV_INTEL_usm_storage_classes``
185 - Introduces two new storage classes that are subclasses of the CrossWorkgroup storage class that provides additional information that can enable optimization.
186 * - ``SPV_INTEL_variable_length_array``
187 - Allows to allocate local arrays whose number of elements is unknown at compile time.
Dmitry Sidorovd057b532024-12-04 19:00:19 +0100188 * - ``SPV_INTEL_joint_matrix``
189 - Adds few matrix capabilities on top of SPV_KHR_cooperative_matrix extension, such as matrix prefetch, get element coordinate and checked load/store/construct instructions, tensor float 32 and bfloat type interpretations for multuply-add instruction.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700190 * - ``SPV_KHR_bit_instructions``
191 - Enables bit instructions to be used by SPIR-V modules without requiring the Shader capability.
192 * - ``SPV_KHR_expect_assume``
193 - Provides additional information to a compiler, similar to the llvm.assume and llvm.expect intrinsics.
194 * - ``SPV_KHR_float_controls``
195 - Provides new execution modes to control floating-point computations by overriding an implementation’s default behavior for rounding modes, denormals, signed zero, and infinities.
Finn Plummer3cdac062024-11-05 10:29:08 -0800196 * - ``SPV_KHR_integer_dot_product``
197 - Adds instructions for dot product operations on integer vectors with optional accumulation. Integer vectors includes 4-component vector of 8 bit integers and 4-component vectors of 8 bit integers packed into 32-bit integers.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700198 * - ``SPV_KHR_linkonce_odr``
199 - Allows to use the LinkOnceODR linkage type that lets a function or global variable to be merged with other functions or global variables of the same name when linkage occurs.
200 * - ``SPV_KHR_no_integer_wrap_decoration``
201 - Adds decorations to indicate that a given instruction does not cause integer wrapping.
Vyacheslav Levytskyy0f26aa52024-05-24 15:33:07 +0200202 * - ``SPV_KHR_shader_clock``
203 - Adds the extension cl_khr_kernel_clock that adds the ability for a kernel to sample the value from clocks provided by compute units.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700204 * - ``SPV_KHR_subgroup_rotate``
205 - Adds a new instruction that enables rotating values across invocations within a subgroup.
206 * - ``SPV_KHR_uniform_group_instructions``
207 - Allows support for additional group operations within uniform control flow.
bwlodarcz62da3592024-08-23 05:27:36 +0200208 * - ``SPV_KHR_non_semantic_info``
209 - Adds the ability to declare extended instruction sets that have no semantic impact and can be safely removed from a module.
Viktoria Maximovaa58a6a92025-03-14 12:12:28 +0100210 * - ``SPV_INTEL_fp_max_error``
211 - Adds the ability to specify the maximum error for floating-point operations.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700212
Vishakh Prakash05f8e082025-01-30 23:17:04 +0530213To enable multiple extensions, list them separated by comma. For example, to enable support for atomic operations on floating-point numbers and arbitrary precision integers, use:
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700214
215``-spirv-ext=+SPV_EXT_shader_atomic_float_add,+SPV_INTEL_arbitrary_precision_integers``
216
217To enable all extensions, use the following option:
218``-spirv-ext=all``
219
220To enable all extensions except specified, specify ``all`` followed by a list of disallowed extensions. For example:
221``-spirv-ext=all,-SPV_INTEL_arbitrary_precision_integers``
222
223SPIR-V representation in LLVM IR
224================================
225
Alex Voicu88e2bb42024-06-07 13:50:23 +0300226SPIR-V is intentionally designed for seamless integration with various Intermediate
227Representations (IRs), including LLVM IR, facilitating straightforward mappings for
228most of its entities. The development of the SPIR-V backend has been guided by a
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700229principle of compatibility with the `Khronos Group SPIR-V LLVM Translator <https://github.com/KhronosGroup/SPIRV-LLVM-Translator>`_.
Alex Voicu88e2bb42024-06-07 13:50:23 +0300230Consequently, the input representation accepted by the SPIR-V backend aligns closely
231with that detailed in `the SPIR-V Representation in LLVM document <https://github.com/KhronosGroup/SPIRV-LLVM-Translator/blob/main/docs/SPIRVRepresentationInLLVM.rst>`_.
232This document, along with the sections that follow, delineate the main points and focus
233on any differences between the LLVM IR that this backend processes and the conventions
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700234used by other tools.
235
236.. _spirv-special-types:
237
238Special types
239-------------
Joshua Cranmerbcad1612023-03-13 14:14:12 -0400240
241SPIR-V specifies several kinds of opaque types. These types are represented
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700242using target extension types and are represented as follows:
Joshua Cranmerbcad1612023-03-13 14:14:12 -0400243
244 .. table:: SPIR-V Opaque Types
245
Steven Perron51147582024-10-03 14:11:06 -0400246 ================== ====================== ===========================================================================================
Joshua Cranmerbcad1612023-03-13 14:14:12 -0400247 SPIR-V Type LLVM type name LLVM type arguments
Steven Perron51147582024-10-03 14:11:06 -0400248 ================== ====================== ===========================================================================================
249 OpTypeImage ``spirv.Image`` sampled type, dimensionality, depth, arrayed, MS, sampled, image format, [access qualifier]
Joshua Cranmerbcad1612023-03-13 14:14:12 -0400250 OpTypeSampler ``spirv.Sampler`` (none)
Steven Perron51147582024-10-03 14:11:06 -0400251 OpTypeSampledImage ``spirv.SampledImage`` sampled type, dimensionality, depth, arrayed, MS, sampled, image format, [access qualifier]
Joshua Cranmerbcad1612023-03-13 14:14:12 -0400252 OpTypeEvent ``spirv.Event`` (none)
253 OpTypeDeviceEvent ``spirv.DeviceEvent`` (none)
254 OpTypeReserveId ``spirv.ReserveId`` (none)
255 OpTypeQueue ``spirv.Queue`` (none)
256 OpTypePipe ``spirv.Pipe`` access qualifier
257 OpTypePipeStorage ``spirv.PipeStorage`` (none)
Steven Perron51147582024-10-03 14:11:06 -0400258 ================== ====================== ===========================================================================================
Joshua Cranmerbcad1612023-03-13 14:14:12 -0400259
260All integer arguments take the same value as they do in their `corresponding
261SPIR-V instruction <https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_type_declaration_instructions>`_.
262For example, the OpenCL type ``image2d_depth_ro_t`` would be represented in
263SPIR-V IR as ``target("spirv.Image", void, 1, 1, 0, 0, 0, 0, 0)``, with its
264dimensionality parameter as ``1`` meaning 2D. Sampled image types include the
265parameters of its underlying image type, so that a sampled image for the
266previous type has the representation
267``target("spirv.SampledImage, void, 1, 1, 0, 0, 0, 0, 0)``.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700268
269.. _spirv-intrinsics:
270
271Target Intrinsics
272-----------------
273
Alex Voicu88e2bb42024-06-07 13:50:23 +0300274The SPIR-V backend employs several LLVM IR intrinsics that facilitate various low-level
275operations essential for generating correct and efficient SPIR-V code. These intrinsics
276cover a range of functionalities from type assignment and memory management to control
277flow and atomic operations. Below is a detailed table of selected intrinsics used in the
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700278SPIR-V backend, along with their descriptions and argument details.
279
280.. list-table:: LLVM IR Intrinsics for SPIR-V
281 :widths: 25 15 20 40
282 :header-rows: 1
283
284 * - Intrinsic ID
285 - Return Type
286 - Argument Types
287 - Description
288 * - `int_spv_assign_type`
289 - None
290 - `[Type, Metadata]`
291 - Associates a type with metadata, crucial for maintaining type information in SPIR-V structures. Not emitted directly but supports the type system internally.
292 * - `int_spv_assign_ptr_type`
293 - None
294 - `[Type, Metadata, Integer]`
295 - Similar to `int_spv_assign_type`, but for pointer types with an additional integer specifying the storage class. Supports SPIR-V's detailed pointer type system. Not emitted directly.
296 * - `int_spv_assign_name`
297 - None
298 - `[Type, Vararg]`
299 - Assigns names to types or values, enhancing readability and debuggability of SPIR-V code. Not emitted directly but used for metadata enrichment.
Vyacheslav Levytskyya059b292024-09-26 10:57:02 +0200300 * - `int_spv_value_md`
301 - None
302 - `[Metadata]`
303 - Assigns a set of attributes (such as name and data type) to a value that is the argument of the associated `llvm.fake.use` intrinsic call. The latter is used as a mean to map virtual registers created by IRTranslator to the original value.
Vyacheslav Levytskyybe9b4da2024-05-14 11:35:11 +0200304 * - `int_spv_assign_decoration`
305 - None
306 - `[Type, Metadata]`
307 - Assigns decoration to values by associating them with metadatas. Not emitted directly but used to support SPIR-V representation in LLVM IR.
Dmitry Sidorov7a44ff12025-03-06 19:44:21 +0100308 * - `int_spv_assign_aliasing_decoration`
309 - None
310 - `[Type, 32-bit Integer, Metadata]`
311 - Assigns one of two memory aliasing decorations (specified by the second argument) to instructions using original aliasing metadata node. Not emitted directly but used to support SPIR-V representation in LLVM IR.
Viktoria Maximovaa58a6a92025-03-14 12:12:28 +0100312 * - `int_spv_assign_fpmaxerror_decoration`
313 - None
314 - `[Type, Metadata]`
315 - Assigns the maximum error decoration to floating-point instructions using the original metadata node. Not emitted directly but used to support SPIR-V representation in LLVM IR.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700316 * - `int_spv_track_constant`
317 - Type
318 - `[Type, Metadata]`
319 - Tracks constants in the SPIR-V module. Essential for optimizing and reducing redundancy. Emitted for internal use only.
320 * - `int_spv_init_global`
321 - None
322 - `[Type, Type]`
323 - Initializes global variables, a necessary step for ensuring correct global state management in SPIR-V. Emitted for internal use only.
324 * - `int_spv_unref_global`
325 - None
326 - `[Type]`
327 - Manages the lifetime of global variables by marking them as unreferenced, thus enabling optimizations related to global variable usage. Emitted for internal use only.
328 * - `int_spv_gep`
329 - Pointer
330 - `[Boolean, Type, Vararg]`
331 - Computes the address of a sub-element of an aggregate type. Critical for accessing array elements and structure fields. Supports conditionally addressing elements in a generic way.
332 * - `int_spv_load`
333 - 32-bit Integer
334 - `[Pointer, 16-bit Integer, 8-bit Integer]`
335 - Loads a value from a memory location. The additional integers specify memory access and alignment details, vital for ensuring correct and efficient memory operations.
336 * - `int_spv_store`
337 - None
338 - `[Type, Pointer, 16-bit Integer, 8-bit Integer]`
339 - Stores a value to a memory location. Like `int_spv_load`, it includes specifications for memory access and alignment, essential for memory operations.
340 * - `int_spv_extractv`
341 - Type
342 - `[32-bit Integer, Vararg]`
343 - Extracts a value from a vector, allowing for vector operations within SPIR-V. Enables manipulation of vector components.
344 * - `int_spv_insertv`
345 - 32-bit Integer
346 - `[32-bit Integer, Type, Vararg]`
347 - Inserts a value into a vector. Complementary to `int_spv_extractv`, it facilitates the construction and manipulation of vectors.
348 * - `int_spv_extractelt`
349 - Type
350 - `[Type, Any Integer]`
351 - Extracts an element from an aggregate type based on an index. Essential for operations on arrays and vectors.
352 * - `int_spv_insertelt`
353 - Type
354 - `[Type, Type, Any Integer]`
355 - Inserts an element into an aggregate type at a specified index. Allows for building and modifying arrays and vectors.
356 * - `int_spv_const_composite`
Vyacheslav Levytskyy2ed8ff32024-05-17 11:42:37 +0200357 - Type
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700358 - `[Vararg]`
359 - Constructs a composite type from given elements. Key for creating arrays, structs, and vectors from individual components.
360 * - `int_spv_bitcast`
361 - Type
362 - `[Type]`
363 - Performs a bit-wise cast between types. Critical for type conversions that do not change the bit representation.
364 * - `int_spv_ptrcast`
365 - Type
366 - `[Type, Metadata, Integer]`
367 - Casts pointers between different types. Similar to `int_spv_bitcast` but specifically for pointers, taking into account SPIR-V's strict type system.
368 * - `int_spv_switch`
369 - None
370 - `[Type, Vararg]`
371 - Implements a multi-way branch based on a value. Enables complex control flow structures, similar to the switch statement in high-level languages.
372 * - `int_spv_cmpxchg`
373 - 32-bit Integer
374 - `[Type, Vararg]`
375 - Performs an atomic compare-and-exchange operation. Crucial for synchronization and concurrency control in compute shaders.
376 * - `int_spv_unreachable`
377 - None
378 - `[]`
379 - Marks a point in the code that should never be reached, enabling optimizations by indicating unreachable code paths.
380 * - `int_spv_alloca`
381 - Type
382 - `[]`
383 - Allocates memory on the stack. Fundamental for local variable storage in functions.
384 * - `int_spv_alloca_array`
385 - Type
386 - `[Any Integer]`
387 - Allocates an array on the stack. Extends `int_spv_alloca` to support array allocations, essential for temporary arrays.
388 * - `int_spv_undef`
389 - 32-bit Integer
390 - `[]`
391 - Generates an undefined value. Useful for optimizations and indicating uninitialized variables.
Vyacheslav Levytskyy214e6b42024-05-24 15:15:03 +0200392 * - `int_spv_inline_asm`
393 - None
394 - `[Metadata, Metadata, Vararg]`
395 - Associates inline assembly features to inline assembly call instances by creating metadatas and preserving original arguments. Not emitted directly but used to support SPIR-V representation in LLVM IR.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700396 * - `int_spv_assume`
397 - None
398 - `[1-bit Integer]`
399 - Provides hints to the optimizer about assumptions that can be made about program state. Improves optimization potential.
400 * - `int_spv_expect`
401 - Any Integer Type
402 - `[Type, Type]`
403 - Guides branch prediction by indicating expected branch paths. Enhances performance by optimizing common code paths.
404 * - `int_spv_thread_id`
405 - 32-bit Integer
406 - `[32-bit Integer]`
407 - Retrieves the thread ID within a workgroup. Essential for identifying execution context in parallel compute operations.
Cassandra Beckley1f152522025-03-19 08:04:41 -0700408 * - `int_spv_flattened_thread_id_in_group`
409 - 32-bit Integer
410 - `[32-bit Integer]`
411 - Provides a flattened index for a given thread within a given group (SV_GroupIndex)
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700412 * - `int_spv_create_handle`
413 - Pointer
414 - `[8-bit Integer]`
415 - Creates a resource handle for graphics or compute resources. Facilitates the management and use of resources in shaders.
Justin Bogneraa07f922024-12-19 12:17:21 -0700416 * - `int_spv_resource_handlefrombinding`
Steven Perron5af7ae52024-10-08 12:31:23 -0400417 - spirv.Image
418 - `[32-bit Integer set, 32-bit Integer binding, 32-bit Integer arraySize, 32-bit Integer index, bool isUniformIndex]`
419 - Returns the handle for the resource at the given set and binding.\
420 If `arraySize > 1`, then the binding represents an array of resources\
421 of the given size, and the handle for the resource at the given index is returned.\
422 If the index is possibly non-uniform, then `isUniformIndex` must get set to true.
Steven Perronba572ab2024-11-12 14:04:45 -0500423 * - `int_spv_typeBufferLoad`
424 - Scalar or vector
425 - `[spirv.Image ImageBuffer, 32-bit Integer coordinate]`
426 - Loads a value from a Vulkan image buffer at the given coordinate. The \
427 image buffer data is assumed to be stored as a 4-element vector. If the \
428 return type is a scalar, then the first element of the vector is \
429 returned. If the return type is an n-element vector, then the first \
430 n-elements of the 4-element vector are returned.
Justin Bogneraa07f922024-12-19 12:17:21 -0700431 * - `int_spv_resource_store_typedbuffer`
Steven Perron756fe542024-11-18 09:06:05 -0500432 - void
433 - `[spirv.Image Image, 32-bit Integer coordinate, vec4 data]`
434 - Stores the data to the image buffer at the given coordinate. The \
435 data must be a 4-element vector.
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700436
437.. _spirv-builtin-functions:
438
439Builtin Functions
440-----------------
441
Alex Voicu88e2bb42024-06-07 13:50:23 +0300442The following section highlights the representation of SPIR-V builtins in LLVM IR,
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700443emphasizing builtins that do not have direct counterparts in LLVM.
444
445Instructions as Function Calls
446~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
447
Alex Voicu88e2bb42024-06-07 13:50:23 +0300448SPIR-V builtins without direct LLVM counterparts are represented as LLVM function calls.
449These functions, termed SPIR-V builtin functions, follow an IA64 mangling scheme with
450SPIR-V-specific extensions. Parsing non-mangled calls to builtins is supported in some cases,
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700451but not tested extensively. The general format is:
452
453.. code-block:: c
454
455 __spirv_{OpCodeName}{_OptionalPostfixes}
456
Alex Voicu88e2bb42024-06-07 13:50:23 +0300457Where `{OpCodeName}` is the SPIR-V opcode name sans the "Op" prefix, and
458`{OptionalPostfixes}` are decoration-specific postfixes, if any. The mangling and
459postfixes allow for the representation of SPIR-V's rich instruction set within LLVM's
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700460framework.
461
462Extended Instruction Sets
463~~~~~~~~~~~~~~~~~~~~~~~~~
464
Alex Voicu88e2bb42024-06-07 13:50:23 +0300465SPIR-V defines several extended instruction sets for additional functionalities, such as
466OpenCL-specific operations. In LLVM IR, these are represented by function calls to
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700467mangled builtins and selected based on the environment. For example:
468
469.. code-block:: c
470
471 acos_f32
472
Alex Voicu88e2bb42024-06-07 13:50:23 +0300473represents the `acos` function from the OpenCL extended instruction set for a float32
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700474input.
475
476Builtin Variables
477~~~~~~~~~~~~~~~~~
478
Alex Voicu88e2bb42024-06-07 13:50:23 +0300479SPIR-V builtin variables, which provide access to special hardware or execution model
480properties, are mapped to either LLVM function calls or LLVM global variables. The
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700481representation follows the naming convention:
482
483.. code-block:: c
484
485 __spirv_BuiltIn{VariableName}
486
Alex Voicu88e2bb42024-06-07 13:50:23 +0300487For instance, the SPIR-V builtin `GlobalInvocationId` is accessible in LLVM IR as
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700488`__spirv_BuiltInGlobalInvocationId`.
489
490Vector Load and Store Builtins
491~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
492
Alex Voicu88e2bb42024-06-07 13:50:23 +0300493SPIR-V's capabilities for loading and storing vectors are represented in LLVM IR using
494functions that mimic the SPIR-V instructions. These builtins handle cases that LLVM's
495native instructions do not directly support, enabling fine-grained control over memory
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700496operations.
497
498Atomic Operations
499~~~~~~~~~~~~~~~~~
500
Alex Voicu88e2bb42024-06-07 13:50:23 +0300501SPIR-V's atomic operations, especially those operating on floating-point data, are
502represented in LLVM IR with corresponding function calls. These builtins ensure
503atomicity in operations where LLVM might not have direct support, essential for parallel
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700504execution and synchronization.
505
506Image Operations
507~~~~~~~~~~~~~~~~
508
Alex Voicu88e2bb42024-06-07 13:50:23 +0300509SPIR-V provides extensive support for image and sampler operations, which LLVM
510represents through function calls to builtins. These include image reads, writes, and
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700511queries, allowing detailed manipulation of image data and parameters.
512
513Group and Subgroup Operations
514~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
515
Alex Voicu88e2bb42024-06-07 13:50:23 +0300516For workgroup and subgroup operations, LLVM uses function calls to represent SPIR-V's
517group-based instructions. These builtins facilitate group synchronization, data sharing,
Michal Paszkowskif5e1cd52024-03-26 03:06:49 -0700518and collective operations essential for efficient parallel computation.