OSDN Git Service

[AMDGPU][MC][DOC] Updated AMD GPU assembler syntax description.
[android-x86/external-llvm.git] / docs / AMDGPUUsage.rst
1 =============================
2 User Guide for AMDGPU Backend
3 =============================
4
5 .. contents::
6    :local:
7
8 Introduction
9 ============
10
11 The AMDGPU backend provides ISA code generation for AMD GPUs, starting with the
12 R600 family up until the current GCN families. It lives in the
13 ``lib/Target/AMDGPU`` directory.
14
15 LLVM
16 ====
17
18 .. _amdgpu-target-triples:
19
20 Target Triples
21 --------------
22
23 Use the ``clang -target <Architecture>-<Vendor>-<OS>-<Environment>`` option to
24 specify the target triple:
25
26   .. table:: AMDGPU Architectures
27      :name: amdgpu-architecture-table
28
29      ============ ==============================================================
30      Architecture Description
31      ============ ==============================================================
32      ``r600``     AMD GPUs HD2XXX-HD6XXX for graphics and compute shaders.
33      ``amdgcn``   AMD GPUs GCN GFX6 onwards for graphics and compute shaders.
34      ============ ==============================================================
35
36   .. table:: AMDGPU Vendors
37      :name: amdgpu-vendor-table
38
39      ============ ==============================================================
40      Vendor       Description
41      ============ ==============================================================
42      ``amd``      Can be used for all AMD GPU usage.
43      ``mesa3d``   Can be used if the OS is ``mesa3d``.
44      ============ ==============================================================
45
46   .. table:: AMDGPU Operating Systems
47      :name: amdgpu-os-table
48
49      ============== ============================================================
50      OS             Description
51      ============== ============================================================
52      *<empty>*      Defaults to the *unknown* OS.
53      ``amdhsa``     Compute kernels executed on HSA [HSA]_ compatible runtimes
54                     such as AMD's ROCm [AMD-ROCm]_.
55      ``amdpal``     Graphic shaders and compute kernels executed on AMD PAL
56                     runtime.
57      ``mesa3d``     Graphic shaders and compute kernels executed on Mesa 3D
58                     runtime.
59      ============== ============================================================
60
61   .. table:: AMDGPU Environments
62      :name: amdgpu-environment-table
63
64      ============ ==============================================================
65      Environment  Description
66      ============ ==============================================================
67      *<empty>*    Default.
68      ============ ==============================================================
69
70 .. _amdgpu-processors:
71
72 Processors
73 ----------
74
75 Use the ``clang -mcpu <Processor>`` option to specify the AMD GPU processor. The
76 names from both the *Processor* and *Alternative Processor* can be used.
77
78   .. table:: AMDGPU Processors
79      :name: amdgpu-processor-table
80
81      =========== =============== ============ ===== ================= ======= ======================
82      Processor   Alternative     Target       dGPU/ Target            ROCm    Example
83                  Processor       Triple       APU   Features          Support Products
84                                  Architecture       Supported
85                                                     [Default]
86      =========== =============== ============ ===== ================= ======= ======================
87      **Radeon HD 2000/3000 Series (R600)** [AMD-RADEON-HD-2000-3000]_
88      -----------------------------------------------------------------------------------------------
89      ``r600``                    ``r600``     dGPU
90      ``r630``                    ``r600``     dGPU
91      ``rs880``                   ``r600``     dGPU
92      ``rv670``                   ``r600``     dGPU
93      **Radeon HD 4000 Series (R700)** [AMD-RADEON-HD-4000]_
94      -----------------------------------------------------------------------------------------------
95      ``rv710``                   ``r600``     dGPU
96      ``rv730``                   ``r600``     dGPU
97      ``rv770``                   ``r600``     dGPU
98      **Radeon HD 5000 Series (Evergreen)** [AMD-RADEON-HD-5000]_
99      -----------------------------------------------------------------------------------------------
100      ``cedar``                   ``r600``     dGPU
101      ``cypress``                 ``r600``     dGPU
102      ``juniper``                 ``r600``     dGPU
103      ``redwood``                 ``r600``     dGPU
104      ``sumo``                    ``r600``     dGPU
105      **Radeon HD 6000 Series (Northern Islands)** [AMD-RADEON-HD-6000]_
106      -----------------------------------------------------------------------------------------------
107      ``barts``                   ``r600``     dGPU
108      ``caicos``                  ``r600``     dGPU
109      ``cayman``                  ``r600``     dGPU
110      ``turks``                   ``r600``     dGPU
111      **GCN GFX6 (Southern Islands (SI))** [AMD-GCN-GFX6]_
112      -----------------------------------------------------------------------------------------------
113      ``gfx600``  - ``tahiti``    ``amdgcn``   dGPU
114      ``gfx601``  - ``hainan``    ``amdgcn``   dGPU
115                  - ``oland``
116                  - ``pitcairn``
117                  - ``verde``
118      **GCN GFX7 (Sea Islands (CI))** [AMD-GCN-GFX7]_
119      -----------------------------------------------------------------------------------------------
120      ``gfx700``  - ``kaveri``    ``amdgcn``   APU                             - A6-7000
121                                                                               - A6 Pro-7050B
122                                                                               - A8-7100
123                                                                               - A8 Pro-7150B
124                                                                               - A10-7300
125                                                                               - A10 Pro-7350B
126                                                                               - FX-7500
127                                                                               - A8-7200P
128                                                                               - A10-7400P
129                                                                               - FX-7600P
130      ``gfx701``  - ``hawaii``    ``amdgcn``   dGPU                    ROCm    - FirePro W8100
131                                                                               - FirePro W9100
132                                                                               - FirePro S9150
133                                                                               - FirePro S9170
134      ``gfx702``                  ``amdgcn``   dGPU                    ROCm    - Radeon R9 290
135                                                                               - Radeon R9 290x
136                                                                               - Radeon R390
137                                                                               - Radeon R390x
138      ``gfx703``  - ``kabini``    ``amdgcn``   APU                             - E1-2100
139                  - ``mullins``                                                - E1-2200
140                                                                               - E1-2500
141                                                                               - E2-3000
142                                                                               - E2-3800
143                                                                               - A4-5000
144                                                                               - A4-5100
145                                                                               - A6-5200
146                                                                               - A4 Pro-3340B
147      ``gfx704``  - ``bonaire``   ``amdgcn``   dGPU                            - Radeon HD 7790
148                                                                               - Radeon HD 8770
149                                                                               - R7 260
150                                                                               - R7 260X
151      **GCN GFX8 (Volcanic Islands (VI))** [AMD-GCN-GFX8]_
152      -----------------------------------------------------------------------------------------------
153      ``gfx801``  - ``carrizo``   ``amdgcn``   APU   - xnack                   - A6-8500P
154                                                       [on]                    - Pro A6-8500B
155                                                                               - A8-8600P
156                                                                               - Pro A8-8600B
157                                                                               - FX-8800P
158                                                                               - Pro A12-8800B
159      \                           ``amdgcn``   APU   - xnack           ROCm    - A10-8700P
160                                                       [on]                    - Pro A10-8700B
161                                                                               - A10-8780P
162      \                           ``amdgcn``   APU   - xnack                   - A10-9600P
163                                                       [on]                    - A10-9630P
164                                                                               - A12-9700P
165                                                                               - A12-9730P
166                                                                               - FX-9800P
167                                                                               - FX-9830P
168      \                           ``amdgcn``   APU   - xnack                   - E2-9010
169                                                       [on]                    - A6-9210
170                                                                               - A9-9410
171      ``gfx802``  - ``iceland``   ``amdgcn``   dGPU  - xnack           ROCm    - FirePro S7150
172                  - ``tonga``                          [off]                   - FirePro S7100
173                                                                               - FirePro W7100
174                                                                               - Radeon R285
175                                                                               - Radeon R9 380
176                                                                               - Radeon R9 385
177                                                                               - Mobile FirePro
178                                                                                 M7170
179      ``gfx803``  - ``fiji``      ``amdgcn``   dGPU  - xnack           ROCm    - Radeon R9 Nano
180                                                       [off]                   - Radeon R9 Fury
181                                                                               - Radeon R9 FuryX
182                                                                               - Radeon Pro Duo
183                                                                               - FirePro S9300x2
184                                                                               - Radeon Instinct MI8
185      \           - ``polaris10`` ``amdgcn``   dGPU  - xnack           ROCm    - Radeon RX 470
186                                                       [off]                   - Radeon RX 480
187                                                                               - Radeon Instinct MI6
188      \           - ``polaris11`` ``amdgcn``   dGPU  - xnack           ROCm    - Radeon RX 460
189                                                       [off]
190      ``gfx810``  - ``stoney``    ``amdgcn``   APU   - xnack
191                                                       [on]
192      **GCN GFX9** [AMD-GCN-GFX9]_
193      -----------------------------------------------------------------------------------------------
194      ``gfx900``                  ``amdgcn``   dGPU  - xnack           ROCm    - Radeon Vega
195                                                       [off]                     Frontier Edition
196                                                                               - Radeon RX Vega 56
197                                                                               - Radeon RX Vega 64
198                                                                               - Radeon RX Vega 64
199                                                                                 Liquid
200                                                                               - Radeon Instinct MI25
201      ``gfx902``                  ``amdgcn``   APU   - xnack                   - Ryzen 3 2200G
202                                                       [on]                    - Ryzen 5 2400G
203      ``gfx904``                  ``amdgcn``   dGPU  - xnack                   *TBA*
204                                                       [off]
205                                                                               .. TODO
206                                                                                  Add product
207                                                                                  names.
208      ``gfx906``                  ``amdgcn``   dGPU  - xnack                   - Radeon Instinct MI50
209                                                       [off]                   - Radeon Instinct MI60
210      ``gfx909``                  ``amdgcn``   APU   - xnack                   *TBA* (Raven Ridge 2)
211                                                       [on]
212                                                                               .. TODO
213                                                                                  Add product
214                                                                                  names.
215      **GCN GFX10** [AMD-GCN-GFX10]_
216      -----------------------------------------------------------------------------------------------
217      ``gfx1010``                 ``amdgcn``   dGPU  - xnack                   *TBA*
218                                                       [off]
219                                                     - wavefrontsize64
220                                                       [off]
221                                                     - cumode
222                                                       [off]
223                                                                               .. TODO
224                                                                                  Add product
225                                                                                  names.
226      ``gfx1011``                 ``amdgcn``   dGPU  - xnack                   *TBA*
227                                                       [off]
228                                                     - wavefrontsize64
229                                                       [off]
230                                                     - cumode
231                                                       [off]
232                                                                               .. TODO
233                                                                                  Add product
234                                                                                  names.
235      ``gfx1012``                 ``amdgcn``   dGPU  - xnack                   *TBA*
236                                                       [off]
237                                                     - wavefrontsize64
238                                                       [off]
239                                                     - cumode
240                                                       [off]
241                                                                               .. TODO
242                                                                                  Add product
243                                                                                  names.
244      =========== =============== ============ ===== ================= ======= ======================
245
246 .. _amdgpu-target-features:
247
248 Target Features
249 ---------------
250
251 Target features control how code is generated to support certain
252 processor specific features. Not all target features are supported by
253 all processors. The runtime must ensure that the features supported by
254 the device used to execute the code match the features enabled when
255 generating the code. A mismatch of features may result in incorrect
256 execution, or a reduction in performance.
257
258 The target features supported by each processor, and the default value
259 used if not specified explicitly, is listed in
260 :ref:`amdgpu-processor-table`.
261
262 Use the ``clang -m[no-]<TargetFeature>`` option to specify the AMD GPU
263 target features.
264
265 For example:
266
267 ``-mxnack``
268   Enable the ``xnack`` feature.
269 ``-mno-xnack``
270   Disable the ``xnack`` feature.
271
272   .. table:: AMDGPU Target Features
273      :name: amdgpu-target-feature-table
274
275      ====================== ==================================================
276      Target Feature         Description
277      ====================== ==================================================
278      -m[no-]xnack           Enable/disable generating code that has
279                             memory clauses that are compatible with
280                             having XNACK replay enabled.
281
282                             This is used for demand paging and page
283                             migration. If XNACK replay is enabled in
284                             the device, then if a page fault occurs
285                             the code may execute incorrectly if the
286                             ``xnack`` feature is not enabled. Executing
287                             code that has the feature enabled on a
288                             device that does not have XNACK replay
289                             enabled will execute correctly, but may
290                             be less performant than code with the
291                             feature disabled.
292
293      -m[no-]sram-ecc        Enable/disable generating code that assumes SRAM
294                             ECC is enabled/disabled.
295
296      -m[no-]wavefrontsize64 Control the default wavefront size used when
297                             generating code for kernels. When disabled
298                             native wavefront size 32 is used, when enabled
299                             wavefront size 64 is used.
300
301      -m[no-]cumode          Control the default wavefront execution mode used
302                             when generating code for kernels. When disabled
303                             native WGP wavefront execution mode is used,
304                             when enabled CU wavefront execution mode is used
305                             (see :ref:`amdgpu-amdhsa-memory-model`).
306      ====================== ==================================================
307
308 .. _amdgpu-address-spaces:
309
310 Address Spaces
311 --------------
312
313 The AMDGPU backend uses the following address space mappings.
314
315 The memory space names used in the table, aside from the region memory space, is
316 from the OpenCL standard.
317
318 LLVM Address Space number is used throughout LLVM (for example, in LLVM IR).
319
320   .. table:: Address Space Mapping
321      :name: amdgpu-address-space-mapping-table
322
323      ================== =================================
324      LLVM Address Space Memory Space
325      ================== =================================
326      0                  Generic (Flat)
327      1                  Global
328      2                  Region (GDS)
329      3                  Local (group/LDS)
330      4                  Constant
331      5                  Private (Scratch)
332      6                  Constant 32-bit
333      7                  Buffer Fat Pointer (experimental)
334      ================== =================================
335
336 The buffer fat pointer is an experimental address space that is currently
337 unsupported in the backend. It exposes a non-integral pointer that is in future
338 intended to support the modelling of 128-bit buffer descriptors + a 32-bit
339 offset into the buffer descriptor (in total encapsulating a 160-bit 'pointer'),
340 allowing us to use normal LLVM load/store/atomic operations to model the buffer
341 descriptors used heavily in graphics workloads targeting the backend.
342
343 .. _amdgpu-memory-scopes:
344
345 Memory Scopes
346 -------------
347
348 This section provides LLVM memory synchronization scopes supported by the AMDGPU
349 backend memory model when the target triple OS is ``amdhsa`` (see
350 :ref:`amdgpu-amdhsa-memory-model` and :ref:`amdgpu-target-triples`).
351
352 The memory model supported is based on the HSA memory model [HSA]_ which is
353 based in turn on HRF-indirect with scope inclusion [HRF]_. The happens-before
354 relation is transitive over the synchonizes-with relation independent of scope,
355 and synchonizes-with allows the memory scope instances to be inclusive (see
356 table :ref:`amdgpu-amdhsa-llvm-sync-scopes-table`).
357
358 This is different to the OpenCL [OpenCL]_ memory model which does not have scope
359 inclusion and requires the memory scopes to exactly match. However, this
360 is conservatively correct for OpenCL.
361
362   .. table:: AMDHSA LLVM Sync Scopes
363      :name: amdgpu-amdhsa-llvm-sync-scopes-table
364
365      ======================= ===================================================
366      LLVM Sync Scope         Description
367      ======================= ===================================================
368      *none*                  The default: ``system``.
369
370                              Synchronizes with, and participates in modification
371                              and seq_cst total orderings with, other operations
372                              (except image operations) for all address spaces
373                              (except private, or generic that accesses private)
374                              provided the other operation's sync scope is:
375
376                              - ``system``.
377                              - ``agent`` and executed by a thread on the same
378                                agent.
379                              - ``workgroup`` and executed by a thread in the
380                                same workgroup.
381                              - ``wavefront`` and executed by a thread in the
382                                same wavefront.
383
384      ``agent``               Synchronizes with, and participates in modification
385                              and seq_cst total orderings with, other operations
386                              (except image operations) for all address spaces
387                              (except private, or generic that accesses private)
388                              provided the other operation's sync scope is:
389
390                              - ``system`` or ``agent`` and executed by a thread
391                                on the same agent.
392                              - ``workgroup`` and executed by a thread in the
393                                same workgroup.
394                              - ``wavefront`` and executed by a thread in the
395                                same wavefront.
396
397      ``workgroup``           Synchronizes with, and participates in modification
398                              and seq_cst total orderings with, other operations
399                              (except image operations) for all address spaces
400                              (except private, or generic that accesses private)
401                              provided the other operation's sync scope is:
402
403                              - ``system``, ``agent`` or ``workgroup`` and
404                                executed by a thread in the same workgroup.
405                              - ``wavefront`` and executed by a thread in the
406                                same wavefront.
407
408      ``wavefront``           Synchronizes with, and participates in modification
409                              and seq_cst total orderings with, other operations
410                              (except image operations) for all address spaces
411                              (except private, or generic that accesses private)
412                              provided the other operation's sync scope is:
413
414                              - ``system``, ``agent``, ``workgroup`` or
415                                ``wavefront`` and executed by a thread in the
416                                same wavefront.
417
418      ``singlethread``        Only synchronizes with, and participates in
419                              modification and seq_cst total orderings with,
420                              other operations (except image operations) running
421                              in the same thread for all address spaces (for
422                              example, in signal handlers).
423
424      ``one-as``              Same as ``system`` but only synchronizes with other
425                              operations within the same address space.
426
427      ``agent-one-as``        Same as ``agent`` but only synchronizes with other
428                              operations within the same address space.
429
430      ``workgroup-one-as``    Same as ``workgroup`` but only synchronizes with
431                              other operations within the same address space.
432
433      ``wavefront-one-as``    Same as ``wavefront`` but only synchronizes with
434                              other operations within the same address space.
435
436      ``singlethread-one-as`` Same as ``singlethread`` but only synchronizes with
437                              other operations within the same address space.
438      ======================= ===================================================
439
440 AMDGPU Intrinsics
441 -----------------
442
443 The AMDGPU backend implements the following LLVM IR intrinsics.
444
445 *This section is WIP.*
446
447 .. TODO
448    List AMDGPU intrinsics
449
450 AMDGPU Attributes
451 -----------------
452
453 The AMDGPU backend supports the following LLVM IR attributes.
454
455   .. table:: AMDGPU LLVM IR Attributes
456      :name: amdgpu-llvm-ir-attributes-table
457
458      ======================================= ==========================================================
459      LLVM Attribute                          Description
460      ======================================= ==========================================================
461      "amdgpu-flat-work-group-size"="min,max" Specify the minimum and maximum flat work group sizes that
462                                              will be specified when the kernel is dispatched. Generated
463                                              by the ``amdgpu_flat_work_group_size`` CLANG attribute [CLANG-ATTR]_.
464      "amdgpu-implicitarg-num-bytes"="n"      Number of kernel argument bytes to add to the kernel
465                                              argument block size for the implicit arguments. This
466                                              varies by OS and language (for OpenCL see
467                                              :ref:`opencl-kernel-implicit-arguments-appended-for-amdhsa-os-table`).
468      "amdgpu-num-sgpr"="n"                   Specifies the number of SGPRs to use. Generated by
469                                              the ``amdgpu_num_sgpr`` CLANG attribute [CLANG-ATTR]_.
470      "amdgpu-num-vgpr"="n"                   Specifies the number of VGPRs to use. Generated by the
471                                              ``amdgpu_num_vgpr`` CLANG attribute [CLANG-ATTR]_.
472      "amdgpu-waves-per-eu"="m,n"             Specify the minimum and maximum number of waves per
473                                              execution unit. Generated by the ``amdgpu_waves_per_eu``
474                                              CLANG attribute [CLANG-ATTR]_.
475      "amdgpu-ieee" true/false.               Specify whether the function expects the IEEE field of the
476                                              mode register to be set on entry. Overrides the default for
477                                              the calling convention.
478      "amdgpu-dx10-clamp" true/false.         Specify whether the function expects the DX10_CLAMP field of
479                                              the mode register to be set on entry. Overrides the default
480                                              for the calling convention.
481      ======================================= ==========================================================
482
483 Code Object
484 ===========
485
486 The AMDGPU backend generates a standard ELF [ELF]_ relocatable code object that
487 can be linked by ``lld`` to produce a standard ELF shared code object which can
488 be loaded and executed on an AMDGPU target.
489
490 Header
491 ------
492
493 The AMDGPU backend uses the following ELF header:
494
495   .. table:: AMDGPU ELF Header
496      :name: amdgpu-elf-header-table
497
498      ========================== ===============================
499      Field                      Value
500      ========================== ===============================
501      ``e_ident[EI_CLASS]``      ``ELFCLASS64``
502      ``e_ident[EI_DATA]``       ``ELFDATA2LSB``
503      ``e_ident[EI_OSABI]``      - ``ELFOSABI_NONE``
504                                 - ``ELFOSABI_AMDGPU_HSA``
505                                 - ``ELFOSABI_AMDGPU_PAL``
506                                 - ``ELFOSABI_AMDGPU_MESA3D``
507      ``e_ident[EI_ABIVERSION]`` - ``ELFABIVERSION_AMDGPU_HSA``
508                                 - ``ELFABIVERSION_AMDGPU_PAL``
509                                 - ``ELFABIVERSION_AMDGPU_MESA3D``
510      ``e_type``                 - ``ET_REL``
511                                 - ``ET_DYN``
512      ``e_machine``              ``EM_AMDGPU``
513      ``e_entry``                0
514      ``e_flags``                See :ref:`amdgpu-elf-header-e_flags-table`
515      ========================== ===============================
516
517 ..
518
519   .. table:: AMDGPU ELF Header Enumeration Values
520      :name: amdgpu-elf-header-enumeration-values-table
521
522      =============================== =====
523      Name                            Value
524      =============================== =====
525      ``EM_AMDGPU``                   224
526      ``ELFOSABI_NONE``               0
527      ``ELFOSABI_AMDGPU_HSA``         64
528      ``ELFOSABI_AMDGPU_PAL``         65
529      ``ELFOSABI_AMDGPU_MESA3D``      66
530      ``ELFABIVERSION_AMDGPU_HSA``    1
531      ``ELFABIVERSION_AMDGPU_PAL``    0
532      ``ELFABIVERSION_AMDGPU_MESA3D`` 0
533      =============================== =====
534
535 ``e_ident[EI_CLASS]``
536   The ELF class is:
537
538   * ``ELFCLASS32`` for ``r600`` architecture.
539
540   * ``ELFCLASS64`` for ``amdgcn`` architecture which only supports 64
541     bit applications.
542
543 ``e_ident[EI_DATA]``
544   All AMDGPU targets use ``ELFDATA2LSB`` for little-endian byte ordering.
545
546 ``e_ident[EI_OSABI]``
547   One of the following AMD GPU architecture specific OS ABIs
548   (see :ref:`amdgpu-os-table`):
549
550   * ``ELFOSABI_NONE`` for *unknown* OS.
551
552   * ``ELFOSABI_AMDGPU_HSA`` for ``amdhsa`` OS.
553
554   * ``ELFOSABI_AMDGPU_PAL`` for ``amdpal`` OS.
555
556   * ``ELFOSABI_AMDGPU_MESA3D`` for ``mesa3D`` OS.
557
558 ``e_ident[EI_ABIVERSION]``
559   The ABI version of the AMD GPU architecture specific OS ABI to which the code
560   object conforms:
561
562   * ``ELFABIVERSION_AMDGPU_HSA`` is used to specify the version of AMD HSA
563     runtime ABI.
564
565   * ``ELFABIVERSION_AMDGPU_PAL`` is used to specify the version of AMD PAL
566     runtime ABI.
567
568   * ``ELFABIVERSION_AMDGPU_MESA3D`` is used to specify the version of AMD MESA
569     3D runtime ABI.
570
571 ``e_type``
572   Can be one of the following values:
573
574
575   ``ET_REL``
576     The type produced by the AMD GPU backend compiler as it is relocatable code
577     object.
578
579   ``ET_DYN``
580     The type produced by the linker as it is a shared code object.
581
582   The AMD HSA runtime loader requires a ``ET_DYN`` code object.
583
584 ``e_machine``
585   The value ``EM_AMDGPU`` is used for the machine for all processors supported
586   by the ``r600`` and ``amdgcn`` architectures (see
587   :ref:`amdgpu-processor-table`). The specific processor is specified in the
588   ``EF_AMDGPU_MACH`` bit field of the ``e_flags`` (see
589   :ref:`amdgpu-elf-header-e_flags-table`).
590
591 ``e_entry``
592   The entry point is 0 as the entry points for individual kernels must be
593   selected in order to invoke them through AQL packets.
594
595 ``e_flags``
596   The AMDGPU backend uses the following ELF header flags:
597
598   .. table:: AMDGPU ELF Header ``e_flags``
599      :name: amdgpu-elf-header-e_flags-table
600
601      ================================= ========== =============================
602      Name                              Value      Description
603      ================================= ========== =============================
604      **AMDGPU Processor Flag**                    See :ref:`amdgpu-processor-table`.
605      -------------------------------------------- -----------------------------
606      ``EF_AMDGPU_MACH``                0x000000ff AMDGPU processor selection
607                                                   mask for
608                                                   ``EF_AMDGPU_MACH_xxx`` values
609                                                   defined in
610                                                   :ref:`amdgpu-ef-amdgpu-mach-table`.
611      ``EF_AMDGPU_XNACK``               0x00000100 Indicates if the ``xnack``
612                                                   target feature is
613                                                   enabled for all code
614                                                   contained in the code object.
615                                                   If the processor
616                                                   does not support the
617                                                   ``xnack`` target
618                                                   feature then must
619                                                   be 0.
620                                                   See
621                                                   :ref:`amdgpu-target-features`.
622      ``EF_AMDGPU_SRAM_ECC``            0x00000200 Indicates if the ``sram-ecc``
623                                                   target feature is
624                                                   enabled for all code
625                                                   contained in the code object.
626                                                   If the processor
627                                                   does not support the
628                                                   ``sram-ecc`` target
629                                                   feature then must
630                                                   be 0.
631                                                   See
632                                                   :ref:`amdgpu-target-features`.
633      ================================= ========== =============================
634
635   .. table:: AMDGPU ``EF_AMDGPU_MACH`` Values
636      :name: amdgpu-ef-amdgpu-mach-table
637
638      ================================= ========== =============================
639      Name                              Value      Description (see
640                                                   :ref:`amdgpu-processor-table`)
641      ================================= ========== =============================
642      ``EF_AMDGPU_MACH_NONE``           0x000      *not specified*
643      ``EF_AMDGPU_MACH_R600_R600``      0x001      ``r600``
644      ``EF_AMDGPU_MACH_R600_R630``      0x002      ``r630``
645      ``EF_AMDGPU_MACH_R600_RS880``     0x003      ``rs880``
646      ``EF_AMDGPU_MACH_R600_RV670``     0x004      ``rv670``
647      ``EF_AMDGPU_MACH_R600_RV710``     0x005      ``rv710``
648      ``EF_AMDGPU_MACH_R600_RV730``     0x006      ``rv730``
649      ``EF_AMDGPU_MACH_R600_RV770``     0x007      ``rv770``
650      ``EF_AMDGPU_MACH_R600_CEDAR``     0x008      ``cedar``
651      ``EF_AMDGPU_MACH_R600_CYPRESS``   0x009      ``cypress``
652      ``EF_AMDGPU_MACH_R600_JUNIPER``   0x00a      ``juniper``
653      ``EF_AMDGPU_MACH_R600_REDWOOD``   0x00b      ``redwood``
654      ``EF_AMDGPU_MACH_R600_SUMO``      0x00c      ``sumo``
655      ``EF_AMDGPU_MACH_R600_BARTS``     0x00d      ``barts``
656      ``EF_AMDGPU_MACH_R600_CAICOS``    0x00e      ``caicos``
657      ``EF_AMDGPU_MACH_R600_CAYMAN``    0x00f      ``cayman``
658      ``EF_AMDGPU_MACH_R600_TURKS``     0x010      ``turks``
659      *reserved*                        0x011 -    Reserved for ``r600``
660                                        0x01f      architecture processors.
661      ``EF_AMDGPU_MACH_AMDGCN_GFX600``  0x020      ``gfx600``
662      ``EF_AMDGPU_MACH_AMDGCN_GFX601``  0x021      ``gfx601``
663      ``EF_AMDGPU_MACH_AMDGCN_GFX700``  0x022      ``gfx700``
664      ``EF_AMDGPU_MACH_AMDGCN_GFX701``  0x023      ``gfx701``
665      ``EF_AMDGPU_MACH_AMDGCN_GFX702``  0x024      ``gfx702``
666      ``EF_AMDGPU_MACH_AMDGCN_GFX703``  0x025      ``gfx703``
667      ``EF_AMDGPU_MACH_AMDGCN_GFX704``  0x026      ``gfx704``
668      *reserved*                        0x027      Reserved.
669      ``EF_AMDGPU_MACH_AMDGCN_GFX801``  0x028      ``gfx801``
670      ``EF_AMDGPU_MACH_AMDGCN_GFX802``  0x029      ``gfx802``
671      ``EF_AMDGPU_MACH_AMDGCN_GFX803``  0x02a      ``gfx803``
672      ``EF_AMDGPU_MACH_AMDGCN_GFX810``  0x02b      ``gfx810``
673      ``EF_AMDGPU_MACH_AMDGCN_GFX900``  0x02c      ``gfx900``
674      ``EF_AMDGPU_MACH_AMDGCN_GFX902``  0x02d      ``gfx902``
675      ``EF_AMDGPU_MACH_AMDGCN_GFX904``  0x02e      ``gfx904``
676      ``EF_AMDGPU_MACH_AMDGCN_GFX906``  0x02f      ``gfx906``
677      *reserved*                        0x030      Reserved.
678      ``EF_AMDGPU_MACH_AMDGCN_GFX909``  0x031      ``gfx909``
679      *reserved*                        0x032      Reserved.
680      ``EF_AMDGPU_MACH_AMDGCN_GFX1010`` 0x033      ``gfx1010``
681      ``EF_AMDGPU_MACH_AMDGCN_GFX1011`` 0x034      ``gfx1011``
682      ``EF_AMDGPU_MACH_AMDGCN_GFX1012`` 0x035      ``gfx1012``
683      ================================= ========== =============================
684
685 Sections
686 --------
687
688 An AMDGPU target ELF code object has the standard ELF sections which include:
689
690   .. table:: AMDGPU ELF Sections
691      :name: amdgpu-elf-sections-table
692
693      ================== ================ =================================
694      Name               Type             Attributes
695      ================== ================ =================================
696      ``.bss``           ``SHT_NOBITS``   ``SHF_ALLOC`` + ``SHF_WRITE``
697      ``.data``          ``SHT_PROGBITS`` ``SHF_ALLOC`` + ``SHF_WRITE``
698      ``.debug_``\ *\**  ``SHT_PROGBITS`` *none*
699      ``.dynamic``       ``SHT_DYNAMIC``  ``SHF_ALLOC``
700      ``.dynstr``        ``SHT_PROGBITS`` ``SHF_ALLOC``
701      ``.dynsym``        ``SHT_PROGBITS`` ``SHF_ALLOC``
702      ``.got``           ``SHT_PROGBITS`` ``SHF_ALLOC`` + ``SHF_WRITE``
703      ``.hash``          ``SHT_HASH``     ``SHF_ALLOC``
704      ``.note``          ``SHT_NOTE``     *none*
705      ``.rela``\ *name*  ``SHT_RELA``     *none*
706      ``.rela.dyn``      ``SHT_RELA``     *none*
707      ``.rodata``        ``SHT_PROGBITS`` ``SHF_ALLOC``
708      ``.shstrtab``      ``SHT_STRTAB``   *none*
709      ``.strtab``        ``SHT_STRTAB``   *none*
710      ``.symtab``        ``SHT_SYMTAB``   *none*
711      ``.text``          ``SHT_PROGBITS`` ``SHF_ALLOC`` + ``SHF_EXECINSTR``
712      ================== ================ =================================
713
714 These sections have their standard meanings (see [ELF]_) and are only generated
715 if needed.
716
717 ``.debug``\ *\**
718   The standard DWARF sections. See :ref:`amdgpu-dwarf` for information on the
719   DWARF produced by the AMDGPU backend.
720
721 ``.dynamic``, ``.dynstr``, ``.dynsym``, ``.hash``
722   The standard sections used by a dynamic loader.
723
724 ``.note``
725   See :ref:`amdgpu-note-records` for the note records supported by the AMDGPU
726   backend.
727
728 ``.rela``\ *name*, ``.rela.dyn``
729   For relocatable code objects, *name* is the name of the section that the
730   relocation records apply. For example, ``.rela.text`` is the section name for
731   relocation records associated with the ``.text`` section.
732
733   For linked shared code objects, ``.rela.dyn`` contains all the relocation
734   records from each of the relocatable code object's ``.rela``\ *name* sections.
735
736   See :ref:`amdgpu-relocation-records` for the relocation records supported by
737   the AMDGPU backend.
738
739 ``.text``
740   The executable machine code for the kernels and functions they call. Generated
741   as position independent code. See :ref:`amdgpu-code-conventions` for
742   information on conventions used in the isa generation.
743
744 .. _amdgpu-note-records:
745
746 Note Records
747 ------------
748
749 The AMDGPU backend code object contains ELF note records in the ``.note``
750 section. The set of generated notes and their semantics depend on the code
751 object version; see :ref:`amdgpu-note-records-v2` and
752 :ref:`amdgpu-note-records-v3`.
753
754 As required by ``ELFCLASS32`` and ``ELFCLASS64``, minimal zero byte padding
755 must be generated after the ``name`` field to ensure the ``desc`` field is 4
756 byte aligned. In addition, minimal zero byte padding must be generated to
757 ensure the ``desc`` field size is a multiple of 4 bytes. The ``sh_addralign``
758 field of the ``.note`` section must be at least 4 to indicate at least 8 byte
759 alignment.
760
761 .. _amdgpu-note-records-v2:
762
763 Code Object V2 Note Records (-mattr=-code-object-v3)
764 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
765
766 .. warning:: Code Object V2 is not the default code object version emitted by
767   this version of LLVM. For a description of the notes generated with the
768   default configuration (Code Object V3) see :ref:`amdgpu-note-records-v3`.
769
770 The AMDGPU backend code object uses the following ELF note record in the
771 ``.note`` section when compiling for Code Object V2 (-mattr=-code-object-v3).
772
773 Additional note records may be present, but any which are not documented here
774 are deprecated and should not be used.
775
776   .. table:: AMDGPU Code Object V2 ELF Note Records
777      :name: amdgpu-elf-note-records-table-v2
778
779      ===== ============================== ======================================
780      Name  Type                           Description
781      ===== ============================== ======================================
782      "AMD" ``NT_AMD_AMDGPU_HSA_METADATA`` <metadata null terminated string>
783      ===== ============================== ======================================
784
785 ..
786
787   .. table:: AMDGPU Code Object V2 ELF Note Record Enumeration Values
788      :name: amdgpu-elf-note-record-enumeration-values-table-v2
789
790      ============================== =====
791      Name                           Value
792      ============================== =====
793      *reserved*                       0-9
794      ``NT_AMD_AMDGPU_HSA_METADATA``    10
795      *reserved*                        11
796      ============================== =====
797
798 ``NT_AMD_AMDGPU_HSA_METADATA``
799   Specifies extensible metadata associated with the code objects executed on HSA
800   [HSA]_ compatible runtimes such as AMD's ROCm [AMD-ROCm]_. It is required when
801   the target triple OS is ``amdhsa`` (see :ref:`amdgpu-target-triples`). See
802   :ref:`amdgpu-amdhsa-code-object-metadata-v2` for the syntax of the code
803   object metadata string.
804
805 .. _amdgpu-note-records-v3:
806
807 Code Object V3 Note Records (-mattr=+code-object-v3)
808 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
809
810 The AMDGPU backend code object uses the following ELF note record in the
811 ``.note`` section when compiling for Code Object V3 (-mattr=+code-object-v3).
812
813 Additional note records may be present, but any which are not documented here
814 are deprecated and should not be used.
815
816   .. table:: AMDGPU Code Object V3 ELF Note Records
817      :name: amdgpu-elf-note-records-table-v3
818
819      ======== ============================== ======================================
820      Name     Type                           Description
821      ======== ============================== ======================================
822      "AMDGPU" ``NT_AMDGPU_METADATA``         Metadata in Message Pack [MsgPack]_
823                                              binary format.
824      ======== ============================== ======================================
825
826 ..
827
828   .. table:: AMDGPU Code Object V3 ELF Note Record Enumeration Values
829      :name: amdgpu-elf-note-record-enumeration-values-table-v3
830
831      ============================== =====
832      Name                           Value
833      ============================== =====
834      *reserved*                     0-31
835      ``NT_AMDGPU_METADATA``         32
836      ============================== =====
837
838 ``NT_AMDGPU_METADATA``
839   Specifies extensible metadata associated with an AMDGPU code
840   object. It is encoded as a map in the Message Pack [MsgPack]_ binary
841   data format. See :ref:`amdgpu-amdhsa-code-object-metadata-v3` for the
842   map keys defined for the ``amdhsa`` OS.
843
844 .. _amdgpu-symbols:
845
846 Symbols
847 -------
848
849 Symbols include the following:
850
851   .. table:: AMDGPU ELF Symbols
852      :name: amdgpu-elf-symbols-table
853
854      ===================== ================== ================ ==================
855      Name                  Type               Section          Description
856      ===================== ================== ================ ==================
857      *link-name*           ``STT_OBJECT``     - ``.data``      Global variable
858                                               - ``.rodata``
859                                               - ``.bss``
860      *link-name*\ ``.kd``  ``STT_OBJECT``     - ``.rodata``    Kernel descriptor
861      *link-name*           ``STT_FUNC``       - ``.text``      Kernel entry point
862      *link-name*           ``STT_OBJECT``     - SHN_AMDGPU_LDS Global variable in LDS
863      ===================== ================== ================ ==================
864
865 Global variable
866   Global variables both used and defined by the compilation unit.
867
868   If the symbol is defined in the compilation unit then it is allocated in the
869   appropriate section according to if it has initialized data or is readonly.
870
871   If the symbol is external then its section is ``STN_UNDEF`` and the loader
872   will resolve relocations using the definition provided by another code object
873   or explicitly defined by the runtime.
874
875   If the symbol resides in local/group memory (LDS) then its section is the
876   special processor-specific section name ``SHN_AMDGPU_LDS``, and the
877   ``st_value`` field describes alignment requirements as it does for common
878   symbols.
879
880   .. TODO
881      Add description of linked shared object symbols. Seems undefined symbols
882      are marked as STT_NOTYPE.
883
884 Kernel descriptor
885   Every HSA kernel has an associated kernel descriptor. It is the address of the
886   kernel descriptor that is used in the AQL dispatch packet used to invoke the
887   kernel, not the kernel entry point. The layout of the HSA kernel descriptor is
888   defined in :ref:`amdgpu-amdhsa-kernel-descriptor`.
889
890 Kernel entry point
891   Every HSA kernel also has a symbol for its machine code entry point.
892
893 .. _amdgpu-relocation-records:
894
895 Relocation Records
896 ------------------
897
898 AMDGPU backend generates ``Elf64_Rela`` relocation records. Supported
899 relocatable fields are:
900
901 ``word32``
902   This specifies a 32-bit field occupying 4 bytes with arbitrary byte
903   alignment. These values use the same byte order as other word values in the
904   AMD GPU architecture.
905
906 ``word64``
907   This specifies a 64-bit field occupying 8 bytes with arbitrary byte
908   alignment. These values use the same byte order as other word values in the
909   AMD GPU architecture.
910
911 Following notations are used for specifying relocation calculations:
912
913 **A**
914   Represents the addend used to compute the value of the relocatable field.
915
916 **G**
917   Represents the offset into the global offset table at which the relocation
918   entry's symbol will reside during execution.
919
920 **GOT**
921   Represents the address of the global offset table.
922
923 **P**
924   Represents the place (section offset for ``et_rel`` or address for ``et_dyn``)
925   of the storage unit being relocated (computed using ``r_offset``).
926
927 **S**
928   Represents the value of the symbol whose index resides in the relocation
929   entry. Relocations not using this must specify a symbol index of ``STN_UNDEF``.
930
931 **B**
932   Represents the base address of a loaded executable or shared object which is
933   the difference between the ELF address and the actual load address. Relocations
934   using this are only valid in executable or shared objects.
935
936 The following relocation types are supported:
937
938   .. table:: AMDGPU ELF Relocation Records
939      :name: amdgpu-elf-relocation-records-table
940
941      ========================== ======= =====  ==========  ==============================
942      Relocation Type            Kind    Value  Field       Calculation
943      ========================== ======= =====  ==========  ==============================
944      ``R_AMDGPU_NONE``                  0      *none*      *none*
945      ``R_AMDGPU_ABS32_LO``      Static, 1      ``word32``  (S + A) & 0xFFFFFFFF
946                                 Dynamic
947      ``R_AMDGPU_ABS32_HI``      Static, 2      ``word32``  (S + A) >> 32
948                                 Dynamic
949      ``R_AMDGPU_ABS64``         Static, 3      ``word64``  S + A
950                                 Dynamic
951      ``R_AMDGPU_REL32``         Static  4      ``word32``  S + A - P
952      ``R_AMDGPU_REL64``         Static  5      ``word64``  S + A - P
953      ``R_AMDGPU_ABS32``         Static, 6      ``word32``  S + A
954                                 Dynamic
955      ``R_AMDGPU_GOTPCREL``      Static  7      ``word32``  G + GOT + A - P
956      ``R_AMDGPU_GOTPCREL32_LO`` Static  8      ``word32``  (G + GOT + A - P) & 0xFFFFFFFF
957      ``R_AMDGPU_GOTPCREL32_HI`` Static  9      ``word32``  (G + GOT + A - P) >> 32
958      ``R_AMDGPU_REL32_LO``      Static  10     ``word32``  (S + A - P) & 0xFFFFFFFF
959      ``R_AMDGPU_REL32_HI``      Static  11     ``word32``  (S + A - P) >> 32
960      *reserved*                         12
961      ``R_AMDGPU_RELATIVE64``    Dynamic 13     ``word64``  B + A
962      ========================== ======= =====  ==========  ==============================
963
964 ``R_AMDGPU_ABS32_LO`` and ``R_AMDGPU_ABS32_HI`` are only supported by
965 the ``mesa3d`` OS, which does not support ``R_AMDGPU_ABS64``.
966
967 There is no current OS loader support for 32 bit programs and so
968 ``R_AMDGPU_ABS32`` is not used.
969
970 .. _amdgpu-dwarf:
971
972 DWARF
973 -----
974
975 Standard DWARF [DWARF]_ Version 5 sections can be generated. These contain
976 information that maps the code object executable code and data to the source
977 language constructs. It can be used by tools such as debuggers and profilers.
978
979 Address Space Mapping
980 ~~~~~~~~~~~~~~~~~~~~~
981
982 The following address space mapping is used:
983
984   .. table:: AMDGPU DWARF Address Space Mapping
985      :name: amdgpu-dwarf-address-space-mapping-table
986
987      =================== =================
988      DWARF Address Space Memory Space
989      =================== =================
990      1                   Private (Scratch)
991      2                   Local (group/LDS)
992      *omitted*           Global
993      *omitted*           Constant
994      *omitted*           Generic (Flat)
995      *not supported*     Region (GDS)
996      =================== =================
997
998 See :ref:`amdgpu-address-spaces` for information on the memory space terminology
999 used in the table.
1000
1001 An ``address_class`` attribute is generated on pointer type DIEs to specify the
1002 DWARF address space of the value of the pointer when it is in the *private* or
1003 *local* address space. Otherwise the attribute is omitted.
1004
1005 An ``XDEREF`` operation is generated in location list expressions for variables
1006 that are allocated in the *private* and *local* address space. Otherwise no
1007 ``XDREF`` is omitted.
1008
1009 Register Mapping
1010 ~~~~~~~~~~~~~~~~
1011
1012 *This section is WIP.*
1013
1014 .. TODO
1015    Define DWARF register enumeration.
1016
1017    If want to present a wavefront state then should expose vector registers as
1018    64 wide (rather than per work-item view that LLVM uses). Either as separate
1019    registers, or a 64x4 byte single register. In either case use a new LANE op
1020    (akin to XDREF) to select the current lane usage in a location
1021    expression. This would also allow scalar register spilling to vector register
1022    lanes to be expressed (currently no debug information is being generated for
1023    spilling). If choose a wide single register approach then use LANE in
1024    conjunction with PIECE operation to select the dword part of the register for
1025    the current lane. If the separate register approach then use LANE to select
1026    the register.
1027
1028 Source Text
1029 ~~~~~~~~~~~
1030
1031 Source text for online-compiled programs (e.g. those compiled by the OpenCL
1032 runtime) may be embedded into the DWARF v5 line table using the ``clang
1033 -gembed-source`` option, described in table :ref:`amdgpu-debug-options`.
1034
1035 For example:
1036
1037 ``-gembed-source``
1038   Enable the embedded source DWARF v5 extension.
1039 ``-gno-embed-source``
1040   Disable the embedded source DWARF v5 extension.
1041
1042   .. table:: AMDGPU Debug Options
1043      :name: amdgpu-debug-options
1044
1045      ==================== ==================================================
1046      Debug Flag           Description
1047      ==================== ==================================================
1048      -g[no-]embed-source  Enable/disable embedding source text in DWARF
1049                           debug sections. Useful for environments where
1050                           source cannot be written to disk, such as
1051                           when performing online compilation.
1052      ==================== ==================================================
1053
1054 This option enables one extended content types in the DWARF v5 Line Number
1055 Program Header, which is used to encode embedded source.
1056
1057   .. table:: AMDGPU DWARF Line Number Program Header Extended Content Types
1058      :name: amdgpu-dwarf-extended-content-types
1059
1060      ============================  ======================
1061      Content Type                  Form
1062      ============================  ======================
1063      ``DW_LNCT_LLVM_source``       ``DW_FORM_line_strp``
1064      ============================  ======================
1065
1066 The source field will contain the UTF-8 encoded, null-terminated source text
1067 with ``'\n'`` line endings. When the source field is present, consumers can use
1068 the embedded source instead of attempting to discover the source on disk. When
1069 the source field is absent, consumers can access the file to get the source
1070 text.
1071
1072 The above content type appears in the ``file_name_entry_format`` field of the
1073 line table prologue, and its corresponding value appear in the ``file_names``
1074 field. The current encoding of the content type is documented in table
1075 :ref:`amdgpu-dwarf-extended-content-types-encoding`
1076
1077   .. table:: AMDGPU DWARF Line Number Program Header Extended Content Types Encoding
1078      :name: amdgpu-dwarf-extended-content-types-encoding
1079
1080      ============================  ====================
1081      Content Type                  Value
1082      ============================  ====================
1083      ``DW_LNCT_LLVM_source``       0x2001
1084      ============================  ====================
1085
1086 .. _amdgpu-code-conventions:
1087
1088 Code Conventions
1089 ================
1090
1091 This section provides code conventions used for each supported target triple OS
1092 (see :ref:`amdgpu-target-triples`).
1093
1094 AMDHSA
1095 ------
1096
1097 This section provides code conventions used when the target triple OS is
1098 ``amdhsa`` (see :ref:`amdgpu-target-triples`).
1099
1100 .. _amdgpu-amdhsa-code-object-target-identification:
1101
1102 Code Object Target Identification
1103 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1104
1105 The AMDHSA OS uses the following syntax to specify the code object
1106 target as a single string:
1107
1108   ``<Architecture>-<Vendor>-<OS>-<Environment>-<Processor><Target Features>``
1109
1110 Where:
1111
1112   - ``<Architecture>``, ``<Vendor>``, ``<OS>`` and ``<Environment>``
1113     are the same as the *Target Triple* (see
1114     :ref:`amdgpu-target-triples`).
1115
1116   - ``<Processor>`` is the same as the *Processor* (see
1117     :ref:`amdgpu-processors`).
1118
1119   - ``<Target Features>`` is a list of the enabled *Target Features*
1120     (see :ref:`amdgpu-target-features`), each prefixed by a plus, that
1121     apply to *Processor*. The list must be in the same order as listed
1122     in the table :ref:`amdgpu-target-feature-table`. Note that *Target
1123     Features* must be included in the list if they are enabled even if
1124     that is the default for *Processor*.
1125
1126 For example:
1127
1128   ``"amdgcn-amd-amdhsa--gfx902+xnack"``
1129
1130 .. _amdgpu-amdhsa-code-object-metadata:
1131
1132 Code Object Metadata
1133 ~~~~~~~~~~~~~~~~~~~~
1134
1135 The code object metadata specifies extensible metadata associated with the code
1136 objects executed on HSA [HSA]_ compatible runtimes such as AMD's ROCm
1137 [AMD-ROCm]_. The encoding and semantics of this metadata depends on the code
1138 object version; see :ref:`amdgpu-amdhsa-code-object-metadata-v2` and
1139 :ref:`amdgpu-amdhsa-code-object-metadata-v3`.
1140
1141 Code object metadata is specified in a note record (see
1142 :ref:`amdgpu-note-records`) and is required when the target triple OS is
1143 ``amdhsa`` (see :ref:`amdgpu-target-triples`). It must contain the minimum
1144 information necessary to support the ROCM kernel queries. For example, the
1145 segment sizes needed in a dispatch packet. In addition, a high level language
1146 runtime may require other information to be included. For example, the AMD
1147 OpenCL runtime records kernel argument information.
1148
1149 .. _amdgpu-amdhsa-code-object-metadata-v2:
1150
1151 Code Object V2 Metadata (-mattr=-code-object-v3)
1152 ++++++++++++++++++++++++++++++++++++++++++++++++
1153
1154 .. warning:: Code Object V2 is not the default code object version emitted by
1155   this version of LLVM. For a description of the metadata generated with the
1156   default configuration (Code Object V3) see
1157   :ref:`amdgpu-amdhsa-code-object-metadata-v3`.
1158
1159 Code object V2 metadata is specified by the ``NT_AMD_AMDGPU_METADATA`` note
1160 record (see :ref:`amdgpu-note-records-v2`).
1161
1162 The metadata is specified as a YAML formatted string (see [YAML]_ and
1163 :doc:`YamlIO`).
1164
1165 .. TODO
1166    Is the string null terminated? It probably should not if YAML allows it to
1167    contain null characters, otherwise it should be.
1168
1169 The metadata is represented as a single YAML document comprised of the mapping
1170 defined in table :ref:`amdgpu-amdhsa-code-object-metadata-map-table-v2` and
1171 referenced tables.
1172
1173 For boolean values, the string values of ``false`` and ``true`` are used for
1174 false and true respectively.
1175
1176 Additional information can be added to the mappings. To avoid conflicts, any
1177 non-AMD key names should be prefixed by "*vendor-name*.".
1178
1179   .. table:: AMDHSA Code Object V2 Metadata Map
1180      :name: amdgpu-amdhsa-code-object-metadata-map-table-v2
1181
1182      ========== ============== ========= =======================================
1183      String Key Value Type     Required? Description
1184      ========== ============== ========= =======================================
1185      "Version"  sequence of    Required  - The first integer is the major
1186                 2 integers                 version. Currently 1.
1187                                          - The second integer is the minor
1188                                            version. Currently 0.
1189      "Printf"   sequence of              Each string is encoded information
1190                 strings                  about a printf function call. The
1191                                          encoded information is organized as
1192                                          fields separated by colon (':'):
1193
1194                                          ``ID:N:S[0]:S[1]:...:S[N-1]:FormatString``
1195
1196                                          where:
1197
1198                                          ``ID``
1199                                            A 32 bit integer as a unique id for
1200                                            each printf function call
1201
1202                                          ``N``
1203                                            A 32 bit integer equal to the number
1204                                            of arguments of printf function call
1205                                            minus 1
1206
1207                                          ``S[i]`` (where i = 0, 1, ... , N-1)
1208                                            32 bit integers for the size in bytes
1209                                            of the i-th FormatString argument of
1210                                            the printf function call
1211
1212                                          FormatString
1213                                            The format string passed to the
1214                                            printf function call.
1215      "Kernels"  sequence of    Required  Sequence of the mappings for each
1216                 mapping                  kernel in the code object. See
1217                                          :ref:`amdgpu-amdhsa-code-object-kernel-metadata-map-table-v2`
1218                                          for the definition of the mapping.
1219      ========== ============== ========= =======================================
1220
1221 ..
1222
1223   .. table:: AMDHSA Code Object V2 Kernel Metadata Map
1224      :name: amdgpu-amdhsa-code-object-kernel-metadata-map-table-v2
1225
1226      ================= ============== ========= ================================
1227      String Key        Value Type     Required? Description
1228      ================= ============== ========= ================================
1229      "Name"            string         Required  Source name of the kernel.
1230      "SymbolName"      string         Required  Name of the kernel
1231                                                 descriptor ELF symbol.
1232      "Language"        string                   Source language of the kernel.
1233                                                 Values include:
1234
1235                                                 - "OpenCL C"
1236                                                 - "OpenCL C++"
1237                                                 - "HCC"
1238                                                 - "OpenMP"
1239
1240      "LanguageVersion" sequence of              - The first integer is the major
1241                        2 integers                 version.
1242                                                 - The second integer is the
1243                                                   minor version.
1244      "Attrs"           mapping                  Mapping of kernel attributes.
1245                                                 See
1246                                                 :ref:`amdgpu-amdhsa-code-object-kernel-attribute-metadata-map-table-v2`
1247                                                 for the mapping definition.
1248      "Args"            sequence of              Sequence of mappings of the
1249                        mapping                  kernel arguments. See
1250                                                 :ref:`amdgpu-amdhsa-code-object-kernel-argument-metadata-map-table-v2`
1251                                                 for the definition of the mapping.
1252      "CodeProps"       mapping                  Mapping of properties related to
1253                                                 the kernel code. See
1254                                                 :ref:`amdgpu-amdhsa-code-object-kernel-code-properties-metadata-map-table-v2`
1255                                                 for the mapping definition.
1256      ================= ============== ========= ================================
1257
1258 ..
1259
1260   .. table:: AMDHSA Code Object V2 Kernel Attribute Metadata Map
1261      :name: amdgpu-amdhsa-code-object-kernel-attribute-metadata-map-table-v2
1262
1263      =================== ============== ========= ==============================
1264      String Key          Value Type     Required? Description
1265      =================== ============== ========= ==============================
1266      "ReqdWorkGroupSize" sequence of              If not 0, 0, 0 then all values
1267                          3 integers               must be >=1 and the dispatch
1268                                                   work-group size X, Y, Z must
1269                                                   correspond to the specified
1270                                                   values. Defaults to 0, 0, 0.
1271
1272                                                   Corresponds to the OpenCL
1273                                                   ``reqd_work_group_size``
1274                                                   attribute.
1275      "WorkGroupSizeHint" sequence of              The dispatch work-group size
1276                          3 integers               X, Y, Z is likely to be the
1277                                                   specified values.
1278
1279                                                   Corresponds to the OpenCL
1280                                                   ``work_group_size_hint``
1281                                                   attribute.
1282      "VecTypeHint"       string                   The name of a scalar or vector
1283                                                   type.
1284
1285                                                   Corresponds to the OpenCL
1286                                                   ``vec_type_hint`` attribute.
1287
1288      "RuntimeHandle"     string                   The external symbol name
1289                                                   associated with a kernel.
1290                                                   OpenCL runtime allocates a
1291                                                   global buffer for the symbol
1292                                                   and saves the kernel's address
1293                                                   to it, which is used for
1294                                                   device side enqueueing. Only
1295                                                   available for device side
1296                                                   enqueued kernels.
1297      =================== ============== ========= ==============================
1298
1299 ..
1300
1301   .. table:: AMDHSA Code Object V2 Kernel Argument Metadata Map
1302      :name: amdgpu-amdhsa-code-object-kernel-argument-metadata-map-table-v2
1303
1304      ================= ============== ========= ================================
1305      String Key        Value Type     Required? Description
1306      ================= ============== ========= ================================
1307      "Name"            string                   Kernel argument name.
1308      "TypeName"        string                   Kernel argument type name.
1309      "Size"            integer        Required  Kernel argument size in bytes.
1310      "Align"           integer        Required  Kernel argument alignment in
1311                                                 bytes. Must be a power of two.
1312      "ValueKind"       string         Required  Kernel argument kind that
1313                                                 specifies how to set up the
1314                                                 corresponding argument.
1315                                                 Values include:
1316
1317                                                 "ByValue"
1318                                                   The argument is copied
1319                                                   directly into the kernarg.
1320
1321                                                 "GlobalBuffer"
1322                                                   A global address space pointer
1323                                                   to the buffer data is passed
1324                                                   in the kernarg.
1325
1326                                                 "DynamicSharedPointer"
1327                                                   A group address space pointer
1328                                                   to dynamically allocated LDS
1329                                                   is passed in the kernarg.
1330
1331                                                 "Sampler"
1332                                                   A global address space
1333                                                   pointer to a S# is passed in
1334                                                   the kernarg.
1335
1336                                                 "Image"
1337                                                   A global address space
1338                                                   pointer to a T# is passed in
1339                                                   the kernarg.
1340
1341                                                 "Pipe"
1342                                                   A global address space pointer
1343                                                   to an OpenCL pipe is passed in
1344                                                   the kernarg.
1345
1346                                                 "Queue"
1347                                                   A global address space pointer
1348                                                   to an OpenCL device enqueue
1349                                                   queue is passed in the
1350                                                   kernarg.
1351
1352                                                 "HiddenGlobalOffsetX"
1353                                                   The OpenCL grid dispatch
1354                                                   global offset for the X
1355                                                   dimension is passed in the
1356                                                   kernarg.
1357
1358                                                 "HiddenGlobalOffsetY"
1359                                                   The OpenCL grid dispatch
1360                                                   global offset for the Y
1361                                                   dimension is passed in the
1362                                                   kernarg.
1363
1364                                                 "HiddenGlobalOffsetZ"
1365                                                   The OpenCL grid dispatch
1366                                                   global offset for the Z
1367                                                   dimension is passed in the
1368                                                   kernarg.
1369
1370                                                 "HiddenNone"
1371                                                   An argument that is not used
1372                                                   by the kernel. Space needs to
1373                                                   be left for it, but it does
1374                                                   not need to be set up.
1375
1376                                                 "HiddenPrintfBuffer"
1377                                                   A global address space pointer
1378                                                   to the runtime printf buffer
1379                                                   is passed in kernarg.
1380
1381                                                 "HiddenDefaultQueue"
1382                                                   A global address space pointer
1383                                                   to the OpenCL device enqueue
1384                                                   queue that should be used by
1385                                                   the kernel by default is
1386                                                   passed in the kernarg.
1387
1388                                                 "HiddenCompletionAction"
1389                                                   A global address space pointer
1390                                                   to help link enqueued kernels into
1391                                                   the ancestor tree for determining
1392                                                   when the parent kernel has finished.
1393
1394                                                 "HiddenMultiGridSyncArg"
1395                                                   A global address space pointer for
1396                                                   multi-grid synchronization is
1397                                                   passed in the kernarg.
1398
1399      "ValueType"       string         Required  Kernel argument value type. Only
1400                                                 present if "ValueKind" is
1401                                                 "ByValue". For vector data
1402                                                 types, the value is for the
1403                                                 element type. Values include:
1404
1405                                                 - "Struct"
1406                                                 - "I8"
1407                                                 - "U8"
1408                                                 - "I16"
1409                                                 - "U16"
1410                                                 - "F16"
1411                                                 - "I32"
1412                                                 - "U32"
1413                                                 - "F32"
1414                                                 - "I64"
1415                                                 - "U64"
1416                                                 - "F64"
1417
1418                                                 .. TODO
1419                                                    How can it be determined if a
1420                                                    vector type, and what size
1421                                                    vector?
1422      "PointeeAlign"    integer                  Alignment in bytes of pointee
1423                                                 type for pointer type kernel
1424                                                 argument. Must be a power
1425                                                 of 2. Only present if
1426                                                 "ValueKind" is
1427                                                 "DynamicSharedPointer".
1428      "AddrSpaceQual"   string                   Kernel argument address space
1429                                                 qualifier. Only present if
1430                                                 "ValueKind" is "GlobalBuffer" or
1431                                                 "DynamicSharedPointer". Values
1432                                                 are:
1433
1434                                                 - "Private"
1435                                                 - "Global"
1436                                                 - "Constant"
1437                                                 - "Local"
1438                                                 - "Generic"
1439                                                 - "Region"
1440
1441                                                 .. TODO
1442                                                    Is GlobalBuffer only Global
1443                                                    or Constant? Is
1444                                                    DynamicSharedPointer always
1445                                                    Local? Can HCC allow Generic?
1446                                                    How can Private or Region
1447                                                    ever happen?
1448      "AccQual"         string                   Kernel argument access
1449                                                 qualifier. Only present if
1450                                                 "ValueKind" is "Image" or
1451                                                 "Pipe". Values
1452                                                 are:
1453
1454                                                 - "ReadOnly"
1455                                                 - "WriteOnly"
1456                                                 - "ReadWrite"
1457
1458                                                 .. TODO
1459                                                    Does this apply to
1460                                                    GlobalBuffer?
1461      "ActualAccQual"   string                   The actual memory accesses
1462                                                 performed by the kernel on the
1463                                                 kernel argument. Only present if
1464                                                 "ValueKind" is "GlobalBuffer",
1465                                                 "Image", or "Pipe". This may be
1466                                                 more restrictive than indicated
1467                                                 by "AccQual" to reflect what the
1468                                                 kernel actual does. If not
1469                                                 present then the runtime must
1470                                                 assume what is implied by
1471                                                 "AccQual" and "IsConst". Values
1472                                                 are:
1473
1474                                                 - "ReadOnly"
1475                                                 - "WriteOnly"
1476                                                 - "ReadWrite"
1477
1478      "IsConst"         boolean                  Indicates if the kernel argument
1479                                                 is const qualified. Only present
1480                                                 if "ValueKind" is
1481                                                 "GlobalBuffer".
1482
1483      "IsRestrict"      boolean                  Indicates if the kernel argument
1484                                                 is restrict qualified. Only
1485                                                 present if "ValueKind" is
1486                                                 "GlobalBuffer".
1487
1488      "IsVolatile"      boolean                  Indicates if the kernel argument
1489                                                 is volatile qualified. Only
1490                                                 present if "ValueKind" is
1491                                                 "GlobalBuffer".
1492
1493      "IsPipe"          boolean                  Indicates if the kernel argument
1494                                                 is pipe qualified. Only present
1495                                                 if "ValueKind" is "Pipe".
1496
1497                                                 .. TODO
1498                                                    Can GlobalBuffer be pipe
1499                                                    qualified?
1500      ================= ============== ========= ================================
1501
1502 ..
1503
1504   .. table:: AMDHSA Code Object V2 Kernel Code Properties Metadata Map
1505      :name: amdgpu-amdhsa-code-object-kernel-code-properties-metadata-map-table-v2
1506
1507      ============================ ============== ========= =====================
1508      String Key                   Value Type     Required? Description
1509      ============================ ============== ========= =====================
1510      "KernargSegmentSize"         integer        Required  The size in bytes of
1511                                                            the kernarg segment
1512                                                            that holds the values
1513                                                            of the arguments to
1514                                                            the kernel.
1515      "GroupSegmentFixedSize"      integer        Required  The amount of group
1516                                                            segment memory
1517                                                            required by a
1518                                                            work-group in
1519                                                            bytes. This does not
1520                                                            include any
1521                                                            dynamically allocated
1522                                                            group segment memory
1523                                                            that may be added
1524                                                            when the kernel is
1525                                                            dispatched.
1526      "PrivateSegmentFixedSize"    integer        Required  The amount of fixed
1527                                                            private address space
1528                                                            memory required for a
1529                                                            work-item in
1530                                                            bytes. If the kernel
1531                                                            uses a dynamic call
1532                                                            stack then additional
1533                                                            space must be added
1534                                                            to this value for the
1535                                                            call stack.
1536      "KernargSegmentAlign"        integer        Required  The maximum byte
1537                                                            alignment of
1538                                                            arguments in the
1539                                                            kernarg segment. Must
1540                                                            be a power of 2.
1541      "WavefrontSize"              integer        Required  Wavefront size. Must
1542                                                            be a power of 2.
1543      "NumSGPRs"                   integer        Required  Number of scalar
1544                                                            registers used by a
1545                                                            wavefront for
1546                                                            GFX6-GFX10. This
1547                                                            includes the special
1548                                                            SGPRs for VCC, Flat
1549                                                            Scratch (GFX7-GFX10)
1550                                                            and XNACK (for
1551                                                            GFX8-GFX10). It does
1552                                                            not include the 16
1553                                                            SGPR added if a trap
1554                                                            handler is
1555                                                            enabled. It is not
1556                                                            rounded up to the
1557                                                            allocation
1558                                                            granularity.
1559      "NumVGPRs"                   integer        Required  Number of vector
1560                                                            registers used by
1561                                                            each work-item for
1562                                                            GFX6-GFX10
1563      "MaxFlatWorkGroupSize"       integer        Required  Maximum flat
1564                                                            work-group size
1565                                                            supported by the
1566                                                            kernel in work-items.
1567                                                            Must be >=1 and
1568                                                            consistent with
1569                                                            ReqdWorkGroupSize if
1570                                                            not 0, 0, 0.
1571      "NumSpilledSGPRs"            integer                  Number of stores from
1572                                                            a scalar register to
1573                                                            a register allocator
1574                                                            created spill
1575                                                            location.
1576      "NumSpilledVGPRs"            integer                  Number of stores from
1577                                                            a vector register to
1578                                                            a register allocator
1579                                                            created spill
1580                                                            location.
1581      ============================ ============== ========= =====================
1582
1583 .. _amdgpu-amdhsa-code-object-metadata-v3:
1584
1585 Code Object V3 Metadata (-mattr=+code-object-v3)
1586 ++++++++++++++++++++++++++++++++++++++++++++++++
1587
1588 Code object V3 metadata is specified by the ``NT_AMDGPU_METADATA`` note record
1589 (see :ref:`amdgpu-note-records-v3`).
1590
1591 The metadata is represented as Message Pack formatted binary data (see
1592 [MsgPack]_). The top level is a Message Pack map that includes the
1593 keys defined in table
1594 :ref:`amdgpu-amdhsa-code-object-metadata-map-table-v3` and referenced
1595 tables.
1596
1597 Additional information can be added to the maps. To avoid conflicts,
1598 any key names should be prefixed by "*vendor-name*." where
1599 ``vendor-name`` can be the the name of the vendor and specific vendor
1600 tool that generates the information. The prefix is abbreviated to
1601 simply "." when it appears within a map that has been added by the
1602 same *vendor-name*.
1603
1604   .. table:: AMDHSA Code Object V3 Metadata Map
1605      :name: amdgpu-amdhsa-code-object-metadata-map-table-v3
1606
1607      ================= ============== ========= =======================================
1608      String Key        Value Type     Required? Description
1609      ================= ============== ========= =======================================
1610      "amdhsa.version"  sequence of    Required  - The first integer is the major
1611                        2 integers                 version. Currently 1.
1612                                                 - The second integer is the minor
1613                                                   version. Currently 0.
1614      "amdhsa.printf"   sequence of              Each string is encoded information
1615                        strings                  about a printf function call. The
1616                                                 encoded information is organized as
1617                                                 fields separated by colon (':'):
1618
1619                                                 ``ID:N:S[0]:S[1]:...:S[N-1]:FormatString``
1620
1621                                                 where:
1622
1623                                                 ``ID``
1624                                                   A 32 bit integer as a unique id for
1625                                                   each printf function call
1626
1627                                                 ``N``
1628                                                   A 32 bit integer equal to the number
1629                                                   of arguments of printf function call
1630                                                   minus 1
1631
1632                                                 ``S[i]`` (where i = 0, 1, ... , N-1)
1633                                                   32 bit integers for the size in bytes
1634                                                   of the i-th FormatString argument of
1635                                                   the printf function call
1636
1637                                                 FormatString
1638                                                   The format string passed to the
1639                                                   printf function call.
1640      "amdhsa.kernels"  sequence of    Required  Sequence of the maps for each
1641                        map                      kernel in the code object. See
1642                                                 :ref:`amdgpu-amdhsa-code-object-kernel-metadata-map-table-v3`
1643                                                 for the definition of the keys included
1644                                                 in that map.
1645      ================= ============== ========= =======================================
1646
1647 ..
1648
1649   .. table:: AMDHSA Code Object V3 Kernel Metadata Map
1650      :name: amdgpu-amdhsa-code-object-kernel-metadata-map-table-v3
1651
1652      =================================== ============== ========= ================================
1653      String Key                          Value Type     Required? Description
1654      =================================== ============== ========= ================================
1655      ".name"                             string         Required  Source name of the kernel.
1656      ".symbol"                           string         Required  Name of the kernel
1657                                                                   descriptor ELF symbol.
1658      ".language"                         string                   Source language of the kernel.
1659                                                                   Values include:
1660
1661                                                                   - "OpenCL C"
1662                                                                   - "OpenCL C++"
1663                                                                   - "HCC"
1664                                                                   - "HIP"
1665                                                                   - "OpenMP"
1666                                                                   - "Assembler"
1667
1668      ".language_version"                 sequence of              - The first integer is the major
1669                                          2 integers                 version.
1670                                                                   - The second integer is the
1671                                                                     minor version.
1672      ".args"                             sequence of              Sequence of maps of the
1673                                          map                      kernel arguments. See
1674                                                                   :ref:`amdgpu-amdhsa-code-object-kernel-argument-metadata-map-table-v3`
1675                                                                   for the definition of the keys
1676                                                                   included in that map.
1677      ".reqd_workgroup_size"              sequence of              If not 0, 0, 0 then all values
1678                                          3 integers               must be >=1 and the dispatch
1679                                                                   work-group size X, Y, Z must
1680                                                                   correspond to the specified
1681                                                                   values. Defaults to 0, 0, 0.
1682
1683                                                                   Corresponds to the OpenCL
1684                                                                   ``reqd_work_group_size``
1685                                                                   attribute.
1686      ".workgroup_size_hint"              sequence of              The dispatch work-group size
1687                                          3 integers               X, Y, Z is likely to be the
1688                                                                   specified values.
1689
1690                                                                   Corresponds to the OpenCL
1691                                                                   ``work_group_size_hint``
1692                                                                   attribute.
1693      ".vec_type_hint"                    string                   The name of a scalar or vector
1694                                                                   type.
1695
1696                                                                   Corresponds to the OpenCL
1697                                                                   ``vec_type_hint`` attribute.
1698
1699      ".device_enqueue_symbol"            string                   The external symbol name
1700                                                                   associated with a kernel.
1701                                                                   OpenCL runtime allocates a
1702                                                                   global buffer for the symbol
1703                                                                   and saves the kernel's address
1704                                                                   to it, which is used for
1705                                                                   device side enqueueing. Only
1706                                                                   available for device side
1707                                                                   enqueued kernels.
1708      ".kernarg_segment_size"             integer        Required  The size in bytes of
1709                                                                   the kernarg segment
1710                                                                   that holds the values
1711                                                                   of the arguments to
1712                                                                   the kernel.
1713      ".group_segment_fixed_size"         integer        Required  The amount of group
1714                                                                   segment memory
1715                                                                   required by a
1716                                                                   work-group in
1717                                                                   bytes. This does not
1718                                                                   include any
1719                                                                   dynamically allocated
1720                                                                   group segment memory
1721                                                                   that may be added
1722                                                                   when the kernel is
1723                                                                   dispatched.
1724      ".private_segment_fixed_size"       integer        Required  The amount of fixed
1725                                                                   private address space
1726                                                                   memory required for a
1727                                                                   work-item in
1728                                                                   bytes. If the kernel
1729                                                                   uses a dynamic call
1730                                                                   stack then additional
1731                                                                   space must be added
1732                                                                   to this value for the
1733                                                                   call stack.
1734      ".kernarg_segment_align"            integer        Required  The maximum byte
1735                                                                   alignment of
1736                                                                   arguments in the
1737                                                                   kernarg segment. Must
1738                                                                   be a power of 2.
1739      ".wavefront_size"                   integer        Required  Wavefront size. Must
1740                                                                   be a power of 2.
1741      ".sgpr_count"                       integer        Required  Number of scalar
1742                                                                   registers required by a
1743                                                                   wavefront for
1744                                                                   GFX6-GFX9. A register
1745                                                                   is required if it is
1746                                                                   used explicitly, or
1747                                                                   if a higher numbered
1748                                                                   register is used
1749                                                                   explicitly. This
1750                                                                   includes the special
1751                                                                   SGPRs for VCC, Flat
1752                                                                   Scratch (GFX7-GFX9)
1753                                                                   and XNACK (for
1754                                                                   GFX8-GFX9). It does
1755                                                                   not include the 16
1756                                                                   SGPR added if a trap
1757                                                                   handler is
1758                                                                   enabled. It is not
1759                                                                   rounded up to the
1760                                                                   allocation
1761                                                                   granularity.
1762      ".vgpr_count"                       integer        Required  Number of vector
1763                                                                   registers required by
1764                                                                   each work-item for
1765                                                                   GFX6-GFX9. A register
1766                                                                   is required if it is
1767                                                                   used explicitly, or
1768                                                                   if a higher numbered
1769                                                                   register is used
1770                                                                   explicitly.
1771      ".max_flat_workgroup_size"          integer        Required  Maximum flat
1772                                                                   work-group size
1773                                                                   supported by the
1774                                                                   kernel in work-items.
1775                                                                   Must be >=1 and
1776                                                                   consistent with
1777                                                                   ReqdWorkGroupSize if
1778                                                                   not 0, 0, 0.
1779      ".sgpr_spill_count"                 integer                  Number of stores from
1780                                                                   a scalar register to
1781                                                                   a register allocator
1782                                                                   created spill
1783                                                                   location.
1784      ".vgpr_spill_count"                 integer                  Number of stores from
1785                                                                   a vector register to
1786                                                                   a register allocator
1787                                                                   created spill
1788                                                                   location.
1789      =================================== ============== ========= ================================
1790
1791 ..
1792
1793   .. table:: AMDHSA Code Object V3 Kernel Argument Metadata Map
1794      :name: amdgpu-amdhsa-code-object-kernel-argument-metadata-map-table-v3
1795
1796      ====================== ============== ========= ================================
1797      String Key             Value Type     Required? Description
1798      ====================== ============== ========= ================================
1799      ".name"                string                   Kernel argument name.
1800      ".type_name"           string                   Kernel argument type name.
1801      ".size"                integer        Required  Kernel argument size in bytes.
1802      ".offset"              integer        Required  Kernel argument offset in
1803                                                      bytes. The offset must be a
1804                                                      multiple of the alignment
1805                                                      required by the argument.
1806      ".value_kind"          string         Required  Kernel argument kind that
1807                                                      specifies how to set up the
1808                                                      corresponding argument.
1809                                                      Values include:
1810
1811                                                      "by_value"
1812                                                        The argument is copied
1813                                                        directly into the kernarg.
1814
1815                                                      "global_buffer"
1816                                                        A global address space pointer
1817                                                        to the buffer data is passed
1818                                                        in the kernarg.
1819
1820                                                      "dynamic_shared_pointer"
1821                                                        A group address space pointer
1822                                                        to dynamically allocated LDS
1823                                                        is passed in the kernarg.
1824
1825                                                      "sampler"
1826                                                        A global address space
1827                                                        pointer to a S# is passed in
1828                                                        the kernarg.
1829
1830                                                      "image"
1831                                                        A global address space
1832                                                        pointer to a T# is passed in
1833                                                        the kernarg.
1834
1835                                                      "pipe"
1836                                                        A global address space pointer
1837                                                        to an OpenCL pipe is passed in
1838                                                        the kernarg.
1839
1840                                                      "queue"
1841                                                        A global address space pointer
1842                                                        to an OpenCL device enqueue
1843                                                        queue is passed in the
1844                                                        kernarg.
1845
1846                                                      "hidden_global_offset_x"
1847                                                        The OpenCL grid dispatch
1848                                                        global offset for the X
1849                                                        dimension is passed in the
1850                                                        kernarg.
1851
1852                                                      "hidden_global_offset_y"
1853                                                        The OpenCL grid dispatch
1854                                                        global offset for the Y
1855                                                        dimension is passed in the
1856                                                        kernarg.
1857
1858                                                      "hidden_global_offset_z"
1859                                                        The OpenCL grid dispatch
1860                                                        global offset for the Z
1861                                                        dimension is passed in the
1862                                                        kernarg.
1863
1864                                                      "hidden_none"
1865                                                        An argument that is not used
1866                                                        by the kernel. Space needs to
1867                                                        be left for it, but it does
1868                                                        not need to be set up.
1869
1870                                                      "hidden_printf_buffer"
1871                                                        A global address space pointer
1872                                                        to the runtime printf buffer
1873                                                        is passed in kernarg.
1874
1875                                                      "hidden_default_queue"
1876                                                        A global address space pointer
1877                                                        to the OpenCL device enqueue
1878                                                        queue that should be used by
1879                                                        the kernel by default is
1880                                                        passed in the kernarg.
1881
1882                                                      "hidden_completion_action"
1883                                                        A global address space pointer
1884                                                        to help link enqueued kernels into
1885                                                        the ancestor tree for determining
1886                                                        when the parent kernel has finished.
1887
1888                                                      "hidden_multigrid_sync_arg"
1889                                                        A global address space pointer for
1890                                                        multi-grid synchronization is
1891                                                        passed in the kernarg.
1892
1893      ".value_type"          string         Required  Kernel argument value type. Only
1894                                                      present if ".value_kind" is
1895                                                      "by_value". For vector data
1896                                                      types, the value is for the
1897                                                      element type. Values include:
1898
1899                                                      - "struct"
1900                                                      - "i8"
1901                                                      - "u8"
1902                                                      - "i16"
1903                                                      - "u16"
1904                                                      - "f16"
1905                                                      - "i32"
1906                                                      - "u32"
1907                                                      - "f32"
1908                                                      - "i64"
1909                                                      - "u64"
1910                                                      - "f64"
1911
1912                                                      .. TODO
1913                                                         How can it be determined if a
1914                                                         vector type, and what size
1915                                                         vector?
1916      ".pointee_align"       integer                  Alignment in bytes of pointee
1917                                                      type for pointer type kernel
1918                                                      argument. Must be a power
1919                                                      of 2. Only present if
1920                                                      ".value_kind" is
1921                                                      "dynamic_shared_pointer".
1922      ".address_space"       string                   Kernel argument address space
1923                                                      qualifier. Only present if
1924                                                      ".value_kind" is "global_buffer" or
1925                                                      "dynamic_shared_pointer". Values
1926                                                      are:
1927
1928                                                      - "private"
1929                                                      - "global"
1930                                                      - "constant"
1931                                                      - "local"
1932                                                      - "generic"
1933                                                      - "region"
1934
1935                                                      .. TODO
1936                                                         Is "global_buffer" only "global"
1937                                                         or "constant"? Is
1938                                                         "dynamic_shared_pointer" always
1939                                                         "local"? Can HCC allow "generic"?
1940                                                         How can "private" or "region"
1941                                                         ever happen?
1942      ".access"              string                   Kernel argument access
1943                                                      qualifier. Only present if
1944                                                      ".value_kind" is "image" or
1945                                                      "pipe". Values
1946                                                      are:
1947
1948                                                      - "read_only"
1949                                                      - "write_only"
1950                                                      - "read_write"
1951
1952                                                      .. TODO
1953                                                         Does this apply to
1954                                                         "global_buffer"?
1955      ".actual_access"       string                   The actual memory accesses
1956                                                      performed by the kernel on the
1957                                                      kernel argument. Only present if
1958                                                      ".value_kind" is "global_buffer",
1959                                                      "image", or "pipe". This may be
1960                                                      more restrictive than indicated
1961                                                      by ".access" to reflect what the
1962                                                      kernel actual does. If not
1963                                                      present then the runtime must
1964                                                      assume what is implied by
1965                                                      ".access" and ".is_const"      . Values
1966                                                      are:
1967
1968                                                      - "read_only"
1969                                                      - "write_only"
1970                                                      - "read_write"
1971
1972      ".is_const"            boolean                  Indicates if the kernel argument
1973                                                      is const qualified. Only present
1974                                                      if ".value_kind" is
1975                                                      "global_buffer".
1976
1977      ".is_restrict"         boolean                  Indicates if the kernel argument
1978                                                      is restrict qualified. Only
1979                                                      present if ".value_kind" is
1980                                                      "global_buffer".
1981
1982      ".is_volatile"         boolean                  Indicates if the kernel argument
1983                                                      is volatile qualified. Only
1984                                                      present if ".value_kind" is
1985                                                      "global_buffer".
1986
1987      ".is_pipe"             boolean                  Indicates if the kernel argument
1988                                                      is pipe qualified. Only present
1989                                                      if ".value_kind" is "pipe".
1990
1991                                                      .. TODO
1992                                                         Can "global_buffer" be pipe
1993                                                         qualified?
1994      ====================== ============== ========= ================================
1995
1996 ..
1997
1998 Kernel Dispatch
1999 ~~~~~~~~~~~~~~~
2000
2001 The HSA architected queuing language (AQL) defines a user space memory interface
2002 that can be used to control the dispatch of kernels, in an agent independent
2003 way. An agent can have zero or more AQL queues created for it using the ROCm
2004 runtime, in which AQL packets (all of which are 64 bytes) can be placed. See the
2005 *HSA Platform System Architecture Specification* [HSA]_ for the AQL queue
2006 mechanics and packet layouts.
2007
2008 The packet processor of a kernel agent is responsible for detecting and
2009 dispatching HSA kernels from the AQL queues associated with it. For AMD GPUs the
2010 packet processor is implemented by the hardware command processor (CP),
2011 asynchronous dispatch controller (ADC) and shader processor input controller
2012 (SPI).
2013
2014 The ROCm runtime can be used to allocate an AQL queue object. It uses the kernel
2015 mode driver to initialize and register the AQL queue with CP.
2016
2017 To dispatch a kernel the following actions are performed. This can occur in the
2018 CPU host program, or from an HSA kernel executing on a GPU.
2019
2020 1. A pointer to an AQL queue for the kernel agent on which the kernel is to be
2021    executed is obtained.
2022 2. A pointer to the kernel descriptor (see
2023    :ref:`amdgpu-amdhsa-kernel-descriptor`) of the kernel to execute is
2024    obtained. It must be for a kernel that is contained in a code object that that
2025    was loaded by the ROCm runtime on the kernel agent with which the AQL queue is
2026    associated.
2027 3. Space is allocated for the kernel arguments using the ROCm runtime allocator
2028    for a memory region with the kernarg property for the kernel agent that will
2029    execute the kernel. It must be at least 16 byte aligned.
2030 4. Kernel argument values are assigned to the kernel argument memory
2031    allocation. The layout is defined in the *HSA Programmer's Language Reference*
2032    [HSA]_. For AMDGPU the kernel execution directly accesses the kernel argument
2033    memory in the same way constant memory is accessed. (Note that the HSA
2034    specification allows an implementation to copy the kernel argument contents to
2035    another location that is accessed by the kernel.)
2036 5. An AQL kernel dispatch packet is created on the AQL queue. The ROCm runtime
2037    api uses 64 bit atomic operations to reserve space in the AQL queue for the
2038    packet. The packet must be set up, and the final write must use an atomic
2039    store release to set the packet kind to ensure the packet contents are
2040    visible to the kernel agent. AQL defines a doorbell signal mechanism to
2041    notify the kernel agent that the AQL queue has been updated. These rules, and
2042    the layout of the AQL queue and kernel dispatch packet is defined in the *HSA
2043    System Architecture Specification* [HSA]_.
2044 6. A kernel dispatch packet includes information about the actual dispatch,
2045    such as grid and work-group size, together with information from the code
2046    object about the kernel, such as segment sizes. The ROCm runtime queries on
2047    the kernel symbol can be used to obtain the code object values which are
2048    recorded in the :ref:`amdgpu-amdhsa-code-object-metadata`.
2049 7. CP executes micro-code and is responsible for detecting and setting up the
2050    GPU to execute the wavefronts of a kernel dispatch.
2051 8. CP ensures that when the a wavefront starts executing the kernel machine
2052    code, the scalar general purpose registers (SGPR) and vector general purpose
2053    registers (VGPR) are set up as required by the machine code. The required
2054    setup is defined in the :ref:`amdgpu-amdhsa-kernel-descriptor`. The initial
2055    register state is defined in
2056    :ref:`amdgpu-amdhsa-initial-kernel-execution-state`.
2057 9. The prolog of the kernel machine code (see
2058    :ref:`amdgpu-amdhsa-kernel-prolog`) sets up the machine state as necessary
2059    before continuing executing the machine code that corresponds to the kernel.
2060 10. When the kernel dispatch has completed execution, CP signals the completion
2061     signal specified in the kernel dispatch packet if not 0.
2062
2063 .. _amdgpu-amdhsa-memory-spaces:
2064
2065 Memory Spaces
2066 ~~~~~~~~~~~~~
2067
2068 The memory space properties are:
2069
2070   .. table:: AMDHSA Memory Spaces
2071      :name: amdgpu-amdhsa-memory-spaces-table
2072
2073      ================= =========== ======== ======= ==================
2074      Memory Space Name HSA Segment Hardware Address NULL Value
2075                        Name        Name     Size
2076      ================= =========== ======== ======= ==================
2077      Private           private     scratch  32      0x00000000
2078      Local             group       LDS      32      0xFFFFFFFF
2079      Global            global      global   64      0x0000000000000000
2080      Constant          constant    *same as 64      0x0000000000000000
2081                                    global*
2082      Generic           flat        flat     64      0x0000000000000000
2083      Region            N/A         GDS      32      *not implemented
2084                                                     for AMDHSA*
2085      ================= =========== ======== ======= ==================
2086
2087 The global and constant memory spaces both use global virtual addresses, which
2088 are the same virtual address space used by the CPU. However, some virtual
2089 addresses may only be accessible to the CPU, some only accessible by the GPU,
2090 and some by both.
2091
2092 Using the constant memory space indicates that the data will not change during
2093 the execution of the kernel. This allows scalar read instructions to be
2094 used. The vector and scalar L1 caches are invalidated of volatile data before
2095 each kernel dispatch execution to allow constant memory to change values between
2096 kernel dispatches.
2097
2098 The local memory space uses the hardware Local Data Store (LDS) which is
2099 automatically allocated when the hardware creates work-groups of wavefronts, and
2100 freed when all the wavefronts of a work-group have terminated. The data store
2101 (DS) instructions can be used to access it.
2102
2103 The private memory space uses the hardware scratch memory support. If the kernel
2104 uses scratch, then the hardware allocates memory that is accessed using
2105 wavefront lane dword (4 byte) interleaving. The mapping used from private
2106 address to physical address is:
2107
2108   ``wavefront-scratch-base +
2109   (private-address * wavefront-size * 4) +
2110   (wavefront-lane-id * 4)``
2111
2112 There are different ways that the wavefront scratch base address is determined
2113 by a wavefront (see :ref:`amdgpu-amdhsa-initial-kernel-execution-state`). This
2114 memory can be accessed in an interleaved manner using buffer instruction with
2115 the scratch buffer descriptor and per wavefront scratch offset, by the scratch
2116 instructions, or by flat instructions. If each lane of a wavefront accesses the
2117 same private address, the interleaving results in adjacent dwords being accessed
2118 and hence requires fewer cache lines to be fetched. Multi-dword access is not
2119 supported except by flat and scratch instructions in GFX9-GFX10.
2120
2121 The generic address space uses the hardware flat address support available in
2122 GFX7-GFX10. This uses two fixed ranges of virtual addresses (the private and
2123 local appertures), that are outside the range of addressible global memory, to
2124 map from a flat address to a private or local address.
2125
2126 FLAT instructions can take a flat address and access global, private (scratch)
2127 and group (LDS) memory depending in if the address is within one of the
2128 apperture ranges. Flat access to scratch requires hardware aperture setup and
2129 setup in the kernel prologue (see :ref:`amdgpu-amdhsa-flat-scratch`). Flat
2130 access to LDS requires hardware aperture setup and M0 (GFX7-GFX8) register setup
2131 (see :ref:`amdgpu-amdhsa-m0`).
2132
2133 To convert between a segment address and a flat address the base address of the
2134 appertures address can be used. For GFX7-GFX8 these are available in the
2135 :ref:`amdgpu-amdhsa-hsa-aql-queue` the address of which can be obtained with
2136 Queue Ptr SGPR (see :ref:`amdgpu-amdhsa-initial-kernel-execution-state`). For
2137 GFX9-GFX10 the appature base addresses are directly available as inline constant
2138 registers ``SRC_SHARED_BASE/LIMIT`` and ``SRC_PRIVATE_BASE/LIMIT``. In 64 bit
2139 address mode the apperture sizes are 2^32 bytes and the base is aligned to 2^32
2140 which makes it easier to convert from flat to segment or segment to flat.
2141
2142 Image and Samplers
2143 ~~~~~~~~~~~~~~~~~~
2144
2145 Image and sample handles created by the ROCm runtime are 64 bit addresses of a
2146 hardware 32 byte V# and 48 byte S# object respectively. In order to support the
2147 HSA ``query_sampler`` operations two extra dwords are used to store the HSA BRIG
2148 enumeration values for the queries that are not trivially deducible from the S#
2149 representation.
2150
2151 HSA Signals
2152 ~~~~~~~~~~~
2153
2154 HSA signal handles created by the ROCm runtime are 64 bit addresses of a
2155 structure allocated in memory accessible from both the CPU and GPU. The
2156 structure is defined by the ROCm runtime and subject to change between releases
2157 (see [AMD-ROCm-github]_).
2158
2159 .. _amdgpu-amdhsa-hsa-aql-queue:
2160
2161 HSA AQL Queue
2162 ~~~~~~~~~~~~~
2163
2164 The HSA AQL queue structure is defined by the ROCm runtime and subject to change
2165 between releases (see [AMD-ROCm-github]_). For some processors it contains
2166 fields needed to implement certain language features such as the flat address
2167 aperture bases. It also contains fields used by CP such as managing the
2168 allocation of scratch memory.
2169
2170 .. _amdgpu-amdhsa-kernel-descriptor:
2171
2172 Kernel Descriptor
2173 ~~~~~~~~~~~~~~~~~
2174
2175 A kernel descriptor consists of the information needed by CP to initiate the
2176 execution of a kernel, including the entry point address of the machine code
2177 that implements the kernel.
2178
2179 Kernel Descriptor for GFX6-GFX10
2180 ++++++++++++++++++++++++++++++++
2181
2182 CP microcode requires the Kernel descriptor to be allocated on 64 byte
2183 alignment.
2184
2185   .. table:: Kernel Descriptor for GFX6-GFX10
2186      :name: amdgpu-amdhsa-kernel-descriptor-gfx6-gfx10-table
2187
2188      ======= ======= =============================== ============================
2189      Bits    Size    Field Name                      Description
2190      ======= ======= =============================== ============================
2191      31:0    4 bytes GROUP_SEGMENT_FIXED_SIZE        The amount of fixed local
2192                                                      address space memory
2193                                                      required for a work-group
2194                                                      in bytes. This does not
2195                                                      include any dynamically
2196                                                      allocated local address
2197                                                      space memory that may be
2198                                                      added when the kernel is
2199                                                      dispatched.
2200      63:32   4 bytes PRIVATE_SEGMENT_FIXED_SIZE      The amount of fixed
2201                                                      private address space
2202                                                      memory required for a
2203                                                      work-item in bytes. If
2204                                                      is_dynamic_callstack is 1
2205                                                      then additional space must
2206                                                      be added to this value for
2207                                                      the call stack.
2208      127:64  8 bytes                                 Reserved, must be 0.
2209      191:128 8 bytes KERNEL_CODE_ENTRY_BYTE_OFFSET   Byte offset (possibly
2210                                                      negative) from base
2211                                                      address of kernel
2212                                                      descriptor to kernel's
2213                                                      entry point instruction
2214                                                      which must be 256 byte
2215                                                      aligned.
2216      351:272 20                                      Reserved, must be 0.
2217              bytes
2218      383:352 4 bytes COMPUTE_PGM_RSRC3               GFX6-9
2219                                                        Reserved, must be 0.
2220                                                      GFX10
2221                                                        Compute Shader (CS)
2222                                                        program settings used by
2223                                                        CP to set up
2224                                                        ``COMPUTE_PGM_RSRC3``
2225                                                        configuration
2226                                                        register. See
2227                                                        :ref:`amdgpu-amdhsa-compute_pgm_rsrc3-gfx10-table`.
2228      415:384 4 bytes COMPUTE_PGM_RSRC1               Compute Shader (CS)
2229                                                      program settings used by
2230                                                      CP to set up
2231                                                      ``COMPUTE_PGM_RSRC1``
2232                                                      configuration
2233                                                      register. See
2234                                                      :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
2235      447:416 4 bytes COMPUTE_PGM_RSRC2               Compute Shader (CS)
2236                                                      program settings used by
2237                                                      CP to set up
2238                                                      ``COMPUTE_PGM_RSRC2``
2239                                                      configuration
2240                                                      register. See
2241                                                      :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
2242      448     1 bit   ENABLE_SGPR_PRIVATE_SEGMENT     Enable the setup of the
2243                      _BUFFER                         SGPR user data registers
2244                                                      (see
2245                                                      :ref:`amdgpu-amdhsa-initial-kernel-execution-state`).
2246
2247                                                      The total number of SGPR
2248                                                      user data registers
2249                                                      requested must not exceed
2250                                                      16 and match value in
2251                                                      ``compute_pgm_rsrc2.user_sgpr.user_sgpr_count``.
2252                                                      Any requests beyond 16
2253                                                      will be ignored.
2254      449     1 bit   ENABLE_SGPR_DISPATCH_PTR        *see above*
2255      450     1 bit   ENABLE_SGPR_QUEUE_PTR           *see above*
2256      451     1 bit   ENABLE_SGPR_KERNARG_SEGMENT_PTR *see above*
2257      452     1 bit   ENABLE_SGPR_DISPATCH_ID         *see above*
2258      453     1 bit   ENABLE_SGPR_FLAT_SCRATCH_INIT   *see above*
2259      454     1 bit   ENABLE_SGPR_PRIVATE_SEGMENT     *see above*
2260                      _SIZE
2261      457:455 3 bits                                  Reserved, must be 0.
2262      458     1 bit   ENABLE_WAVEFRONT_SIZE32         GFX6-9
2263                                                        Reserved, must be 0.
2264                                                      GFX10
2265                                                        - If 0 execute in
2266                                                          wavefront size 64 mode.
2267                                                        - If 1 execute in
2268                                                          native wavefront size
2269                                                          32 mode.
2270      463:459 5 bits                                  Reserved, must be 0.
2271      511:464 6 bytes                                 Reserved, must be 0.
2272      512     **Total size 64 bytes.**
2273      ======= ====================================================================
2274
2275 ..
2276
2277   .. table:: compute_pgm_rsrc1 for GFX6-GFX10
2278      :name: amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table
2279
2280      ======= ======= =============================== ===========================================================================
2281      Bits    Size    Field Name                      Description
2282      ======= ======= =============================== ===========================================================================
2283      5:0     6 bits  GRANULATED_WORKITEM_VGPR_COUNT  Number of vector register
2284                                                      blocks used by each work-item;
2285                                                      granularity is device
2286                                                      specific:
2287
2288                                                      GFX6-GFX9
2289                                                        - vgprs_used 0..256
2290                                                        - max(0, ceil(vgprs_used / 4) - 1)
2291                                                      GFX10 (wavefront size 64)
2292                                                        - max_vgpr 1..256
2293                                                        - max(0, ceil(vgprs_used / 4) - 1)
2294                                                      GFX10 (wavefront size 32)
2295                                                        - max_vgpr 1..256
2296                                                        - max(0, ceil(vgprs_used / 8) - 1)
2297
2298                                                      Where vgprs_used is defined
2299                                                      as the highest VGPR number
2300                                                      explicitly referenced plus
2301                                                      one.
2302
2303                                                      Used by CP to set up
2304                                                      ``COMPUTE_PGM_RSRC1.VGPRS``.
2305
2306                                                      The
2307                                                      :ref:`amdgpu-assembler`
2308                                                      calculates this
2309                                                      automatically for the
2310                                                      selected processor from
2311                                                      values provided to the
2312                                                      `.amdhsa_kernel` directive
2313                                                      by the
2314                                                      `.amdhsa_next_free_vgpr`
2315                                                      nested directive (see
2316                                                      :ref:`amdhsa-kernel-directives-table`).
2317      9:6     4 bits  GRANULATED_WAVEFRONT_SGPR_COUNT Number of scalar register
2318                                                      blocks used by a wavefront;
2319                                                      granularity is device
2320                                                      specific:
2321
2322                                                      GFX6-GFX8
2323                                                        - sgprs_used 0..112
2324                                                        - max(0, ceil(sgprs_used / 8) - 1)
2325                                                      GFX9
2326                                                        - sgprs_used 0..112
2327                                                        - 2 * max(0, ceil(sgprs_used / 16) - 1)
2328                                                      GFX10
2329                                                        Reserved, must be 0.
2330                                                        (128 SGPRs always
2331                                                        allocated.)
2332
2333                                                      Where sgprs_used is
2334                                                      defined as the highest
2335                                                      SGPR number explicitly
2336                                                      referenced plus one, plus
2337                                                      a target-specific number
2338                                                      of additional special
2339                                                      SGPRs for VCC,
2340                                                      FLAT_SCRATCH (GFX7+) and
2341                                                      XNACK_MASK (GFX8+), and
2342                                                      any additional
2343                                                      target-specific
2344                                                      limitations. It does not
2345                                                      include the 16 SGPRs added
2346                                                      if a trap handler is
2347                                                      enabled.
2348
2349                                                      The target-specific
2350                                                      limitations and special
2351                                                      SGPR layout are defined in
2352                                                      the hardware
2353                                                      documentation, which can
2354                                                      be found in the
2355                                                      :ref:`amdgpu-processors`
2356                                                      table.
2357
2358                                                      Used by CP to set up
2359                                                      ``COMPUTE_PGM_RSRC1.SGPRS``.
2360
2361                                                      The
2362                                                      :ref:`amdgpu-assembler`
2363                                                      calculates this
2364                                                      automatically for the
2365                                                      selected processor from
2366                                                      values provided to the
2367                                                      `.amdhsa_kernel` directive
2368                                                      by the
2369                                                      `.amdhsa_next_free_sgpr`
2370                                                      and `.amdhsa_reserve_*`
2371                                                      nested directives (see
2372                                                      :ref:`amdhsa-kernel-directives-table`).
2373      11:10   2 bits  PRIORITY                        Must be 0.
2374
2375                                                      Start executing wavefront
2376                                                      at the specified priority.
2377
2378                                                      CP is responsible for
2379                                                      filling in
2380                                                      ``COMPUTE_PGM_RSRC1.PRIORITY``.
2381      13:12   2 bits  FLOAT_ROUND_MODE_32             Wavefront starts execution
2382                                                      with specified rounding
2383                                                      mode for single (32
2384                                                      bit) floating point
2385                                                      precision floating point
2386                                                      operations.
2387
2388                                                      Floating point rounding
2389                                                      mode values are defined in
2390                                                      :ref:`amdgpu-amdhsa-floating-point-rounding-mode-enumeration-values-table`.
2391
2392                                                      Used by CP to set up
2393                                                      ``COMPUTE_PGM_RSRC1.FLOAT_MODE``.
2394      15:14   2 bits  FLOAT_ROUND_MODE_16_64          Wavefront starts execution
2395                                                      with specified rounding
2396                                                      denorm mode for half/double (16
2397                                                      and 64 bit) floating point
2398                                                      precision floating point
2399                                                      operations.
2400
2401                                                      Floating point rounding
2402                                                      mode values are defined in
2403                                                      :ref:`amdgpu-amdhsa-floating-point-rounding-mode-enumeration-values-table`.
2404
2405                                                      Used by CP to set up
2406                                                      ``COMPUTE_PGM_RSRC1.FLOAT_MODE``.
2407      17:16   2 bits  FLOAT_DENORM_MODE_32            Wavefront starts execution
2408                                                      with specified denorm mode
2409                                                      for single (32
2410                                                      bit)  floating point
2411                                                      precision floating point
2412                                                      operations.
2413
2414                                                      Floating point denorm mode
2415                                                      values are defined in
2416                                                      :ref:`amdgpu-amdhsa-floating-point-denorm-mode-enumeration-values-table`.
2417
2418                                                      Used by CP to set up
2419                                                      ``COMPUTE_PGM_RSRC1.FLOAT_MODE``.
2420      19:18   2 bits  FLOAT_DENORM_MODE_16_64         Wavefront starts execution
2421                                                      with specified denorm mode
2422                                                      for half/double (16
2423                                                      and 64 bit) floating point
2424                                                      precision floating point
2425                                                      operations.
2426
2427                                                      Floating point denorm mode
2428                                                      values are defined in
2429                                                      :ref:`amdgpu-amdhsa-floating-point-denorm-mode-enumeration-values-table`.
2430
2431                                                      Used by CP to set up
2432                                                      ``COMPUTE_PGM_RSRC1.FLOAT_MODE``.
2433      20      1 bit   PRIV                            Must be 0.
2434
2435                                                      Start executing wavefront
2436                                                      in privilege trap handler
2437                                                      mode.
2438
2439                                                      CP is responsible for
2440                                                      filling in
2441                                                      ``COMPUTE_PGM_RSRC1.PRIV``.
2442      21      1 bit   ENABLE_DX10_CLAMP               Wavefront starts execution
2443                                                      with DX10 clamp mode
2444                                                      enabled. Used by the vector
2445                                                      ALU to force DX10 style
2446                                                      treatment of NaN's (when
2447                                                      set, clamp NaN to zero,
2448                                                      otherwise pass NaN
2449                                                      through).
2450
2451                                                      Used by CP to set up
2452                                                      ``COMPUTE_PGM_RSRC1.DX10_CLAMP``.
2453      22      1 bit   DEBUG_MODE                      Must be 0.
2454
2455                                                      Start executing wavefront
2456                                                      in single step mode.
2457
2458                                                      CP is responsible for
2459                                                      filling in
2460                                                      ``COMPUTE_PGM_RSRC1.DEBUG_MODE``.
2461      23      1 bit   ENABLE_IEEE_MODE                Wavefront starts execution
2462                                                      with IEEE mode
2463                                                      enabled. Floating point
2464                                                      opcodes that support
2465                                                      exception flag gathering
2466                                                      will quiet and propagate
2467                                                      signaling-NaN inputs per
2468                                                      IEEE 754-2008. Min_dx10 and
2469                                                      max_dx10 become IEEE
2470                                                      754-2008 compliant due to
2471                                                      signaling-NaN propagation
2472                                                      and quieting.
2473
2474                                                      Used by CP to set up
2475                                                      ``COMPUTE_PGM_RSRC1.IEEE_MODE``.
2476      24      1 bit   BULKY                           Must be 0.
2477
2478                                                      Only one work-group allowed
2479                                                      to execute on a compute
2480                                                      unit.
2481
2482                                                      CP is responsible for
2483                                                      filling in
2484                                                      ``COMPUTE_PGM_RSRC1.BULKY``.
2485      25      1 bit   CDBG_USER                       Must be 0.
2486
2487                                                      Flag that can be used to
2488                                                      control debugging code.
2489
2490                                                      CP is responsible for
2491                                                      filling in
2492                                                      ``COMPUTE_PGM_RSRC1.CDBG_USER``.
2493      26      1 bit   FP16_OVFL                       GFX6-GFX8
2494                                                        Reserved, must be 0.
2495                                                      GFX9-GFX10
2496                                                        Wavefront starts execution
2497                                                        with specified fp16 overflow
2498                                                        mode.
2499
2500                                                        - If 0, fp16 overflow generates
2501                                                          +/-INF values.
2502                                                        - If 1, fp16 overflow that is the
2503                                                          result of an +/-INF input value
2504                                                          or divide by 0 produces a +/-INF,
2505                                                          otherwise clamps computed
2506                                                          overflow to +/-MAX_FP16 as
2507                                                          appropriate.
2508
2509                                                        Used by CP to set up
2510                                                        ``COMPUTE_PGM_RSRC1.FP16_OVFL``.
2511      28:27   2 bits                                  Reserved, must be 0.
2512      29      1 bit    WGP_MODE                       GFX6-GFX9
2513                                                        Reserved, must be 0.
2514                                                      GFX10
2515                                                        - If 0 execute work-groups in
2516                                                          CU wavefront execution mode.
2517                                                        - If 1 execute work-groups on
2518                                                          in WGP wavefront execution mode.
2519
2520                                                        See :ref:`amdgpu-amdhsa-memory-model`.
2521
2522                                                        Used by CP to set up
2523                                                        ``COMPUTE_PGM_RSRC1.WGP_MODE``.
2524      30      1 bit    MEM_ORDERED                    GFX6-9
2525                                                        Reserved, must be 0.
2526                                                      GFX10
2527                                                        Controls the behavior of the
2528                                                        waitcnt's vmcnt and vscnt
2529                                                        counters.
2530
2531                                                        - If 0 vmcnt reports completion
2532                                                          of load and atomic with return
2533                                                          out of order with sample
2534                                                          instructions, and the vscnt
2535                                                          reports the completion of
2536                                                          store and atomic without
2537                                                          return in order.
2538                                                        - If 1 vmcnt reports completion
2539                                                          of load, atomic with return
2540                                                          and sample instructions in
2541                                                          order, and the vscnt reports
2542                                                          the completion of store and
2543                                                          atomic without return in order.
2544
2545                                                        Used by CP to set up
2546                                                        ``COMPUTE_PGM_RSRC1.MEM_ORDERED``.
2547      31      1 bit    FWD_PROGRESS                   GFX6-9
2548                                                        Reserved, must be 0.
2549                                                      GFX10
2550                                                        - If 0 execute SIMD wavefronts
2551                                                          using oldest first policy.
2552                                                        - If 1 execute SIMD wavefronts to
2553                                                          ensure wavefronts will make some
2554                                                          forward progress.
2555
2556                                                        Used by CP to set up
2557                                                        ``COMPUTE_PGM_RSRC1.FWD_PROGRESS``.
2558      32      **Total size 4 bytes**
2559      ======= ===================================================================================================================
2560
2561 ..
2562
2563   .. table:: compute_pgm_rsrc2 for GFX6-GFX10
2564      :name: amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table
2565
2566      ======= ======= =============================== ===========================================================================
2567      Bits    Size    Field Name                      Description
2568      ======= ======= =============================== ===========================================================================
2569      0       1 bit   ENABLE_SGPR_PRIVATE_SEGMENT     Enable the setup of the
2570                      _WAVEFRONT_OFFSET               SGPR wavefront scratch offset
2571                                                      system register (see
2572                                                      :ref:`amdgpu-amdhsa-initial-kernel-execution-state`).
2573
2574                                                      Used by CP to set up
2575                                                      ``COMPUTE_PGM_RSRC2.SCRATCH_EN``.
2576      5:1     5 bits  USER_SGPR_COUNT                 The total number of SGPR
2577                                                      user data registers
2578                                                      requested. This number must
2579                                                      match the number of user
2580                                                      data registers enabled.
2581
2582                                                      Used by CP to set up
2583                                                      ``COMPUTE_PGM_RSRC2.USER_SGPR``.
2584      6       1 bit   ENABLE_TRAP_HANDLER             Must be 0.
2585
2586                                                      This bit represents
2587                                                      ``COMPUTE_PGM_RSRC2.TRAP_PRESENT``,
2588                                                      which is set by the CP if
2589                                                      the runtime has installed a
2590                                                      trap handler.
2591      7       1 bit   ENABLE_SGPR_WORKGROUP_ID_X      Enable the setup of the
2592                                                      system SGPR register for
2593                                                      the work-group id in the X
2594                                                      dimension (see
2595                                                      :ref:`amdgpu-amdhsa-initial-kernel-execution-state`).
2596
2597                                                      Used by CP to set up
2598                                                      ``COMPUTE_PGM_RSRC2.TGID_X_EN``.
2599      8       1 bit   ENABLE_SGPR_WORKGROUP_ID_Y      Enable the setup of the
2600                                                      system SGPR register for
2601                                                      the work-group id in the Y
2602                                                      dimension (see
2603                                                      :ref:`amdgpu-amdhsa-initial-kernel-execution-state`).
2604
2605                                                      Used by CP to set up
2606                                                      ``COMPUTE_PGM_RSRC2.TGID_Y_EN``.
2607      9       1 bit   ENABLE_SGPR_WORKGROUP_ID_Z      Enable the setup of the
2608                                                      system SGPR register for
2609                                                      the work-group id in the Z
2610                                                      dimension (see
2611                                                      :ref:`amdgpu-amdhsa-initial-kernel-execution-state`).
2612
2613                                                      Used by CP to set up
2614                                                      ``COMPUTE_PGM_RSRC2.TGID_Z_EN``.
2615      10      1 bit   ENABLE_SGPR_WORKGROUP_INFO      Enable the setup of the
2616                                                      system SGPR register for
2617                                                      work-group information (see
2618                                                      :ref:`amdgpu-amdhsa-initial-kernel-execution-state`).
2619
2620                                                      Used by CP to set up
2621                                                      ``COMPUTE_PGM_RSRC2.TGID_SIZE_EN``.
2622      12:11   2 bits  ENABLE_VGPR_WORKITEM_ID         Enable the setup of the
2623                                                      VGPR system registers used
2624                                                      for the work-item ID.
2625                                                      :ref:`amdgpu-amdhsa-system-vgpr-work-item-id-enumeration-values-table`
2626                                                      defines the values.
2627
2628                                                      Used by CP to set up
2629                                                      ``COMPUTE_PGM_RSRC2.TIDIG_CMP_CNT``.
2630      13      1 bit   ENABLE_EXCEPTION_ADDRESS_WATCH  Must be 0.
2631
2632                                                      Wavefront starts execution
2633                                                      with address watch
2634                                                      exceptions enabled which
2635                                                      are generated when L1 has
2636                                                      witnessed a thread access
2637                                                      an *address of
2638                                                      interest*.
2639
2640                                                      CP is responsible for
2641                                                      filling in the address
2642                                                      watch bit in
2643                                                      ``COMPUTE_PGM_RSRC2.EXCP_EN_MSB``
2644                                                      according to what the
2645                                                      runtime requests.
2646      14      1 bit   ENABLE_EXCEPTION_MEMORY         Must be 0.
2647
2648                                                      Wavefront starts execution
2649                                                      with memory violation
2650                                                      exceptions exceptions
2651                                                      enabled which are generated
2652                                                      when a memory violation has
2653                                                      occurred for this wavefront from
2654                                                      L1 or LDS
2655                                                      (write-to-read-only-memory,
2656                                                      mis-aligned atomic, LDS
2657                                                      address out of range,
2658                                                      illegal address, etc.).
2659
2660                                                      CP sets the memory
2661                                                      violation bit in
2662                                                      ``COMPUTE_PGM_RSRC2.EXCP_EN_MSB``
2663                                                      according to what the
2664                                                      runtime requests.
2665      23:15   9 bits  GRANULATED_LDS_SIZE             Must be 0.
2666
2667                                                      CP uses the rounded value
2668                                                      from the dispatch packet,
2669                                                      not this value, as the
2670                                                      dispatch may contain
2671                                                      dynamically allocated group
2672                                                      segment memory. CP writes
2673                                                      directly to
2674                                                      ``COMPUTE_PGM_RSRC2.LDS_SIZE``.
2675
2676                                                      Amount of group segment
2677                                                      (LDS) to allocate for each
2678                                                      work-group. Granularity is
2679                                                      device specific:
2680
2681                                                      GFX6:
2682                                                        roundup(lds-size / (64 * 4))
2683                                                      GFX7-GFX10:
2684                                                        roundup(lds-size / (128 * 4))
2685
2686      24      1 bit   ENABLE_EXCEPTION_IEEE_754_FP    Wavefront starts execution
2687                      _INVALID_OPERATION              with specified exceptions
2688                                                      enabled.
2689
2690                                                      Used by CP to set up
2691                                                      ``COMPUTE_PGM_RSRC2.EXCP_EN``
2692                                                      (set from bits 0..6).
2693
2694                                                      IEEE 754 FP Invalid
2695                                                      Operation
2696      25      1 bit   ENABLE_EXCEPTION_FP_DENORMAL    FP Denormal one or more
2697                      _SOURCE                         input operands is a
2698                                                      denormal number
2699      26      1 bit   ENABLE_EXCEPTION_IEEE_754_FP    IEEE 754 FP Division by
2700                      _DIVISION_BY_ZERO               Zero
2701      27      1 bit   ENABLE_EXCEPTION_IEEE_754_FP    IEEE 754 FP FP Overflow
2702                      _OVERFLOW
2703      28      1 bit   ENABLE_EXCEPTION_IEEE_754_FP    IEEE 754 FP Underflow
2704                      _UNDERFLOW
2705      29      1 bit   ENABLE_EXCEPTION_IEEE_754_FP    IEEE 754 FP Inexact
2706                      _INEXACT
2707      30      1 bit   ENABLE_EXCEPTION_INT_DIVIDE_BY  Integer Division by Zero
2708                      _ZERO                           (rcp_iflag_f32 instruction
2709                                                      only)
2710      31      1 bit                                   Reserved, must be 0.
2711      32      **Total size 4 bytes.**
2712      ======= ===================================================================================================================
2713
2714 ..
2715
2716   .. table:: compute_pgm_rsrc3 for GFX10
2717      :name: amdgpu-amdhsa-compute_pgm_rsrc3-gfx10-table
2718
2719      ======= ======= =============================== ===========================================================================
2720      Bits    Size    Field Name                      Description
2721      ======= ======= =============================== ===========================================================================
2722      3:0     4 bits  SHARED_VGPR_COUNT               Number of shared VGPRs for wavefront size 64. Granularity 8. Value 0-120.
2723                                                      compute_pgm_rsrc1.vgprs + shared_vgpr_cnt cannot exceed 64.
2724      31:4    28                                      Reserved, must be 0.
2725              bits
2726      32      **Total size 4 bytes.**
2727      ======= ===================================================================================================================
2728
2729 ..
2730
2731   .. table:: Floating Point Rounding Mode Enumeration Values
2732      :name: amdgpu-amdhsa-floating-point-rounding-mode-enumeration-values-table
2733
2734      ====================================== ===== ==============================
2735      Enumeration Name                       Value Description
2736      ====================================== ===== ==============================
2737      FLOAT_ROUND_MODE_NEAR_EVEN             0     Round Ties To Even
2738      FLOAT_ROUND_MODE_PLUS_INFINITY         1     Round Toward +infinity
2739      FLOAT_ROUND_MODE_MINUS_INFINITY        2     Round Toward -infinity
2740      FLOAT_ROUND_MODE_ZERO                  3     Round Toward 0
2741      ====================================== ===== ==============================
2742
2743 ..
2744
2745   .. table:: Floating Point Denorm Mode Enumeration Values
2746      :name: amdgpu-amdhsa-floating-point-denorm-mode-enumeration-values-table
2747
2748      ====================================== ===== ==============================
2749      Enumeration Name                       Value Description
2750      ====================================== ===== ==============================
2751      FLOAT_DENORM_MODE_FLUSH_SRC_DST        0     Flush Source and Destination
2752                                                   Denorms
2753      FLOAT_DENORM_MODE_FLUSH_DST            1     Flush Output Denorms
2754      FLOAT_DENORM_MODE_FLUSH_SRC            2     Flush Source Denorms
2755      FLOAT_DENORM_MODE_FLUSH_NONE           3     No Flush
2756      ====================================== ===== ==============================
2757
2758 ..
2759
2760   .. table:: System VGPR Work-Item ID Enumeration Values
2761      :name: amdgpu-amdhsa-system-vgpr-work-item-id-enumeration-values-table
2762
2763      ======================================== ===== ============================
2764      Enumeration Name                         Value Description
2765      ======================================== ===== ============================
2766      SYSTEM_VGPR_WORKITEM_ID_X                0     Set work-item X dimension
2767                                                     ID.
2768      SYSTEM_VGPR_WORKITEM_ID_X_Y              1     Set work-item X and Y
2769                                                     dimensions ID.
2770      SYSTEM_VGPR_WORKITEM_ID_X_Y_Z            2     Set work-item X, Y and Z
2771                                                     dimensions ID.
2772      SYSTEM_VGPR_WORKITEM_ID_UNDEFINED        3     Undefined.
2773      ======================================== ===== ============================
2774
2775 .. _amdgpu-amdhsa-initial-kernel-execution-state:
2776
2777 Initial Kernel Execution State
2778 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2779
2780 This section defines the register state that will be set up by the packet
2781 processor prior to the start of execution of every wavefront. This is limited by
2782 the constraints of the hardware controllers of CP/ADC/SPI.
2783
2784 The order of the SGPR registers is defined, but the compiler can specify which
2785 ones are actually setup in the kernel descriptor using the ``enable_sgpr_*`` bit
2786 fields (see :ref:`amdgpu-amdhsa-kernel-descriptor`). The register numbers used
2787 for enabled registers are dense starting at SGPR0: the first enabled register is
2788 SGPR0, the next enabled register is SGPR1 etc.; disabled registers do not have
2789 an SGPR number.
2790
2791 The initial SGPRs comprise up to 16 User SRGPs that are set by CP and apply to
2792 all wavefronts of the grid. It is possible to specify more than 16 User SGPRs using
2793 the ``enable_sgpr_*`` bit fields, in which case only the first 16 are actually
2794 initialized. These are then immediately followed by the System SGPRs that are
2795 set up by ADC/SPI and can have different values for each wavefront of the grid
2796 dispatch.
2797
2798 SGPR register initial state is defined in
2799 :ref:`amdgpu-amdhsa-sgpr-register-set-up-order-table`.
2800
2801   .. table:: SGPR Register Set Up Order
2802      :name: amdgpu-amdhsa-sgpr-register-set-up-order-table
2803
2804      ========== ========================== ====== ==============================
2805      SGPR Order Name                       Number Description
2806                 (kernel descriptor enable  of
2807                 field)                     SGPRs
2808      ========== ========================== ====== ==============================
2809      First      Private Segment Buffer     4      V# that can be used, together
2810                 (enable_sgpr_private              with Scratch Wavefront Offset
2811                 _segment_buffer)                  as an offset, to access the
2812                                                   private memory space using a
2813                                                   segment address.
2814
2815                                                   CP uses the value provided by
2816                                                   the runtime.
2817      then       Dispatch Ptr               2      64 bit address of AQL dispatch
2818                 (enable_sgpr_dispatch_ptr)        packet for kernel dispatch
2819                                                   actually executing.
2820      then       Queue Ptr                  2      64 bit address of amd_queue_t
2821                 (enable_sgpr_queue_ptr)           object for AQL queue on which
2822                                                   the dispatch packet was
2823                                                   queued.
2824      then       Kernarg Segment Ptr        2      64 bit address of Kernarg
2825                 (enable_sgpr_kernarg              segment. This is directly
2826                 _segment_ptr)                     copied from the
2827                                                   kernarg_address in the kernel
2828                                                   dispatch packet.
2829
2830                                                   Having CP load it once avoids
2831                                                   loading it at the beginning of
2832                                                   every wavefront.
2833      then       Dispatch Id                2      64 bit Dispatch ID of the
2834                 (enable_sgpr_dispatch_id)         dispatch packet being
2835                                                   executed.
2836      then       Flat Scratch Init          2      This is 2 SGPRs:
2837                 (enable_sgpr_flat_scratch
2838                 _init)                            GFX6
2839                                                     Not supported.
2840                                                   GFX7-GFX8
2841                                                     The first SGPR is a 32 bit
2842                                                     byte offset from
2843                                                     ``SH_HIDDEN_PRIVATE_BASE_VIMID``
2844                                                     to per SPI base of memory
2845                                                     for scratch for the queue
2846                                                     executing the kernel
2847                                                     dispatch. CP obtains this
2848                                                     from the runtime. (The
2849                                                     Scratch Segment Buffer base
2850                                                     address is
2851                                                     ``SH_HIDDEN_PRIVATE_BASE_VIMID``
2852                                                     plus this offset.) The value
2853                                                     of Scratch Wavefront Offset must
2854                                                     be added to this offset by
2855                                                     the kernel machine code,
2856                                                     right shifted by 8, and
2857                                                     moved to the FLAT_SCRATCH_HI
2858                                                     SGPR register.
2859                                                     FLAT_SCRATCH_HI corresponds
2860                                                     to SGPRn-4 on GFX7, and
2861                                                     SGPRn-6 on GFX8 (where SGPRn
2862                                                     is the highest numbered SGPR
2863                                                     allocated to the wavefront).
2864                                                     FLAT_SCRATCH_HI is
2865                                                     multiplied by 256 (as it is
2866                                                     in units of 256 bytes) and
2867                                                     added to
2868                                                     ``SH_HIDDEN_PRIVATE_BASE_VIMID``
2869                                                     to calculate the per wavefront
2870                                                     FLAT SCRATCH BASE in flat
2871                                                     memory instructions that
2872                                                     access the scratch
2873                                                     apperture.
2874
2875                                                     The second SGPR is 32 bit
2876                                                     byte size of a single
2877                                                     work-item's scratch memory
2878                                                     usage. CP obtains this from
2879                                                     the runtime, and it is
2880                                                     always a multiple of DWORD.
2881                                                     CP checks that the value in
2882                                                     the kernel dispatch packet
2883                                                     Private Segment Byte Size is
2884                                                     not larger, and requests the
2885                                                     runtime to increase the
2886                                                     queue's scratch size if
2887                                                     necessary. The kernel code
2888                                                     must move it to
2889                                                     FLAT_SCRATCH_LO which is
2890                                                     SGPRn-3 on GFX7 and SGPRn-5
2891                                                     on GFX8. FLAT_SCRATCH_LO is
2892                                                     used as the FLAT SCRATCH
2893                                                     SIZE in flat memory
2894                                                     instructions. Having CP load
2895                                                     it once avoids loading it at
2896                                                     the beginning of every
2897                                                     wavefront.
2898                                                   GFX9-GFX10
2899                                                     This is the
2900                                                     64 bit base address of the
2901                                                     per SPI scratch backing
2902                                                     memory managed by SPI for
2903                                                     the queue executing the
2904                                                     kernel dispatch. CP obtains
2905                                                     this from the runtime (and
2906                                                     divides it if there are
2907                                                     multiple Shader Arrays each
2908                                                     with its own SPI). The value
2909                                                     of Scratch Wavefront Offset must
2910                                                     be added by the kernel
2911                                                     machine code and the result
2912                                                     moved to the FLAT_SCRATCH
2913                                                     SGPR which is SGPRn-6 and
2914                                                     SGPRn-5. It is used as the
2915                                                     FLAT SCRATCH BASE in flat
2916                                                     memory instructions.
2917      then       Private Segment Size       1      The 32 bit byte size of a
2918                                                   (enable_sgpr_private single
2919                                                   work-item's
2920                                                   scratch_segment_size) memory
2921                                                   allocation. This is the
2922                                                   value from the kernel
2923                                                   dispatch packet Private
2924                                                   Segment Byte Size rounded up
2925                                                   by CP to a multiple of
2926                                                   DWORD.
2927
2928                                                   Having CP load it once avoids
2929                                                   loading it at the beginning of
2930                                                   every wavefront.
2931
2932                                                   This is not used for
2933                                                   GFX7-GFX8 since it is the same
2934                                                   value as the second SGPR of
2935                                                   Flat Scratch Init. However, it
2936                                                   may be needed for GFX9-GFX10 which
2937                                                   changes the meaning of the
2938                                                   Flat Scratch Init value.
2939      then       Grid Work-Group Count X    1      32 bit count of the number of
2940                 (enable_sgpr_grid                 work-groups in the X dimension
2941                 _workgroup_count_X)               for the grid being
2942                                                   executed. Computed from the
2943                                                   fields in the kernel dispatch
2944                                                   packet as ((grid_size.x +
2945                                                   workgroup_size.x - 1) /
2946                                                   workgroup_size.x).
2947      then       Grid Work-Group Count Y    1      32 bit count of the number of
2948                 (enable_sgpr_grid                 work-groups in the Y dimension
2949                 _workgroup_count_Y &&             for the grid being
2950                 less than 16 previous             executed. Computed from the
2951                 SGPRs)                            fields in the kernel dispatch
2952                                                   packet as ((grid_size.y +
2953                                                   workgroup_size.y - 1) /
2954                                                   workgroupSize.y).
2955
2956                                                   Only initialized if <16
2957                                                   previous SGPRs initialized.
2958      then       Grid Work-Group Count Z    1      32 bit count of the number of
2959                 (enable_sgpr_grid                 work-groups in the Z dimension
2960                 _workgroup_count_Z &&             for the grid being
2961                 less than 16 previous             executed. Computed from the
2962                 SGPRs)                            fields in the kernel dispatch
2963                                                   packet as ((grid_size.z +
2964                                                   workgroup_size.z - 1) /
2965                                                   workgroupSize.z).
2966
2967                                                   Only initialized if <16
2968                                                   previous SGPRs initialized.
2969      then       Work-Group Id X            1      32 bit work-group id in X
2970                 (enable_sgpr_workgroup_id         dimension of grid for
2971                 _X)                               wavefront.
2972      then       Work-Group Id Y            1      32 bit work-group id in Y
2973                 (enable_sgpr_workgroup_id         dimension of grid for
2974                 _Y)                               wavefront.
2975      then       Work-Group Id Z            1      32 bit work-group id in Z
2976                 (enable_sgpr_workgroup_id         dimension of grid for
2977                 _Z)                               wavefront.
2978      then       Work-Group Info            1      {first_wavefront, 14'b0000,
2979                 (enable_sgpr_workgroup            ordered_append_term[10:0],
2980                 _info)                            threadgroup_size_in_wavefronts[5:0]}
2981      then       Scratch Wavefront Offset   1      32 bit byte offset from base
2982                 (enable_sgpr_private              of scratch base of queue
2983                 _segment_wavefront_offset)        executing the kernel
2984                                                   dispatch. Must be used as an
2985                                                   offset with Private
2986                                                   segment address when using
2987                                                   Scratch Segment Buffer. It
2988                                                   must be used to set up FLAT
2989                                                   SCRATCH for flat addressing
2990                                                   (see
2991                                                   :ref:`amdgpu-amdhsa-flat-scratch`).
2992      ========== ========================== ====== ==============================
2993
2994 The order of the VGPR registers is defined, but the compiler can specify which
2995 ones are actually setup in the kernel descriptor using the ``enable_vgpr*`` bit
2996 fields (see :ref:`amdgpu-amdhsa-kernel-descriptor`). The register numbers used
2997 for enabled registers are dense starting at VGPR0: the first enabled register is
2998 VGPR0, the next enabled register is VGPR1 etc.; disabled registers do not have a
2999 VGPR number.
3000
3001 VGPR register initial state is defined in
3002 :ref:`amdgpu-amdhsa-vgpr-register-set-up-order-table`.
3003
3004   .. table:: VGPR Register Set Up Order
3005      :name: amdgpu-amdhsa-vgpr-register-set-up-order-table
3006
3007      ========== ========================== ====== ==============================
3008      VGPR Order Name                       Number Description
3009                 (kernel descriptor enable  of
3010                 field)                     VGPRs
3011      ========== ========================== ====== ==============================
3012      First      Work-Item Id X             1      32 bit work item id in X
3013                 (Always initialized)              dimension of work-group for
3014                                                   wavefront lane.
3015      then       Work-Item Id Y             1      32 bit work item id in Y
3016                 (enable_vgpr_workitem_id          dimension of work-group for
3017                 > 0)                              wavefront lane.
3018      then       Work-Item Id Z             1      32 bit work item id in Z
3019                 (enable_vgpr_workitem_id          dimension of work-group for
3020                 > 1)                              wavefront lane.
3021      ========== ========================== ====== ==============================
3022
3023 The setting of registers is done by GPU CP/ADC/SPI hardware as follows:
3024
3025 1. SGPRs before the Work-Group Ids are set by CP using the 16 User Data
3026    registers.
3027 2. Work-group Id registers X, Y, Z are set by ADC which supports any
3028    combination including none.
3029 3. Scratch Wavefront Offset is set by SPI in a per wavefront basis which is why
3030    its value cannot included with the flat scratch init value which is per queue.
3031 4. The VGPRs are set by SPI which only supports specifying either (X), (X, Y)
3032    or (X, Y, Z).
3033
3034 Flat Scratch register pair are adjacent SGRRs so they can be moved as a 64 bit
3035 value to the hardware required SGPRn-3 and SGPRn-4 respectively.
3036
3037 The global segment can be accessed either using buffer instructions (GFX6 which
3038 has V# 64 bit address support), flat instructions (GFX7-GFX10), or global
3039 instructions (GFX9-GFX10).
3040
3041 If buffer operations are used then the compiler can generate a V# with the
3042 following properties:
3043
3044 * base address of 0
3045 * no swizzle
3046 * ATC: 1 if IOMMU present (such as APU)
3047 * ptr64: 1
3048 * MTYPE set to support memory coherence that matches the runtime (such as CC for
3049   APU and NC for dGPU).
3050
3051 .. _amdgpu-amdhsa-kernel-prolog:
3052
3053 Kernel Prolog
3054 ~~~~~~~~~~~~~
3055
3056 .. _amdgpu-amdhsa-m0:
3057
3058 M0
3059 ++
3060
3061 GFX6-GFX8
3062   The M0 register must be initialized with a value at least the total LDS size
3063   if the kernel may access LDS via DS or flat operations. Total LDS size is
3064   available in dispatch packet. For M0, it is also possible to use maximum
3065   possible value of LDS for given target (0x7FFF for GFX6 and 0xFFFF for
3066   GFX7-GFX8).
3067 GFX9-GFX10
3068   The M0 register is not used for range checking LDS accesses and so does not
3069   need to be initialized in the prolog.
3070
3071 .. _amdgpu-amdhsa-flat-scratch:
3072
3073 Flat Scratch
3074 ++++++++++++
3075
3076 If the kernel may use flat operations to access scratch memory, the prolog code
3077 must set up FLAT_SCRATCH register pair (FLAT_SCRATCH_LO/FLAT_SCRATCH_HI which
3078 are in SGPRn-4/SGPRn-3). Initialization uses Flat Scratch Init and Scratch Wavefront
3079 Offset SGPR registers (see :ref:`amdgpu-amdhsa-initial-kernel-execution-state`):
3080
3081 GFX6
3082   Flat scratch is not supported.
3083
3084 GFX7-GFX8
3085   1. The low word of Flat Scratch Init is 32 bit byte offset from
3086      ``SH_HIDDEN_PRIVATE_BASE_VIMID`` to the base of scratch backing memory
3087      being managed by SPI for the queue executing the kernel dispatch. This is
3088      the same value used in the Scratch Segment Buffer V# base address. The
3089      prolog must add the value of Scratch Wavefront Offset to get the wavefront's byte
3090      scratch backing memory offset from ``SH_HIDDEN_PRIVATE_BASE_VIMID``. Since
3091      FLAT_SCRATCH_LO is in units of 256 bytes, the offset must be right shifted
3092      by 8 before moving into FLAT_SCRATCH_LO.
3093   2. The second word of Flat Scratch Init is 32 bit byte size of a single
3094      work-items scratch memory usage. This is directly loaded from the kernel
3095      dispatch packet Private Segment Byte Size and rounded up to a multiple of
3096      DWORD. Having CP load it once avoids loading it at the beginning of every
3097      wavefront. The prolog must move it to FLAT_SCRATCH_LO for use as FLAT SCRATCH
3098      SIZE.
3099
3100 GFX9-GFX10
3101   The Flat Scratch Init is the 64 bit address of the base of scratch backing
3102   memory being managed by SPI for the queue executing the kernel dispatch. The
3103   prolog must add the value of Scratch Wavefront Offset and moved to the FLAT_SCRATCH
3104   pair for use as the flat scratch base in flat memory instructions.
3105
3106 .. _amdgpu-amdhsa-memory-model:
3107
3108 Memory Model
3109 ~~~~~~~~~~~~
3110
3111 This section describes the mapping of LLVM memory model onto AMDGPU machine code
3112 (see :ref:`memmodel`). *The implementation is WIP.*
3113
3114 .. TODO
3115    Update when implementation complete.
3116
3117 The AMDGPU backend supports the memory synchronization scopes specified in
3118 :ref:`amdgpu-memory-scopes`.
3119
3120 The code sequences used to implement the memory model are defined in table
3121 :ref:`amdgpu-amdhsa-memory-model-code-sequences-gfx6-gfx10-table`.
3122
3123 The sequences specify the order of instructions that a single thread must
3124 execute. The ``s_waitcnt`` and ``buffer_wbinvl1_vol`` are defined with respect
3125 to other memory instructions executed by the same thread. This allows them to be
3126 moved earlier or later which can allow them to be combined with other instances
3127 of the same instruction, or hoisted/sunk out of loops to improve
3128 performance. Only the instructions related to the memory model are given;
3129 additional ``s_waitcnt`` instructions are required to ensure registers are
3130 defined before being used. These may be able to be combined with the memory
3131 model ``s_waitcnt`` instructions as described above.
3132
3133 The AMDGPU backend supports the following memory models:
3134
3135   HSA Memory Model [HSA]_
3136     The HSA memory model uses a single happens-before relation for all address
3137     spaces (see :ref:`amdgpu-address-spaces`).
3138   OpenCL Memory Model [OpenCL]_
3139     The OpenCL memory model which has separate happens-before relations for the
3140     global and local address spaces. Only a fence specifying both global and
3141     local address space, and seq_cst instructions join the relationships. Since
3142     the LLVM ``memfence`` instruction does not allow an address space to be
3143     specified the OpenCL fence has to convervatively assume both local and
3144     global address space was specified. However, optimizations can often be
3145     done to eliminate the additional ``s_waitcnt`` instructions when there are
3146     no intervening memory instructions which access the corresponding address
3147     space. The code sequences in the table indicate what can be omitted for the
3148     OpenCL memory. The target triple environment is used to determine if the
3149     source language is OpenCL (see :ref:`amdgpu-opencl`).
3150
3151 ``ds/flat_load/store/atomic`` instructions to local memory are termed LDS
3152 operations.
3153
3154 ``buffer/global/flat_load/store/atomic`` instructions to global memory are
3155 termed vector memory operations.
3156
3157 For GFX6-GFX9:
3158
3159 * Each agent has multiple shader arrays (SA).
3160 * Each SA has multiple compute units (CU).
3161 * Each CU has multiple SIMDs that execute wavefronts.
3162 * The wavefronts for a single work-group are executed in the same CU but may be
3163   executed by different SIMDs.
3164 * Each CU has a single LDS memory shared by the wavefronts of the work-groups
3165   executing on it.
3166 * All LDS operations of a CU are performed as wavefront wide operations in a
3167   global order and involve no caching. Completion is reported to a wavefront in
3168   execution order.
3169 * The LDS memory has multiple request queues shared by the SIMDs of a
3170   CU. Therefore, the LDS operations performed by different wavefronts of a work-group
3171   can be reordered relative to each other, which can result in reordering the
3172   visibility of vector memory operations with respect to LDS operations of other
3173   wavefronts in the same work-group. A ``s_waitcnt lgkmcnt(0)`` is required to
3174   ensure synchronization between LDS operations and vector memory operations
3175   between wavefronts of a work-group, but not between operations performed by the
3176   same wavefront.
3177 * The vector memory operations are performed as wavefront wide operations and
3178   completion is reported to a wavefront in execution order. The exception is
3179   that for GFX7-GFX9 ``flat_load/store/atomic`` instructions can report out of
3180   vector memory order if they access LDS memory, and out of LDS operation order
3181   if they access global memory.
3182 * The vector memory operations access a single vector L1 cache shared by all
3183   SIMDs a CU. Therefore, no special action is required for coherence between the
3184   lanes of a single wavefront, or for coherence between wavefronts in the same
3185   work-group. A ``buffer_wbinvl1_vol`` is required for coherence between wavefronts
3186   executing in different work-groups as they may be executing on different CUs.
3187 * The scalar memory operations access a scalar L1 cache shared by all wavefronts
3188   on a group of CUs. The scalar and vector L1 caches are not coherent. However,
3189   scalar operations are used in a restricted way so do not impact the memory
3190   model. See :ref:`amdgpu-amdhsa-memory-spaces`.
3191 * The vector and scalar memory operations use an L2 cache shared by all CUs on
3192   the same agent.
3193 * The L2 cache has independent channels to service disjoint ranges of virtual
3194   addresses.
3195 * Each CU has a separate request queue per channel. Therefore, the vector and
3196   scalar memory operations performed by wavefronts executing in different work-groups
3197   (which may be executing on different CUs) of an agent can be reordered
3198   relative to each other. A ``s_waitcnt vmcnt(0)`` is required to ensure
3199   synchronization between vector memory operations of different CUs. It ensures a
3200   previous vector memory operation has completed before executing a subsequent
3201   vector memory or LDS operation and so can be used to meet the requirements of
3202   acquire and release.
3203 * The L2 cache can be kept coherent with other agents on some targets, or ranges
3204   of virtual addresses can be set up to bypass it to ensure system coherence.
3205
3206 For GFX10:
3207
3208 * Each agent has multiple shader arrays (SA).
3209 * Each SA has multiple work-group processors (WGP).
3210 * Each WGP has multiple compute units (CU).
3211 * Each CU has multiple SIMDs that execute wavefronts.
3212 * The wavefronts for a single work-group are executed in the same
3213   WGP. In CU wavefront execution mode the wavefronts may be executed by
3214   different SIMDs in the same CU. In WGP wavefront execution mode the
3215   wavefronts may be executed by different SIMDs in different CUs in the same
3216   WGP.
3217 * Each WGP has a single LDS memory shared by the wavefronts of the work-groups
3218   executing on it.
3219 * All LDS operations of a WGP are performed as wavefront wide operations in a
3220   global order and involve no caching. Completion is reported to a wavefront in
3221   execution order.
3222 * The LDS memory has multiple request queues shared by the SIMDs of a
3223   WGP. Therefore, the LDS operations performed by different wavefronts of a work-group
3224   can be reordered relative to each other, which can result in reordering the
3225   visibility of vector memory operations with respect to LDS operations of other
3226   wavefronts in the same work-group. A ``s_waitcnt lgkmcnt(0)`` is required to
3227   ensure synchronization between LDS operations and vector memory operations
3228   between wavefronts of a work-group, but not between operations performed by the
3229   same wavefront.
3230 * The vector memory operations are performed as wavefront wide operations.
3231   Completion of load/store/sample operations are reported to a wavefront in
3232   execution order of other load/store/sample operations performed by that
3233   wavefront.
3234 * The vector memory operations access a vector L0 cache. There is a single L0
3235   cache per CU. Each SIMD of a CU accesses the same L0 cache.
3236   Therefore, no special action is required for coherence between the lanes of a
3237   single wavefront. However, a ``BUFFER_GL0_INV`` is required for coherence
3238   between wavefronts executing in the same work-group as they may be executing on
3239   SIMDs of different CUs that access different L0s. A ``BUFFER_GL0_INV`` is also
3240   required for coherence between wavefronts executing in different work-groups as
3241   they may be executing on different WGPs.
3242 * The scalar memory operations access a scalar L0 cache shared by all wavefronts
3243   on a WGP. The scalar and vector L0 caches are not coherent. However, scalar
3244   operations are used in a restricted way so do not impact the memory model. See
3245   :ref:`amdgpu-amdhsa-memory-spaces`.
3246 * The vector and scalar memory L0 caches use an L1 cache shared by all WGPs on
3247   the same SA. Therefore, no special action is required for coherence between
3248   the wavefronts of a single work-group. However, a ``BUFFER_GL1_INV`` is
3249   required for coherence between wavefronts executing in different work-groups as
3250   they may be executing on different SAs that access different L1s.
3251 * The L1 caches have independent quadrants to service disjoint ranges of virtual
3252   addresses.
3253 * Each L0 cache has a separate request queue per L1 quadrant. Therefore, the
3254   vector and scalar memory operations performed by different wavefronts, whether
3255   executing in the same or different work-groups (which may be executing on
3256   different CUs accessing different L0s), can be reordered relative to each
3257   other. A ``s_waitcnt vmcnt(0) & vscnt(0)`` is required to ensure synchronization
3258   between vector memory operations of different wavefronts. It ensures a previous
3259   vector memory operation has completed before executing a subsequent vector
3260   memory or LDS operation and so can be used to meet the requirements of acquire,
3261   release and sequential consistency.
3262 * The L1 caches use an L2 cache shared by all SAs on the same agent.
3263 * The L2 cache has independent channels to service disjoint ranges of virtual
3264   addresses.
3265 * Each L1 quadrant of a single SA accesses a different L2 channel. Each L1
3266   quadrant has a separate request queue per L2 channel. Therefore, the vector
3267   and scalar memory operations performed by wavefronts executing in different
3268   work-groups (which may be executing on different SAs) of an agent can be
3269   reordered relative to each other. A ``s_waitcnt vmcnt(0) & vscnt(0)`` is
3270   required to ensure synchronization between vector memory operations of
3271   different SAs. It ensures a previous vector memory operation has completed
3272   before executing a subsequent vector memory and so can be used to meet the
3273   requirements of acquire, release and sequential consistency.
3274 * The L2 cache can be kept coherent with other agents on some targets, or ranges
3275   of virtual addresses can be set up to bypass it to ensure system coherence.
3276
3277 Private address space uses ``buffer_load/store`` using the scratch V# (GFX6-GFX8),
3278 or ``scratch_load/store`` (GFX9-GFX10). Since only a single thread is accessing the
3279 memory, atomic memory orderings are not meaningful and all accesses are treated
3280 as non-atomic.
3281
3282 Constant address space uses ``buffer/global_load`` instructions (or equivalent
3283 scalar memory instructions). Since the constant address space contents do not
3284 change during the execution of a kernel dispatch it is not legal to perform
3285 stores, and atomic memory orderings are not meaningful and all access are
3286 treated as non-atomic.
3287
3288 A memory synchronization scope wider than work-group is not meaningful for the
3289 group (LDS) address space and is treated as work-group.
3290
3291 The memory model does not support the region address space which is treated as
3292 non-atomic.
3293
3294 Acquire memory ordering is not meaningful on store atomic instructions and is
3295 treated as non-atomic.
3296
3297 Release memory ordering is not meaningful on load atomic instructions and is
3298 treated a non-atomic.
3299
3300 Acquire-release memory ordering is not meaningful on load or store atomic
3301 instructions and is treated as acquire and release respectively.
3302
3303 AMDGPU backend only uses scalar memory operations to access memory that is
3304 proven to not change during the execution of the kernel dispatch. This includes
3305 constant address space and global address space for program scope const
3306 variables. Therefore the kernel machine code does not have to maintain the
3307 scalar L1 cache to ensure it is coherent with the vector L1 cache. The scalar
3308 and vector L1 caches are invalidated between kernel dispatches by CP since
3309 constant address space data may change between kernel dispatch executions. See
3310 :ref:`amdgpu-amdhsa-memory-spaces`.
3311
3312 The one execption is if scalar writes are used to spill SGPR registers. In this
3313 case the AMDGPU backend ensures the memory location used to spill is never
3314 accessed by vector memory operations at the same time. If scalar writes are used
3315 then a ``s_dcache_wb`` is inserted before the ``s_endpgm`` and before a function
3316 return since the locations may be used for vector memory instructions by a
3317 future wavefront that uses the same scratch area, or a function call that creates a
3318 frame at the same address, respectively. There is no need for a ``s_dcache_inv``
3319 as all scalar writes are write-before-read in the same thread.
3320
3321 For GFX6-GFX9, scratch backing memory (which is used for the private address space)
3322 is accessed with MTYPE NC_NV (non-coherenent non-volatile). Since the private
3323 address space is only accessed by a single thread, and is always
3324 write-before-read, there is never a need to invalidate these entries from the L1
3325 cache. Hence all cache invalidates are done as ``*_vol`` to only invalidate the
3326 volatile cache lines.
3327
3328 For GFX10, scratch backing memory (which is used for the private address space)
3329 is accessed with MTYPE NC (non-coherenent). Since the private address space is
3330 only accessed by a single thread, and is always write-before-read, there is
3331 never a need to invalidate these entries from the L0 or L1 caches.
3332
3333 For GFX10, wavefronts are executed in native mode with in-order reporting of loads
3334 and sample instructions. In this mode vmcnt reports completion of load, atomic
3335 with return and sample instructions in order, and the vscnt reports the
3336 completion of store and atomic without return in order. See ``MEM_ORDERED`` field
3337 in :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
3338
3339 In GFX10, wavefronts can be executed in WGP or CU wavefront execution mode:
3340
3341 * In WGP wavefront execution mode the wavefronts of a work-group are executed
3342   on the SIMDs of both CUs of the WGP. Therefore, explicit management of the per
3343   CU L0 caches is required for work-group synchronization. Also accesses to L1 at
3344   work-group scope need to be expicitly ordered as the accesses from different
3345   CUs are not ordered.
3346 * In CU wavefront execution mode the wavefronts of a work-group are executed on
3347   the SIMDs of a single CU of the WGP. Therefore, all global memory access by
3348   the work-group access the same L0 which in turn ensures L1 accesses are
3349   ordered and so do not require explicit management of the caches for
3350   work-group synchronization.
3351
3352 See ``WGP_MODE`` field in :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`
3353 and :ref:`amdgpu-target-features`.
3354
3355 On dGPU the kernarg backing memory is accessed as UC (uncached) to avoid needing
3356 to invalidate the L2 cache. For GFX6-GFX9, this also causes it to be treated as
3357 non-volatile and so is not invalidated by ``*_vol``. On APU it is accessed as CC
3358 (cache coherent) and so the L2 cache will be coherent with the CPU and other
3359 agents.
3360
3361   .. table:: AMDHSA Memory Model Code Sequences GFX6-GFX10
3362      :name: amdgpu-amdhsa-memory-model-code-sequences-gfx6-gfx10-table
3363
3364      ============ ============ ============== ========== =============================== ==================================
3365      LLVM Instr   LLVM Memory  LLVM Memory    AMDGPU     AMDGPU Machine Code             AMDGPU Machine Code
3366                   Ordering     Sync Scope     Address    GFX6-9                          GFX10
3367                                               Space
3368      ============ ============ ============== ========== =============================== ==================================
3369      **Non-Atomic**
3370      ----------------------------------------------------------------------------------------------------------------------
3371      load         *none*       *none*         - global   - !volatile & !nontemporal      - !volatile & !nontemporal
3372                                               - generic
3373                                               - private    1. buffer/global/flat_load      1. buffer/global/flat_load
3374                                               - constant
3375                                                          - volatile & !nontemporal       - volatile & !nontemporal
3376
3377                                                            1. buffer/global/flat_load      1. buffer/global/flat_load
3378                                                               glc=1                           glc=1 dlc=1
3379
3380                                                          - nontemporal                   - nontemporal
3381
3382                                                            1. buffer/global/flat_load      1. buffer/global/flat_load
3383                                                               glc=1 slc=1                     slc=1
3384
3385      load         *none*       *none*         - local    1. ds_load                      1. ds_load
3386      store        *none*       *none*         - global   - !nontemporal                  - !nontemporal
3387                                               - generic
3388                                               - private    1. buffer/global/flat_store     1. buffer/global/flat_store
3389                                               - constant
3390                                                          - nontemporal                   - nontemporal
3391
3392                                                            1. buffer/global/flat_stote      1. buffer/global/flat_store
3393                                                               glc=1 slc=1                      slc=1
3394
3395      store        *none*       *none*         - local    1. ds_store                     1. ds_store
3396      **Unordered Atomic**
3397      ----------------------------------------------------------------------------------------------------------------------
3398      load atomic  unordered    *any*          *any*      *Same as non-atomic*.           *Same as non-atomic*.
3399      store atomic unordered    *any*          *any*      *Same as non-atomic*.           *Same as non-atomic*.
3400      atomicrmw    unordered    *any*          *any*      *Same as monotonic              *Same as monotonic
3401                                                          atomic*.                        atomic*.
3402      **Monotonic Atomic**
3403      ----------------------------------------------------------------------------------------------------------------------
3404      load atomic  monotonic    - singlethread - global   1. buffer/global/flat_load      1. buffer/global/flat_load
3405                                - wavefront    - generic
3406      load atomic  monotonic    - workgroup    - global   1. buffer/global/flat_load      1. buffer/global/flat_load
3407                                               - generic                                     glc=1
3408
3409                                                                                            - If CU wavefront execution mode, omit glc=1.
3410
3411      load atomic  monotonic    - singlethread - local    1. ds_load                      1. ds_load
3412                                - wavefront
3413                                - workgroup
3414      load atomic  monotonic    - agent        - global   1. buffer/global/flat_load      1. buffer/global/flat_load
3415                                - system       - generic     glc=1                           glc=1 dlc=1
3416      store atomic monotonic    - singlethread - global   1. buffer/global/flat_store     1. buffer/global/flat_store
3417                                - wavefront    - generic
3418                                - workgroup
3419                                - agent
3420                                - system
3421      store atomic monotonic    - singlethread - local    1. ds_store                     1. ds_store
3422                                - wavefront
3423                                - workgroup
3424      atomicrmw    monotonic    - singlethread - global   1. buffer/global/flat_atomic    1. buffer/global/flat_atomic
3425                                - wavefront    - generic
3426                                - workgroup
3427                                - agent
3428                                - system
3429      atomicrmw    monotonic    - singlethread - local    1. ds_atomic                    1. ds_atomic
3430                                - wavefront
3431                                - workgroup
3432      **Acquire Atomic**
3433      ----------------------------------------------------------------------------------------------------------------------
3434      load atomic  acquire      - singlethread - global   1. buffer/global/ds/flat_load   1. buffer/global/ds/flat_load
3435                                - wavefront    - local
3436                                               - generic
3437      load atomic  acquire      - workgroup    - global   1. buffer/global/flat_load      1. buffer/global_load glc=1
3438
3439                                                                                            - If CU wavefront execution mode, omit glc=1.
3440
3441                                                                                          2. s_waitcnt vmcnt(0)
3442
3443                                                                                            - If CU wavefront execution mode, omit.
3444                                                                                            - Must happen before
3445                                                                                              the following buffer_gl0_inv
3446                                                                                              and before any following
3447                                                                                              global/generic
3448                                                                                              load/load
3449                                                                                              atomic/stote/store
3450                                                                                              atomic/atomicrmw.
3451
3452                                                                                          3. buffer_gl0_inv
3453
3454                                                                                            - If CU wavefront execution mode, omit.
3455                                                                                            - Ensures that
3456                                                                                              following
3457                                                                                              loads will not see
3458                                                                                              stale data.
3459
3460      load atomic  acquire      - workgroup    - local    1. ds_load                      1. ds_load
3461                                                          2. s_waitcnt lgkmcnt(0)         2. s_waitcnt lgkmcnt(0)
3462
3463                                                            - If OpenCL, omit.              - If OpenCL, omit.
3464                                                            - Must happen before            - Must happen before
3465                                                              any following                   the following buffer_gl0_inv
3466                                                              global/generic                  and before any following
3467                                                              load/load                       global/generic load/load
3468                                                              atomic/store/store              atomic/store/store
3469                                                              atomic/atomicrmw.               atomic/atomicrmw.
3470                                                            - Ensures any                   - Ensures any
3471                                                              following global                following global
3472                                                              data read is no                 data read is no
3473                                                              older than the load             older than the load
3474                                                              atomic value being              atomic value being
3475                                                              acquired.                       acquired.
3476
3477                                                                                          3. buffer_gl0_inv
3478
3479                                                                                            - If CU wavefront execution mode, omit.
3480                                                                                            - If OpenCL, omit.
3481                                                                                            - Ensures that
3482                                                                                              following
3483                                                                                              loads will not see
3484                                                                                              stale data.
3485
3486      load atomic  acquire      - workgroup    - generic  1. flat_load                    1. flat_load glc=1
3487
3488                                                                                            - If CU wavefront execution mode, omit glc=1.
3489
3490                                                          2. s_waitcnt lgkmcnt(0)         2. s_waitcnt lgkmcnt(0) &
3491                                                                                             vmcnt(0)
3492
3493                                                                                            - If CU wavefront execution mode, omit vmcnt.
3494                                                            - If OpenCL, omit.              - If OpenCL, omit
3495                                                                                              lgkmcnt(0).
3496                                                            - Must happen before            - Must happen before
3497                                                              any following                   the following
3498                                                              global/generic                  buffer_gl0_inv and any
3499                                                              load/load                       following global/generic
3500                                                              atomic/store/store              load/load
3501                                                              atomic/atomicrmw.               atomic/store/store
3502                                                                                              atomic/atomicrmw.
3503                                                            - Ensures any                   - Ensures any
3504                                                              following global                following global
3505                                                              data read is no                 data read is no
3506                                                              older than the load             older than the load
3507                                                              atomic value being              atomic value being
3508                                                              acquired.                       acquired.
3509
3510                                                                                          3. buffer_gl0_inv
3511
3512                                                                                            - If CU wavefront execution mode, omit.
3513                                                                                            - Ensures that
3514                                                                                              following
3515                                                                                              loads will not see
3516                                                                                              stale data.
3517
3518      load atomic  acquire      - agent        - global   1. buffer/global/flat_load      1. buffer/global_load
3519                                - system                     glc=1                           glc=1 dlc=1
3520                                                          2. s_waitcnt vmcnt(0)           2. s_waitcnt vmcnt(0)
3521
3522                                                            - Must happen before            - Must happen before
3523                                                              following                       following
3524                                                              buffer_wbinvl1_vol.             buffer_gl*_inv.
3525                                                            - Ensures the load              - Ensures the load
3526                                                              has completed                   has completed
3527                                                              before invalidating             before invalidating
3528                                                              the cache.                      the caches.
3529
3530                                                          3. buffer_wbinvl1_vol           3. buffer_gl0_inv;
3531                                                                                             buffer_gl1_inv
3532
3533                                                            - Must happen before            - Must happen before
3534                                                              any following                   any following
3535                                                              global/generic                  global/generic
3536                                                              load/load                       load/load
3537                                                              atomic/atomicrmw.               atomic/atomicrmw.
3538                                                            - Ensures that                  - Ensures that
3539                                                              following                       following
3540                                                              loads will not see              loads will not see
3541                                                              stale global data.              stale global data.
3542
3543      load atomic  acquire      - agent        - generic  1. flat_load glc=1              1. flat_load glc=1 dlc=1
3544                                - system                  2. s_waitcnt vmcnt(0) &         2. s_waitcnt vmcnt(0) &
3545                                                             lgkmcnt(0)                      lgkmcnt(0)
3546
3547                                                            - If OpenCL omit                - If OpenCL omit
3548                                                              lgkmcnt(0).                     lgkmcnt(0).
3549                                                            - Must happen before            - Must happen before
3550                                                              following                       following
3551                                                              buffer_wbinvl1_vol.             buffer_gl*_invl.
3552                                                            - Ensures the flat_load         - Ensures the flat_load
3553                                                              has completed                   has completed
3554                                                              before invalidating             before invalidating
3555                                                              the cache.                      the caches.
3556
3557                                                          3. buffer_wbinvl1_vol           3. buffer_gl0_inv;
3558                                                                                             buffer_gl1_inv
3559
3560                                                            - Must happen before            - Must happen before
3561                                                              any following                   any following
3562                                                              global/generic                  global/generic
3563                                                              load/load                       load/load
3564                                                              atomic/atomicrmw.               atomic/atomicrmw.
3565                                                            - Ensures that                  - Ensures that
3566                                                              following loads                 following loads
3567                                                              will not see stale              will not see stale
3568                                                              global data.                    global data.
3569
3570      atomicrmw    acquire      - singlethread - global   1. buffer/global/ds/flat_atomic 1. buffer/global/ds/flat_atomic
3571                                - wavefront    - local
3572                                               - generic
3573      atomicrmw    acquire      - workgroup    - global   1. buffer/global/flat_atomic    1. buffer/global_atomic
3574                                                                                          2. s_waitcnt vm/vscnt(0)
3575
3576                                                                                            - If CU wavefront execution mode, omit.
3577                                                                                            - Use vmcnt if atomic with
3578                                                                                              return and vscnt if atomic
3579                                                                                              with no-return.
3580                                                                                            - Must happen before
3581                                                                                              the following buffer_gl0_inv
3582                                                                                              and before any following
3583                                                                                              global/generic
3584                                                                                              load/load
3585                                                                                              atomic/stote/store
3586                                                                                              atomic/atomicrmw.
3587
3588                                                                                          3. buffer_gl0_inv
3589
3590                                                                                            - If CU wavefront execution mode, omit.
3591                                                                                            - Ensures that
3592                                                                                              following
3593                                                                                              loads will not see
3594                                                                                              stale data.
3595
3596      atomicrmw    acquire      - workgroup    - local    1. ds_atomic                    1. ds_atomic
3597                                                          2. waitcnt lgkmcnt(0)           2. waitcnt lgkmcnt(0)
3598
3599                                                            - If OpenCL, omit.              - If OpenCL, omit.
3600                                                            - Must happen before            - Must happen before
3601                                                              any following                   the following
3602                                                              global/generic                  buffer_gl0_inv.
3603                                                              load/load
3604                                                              atomic/store/store
3605                                                              atomic/atomicrmw.
3606                                                            - Ensures any                   - Ensures any
3607                                                              following global                following global
3608                                                              data read is no                 data read is no
3609                                                              older than the                  older than the
3610                                                              atomicrmw value                 atomicrmw value
3611                                                              being acquired.                 being acquired.
3612
3613                                                                                          3. buffer_gl0_inv
3614
3615                                                                                            - If OpenCL omit.
3616                                                                                            - Ensures that
3617                                                                                              following
3618                                                                                              loads will not see
3619                                                                                              stale data.
3620
3621      atomicrmw    acquire      - workgroup    - generic  1. flat_atomic                  1. flat_atomic
3622                                                          2. waitcnt lgkmcnt(0)           2. waitcnt lgkmcnt(0) &
3623                                                                                             vm/vscnt(0)
3624
3625                                                                                            - If CU wavefront execution mode, omit vm/vscnt.
3626                                                            - If OpenCL, omit.              - If OpenCL, omit
3627                                                                                              waitcnt lgkmcnt(0)..
3628                                                                                            - Use vmcnt if atomic with
3629                                                                                              return and vscnt if atomic
3630                                                                                              with no-return.
3631                                                                                              waitcnt lgkmcnt(0).
3632                                                            - Must happen before            - Must happen before
3633                                                              any following                   the following
3634                                                              global/generic                  buffer_gl0_inv.
3635                                                              load/load
3636                                                              atomic/store/store
3637                                                              atomic/atomicrmw.
3638                                                            - Ensures any                   - Ensures any
3639                                                              following global                following global
3640                                                              data read is no                 data read is no
3641                                                              older than the                  older than the
3642                                                              atomicrmw value                 atomicrmw value
3643                                                              being acquired.                 being acquired.
3644
3645                                                                                          3. buffer_gl0_inv
3646
3647                                                                                            - If CU wavefront execution mode, omit.
3648                                                                                            - Ensures that
3649                                                                                              following
3650                                                                                              loads will not see
3651                                                                                              stale data.
3652
3653      atomicrmw    acquire      - agent        - global   1. buffer/global/flat_atomic    1. buffer/global_atomic
3654                                - system                  2. s_waitcnt vmcnt(0)           2. s_waitcnt vm/vscnt(0)
3655
3656                                                                                            - Use vmcnt if atomic with
3657                                                                                              return and vscnt if atomic
3658                                                                                              with no-return.
3659                                                                                              waitcnt lgkmcnt(0).
3660                                                            - Must happen before            - Must happen before
3661                                                              following                       following
3662                                                              buffer_wbinvl1_vol.             buffer_gl*_inv.
3663                                                            - Ensures the                   - Ensures the
3664                                                              atomicrmw has                   atomicrmw has
3665                                                              completed before                completed before
3666                                                              invalidating the                invalidating the
3667                                                              cache.                          caches.
3668
3669                                                          3. buffer_wbinvl1_vol           3. buffer_gl0_inv;
3670                                                                                             buffer_gl1_inv
3671
3672                                                            - Must happen before            - Must happen before
3673                                                              any following                   any following
3674                                                              global/generic                  global/generic
3675                                                              load/load                       load/load
3676                                                              atomic/atomicrmw.               atomic/atomicrmw.
3677                                                            - Ensures that                  - Ensures that
3678                                                              following loads                 following loads
3679                                                              will not see stale              will not see stale
3680                                                              global data.                    global data.
3681
3682      atomicrmw    acquire      - agent        - generic  1. flat_atomic                  1. flat_atomic
3683                                - system                  2. s_waitcnt vmcnt(0) &         2. s_waitcnt vm/vscnt(0) &
3684                                                             lgkmcnt(0)                      lgkmcnt(0)
3685
3686                                                            - If OpenCL, omit               - If OpenCL, omit
3687                                                              lgkmcnt(0).                     lgkmcnt(0).
3688                                                                                            - Use vmcnt if atomic with
3689                                                                                              return and vscnt if atomic
3690                                                                                              with no-return.
3691                                                            - Must happen before            - Must happen before
3692                                                              following                       following
3693                                                              buffer_wbinvl1_vol.             buffer_gl*_inv.
3694                                                            - Ensures the                   - Ensures the
3695                                                              atomicrmw has                   atomicrmw has
3696                                                              completed before                completed before
3697                                                              invalidating the                invalidating the
3698                                                              cache.                          caches.
3699
3700                                                          3. buffer_wbinvl1_vol           3. buffer_gl0_inv;
3701                                                                                             buffer_gl1_inv
3702
3703                                                            - Must happen before            - Must happen before
3704                                                              any following                   any following
3705                                                              global/generic                  global/generic
3706                                                              load/load                       load/load
3707                                                              atomic/atomicrmw.               atomic/atomicrmw.
3708                                                            - Ensures that                  - Ensures that
3709                                                              following loads                 following loads
3710                                                              will not see stale              will not see stale
3711                                                              global data.                    global data.
3712
3713      fence        acquire      - singlethread *none*     *none*                          *none*
3714                                - wavefront
3715      fence        acquire      - workgroup    *none*     1. s_waitcnt lgkmcnt(0)         1. s_waitcnt lgkmcnt(0) &
3716                                                                                             vmcnt(0) & vscnt(0)
3717
3718                                                                                            - If CU wavefront execution mode, omit vmcnt and
3719                                                                                              vscnt.
3720                                                            - If OpenCL and                 - If OpenCL and
3721                                                              address space is                address space is
3722                                                              not generic, omit.              not generic, omit
3723                                                                                              lgkmcnt(0).
3724                                                                                            - If OpenCL and
3725                                                                                              address space is
3726                                                                                              local, omit
3727                                                                                              vmcnt(0) and vscnt(0).
3728                                                            - However, since LLVM           - However, since LLVM
3729                                                              currently has no                currently has no
3730                                                              address space on                address space on
3731                                                              the fence need to               the fence need to
3732                                                              conservatively                  conservatively
3733                                                              always generate. If             always generate. If
3734                                                              fence had an                    fence had an
3735                                                              address space then              address space then
3736                                                              set to address                  set to address
3737                                                              space of OpenCL                 space of OpenCL
3738                                                              fence flag, or to               fence flag, or to
3739                                                              generic if both                 generic if both
3740                                                              local and global                local and global
3741                                                              flags are                       flags are
3742                                                              specified.                      specified.
3743                                                            - Must happen after
3744                                                              any preceding
3745                                                              local/generic load
3746                                                              atomic/atomicrmw
3747                                                              with an equal or
3748                                                              wider sync scope
3749                                                              and memory ordering
3750                                                              stronger than
3751                                                              unordered (this is
3752                                                              termed the
3753                                                              fence-paired-atomic).
3754                                                            - Must happen before
3755                                                              any following
3756                                                              global/generic
3757                                                              load/load
3758                                                              atomic/store/store
3759                                                              atomic/atomicrmw.
3760                                                            - Ensures any
3761                                                              following global
3762                                                              data read is no
3763                                                              older than the
3764                                                              value read by the
3765                                                              fence-paired-atomic.
3766                                                                                            - Could be split into
3767                                                                                              separate s_waitcnt
3768                                                                                              vmcnt(0), s_waitcnt
3769                                                                                              vscnt(0) and s_waitcnt
3770                                                                                              lgkmcnt(0) to allow
3771                                                                                              them to be
3772                                                                                              independently moved
3773                                                                                              according to the
3774                                                                                              following rules.
3775                                                                                            - s_waitcnt vmcnt(0)
3776                                                                                              must happen after
3777                                                                                              any preceding
3778                                                                                              global/generic load
3779                                                                                              atomic/
3780                                                                                              atomicrmw-with-return-value
3781                                                                                              with an equal or
3782                                                                                              wider sync scope
3783                                                                                              and memory ordering
3784                                                                                              stronger than
3785                                                                                              unordered (this is
3786                                                                                              termed the
3787                                                                                              fence-paired-atomic).
3788                                                                                            - s_waitcnt vscnt(0)
3789                                                                                              must happen after
3790                                                                                              any preceding
3791                                                                                              global/generic
3792                                                                                              atomicrmw-no-return-value
3793                                                                                              with an equal or
3794                                                                                              wider sync scope
3795                                                                                              and memory ordering
3796                                                                                              stronger than
3797                                                                                              unordered (this is
3798                                                                                              termed the
3799                                                                                              fence-paired-atomic).
3800                                                                                            - s_waitcnt lgkmcnt(0)
3801                                                                                              must happen after
3802                                                                                              any preceding
3803                                                                                              local/generic load
3804                                                                                              atomic/atomicrmw
3805                                                                                              with an equal or
3806                                                                                              wider sync scope
3807                                                                                              and memory ordering
3808                                                                                              stronger than
3809                                                                                              unordered (this is
3810                                                                                              termed the
3811                                                                                              fence-paired-atomic).
3812                                                                                            - Must happen before
3813                                                                                              the following
3814                                                                                              buffer_gl0_inv.
3815                                                                                            - Ensures that the
3816                                                                                              fence-paired atomic
3817                                                                                              has completed
3818                                                                                              before invalidating
3819                                                                                              the
3820                                                                                              cache. Therefore
3821                                                                                              any following
3822                                                                                              locations read must
3823                                                                                              be no older than
3824                                                                                              the value read by
3825                                                                                              the
3826                                                                                              fence-paired-atomic.
3827
3828                                                                                          3. buffer_gl0_inv
3829
3830                                                                                            - If CU wavefront execution mode, omit.
3831                                                                                            - Ensures that
3832                                                                                              following
3833                                                                                              loads will not see
3834                                                                                              stale data.
3835
3836      fence        acquire      - agent        *none*     1. s_waitcnt lgkmcnt(0) &       1. s_waitcnt lgkmcnt(0) &
3837                                - system                     vmcnt(0)                        vmcnt(0) & vscnt(0)
3838
3839                                                            - If OpenCL and                 - If OpenCL and
3840                                                              address space is                address space is
3841                                                              not generic, omit               not generic, omit
3842                                                              lgkmcnt(0).                     lgkmcnt(0).
3843                                                                                            - If OpenCL and
3844                                                                                              address space is
3845                                                                                              local, omit
3846                                                                                              vmcnt(0) and vscnt(0).
3847                                                            - However, since LLVM           - However, since LLVM
3848                                                              currently has no                currently has no
3849                                                              address space on                address space on
3850                                                              the fence need to               the fence need to
3851                                                              conservatively                  conservatively
3852                                                              always generate                 always generate
3853                                                              (see comment for                (see comment for
3854                                                              previous fence).                previous fence).
3855                                                            - Could be split into
3856                                                              separate s_waitcnt
3857                                                              vmcnt(0) and
3858                                                              s_waitcnt
3859                                                              lgkmcnt(0) to allow
3860                                                              them to be
3861                                                              independently moved
3862                                                              according to the
3863                                                              following rules.
3864                                                            - s_waitcnt vmcnt(0)
3865                                                              must happen after
3866                                                              any preceding
3867                                                              global/generic load
3868                                                              atomic/atomicrmw
3869                                                              with an equal or
3870                                                              wider sync scope
3871                                                              and memory ordering
3872                                                              stronger than
3873                                                              unordered (this is
3874                                                              termed the
3875                                                              fence-paired-atomic).
3876                                                            - s_waitcnt lgkmcnt(0)
3877                                                              must happen after
3878                                                              any preceding
3879                                                              local/generic load
3880                                                              atomic/atomicrmw
3881                                                              with an equal or
3882                                                              wider sync scope
3883                                                              and memory ordering
3884                                                              stronger than
3885                                                              unordered (this is
3886                                                              termed the
3887                                                              fence-paired-atomic).
3888                                                            - Must happen before
3889                                                              the following
3890                                                              buffer_wbinvl1_vol.
3891                                                            - Ensures that the
3892                                                              fence-paired atomic
3893                                                              has completed
3894                                                              before invalidating
3895                                                              the
3896                                                              cache. Therefore
3897                                                              any following
3898                                                              locations read must
3899                                                              be no older than
3900                                                              the value read by
3901                                                              the
3902                                                              fence-paired-atomic.
3903                                                                                            - Could be split into
3904                                                                                              separate s_waitcnt
3905                                                                                              vmcnt(0), s_waitcnt
3906                                                                                              vscnt(0) and s_waitcnt
3907                                                                                              lgkmcnt(0) to allow
3908                                                                                              them to be
3909                                                                                              independently moved
3910                                                                                              according to the
3911                                                                                              following rules.
3912                                                                                            - s_waitcnt vmcnt(0)
3913                                                                                              must happen after
3914                                                                                              any preceding
3915                                                                                              global/generic load
3916                                                                                              atomic/
3917                                                                                              atomicrmw-with-return-value
3918                                                                                              with an equal or
3919                                                                                              wider sync scope
3920                                                                                              and memory ordering
3921                                                                                              stronger than
3922                                                                                              unordered (this is
3923                                                                                              termed the
3924                                                                                              fence-paired-atomic).
3925                                                                                            - s_waitcnt vscnt(0)
3926                                                                                              must happen after
3927                                                                                              any preceding
3928                                                                                              global/generic
3929                                                                                              atomicrmw-no-return-value
3930                                                                                              with an equal or
3931                                                                                              wider sync scope
3932                                                                                              and memory ordering
3933                                                                                              stronger than
3934                                                                                              unordered (this is
3935                                                                                              termed the
3936                                                                                              fence-paired-atomic).
3937                                                                                            - s_waitcnt lgkmcnt(0)
3938                                                                                              must happen after
3939                                                                                              any preceding
3940                                                                                              local/generic load
3941                                                                                              atomic/atomicrmw
3942                                                                                              with an equal or
3943                                                                                              wider sync scope
3944                                                                                              and memory ordering
3945                                                                                              stronger than
3946                                                                                              unordered (this is
3947                                                                                              termed the
3948                                                                                              fence-paired-atomic).
3949                                                                                            - Must happen before
3950                                                                                              the following
3951                                                                                              buffer_gl*_inv.
3952                                                                                            - Ensures that the
3953                                                                                              fence-paired atomic
3954                                                                                              has completed
3955                                                                                              before invalidating
3956                                                                                              the
3957                                                                                              caches. Therefore
3958                                                                                              any following
3959                                                                                              locations read must
3960                                                                                              be no older than
3961                                                                                              the value read by
3962                                                                                              the
3963                                                                                              fence-paired-atomic.
3964
3965                                                          2. buffer_wbinvl1_vol           2. buffer_gl0_inv;
3966                                                                                             buffer_gl1_inv
3967
3968                                                            - Must happen before any        - Must happen before any
3969                                                              following global/generic        following global/generic
3970                                                              load/load                       load/load
3971                                                              atomic/store/store              atomic/store/store
3972                                                              atomic/atomicrmw.               atomic/atomicrmw.
3973                                                            - Ensures that                  - Ensures that
3974                                                              following loads                 following loads
3975                                                              will not see stale              will not see stale
3976                                                              global data.                    global data.
3977
3978      **Release Atomic**
3979      ----------------------------------------------------------------------------------------------------------------------
3980      store atomic release      - singlethread - global   1. buffer/global/ds/flat_store  1. buffer/global/ds/flat_store
3981                                - wavefront    - local
3982                                               - generic
3983      store atomic release      - workgroup    - global   1. s_waitcnt lgkmcnt(0)         1. s_waitcnt lgkmcnt(0) &
3984                                                                                             vmcnt(0) & vscnt(0)
3985
3986                                                                                            - If CU wavefront execution mode, omit vmcnt and
3987                                                                                              vscnt.
3988                                                            - If OpenCL, omit.              - If OpenCL, omit
3989                                                                                              lgkmcnt(0).
3990                                                            - Must happen after
3991                                                              any preceding
3992                                                              local/generic
3993                                                              load/store/load
3994                                                              atomic/store
3995                                                              atomic/atomicrmw.
3996                                                                                            - Could be split into
3997                                                                                              separate s_waitcnt
3998                                                                                              vmcnt(0), s_waitcnt
3999                                                                                              vscnt(0) and s_waitcnt
4000                                                                                              lgkmcnt(0) to allow
4001                                                                                              them to be
4002                                                                                              independently moved
4003                                                                                              according to the
4004                                                                                              following rules.
4005                                                                                            - s_waitcnt vmcnt(0)
4006                                                                                              must happen after
4007                                                                                              any preceding
4008                                                                                              global/generic load/load
4009                                                                                              atomic/
4010                                                                                              atomicrmw-with-return-value.
4011                                                                                            - s_waitcnt vscnt(0)
4012                                                                                              must happen after
4013                                                                                              any preceding
4014                                                                                              global/generic
4015                                                                                              store/store
4016                                                                                              atomic/
4017                                                                                              atomicrmw-no-return-value.
4018                                                                                            - s_waitcnt lgkmcnt(0)
4019                                                                                              must happen after
4020                                                                                              any preceding
4021                                                                                              local/generic
4022                                                                                              load/store/load
4023                                                                                              atomic/store
4024                                                                                              atomic/atomicrmw.
4025                                                            - Must happen before            - Must happen before
4026                                                              the following                   the following
4027                                                              store.                          store.
4028                                                            - Ensures that all              - Ensures that all
4029                                                              memory operations               memory operations
4030                                                              to local have                   have
4031                                                              completed before                completed before
4032                                                              performing the                  performing the
4033                                                              store that is being             store that is being
4034                                                              released.                       released.
4035
4036                                                          2. buffer/global/flat_store     2. buffer/global_store
4037      store atomic release      - workgroup    - local                                    1. waitcnt vmcnt(0) & vscnt(0)
4038
4039                                                                                            - If CU wavefront execution mode, omit.
4040                                                                                            - If OpenCL, omit.
4041                                                                                            - Could be split into
4042                                                                                              separate s_waitcnt
4043                                                                                              vmcnt(0) and s_waitcnt
4044                                                                                              vscnt(0) to allow
4045                                                                                              them to be
4046                                                                                              independently moved
4047                                                                                              according to the
4048                                                                                              following rules.
4049                                                                                            - s_waitcnt vmcnt(0)
4050                                                                                              must happen after
4051                                                                                              any preceding
4052                                                                                              global/generic load/load
4053                                                                                              atomic/
4054                                                                                              atomicrmw-with-return-value.
4055                                                                                            - s_waitcnt vscnt(0)
4056                                                                                              must happen after
4057                                                                                              any preceding
4058                                                                                              global/generic
4059                                                                                              store/store atomic/
4060                                                                                              atomicrmw-no-return-value.
4061                                                                                            - Must happen before
4062                                                                                              the following
4063                                                                                              store.
4064                                                                                            - Ensures that all
4065                                                                                              global memory
4066                                                                                              operations have
4067                                                                                              completed before
4068                                                                                              performing the
4069                                                                                              store that is being
4070                                                                                              released.
4071
4072                                                          1. ds_store                     2. ds_store
4073      store atomic release      - workgroup    - generic  1. s_waitcnt lgkmcnt(0)         1. s_waitcnt lgkmcnt(0) &
4074                                                                                             vmcnt(0) & vscnt(0)
4075
4076                                                                                            - If CU wavefront execution mode, omit vmcnt and
4077                                                                                              vscnt.
4078                                                            - If OpenCL, omit.              - If OpenCL, omit
4079                                                                                              lgkmcnt(0).
4080                                                            - Must happen after
4081                                                              any preceding
4082                                                              local/generic
4083                                                              load/store/load
4084                                                              atomic/store
4085                                                              atomic/atomicrmw.
4086                                                                                            - Could be split into
4087                                                                                              separate s_waitcnt
4088                                                                                              vmcnt(0), s_waitcnt
4089                                                                                              vscnt(0) and s_waitcnt
4090                                                                                              lgkmcnt(0) to allow
4091                                                                                              them to be
4092                                                                                              independently moved
4093                                                                                              according to the
4094                                                                                              following rules.
4095                                                                                            - s_waitcnt vmcnt(0)
4096                                                                                              must happen after
4097                                                                                              any preceding
4098                                                                                              global/generic load/load
4099                                                                                              atomic/
4100                                                                                              atomicrmw-with-return-value.
4101                                                                                            - s_waitcnt vscnt(0)
4102                                                                                              must happen after
4103                                                                                              any preceding
4104                                                                                              global/generic
4105                                                                                              store/store
4106                                                                                              atomic/
4107                                                                                              atomicrmw-no-return-value.
4108                                                                                            - s_waitcnt lgkmcnt(0)
4109                                                                                              must happen after
4110                                                                                              any preceding
4111                                                                                              local/generic load/store/load
4112                                                                                              atomic/store atomic/atomicrmw.
4113                                                            - Must happen before            - Must happen before
4114                                                              the following                   the following
4115                                                              store.                          store.
4116                                                            - Ensures that all              - Ensures that all
4117                                                              memory operations               memory operations
4118                                                              to local have                   have
4119                                                              completed before                completed before
4120                                                              performing the                  performing the
4121                                                              store that is being             store that is being
4122                                                              released.                       released.
4123
4124                                                          2. flat_store                   2. flat_store
4125      store atomic release      - agent        - global   1. s_waitcnt lgkmcnt(0) &         1. s_waitcnt lgkmcnt(0) &
4126                                - system       - generic     vmcnt(0)                          vmcnt(0) & vscnt(0)
4127
4128                                                            - If OpenCL, omit               - If OpenCL, omit
4129                                                              lgkmcnt(0).                     lgkmcnt(0).
4130                                                            - Could be split into           - Could be split into
4131                                                              separate s_waitcnt              separate s_waitcnt
4132                                                              vmcnt(0) and                    vmcnt(0), s_waitcnt vscnt(0)
4133                                                              s_waitcnt                       and s_waitcnt
4134                                                              lgkmcnt(0) to allow             lgkmcnt(0) to allow
4135                                                              them to be                      them to be
4136                                                              independently moved             independently moved
4137                                                              according to the                according to the
4138                                                              following rules.                following rules.
4139                                                            - s_waitcnt vmcnt(0)            - s_waitcnt vmcnt(0)
4140                                                              must happen after               must happen after
4141                                                              any preceding                   any preceding
4142                                                              global/generic                  global/generic
4143                                                              load/store/load                 load/load
4144                                                              atomic/store                    atomic/
4145                                                              atomic/atomicrmw.               atomicrmw-with-return-value.
4146                                                                                            - s_waitcnt vscnt(0)
4147                                                                                              must happen after
4148                                                                                              any preceding
4149                                                                                              global/generic
4150                                                                                              store/store atomic/
4151                                                                                              atomicrmw-no-return-value.
4152                                                            - s_waitcnt lgkmcnt(0)          - s_waitcnt lgkmcnt(0)
4153                                                              must happen after               must happen after
4154                                                              any preceding                   any preceding
4155                                                              local/generic                   local/generic
4156                                                              load/store/load                 load/store/load
4157                                                              atomic/store                    atomic/store
4158                                                              atomic/atomicrmw.               atomic/atomicrmw.
4159                                                            - Must happen before            - Must happen before
4160                                                              the following                   the following
4161                                                              store.                          store.
4162                                                            - Ensures that all              - Ensures that all
4163                                                              memory operations               memory operations
4164                                                              to memory have                  to memory have
4165                                                              completed before                completed before
4166                                                              performing the                  performing the
4167                                                              store that is being             store that is being
4168                                                              released.                       released.
4169
4170                                                          2. buffer/global/ds/flat_store  2. buffer/global/ds/flat_store
4171      atomicrmw    release      - singlethread - global   1. buffer/global/ds/flat_atomic 1. buffer/global/ds/flat_atomic
4172                                - wavefront    - local
4173                                               - generic
4174      atomicrmw    release      - workgroup    - global   1. s_waitcnt lgkmcnt(0)         1. s_waitcnt lgkmcnt(0) &
4175                                                                                             vmcnt(0) & vscnt(0)
4176
4177                                                                                            - If CU wavefront execution mode, omit vmcnt and
4178                                                                                              vscnt.
4179                                                            - If OpenCL, omit.
4180
4181                                                            - Must happen after
4182                                                              any preceding
4183                                                              local/generic
4184                                                              load/store/load
4185                                                              atomic/store
4186                                                              atomic/atomicrmw.
4187                                                                                            - Could be split into
4188                                                                                              separate s_waitcnt
4189                                                                                              vmcnt(0), s_waitcnt
4190                                                                                              vscnt(0) and s_waitcnt
4191                                                                                              lgkmcnt(0) to allow
4192                                                                                              them to be
4193                                                                                              independently moved
4194                                                                                              according to the
4195                                                                                              following rules.
4196                                                                                            - s_waitcnt vmcnt(0)
4197                                                                                              must happen after
4198                                                                                              any preceding
4199                                                                                              global/generic load/load
4200                                                                                              atomic/
4201                                                                                              atomicrmw-with-return-value.
4202                                                                                            - s_waitcnt vscnt(0)
4203                                                                                              must happen after
4204                                                                                              any preceding
4205                                                                                              global/generic
4206                                                                                              store/store
4207                                                                                              atomic/
4208                                                                                              atomicrmw-no-return-value.
4209                                                                                            - s_waitcnt lgkmcnt(0)
4210                                                                                              must happen after
4211                                                                                              any preceding
4212                                                                                              local/generic
4213                                                                                              load/store/load
4214                                                                                              atomic/store
4215                                                                                              atomic/atomicrmw.
4216                                                            - Must happen before            - Must happen before
4217                                                              the following                   the following
4218                                                              atomicrmw.                      atomicrmw.
4219                                                            - Ensures that all              - Ensures that all
4220                                                              memory operations               memory operations
4221                                                              to local have                   have
4222                                                              completed before                completed before
4223                                                              performing the                  performing the
4224                                                              atomicrmw that is               atomicrmw that is
4225                                                              being released.                 being released.
4226
4227                                                          2. buffer/global/flat_atomic    2. buffer/global_atomic
4228      atomicrmw    release      - workgroup    - local                                    1. waitcnt vmcnt(0) & vscnt(0)
4229
4230                                                                                            - If CU wavefront execution mode, omit.
4231                                                                                            - If OpenCL, omit.
4232                                                                                            - Could be split into
4233                                                                                              separate s_waitcnt
4234                                                                                              vmcnt(0) and s_waitcnt
4235                                                                                              vscnt(0) to allow
4236                                                                                              them to be
4237                                                                                              independently moved
4238                                                                                              according to the
4239                                                                                              following rules.
4240                                                                                            - s_waitcnt vmcnt(0)
4241                                                                                              must happen after
4242                                                                                              any preceding
4243                                                                                              global/generic load/load
4244                                                                                              atomic/
4245                                                                                              atomicrmw-with-return-value.
4246                                                                                            - s_waitcnt vscnt(0)
4247                                                                                              must happen after
4248                                                                                              any preceding
4249                                                                                              global/generic
4250                                                                                              store/store atomic/
4251                                                                                              atomicrmw-no-return-value.
4252                                                                                            - Must happen before
4253                                                                                              the following
4254                                                                                              store.
4255                                                                                            - Ensures that all
4256                                                                                              global memory
4257                                                                                              operations have
4258                                                                                              completed before
4259                                                                                              performing the
4260                                                                                              store that is being
4261                                                                                              released.
4262
4263                                                          1. ds_atomic                    2. ds_atomic
4264      atomicrmw    release      - workgroup    - generic  1. s_waitcnt lgkmcnt(0)         1. s_waitcnt lgkmcnt(0) &
4265                                                                                             vmcnt(0) & vscnt(0)
4266
4267                                                                                            - If CU wavefront execution mode, omit vmcnt and
4268                                                                                              vscnt.
4269                                                            - If OpenCL, omit.              - If OpenCL, omit
4270                                                                                              waitcnt lgkmcnt(0).
4271                                                            - Must happen after
4272                                                              any preceding
4273                                                              local/generic
4274                                                              load/store/load
4275                                                              atomic/store
4276                                                              atomic/atomicrmw.
4277                                                                                            - Could be split into
4278                                                                                              separate s_waitcnt
4279                                                                                              vmcnt(0), s_waitcnt
4280                                                                                              vscnt(0) and s_waitcnt
4281                                                                                              lgkmcnt(0) to allow
4282                                                                                              them to be
4283                                                                                              independently moved
4284                                                                                              according to the
4285                                                                                              following rules.
4286                                                                                            - s_waitcnt vmcnt(0)
4287                                                                                              must happen after
4288                                                                                              any preceding
4289                                                                                              global/generic load/load
4290                                                                                              atomic/
4291                                                                                              atomicrmw-with-return-value.
4292                                                                                            - s_waitcnt vscnt(0)
4293                                                                                              must happen after
4294                                                                                              any preceding
4295                                                                                              global/generic
4296                                                                                              store/store
4297                                                                                              atomic/
4298                                                                                              atomicrmw-no-return-value.
4299                                                                                            - s_waitcnt lgkmcnt(0)
4300                                                                                              must happen after
4301                                                                                              any preceding
4302                                                                                              local/generic load/store/load
4303                                                                                              atomic/store atomic/atomicrmw.
4304                                                            - Must happen before            - Must happen before
4305                                                              the following                   the following
4306                                                              atomicrmw.                      atomicrmw.
4307                                                            - Ensures that all              - Ensures that all
4308                                                              memory operations               memory operations
4309                                                              to local have                   have
4310                                                              completed before                completed before
4311                                                              performing the                  performing the
4312                                                              atomicrmw that is               atomicrmw that is
4313                                                              being released.                 being released.
4314
4315                                                          2. flat_atomic                  2. flat_atomic
4316      atomicrmw    release      - agent        - global   1. s_waitcnt lgkmcnt(0) &       1. s_waitcnt lkkmcnt(0) &
4317                                - system       - generic     vmcnt(0)                         vmcnt(0) & vscnt(0)
4318
4319                                                            - If OpenCL, omit               - If OpenCL, omit
4320                                                              lgkmcnt(0).                     lgkmcnt(0).
4321                                                            - Could be split into           - Could be split into
4322                                                              separate s_waitcnt              separate s_waitcnt
4323                                                              vmcnt(0) and                    vmcnt(0), s_waitcnt
4324                                                              s_waitcnt                       vscnt(0) and s_waitcnt
4325                                                              lgkmcnt(0) to allow             lgkmcnt(0) to allow
4326                                                              them to be                      them to be
4327                                                              independently moved             independently moved
4328                                                              according to the                according to the
4329                                                              following rules.                following rules.
4330                                                            - s_waitcnt vmcnt(0)            - s_waitcnt vmcnt(0)
4331                                                              must happen after               must happen after
4332                                                              any preceding                   any preceding
4333                                                              global/generic                  global/generic
4334                                                              load/store/load                 load/load atomic/
4335                                                              atomic/store                    atomicrmw-with-return-value.
4336                                                              atomic/atomicrmw.
4337                                                                                            - s_waitcnt vscnt(0)
4338                                                                                              must happen after
4339                                                                                              any preceding
4340                                                                                              global/generic
4341                                                                                              store/store atomic/
4342                                                                                              atomicrmw-no-return-value.
4343                                                            - s_waitcnt lgkmcnt(0)          - s_waitcnt lgkmcnt(0)
4344                                                              must happen after               must happen after
4345                                                              any preceding                   any preceding
4346                                                              local/generic                   local/generic
4347                                                              load/store/load                 load/store/load
4348                                                              atomic/store                    atomic/store
4349                                                              atomic/atomicrmw.               atomic/atomicrmw.
4350                                                            - Must happen before            - Must happen before
4351                                                              the following                   the following
4352                                                              atomicrmw.                      atomicrmw.
4353                                                            - Ensures that all              - Ensures that all
4354                                                              memory operations               memory operations
4355                                                              to global and local             to global and local
4356                                                              have completed                  have completed
4357                                                              before performing               before performing
4358                                                              the atomicrmw that              the atomicrmw that
4359                                                              is being released.              is being released.
4360
4361                                                          2. buffer/global/ds/flat_atomic 2. buffer/global/ds/flat_atomic
4362      fence        release      - singlethread *none*     *none*                          *none*
4363                                - wavefront
4364      fence        release      - workgroup    *none*     1. s_waitcnt lgkmcnt(0)         1. s_waitcnt lgkmcnt(0) &
4365                                                                                             vmcnt(0) & vscnt(0)
4366
4367                                                                                            - If CU wavefront execution mode, omit vmcnt and
4368                                                                                              vscnt.
4369                                                            - If OpenCL and                 - If OpenCL and
4370                                                              address space is                address space is
4371                                                              not generic, omit.              not generic, omit
4372                                                                                              lgkmcnt(0).
4373                                                                                            - If OpenCL and
4374                                                                                              address space is
4375                                                                                              local, omit
4376                                                                                              vmcnt(0) and vscnt(0).
4377                                                            - However, since LLVM           - However, since LLVM
4378                                                              currently has no                currently has no
4379                                                              address space on                address space on
4380                                                              the fence need to               the fence need to
4381                                                              conservatively                  conservatively
4382                                                              always generate. If             always generate. If
4383                                                              fence had an                    fence had an
4384                                                              address space then              address space then
4385                                                              set to address                  set to address
4386                                                              space of OpenCL                 space of OpenCL
4387                                                              fence flag, or to               fence flag, or to
4388                                                              generic if both                 generic if both
4389                                                              local and global                local and global
4390                                                              flags are                       flags are
4391                                                              specified.                      specified.
4392                                                            - Must happen after
4393                                                              any preceding
4394                                                              local/generic
4395                                                              load/load
4396                                                              atomic/store/store
4397                                                              atomic/atomicrmw.
4398                                                                                            - Could be split into
4399                                                                                              separate s_waitcnt
4400                                                                                              vmcnt(0), s_waitcnt
4401                                                                                              vscnt(0) and s_waitcnt
4402                                                                                              lgkmcnt(0) to allow
4403                                                                                              them to be
4404                                                                                              independently moved
4405                                                                                              according to the
4406                                                                                              following rules.
4407                                                                                            - s_waitcnt vmcnt(0)
4408                                                                                              must happen after
4409                                                                                              any preceding
4410                                                                                              global/generic
4411                                                                                              load/load
4412                                                                                              atomic/
4413                                                                                              atomicrmw-with-return-value.
4414                                                                                            - s_waitcnt vscnt(0)
4415                                                                                              must happen after
4416                                                                                              any preceding
4417                                                                                              global/generic
4418                                                                                              store/store atomic/
4419                                                                                              atomicrmw-no-return-value.
4420                                                                                            - s_waitcnt lgkmcnt(0)
4421                                                                                              must happen after
4422                                                                                              any preceding
4423                                                                                              local/generic
4424                                                                                              load/store/load
4425                                                                                              atomic/store atomic/
4426                                                                                              atomicrmw.
4427                                                            - Must happen before            - Must happen before
4428                                                              any following store             any following store
4429                                                              atomic/atomicrmw                atomic/atomicrmw
4430                                                              with an equal or                with an equal or
4431                                                              wider sync scope                wider sync scope
4432                                                              and memory ordering             and memory ordering
4433                                                              stronger than                   stronger than
4434                                                              unordered (this is              unordered (this is
4435                                                              termed the                      termed the
4436                                                              fence-paired-atomic).           fence-paired-atomic).
4437                                                            - Ensures that all              - Ensures that all
4438                                                              memory operations               memory operations
4439                                                              to local have                   have
4440                                                              completed before                completed before
4441                                                              performing the                  performing the
4442                                                              following                       following
4443                                                              fence-paired-atomic.            fence-paired-atomic.
4444
4445      fence        release      - agent        *none*     1. s_waitcnt lgkmcnt(0) &       1. s_waitcnt lgkmcnt(0) &
4446                                - system                     vmcnt(0)                        vmcnt(0) & vscnt(0)
4447
4448                                                            - If OpenCL and                 - If OpenCL and
4449                                                              address space is                address space is
4450                                                              not generic, omit               not generic, omit
4451                                                              lgkmcnt(0).                     lgkmcnt(0).
4452                                                            - If OpenCL and                 - If OpenCL and
4453                                                              address space is                address space is
4454                                                              local, omit                     local, omit
4455                                                              vmcnt(0).                       vmcnt(0) and vscnt(0).
4456                                                            - However, since LLVM           - However, since LLVM
4457                                                              currently has no                currently has no
4458                                                              address space on                address space on
4459                                                              the fence need to               the fence need to
4460                                                              conservatively                  conservatively
4461                                                              always generate. If             always generate. If
4462                                                              fence had an                    fence had an
4463                                                              address space then              address space then
4464                                                              set to address                  set to address
4465                                                              space of OpenCL                 space of OpenCL
4466                                                              fence flag, or to               fence flag, or to
4467                                                              generic if both                 generic if both
4468                                                              local and global                local and global
4469                                                              flags are                       flags are
4470                                                              specified.                      specified.
4471                                                            - Could be split into           - Could be split into
4472                                                              separate s_waitcnt              separate s_waitcnt
4473                                                              vmcnt(0) and                    vmcnt(0), s_waitcnt
4474                                                              s_waitcnt                       vscnt(0) and s_waitcnt
4475                                                              lgkmcnt(0) to allow             lgkmcnt(0) to allow
4476                                                              them to be                      them to be
4477                                                              independently moved             independently moved
4478                                                              according to the                according to the
4479                                                              following rules.                following rules.
4480                                                            - s_waitcnt vmcnt(0)            - s_waitcnt vmcnt(0)
4481                                                              must happen after               must happen after
4482                                                              any preceding                   any preceding
4483                                                              global/generic                  global/generic
4484                                                              load/store/load                 load/load atomic/
4485                                                              atomic/store                    atomicrmw-with-return-value.
4486                                                              atomic/atomicrmw.
4487                                                                                            - s_waitcnt vscnt(0)
4488                                                                                              must happen after
4489                                                                                              any preceding
4490                                                                                              global/generic
4491                                                                                              store/store atomic/
4492                                                                                              atomicrmw-no-return-value.
4493                                                            - s_waitcnt lgkmcnt(0)          - s_waitcnt lgkmcnt(0)
4494                                                              must happen after               must happen after
4495                                                              any preceding                   any preceding
4496                                                              local/generic                   local/generic
4497                                                              load/store/load                 load/store/load
4498                                                              atomic/store                    atomic/store
4499                                                              atomic/atomicrmw.               atomic/atomicrmw.
4500                                                            - Must happen before            - Must happen before
4501                                                              any following store             any following store
4502                                                              atomic/atomicrmw                atomic/atomicrmw
4503                                                              with an equal or                with an equal or
4504                                                              wider sync scope                wider sync scope
4505                                                              and memory ordering             and memory ordering
4506                                                              stronger than                   stronger than
4507                                                              unordered (this is              unordered (this is
4508                                                              termed the                      termed the
4509                                                              fence-paired-atomic).           fence-paired-atomic).
4510                                                            - Ensures that all              - Ensures that all
4511                                                              memory operations               memory operations
4512                                                              have                            have
4513                                                              completed before                completed before
4514                                                              performing the                  performing the
4515                                                              following                       following
4516                                                              fence-paired-atomic.            fence-paired-atomic.
4517
4518      **Acquire-Release Atomic**
4519      ----------------------------------------------------------------------------------------------------------------------
4520      atomicrmw    acq_rel      - singlethread - global   1. buffer/global/ds/flat_atomic 1. buffer/global/ds/flat_atomic
4521                                - wavefront    - local
4522                                               - generic
4523      atomicrmw    acq_rel      - workgroup    - global   1. s_waitcnt lgkmcnt(0)         1. s_waitcnt lgkmcnt(0) &
4524                                                                                             vmcnt(0) & vscnt(0)
4525
4526                                                                                            - If CU wavefront execution mode, omit vmcnt and
4527                                                                                              vscnt.
4528                                                            - If OpenCL, omit.              - If OpenCL, omit
4529                                                                                              s_waitcnt lgkmcnt(0).
4530                                                            - Must happen after             - Must happen after
4531                                                              any preceding                   any preceding
4532                                                              local/generic                   local/generic
4533                                                              load/store/load                 load/store/load
4534                                                              atomic/store                    atomic/store
4535                                                              atomic/atomicrmw.               atomic/atomicrmw.
4536                                                                                            - Could be split into
4537                                                                                              separate s_waitcnt
4538                                                                                              vmcnt(0), s_waitcnt
4539                                                                                              vscnt(0) and s_waitcnt
4540                                                                                              lgkmcnt(0) to allow
4541                                                                                              them to be
4542                                                                                              independently moved
4543                                                                                              according to the
4544                                                                                              following rules.
4545                                                                                            - s_waitcnt vmcnt(0)
4546                                                                                              must happen after
4547                                                                                              any preceding
4548                                                                                              global/generic load/load
4549                                                                                              atomic/
4550                                                                                              atomicrmw-with-return-value.
4551                                                                                            - s_waitcnt vscnt(0)
4552                                                                                              must happen after
4553                                                                                              any preceding
4554                                                                                              global/generic
4555                                                                                              store/store
4556                                                                                              atomic/
4557                                                                                              atomicrmw-no-return-value.
4558                                                                                            - s_waitcnt lgkmcnt(0)
4559                                                                                              must happen after
4560                                                                                              any preceding
4561                                                                                              local/generic load/store/load
4562                                                                                              atomic/store atomic/atomicrmw.
4563                                                            - Must happen before            - Must happen before
4564                                                              the following                   the following
4565                                                              atomicrmw.                      atomicrmw.
4566                                                            - Ensures that all              - Ensures that all
4567                                                              memory operations               memory operations
4568                                                              to local have                   have
4569                                                              completed before                completed before
4570                                                              performing the                  performing the
4571                                                              atomicrmw that is               atomicrmw that is
4572                                                              being released.                 being released.
4573
4574                                                          2. buffer/global/flat_atomic    2. buffer/global_atomic
4575                                                                                          3. s_waitcnt vm/vscnt(0)
4576
4577                                                                                            - If CU wavefront execution mode, omit vm/vscnt.
4578                                                                                            - Use vmcnt if atomic with
4579                                                                                              return and vscnt if atomic
4580                                                                                              with no-return.
4581                                                                                              waitcnt lgkmcnt(0).
4582                                                                                            - Must happen before
4583                                                                                              the following
4584                                                                                              buffer_gl0_inv.
4585                                                                                            - Ensures any
4586                                                                                              following global
4587                                                                                              data read is no
4588                                                                                              older than the
4589                                                                                              atomicrmw value
4590                                                                                              being acquired.
4591
4592                                                                                          4. buffer_gl0_inv
4593
4594                                                                                            - If CU wavefront execution mode, omit.
4595                                                                                            - Ensures that
4596                                                                                              following
4597                                                                                              loads will not see
4598                                                                                              stale data.
4599
4600      atomicrmw    acq_rel      - workgroup    - local                                    1. waitcnt vmcnt(0) & vscnt(0)
4601
4602                                                                                            - If CU wavefront execution mode, omit.
4603                                                                                            - If OpenCL, omit.
4604                                                                                            - Could be split into
4605                                                                                              separate s_waitcnt
4606                                                                                              vmcnt(0) and s_waitcnt
4607                                                                                              vscnt(0) to allow
4608                                                                                              them to be
4609                                                                                              independently moved
4610                                                                                              according to the
4611                                                                                              following rules.
4612                                                                                            - s_waitcnt vmcnt(0)
4613                                                                                              must happen after
4614                                                                                              any preceding
4615                                                                                              global/generic load/load
4616                                                                                              atomic/
4617                                                                                              atomicrmw-with-return-value.
4618                                                                                            - s_waitcnt vscnt(0)
4619                                                                                              must happen after
4620                                                                                              any preceding
4621                                                                                              global/generic
4622                                                                                              store/store atomic/
4623                                                                                              atomicrmw-no-return-value.
4624                                                                                            - Must happen before
4625                                                                                              the following
4626                                                                                              store.
4627                                                                                            - Ensures that all
4628                                                                                              global memory
4629                                                                                              operations have
4630                                                                                              completed before
4631                                                                                              performing the
4632                                                                                              store that is being
4633                                                                                              released.
4634
4635                                                          1. ds_atomic                    2. ds_atomic
4636                                                          2. s_waitcnt lgkmcnt(0)         3. s_waitcnt lgkmcnt(0)
4637
4638                                                            - If OpenCL, omit.              - If OpenCL, omit.
4639                                                            - Must happen before            - Must happen before
4640                                                              any following                   the following
4641                                                              global/generic                  buffer_gl0_inv.
4642                                                              load/load
4643                                                              atomic/store/store
4644                                                              atomic/atomicrmw.
4645                                                            - Ensures any                   - Ensures any
4646                                                              following global                following global
4647                                                              data read is no                 data read is no
4648                                                              older than the load             older than the load
4649                                                              atomic value being              atomic value being
4650                                                              acquired.                       acquired.
4651
4652                                                                                          4. buffer_gl0_inv
4653
4654                                                                                            - If CU wavefront execution mode, omit.
4655                                                                                            - If OpenCL omit.
4656                                                                                            - Ensures that
4657                                                                                              following
4658                                                                                              loads will not see
4659                                                                                              stale data.
4660
4661      atomicrmw    acq_rel      - workgroup    - generic  1. s_waitcnt lgkmcnt(0)         1. s_waitcnt lgkmcnt(0) &
4662                                                                                             vmcnt(0) & vscnt(0)
4663
4664                                                                                            - If CU wavefront execution mode, omit vmcnt and
4665                                                                                              vscnt.
4666                                                            - If OpenCL, omit.              - If OpenCL, omit
4667                                                                                              waitcnt lgkmcnt(0).
4668                                                            - Must happen after
4669                                                              any preceding
4670                                                              local/generic
4671                                                              load/store/load
4672                                                              atomic/store
4673                                                              atomic/atomicrmw.
4674                                                                                            - Could be split into
4675                                                                                              separate s_waitcnt
4676                                                                                              vmcnt(0), s_waitcnt
4677                                                                                              vscnt(0) and s_waitcnt
4678                                                                                              lgkmcnt(0) to allow
4679                                                                                              them to be
4680                                                                                              independently moved
4681                                                                                              according to the
4682                                                                                              following rules.
4683                                                                                            - s_waitcnt vmcnt(0)
4684                                                                                              must happen after
4685                                                                                              any preceding
4686                                                                                              global/generic load/load
4687                                                                                              atomic/
4688                                                                                              atomicrmw-with-return-value.
4689                                                                                            - s_waitcnt vscnt(0)
4690                                                                                              must happen after
4691                                                                                              any preceding
4692                                                                                              global/generic
4693                                                                                              store/store
4694                                                                                              atomic/
4695                                                                                              atomicrmw-no-return-value.
4696                                                                                            - s_waitcnt lgkmcnt(0)
4697                                                                                              must happen after
4698                                                                                              any preceding
4699                                                                                              local/generic load/store/load
4700                                                                                              atomic/store atomic/atomicrmw.
4701                                                            - Must happen before            - Must happen before
4702                                                              the following                   the following
4703                                                              atomicrmw.                      atomicrmw.
4704                                                            - Ensures that all              - Ensures that all
4705                                                              memory operations               memory operations
4706                                                              to local have                   have
4707                                                              completed before                completed before
4708                                                              performing the                  performing the
4709                                                              atomicrmw that is               atomicrmw that is
4710                                                              being released.                 being released.
4711
4712                                                          2. flat_atomic                  2. flat_atomic
4713                                                          3. s_waitcnt lgkmcnt(0)         3. s_waitcnt lgkmcnt(0) &
4714                                                                                             vm/vscnt(0)
4715
4716                                                                                            - If CU wavefront execution mode, omit vm/vscnt.
4717                                                            - If OpenCL, omit.              - If OpenCL, omit
4718                                                                                              waitcnt lgkmcnt(0).
4719                                                            - Must happen before            - Must happen before
4720                                                              any following                   the following
4721                                                              global/generic                  buffer_gl0_inv.
4722                                                              load/load
4723                                                              atomic/store/store
4724                                                              atomic/atomicrmw.
4725                                                            - Ensures any                   - Ensures any
4726                                                              following global                following global
4727                                                              data read is no                 data read is no
4728                                                              older than the load             older than the load
4729                                                              atomic value being              atomic value being
4730                                                              acquired.                       acquired.
4731
4732                                                                                          3. buffer_gl0_inv
4733
4734                                                                                            - If CU wavefront execution mode, omit.
4735                                                                                            - Ensures that
4736                                                                                              following
4737                                                                                              loads will not see
4738                                                                                              stale data.
4739
4740      atomicrmw    acq_rel      - agent        - global   1. s_waitcnt lgkmcnt(0) &       1. s_waitcnt lgkmcnt(0) &
4741                                - system                     vmcnt(0)                        vmcnt(0) & vscnt(0)
4742
4743                                                            - If OpenCL, omit               - If OpenCL, omit
4744                                                              lgkmcnt(0).                     lgkmcnt(0).
4745                                                            - Could be split into           - Could be split into
4746                                                              separate s_waitcnt              separate s_waitcnt
4747                                                              vmcnt(0) and                    vmcnt(0), s_waitcnt
4748                                                              s_waitcnt                       vscnt(0) and s_waitcnt
4749                                                              lgkmcnt(0) to allow             lgkmcnt(0) to allow
4750                                                              them to be                      them to be
4751                                                              independently moved             independently moved
4752                                                              according to the                according to the
4753                                                              following rules.                following rules.
4754                                                            - s_waitcnt vmcnt(0)            - s_waitcnt vmcnt(0)
4755                                                              must happen after               must happen after
4756                                                              any preceding                   any preceding
4757                                                              global/generic                  global/generic
4758                                                              load/store/load                 load/load atomic/
4759                                                              atomic/store                    atomicrmw-with-return-value.
4760                                                              atomic/atomicrmw.
4761                                                                                            - s_waitcnt vscnt(0)
4762                                                                                              must happen after
4763                                                                                              any preceding
4764                                                                                              global/generic
4765                                                                                              store/store atomic/
4766                                                                                              atomicrmw-no-return-value.
4767                                                            - s_waitcnt lgkmcnt(0)          - s_waitcnt lgkmcnt(0)
4768                                                              must happen after               must happen after
4769                                                              any preceding                   any preceding
4770                                                              local/generic                   local/generic
4771                                                              load/store/load                 load/store/load
4772                                                              atomic/store                    atomic/store
4773                                                              atomic/atomicrmw.               atomic/atomicrmw.
4774                                                            - Must happen before            - Must happen before
4775                                                              the following                   the following
4776                                                              atomicrmw.                      atomicrmw.
4777                                                            - Ensures that all              - Ensures that all
4778                                                              memory operations               memory operations
4779                                                              to global have                  to global have
4780                                                              completed before                completed before
4781                                                              performing the                  performing the
4782                                                              atomicrmw that is               atomicrmw that is
4783                                                              being released.                 being released.
4784
4785                                                          2. buffer/global/flat_atomic    2. buffer/global_atomic
4786                                                          3. s_waitcnt vmcnt(0)           3. s_waitcnt vm/vscnt(0)
4787
4788                                                                                            - Use vmcnt if atomic with
4789                                                                                              return and vscnt if atomic
4790                                                                                              with no-return.
4791                                                                                              waitcnt lgkmcnt(0).
4792                                                            - Must happen before            - Must happen before
4793                                                              following                       following
4794                                                              buffer_wbinvl1_vol.             buffer_gl*_inv.
4795                                                            - Ensures the                   - Ensures the
4796                                                              atomicrmw has                   atomicrmw has
4797                                                              completed before                completed before
4798                                                              invalidating the                invalidating the
4799                                                              cache.                          caches.
4800
4801                                                          4. buffer_wbinvl1_vol           4. buffer_gl0_inv;
4802                                                                                             buffer_gl1_inv
4803
4804                                                            - Must happen before            - Must happen before
4805                                                              any following                   any following
4806                                                              global/generic                  global/generic
4807                                                              load/load                       load/load
4808                                                              atomic/atomicrmw.               atomic/atomicrmw.
4809                                                            - Ensures that                  - Ensures that
4810                                                              following loads                 following loads
4811                                                              will not see stale              will not see stale
4812                                                              global data.                    global data.
4813
4814      atomicrmw    acq_rel      - agent        - generic  1. s_waitcnt lgkmcnt(0) &       1. s_waitcnt lgkmcnt(0) &
4815                                - system                     vmcnt(0)                        vmcnt(0) & vscnt(0)
4816
4817                                                            - If OpenCL, omit               - If OpenCL, omit
4818                                                              lgkmcnt(0).                     lgkmcnt(0).
4819                                                            - Could be split into           - Could be split into
4820                                                              separate s_waitcnt              separate s_waitcnt
4821                                                              vmcnt(0) and                    vmcnt(0), s_waitcnt
4822                                                              s_waitcnt                       vscnt(0) and s_waitcnt
4823                                                              lgkmcnt(0) to allow             lgkmcnt(0) to allow
4824                                                              them to be                      them to be
4825                                                              independently moved             independently moved
4826                                                              according to the                according to the
4827                                                              following rules.                following rules.
4828                                                            - s_waitcnt vmcnt(0)            - s_waitcnt vmcnt(0)
4829                                                              must happen after               must happen after
4830                                                              any preceding                   any preceding
4831                                                              global/generic                  global/generic
4832                                                              load/store/load                 load/load atomic
4833                                                              atomic/store                    atomicrmw-with-return-value.
4834                                                              atomic/atomicrmw.
4835                                                                                            - s_waitcnt vscnt(0)
4836                                                                                              must happen after
4837                                                                                              any preceding
4838                                                                                              global/generic
4839                                                                                              store/store atomic/
4840                                                                                              atomicrmw-no-return-value.
4841                                                            - s_waitcnt lgkmcnt(0)          - s_waitcnt lgkmcnt(0)
4842                                                              must happen after               must happen after
4843                                                              any preceding                   any preceding
4844                                                              local/generic                   local/generic
4845                                                              load/store/load                 load/store/load
4846                                                              atomic/store                    atomic/store
4847                                                              atomic/atomicrmw.               atomic/atomicrmw.
4848                                                            - Must happen before            - Must happen before
4849                                                              the following                   the following
4850                                                              atomicrmw.                      atomicrmw.
4851                                                            - Ensures that all              - Ensures that all
4852                                                              memory operations               memory operations
4853                                                              to global have                  have
4854                                                              completed before                completed before
4855                                                              performing the                  performing the
4856                                                              atomicrmw that is               atomicrmw that is
4857                                                              being released.                 being released.
4858
4859                                                          2. flat_atomic                  2. flat_atomic
4860                                                          3. s_waitcnt vmcnt(0) &         3. s_waitcnt vm/vscnt(0) &
4861                                                             lgkmcnt(0)                      lgkmcnt(0)
4862
4863                                                            - If OpenCL, omit               - If OpenCL, omit
4864                                                              lgkmcnt(0).                     lgkmcnt(0).
4865                                                                                            - Use vmcnt if atomic with
4866                                                                                              return and vscnt if atomic
4867                                                                                              with no-return.
4868                                                            - Must happen before            - Must happen before
4869                                                              following                       following
4870                                                              buffer_wbinvl1_vol.             buffer_gl*_inv.
4871                                                            - Ensures the                   - Ensures the
4872                                                              atomicrmw has                   atomicrmw has
4873                                                              completed before                completed before
4874                                                              invalidating the                invalidating the
4875                                                              cache.                          caches.
4876
4877                                                          4. buffer_wbinvl1_vol           4. buffer_gl0_inv;
4878                                                                                             buffer_gl1_inv
4879
4880                                                            - Must happen before            - Must happen before
4881                                                              any following                   any following
4882                                                              global/generic                  global/generic
4883                                                              load/load                       load/load
4884                                                              atomic/atomicrmw.               atomic/atomicrmw.
4885                                                            - Ensures that                  - Ensures that
4886                                                              following loads                 following loads
4887                                                              will not see stale              will not see stale
4888                                                              global data.                    global data.
4889
4890      fence        acq_rel      - singlethread *none*     *none*                          *none*
4891                                - wavefront
4892      fence        acq_rel      - workgroup    *none*     1. s_waitcnt lgkmcnt(0)         1. s_waitcnt lgkmcnt(0) &
4893                                                                                             vmcnt(0) & vscnt(0)
4894
4895                                                                                            - If CU wavefront execution mode, omit vmcnt and
4896                                                                                              vscnt.
4897                                                            - If OpenCL and                 - If OpenCL and
4898                                                              address space is                address space is
4899                                                              not generic, omit.              not generic, omit
4900                                                                                              lgkmcnt(0).
4901                                                                                            - If OpenCL and
4902                                                                                              address space is
4903                                                                                              local, omit
4904                                                                                              vmcnt(0) and vscnt(0).
4905                                                            - However,                      - However,
4906                                                              since LLVM                      since LLVM
4907                                                              currently has no                currently has no
4908                                                              address space on                address space on
4909                                                              the fence need to               the fence need to
4910                                                              conservatively                  conservatively
4911                                                              always generate                 always generate
4912                                                              (see comment for                (see comment for
4913                                                              previous fence).                previous fence).
4914                                                            - Must happen after
4915                                                              any preceding
4916                                                              local/generic
4917                                                              load/load
4918                                                              atomic/store/store
4919                                                              atomic/atomicrmw.
4920                                                                                            - Could be split into
4921                                                                                              separate s_waitcnt
4922                                                                                              vmcnt(0), s_waitcnt
4923                                                                                              vscnt(0) and s_waitcnt
4924                                                                                              lgkmcnt(0) to allow
4925                                                                                              them to be
4926                                                                                              independently moved
4927                                                                                              according to the
4928                                                                                              following rules.
4929                                                                                            - s_waitcnt vmcnt(0)
4930                                                                                              must happen after
4931                                                                                              any preceding
4932                                                                                              global/generic
4933                                                                                              load/load
4934                                                                                              atomic/
4935                                                                                              atomicrmw-with-return-value.
4936                                                                                            - s_waitcnt vscnt(0)
4937                                                                                              must happen after
4938                                                                                              any preceding
4939                                                                                              global/generic
4940                                                                                              store/store atomic/
4941                                                                                              atomicrmw-no-return-value.
4942                                                                                            - s_waitcnt lgkmcnt(0)
4943                                                                                              must happen after
4944                                                                                              any preceding
4945                                                                                              local/generic
4946                                                                                              load/store/load
4947                                                                                              atomic/store atomic/
4948                                                                                              atomicrmw.
4949                                                            - Must happen before            - Must happen before
4950                                                              any following                   any following
4951                                                              global/generic                  global/generic
4952                                                              load/load                       load/load
4953                                                              atomic/store/store              atomic/store/store
4954                                                              atomic/atomicrmw.               atomic/atomicrmw.
4955                                                            - Ensures that all              - Ensures that all
4956                                                              memory operations               memory operations
4957                                                              to local have                   have
4958                                                              completed before                completed before
4959                                                              performing any                  performing any
4960                                                              following global                following global
4961                                                              memory operations.              memory operations.
4962                                                            - Ensures that the              - Ensures that the
4963                                                              preceding                       preceding
4964                                                              local/generic load              local/generic load
4965                                                              atomic/atomicrmw                atomic/atomicrmw
4966                                                              with an equal or                with an equal or
4967                                                              wider sync scope                wider sync scope
4968                                                              and memory ordering             and memory ordering
4969                                                              stronger than                   stronger than
4970                                                              unordered (this is              unordered (this is
4971                                                              termed the                      termed the
4972                                                              acquire-fence-paired-atomic     acquire-fence-paired-atomic
4973                                                              ) has completed                 ) has completed
4974                                                              before following                before following
4975                                                              global memory                   global memory
4976                                                              operations. This                operations. This
4977                                                              satisfies the                   satisfies the
4978                                                              requirements of                 requirements of
4979                                                              acquire.                        acquire.
4980                                                            - Ensures that all              - Ensures that all
4981                                                              previous memory                 previous memory
4982                                                              operations have                 operations have
4983                                                              completed before a              completed before a
4984                                                              following                       following
4985                                                              local/generic store             local/generic store
4986                                                              atomic/atomicrmw                atomic/atomicrmw
4987                                                              with an equal or                with an equal or
4988                                                              wider sync scope                wider sync scope
4989                                                              and memory ordering             and memory ordering
4990                                                              stronger than                   stronger than
4991                                                              unordered (this is              unordered (this is
4992                                                              termed the                      termed the
4993                                                              release-fence-paired-atomic     release-fence-paired-atomic
4994                                                              ). This satisfies the           ). This satisfies the
4995                                                              requirements of                 requirements of
4996                                                              release.                        release.
4997                                                                                            - Must happen before
4998                                                                                              the following
4999                                                                                              buffer_gl0_inv.
5000                                                                                            - Ensures that the
5001                                                                                              acquire-fence-paired
5002                                                                                              atomic has completed
5003                                                                                              before invalidating
5004                                                                                              the
5005                                                                                              cache. Therefore
5006                                                                                              any following
5007                                                                                              locations read must
5008                                                                                              be no older than
5009                                                                                              the value read by
5010                                                                                              the
5011                                                                                              acquire-fence-paired-atomic.
5012
5013                                                                                          3. buffer_gl0_inv
5014
5015                                                                                            - If CU wavefront execution mode, omit.
5016                                                                                            - Ensures that
5017                                                                                              following
5018                                                                                              loads will not see
5019                                                                                              stale data.
5020
5021      fence        acq_rel      - agent        *none*     1. s_waitcnt lgkmcnt(0) &       1. s_waitcnt lgkmcnt(0) &
5022                                - system                     vmcnt(0)                        vmcnt(0) & vscnt(0)
5023
5024                                                            - If OpenCL and                 - If OpenCL and
5025                                                              address space is                address space is
5026                                                              not generic, omit               not generic, omit
5027                                                              lgkmcnt(0).                     lgkmcnt(0).
5028                                                                                            - If OpenCL and
5029                                                                                              address space is
5030                                                                                              local, omit
5031                                                                                              vmcnt(0) and vscnt(0).
5032                                                            - However, since LLVM           - However, since LLVM
5033                                                              currently has no                currently has no
5034                                                              address space on                address space on
5035                                                              the fence need to               the fence need to
5036                                                              conservatively                  conservatively
5037                                                              always generate                 always generate
5038                                                              (see comment for                (see comment for
5039                                                              previous fence).                previous fence).
5040                                                            - Could be split into           - Could be split into
5041                                                              separate s_waitcnt              separate s_waitcnt
5042                                                              vmcnt(0) and                    vmcnt(0), s_waitcnt
5043                                                              s_waitcnt                       vscnt(0) and s_waitcnt
5044                                                              lgkmcnt(0) to allow             lgkmcnt(0) to allow
5045                                                              them to be                      them to be
5046                                                              independently moved             independently moved
5047                                                              according to the                according to the
5048                                                              following rules.                following rules.
5049                                                            - s_waitcnt vmcnt(0)            - s_waitcnt vmcnt(0)
5050                                                              must happen after               must happen after
5051                                                              any preceding                   any preceding
5052                                                              global/generic                  global/generic
5053                                                              load/store/load                 load/load
5054                                                              atomic/store                    atomic/
5055                                                              atomic/atomicrmw.               atomicrmw-with-return-value.
5056                                                                                            - s_waitcnt vscnt(0)
5057                                                                                              must happen after
5058                                                                                              any preceding
5059                                                                                              global/generic
5060                                                                                              store/store atomic/
5061                                                                                              atomicrmw-no-return-value.
5062                                                            - s_waitcnt lgkmcnt(0)          - s_waitcnt lgkmcnt(0)
5063                                                              must happen after               must happen after
5064                                                              any preceding                   any preceding
5065                                                              local/generic                   local/generic
5066                                                              load/store/load                 load/store/load
5067                                                              atomic/store                    atomic/store
5068                                                              atomic/atomicrmw.               atomic/atomicrmw.
5069                                                            - Must happen before            - Must happen before
5070                                                              the following                   the following
5071                                                              buffer_wbinvl1_vol.             buffer_gl*_inv.
5072                                                            - Ensures that the              - Ensures that the
5073                                                              preceding                       preceding
5074                                                              global/local/generic            global/local/generic
5075                                                              load                            load
5076                                                              atomic/atomicrmw                atomic/atomicrmw
5077                                                              with an equal or                with an equal or
5078                                                              wider sync scope                wider sync scope
5079                                                              and memory ordering             and memory ordering
5080                                                              stronger than                   stronger than
5081                                                              unordered (this is              unordered (this is
5082                                                              termed the                      termed the
5083                                                              acquire-fence-paired-atomic     acquire-fence-paired-atomic
5084                                                              ) has completed                 ) has completed
5085                                                              before invalidating             before invalidating
5086                                                              the cache. This                 the caches. This
5087                                                              satisfies the                   satisfies the
5088                                                              requirements of                 requirements of
5089                                                              acquire.                        acquire.
5090                                                            - Ensures that all              - Ensures that all
5091                                                              previous memory                 previous memory
5092                                                              operations have                 operations have
5093                                                              completed before a              completed before a
5094                                                              following                       following
5095                                                              global/local/generic            global/local/generic
5096                                                              store                           store
5097                                                              atomic/atomicrmw                atomic/atomicrmw
5098                                                              with an equal or                with an equal or
5099                                                              wider sync scope                wider sync scope
5100                                                              and memory ordering             and memory ordering
5101                                                              stronger than                   stronger than
5102                                                              unordered (this is              unordered (this is
5103                                                              termed the                      termed the
5104                                                              release-fence-paired-atomic     release-fence-paired-atomic
5105                                                              ). This satisfies the           ). This satisfies the
5106                                                              requirements of                 requirements of
5107                                                              release.                        release.
5108
5109                                                          2. buffer_wbinvl1_vol           2. buffer_gl0_inv;
5110                                                                                             buffer_gl1_inv
5111
5112                                                            - Must happen before            - Must happen before
5113                                                              any following                   any following
5114                                                              global/generic                  global/generic
5115                                                              load/load                       load/load
5116                                                              atomic/store/store              atomic/store/store
5117                                                              atomic/atomicrmw.               atomic/atomicrmw.
5118                                                            - Ensures that                  - Ensures that
5119                                                              following loads                 following loads
5120                                                              will not see stale              will not see stale
5121                                                              global data. This               global data. This
5122                                                              satisfies the                   satisfies the
5123                                                              requirements of                 requirements of
5124                                                              acquire.                        acquire.
5125
5126      **Sequential Consistent Atomic**
5127      ----------------------------------------------------------------------------------------------------------------------
5128      load atomic  seq_cst      - singlethread - global   *Same as corresponding          *Same as corresponding
5129                                - wavefront    - local    load atomic acquire,            load atomic acquire,
5130                                               - generic  except must generated           except must generated
5131                                                          all instructions even           all instructions even
5132                                                          for OpenCL.*                    for OpenCL.*
5133      load atomic  seq_cst      - workgroup    - global   1. s_waitcnt lgkmcnt(0)         1. s_waitcnt lgkmcnt(0) &
5134                                               - generic                                     vmcnt(0) & vscnt(0)
5135
5136                                                                                            - If CU wavefront execution mode, omit vmcnt and
5137                                                                                              vscnt.
5138                                                                                            - Could be split into
5139                                                                                              separate s_waitcnt
5140                                                                                              vmcnt(0), s_waitcnt
5141                                                                                              vscnt(0) and s_waitcnt
5142                                                                                              lgkmcnt(0) to allow
5143                                                                                              them to be
5144                                                                                              independently moved
5145                                                                                              according to the
5146                                                                                              following rules.
5147                                                            - Must                          - waitcnt lgkmcnt(0) must
5148                                                              happen after                    happen after
5149                                                              preceding                       preceding
5150                                                              global/generic load             local load
5151                                                              atomic/store                    atomic/store
5152                                                              atomic/atomicrmw                atomic/atomicrmw
5153                                                              with memory                     with memory
5154                                                              ordering of seq_cst             ordering of seq_cst
5155                                                              and with equal or               and with equal or
5156                                                              wider sync scope.               wider sync scope.
5157                                                              (Note that seq_cst              (Note that seq_cst
5158                                                              fences have their               fences have their
5159                                                              own s_waitcnt                   own s_waitcnt
5160                                                              lgkmcnt(0) and so do            lgkmcnt(0) and so do
5161                                                              not need to be                  not need to be
5162                                                              considered.)                    considered.)
5163                                                                                            - waitcnt vmcnt(0)
5164                                                                                              Must happen after
5165                                                                                              preceding
5166                                                                                              global/generic load
5167                                                                                              atomic/
5168                                                                                              atomicrmw-with-return-value
5169                                                                                              with memory
5170                                                                                              ordering of seq_cst
5171                                                                                              and with equal or
5172                                                                                              wider sync scope.
5173                                                                                              (Note that seq_cst
5174                                                                                              fences have their
5175                                                                                              own s_waitcnt
5176                                                                                              vmcnt(0) and so do
5177                                                                                              not need to be
5178                                                                                              considered.)
5179                                                                                            - waitcnt vscnt(0)
5180                                                                                              Must happen after
5181                                                                                              preceding
5182                                                                                              global/generic store
5183                                                                                              atomic/
5184                                                                                              atomicrmw-no-return-value
5185                                                                                              with memory
5186                                                                                              ordering of seq_cst
5187                                                                                              and with equal or
5188                                                                                              wider sync scope.
5189                                                                                              (Note that seq_cst
5190                                                                                              fences have their
5191                                                                                              own s_waitcnt
5192                                                                                              vscnt(0) and so do
5193                                                                                              not need to be
5194                                                                                              considered.)
5195                                                            - Ensures any                   - Ensures any
5196                                                              preceding                       preceding
5197                                                              sequential                      sequential
5198                                                              consistent local                consistent global/local
5199                                                              memory instructions             memory instructions
5200                                                              have completed                  have completed
5201                                                              before executing                before executing
5202                                                              this sequentially               this sequentially
5203                                                              consistent                      consistent
5204                                                              instruction. This               instruction. This
5205                                                              prevents reordering             prevents reordering
5206                                                              a seq_cst store                 a seq_cst store
5207                                                              followed by a                   followed by a
5208                                                              seq_cst load. (Note             seq_cst load. (Note
5209                                                              that seq_cst is                 that seq_cst is
5210                                                              stronger than                   stronger than
5211                                                              acquire/release as              acquire/release as
5212                                                              the reordering of               the reordering of
5213                                                              load acquire                    load acquire
5214                                                              followed by a store             followed by a store
5215                                                              release is                      release is
5216                                                              prevented by the                prevented by the
5217                                                              waitcnt of                      waitcnt of
5218                                                              the release, but                the release, but
5219                                                              there is nothing                there is nothing
5220                                                              preventing a store              preventing a store
5221                                                              release followed by             release followed by
5222                                                              load acquire from               load acquire from
5223                                                              competing out of                competing out of
5224                                                              order.)                         order.)
5225
5226                                                          2. *Following                   2. *Following
5227                                                             instructions same as            instructions same as
5228                                                             corresponding load              corresponding load
5229                                                             atomic acquire,                 atomic acquire,
5230                                                             except must generated           except must generated
5231                                                             all instructions even           all instructions even
5232                                                             for OpenCL.*                    for OpenCL.*
5233      load atomic  seq_cst      - workgroup    - local    *Same as corresponding
5234                                                          load atomic acquire,
5235                                                          except must generated
5236                                                          all instructions even
5237                                                          for OpenCL.*
5238
5239                                                                                          1. s_waitcnt vmcnt(0) & vscnt(0)
5240
5241                                                                                            - If CU wavefront execution mode, omit.
5242                                                                                            - Could be split into
5243                                                                                              separate s_waitcnt
5244                                                                                              vmcnt(0) and s_waitcnt
5245                                                                                              vscnt(0) to allow
5246                                                                                              them to be
5247                                                                                              independently moved
5248                                                                                              according to the
5249                                                                                              following rules.
5250                                                                                            - waitcnt vmcnt(0)
5251                                                                                              Must happen after
5252                                                                                              preceding
5253                                                                                              global/generic load
5254                                                                                              atomic/
5255                                                                                              atomicrmw-with-return-value
5256                                                                                              with memory
5257                                                                                              ordering of seq_cst
5258                                                                                              and with equal or
5259                                                                                              wider sync scope.
5260                                                                                              (Note that seq_cst
5261                                                                                              fences have their
5262                                                                                              own s_waitcnt
5263                                                                                              vmcnt(0) and so do
5264                                                                                              not need to be
5265                                                                                              considered.)
5266                                                                                            - waitcnt vscnt(0)
5267                                                                                              Must happen after
5268                                                                                              preceding
5269                                                                                              global/generic store
5270                                                                                              atomic/
5271                                                                                              atomicrmw-no-return-value
5272                                                                                              with memory
5273                                                                                              ordering of seq_cst
5274                                                                                              and with equal or
5275                                                                                              wider sync scope.
5276                                                                                              (Note that seq_cst
5277                                                                                              fences have their
5278                                                                                              own s_waitcnt
5279                                                                                              vscnt(0) and so do
5280                                                                                              not need to be
5281                                                                                              considered.)
5282                                                                                            - Ensures any
5283                                                                                              preceding
5284                                                                                              sequential
5285                                                                                              consistent global
5286                                                                                              memory instructions
5287                                                                                              have completed
5288                                                                                              before executing
5289                                                                                              this sequentially
5290                                                                                              consistent
5291                                                                                              instruction. This
5292                                                                                              prevents reordering
5293                                                                                              a seq_cst store
5294                                                                                              followed by a
5295                                                                                              seq_cst load. (Note
5296                                                                                              that seq_cst is
5297                                                                                              stronger than
5298                                                                                              acquire/release as
5299                                                                                              the reordering of
5300                                                                                              load acquire
5301                                                                                              followed by a store
5302                                                                                              release is
5303                                                                                              prevented by the
5304                                                                                              waitcnt of
5305                                                                                              the release, but
5306                                                                                              there is nothing
5307                                                                                              preventing a store
5308                                                                                              release followed by
5309                                                                                              load acquire from
5310                                                                                              competing out of
5311                                                                                              order.)
5312
5313                                                                                          2. *Following
5314                                                                                             instructions same as
5315                                                                                             corresponding load
5316                                                                                             atomic acquire,
5317                                                                                             except must generated
5318                                                                                             all instructions even
5319                                                                                             for OpenCL.*
5320
5321      load atomic  seq_cst      - agent        - global   1. s_waitcnt lgkmcnt(0) &       1. s_waitcnt lgkmcnt(0) &
5322                                - system       - generic     vmcnt(0)                        vmcnt(0) & vscnt(0)
5323
5324                                                            - Could be split into           - Could be split into
5325                                                              separate s_waitcnt              separate s_waitcnt
5326                                                              vmcnt(0)                        vmcnt(0), s_waitcnt
5327                                                              and s_waitcnt                   vscnt(0) and s_waitcnt
5328                                                              lgkmcnt(0) to allow             lgkmcnt(0) to allow
5329                                                              them to be                      them to be
5330                                                              independently moved             independently moved
5331                                                              according to the                according to the
5332                                                              following rules.                following rules.
5333                                                            - waitcnt lgkmcnt(0)            - waitcnt lgkmcnt(0)
5334                                                              must happen after               must happen after
5335                                                              preceding                       preceding
5336                                                              global/generic load             local load
5337                                                              atomic/store                    atomic/store
5338                                                              atomic/atomicrmw                atomic/atomicrmw
5339                                                              with memory                     with memory
5340                                                              ordering of seq_cst             ordering of seq_cst
5341                                                              and with equal or               and with equal or
5342                                                              wider sync scope.               wider sync scope.
5343                                                              (Note that seq_cst              (Note that seq_cst
5344                                                              fences have their               fences have their
5345                                                              own s_waitcnt                   own s_waitcnt
5346                                                              lgkmcnt(0) and so do            lgkmcnt(0) and so do
5347                                                              not need to be                  not need to be
5348                                                              considered.)                    considered.)
5349                                                            - waitcnt vmcnt(0)              - waitcnt vmcnt(0)
5350                                                              must happen after               must happen after
5351                                                              preceding                       preceding
5352                                                              global/generic load             global/generic load
5353                                                              atomic/store                    atomic/
5354                                                              atomic/atomicrmw                atomicrmw-with-return-value
5355                                                              with memory                     with memory
5356                                                              ordering of seq_cst             ordering of seq_cst
5357                                                              and with equal or               and with equal or
5358                                                              wider sync scope.               wider sync scope.
5359                                                              (Note that seq_cst              (Note that seq_cst
5360                                                              fences have their               fences have their
5361                                                              own s_waitcnt                   own s_waitcnt
5362                                                              vmcnt(0) and so do              vmcnt(0) and so do
5363                                                              not need to be                  not need to be
5364                                                              considered.)                    considered.)
5365                                                                                            - waitcnt vscnt(0)
5366                                                                                              Must happen after
5367                                                                                              preceding
5368                                                                                              global/generic store
5369                                                                                              atomic/
5370                                                                                              atomicrmw-no-return-value
5371                                                                                              with memory
5372                                                                                              ordering of seq_cst
5373                                                                                              and with equal or
5374                                                                                              wider sync scope.
5375                                                                                              (Note that seq_cst
5376                                                                                              fences have their
5377                                                                                              own s_waitcnt
5378                                                                                              vscnt(0) and so do
5379                                                                                              not need to be
5380                                                                                              considered.)
5381                                                            - Ensures any                   - Ensures any
5382                                                              preceding                       preceding
5383                                                              sequential                      sequential
5384                                                              consistent global               consistent global
5385                                                              memory instructions             memory instructions
5386                                                              have completed                  have completed
5387                                                              before executing                before executing
5388                                                              this sequentially               this sequentially
5389                                                              consistent                      consistent
5390                                                              instruction. This               instruction. This
5391                                                              prevents reordering             prevents reordering
5392                                                              a seq_cst store                 a seq_cst store
5393                                                              followed by a                   followed by a
5394                                                              seq_cst load. (Note             seq_cst load. (Note
5395                                                              that seq_cst is                 that seq_cst is
5396                                                              stronger than                   stronger than
5397                                                              acquire/release as              acquire/release as
5398                                                              the reordering of               the reordering of
5399                                                              load acquire                    load acquire
5400                                                              followed by a store             followed by a store
5401                                                              release is                      release is
5402                                                              prevented by the                prevented by the
5403                                                              waitcnt of                      waitcnt of
5404                                                              the release, but                the release, but
5405                                                              there is nothing                there is nothing
5406                                                              preventing a store              preventing a store
5407                                                              release followed by             release followed by
5408                                                              load acquire from               load acquire from
5409                                                              competing out of                competing out of
5410                                                              order.)                         order.)
5411
5412                                                          2. *Following                   2. *Following
5413                                                             instructions same as            instructions same as
5414                                                             corresponding load              corresponding load
5415                                                             atomic acquire,                 atomic acquire,
5416                                                             except must generated           except must generated
5417                                                             all instructions even           all instructions even
5418                                                             for OpenCL.*                    for OpenCL.*
5419      store atomic seq_cst      - singlethread - global   *Same as corresponding          *Same as corresponding
5420                                - wavefront    - local    store atomic release,           store atomic release,
5421                                - workgroup    - generic  except must generated           except must generated
5422                                                          all instructions even           all instructions even
5423                                                          for OpenCL.*                    for OpenCL.*
5424      store atomic seq_cst      - agent        - global   *Same as corresponding          *Same as corresponding
5425                                - system       - generic  store atomic release,           store atomic release,
5426                                                          except must generated           except must generated
5427                                                          all instructions even           all instructions even
5428                                                          for OpenCL.*                    for OpenCL.*
5429      atomicrmw    seq_cst      - singlethread - global   *Same as corresponding          *Same as corresponding
5430                                - wavefront    - local    atomicrmw acq_rel,              atomicrmw acq_rel,
5431                                - workgroup    - generic  except must generated           except must generated
5432                                                          all instructions even           all instructions even
5433                                                          for OpenCL.*                    for OpenCL.*
5434      atomicrmw    seq_cst      - agent        - global   *Same as corresponding          *Same as corresponding
5435                                - system       - generic  atomicrmw acq_rel,              atomicrmw acq_rel,
5436                                                          except must generated           except must generated
5437                                                          all instructions even           all instructions even
5438                                                          for OpenCL.*                    for OpenCL.*
5439      fence        seq_cst      - singlethread *none*     *Same as corresponding          *Same as corresponding
5440                                - wavefront               fence acq_rel,                  fence acq_rel,
5441                                - workgroup               except must generated           except must generated
5442                                - agent                   all instructions even           all instructions even
5443                                - system                  for OpenCL.*                    for OpenCL.*
5444      ============ ============ ============== ========== =============================== ==================================
5445
5446 The memory order also adds the single thread optimization constrains defined in
5447 table
5448 :ref:`amdgpu-amdhsa-memory-model-single-thread-optimization-constraints-gfx6-gfx10-table`.
5449
5450   .. table:: AMDHSA Memory Model Single Thread Optimization Constraints GFX6-GFX10
5451      :name: amdgpu-amdhsa-memory-model-single-thread-optimization-constraints-gfx6-gfx10-table
5452
5453      ============ ==============================================================
5454      LLVM Memory  Optimization Constraints
5455      Ordering
5456      ============ ==============================================================
5457      unordered    *none*
5458      monotonic    *none*
5459      acquire      - If a load atomic/atomicrmw then no following load/load
5460                     atomic/store/ store atomic/atomicrmw/fence instruction can
5461                     be moved before the acquire.
5462                   - If a fence then same as load atomic, plus no preceding
5463                     associated fence-paired-atomic can be moved after the fence.
5464      release      - If a store atomic/atomicrmw then no preceding load/load
5465                     atomic/store/ store atomic/atomicrmw/fence instruction can
5466                     be moved after the release.
5467                   - If a fence then same as store atomic, plus no following
5468                     associated fence-paired-atomic can be moved before the
5469                     fence.
5470      acq_rel      Same constraints as both acquire and release.
5471      seq_cst      - If a load atomic then same constraints as acquire, plus no
5472                     preceding sequentially consistent load atomic/store
5473                     atomic/atomicrmw/fence instruction can be moved after the
5474                     seq_cst.
5475                   - If a store atomic then the same constraints as release, plus
5476                     no following sequentially consistent load atomic/store
5477                     atomic/atomicrmw/fence instruction can be moved before the
5478                     seq_cst.
5479                   - If an atomicrmw/fence then same constraints as acq_rel.
5480      ============ ==============================================================
5481
5482 Trap Handler ABI
5483 ~~~~~~~~~~~~~~~~
5484
5485 For code objects generated by AMDGPU backend for HSA [HSA]_ compatible runtimes
5486 (such as ROCm [AMD-ROCm]_), the runtime installs a trap handler that supports
5487 the ``s_trap`` instruction with the following usage:
5488
5489   .. table:: AMDGPU Trap Handler for AMDHSA OS
5490      :name: amdgpu-trap-handler-for-amdhsa-os-table
5491
5492      =================== =============== =============== =======================
5493      Usage               Code Sequence   Trap Handler    Description
5494                                          Inputs
5495      =================== =============== =============== =======================
5496      reserved            ``s_trap 0x00``                 Reserved by hardware.
5497      ``debugtrap(arg)``  ``s_trap 0x01`` ``SGPR0-1``:    Reserved for HSA
5498                                            ``queue_ptr`` ``debugtrap``
5499                                          ``VGPR0``:      intrinsic (not
5500                                            ``arg``       implemented).
5501      ``llvm.trap``       ``s_trap 0x02`` ``SGPR0-1``:    Causes dispatch to be
5502                                            ``queue_ptr`` terminated and its
5503                                                          associated queue put
5504                                                          into the error state.
5505      ``llvm.debugtrap``  ``s_trap 0x03``                 - If debugger not
5506                                                            installed then
5507                                                            behaves as a
5508                                                            no-operation. The
5509                                                            trap handler is
5510                                                            entered and
5511                                                            immediately returns
5512                                                            to continue
5513                                                            execution of the
5514                                                            wavefront.
5515                                                          - If the debugger is
5516                                                            installed, causes
5517                                                            the debug trap to be
5518                                                            reported by the
5519                                                            debugger and the
5520                                                            wavefront is put in
5521                                                            the halt state until
5522                                                            resumed by the
5523                                                            debugger.
5524      reserved            ``s_trap 0x04``                 Reserved.
5525      reserved            ``s_trap 0x05``                 Reserved.
5526      reserved            ``s_trap 0x06``                 Reserved.
5527      debugger breakpoint ``s_trap 0x07``                 Reserved for debugger
5528                                                          breakpoints.
5529      reserved            ``s_trap 0x08``                 Reserved.
5530      reserved            ``s_trap 0xfe``                 Reserved.
5531      reserved            ``s_trap 0xff``                 Reserved.
5532      =================== =============== =============== =======================
5533
5534 AMDPAL
5535 ------
5536
5537 This section provides code conventions used when the target triple OS is
5538 ``amdpal`` (see :ref:`amdgpu-target-triples`) for passing runtime parameters
5539 from the application/runtime to each invocation of a hardware shader. These
5540 parameters include both generic, application-controlled parameters called
5541 *user data* as well as system-generated parameters that are a product of the
5542 draw or dispatch execution.
5543
5544 User Data
5545 ~~~~~~~~~
5546
5547 Each hardware stage has a set of 32-bit *user data registers* which can be
5548 written from a command buffer and then loaded into SGPRs when waves are launched
5549 via a subsequent dispatch or draw operation. This is the way most arguments are
5550 passed from the application/runtime to a hardware shader.
5551
5552 Compute User Data
5553 ~~~~~~~~~~~~~~~~~
5554
5555 Compute shader user data mappings are simpler than graphics shaders, and have a
5556 fixed mapping.
5557
5558 Note that there are always 10 available *user data entries* in registers -
5559 entries beyond that limit must be fetched from memory (via the spill table
5560 pointer) by the shader.
5561
5562   .. table:: PAL Compute Shader User Data Registers
5563      :name: pal-compute-user-data-registers
5564
5565      ============= ================================
5566      User Register Description
5567      ============= ================================
5568      0             Global Internal Table (32-bit pointer)
5569      1             Per-Shader Internal Table (32-bit pointer)
5570      2 - 11        Application-Controlled User Data (10 32-bit values)
5571      12            Spill Table (32-bit pointer)
5572      13 - 14       Thread Group Count (64-bit pointer)
5573      15            GDS Range
5574      ============= ================================
5575
5576 Graphics User Data
5577 ~~~~~~~~~~~~~~~~~~
5578
5579 Graphics pipelines support a much more flexible user data mapping:
5580
5581   .. table:: PAL Graphics Shader User Data Registers
5582      :name: pal-graphics-user-data-registers
5583
5584      ============= ================================
5585      User Register Description
5586      ============= ================================
5587      0             Global Internal Table (32-bit pointer)
5588      +             Per-Shader Internal Table (32-bit pointer)
5589      + 1-15        Application Controlled User Data
5590                    (1-15 Contiguous 32-bit Values in Registers)
5591      +             Spill Table (32-bit pointer)
5592      +             Draw Index (First Stage Only)
5593      +             Vertex Offset (First Stage Only)
5594      +             Instance Offset (First Stage Only)
5595      ============= ================================
5596
5597   The placement of the global internal table remains fixed in the first *user
5598   data SGPR register*. Otherwise all parameters are optional, and can be mapped
5599   to any desired *user data SGPR register*, with the following regstrictions:
5600
5601   * Draw Index, Vertex Offset, and Instance Offset can only be used by the first
5602     activehardware stage in a graphics pipeline (i.e. where the API vertex
5603     shader runs).
5604
5605   * Application-controlled user data must be mapped into a contiguous range of
5606     user data registers.
5607
5608   * The application-controlled user data range supports compaction remapping, so
5609     only *entries* that are actually consumed by the shader must be assigned to
5610     corresponding *registers*. Note that in order to support an efficient runtime
5611     implementation, the remapping must pack *registers* in the same order as
5612     *entries*, with unused *entries* removed.
5613
5614 .. _pal_global_internal_table:
5615
5616 Global Internal Table
5617 ~~~~~~~~~~~~~~~~~~~~~
5618
5619 The global internal table is a table of *shader resource descriptors* (SRDs) that
5620 define how certain engine-wide, runtime-managed resources should be accessed
5621 from a shader. The majority of these resources have HW-defined formats, and it
5622 is up to the compiler to write/read data as required by the target hardware.
5623
5624 The following table illustrates the required format:
5625
5626   .. table:: PAL Global Internal Table
5627      :name: pal-git-table
5628
5629      ============= ================================
5630      Offset        Description
5631      ============= ================================
5632      0-3           Graphics Scratch SRD
5633      4-7           Compute Scratch SRD
5634      8-11          ES/GS Ring Output SRD
5635      12-15         ES/GS Ring Input SRD
5636      16-19         GS/VS Ring Output #0
5637      20-23         GS/VS Ring Output #1
5638      24-27         GS/VS Ring Output #2
5639      28-31         GS/VS Ring Output #3
5640      32-35         GS/VS Ring Input SRD
5641      36-39         Tessellation Factor Buffer SRD
5642      40-43         Off-Chip LDS Buffer SRD
5643      44-47         Off-Chip Param Cache Buffer SRD
5644      48-51         Sample Position Buffer SRD
5645      52            vaRange::ShadowDescriptorTable High Bits
5646      ============= ================================
5647
5648   The pointer to the global internal table passed to the shader as user data
5649   is a 32-bit pointer. The top 32 bits should be assumed to be the same as
5650   the top 32 bits of the pipeline, so the shader may use the program
5651   counter's top 32 bits.
5652
5653 Unspecified OS
5654 --------------
5655
5656 This section provides code conventions used when the target triple OS is
5657 empty (see :ref:`amdgpu-target-triples`).
5658
5659 Trap Handler ABI
5660 ~~~~~~~~~~~~~~~~
5661
5662 For code objects generated by AMDGPU backend for non-amdhsa OS, the runtime does
5663 not install a trap handler. The ``llvm.trap`` and ``llvm.debugtrap``
5664 instructions are handled as follows:
5665
5666   .. table:: AMDGPU Trap Handler for Non-AMDHSA OS
5667      :name: amdgpu-trap-handler-for-non-amdhsa-os-table
5668
5669      =============== =============== ===========================================
5670      Usage           Code Sequence   Description
5671      =============== =============== ===========================================
5672      llvm.trap       s_endpgm        Causes wavefront to be terminated.
5673      llvm.debugtrap  *none*          Compiler warning given that there is no
5674                                      trap handler installed.
5675      =============== =============== ===========================================
5676
5677 Source Languages
5678 ================
5679
5680 .. _amdgpu-opencl:
5681
5682 OpenCL
5683 ------
5684
5685 When the language is OpenCL the following differences occur:
5686
5687 1. The OpenCL memory model is used (see :ref:`amdgpu-amdhsa-memory-model`).
5688 2. The AMDGPU backend appends additional arguments to the kernel's explicit
5689    arguments for the AMDHSA OS (see
5690    :ref:`opencl-kernel-implicit-arguments-appended-for-amdhsa-os-table`).
5691 3. Additional metadata is generated
5692    (see :ref:`amdgpu-amdhsa-code-object-metadata`).
5693
5694   .. table:: OpenCL kernel implicit arguments appended for AMDHSA OS
5695      :name: opencl-kernel-implicit-arguments-appended-for-amdhsa-os-table
5696
5697      ======== ==== ========= ===========================================
5698      Position Byte Byte      Description
5699               Size Alignment
5700      ======== ==== ========= ===========================================
5701      1        8    8         OpenCL Global Offset X
5702      2        8    8         OpenCL Global Offset Y
5703      3        8    8         OpenCL Global Offset Z
5704      4        8    8         OpenCL address of printf buffer
5705      5        8    8         OpenCL address of virtual queue used by
5706                              enqueue_kernel.
5707      6        8    8         OpenCL address of AqlWrap struct used by
5708                              enqueue_kernel.
5709      7        8    8         Pointer argument used for Multi-gird
5710                              synchronization.
5711      ======== ==== ========= ===========================================
5712
5713 .. _amdgpu-hcc:
5714
5715 HCC
5716 ---
5717
5718 When the language is HCC the following differences occur:
5719
5720 1. The HSA memory model is used (see :ref:`amdgpu-amdhsa-memory-model`).
5721
5722 .. _amdgpu-assembler:
5723
5724 Assembler
5725 ---------
5726
5727 AMDGPU backend has LLVM-MC based assembler which is currently in development.
5728 It supports AMDGCN GFX6-GFX10.
5729
5730 This section describes general syntax for instructions and operands.
5731
5732 Instructions
5733 ~~~~~~~~~~~~
5734
5735 .. toctree::
5736    :hidden:
5737
5738    AMDGPU/AMDGPUAsmGFX7
5739    AMDGPU/AMDGPUAsmGFX8
5740    AMDGPU/AMDGPUAsmGFX9
5741    AMDGPU/AMDGPUAsmGFX10
5742    AMDGPUModifierSyntax
5743    AMDGPUOperandSyntax
5744    AMDGPUInstructionSyntax
5745    AMDGPUInstructionNotation
5746
5747 An instruction has the following :doc:`syntax<AMDGPUInstructionSyntax>`:
5748
5749     ``<``\ *opcode*\ ``>    <``\ *operand0*\ ``>, <``\ *operand1*\ ``>,...    <``\ *modifier0*\ ``> <``\ *modifier1*\ ``>...``
5750
5751 :doc:`Operands<AMDGPUOperandSyntax>` are normally comma-separated while
5752 :doc:`modifiers<AMDGPUModifierSyntax>` are space-separated.
5753
5754 The order of *operands* and *modifiers* is fixed.
5755 Most *modifiers* are optional and may be omitted.
5756
5757 See detailed instruction syntax description for :doc:`GFX7<AMDGPU/AMDGPUAsmGFX7>`,
5758 :doc:`GFX8<AMDGPU/AMDGPUAsmGFX8>`, :doc:`GFX9<AMDGPU/AMDGPUAsmGFX9>`
5759 and :doc:`GFX10<AMDGPU/AMDGPUAsmGFX10>`.
5760
5761 Note that features under development are not included in this description.
5762
5763 For more information about instructions, their semantics and supported combinations of
5764 operands, refer to one of instruction set architecture manuals
5765 [AMD-GCN-GFX6]_, [AMD-GCN-GFX7]_, [AMD-GCN-GFX8]_, [AMD-GCN-GFX9]_ and
5766 [AMD-GCN-GFX10]_.
5767
5768 Operands
5769 ~~~~~~~~
5770
5771 Detailed description of operands may be found :doc:`here<AMDGPUOperandSyntax>`.
5772
5773 Modifiers
5774 ~~~~~~~~~
5775
5776 Detailed description of modifiers may be found :doc:`here<AMDGPUModifierSyntax>`.
5777
5778 Instruction Examples
5779 ~~~~~~~~~~~~~~~~~~~~
5780
5781 DS
5782 ++
5783
5784 .. code-block:: nasm
5785
5786   ds_add_u32 v2, v4 offset:16
5787   ds_write_src2_b64 v2 offset0:4 offset1:8
5788   ds_cmpst_f32 v2, v4, v6
5789   ds_min_rtn_f64 v[8:9], v2, v[4:5]
5790
5791
5792 For full list of supported instructions, refer to "LDS/GDS instructions" in ISA Manual.
5793
5794 FLAT
5795 ++++
5796
5797 .. code-block:: nasm
5798
5799   flat_load_dword v1, v[3:4]
5800   flat_store_dwordx3 v[3:4], v[5:7]
5801   flat_atomic_swap v1, v[3:4], v5 glc
5802   flat_atomic_cmpswap v1, v[3:4], v[5:6] glc slc
5803   flat_atomic_fmax_x2 v[1:2], v[3:4], v[5:6] glc
5804
5805 For full list of supported instructions, refer to "FLAT instructions" in ISA Manual.
5806
5807 MUBUF
5808 +++++
5809
5810 .. code-block:: nasm
5811
5812   buffer_load_dword v1, off, s[4:7], s1
5813   buffer_store_dwordx4 v[1:4], v2, ttmp[4:7], s1 offen offset:4 glc tfe
5814   buffer_store_format_xy v[1:2], off, s[4:7], s1
5815   buffer_wbinvl1
5816   buffer_atomic_inc v1, v2, s[8:11], s4 idxen offset:4 slc
5817
5818 For full list of supported instructions, refer to "MUBUF Instructions" in ISA Manual.
5819
5820 SMRD/SMEM
5821 +++++++++
5822
5823 .. code-block:: nasm
5824
5825   s_load_dword s1, s[2:3], 0xfc
5826   s_load_dwordx8 s[8:15], s[2:3], s4
5827   s_load_dwordx16 s[88:103], s[2:3], s4
5828   s_dcache_inv_vol
5829   s_memtime s[4:5]
5830
5831 For full list of supported instructions, refer to "Scalar Memory Operations" in ISA Manual.
5832
5833 SOP1
5834 ++++
5835
5836 .. code-block:: nasm
5837
5838   s_mov_b32 s1, s2
5839   s_mov_b64 s[0:1], 0x80000000
5840   s_cmov_b32 s1, 200
5841   s_wqm_b64 s[2:3], s[4:5]
5842   s_bcnt0_i32_b64 s1, s[2:3]
5843   s_swappc_b64 s[2:3], s[4:5]
5844   s_cbranch_join s[4:5]
5845
5846 For full list of supported instructions, refer to "SOP1 Instructions" in ISA Manual.
5847
5848 SOP2
5849 ++++
5850
5851 .. code-block:: nasm
5852
5853   s_add_u32 s1, s2, s3
5854   s_and_b64 s[2:3], s[4:5], s[6:7]
5855   s_cselect_b32 s1, s2, s3
5856   s_andn2_b32 s2, s4, s6
5857   s_lshr_b64 s[2:3], s[4:5], s6
5858   s_ashr_i32 s2, s4, s6
5859   s_bfm_b64 s[2:3], s4, s6
5860   s_bfe_i64 s[2:3], s[4:5], s6
5861   s_cbranch_g_fork s[4:5], s[6:7]
5862
5863 For full list of supported instructions, refer to "SOP2 Instructions" in ISA Manual.
5864
5865 SOPC
5866 ++++
5867
5868 .. code-block:: nasm
5869
5870   s_cmp_eq_i32 s1, s2
5871   s_bitcmp1_b32 s1, s2
5872   s_bitcmp0_b64 s[2:3], s4
5873   s_setvskip s3, s5
5874
5875 For full list of supported instructions, refer to "SOPC Instructions" in ISA Manual.
5876
5877 SOPP
5878 ++++
5879
5880 .. code-block:: nasm
5881
5882   s_barrier
5883   s_nop 2
5884   s_endpgm
5885   s_waitcnt 0 ; Wait for all counters to be 0
5886   s_waitcnt vmcnt(0) & expcnt(0) & lgkmcnt(0) ; Equivalent to above
5887   s_waitcnt vmcnt(1) ; Wait for vmcnt counter to be 1.
5888   s_sethalt 9
5889   s_sleep 10
5890   s_sendmsg 0x1
5891   s_sendmsg sendmsg(MSG_INTERRUPT)
5892   s_trap 1
5893
5894 For full list of supported instructions, refer to "SOPP Instructions" in ISA Manual.
5895
5896 Unless otherwise mentioned, little verification is performed on the operands
5897 of SOPP Instructions, so it is up to the programmer to be familiar with the
5898 range or acceptable values.
5899
5900 VALU
5901 ++++
5902
5903 For vector ALU instruction opcodes (VOP1, VOP2, VOP3, VOPC, VOP_DPP, VOP_SDWA),
5904 the assembler will automatically use optimal encoding based on its operands.
5905 To force specific encoding, one can add a suffix to the opcode of the instruction:
5906
5907 * _e32 for 32-bit VOP1/VOP2/VOPC
5908 * _e64 for 64-bit VOP3
5909 * _dpp for VOP_DPP
5910 * _sdwa for VOP_SDWA
5911
5912 VOP1/VOP2/VOP3/VOPC examples:
5913
5914 .. code-block:: nasm
5915
5916   v_mov_b32 v1, v2
5917   v_mov_b32_e32 v1, v2
5918   v_nop
5919   v_cvt_f64_i32_e32 v[1:2], v2
5920   v_floor_f32_e32 v1, v2
5921   v_bfrev_b32_e32 v1, v2
5922   v_add_f32_e32 v1, v2, v3
5923   v_mul_i32_i24_e64 v1, v2, 3
5924   v_mul_i32_i24_e32 v1, -3, v3
5925   v_mul_i32_i24_e32 v1, -100, v3
5926   v_addc_u32 v1, s[0:1], v2, v3, s[2:3]
5927   v_max_f16_e32 v1, v2, v3
5928
5929 VOP_DPP examples:
5930
5931 .. code-block:: nasm
5932
5933   v_mov_b32 v0, v0 quad_perm:[0,2,1,1]
5934   v_sin_f32 v0, v0 row_shl:1 row_mask:0xa bank_mask:0x1 bound_ctrl:0
5935   v_mov_b32 v0, v0 wave_shl:1
5936   v_mov_b32 v0, v0 row_mirror
5937   v_mov_b32 v0, v0 row_bcast:31
5938   v_mov_b32 v0, v0 quad_perm:[1,3,0,1] row_mask:0xa bank_mask:0x1 bound_ctrl:0
5939   v_add_f32 v0, v0, |v0| row_shl:1 row_mask:0xa bank_mask:0x1 bound_ctrl:0
5940   v_max_f16 v1, v2, v3 row_shl:1 row_mask:0xa bank_mask:0x1 bound_ctrl:0
5941
5942 VOP_SDWA examples:
5943
5944 .. code-block:: nasm
5945
5946   v_mov_b32 v1, v2 dst_sel:BYTE_0 dst_unused:UNUSED_PRESERVE src0_sel:DWORD
5947   v_min_u32 v200, v200, v1 dst_sel:WORD_1 dst_unused:UNUSED_PAD src0_sel:BYTE_1 src1_sel:DWORD
5948   v_sin_f32 v0, v0 dst_unused:UNUSED_PAD src0_sel:WORD_1
5949   v_fract_f32 v0, |v0| dst_sel:DWORD dst_unused:UNUSED_PAD src0_sel:WORD_1
5950   v_cmpx_le_u32 vcc, v1, v2 src0_sel:BYTE_2 src1_sel:WORD_0
5951
5952 For full list of supported instructions, refer to "Vector ALU instructions".
5953
5954 .. TODO
5955    Remove once we switch to code object v3 by default.
5956
5957 .. _amdgpu-amdhsa-assembler-predefined-symbols-v2:
5958
5959 Code Object V2 Predefined Symbols (-mattr=-code-object-v3)
5960 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5961
5962 .. warning:: Code Object V2 is not the default code object version emitted by
5963   this version of LLVM. For a description of the predefined symbols available
5964   with the default configuration (Code Object V3) see
5965   :ref:`amdgpu-amdhsa-assembler-predefined-symbols-v3`.
5966
5967 The AMDGPU assembler defines and updates some symbols automatically. These
5968 symbols do not affect code generation.
5969
5970 .option.machine_version_major
5971 +++++++++++++++++++++++++++++
5972
5973 Set to the GFX major generation number of the target being assembled for. For
5974 example, when assembling for a "GFX9" target this will be set to the integer
5975 value "9". The possible GFX major generation numbers are presented in
5976 :ref:`amdgpu-processors`.
5977
5978 .option.machine_version_minor
5979 +++++++++++++++++++++++++++++
5980
5981 Set to the GFX minor generation number of the target being assembled for. For
5982 example, when assembling for a "GFX810" target this will be set to the integer
5983 value "1". The possible GFX minor generation numbers are presented in
5984 :ref:`amdgpu-processors`.
5985
5986 .option.machine_version_stepping
5987 ++++++++++++++++++++++++++++++++
5988
5989 Set to the GFX stepping generation number of the target being assembled for.
5990 For example, when assembling for a "GFX704" target this will be set to the
5991 integer value "4". The possible GFX stepping generation numbers are presented
5992 in :ref:`amdgpu-processors`.
5993
5994 .kernel.vgpr_count
5995 ++++++++++++++++++
5996
5997 Set to zero each time a
5998 :ref:`amdgpu-amdhsa-assembler-directive-amdgpu_hsa_kernel` directive is
5999 encountered. At each instruction, if the current value of this symbol is less
6000 than or equal to the maximum VPGR number explicitly referenced within that
6001 instruction then the symbol value is updated to equal that VGPR number plus
6002 one.
6003
6004 .kernel.sgpr_count
6005 ++++++++++++++++++
6006
6007 Set to zero each time a
6008 :ref:`amdgpu-amdhsa-assembler-directive-amdgpu_hsa_kernel` directive is
6009 encountered. At each instruction, if the current value of this symbol is less
6010 than or equal to the maximum VPGR number explicitly referenced within that
6011 instruction then the symbol value is updated to equal that SGPR number plus
6012 one.
6013
6014 .. _amdgpu-amdhsa-assembler-directives-v2:
6015
6016 Code Object V2 Directives (-mattr=-code-object-v3)
6017 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6018
6019 .. warning:: Code Object V2 is not the default code object version emitted by
6020   this version of LLVM. For a description of the directives supported with
6021   the default configuration (Code Object V3) see
6022   :ref:`amdgpu-amdhsa-assembler-directives-v3`.
6023
6024 AMDGPU ABI defines auxiliary data in output code object. In assembly source,
6025 one can specify them with assembler directives.
6026
6027 .hsa_code_object_version major, minor
6028 +++++++++++++++++++++++++++++++++++++
6029
6030 *major* and *minor* are integers that specify the version of the HSA code
6031 object that will be generated by the assembler.
6032
6033 .hsa_code_object_isa [major, minor, stepping, vendor, arch]
6034 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6035
6036
6037 *major*, *minor*, and *stepping* are all integers that describe the instruction
6038 set architecture (ISA) version of the assembly program.
6039
6040 *vendor* and *arch* are quoted strings.  *vendor* should always be equal to
6041 "AMD" and *arch* should always be equal to "AMDGPU".
6042
6043 By default, the assembler will derive the ISA version, *vendor*, and *arch*
6044 from the value of the -mcpu option that is passed to the assembler.
6045
6046 .. _amdgpu-amdhsa-assembler-directive-amdgpu_hsa_kernel:
6047
6048 .amdgpu_hsa_kernel (name)
6049 +++++++++++++++++++++++++
6050
6051 This directives specifies that the symbol with given name is a kernel entry point
6052 (label) and the object should contain corresponding symbol of type STT_AMDGPU_HSA_KERNEL.
6053
6054 .amd_kernel_code_t
6055 ++++++++++++++++++
6056
6057 This directive marks the beginning of a list of key / value pairs that are used
6058 to specify the amd_kernel_code_t object that will be emitted by the assembler.
6059 The list must be terminated by the *.end_amd_kernel_code_t* directive.  For
6060 any amd_kernel_code_t values that are unspecified a default value will be
6061 used.  The default value for all keys is 0, with the following exceptions:
6062
6063 - *amd_code_version_major* defaults to 1.
6064 - *amd_kernel_code_version_minor* defaults to 2.
6065 - *amd_machine_kind* defaults to 1.
6066 - *amd_machine_version_major*, *machine_version_minor*, and
6067   *amd_machine_version_stepping* are derived from the value of the -mcpu option
6068   that is passed to the assembler.
6069 - *kernel_code_entry_byte_offset* defaults to 256.
6070 - *wavefront_size* defaults 6 for all targets before GFX10. For GFX10 onwards
6071   defaults to 6 if target feature ``wavefrontsize64`` is enabled, otherwise 5.
6072   Note that wavefront size is specified as a power of two, so a value of **n**
6073   means a size of 2^ **n**.
6074 - *call_convention* defaults to -1.
6075 - *kernarg_segment_alignment*, *group_segment_alignment*, and
6076   *private_segment_alignment* default to 4. Note that alignments are specified
6077   as a power of 2, so a value of **n** means an alignment of 2^ **n**.
6078 - *enable_wgp_mode* defaults to 1 if target feature ``cumode`` is disabled for
6079   GFX10 onwards.
6080 - *enable_mem_ordered* defaults to 1 for GFX10 onwards.
6081
6082 The *.amd_kernel_code_t* directive must be placed immediately after the
6083 function label and before any instructions.
6084
6085 For a full list of amd_kernel_code_t keys, refer to AMDGPU ABI document,
6086 comments in lib/Target/AMDGPU/AmdKernelCodeT.h and test/CodeGen/AMDGPU/hsa.s.
6087
6088 .. _amdgpu-amdhsa-assembler-example-v2:
6089
6090 Code Object V2 Example Source Code (-mattr=-code-object-v3)
6091 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6092
6093 .. warning:: Code Object V2 is not the default code object version emitted by
6094   this version of LLVM. For a description of the directives supported with
6095   the default configuration (Code Object V3) see
6096   :ref:`amdgpu-amdhsa-assembler-example-v3`.
6097
6098 Here is an example of a minimal assembly source file, defining one HSA kernel:
6099
6100 .. code-block:: none
6101
6102    .hsa_code_object_version 1,0
6103    .hsa_code_object_isa
6104
6105    .hsatext
6106    .globl  hello_world
6107    .p2align 8
6108    .amdgpu_hsa_kernel hello_world
6109
6110    hello_world:
6111
6112       .amd_kernel_code_t
6113          enable_sgpr_kernarg_segment_ptr = 1
6114          is_ptr64 = 1
6115          compute_pgm_rsrc1_vgprs = 0
6116          compute_pgm_rsrc1_sgprs = 0
6117          compute_pgm_rsrc2_user_sgpr = 2
6118          compute_pgm_rsrc1_wgp_mode = 0
6119          compute_pgm_rsrc1_mem_ordered = 0
6120          compute_pgm_rsrc1_fwd_progress = 1
6121      .end_amd_kernel_code_t
6122
6123      s_load_dwordx2 s[0:1], s[0:1] 0x0
6124      v_mov_b32 v0, 3.14159
6125      s_waitcnt lgkmcnt(0)
6126      v_mov_b32 v1, s0
6127      v_mov_b32 v2, s1
6128      flat_store_dword v[1:2], v0
6129      s_endpgm
6130    .Lfunc_end0:
6131         .size   hello_world, .Lfunc_end0-hello_world
6132
6133 .. _amdgpu-amdhsa-assembler-predefined-symbols-v3:
6134
6135 Code Object V3 Predefined Symbols (-mattr=+code-object-v3)
6136 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6137
6138 The AMDGPU assembler defines and updates some symbols automatically. These
6139 symbols do not affect code generation.
6140
6141 .amdgcn.gfx_generation_number
6142 +++++++++++++++++++++++++++++
6143
6144 Set to the GFX major generation number of the target being assembled for. For
6145 example, when assembling for a "GFX9" target this will be set to the integer
6146 value "9". The possible GFX major generation numbers are presented in
6147 :ref:`amdgpu-processors`.
6148
6149 .amdgcn.gfx_generation_minor
6150 ++++++++++++++++++++++++++++
6151
6152 Set to the GFX minor generation number of the target being assembled for. For
6153 example, when assembling for a "GFX810" target this will be set to the integer
6154 value "1". The possible GFX minor generation numbers are presented in
6155 :ref:`amdgpu-processors`.
6156
6157 .amdgcn.gfx_generation_stepping
6158 +++++++++++++++++++++++++++++++
6159
6160 Set to the GFX stepping generation number of the target being assembled for.
6161 For example, when assembling for a "GFX704" target this will be set to the
6162 integer value "4". The possible GFX stepping generation numbers are presented
6163 in :ref:`amdgpu-processors`.
6164
6165 .. _amdgpu-amdhsa-assembler-symbol-next_free_vgpr:
6166
6167 .amdgcn.next_free_vgpr
6168 ++++++++++++++++++++++
6169
6170 Set to zero before assembly begins. At each instruction, if the current value
6171 of this symbol is less than or equal to the maximum VGPR number explicitly
6172 referenced within that instruction then the symbol value is updated to equal
6173 that VGPR number plus one.
6174
6175 May be used to set the `.amdhsa_next_free_vpgr` directive in
6176 :ref:`amdhsa-kernel-directives-table`.
6177
6178 May be set at any time, e.g. manually set to zero at the start of each kernel.
6179
6180 .. _amdgpu-amdhsa-assembler-symbol-next_free_sgpr:
6181
6182 .amdgcn.next_free_sgpr
6183 ++++++++++++++++++++++
6184
6185 Set to zero before assembly begins. At each instruction, if the current value
6186 of this symbol is less than or equal the maximum SGPR number explicitly
6187 referenced within that instruction then the symbol value is updated to equal
6188 that SGPR number plus one.
6189
6190 May be used to set the `.amdhsa_next_free_spgr` directive in
6191 :ref:`amdhsa-kernel-directives-table`.
6192
6193 May be set at any time, e.g. manually set to zero at the start of each kernel.
6194
6195 .. _amdgpu-amdhsa-assembler-directives-v3:
6196
6197 Code Object V3 Directives (-mattr=+code-object-v3)
6198 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6199
6200 Directives which begin with ``.amdgcn`` are valid for all ``amdgcn``
6201 architecture processors, and are not OS-specific. Directives which begin with
6202 ``.amdhsa`` are specific to ``amdgcn`` architecture processors when the
6203 ``amdhsa`` OS is specified. See :ref:`amdgpu-target-triples` and
6204 :ref:`amdgpu-processors`.
6205
6206 .amdgcn_target <target>
6207 +++++++++++++++++++++++
6208
6209 Optional directive which declares the target supported by the containing
6210 assembler source file. Valid values are described in
6211 :ref:`amdgpu-amdhsa-code-object-target-identification`. Used by the assembler
6212 to validate command-line options such as ``-triple``, ``-mcpu``, and those
6213 which specify target features.
6214
6215 .amdhsa_kernel <name>
6216 +++++++++++++++++++++
6217
6218 Creates a correctly aligned AMDHSA kernel descriptor and a symbol,
6219 ``<name>.kd``, in the current location of the current section. Only valid when
6220 the OS is ``amdhsa``. ``<name>`` must be a symbol that labels the first
6221 instruction to execute, and does not need to be previously defined.
6222
6223 Marks the beginning of a list of directives used to generate the bytes of a
6224 kernel descriptor, as described in :ref:`amdgpu-amdhsa-kernel-descriptor`.
6225 Directives which may appear in this list are described in
6226 :ref:`amdhsa-kernel-directives-table`. Directives may appear in any order, must
6227 be valid for the target being assembled for, and cannot be repeated. Directives
6228 support the range of values specified by the field they reference in
6229 :ref:`amdgpu-amdhsa-kernel-descriptor`. If a directive is not specified, it is
6230 assumed to have its default value, unless it is marked as "Required", in which
6231 case it is an error to omit the directive. This list of directives is
6232 terminated by an ``.end_amdhsa_kernel`` directive.
6233
6234   .. table:: AMDHSA Kernel Assembler Directives
6235      :name: amdhsa-kernel-directives-table
6236
6237      ======================================================== =================== ============ ===================
6238      Directive                                                Default             Supported On Description
6239      ======================================================== =================== ============ ===================
6240      ``.amdhsa_group_segment_fixed_size``                     0                   GFX6-GFX10   Controls GROUP_SEGMENT_FIXED_SIZE in
6241                                                                                                :ref:`amdgpu-amdhsa-kernel-descriptor-gfx6-gfx10-table`.
6242      ``.amdhsa_private_segment_fixed_size``                   0                   GFX6-GFX10   Controls PRIVATE_SEGMENT_FIXED_SIZE in
6243                                                                                                :ref:`amdgpu-amdhsa-kernel-descriptor-gfx6-gfx10-table`.
6244      ``.amdhsa_user_sgpr_private_segment_buffer``             0                   GFX6-GFX10   Controls ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER in
6245                                                                                                :ref:`amdgpu-amdhsa-kernel-descriptor-gfx6-gfx10-table`.
6246      ``.amdhsa_user_sgpr_dispatch_ptr``                       0                   GFX6-GFX10   Controls ENABLE_SGPR_DISPATCH_PTR in
6247                                                                                                :ref:`amdgpu-amdhsa-kernel-descriptor-gfx6-gfx10-table`.
6248      ``.amdhsa_user_sgpr_queue_ptr``                          0                   GFX6-GFX10   Controls ENABLE_SGPR_QUEUE_PTR in
6249                                                                                                :ref:`amdgpu-amdhsa-kernel-descriptor-gfx6-gfx10-table`.
6250      ``.amdhsa_user_sgpr_kernarg_segment_ptr``                0                   GFX6-GFX10   Controls ENABLE_SGPR_KERNARG_SEGMENT_PTR in
6251                                                                                                :ref:`amdgpu-amdhsa-kernel-descriptor-gfx6-gfx10-table`.
6252      ``.amdhsa_user_sgpr_dispatch_id``                        0                   GFX6-GFX10   Controls ENABLE_SGPR_DISPATCH_ID in
6253                                                                                                :ref:`amdgpu-amdhsa-kernel-descriptor-gfx6-gfx10-table`.
6254      ``.amdhsa_user_sgpr_flat_scratch_init``                  0                   GFX6-GFX10   Controls ENABLE_SGPR_FLAT_SCRATCH_INIT in
6255                                                                                                :ref:`amdgpu-amdhsa-kernel-descriptor-gfx6-gfx10-table`.
6256      ``.amdhsa_user_sgpr_private_segment_size``               0                   GFX6-GFX10   Controls ENABLE_SGPR_PRIVATE_SEGMENT_SIZE in
6257                                                                                                :ref:`amdgpu-amdhsa-kernel-descriptor-gfx6-gfx10-table`.
6258      ``.amdhsa_wavefront_size32``                             Target              GFX10        Controls ENABLE_WAVEFRONT_SIZE32 in
6259                                                               Feature                          :ref:`amdgpu-amdhsa-kernel-descriptor-gfx6-gfx10-table`.
6260                                                               Specific
6261                                                               (-wavefrontsize64)
6262      ``.amdhsa_system_sgpr_private_segment_wavefront_offset`` 0                   GFX6-GFX10   Controls ENABLE_SGPR_PRIVATE_SEGMENT_WAVEFRONT_OFFSET in
6263                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6264      ``.amdhsa_system_sgpr_workgroup_id_x``                   1                   GFX6-GFX10   Controls ENABLE_SGPR_WORKGROUP_ID_X in
6265                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6266      ``.amdhsa_system_sgpr_workgroup_id_y``                   0                   GFX6-GFX10   Controls ENABLE_SGPR_WORKGROUP_ID_Y in
6267                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6268      ``.amdhsa_system_sgpr_workgroup_id_z``                   0                   GFX6-GFX10   Controls ENABLE_SGPR_WORKGROUP_ID_Z in
6269                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6270      ``.amdhsa_system_sgpr_workgroup_info``                   0                   GFX6-GFX10   Controls ENABLE_SGPR_WORKGROUP_INFO in
6271                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6272      ``.amdhsa_system_vgpr_workitem_id``                      0                   GFX6-GFX10   Controls ENABLE_VGPR_WORKITEM_ID in
6273                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6274                                                                                                Possible values are defined in
6275                                                                                                :ref:`amdgpu-amdhsa-system-vgpr-work-item-id-enumeration-values-table`.
6276      ``.amdhsa_next_free_vgpr``                               Required            GFX6-GFX10   Maximum VGPR number explicitly referenced, plus one.
6277                                                                                                Used to calculate GRANULATED_WORKITEM_VGPR_COUNT in
6278                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6279      ``.amdhsa_next_free_sgpr``                               Required            GFX6-GFX10   Maximum SGPR number explicitly referenced, plus one.
6280                                                                                                Used to calculate GRANULATED_WAVEFRONT_SGPR_COUNT in
6281                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6282      ``.amdhsa_reserve_vcc``                                  1                   GFX6-GFX10   Whether the kernel may use the special VCC SGPR.
6283                                                                                                Used to calculate GRANULATED_WAVEFRONT_SGPR_COUNT in
6284                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6285      ``.amdhsa_reserve_flat_scratch``                         1                   GFX7-GFX10   Whether the kernel may use flat instructions to access
6286                                                                                                scratch memory. Used to calculate
6287                                                                                                GRANULATED_WAVEFRONT_SGPR_COUNT in
6288                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6289      ``.amdhsa_reserve_xnack_mask``                           Target              GFX8-GFX10   Whether the kernel may trigger XNACK replay.
6290                                                               Feature                          Used to calculate GRANULATED_WAVEFRONT_SGPR_COUNT in
6291                                                               Specific                         :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6292                                                               (+xnack)
6293      ``.amdhsa_float_round_mode_32``                          0                   GFX6-GFX10   Controls FLOAT_ROUND_MODE_32 in
6294                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6295                                                                                                Possible values are defined in
6296                                                                                                :ref:`amdgpu-amdhsa-floating-point-rounding-mode-enumeration-values-table`.
6297      ``.amdhsa_float_round_mode_16_64``                       0                   GFX6-GFX10   Controls FLOAT_ROUND_MODE_16_64 in
6298                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6299                                                                                                Possible values are defined in
6300                                                                                                :ref:`amdgpu-amdhsa-floating-point-rounding-mode-enumeration-values-table`.
6301      ``.amdhsa_float_denorm_mode_32``                         0                   GFX6-GFX10   Controls FLOAT_DENORM_MODE_32 in
6302                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6303                                                                                                Possible values are defined in
6304                                                                                                :ref:`amdgpu-amdhsa-floating-point-denorm-mode-enumeration-values-table`.
6305      ``.amdhsa_float_denorm_mode_16_64``                      3                   GFX6-GFX10   Controls FLOAT_DENORM_MODE_16_64 in
6306                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6307                                                                                                Possible values are defined in
6308                                                                                                :ref:`amdgpu-amdhsa-floating-point-denorm-mode-enumeration-values-table`.
6309      ``.amdhsa_dx10_clamp``                                   1                   GFX6-GFX10   Controls ENABLE_DX10_CLAMP in
6310                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6311      ``.amdhsa_ieee_mode``                                    1                   GFX6-GFX10   Controls ENABLE_IEEE_MODE in
6312                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6313      ``.amdhsa_fp16_overflow``                                0                   GFX9-GFX10   Controls FP16_OVFL in
6314                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6315      ``.amdhsa_workgroup_processor_mode``                     Target              GFX10        Controls ENABLE_WGP_MODE in
6316                                                               Feature                          :ref:`amdgpu-amdhsa-kernel-descriptor-gfx6-gfx10-table`.
6317                                                               Specific
6318                                                               (-cumode)
6319      ``.amdhsa_memory_ordered``                               1                   GFX10        Controls MEM_ORDERED in
6320                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6321      ``.amdhsa_forward_progress``                             0                   GFX10        Controls FWD_PROGRESS in
6322                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc1-gfx6-gfx10-table`.
6323      ``.amdhsa_exception_fp_ieee_invalid_op``                 0                   GFX6-GFX10   Controls ENABLE_EXCEPTION_IEEE_754_FP_INVALID_OPERATION in
6324                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6325      ``.amdhsa_exception_fp_denorm_src``                      0                   GFX6-GFX10   Controls ENABLE_EXCEPTION_FP_DENORMAL_SOURCE in
6326                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6327      ``.amdhsa_exception_fp_ieee_div_zero``                   0                   GFX6-GFX10   Controls ENABLE_EXCEPTION_IEEE_754_FP_DIVISION_BY_ZERO in
6328                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6329      ``.amdhsa_exception_fp_ieee_overflow``                   0                   GFX6-GFX10   Controls ENABLE_EXCEPTION_IEEE_754_FP_OVERFLOW in
6330                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6331      ``.amdhsa_exception_fp_ieee_underflow``                  0                   GFX6-GFX10   Controls ENABLE_EXCEPTION_IEEE_754_FP_UNDERFLOW in
6332                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6333      ``.amdhsa_exception_fp_ieee_inexact``                    0                   GFX6-GFX10   Controls ENABLE_EXCEPTION_IEEE_754_FP_INEXACT in
6334                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6335      ``.amdhsa_exception_int_div_zero``                       0                   GFX6-GFX10   Controls ENABLE_EXCEPTION_INT_DIVIDE_BY_ZERO in
6336                                                                                                :ref:`amdgpu-amdhsa-compute_pgm_rsrc2-gfx6-gfx10-table`.
6337      ======================================================== =================== ============ ===================
6338
6339 .amdgpu_metadata
6340 ++++++++++++++++
6341
6342 Optional directive which declares the contents of the ``NT_AMDGPU_METADATA``
6343 note record (see :ref:`amdgpu-elf-note-records-table-v3`).
6344
6345 The contents must be in the [YAML]_ markup format, with the same structure and
6346 semantics described in :ref:`amdgpu-amdhsa-code-object-metadata-v3`.
6347
6348 This directive is terminated by an ``.end_amdgpu_metadata`` directive.
6349
6350 .. _amdgpu-amdhsa-assembler-example-v3:
6351
6352 Code Object V3 Example Source Code (-mattr=+code-object-v3)
6353 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6354
6355 Here is an example of a minimal assembly source file, defining one HSA kernel:
6356
6357 .. code-block:: none
6358
6359   .amdgcn_target "amdgcn-amd-amdhsa--gfx900+xnack" // optional
6360
6361   .text
6362   .globl hello_world
6363   .p2align 8
6364   .type hello_world,@function
6365   hello_world:
6366     s_load_dwordx2 s[0:1], s[0:1] 0x0
6367     v_mov_b32 v0, 3.14159
6368     s_waitcnt lgkmcnt(0)
6369     v_mov_b32 v1, s0
6370     v_mov_b32 v2, s1
6371     flat_store_dword v[1:2], v0
6372     s_endpgm
6373   .Lfunc_end0:
6374     .size   hello_world, .Lfunc_end0-hello_world
6375
6376   .rodata
6377   .p2align 6
6378   .amdhsa_kernel hello_world
6379     .amdhsa_user_sgpr_kernarg_segment_ptr 1
6380     .amdhsa_next_free_vgpr .amdgcn.next_free_vgpr
6381     .amdhsa_next_free_sgpr .amdgcn.next_free_sgpr
6382   .end_amdhsa_kernel
6383
6384   .amdgpu_metadata
6385   ---
6386   amdhsa.version:
6387     - 1
6388     - 0
6389   amdhsa.kernels:
6390     - .name: hello_world
6391       .symbol: hello_world.kd
6392       .kernarg_segment_size: 48
6393       .group_segment_fixed_size: 0
6394       .private_segment_fixed_size: 0
6395       .kernarg_segment_align: 4
6396       .wavefront_size: 64
6397       .sgpr_count: 2
6398       .vgpr_count: 3
6399       .max_flat_workgroup_size: 256
6400   ...
6401   .end_amdgpu_metadata
6402
6403 If an assembly source file contains multiple kernels and/or functions, the
6404 :ref:`amdgpu-amdhsa-assembler-symbol-next_free_vgpr` and
6405 :ref:`amdgpu-amdhsa-assembler-symbol-next_free_sgpr` symbols may be reset using
6406 the ``.set <symbol>, <expression>`` directive. For example, in the case of two
6407 kernels, where ``function1`` is only called from ``kernel1`` it is sufficient
6408 to group the function with the kernel that calls it and reset the symbols
6409 between the two connected components:
6410
6411 .. code-block:: none
6412
6413   .amdgcn_target "amdgcn-amd-amdhsa--gfx900+xnack" // optional
6414
6415   // gpr tracking symbols are implicitly set to zero
6416
6417   .text
6418   .globl kern0
6419   .p2align 8
6420   .type kern0,@function
6421   kern0:
6422     // ...
6423     s_endpgm
6424   .Lkern0_end:
6425     .size   kern0, .Lkern0_end-kern0
6426
6427   .rodata
6428   .p2align 6
6429   .amdhsa_kernel kern0
6430     // ...
6431     .amdhsa_next_free_vgpr .amdgcn.next_free_vgpr
6432     .amdhsa_next_free_sgpr .amdgcn.next_free_sgpr
6433   .end_amdhsa_kernel
6434
6435   // reset symbols to begin tracking usage in func1 and kern1
6436   .set .amdgcn.next_free_vgpr, 0
6437   .set .amdgcn.next_free_sgpr, 0
6438
6439   .text
6440   .hidden func1
6441   .global func1
6442   .p2align 2
6443   .type func1,@function
6444   func1:
6445     // ...
6446     s_setpc_b64 s[30:31]
6447   .Lfunc1_end:
6448   .size func1, .Lfunc1_end-func1
6449
6450   .globl kern1
6451   .p2align 8
6452   .type kern1,@function
6453   kern1:
6454     // ...
6455     s_getpc_b64 s[4:5]
6456     s_add_u32 s4, s4, func1@rel32@lo+4
6457     s_addc_u32 s5, s5, func1@rel32@lo+4
6458     s_swappc_b64 s[30:31], s[4:5]
6459     // ...
6460     s_endpgm
6461   .Lkern1_end:
6462     .size   kern1, .Lkern1_end-kern1
6463
6464   .rodata
6465   .p2align 6
6466   .amdhsa_kernel kern1
6467     // ...
6468     .amdhsa_next_free_vgpr .amdgcn.next_free_vgpr
6469     .amdhsa_next_free_sgpr .amdgcn.next_free_sgpr
6470   .end_amdhsa_kernel
6471
6472 These symbols cannot identify connected components in order to automatically
6473 track the usage for each kernel. However, in some cases careful organization of
6474 the kernels and functions in the source file means there is minimal additional
6475 effort required to accurately calculate GPR usage.
6476
6477 Additional Documentation
6478 ========================
6479
6480 .. [AMD-RADEON-HD-2000-3000] `AMD R6xx shader ISA <http://developer.amd.com/wordpress/media/2012/10/R600_Instruction_Set_Architecture.pdf>`__
6481 .. [AMD-RADEON-HD-4000] `AMD R7xx shader ISA <http://developer.amd.com/wordpress/media/2012/10/R700-Family_Instruction_Set_Architecture.pdf>`__
6482 .. [AMD-RADEON-HD-5000] `AMD Evergreen shader ISA <http://developer.amd.com/wordpress/media/2012/10/AMD_Evergreen-Family_Instruction_Set_Architecture.pdf>`__
6483 .. [AMD-RADEON-HD-6000] `AMD Cayman/Trinity shader ISA <http://developer.amd.com/wordpress/media/2012/10/AMD_HD_6900_Series_Instruction_Set_Architecture.pdf>`__
6484 .. [AMD-GCN-GFX6] `AMD Southern Islands Series ISA <http://developer.amd.com/wordpress/media/2012/12/AMD_Southern_Islands_Instruction_Set_Architecture.pdf>`__
6485 .. [AMD-GCN-GFX7] `AMD Sea Islands Series ISA <http://developer.amd.com/wordpress/media/2013/07/AMD_Sea_Islands_Instruction_Set_Architecture.pdf>`_
6486 .. [AMD-GCN-GFX8] `AMD GCN3 Instruction Set Architecture <http://amd-dev.wpengine.netdna-cdn.com/wordpress/media/2013/12/AMD_GCN3_Instruction_Set_Architecture_rev1.1.pdf>`__
6487 .. [AMD-GCN-GFX9] `AMD "Vega" Instruction Set Architecture <http://developer.amd.com/wordpress/media/2013/12/Vega_Shader_ISA_28July2017.pdf>`__
6488 .. [AMD-GCN-GFX10] AMD "Navi" Instruction Set Architecture *TBA*
6489 .. TODO
6490    ttye Add link when made public.
6491 .. [AMD-ROCm] `ROCm: Open Platform for Development, Discovery and Education Around GPU Computing <http://gpuopen.com/compute-product/rocm/>`__
6492 .. [AMD-ROCm-github] `ROCm github <http://github.com/RadeonOpenCompute>`__
6493 .. [HSA] `Heterogeneous System Architecture (HSA) Foundation <http://www.hsafoundation.com/>`__
6494 .. [ELF] `Executable and Linkable Format (ELF) <http://www.sco.com/developers/gabi/>`__
6495 .. [DWARF] `DWARF Debugging Information Format <http://dwarfstd.org/>`__
6496 .. [YAML] `YAML Ain't Markup Language (YAML™) Version 1.2 <http://www.yaml.org/spec/1.2/spec.html>`__
6497 .. [MsgPack] `Message Pack <http://www.msgpack.org/>`__
6498 .. [OpenCL] `The OpenCL Specification Version 2.0 <http://www.khronos.org/registry/cl/specs/opencl-2.0.pdf>`__
6499 .. [HRF] `Heterogeneous-race-free Memory Models <http://benedictgaster.org/wp-content/uploads/2014/01/asplos269-FINAL.pdf>`__
6500 .. [CLANG-ATTR] `Attributes in Clang <http://clang.llvm.org/docs/AttributeReference.html>`__