ISARef Multi-ISA & Silicon Reference
45,419 ISA items · 105,907 timings · 1,823 microarchs · 16,083 chips · 105 sources
Arch:
Tier:

NVIDIA SASS Encoding & Control Codes

T4 nvidia-gpu guide guide
NVIDIA SASS Encoding & Control Codes Guide (CuAssembler)
SourceNVIDIA Native SASS Binary Encodings (CuAssembler + maxas + envytools) (nvidia_sass)
Reliability TierT4 — Undocumented / Reverse-Engineered
ISA / Silicon ScopeNVIDIA Maxwell–Ampere (SM_50–SM_86)
NVIDIA Maxwell–Ampere (SM_50–SM_86)
License & VersionMIT
master
Upstreamhttps://github.com/cloudcores/CuAssembler
Note: Community documentation of undocumented NVIDIA native SASS machine code.

Summary

Native NVIDIA SASS instruction encoding, scheduling control codes, and pipeline architecture notes.

Syntax / Encoding

NVIDIA SASS Encoding & Control Codes

Reference Notes

CuAssembler User Guide
- [A brief instruction on format of cubin and cuasm](#a-brief-instruction-on-format-of-cubin-and-cuasm)
  - [File Structure](#file-structure)
  - [Sections and Segments](#sections-and-segments)
  - [Basic syntax of cuasm](#basic-syntax-of-cuasm)
  - [Kernel text sections](#kernel-text-sections)
  - [Limitations, Traps and Pitfalls](#limitations-traps-and-pitfalls)
- [How CuAssembler works](#how-cuassembler-works)
  - [Automatic Instruction Encoding](#automatic-instruction-encoding)
  - [Special Treatments of Encoding](#special-treatments-of-encoding)
  - [Instruction Assembler Repository](#instruction-assembler-repository)

# A brief instruction on format of cubin and cuasm

**Cubin** is an ELF format binary, thus most of its file structure will follow the generic conventions of ELF. However, there are also many CUDA specific features involved. **Cuasm** is just a text form of cubin, with most of the cubin information explicitly described with assembly directives. Most of the assembly directives will follow the same semantics of `nvdisasm` (actually most of the cuasm contents are copied from `nvdisasm` output), but there are also some new directives, helping make some information clear and explicit.

## File Structure

An ELF file contains a file header, several sections, none or several program segments. Usually the cubin files are arranged like this:

* **File Header** : The ELF file header will specify some common information, such as identifier magic number, 32bit or 64bit, section header offset, program header offset, etc. For cubin, it will also specify the version of current cubin: virtual architecture version and SM version, toolkit version etc. 
* **Section Data** : The data of every section.
* **Section Header** : Header information of every section. Defines the section name, section offset, size, flags, type, extra info, and linkage with respect to other sections, etc.
* **Segment Header** : Header information of every segment. Defines how the sections will be loaded. **NOTE**: for some `ET_REL` type of ELF, there may be no segment. They are likely to be linked to another cubin for final executable. Currently, only `ET_EXEC` type of ELF is tested, it usually contains 3 segments.

## Sections and Segments

This is a sample layout of cubin sections (debug version of cubin will have much more sections, which are not concerned here):

```
Index Offset   Size ES Align        Type        Flags Link     Info Name
    1     40    418  0  1            STRTAB       0    0        0 .shstrtab
    2    458    783  0  1            STRTAB       0    0        0 .strtab
    3    be0    450 18  8            SYMTAB       0    2       22 .symtab
    4   1030    5c8  0  1          PROGBITS       0    0        0 .debug_frame
    5   15f8    21c  0  4         CUDA_INFO       0    3        0 .nv.info
    6   1814     78  0  4         CUDA_INFO       0    3       1a .nv.info._Z7argtestPiS_S_
    7   188c     4c  0  4         CUDA_INFO       0    3       1b .nv.info._Z11shared_testfPf
    8   18d8     60  0  4         CUDA_INFO       0    3       1c .nv.info._Z11nvinfo_testiiPi
    9   1938     4c  0  4         CUDA_INFO       0    3       1d .nv.info._Z5childPii
    a   1984     5c  0  4         CUDA_INFO       0    3       1e .nv.info._Z10local_testiiPi
    b   19e0     4c  0  4         CUDA_INFO       0    3       1f .nv.info._Z4test6float4PS_
    c   1a30     d0  8  8    CUDA_RELOCINFO       0    0        0 .nv.rel.action
    d   1b00     70 10  8               REL       0    3       1a .rel.text._Z7argtestPiS_S_
    e   1b70     30 18  8              RELA       0    3       1a .rela.text._Z7argtestPiS_S_
    f   1ba0     40 10  8               REL       0    3       1d .rel.text._Z5childPii
   10   1be0     40 10  8               REL       0    3       14 .rel.nv.constant0._Z7argtestPiS_S_
   11   1c20     d0 10  8               REL       0    3        4 .rel.debug_frame
   12   1cf0    141  0  4          PROGBITS       2    0        0 .nv.constant3
   13   1e34     48  0  4          PROGBITS       2    0       1a .nv.constant2._Z7argtestPiS_S_
   14   1e7c    188  0  4          PROGBITS       2    0       1a .nv.constant0._Z7argtestPiS_S_
   15   2004    170  0  4          PROGBITS       2    0       1b .nv.constant0._Z11shared_testfPf
   16   2174    170  0  4          PROGBITS       2    0       1c .nv.constant0._Z11nvinfo_testiiPi
   17   22e4    16c  0  4          PROGBITS       2    0       1d .nv.constant0._Z5childPii
   18   2450    170  0  4          PROGBITS       2    0       1e .nv.constant0._Z10local_testiiPi
   19   25c0    178  0  4          PROGBITS       2    0       1f .nv.constant0._Z4test6float4PS_
   1a   2780    d80  0 80          PROGBITS       6    3 18000023 .text._Z7argtestPiS_S_
   1b   3500    200  0 80          PROGBITS  100006    3  c000029 .text._Z11shared_testfPf
   1c   3700    100  0 80          PROGBITS       6    3  a00002a .text._Z11nvinfo_testiiPi
   1d   3800    280  0 80          PROGBITS       6    3  e00002b .text._Z5childPii
   1e   3a80    180  0 80          PROGBITS       6    3  d00002c .text._Z10local_testiiPi
   1f   3c00    480  0 80          PROGBITS       6    3  a00002d .text._Z4test6float4PS_
   20   4080     5c  0  8          PROGBITS       3    0        0 .nv.global.init
   21   40e0      0  0 10            NOBITS       3    0       1a .nv.shared._Z7argtestPiS_S_
   22   40e0     a0  0  4            NOBITS       3    0        0 .nv.global
   23   40e0   1010  0 10            NOBITS       3    0       1b .nv.shared._Z11shared_testfPf
   24   40e0      0  0 10            NOBITS       3    0       1c .nv.shared._Z11nvinfo_testiiPi
   25   40e0      0  0 10            NOBITS       3    0       1d .nv.shared._Z5childPii
   26   40e0      0  0 10            NOBITS       3    0       1e .nv.shared._Z10local_testiiPi
   27   40e0      0  0 10            NOBITS       3    0       1f .nv.shared._Z4test6float4PS_
```

* `.shstrtab/.strtab/.symtab` : tables for section string, symbol string, and symbol entries. Currently all of them are copied from the original cubin.
* `.nv.info.*` : Some attributes associated with kernels. `cuobjdump -elf *.cubin` can show those information in human readable format. Some of those attributes should be modified when kernel text changed, some can be done by CuAssembler(such as `EIATTR_EXIT_INSTR_OFFSETS`, `EIATTR_CTAIDZ_USED`, etc.), but there are more that cannot. Some attributes are strongly correlated to the offset of some instructions, CuAssembler utilizes a special form of label to handle this kind of attributes, which is necessary to make them work when the instruction sequence is changed.
* `.rel.*` : Relocations. Relocation section should work with the associated section, such as `.rel.abc` to `.abc`. Relocation is a special mechanism which allows runtime initialization of some symbols unknown during compile-time, such as some global constants and function entries. 
* `.nv.constant#.*` : constant memory contents for global constants or kernel dependent constants. The actual arrangement of contant memories may vary with respect to SM version(or even toolkit version?), thus you'd better check it in original SASS code generated with CUDA C. In the example above, constant bank 3 `.nv.constant3` is for global, referred by `c[0x3][###]`. Bank 2 is for compiler generated constants, and bank 0 for kernel arguments and grid/block constants. Both of them are kernel dependent.
* `.text.*` : Kernel instruction sections. Most of the modification should be done to these sections.
* `.nv.shared.*` : Nobits sections. I don't find the shared memory is runtime initializable, thus seems they are only for space allocation.

## Basic syntax of cuasm
Most of the syntax of cuasm will follow the convention of `nvdisasm`, but since `nvdisasm` does not show all the information of cubin. We need more syntax to describe the file more specifically.

**Comments**:

C style comments `/* ... */` and cpp style comments `// ...` are supported. A special form of branch target annotation `(* ... *)` will also be treated as comments. They will all be replaced to spaces. **NOTE**: Currently no cross line comments are allowed, all comments should be in the same line.

**Directives**:
A directive is a predefined keyword, usually starts with a dot `.`. Current list of supported directives defined by `nvdisasm`:

| Directive          | Notes          |
|--------------------|----------------|
| `.headerflags`*     | set ELF header |
| `.elftype`*         | set ELF type |
| `.section`*         | declare a section |
| `.sectioninfo`*     | set section info |
| `.sectionflags`*    | set section flags |
| `.sectionentsize`*  | set section entsize |
| `.align`           | set alignment |
| `.byte`            | emit bytes |
| `.short`           | emit shorts |
| `.word`            | emit word (4B?) |
| `.dword`           | emit dword (8B?) |
| `.type`*           | set symbol type |
| `.size`*           | set symbol size |
| `.global`*          | declare a global symbol |
| `.weak`*            | declare a weak symbol |
| `.zero`            | emit zero bytes |
| `.other`*           | set symbol other  |

Directives annotated with an asterisk are currently not really functional, since the contents are actually copied from original cubin. CuAssembler defined some new internal directives(prefixed with `.__`) that keeps those information unchanged. 

|  |                                |
|--|--------------------------------|
| ELF header | |
| | .__elf_ident_osabi |
| | .__elf_ident_abiversion |
| | .__elf_type |
| | .__elf_machine |
| | .__elf_version |
| | .__elf_entry |
| | .__elf_phoff |
| | .__elf_shoff |
| | .__elf_flags |
| | .__elf_ehsize |
| | .__elf_phentsize |
| | .__elf_phnum |
| | .__elf_shentsize |
| | .__elf_shnum |
| | .__elf_shstrndx |
| Section header | |
| | .__section_name |
| | .__section_type |
| | .__section_flags |
| | .__section_addr |
| | .__section_offset |
| | .__section_size |
| | .__section_link |
| | .__section_info |
| | .__section_entsize |
| Segment header | |
| | .__segment |
| | .__segment_offset |
| | .__segment_vaddr |
| | .__segment_paddr |
| | .__segment_filesz |
| | .__segment_memsz |
| | .__segment_align |
| | .__segment_startsection |
| | .__segment_endsection |

**Labels and Symbols**:

A **label** is just an identifier(may include `.`, `


  
  
  NVIDIA SASS Encoding & Control Codes (nvidia-gpu… — ISARef
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  


  
ISARef Multi-ISA & Silicon Reference
45,419 ISA items · 105,907 timings · 1,823 microarchs · 16,083 chips · 105 sources
Arch:
Tier:
, and any word character) followed by a colon `label:`, such as: ``` _Z10local_testiiPi: .L_203: __cudart_i2opi_f: $str: $_Z7argtestPiS_S_$_Z2f1ii: ``` Labels can be used for reference when the real offset should be filled. A **symbol** is a special label that may be visible externally, i.e., give current address when the module is loaded. A symbol can be defined as: ```asm .global _Z10local_testiiPi .type _Z10local_testiiPi,@function .size _Z10local_testiiPi,(.L_203 - _Z10local_testiiPi) .other _Z10local_testiiPi,@"STO_CUDA_ENTRY STV_DEFAULT" _Z10local_testiiPi: ``` The last line is actually a label(with **same identifier**) that tells the location of the symbol. Symbols without corresponding label are usually defined externally, such as `vprintf` and some other internal non-inline device functions. Every symbol has an entry in `.symtab` section. `cuobjdump -elf *.cubin` can show those entries in human readable form. **CAUTION**: 1. There are far too many treatments needed for different types of symbols. I don't want to follow those tedious or even troublesome treatments(and probably hidden convention privately defined by NVIDIA). Since most of those symbols can be prepared by CUDA C, I just copied them from original cubin, but still keeping those statements legal yet nonfunctional. 2. Cubin of ELF type `ET_REL` may have more types of symbol, probably for later linking? It's quite difficult to support them all, thus `ET_REL` type of ELF will not be supported. ## Kernel text sections Kernel text